Updated to fedora-glibc-20050627T0850
[glibc.git] / glibc-compat / nss_dns / dns-host.c
blob5db030cde12837108201e69e643bbb77693ef6fe
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
25 * -
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
31 * are met:
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
55 * SUCH DAMAGE.
56 * -
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
73 * SOFTWARE.
74 * -
75 * --Copyright--
78 #include <ctype.h>
79 #include <errno.h>
80 #include <glibc-compat/include/netdb.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <stddef.h>
84 #include <string.h>
85 #include <sys/syslog.h>
87 #include "nsswitch.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
97 #if PACKETSZ > 65536
98 # define MAXPACKET PACKETSZ
99 #else
100 # define MAXPACKET 65536
101 #endif
102 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
103 #ifdef MAXHOSTNAMELEN
104 # undef MAXHOSTNAMELEN
105 #endif
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
115 HEADER hdr;
116 u_char buf[MAXPACKET];
117 } querybuf;
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);
125 enum nss_status
126 _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
127 char *buffer, size_t buflen, int *h_errnop)
129 union
131 querybuf *buf;
132 u_char *ptr;
133 } host_buffer;
134 querybuf *orig_host_buffer;
135 int size, type, n;
136 const char *cp;
137 enum nss_status status;
139 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
141 *h_errnop = NETDB_INTERNAL;
142 return NSS_STATUS_UNAVAIL;
145 switch (af) {
146 case AF_INET:
147 size = INADDRSZ;
148 type = T_A;
149 break;
150 case AF_INET6:
151 size = IN6ADDRSZ;
152 type = T_AAAA;
153 break;
154 default:
155 *h_errnop = NETDB_INTERNAL;
156 __set_errno (EAFNOSUPPORT);
157 return NSS_STATUS_UNAVAIL;
160 result->h_addrtype = af;
161 result->h_length = size;
164 * if there aren't any dots, it could be a user-level alias.
165 * this is also done in res_query() since we are not the only
166 * function that looks up host names.
168 if (strchr (name, '.') == NULL && (cp = __hostalias (name)) != NULL)
169 name = cp;
171 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
173 n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf, 1024,
174 &host_buffer.ptr);
175 if (n < 0)
177 *h_errnop = h_errno;
178 if (host_buffer.buf != orig_host_buffer)
179 free (host_buffer.buf);
180 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
183 status = getanswer_r (host_buffer.buf, n, name, type, result, buffer, buflen,
184 h_errnop);
185 if (host_buffer.buf != orig_host_buffer)
186 free (host_buffer.buf);
187 return status;
191 enum nss_status
192 _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
193 char *buffer, size_t buflen, int *h_errnop)
195 enum nss_status status = NSS_STATUS_NOTFOUND;
197 if (_res.options & RES_USE_INET6)
198 status = _nss_dns_gethostbyname2_r (name, AF_INET6, result, buffer,
199 buflen, h_errnop);
200 if (status == NSS_STATUS_NOTFOUND)
201 status = _nss_dns_gethostbyname2_r (name, AF_INET, result, buffer,
202 buflen, h_errnop);
204 return status;
208 enum nss_status
209 _nss_dns_gethostbyaddr_r (const char *addr, int len, int af,
210 struct hostent *result, char *buffer, size_t buflen,
211 int *h_errnop)
213 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
214 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
215 const u_char *uaddr = (const u_char *)addr;
216 struct host_data
218 char *aliases[MAX_NR_ALIASES];
219 unsigned char host_addr[16]; /* IPv4 or IPv6 */
220 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
221 char linebuffer[0];
222 } *host_data = (struct host_data *) buffer;
223 union
225 querybuf *buf;
226 u_char *ptr;
227 } host_buffer;
228 querybuf *orig_host_buffer;
229 char qbuf[MAXDNAME+1], *qp;
230 int size, n, status;
232 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
234 *h_errnop = NETDB_INTERNAL;
235 return NSS_STATUS_UNAVAIL;
238 if (af == AF_INET6 && len == IN6ADDRSZ &&
239 (memcmp (uaddr, mapped, sizeof mapped) == 0
240 || memcmp (uaddr, tunnelled, sizeof tunnelled) == 0))
242 /* Unmap. */
243 addr += sizeof mapped;
244 uaddr += sizeof mapped;
245 af = AF_INET;
246 len = INADDRSZ;
249 switch (af)
251 case AF_INET:
252 size = INADDRSZ;
253 break;
254 case AF_INET6:
255 size = IN6ADDRSZ;
256 break;
257 default:
258 __set_errno (EAFNOSUPPORT);
259 *h_errnop = NETDB_INTERNAL;
260 return NSS_STATUS_UNAVAIL;
262 if (size != len)
264 __set_errno (EAFNOSUPPORT);
265 *h_errnop = NETDB_INTERNAL;
266 return NSS_STATUS_UNAVAIL;
269 switch (af)
271 case AF_INET:
272 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
273 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
274 break;
275 case AF_INET6:
276 qp = qbuf;
277 for (n = IN6ADDRSZ - 1; n >= 0; n--)
278 qp += sprintf (qp, "%x.%x.", uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf);
279 strcpy(qp, "ip6.int");
280 break;
281 default:
282 /* Cannot happen. */
283 break;
286 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
288 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
289 1024, &host_buffer.ptr);
290 if (n < 0)
292 *h_errnop = h_errno;
293 if (host_buffer.buf != orig_host_buffer)
294 free (host_buffer.buf);
295 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
298 status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen,
299 h_errnop);
300 if (host_buffer.buf != orig_host_buffer)
301 free (host_buffer.buf);
302 if (status != NSS_STATUS_SUCCESS)
304 *h_errnop = h_errno;
305 return status;
308 #ifdef SUNSECURITY
309 This is not implemented because it is not possible to use the current
310 source from bind in a multi-threaded program.
311 #endif
313 result->h_addrtype = af;
314 result->h_length = len;
315 memcpy (host_data->host_addr, addr, len);
316 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
317 host_data->h_addr_ptrs[1] = NULL;
318 if (af == AF_INET && (_res.options & RES_USE_INET6))
320 map_v4v6_address ((char *) host_data->host_addr,
321 (char *) host_data->host_addr);
322 result->h_addrtype = AF_INET6;
323 result->h_length = IN6ADDRSZ;
325 *h_errnop = NETDB_SUCCESS;
326 return NSS_STATUS_SUCCESS;
330 static enum nss_status
331 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
332 struct hostent *result, char *buffer, size_t buflen,
333 int *h_errnop)
335 struct host_data
337 char *aliases[MAX_NR_ALIASES];
338 unsigned char host_addr[16]; /* IPv4 or IPv6 */
339 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
340 char linebuffer[0];
341 } *host_data = (struct host_data *) buffer;
342 int linebuflen = buflen - offsetof (struct host_data, linebuffer);
343 register const HEADER *hp;
344 const u_char *end_of_message, *cp;
345 int n, ancount, qdcount;
346 int haveanswer, had_error;
347 char *bp, **ap, **hap;
348 char tbuf[MAXDNAME];
349 const char *tname;
350 int (*name_ok) (const char *);
352 tname = qname;
353 result->h_name = NULL;
354 end_of_message = answer->buf + anslen;
355 switch (qtype)
357 case T_A:
358 case T_AAAA:
359 name_ok = res_hnok;
360 break;
361 case T_PTR:
362 name_ok = res_dnok;
363 break;
364 default:
365 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
369 * find first satisfactory answer
371 hp = &answer->hdr;
372 bp = host_data->linebuffer;
373 ancount = ntohs (hp->ancount);
374 qdcount = ntohs (hp->qdcount);
375 cp = answer->buf + HFIXEDSZ;
376 if (qdcount != 1)
378 *h_errnop = NO_RECOVERY;
379 return NSS_STATUS_UNAVAIL;
382 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
383 if (n < 0 || (*name_ok) (bp) == 0)
385 *h_errnop = NO_RECOVERY;
386 return NSS_STATUS_UNAVAIL;
388 cp += n + QFIXEDSZ;
390 if (qtype == T_A || qtype == T_AAAA)
392 /* res_send() has already verified that the query name is the
393 * same as the one we sent; this just gets the expanded name
394 * (i.e., with the succeeding search-domain tacked on).
396 n = strlen (bp) + 1; /* for the \0 */
397 if (n >= MAXHOSTNAMELEN)
399 __set_h_errno (NO_RECOVERY);
400 return NSS_STATUS_TRYAGAIN;
402 result->h_name = bp;
403 bp += n;
404 linebuflen -= n;
405 /* The qname can be abbreviated, but h_name is now absolute. */
406 qname = result->h_name;
409 ap = host_data->aliases;
410 *ap = NULL;
411 result->h_aliases = host_data->aliases;
412 hap = host_data->h_addr_ptrs;
413 *hap = NULL;
414 result->h_addr_list = host_data->h_addr_ptrs;
415 haveanswer = 0;
416 had_error = 0;
418 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
420 int type, class;
422 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
423 if (n < 0 || (*name_ok) (bp) == 0)
425 ++had_error;
426 continue;
428 cp += n; /* name */
429 type = _getshort (cp);
430 cp += INT16SZ; /* type */
431 class = _getshort(cp);
432 cp += INT16SZ + INT32SZ; /* class, TTL */
433 n = _getshort(cp);
434 cp += INT16SZ; /* len */
435 if (class != C_IN)
437 /* XXX - debug? syslog? */
438 cp += n;
439 continue; /* XXX - had_error++ ? */
442 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
444 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
445 continue;
446 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
447 if (n < 0 || (*name_ok) (tbuf) == 0)
449 ++had_error;
450 continue;
452 cp += n;
453 /* Store alias. */
454 *ap++ = bp;
455 n = strlen (bp) + 1; /* For the \0. */
456 if (n >= MAXHOSTNAMELEN)
458 ++had_error;
459 continue;
461 bp += n;
462 linebuflen -= n;
463 /* Get canonical name. */
464 n = strlen (tbuf) + 1; /* For the \0. */
465 if ((size_t) n > linebuflen || n >= MAXHOSTNAMELEN)
467 ++had_error;
468 continue;
470 strcpy (bp, tbuf); /* Cannot overflow. */
471 result->h_name = bp;
472 bp += n;
473 linebuflen -= n;
474 continue;
477 if (qtype == T_PTR && type == T_CNAME)
479 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
480 if (n < 0 || res_dnok (tbuf) == 0)
482 ++had_error;
483 continue;
485 cp += n;
486 /* Get canonical name. */
487 n = strlen (tbuf) + 1; /* For the \0. */
488 if ((size_t) n > linebuflen || n >= MAXHOSTNAMELEN)
490 ++had_error;
491 continue;
493 strcpy (bp, tbuf); /* Cannot overflow. */
494 tname = bp;
495 bp += n;
496 linebuflen -= n;
497 continue;
499 if (type != qtype)
501 syslog (LOG_NOTICE | LOG_AUTH,
502 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
503 qname, p_class (C_IN), p_type (qtype), p_type (type));
504 cp += n;
505 continue; /* XXX - had_error++ ? */
508 switch (type)
510 case T_PTR:
511 if (strcasecmp (tname, bp) != 0)
513 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
514 cp += n;
515 continue; /* XXX - had_error++ ? */
517 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
518 if (n < 0 || res_hnok (bp) == 0)
520 ++had_error;
521 break;
523 #if MULTI_PTRS_ARE_ALIASES
524 cp += n;
525 if (haveanswer == 0)
526 result->h_name = bp;
527 else if (ap < &host_data->aliases[MAXALIASES-1])
528 *ap++ = bp;
529 else
530 n = -1;
531 if (n != -1)
533 n = strlen (bp) + 1; /* for the \0 */
534 if (n >= MAXHOSTNAMELEN)
536 ++had_error;
537 break;
539 bp += n;
540 linebuflen -= n;
542 break;
543 #else
544 result->h_name = bp;
545 if (_res.options & RES_USE_INET6)
547 n = strlen (bp) + 1; /* for the \0 */
548 if (n >= MAXHOSTNAMELEN)
550 ++had_error;
551 break;
553 bp += n;
554 linebuflen -= n;
555 map_v4v6_hostent (result, &bp, &linebuflen);
557 *h_errnop = NETDB_SUCCESS;
558 return NSS_STATUS_SUCCESS;
559 #endif
560 case T_A:
561 case T_AAAA:
562 if (strcasecmp (result->h_name, bp) != 0)
564 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
565 cp += n;
566 continue; /* XXX - had_error++ ? */
568 if (n != result->h_length)
570 cp += n;
571 continue;
573 if (!haveanswer)
575 register int nn;
577 result->h_name = bp;
578 nn = strlen (bp) + 1; /* for the \0 */
579 bp += nn;
580 linebuflen -= nn;
583 linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align));
584 bp += sizeof (align) - ((u_long) bp % sizeof (align));
586 if (n >= linebuflen)
588 ++had_error;
589 continue;
591 if (hap >= &host_data->h_addr_ptrs[MAX_NR_ADDRS-1])
593 cp += n;
594 continue;
596 memcpy (*hap++ = bp, cp, n);
597 bp += n;
598 cp += n;
599 linebuflen -= n;
600 break;
601 default:
602 abort ();
604 if (had_error == 0)
605 ++haveanswer;
608 if (haveanswer > 0)
610 *ap = NULL;
611 *hap = NULL;
612 #if defined(RESOLVSORT)
614 * Note: we sort even if host can take only one address
615 * in its return structures - should give it the "best"
616 * address in that case, not some random one
618 if (_res.nsort && haveanswer > 1 && qtype == T_A)
619 addrsort (host_data->h_addr_ptrs, haveanswer);
620 #endif /*RESOLVSORT*/
622 if (result->h_name == NULL)
624 n = strlen (qname) + 1; /* For the \0. */
625 if (n > linebuflen || n >= MAXHOSTNAMELEN)
626 goto no_recovery;
627 strcpy (bp, qname); /* Cannot overflow. */
628 result->h_name = bp;
629 bp += n;
630 linebuflen -= n;
633 if (_res.options & RES_USE_INET6)
634 map_v4v6_hostent (result, &bp, &linebuflen);
635 *h_errnop = NETDB_SUCCESS;
636 return NSS_STATUS_SUCCESS;
638 no_recovery:
639 *h_errnop = NO_RECOVERY;
640 return NSS_STATUS_TRYAGAIN;