]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 1324] support bracketed IPv6 numeric addresses for restrict.
authorDave Hart <hart@ntp.org>
Wed, 30 Sep 2009 06:32:09 +0000 (06:32 +0000)
committerDave Hart <hart@ntp.org>
Wed, 30 Sep 2009 06:32:09 +0000 (06:32 +0000)
bk: 4ac2fb69Sc4j4r_kDhr7189pbbsc9A

ChangeLog
ntpd/ntp_config.c
ntpd/ntp_io.c

index 76f3725e1761ed7bedf5f61d2c25f6a7dd2cb5f9..0e4ca484855378014a99e68e7192429b967d3ffb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* [Bug 1324] support bracketed IPv6 numeric addresses for restrict.
 (4.2.5p224) 2009/09/29 Released by Harlan Stenn <stenn@ntp.org>
 * Clockhop and documentation fixes from Dave Mills.
 * Remove "tos maxhop" ntp.conf knob.
index 0a2e58c3f0215dda8682ae83865fb674479cdfe4..9ae41734310a2e2b152551b76a05e6d1eaf6b664 100644 (file)
@@ -2149,8 +2149,9 @@ config_access(
        }
 
        /* Configure the restrict options */
-       my_node = queue_head(ptree->restrict_opts);
-       while (my_node != NULL) {
+       for (my_node = queue_head(ptree->restrict_opts);
+            my_node != NULL;
+            my_node = next_node(my_node)) {
 
                ZERO_SOCK(&addr_sock);
 
@@ -2279,8 +2280,6 @@ config_access(
                        fprintf(stderr, "%s\n", signd_warning);
                        msyslog(LOG_WARNING, signd_warning);
                }
-
-               my_node = next_node(my_node);
        }
 }
 
@@ -2520,8 +2519,9 @@ apply_enable_disable(
        bc_entry *pentry;
 #endif
 
-       curr_flag = queue_head(q);
-       while (curr_flag != NULL) {
+       for (curr_flag = queue_head(q);
+            curr_flag != NULL;
+            curr_flag = next_node(curr_flag)) {
 
                option = curr_flag->value.i;
                switch (option) {
@@ -2578,7 +2578,6 @@ apply_enable_disable(
                        break;
 #endif
                }
-               curr_flag = next_node(curr_flag);
        }
 }
 
@@ -2861,8 +2860,10 @@ config_trap(
        /* silence warning about addr_sock potentially uninitialized */
        AF(&addr_sock) = AF_UNSPEC;
 
-       curr_trap = queue_head(ptree->trap);
-       while (curr_trap != NULL) {
+       for (curr_trap = queue_head(ptree->trap);
+            curr_trap != NULL;
+            curr_trap = next_node(curr_trap)) {
+
                err_flag = 0;
                port_no = 0;
                localaddr = NULL;
@@ -2911,10 +2912,8 @@ config_trap(
                if (!err_flag) {
                        ZERO_SOCK(&peeraddr);
                        if (1 != getnetnum(curr_trap->addr->address,
-                                          &peeraddr, 1, t_UNK)) {
-                               err_flag = 1;
-                               break;
-                       }
+                                          &peeraddr, 1, t_UNK))
+                               continue;
 
                        /* port is at same location for v4 and v6 */
                        SET_PORT(&peeraddr, port_no ? port_no : TRAPPORT);
@@ -2931,7 +2930,6 @@ config_trap(
                                        "can't set trap for %s",
                                        stoa(&peeraddr));
                }
-               curr_trap = next_node(curr_trap);
        }
 }
 
@@ -3445,8 +3443,9 @@ config_unpeers(
        int status;
        int found;
 
-       while (!empty(ptree->unpeers)) {
-               curr_unpeer = (struct unpeer_node *) dequeue(ptree->unpeers);
+       for (curr_unpeer = queue_head(ptree->unpeers);
+            curr_unpeer != NULL;
+            curr_unpeer = next_node(curr_unpeer)) {
 
                /*
                 * Either AssocID will be zero, and we unpeer by name/
@@ -3519,10 +3518,6 @@ config_unpeers(
                        }
                        freeaddrinfo(res_bak);
                }
-
-               /* Ok, everything done. Free up peer node memory */
-               destroy_address_node(curr_unpeer->addr);
-               free_node(curr_unpeer);
        }
 }
 
@@ -3601,10 +3596,8 @@ config_sim(
                if (NULL == serv_info) {
                        fprintf(stderr, "Simulator server list is corrupt\n");
                        exit(1);
-               } else {
+               } else
                        memcpy(&simulation.servers[i], serv_info, sizeof(server_info));
-                       free_node(serv_info);
-               }
                serv_info = next_node(serv_info);
        }
 
@@ -4173,13 +4166,16 @@ getnetnum(
  */
 static int
 get_multiple_netnums(
-       const char *num,
+       const char *nameornum,
        sockaddr_u *addr,
        struct addrinfo **res,
        int complain,
        enum gnn_type a_type
        )
 {
+       char lookbuf[1024];
+       const char *lookup;
+       char *pch;
        struct addrinfo hints;
        struct addrinfo *ptr;
        int retval;
@@ -4187,39 +4183,73 @@ get_multiple_netnums(
 
        memset(&hints, 0, sizeof(hints));
 
-       if (is_ip_address(num, &ipaddr))
+       if (strlen(nameornum) >= sizeof(lookbuf)) {
+               NTP_INSIST(strlen(nameornum) < sizeof(lookbuf));
+               return 0;
+       }
+
+       lookup = nameornum;
+       if (is_ip_address(nameornum, &ipaddr)) {
                hints.ai_flags = AI_NUMERICHOST;
+               hints.ai_family = ipaddr.family;
+               if ('[' == nameornum[0]) {
+                       lookup = lookbuf;
+                       strncpy(lookbuf, &nameornum[1],
+                               sizeof(lookbuf));
+                       pch = strchr(lookbuf, ']');
+                       if (pch != NULL)
+                               *pch = '\0';
+               }
+               pch = strchr(lookup, '%');
+               if (pch != NULL) {
+                       if (lookup != lookbuf) {
+                               lookup = lookbuf;
+                               strncpy(lookbuf, nameornum,
+                                       sizeof(lookbuf));
+                               pch = strchr(lookup, '%');
+                       }
+                       *pch = '\0';
+               }
+       }
 
-       if (!ipv6_works)
-               hints.ai_family = AF_INET;
-       else if (!ipv4_works)
-               hints.ai_family = AF_INET6;
-       else if (IS_IPV4(addr) || IS_IPV6(addr))
-               hints.ai_family = AF(addr);
-       else
-               hints.ai_family = AF_UNSPEC;
+       if (AF_INET6 == hints.ai_family && !ipv6_works)
+               return 0;
+
+       if (AF_UNSPEC == hints.ai_family) {
+               if (!ipv6_works)
+                       hints.ai_family = AF_INET;
+               else if (!ipv4_works)
+                       hints.ai_family = AF_INET6;
+               else if (IS_IPV4(addr) || IS_IPV6(addr))
+                       hints.ai_family = AF(addr);
+       }
 
        /* Get host address. Looking for UDP datagram connection */
        hints.ai_socktype = SOCK_DGRAM;
 
-       DPRINTF(4, ("getaddrinfo %s\n", num));
+       DPRINTF(4, ("getaddrinfo %s%s\n", 
+                   (AF_UNSPEC == hints.ai_family)
+                       ? ""
+                       : (AF_INET == hints.ai_family)
+                               ? "v4 "
+                               : "v6 ",
+                   lookup));
 
-       retval = getaddrinfo(num, "ntp", &hints, &ptr);
+       retval = getaddrinfo(lookup, "ntp", &hints, &ptr);
 
        if (retval || (AF_INET6 == ptr->ai_family && !ipv6_works)) {
                if (complain)
                        msyslog(LOG_ERR,
-                               "getaddrinfo: \"%s\" invalid host "
-                               "address, ignored",
-                               num);
+                               "getaddrinfo: \"%s\" invalid host address, ignored",
+                               lookup);
                else
-                       DPRINTF(1, ("getaddrinfo: \"%s\" invalid host "
-                                   "address.\n",
-                                   num));
+                       DPRINTF(1, ("getaddrinfo: \"%s\" invalid host address.\n",
+                                   lookup));
 
-               if (!retval)
+               if (!retval) {
+                       freeaddrinfo(ptr);
                        return -1;
-               else 
+               else 
                        return 0;
        }
        *res = ptr;
index ba6b575fa8d3af8ae3d598fb14f533b85d6725bb..ff2a1b5327d51cc68f5b70d086991afd3d5f3b6e 100644 (file)
@@ -754,6 +754,8 @@ is_ip_address(
 {
        struct in_addr in4;
        struct in6_addr in6;
+       char tmpbuf[128];
+       char *pch;
 
        NTP_REQUIRE(host != NULL);
        NTP_REQUIRE(addr != NULL);
@@ -770,13 +772,17 @@ is_ip_address(
        if (inet_pton(AF_INET, host, &in4) == 1) {
                isc_netaddr_fromin(addr, &in4);
                return (ISC_TRUE);
-       } else if (strlen(host) <= 127U) {
-               char tmpbuf[128], *d;
-
-               strcpy(tmpbuf, host);
-               d = strchr(tmpbuf, '%');
-               if (d != NULL)
-                       *d = '\0';
+       } else if (sizeof(tmpbuf) > strlen(host)) {
+               if ('[' == host[0]) {
+                       strncpy(tmpbuf, &host[1], sizeof(tmpbuf));
+                       pch = strchr(tmpbuf, ']');
+                       if (pch != NULL)
+                               *pch = '\0';
+               } else
+                       strncpy(tmpbuf, host, sizeof(tmpbuf));
+               pch = strchr(tmpbuf, '%');
+               if (pch != NULL)
+                       *pch = '\0';
 
                if (inet_pton(AF_INET6, tmpbuf, &in6) == 1) {
                        isc_netaddr_fromin6(addr, &in6);