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 `getnetnamadr.c' from
21 the bind package and it has the following copyright. */
23 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
24 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
26 * Permission to use, copy, modify, and distribute this software for any
27 * purpose with or without fee is hereby granted, provided that the above
28 * copyright notice and this permission notice appear in all copies.
31 * Copyright (c) 1983, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 #include <sys/syslog.h>
72 #include <netinet/in.h>
73 #include <arpa/inet.h>
74 #include <arpa/nameser.h>
79 /* Get implementation for some internal functions. */
80 #include "../resolv/mapv4v6addr.h"
81 #include "../resolv/mapv4v6hostent.h"
83 /* Maximum number of aliases we allow. */
84 #define MAX_NR_ALIASES 48
85 #define MAX_NR_ADDRS 48
88 # define MAXPACKET PACKETSZ
90 # define MAXPACKET 1024
93 static const char AskedForGot
[] = "\
94 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
97 /* We need this time later. */
98 typedef union querybuf
101 u_char buf
[MAXPACKET
];
105 static enum nss_status
getanswer_r (const querybuf
*answer
, int anslen
,
106 const char *qname
, int qtype
,
107 struct hostent
*result
,
108 char *buffer
, int buflen
, int *h_errnop
);
111 _nss_dns_gethostbyname2_r (const char *name
, int af
, struct hostent
*result
,
112 char *buffer
, int buflen
, int *h_errnop
)
116 char *aliases
[MAX_NR_ALIASES
];
117 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
118 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
120 } *host_data
= (struct host_data
*) buffer
;
121 int linebuflen
= buflen
- offsetof (struct host_data
, linebuffer
);
122 querybuf host_buffer
;
136 *h_errnop
= NETDB_INTERNAL
;
137 errno
= EAFNOSUPPORT
;
138 return NSS_STATUS_UNAVAIL
;
141 result
->h_addrtype
= af
;
142 result
->h_length
= size
;
145 * if there aren't any dots, it could be a user-level alias.
146 * this is also done in res_query() since we are not the only
147 * function that looks up host names.
149 if (strchr (name
, '.') == NULL
&& (cp
= __hostalias (name
)) != NULL
)
153 * disallow names consisting only of digits/dots, unless
156 if (isdigit (name
[0]))
157 for (cp
= name
;; ++cp
)
166 * All-numeric, no dot at the end. Fake up a hostent
167 * as if we'd actually done a lookup.
169 if (inet_pton (af
, name
, host_data
->host_addr
) <= 0)
171 *h_errnop
= HOST_NOT_FOUND
;
172 return NSS_STATUS_NOTFOUND
;
175 bp
= __stpncpy (host_data
->linebuffer
, name
, linebuflen
);
176 host_data
->linebuffer
[linebuflen
- 1] = '\0';
177 linebuflen
-= bp
- host_data
->linebuffer
;
179 result
->h_name
= host_data
->linebuffer
;
180 result
->h_aliases
= host_data
->aliases
;
181 host_data
->aliases
[0] = NULL
;
182 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
183 host_data
->h_addr_ptrs
[1] = NULL
;
184 result
->h_addr_list
= host_data
->h_addr_ptrs
;
186 if (_res
.options
& RES_USE_INET6
)
187 map_v4v6_hostent (result
, &bp
, &linebuflen
);
188 *h_errnop
= NETDB_SUCCESS
;
189 return NSS_STATUS_SUCCESS
;
191 if (!isdigit (*cp
) && *cp
!= '.')
195 n
= res_search (name
, C_IN
, type
, host_buffer
.buf
, sizeof (host_buffer
));
197 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
199 return getanswer_r (&host_buffer
, n
, name
, type
, result
, buffer
, buflen
,
205 _nss_dns_gethostbyname_r (const char *name
, struct hostent
*result
,
206 char *buffer
, int buflen
, int *h_errnop
)
208 enum nss_status status
= NSS_STATUS_NOTFOUND
;
210 if (_res
.options
& RES_USE_INET6
)
211 status
= _nss_dns_gethostbyname2_r (name
, AF_INET6
, result
, buffer
,
213 if (status
== NSS_STATUS_NOTFOUND
)
214 status
= _nss_dns_gethostbyname2_r (name
, AF_INET
, result
, buffer
,
222 _nss_dns_gethostbyaddr_r (const char *addr
, int len
, int af
,
223 struct hostent
*result
, char *buffer
, int buflen
,
226 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
227 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
228 const u_char
*uaddr
= (const u_char
*)addr
;
231 char *aliases
[MAX_NR_ALIASES
];
232 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
233 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
235 } *host_data
= (struct host_data
*) buffer
;
236 querybuf host_buffer
;
237 char qbuf
[MAXDNAME
+1], *qp
;
240 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
241 (bcmp (uaddr
, mapped
, sizeof mapped
) == 0
242 || bcmp (uaddr
, tunnelled
, sizeof tunnelled
) == 0))
245 addr
+= sizeof mapped
;
246 uaddr
+= sizeof mapped
;
260 errno
= EAFNOSUPPORT
;
261 *h_errnop
= NETDB_INTERNAL
;
262 return NSS_STATUS_UNAVAIL
;
266 errno
= EAFNOSUPPORT
;
267 *h_errnop
= NETDB_INTERNAL
;
268 return NSS_STATUS_UNAVAIL
;
274 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", (uaddr
[3] & 0xff),
275 (uaddr
[2] & 0xff), (uaddr
[1] & 0xff), (uaddr
[0] & 0xff));
279 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--)
280 qp
+= sprintf (qp
, "%x.%x.", uaddr
[n
] & 0xf, (uaddr
[n
] >> 4) & 0xf);
281 strcpy(qp
, "ip6.int");
287 n
= res_query (qbuf
, C_IN
, T_PTR
, (u_char
*)host_buffer
.buf
,
290 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
292 status
= getanswer_r (&host_buffer
, n
, qbuf
, T_PTR
, result
, buffer
, buflen
,
294 if (status
!= NSS_STATUS_SUCCESS
)
298 This is
not implemented because it is
not possible to use the current
299 source from bind in a multi
-threaded program
.
302 result
->h_addrtype
= af
;
303 result
->h_length
= len
;
304 bcopy (addr
, host_data
->host_addr
, len
);
305 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
306 host_data
->h_addr_ptrs
[1] = NULL
;
307 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
))
309 map_v4v6_address ((char *) host_data
->host_addr
,
310 (char *) host_data
->host_addr
);
311 result
->h_addrtype
= AF_INET6
;
312 result
->h_length
= IN6ADDRSZ
;
314 *h_errnop
= NETDB_SUCCESS
;
315 return NSS_STATUS_SUCCESS
;
319 static enum nss_status
320 getanswer_r (const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
321 struct hostent
*result
, char *buffer
, int buflen
, int *h_errnop
)
325 char *aliases
[MAX_NR_ALIASES
];
326 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
327 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
329 } *host_data
= (struct host_data
*) buffer
;
330 int linebuflen
= buflen
- offsetof (struct host_data
, linebuffer
);
331 register const HEADER
*hp
;
332 const u_char
*end_of_message
, *cp
;
333 int n
, ancount
, qdcount
;
334 int haveanswer
, had_error
;
335 char *bp
, **ap
, **hap
;
336 char tbuf
[MAXDNAME
+1];
338 int (*name_ok
) __P ((const char *));
341 result
->h_name
= NULL
;
342 end_of_message
= answer
->buf
+ anslen
;
353 return NSS_STATUS_UNAVAIL
; /* XXX should be abort(); */
357 * find first satisfactory answer
360 bp
= host_data
->linebuffer
;
361 ancount
= ntohs (hp
->ancount
);
362 qdcount
= ntohs (hp
->qdcount
);
363 cp
= answer
->buf
+ HFIXEDSZ
;
366 *h_errnop
= NO_RECOVERY
;
367 return NSS_STATUS_UNAVAIL
;
370 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
371 if (n
< 0 || (*name_ok
) (bp
) == 0)
373 *h_errnop
= NO_RECOVERY
;
374 return NSS_STATUS_UNAVAIL
;
378 if (qtype
== T_A
|| qtype
== T_AAAA
)
380 /* res_send() has already verified that the query name is the
381 * same as the one we sent; this just gets the expanded name
382 * (i.e., with the succeeding search-domain tacked on).
384 n
= strlen (bp
) + 1; /* for the \0 */
388 /* The qname can be abbreviated, but h_name is now absolute. */
389 qname
= result
->h_name
;
392 ap
= host_data
->aliases
;
394 result
->h_aliases
= host_data
->aliases
;
395 hap
= host_data
->h_addr_ptrs
;
397 result
->h_addr_list
= host_data
->h_addr_ptrs
;
401 while (ancount
-- > 0 && cp
< end_of_message
&& had_error
== 0)
405 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
406 if (n
< 0 || (*name_ok
) (bp
) == 0)
412 type
= _getshort (cp
);
413 cp
+= INT16SZ
; /* type */
414 class = _getshort(cp
);
415 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
417 cp
+= INT16SZ
; /* len */
420 /* XXX - debug? syslog? */
422 continue; /* XXX - had_error++ ? */
425 if ((qtype
==T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
)
427 if (ap
>= &host_data
->aliases
[MAX_NR_ALIASES
- 1])
429 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
430 if (n
< 0 || (*name_ok
) (tbuf
) == 0)
438 n
= strlen (bp
) + 1; /* For the \0. */
441 /* Get canonical name. */
442 n
= strlen (tbuf
) + 1; /* For the \0. */
455 if (qtype
== T_PTR
&& type
== T_CNAME
)
457 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
458 if (n
< 0 || res_hnok (tbuf
) == 0)
464 /* Get canonical name. */
465 n
= strlen (tbuf
) + 1; /* For the \0. */
479 syslog (LOG_NOTICE
| LOG_AUTH
,
480 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
481 qname
, p_class (C_IN
), p_type (qtype
), p_type (type
));
483 continue; /* XXX - had_error++ ? */
489 if (strcasecmp (tname
, bp
) != 0)
491 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, qname
, bp
);
493 continue; /* XXX - had_error++ ? */
495 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
496 if (n
< 0 || res_hnok (bp
) == 0)
501 #if MULTI_PTRS_ARE_ALIASES
505 else if (ap
< &host_data
->aliases
[MAXALIASES
-1])
511 n
= strlen (bp
) + 1; /* for the \0 */
518 if (_res
.options
& RES_USE_INET6
)
520 n
= strlen (bp
) + 1; /* for the \0 */
523 map_v4v6_hostent (result
, &bp
, &linebuflen
);
525 *h_errnop
= NETDB_SUCCESS
;
526 return NSS_STATUS_SUCCESS
;
530 if (strcasecmp (result
->h_name
, bp
) != 0)
532 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, result
->h_name
, bp
);
534 continue; /* XXX - had_error++ ? */
538 if (n
!= result
->h_length
)
549 nn
= strlen (bp
) + 1; /* for the \0 */
554 bp
+= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
561 if (hap
>= &host_data
->h_addr_ptrs
[MAX_NR_ADDRS
-1])
566 bcopy (cp
, *hap
++ = bp
, n
);
582 #if defined(RESOLVSORT)
584 * Note: we sort even if host can take only one address
585 * in its return structures - should give it the "best"
586 * address in that case, not some random one
588 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
589 addrsort (host_data
->h_addr_ptrs
, haveanswer
);
590 #endif /*RESOLVSORT*/
592 if (result
->h_name
== NULL
)
594 n
= strlen (qname
) + 1; /* For the \0. */
603 if (_res
.options
& RES_USE_INET6
)
604 map_v4v6_hostent (result
, &bp
, &linebuflen
);
605 *h_errnop
= NETDB_SUCCESS
;
606 return NSS_STATUS_SUCCESS
;
609 *h_errnop
= TRY_AGAIN
;
610 return NSS_STATUS_TRYAGAIN
;