1 /* Copyright (C) 1996, 1997 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
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
, 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
)
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 __set_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
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
249 return getanswer_r (&host_buffer
, n
, name
, type
, result
, buffer
, buflen
,
255 _nss_dns_gethostbyname_r (const char *name
, struct hostent
*result
,
256 char *buffer
, size_t buflen
, int *h_errnop
)
258 enum nss_status status
= NSS_STATUS_NOTFOUND
;
260 if (_res
.options
& RES_USE_INET6
)
261 status
= _nss_dns_gethostbyname2_r (name
, AF_INET6
, result
, buffer
,
263 if (status
== NSS_STATUS_NOTFOUND
)
264 status
= _nss_dns_gethostbyname2_r (name
, AF_INET
, result
, buffer
,
272 _nss_dns_gethostbyaddr_r (const char *addr
, int len
, int af
,
273 struct hostent
*result
, char *buffer
, size_t buflen
,
276 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
277 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
278 const u_char
*uaddr
= (const u_char
*)addr
;
281 char *aliases
[MAX_NR_ALIASES
];
282 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
283 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
285 } *host_data
= (struct host_data
*) buffer
;
286 querybuf host_buffer
;
287 char qbuf
[MAXDNAME
+1], *qp
;
290 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
291 (bcmp (uaddr
, mapped
, sizeof mapped
) == 0
292 || bcmp (uaddr
, tunnelled
, sizeof tunnelled
) == 0))
295 addr
+= sizeof mapped
;
296 uaddr
+= sizeof mapped
;
310 __set_errno (EAFNOSUPPORT
);
311 *h_errnop
= NETDB_INTERNAL
;
312 return NSS_STATUS_UNAVAIL
;
316 __set_errno (EAFNOSUPPORT
);
317 *h_errnop
= NETDB_INTERNAL
;
318 return NSS_STATUS_UNAVAIL
;
324 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", (uaddr
[3] & 0xff),
325 (uaddr
[2] & 0xff), (uaddr
[1] & 0xff), (uaddr
[0] & 0xff));
329 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--)
330 qp
+= sprintf (qp
, "%x.%x.", uaddr
[n
] & 0xf, (uaddr
[n
] >> 4) & 0xf);
331 strcpy(qp
, "ip6.int");
337 n
= res_query (qbuf
, C_IN
, T_PTR
, (u_char
*)host_buffer
.buf
,
340 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
342 status
= getanswer_r (&host_buffer
, n
, qbuf
, T_PTR
, result
, buffer
, buflen
,
344 if (status
!= NSS_STATUS_SUCCESS
)
348 This is
not implemented because it is
not possible to use the current
349 source from bind in a multi
-threaded program
.
352 result
->h_addrtype
= af
;
353 result
->h_length
= len
;
354 bcopy (addr
, host_data
->host_addr
, len
);
355 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
356 host_data
->h_addr_ptrs
[1] = NULL
;
357 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
))
359 map_v4v6_address ((char *) host_data
->host_addr
,
360 (char *) host_data
->host_addr
);
361 result
->h_addrtype
= AF_INET6
;
362 result
->h_length
= IN6ADDRSZ
;
364 *h_errnop
= NETDB_SUCCESS
;
365 return NSS_STATUS_SUCCESS
;
369 static enum nss_status
370 getanswer_r (const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
371 struct hostent
*result
, char *buffer
, size_t buflen
,
376 char *aliases
[MAX_NR_ALIASES
];
377 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
378 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
380 } *host_data
= (struct host_data
*) buffer
;
381 int linebuflen
= buflen
- offsetof (struct host_data
, linebuffer
);
382 register const HEADER
*hp
;
383 const u_char
*end_of_message
, *cp
;
384 int n
, ancount
, qdcount
;
385 int haveanswer
, had_error
;
386 char *bp
, **ap
, **hap
;
389 int (*name_ok
) __P ((const char *));
392 result
->h_name
= NULL
;
393 end_of_message
= answer
->buf
+ anslen
;
404 return NSS_STATUS_UNAVAIL
; /* XXX should be abort(); */
408 * find first satisfactory answer
411 bp
= host_data
->linebuffer
;
412 ancount
= ntohs (hp
->ancount
);
413 qdcount
= ntohs (hp
->qdcount
);
414 cp
= answer
->buf
+ HFIXEDSZ
;
417 *h_errnop
= NO_RECOVERY
;
418 return NSS_STATUS_UNAVAIL
;
421 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
422 if (n
< 0 || (*name_ok
) (bp
) == 0)
424 *h_errnop
= NO_RECOVERY
;
425 return NSS_STATUS_UNAVAIL
;
429 if (qtype
== T_A
|| qtype
== T_AAAA
)
431 /* res_send() has already verified that the query name is the
432 * same as the one we sent; this just gets the expanded name
433 * (i.e., with the succeeding search-domain tacked on).
435 n
= strlen (bp
) + 1; /* for the \0 */
439 /* The qname can be abbreviated, but h_name is now absolute. */
440 qname
= result
->h_name
;
443 ap
= host_data
->aliases
;
445 result
->h_aliases
= host_data
->aliases
;
446 hap
= host_data
->h_addr_ptrs
;
448 result
->h_addr_list
= host_data
->h_addr_ptrs
;
452 while (ancount
-- > 0 && cp
< end_of_message
&& had_error
== 0)
456 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
457 if (n
< 0 || (*name_ok
) (bp
) == 0)
463 type
= _getshort (cp
);
464 cp
+= INT16SZ
; /* type */
465 class = _getshort(cp
);
466 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
468 cp
+= INT16SZ
; /* len */
471 /* XXX - debug? syslog? */
473 continue; /* XXX - had_error++ ? */
476 if ((qtype
==T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
)
478 if (ap
>= &host_data
->aliases
[MAX_NR_ALIASES
- 1])
480 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
481 if (n
< 0 || (*name_ok
) (tbuf
) == 0)
489 n
= strlen (bp
) + 1; /* For the \0. */
492 /* Get canonical name. */
493 n
= strlen (tbuf
) + 1; /* For the \0. */
499 strcpy (bp
, tbuf
); /* Cannot overflow. */
506 if (qtype
== T_PTR
&& type
== T_CNAME
)
508 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
509 if (n
< 0 || res_hnok (tbuf
) == 0)
515 /* Get canonical name. */
516 n
= strlen (tbuf
) + 1; /* For the \0. */
522 strcpy (bp
, tbuf
); /* Cannot overflow. */
530 syslog (LOG_NOTICE
| LOG_AUTH
,
531 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
532 qname
, p_class (C_IN
), p_type (qtype
), p_type (type
));
534 continue; /* XXX - had_error++ ? */
540 if (strcasecmp (tname
, bp
) != 0)
542 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, qname
, bp
);
544 continue; /* XXX - had_error++ ? */
546 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
547 if (n
< 0 || res_hnok (bp
) == 0)
552 #if MULTI_PTRS_ARE_ALIASES
556 else if (ap
< &host_data
->aliases
[MAXALIASES
-1])
562 n
= strlen (bp
) + 1; /* for the \0 */
569 if (_res
.options
& RES_USE_INET6
)
571 n
= strlen (bp
) + 1; /* for the \0 */
574 map_v4v6_hostent (result
, &bp
, &linebuflen
);
576 *h_errnop
= NETDB_SUCCESS
;
577 return NSS_STATUS_SUCCESS
;
581 if (strcasecmp (result
->h_name
, bp
) != 0)
583 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, result
->h_name
, bp
);
585 continue; /* XXX - had_error++ ? */
587 if (n
!= result
->h_length
)
597 nn
= strlen (bp
) + 1; /* for the \0 */
602 bp
+= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
609 if (hap
>= &host_data
->h_addr_ptrs
[MAX_NR_ADDRS
-1])
614 bcopy (cp
, *hap
++ = bp
, n
);
630 #if defined(RESOLVSORT)
632 * Note: we sort even if host can take only one address
633 * in its return structures - should give it the "best"
634 * address in that case, not some random one
636 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
637 addrsort (host_data
->h_addr_ptrs
, haveanswer
);
638 #endif /*RESOLVSORT*/
640 if (result
->h_name
== NULL
)
642 n
= strlen (qname
) + 1; /* For the \0. */
645 strcpy (bp
, qname
); /* Cannot overflow. */
651 if (_res
.options
& RES_USE_INET6
)
652 map_v4v6_hostent (result
, &bp
, &linebuflen
);
653 *h_errnop
= NETDB_SUCCESS
;
654 return NSS_STATUS_SUCCESS
;
657 *h_errnop
= TRY_AGAIN
;
658 return NSS_STATUS_TRYAGAIN
;