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
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 1024
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 *errnop
, int *h_errnop
);
126 _nss_dns_gethostbyname2_r (const char *name
, int af
, struct hostent
*result
,
127 char *buffer
, size_t buflen
, int *errnop
,
130 querybuf host_buffer
;
144 *h_errnop
= NETDB_INTERNAL
;
145 *errnop
= EAFNOSUPPORT
;
146 return NSS_STATUS_UNAVAIL
;
149 result
->h_addrtype
= af
;
150 result
->h_length
= size
;
153 * if there aren't any dots, it could be a user-level alias.
154 * this is also done in res_query() since we are not the only
155 * function that looks up host names.
157 if (strchr (name
, '.') == NULL
&& (cp
= __hostalias (name
)) != NULL
)
160 n
= res_search (name
, C_IN
, type
, host_buffer
.buf
, sizeof (host_buffer
.buf
));
165 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
168 return getanswer_r (&host_buffer
, n
, name
, type
, result
, buffer
, buflen
,
174 _nss_dns_gethostbyname_r (const char *name
, struct hostent
*result
,
175 char *buffer
, size_t buflen
, int *errnop
,
178 enum nss_status status
= NSS_STATUS_NOTFOUND
;
180 if (_res
.options
& RES_USE_INET6
)
181 status
= _nss_dns_gethostbyname2_r (name
, AF_INET6
, result
, buffer
,
182 buflen
, errnop
, h_errnop
);
183 if (status
== NSS_STATUS_NOTFOUND
)
184 status
= _nss_dns_gethostbyname2_r (name
, AF_INET
, result
, buffer
,
185 buflen
, errnop
, h_errnop
);
192 _nss_dns_gethostbyaddr_r (const char *addr
, int len
, int af
,
193 struct hostent
*result
, char *buffer
, size_t buflen
,
194 int *errnop
, int *h_errnop
)
196 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
197 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
198 const u_char
*uaddr
= (const u_char
*)addr
;
201 char *aliases
[MAX_NR_ALIASES
];
202 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
203 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
205 } *host_data
= (struct host_data
*) buffer
;
206 querybuf host_buffer
;
207 char qbuf
[MAXDNAME
+1], *qp
;
210 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
211 (memcmp (uaddr
, mapped
, sizeof mapped
) == 0
212 || memcmp (uaddr
, tunnelled
, sizeof tunnelled
) == 0))
215 addr
+= sizeof mapped
;
216 uaddr
+= sizeof mapped
;
230 *errnop
= EAFNOSUPPORT
;
231 *h_errnop
= NETDB_INTERNAL
;
232 return NSS_STATUS_UNAVAIL
;
236 *errnop
= EAFNOSUPPORT
;
237 *h_errnop
= NETDB_INTERNAL
;
238 return NSS_STATUS_UNAVAIL
;
244 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", (uaddr
[3] & 0xff),
245 (uaddr
[2] & 0xff), (uaddr
[1] & 0xff), (uaddr
[0] & 0xff));
249 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--)
250 qp
+= sprintf (qp
, "%x.%x.", uaddr
[n
] & 0xf, (uaddr
[n
] >> 4) & 0xf);
251 strcpy (qp
, "ip6.int");
257 n
= res_query (qbuf
, C_IN
, T_PTR
, (u_char
*)host_buffer
.buf
,
263 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
266 status
= getanswer_r (&host_buffer
, n
, qbuf
, T_PTR
, result
, buffer
, buflen
,
268 if (status
!= NSS_STATUS_SUCCESS
)
276 This is
not implemented because it is
not possible to use the current
277 source from bind in a multi
-threaded program
.
280 result
->h_addrtype
= af
;
281 result
->h_length
= len
;
282 memcpy (host_data
->host_addr
, addr
, len
);
283 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
284 host_data
->h_addr_ptrs
[1] = NULL
;
285 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
))
287 map_v4v6_address ((char *) host_data
->host_addr
,
288 (char *) host_data
->host_addr
);
289 result
->h_addrtype
= AF_INET6
;
290 result
->h_length
= IN6ADDRSZ
;
292 *h_errnop
= NETDB_SUCCESS
;
293 return NSS_STATUS_SUCCESS
;
297 static enum nss_status
298 getanswer_r (const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
299 struct hostent
*result
, char *buffer
, size_t buflen
,
300 int *errnop
, int *h_errnop
)
304 char *aliases
[MAX_NR_ALIASES
];
305 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
306 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
308 } *host_data
= (struct host_data
*) buffer
;
309 int linebuflen
= buflen
- offsetof (struct host_data
, linebuffer
);
310 register const HEADER
*hp
;
311 const u_char
*end_of_message
, *cp
;
312 int n
, ancount
, qdcount
;
313 int haveanswer
, had_error
;
314 char *bp
, **ap
, **hap
;
317 int (*name_ok
) __P ((const char *));
320 result
->h_name
= NULL
;
321 end_of_message
= answer
->buf
+ anslen
;
332 return NSS_STATUS_UNAVAIL
; /* XXX should be abort(); */
336 * find first satisfactory answer
339 bp
= host_data
->linebuffer
;
340 ancount
= ntohs (hp
->ancount
);
341 qdcount
= ntohs (hp
->qdcount
);
342 cp
= answer
->buf
+ HFIXEDSZ
;
345 *h_errnop
= NO_RECOVERY
;
346 return NSS_STATUS_UNAVAIL
;
349 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
350 if (n
< 0 || (*name_ok
) (bp
) == 0)
352 if (errno
== EMSGSIZE
)
354 /* There is not enough room in the input buffer. */
356 *h_errnop
= NETDB_INTERNAL
;
361 *h_errnop
= NO_RECOVERY
;
363 return NSS_STATUS_UNAVAIL
;
367 if (qtype
== T_A
|| qtype
== T_AAAA
)
369 /* res_send() has already verified that the query name is the
370 * same as the one we sent; this just gets the expanded name
371 * (i.e., with the succeeding search-domain tacked on).
373 n
= strlen (bp
) + 1; /* for the \0 */
374 if (n
>= MAXHOSTNAMELEN
)
376 *h_errnop
= NO_RECOVERY
;
377 return NSS_STATUS_TRYAGAIN
;
382 /* The qname can be abbreviated, but h_name is now absolute. */
383 qname
= result
->h_name
;
386 ap
= host_data
->aliases
;
388 result
->h_aliases
= host_data
->aliases
;
389 hap
= host_data
->h_addr_ptrs
;
391 result
->h_addr_list
= host_data
->h_addr_ptrs
;
395 while (ancount
-- > 0 && cp
< end_of_message
&& had_error
== 0)
399 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
400 if (n
< 0 || (*name_ok
) (bp
) == 0)
406 type
= _getshort (cp
);
407 cp
+= INT16SZ
; /* type */
408 class = _getshort(cp
);
409 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
411 cp
+= INT16SZ
; /* len */
414 /* XXX - debug? syslog? */
416 continue; /* XXX - had_error++ ? */
419 if ((qtype
==T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
)
421 if (ap
>= &host_data
->aliases
[MAX_NR_ALIASES
- 1])
423 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
424 if (n
< 0 || (*name_ok
) (tbuf
) == 0)
432 n
= strlen (bp
) + 1; /* For the \0. */
433 if (n
>= MAXHOSTNAMELEN
)
440 /* Get canonical name. */
441 n
= strlen (tbuf
) + 1; /* For the \0. */
442 if ((size_t) n
> buflen
|| n
>= MAXHOSTNAMELEN
)
447 result
->h_name
= strcpy (bp
, tbuf
); /* Cannot overflow. */
453 if (qtype
== T_PTR
&& type
== T_CNAME
)
455 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
456 if (n
< 0 || res_dnok (tbuf
) == 0)
462 /* Get canonical name. */
463 n
= strlen (tbuf
) + 1; /* For the \0. */
464 if ((size_t) n
> buflen
|| n
>= MAXHOSTNAMELEN
)
469 tname
= strcpy (bp
, tbuf
); /* Cannot overflow. */
474 if (type
== T_SIG
|| type
== T_KEY
|| type
== T_NXT
)
476 /* We don't support DNSSEC yet. For now, ignore the record
477 and send a low priority message to syslog. */
478 syslog (LOG_DEBUG
| LOG_AUTH
,
479 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
480 qname
, p_class (C_IN
), p_type(qtype
), p_type (type
));
486 syslog (LOG_NOTICE
| LOG_AUTH
,
487 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
488 qname
, p_class (C_IN
), p_type (qtype
), p_type (type
));
490 continue; /* XXX - had_error++ ? */
496 if (strcasecmp (tname
, bp
) != 0)
498 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, qname
, bp
);
500 continue; /* XXX - had_error++ ? */
502 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
503 if (n
< 0 || res_hnok (bp
) == 0)
508 #if MULTI_PTRS_ARE_ALIASES
512 else if (ap
< &host_data
->aliases
[MAXALIASES
-1])
518 n
= strlen (bp
) + 1; /* for the \0 */
519 if (n
>= MAXHOSTNAMELEN
)
530 if (_res
.options
& RES_USE_INET6
)
532 n
= strlen (bp
) + 1; /* for the \0 */
533 if (n
>= MAXHOSTNAMELEN
)
540 map_v4v6_hostent (result
, &bp
, &linebuflen
);
542 *h_errnop
= NETDB_SUCCESS
;
543 return NSS_STATUS_SUCCESS
;
547 if (strcasecmp (result
->h_name
, bp
) != 0)
549 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, result
->h_name
, bp
);
551 continue; /* XXX - had_error++ ? */
553 if (n
!= result
->h_length
)
563 nn
= strlen (bp
) + 1; /* for the \0 */
568 bp
+= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
575 if (hap
>= &host_data
->h_addr_ptrs
[MAX_NR_ADDRS
-1])
580 memcpy (*hap
++ = bp
, cp
, n
);
596 #if defined RESOLVSORT
598 * Note: we sort even if host can take only one address
599 * in its return structures - should give it the "best"
600 * address in that case, not some random one
602 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
603 addrsort (host_data
->h_addr_ptrs
, haveanswer
);
604 #endif /*RESOLVSORT*/
606 if (result
->h_name
== NULL
)
608 n
= strlen (qname
) + 1; /* For the \0. */
609 if (n
> linebuflen
|| n
>= MAXHOSTNAMELEN
)
611 result
->h_name
= strcpy (bp
, qname
); /* Cannot overflow. */
616 if (_res
.options
& RES_USE_INET6
)
617 map_v4v6_hostent (result
, &bp
, &linebuflen
);
618 *h_errnop
= NETDB_SUCCESS
;
619 return NSS_STATUS_SUCCESS
;
622 *h_errnop
= NO_RECOVERY
;
623 return NSS_STATUS_TRYAGAIN
;