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>
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
103 static const char AskedForGot[] = "\
104 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
107 /* We need this time later. */
108 typedef union querybuf
111 u_char buf[MAXPACKET];
115 static enum nss_status getanswer_r (const querybuf *answer, int anslen,
116 const char *qname, int qtype,
117 struct hostent *result, char *buffer,
118 size_t buflen, int *h_errnop);
121 _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
122 char *buffer, size_t buflen, int *h_errnop)
124 querybuf host_buffer;
138 *h_errnop = NETDB_INTERNAL;
139 __set_errno (EAFNOSUPPORT);
140 return NSS_STATUS_UNAVAIL;
143 result->h_addrtype = af;
144 result->h_length = size;
147 * if there aren't any dots, it could be a user-level alias.
148 * this is also done in res_query() since we are not the only
149 * function that looks up host names.
151 if (strchr (name, '.') == NULL && (cp = __hostalias (name)) != NULL)
154 n = res_search (name, C_IN, type, host_buffer.buf, sizeof (host_buffer.buf));
156 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
158 return getanswer_r (&host_buffer, n, name, type, result, buffer, buflen,
164 _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
165 char *buffer, size_t buflen, int *h_errnop)
167 enum nss_status status = NSS_STATUS_NOTFOUND;
169 if (_res.options & RES_USE_INET6)
170 status = _nss_dns_gethostbyname2_r (name, AF_INET6, result, buffer,
172 if (status == NSS_STATUS_NOTFOUND)
173 status = _nss_dns_gethostbyname2_r (name, AF_INET, result, buffer,
181 _nss_dns_gethostbyaddr_r (const char *addr, int len, int af,
182 struct hostent *result, char *buffer, size_t buflen,
185 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
186 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
187 const u_char *uaddr = (const u_char *)addr;
190 char *aliases[MAX_NR_ALIASES];
191 unsigned char host_addr[16]; /* IPv4 or IPv6 */
192 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
194 } *host_data = (struct host_data *) buffer;
195 querybuf host_buffer;
196 char qbuf[MAXDNAME+1], *qp;
199 if (af == AF_INET6 && len == IN6ADDRSZ &&
200 (memcmp (uaddr, mapped, sizeof mapped) == 0
201 || memcmp (uaddr, tunnelled, sizeof tunnelled) == 0))
204 addr += sizeof mapped;
205 uaddr += sizeof mapped;
219 __set_errno (EAFNOSUPPORT);
220 *h_errnop = NETDB_INTERNAL;
221 return NSS_STATUS_UNAVAIL;
225 __set_errno (EAFNOSUPPORT);
226 *h_errnop = NETDB_INTERNAL;
227 return NSS_STATUS_UNAVAIL;
233 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
234 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
238 for (n = IN6ADDRSZ - 1; n >= 0; n--)
239 qp += sprintf (qp, "%x.%x.", uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf);
240 strcpy(qp, "ip6.int");
246 n = res_query (qbuf, C_IN, T_PTR, (u_char *)host_buffer.buf,
249 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
251 status = getanswer_r (&host_buffer, n, qbuf, T_PTR, result, buffer, buflen,
253 if (status != NSS_STATUS_SUCCESS)
257 This is not implemented because it is not possible to use the current
258 source from bind in a multi-threaded program.
261 result->h_addrtype = af;
262 result->h_length = len;
263 memcpy (host_data->host_addr, addr, len);
264 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
265 host_data->h_addr_ptrs[1] = NULL;
266 if (af == AF_INET && (_res.options & RES_USE_INET6))
268 map_v4v6_address ((char *) host_data->host_addr,
269 (char *) host_data->host_addr);
270 result->h_addrtype = AF_INET6;
271 result->h_length = IN6ADDRSZ;
273 *h_errnop = NETDB_SUCCESS;
274 return NSS_STATUS_SUCCESS;
278 static enum nss_status
279 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
280 struct hostent *result, char *buffer, size_t buflen,
285 char *aliases[MAX_NR_ALIASES];
286 unsigned char host_addr[16]; /* IPv4 or IPv6 */
287 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
289 } *host_data = (struct host_data *) buffer;
290 int linebuflen = buflen - offsetof (struct host_data, linebuffer);
291 register const HEADER *hp;
292 const u_char *end_of_message, *cp;
293 int n, ancount, qdcount;
294 int haveanswer, had_error;
295 char *bp, **ap, **hap;
298 int (*name_ok) __P ((const char *));
301 result->h_name = NULL;
302 end_of_message = answer->buf + anslen;
313 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
317 * find first satisfactory answer
320 bp = host_data->linebuffer;
321 ancount = ntohs (hp->ancount);
322 qdcount = ntohs (hp->qdcount);
323 cp = answer->buf + HFIXEDSZ;
326 *h_errnop = NO_RECOVERY;
327 return NSS_STATUS_UNAVAIL;
330 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
331 if (n < 0 || (*name_ok) (bp) == 0)
333 *h_errnop = NO_RECOVERY;
334 return NSS_STATUS_UNAVAIL;
338 if (qtype == T_A || qtype == T_AAAA)
340 /* res_send() has already verified that the query name is the
341 * same as the one we sent; this just gets the expanded name
342 * (i.e., with the succeeding search-domain tacked on).
344 n = strlen (bp) + 1; /* for the \0 */
345 if (n >= MAXHOSTNAMELEN)
347 __set_h_errno (NO_RECOVERY);
348 return NSS_STATUS_TRYAGAIN;
353 /* The qname can be abbreviated, but h_name is now absolute. */
354 qname = result->h_name;
357 ap = host_data->aliases;
359 result->h_aliases = host_data->aliases;
360 hap = host_data->h_addr_ptrs;
362 result->h_addr_list = host_data->h_addr_ptrs;
366 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
370 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
371 if (n < 0 || (*name_ok) (bp) == 0)
377 type = _getshort (cp);
378 cp += INT16SZ; /* type */
379 class = _getshort(cp);
380 cp += INT16SZ + INT32SZ; /* class, TTL */
382 cp += INT16SZ; /* len */
385 /* XXX - debug? syslog? */
387 continue; /* XXX - had_error++ ? */
390 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
392 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
394 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
395 if (n < 0 || (*name_ok) (tbuf) == 0)
403 n = strlen (bp) + 1; /* For the \0. */
404 if (n >= MAXHOSTNAMELEN)
411 /* Get canonical name. */
412 n = strlen (tbuf) + 1; /* For the \0. */
413 if ((size_t) n > buflen || n >= MAXHOSTNAMELEN)
418 strcpy (bp, tbuf); /* Cannot overflow. */
425 if (qtype == T_PTR && type == T_CNAME)
427 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
428 if (n < 0 || res_hnok (tbuf) == 0)
434 /* Get canonical name. */
435 n = strlen (tbuf) + 1; /* For the \0. */
436 if ((size_t) n > buflen || n >= MAXHOSTNAMELEN)
441 strcpy (bp, tbuf); /* Cannot overflow. */
449 syslog (LOG_NOTICE | LOG_AUTH,
450 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
451 qname, p_class (C_IN), p_type (qtype), p_type (type));
453 continue; /* XXX - had_error++ ? */
459 if (strcasecmp (tname, bp) != 0)
461 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
463 continue; /* XXX - had_error++ ? */
465 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
466 if (n < 0 || res_hnok (bp) == 0)
471 #if MULTI_PTRS_ARE_ALIASES
475 else if (ap < &host_data->aliases[MAXALIASES-1])
481 n = strlen (bp) + 1; /* for the \0 */
482 if (n >= MAXHOSTNAMELEN)
493 if (_res.options & RES_USE_INET6)
495 n = strlen (bp) + 1; /* for the \0 */
496 if (n >= MAXHOSTNAMELEN)
503 map_v4v6_hostent (result, &bp, &linebuflen);
505 *h_errnop = NETDB_SUCCESS;
506 return NSS_STATUS_SUCCESS;
510 if (strcasecmp (result->h_name, bp) != 0)
512 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
514 continue; /* XXX - had_error++ ? */
516 if (n != result->h_length)
526 nn = strlen (bp) + 1; /* for the \0 */
531 bp += sizeof (align) - ((u_long) bp % sizeof (align));
538 if (hap >= &host_data->h_addr_ptrs[MAX_NR_ADDRS-1])
543 memcpy (*hap++ = bp, cp, n);
559 #if defined(RESOLVSORT)
561 * Note: we sort even if host can take only one address
562 * in its return structures - should give it the "best"
563 * address in that case, not some random one
565 if (_res.nsort && haveanswer > 1 && qtype == T_A)
566 addrsort (host_data->h_addr_ptrs, haveanswer);
567 #endif /*RESOLVSORT*/
569 if (result->h_name == NULL)
571 n = strlen (qname) + 1; /* For the \0. */
572 if (n > linebuflen || n >= MAXHOSTNAMELEN)
574 strcpy (bp, qname); /* Cannot overflow. */
580 if (_res.options & RES_USE_INET6)
581 map_v4v6_hostent (result, &bp, &linebuflen);
582 *h_errnop = NETDB_SUCCESS;
583 return NSS_STATUS_SUCCESS;
586 *h_errnop = NO_RECOVERY;
587 return NSS_STATUS_TRYAGAIN;