update from main archive 960921
[glibc/history.git] / nss / nss_dns / dns-host.c
blobd9bf4d7be5f8631c2ddb4e29b9099257a957c357
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 `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 <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 <netinet/in.h>
88 #include <arpa/inet.h>
89 #include <arpa/nameser.h>
90 #include <resolv.h>
92 #include "nsswitch.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
102 #if PACKETSZ > 1024
103 # define MAXPACKET PACKETSZ
104 #else
105 # define MAXPACKET 1024
106 #endif
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,
123 char *buffer, int buflen, int *h_errnop);
125 enum nss_status
126 _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
127 char *buffer, int buflen, int *h_errnop)
129 struct host_data
131 char *aliases[MAX_NR_ALIASES];
132 unsigned char host_addr[16]; /* IPv4 or IPv6 */
133 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
134 char linebuffer[0];
135 } *host_data = (struct host_data *) buffer;
136 int linebuflen = buflen - offsetof (struct host_data, linebuffer);
137 querybuf host_buffer;
138 int size, type, n;
139 const char *cp;
141 switch (af) {
142 case AF_INET:
143 size = INADDRSZ;
144 type = T_A;
145 break;
146 case AF_INET6:
147 size = IN6ADDRSZ;
148 type = T_AAAA;
149 break;
150 default:
151 *h_errnop = NETDB_INTERNAL;
152 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)
165 name = cp;
168 * disallow names consisting only of digits/dots, unless
169 * they end in a dot.
171 if (isdigit (name[0]))
172 for (cp = name;; ++cp)
174 if (*cp == '\0')
176 char *bp;
178 if (*--cp == '.')
179 break;
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 != '.')
207 break;
209 if (isxdigit (name[0]) || name[0] == ':')
210 for (cp = name;; ++cp)
212 if (*cp == '\0')
214 char *bp;
216 if (*--cp == '.')
217 break;
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 != '.')
242 break;
245 n = res_search (name, C_IN, type, host_buffer.buf, sizeof (host_buffer));
246 if (n < 0)
247 return (errno == ECONNREFUSED
248 || errno == EPFNOSUPPORT
249 || errno == EAFNOSUPPORT)
250 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
252 return getanswer_r (&host_buffer, n, name, type, result, buffer, buflen,
253 h_errnop);
257 enum nss_status
258 _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
259 char *buffer, int buflen, int *h_errnop)
261 enum nss_status status = NSS_STATUS_NOTFOUND;
263 if (_res.options & RES_USE_INET6)
264 status = _nss_dns_gethostbyname2_r (name, AF_INET6, result, buffer,
265 buflen, h_errnop);
266 if (status == NSS_STATUS_NOTFOUND)
267 status = _nss_dns_gethostbyname2_r (name, AF_INET, result, buffer,
268 buflen, h_errnop);
270 return status;
274 enum nss_status
275 _nss_dns_gethostbyaddr_r (const char *addr, int len, int af,
276 struct hostent *result, char *buffer, int buflen,
277 int *h_errnop)
279 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
280 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
281 const u_char *uaddr = (const u_char *)addr;
282 struct host_data
284 char *aliases[MAX_NR_ALIASES];
285 unsigned char host_addr[16]; /* IPv4 or IPv6 */
286 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
287 char linebuffer[0];
288 } *host_data = (struct host_data *) buffer;
289 querybuf host_buffer;
290 char qbuf[MAXDNAME+1], *qp;
291 int size, n, status;
293 if (af == AF_INET6 && len == IN6ADDRSZ &&
294 (bcmp (uaddr, mapped, sizeof mapped) == 0
295 || bcmp (uaddr, tunnelled, sizeof tunnelled) == 0))
297 /* Unmap. */
298 addr += sizeof mapped;
299 uaddr += sizeof mapped;
300 af = AF_INET;
301 len = INADDRSZ;
304 switch (af)
306 case AF_INET:
307 size = INADDRSZ;
308 break;
309 case AF_INET6:
310 size = IN6ADDRSZ;
311 break;
312 default:
313 errno = EAFNOSUPPORT;
314 *h_errnop = NETDB_INTERNAL;
315 return NSS_STATUS_UNAVAIL;
317 if (size != len)
319 errno = EAFNOSUPPORT;
320 *h_errnop = NETDB_INTERNAL;
321 return NSS_STATUS_UNAVAIL;
324 switch (af)
326 case AF_INET:
327 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
328 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
329 break;
330 case AF_INET6:
331 qp = qbuf;
332 for (n = IN6ADDRSZ - 1; n >= 0; n--)
333 qp += sprintf (qp, "%x.%x.", uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf);
334 strcpy(qp, "ip6.int");
335 break;
336 default:
337 /* Cannot happen. */
340 n = res_query (qbuf, C_IN, T_PTR, (u_char *)host_buffer.buf,
341 sizeof host_buffer);
342 if (n < 0)
343 return (errno == ECONNREFUSED
344 || errno == EPFNOSUPPORT
345 || errno == EAFNOSUPPORT)
346 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
348 status = getanswer_r (&host_buffer, n, qbuf, T_PTR, result, buffer, buflen,
349 h_errnop);
350 if (status != NSS_STATUS_SUCCESS)
351 return status;
353 #ifdef SUNSECURITY
354 This is not implemented because it is not possible to use the current
355 source from bind in a multi-threaded program.
356 #endif
358 result->h_addrtype = af;
359 result->h_length = len;
360 bcopy (addr, host_data->host_addr, len);
361 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
362 host_data->h_addr_ptrs[1] = NULL;
363 if (af == AF_INET && (_res.options & RES_USE_INET6))
365 map_v4v6_address ((char *) host_data->host_addr,
366 (char *) host_data->host_addr);
367 result->h_addrtype = AF_INET6;
368 result->h_length = IN6ADDRSZ;
370 *h_errnop = NETDB_SUCCESS;
371 return NSS_STATUS_SUCCESS;
375 static enum nss_status
376 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
377 struct hostent *result, char *buffer, int buflen, int *h_errnop)
379 struct host_data
381 char *aliases[MAX_NR_ALIASES];
382 unsigned char host_addr[16]; /* IPv4 or IPv6 */
383 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
384 char linebuffer[0];
385 } *host_data = (struct host_data *) buffer;
386 int linebuflen = buflen - offsetof (struct host_data, linebuffer);
387 register const HEADER *hp;
388 const u_char *end_of_message, *cp;
389 int n, ancount, qdcount;
390 int haveanswer, had_error;
391 char *bp, **ap, **hap;
392 char tbuf[MAXDNAME];
393 const char *tname;
394 int (*name_ok) __P ((const char *));
396 tname = qname;
397 result->h_name = NULL;
398 end_of_message = answer->buf + anslen;
399 switch (qtype)
401 case T_A:
402 case T_AAAA:
403 name_ok = res_hnok;
404 break;
405 case T_PTR:
406 name_ok = res_dnok;
407 break;
408 default:
409 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
413 * find first satisfactory answer
415 hp = &answer->hdr;
416 bp = host_data->linebuffer;
417 ancount = ntohs (hp->ancount);
418 qdcount = ntohs (hp->qdcount);
419 cp = answer->buf + HFIXEDSZ;
420 if (qdcount != 1)
422 *h_errnop = NO_RECOVERY;
423 return NSS_STATUS_UNAVAIL;
426 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
427 if (n < 0 || (*name_ok) (bp) == 0)
429 *h_errnop = NO_RECOVERY;
430 return NSS_STATUS_UNAVAIL;
432 cp += n + QFIXEDSZ;
434 if (qtype == T_A || qtype == T_AAAA)
436 /* res_send() has already verified that the query name is the
437 * same as the one we sent; this just gets the expanded name
438 * (i.e., with the succeeding search-domain tacked on).
440 n = strlen (bp) + 1; /* for the \0 */
441 result->h_name = bp;
442 bp += n;
443 linebuflen -= n;
444 /* The qname can be abbreviated, but h_name is now absolute. */
445 qname = result->h_name;
448 ap = host_data->aliases;
449 *ap = NULL;
450 result->h_aliases = host_data->aliases;
451 hap = host_data->h_addr_ptrs;
452 *hap = NULL;
453 result->h_addr_list = host_data->h_addr_ptrs;
454 haveanswer = 0;
455 had_error = 0;
457 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
459 int type, class;
461 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
462 if (n < 0 || (*name_ok) (bp) == 0)
464 ++had_error;
465 continue;
467 cp += n; /* name */
468 type = _getshort (cp);
469 cp += INT16SZ; /* type */
470 class = _getshort(cp);
471 cp += INT16SZ + INT32SZ; /* class, TTL */
472 n = _getshort(cp);
473 cp += INT16SZ; /* len */
474 if (class != C_IN)
476 /* XXX - debug? syslog? */
477 cp += n;
478 continue; /* XXX - had_error++ ? */
481 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
483 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
484 continue;
485 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
486 if (n < 0 || (*name_ok) (tbuf) == 0)
488 ++had_error;
489 continue;
491 cp += n;
492 /* Store alias. */
493 *ap++ = bp;
494 n = strlen (bp) + 1; /* For the \0. */
495 bp += n;
496 linebuflen -= n;
497 /* Get canonical name. */
498 n = strlen (tbuf) + 1; /* For the \0. */
499 if (n > buflen)
501 ++had_error;
502 continue;
504 strcpy (bp, tbuf);
505 result->h_name = bp;
506 bp += n;
507 linebuflen -= n;
508 continue;
511 if (qtype == T_PTR && type == T_CNAME)
513 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
514 if (n < 0 || res_hnok (tbuf) == 0)
516 ++had_error;
517 continue;
519 cp += n;
520 /* Get canonical name. */
521 n = strlen (tbuf) + 1; /* For the \0. */
522 if (n > buflen)
524 ++had_error;
525 continue;
527 strcpy (bp, tbuf);
528 tname = bp;
529 bp += n;
530 linebuflen -= n;
531 continue;
533 if (type != qtype)
535 syslog (LOG_NOTICE | LOG_AUTH,
536 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
537 qname, p_class (C_IN), p_type (qtype), p_type (type));
538 cp += n;
539 continue; /* XXX - had_error++ ? */
542 switch (type)
544 case T_PTR:
545 if (strcasecmp (tname, bp) != 0)
547 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
548 cp += n;
549 continue; /* XXX - had_error++ ? */
551 n = dn_expand (answer->buf, end_of_message, cp, bp, linebuflen);
552 if (n < 0 || res_hnok (bp) == 0)
554 ++had_error;
555 break;
557 #if MULTI_PTRS_ARE_ALIASES
558 cp += n;
559 if (haveanswer == 0)
560 result->h_name = bp;
561 else if (ap < &host_data->aliases[MAXALIASES-1])
562 *ap++ = bp;
563 else
564 n = -1;
565 if (n != -1)
567 n = strlen (bp) + 1; /* for the \0 */
568 bp += n;
569 linebuflen -= n;
571 break;
572 #else
573 result->h_name = bp;
574 if (_res.options & RES_USE_INET6)
576 n = strlen (bp) + 1; /* for the \0 */
577 bp += n;
578 linebuflen -= n;
579 map_v4v6_hostent (result, &bp, &linebuflen);
581 *h_errnop = NETDB_SUCCESS;
582 return NSS_STATUS_SUCCESS;
583 #endif
584 case T_A:
585 case T_AAAA:
586 if (strcasecmp (result->h_name, bp) != 0)
588 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
589 cp += n;
590 continue; /* XXX - had_error++ ? */
592 if (haveanswer)
594 if (n != result->h_length)
596 cp += n;
597 continue;
600 else
602 register int nn;
604 result->h_name = bp;
605 nn = strlen (bp) + 1; /* for the \0 */
606 bp += nn;
607 linebuflen -= nn;
610 bp += sizeof (align) - ((u_long) bp % sizeof (align));
612 if (n >= linebuflen)
614 ++had_error;
615 continue;
617 if (hap >= &host_data->h_addr_ptrs[MAX_NR_ADDRS-1])
619 cp += n;
620 continue;
622 bcopy (cp, *hap++ = bp, n);
623 bp += n;
624 cp += n;
625 linebuflen -= n;
626 break;
627 default:
628 abort ();
630 if (had_error == 0)
631 ++haveanswer;
634 if (haveanswer > 0)
636 *ap = NULL;
637 *hap = NULL;
638 #if defined(RESOLVSORT)
640 * Note: we sort even if host can take only one address
641 * in its return structures - should give it the "best"
642 * address in that case, not some random one
644 if (_res.nsort && haveanswer > 1 && qtype == T_A)
645 addrsort (host_data->h_addr_ptrs, haveanswer);
646 #endif /*RESOLVSORT*/
648 if (result->h_name == NULL)
650 n = strlen (qname) + 1; /* For the \0. */
651 if (n > linebuflen)
652 goto try_again;
653 strcpy (bp, qname);
654 result->h_name = bp;
655 bp += n;
656 linebuflen -= n;
659 if (_res.options & RES_USE_INET6)
660 map_v4v6_hostent (result, &bp, &linebuflen);
661 *h_errnop = NETDB_SUCCESS;
662 return NSS_STATUS_SUCCESS;
664 try_again:
665 *h_errnop = TRY_AGAIN;
666 return NSS_STATUS_TRYAGAIN;