Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-6]: src/dist/bind/bin/host Pull up revision 1.6 (requested by i...



details:   https://anonhg.NetBSD.org/src/rev/df6e1f782069
branches:  netbsd-1-6
changeset: 528122:df6e1f782069
user:      lukem <lukem%NetBSD.org@localhost>
date:      Fri Jun 28 11:28:22 2002 +0000

description:
Pull up revision 1.6 (requested by itojun in ticket #387):
Update to BIND 8.3.3.  Fixes buffer overrun in resolver code.

diffstat:

 dist/bind/bin/host/host.c |  406 +++++++++++++++++++++++++++++++--------------
 1 files changed, 275 insertions(+), 131 deletions(-)

diffs (truncated from 730 to 300 lines):

diff -r 71f782ffa209 -r df6e1f782069 dist/bind/bin/host/host.c
--- a/dist/bind/bin/host/host.c Fri Jun 28 11:28:11 2002 +0000
+++ b/dist/bind/bin/host/host.c Fri Jun 28 11:28:22 2002 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: host.c,v 1.5 2001/05/17 22:59:38 itojun Exp $  */
+/*     $NetBSD: host.c,v 1.5.2.1 2002/06/28 11:28:22 lukem Exp $       */
 
 #ifndef lint
-static const char rcsid[] = "Id: host.c,v 8.43.2.1 2001/04/26 02:56:07 marka Exp";
+static const char rcsid[] = "Id: host.c,v 8.53 2002/06/18 02:34:02 marka Exp";
 #endif /* not lint */
 
 /*
@@ -102,7 +102,6 @@
 
 #include <ctype.h>
 #include <netdb.h>
-#include <resolv.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -115,13 +114,15 @@
 
 #include "port_after.h"
 
+#include <resolv.h>
+
 /* Global. */
 
 #ifndef PATH_SEP
 #define PATH_SEP '/'
 #endif
 #define SIG_RDATA_BY_NAME      18
-#define NS_HEADERDATA_SIZE 10
+#define NS_HEADERDATA_SIZE 10  /* type + class + ttl + rdlen */
 
 #define NUMNS          8
 #define NUMNSADDR      16
@@ -187,9 +188,8 @@
 
 static int             parsetype(const char *s);
 static int             parseclass(const char *s);
-static void            printanswer(const struct hostent *hp);
 static void            hperror(int errnum);
-static int             addrinfo(struct in_addr addr);
+static int             addrinfo(struct sockaddr_storage *addr);
 static int             gethostinfo(char *name);
 static int             getdomaininfo(const char *name, const char *domain);
 static int             getinfo(const char *name, const char *domain,
@@ -227,12 +227,16 @@
 
 int
 main(int argc, char **argv) {
-       struct in_addr addr;
+       struct sockaddr_storage addr;
        struct hostent *hp;
        char *s;
        int waitmode = 0;
        int ncnames, ch;
        int nkeychains;
+       struct addrinfo *answer = NULL;
+       struct addrinfo *cur = NULL;
+       struct addrinfo hint;
+       int ip = 0;
 
        dst_init();
 
@@ -294,33 +298,93 @@
        if (argc > 1)
                usage("extra undefined arguments");
        if (argc == 1) {
+               union res_sockaddr_union u[MAXNS];
+               int nscount;
+
                s = *argv++;
                argc--;
                server_specified++;
+               memset(&hint, 0, sizeof(hint));
+               hint.ai_flags = AI_CANONNAME;
+               hint.ai_family = PF_UNSPEC;
+               hint.ai_socktype = SOCK_DGRAM;
                
-               if (!inet_aton(s, &addr)) {
-                       hp = gethostbyname(s);
-                       if (hp == NULL) {
-                               fprintf(stderr,
-                                       "Error in looking up server name:\n");
-                               hperror(res.res_h_errno);
-                               exit(1);
+               if (!getaddrinfo(s, NULL, &hint, &answer)) {
+                       nscount = 0;
+                       if (answer->ai_canonname != NULL) {
+                               printf("Using domain server:\n");
+                               printf("Name: %s\n", answer->ai_canonname);
+                               printf("Addresses:");
+                       } else
+                               printf("Using domain server");
+                       
+                       for (cur = answer; cur != NULL; cur = cur->ai_next) {
+                               char buf[80];
+                               struct sockaddr_in6 *sin6;
+                               struct sockaddr_in *sin;
+                               
+                               switch (cur->ai_addr->sa_family) {
+                               case AF_INET6:
+                                       sin6 = 
+                                          (struct sockaddr_in6 *)cur->ai_addr;
+                                       inet_ntop(cur->ai_addr->sa_family,
+                                                 &sin6->sin6_addr,
+                                                 buf, sizeof(buf));
+                                       printf(" %s", buf);
+                                       if (nscount >= MAXNS)
+                                               break;
+                                       u[nscount].sin6 = *sin6;
+                                       u[nscount++].sin6.sin6_port =
+                                               htons(NAMESERVER_PORT);
+                                       break;
+                               case AF_INET:
+                                       sin =
+                                            (struct sockaddr_in*)cur->ai_addr;
+                                       inet_ntop(cur->ai_addr->sa_family,
+                                                 &sin->sin_addr,
+                                                 buf, sizeof(buf));
+                                       printf(" %s", buf);
+                                       if (nscount >= MAXNS)
+                                               break;
+                                       u[nscount].sin = *sin;
+                                       u[nscount++].sin6.sin6_port =
+                                               htons(NAMESERVER_PORT);
+                                       break;
+                               }
                        }
-                       memcpy(&res.nsaddr.sin_addr, hp->h_addr, NS_INADDRSZ);
-                       printf("Using domain server:\n");
-                       printanswer(hp);
+                       if (nscount != 0) {
+                               res_setservers(&res, u, nscount);
+                       }
+                       if (answer->ai_canonname != NULL)
+                               printf("\n\n");
+                       else
+                               printf(":\n\n");
+                       freeaddrinfo(answer);
                } else {
-                       res.nsaddr.sin_family = AF_INET;
-                       res.nsaddr.sin_addr = addr;
-                       res.nsaddr.sin_port = htons(NAMESERVER_PORT);
-                       printf("Using domain server %s:\n",
-                              inet_ntoa(res.nsaddr.sin_addr));
+                       fprintf(stderr, "Error in looking up server name:\n");
+                       exit(1);
                }
-               res.nscount = 1;
                res.retry = 2;
        }
-       if (strcmp(getdomain, ".") == 0 || !inet_aton(getdomain, &addr))
-               addr.s_addr = INADDR_NONE;
+       memset(&hint, 0, sizeof(hint));
+       hint.ai_flags = AI_NUMERICHOST;
+       hint.ai_socktype = SOCK_DGRAM;
+       if(!getaddrinfo(getdomain, NULL, &hint, &answer)) { 
+               memset(&addr, 0, sizeof(addr));
+               switch (answer->ai_family) {
+               case AF_INET:
+                       memcpy(&addr, answer->ai_addr,
+                              sizeof(struct sockaddr_in));
+                       ip = 1;
+                       break;
+               case AF_INET6:
+                       memcpy(&addr, answer->ai_addr,
+                              sizeof(struct sockaddr_in6));
+                       ip = 1;
+                       break;
+               }
+               freeaddrinfo(answer);
+       } 
        hp = NULL;
        res.res_h_errno = TRY_AGAIN;
 /*
@@ -332,7 +396,7 @@
                exit(ListHosts(getdomain, querytype ? querytype : ns_t_a));
        ncnames = 5; nkeychains = 18;
        while (hp == NULL && res.res_h_errno == TRY_AGAIN) {
-               if (addr.s_addr == INADDR_NONE) {
+               if (!ip) {
                        cname = NULL;
                        hp = (struct hostent *)(unsigned long)gethostinfo(getdomain);
                        getdomain[0] = 0; /* clear this query */
@@ -356,7 +420,7 @@
                                        printf ("%s for %s not found, last verified key %s\n",
                                                chase_step & SD_SIG ? "Key" : "Signature",
                                                chase_step & SD_SIG ? chase_signer : chase_domain, 
-                                               chase_lastgoodkey ? chase_lastgoodkey : "None");
+                                               chase_lastgoodkey[0] ? chase_lastgoodkey : "None");
                                }
                        }
                        if (!getdomain[0] && cname) {
@@ -380,7 +444,7 @@
                                continue;
                        }
                } else {
-                       if (addrinfo(addr) == 0)
+                       if (addrinfo(&addr) == 0)
                                hp = NULL;
                        else
                                hp = (struct hostent *)1;       /* XXX */
@@ -430,21 +494,6 @@
 }
 
 static void
-printanswer(const struct hostent *hp) {
-       struct in_addr **hptr;
-       char **cp;
-
-       printf("Name: %s\n", hp->h_name);
-       printf("Address:");
-       for (hptr = (struct in_addr **)hp->h_addr_list; *hptr; hptr++)
-               printf(" %s", inet_ntoa(**hptr));
-       printf("\nAliases:");
-       for (cp = hp->h_aliases; cp && *cp && **cp; cp++)
-               printf(" %s", *cp);
-       printf("\n\n");
-}
-
-static void
 hperror(int errnum) {
        switch(errnum) {
        case HOST_NOT_FOUND:
@@ -527,15 +576,50 @@
 }
 
 static int
-addrinfo(struct in_addr addr) {
-       u_int32_t ha = ntohl(addr.s_addr);
+addrinfo(struct sockaddr_storage *addr) {
        char name[NS_MAXDNAME];
+       unsigned char *p;
+       struct in6_addr *addr6;
 
-       sprintf(name, "%u.%u.%u.%u.IN-ADDR.ARPA.",
-               (ha) & 0xff,
-               (ha >> 8) & 0xff,
-               (ha >> 16) & 0xff,
-               (ha >> 24) & 0xff);
+       switch(addr->ss_family) {
+       case AF_INET:
+               p = (unsigned char*)&((struct sockaddr_in *)addr)->sin_addr;
+       mapped:
+               sprintf(name, "%u.%u.%u.%u.IN-ADDR.ARPA.",
+                       p[3], p[2], p[1], p[0]);
+               break;
+       case AF_INET6:
+               addr6 = &((struct sockaddr_in6 *)addr)->sin6_addr;
+               p = (unsigned char *)addr6;
+               if (IN6_IS_ADDR_V4MAPPED(addr6) ||
+                   IN6_IS_ADDR_V4COMPAT(addr6)) {
+                       p += 12;
+                       goto mapped;
+               }
+               sprintf(name,
+                       "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
+                       "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
+                       "IP6.ARPA",
+                       p[15] & 0xf, (p[15] >> 4) & 0xf,
+                       p[14] & 0xf, (p[14] >> 4) & 0xf,
+                       p[13] & 0xf, (p[13] >> 4) & 0xf,
+                       p[12] & 0xf, (p[12] >> 4) & 0xf,
+                       p[11] & 0xf, (p[11] >> 4) & 0xf,
+                       p[10] & 0xf, (p[10] >> 4) & 0xf,
+                       p[9] & 0xf, (p[9] >> 4) & 0xf,
+                       p[8] & 0xf, (p[8] >> 4) & 0xf,
+                       p[7] & 0xf, (p[7] >> 4) & 0xf,
+                       p[6] & 0xf, (p[6] >> 4) & 0xf,
+                       p[5] & 0xf, (p[5] >> 4) & 0xf,
+                       p[4] & 0xf, (p[4] >> 4) & 0xf,
+                       p[3] & 0xf, (p[3] >> 4) & 0xf,
+                       p[2] & 0xf, (p[2] >> 4) & 0xf,
+                       p[1] & 0xf, (p[1] >> 4) & 0xf,
+                       p[0] & 0xf, (p[0] >> 4) & 0xf);
+               break;
+       default:
+               abort();
+       }
        return (getinfo(name, NULL, ns_t_ptr));
 }
 
@@ -593,7 +677,7 @@
 
 static int
 getdomaininfo(const char *name, const char *domain) {
-       int val1, val2;
+       int val1, val2, val3;
 
        if (querytype)
                return (getinfo(name, domain, gettype=querytype));
@@ -601,8 +685,9 @@
                val1 = getinfo(name, domain, gettype=ns_t_a);
                if (cname || verbose)
                        return (val1);
-               val2 = getinfo(name, domain, gettype=ns_t_mx);



Home | Main Index | Thread Index | Old Index