]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
inet_pton() on OSX doesn't accept "127.0" as 127.0.0.0
authorAlan T. DeKok <aland@freeradius.org>
Wed, 17 Nov 2021 17:24:58 +0000 (12:24 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 17 Nov 2021 17:24:58 +0000 (12:24 -0500)
despite what the documentation says.

But getaddrinfo() does accept it.  So when we turn off hostname
lookups, IP address parsing fails.

Work around it by just doing the work ourselves.

src/lib/util/inet.c
src/tests/unit/data_types.txt

index 012c3100f9063925b05b91bb0f74c91f18fa5e56..8291b7d005d82a66872520aad1df04a7204d016e 100644 (file)
@@ -517,10 +517,21 @@ int fr_inet_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resol
                        out->addr.v4.s_addr = htonl(strtoul(value, NULL, 0));
 
                } else if (!resolve) {
-                       if (inet_pton(AF_INET, value, &out->addr.v4.s_addr) <= 0) {
+                       unsigned int a, b, c, d;
+                       int num;
+                       char rest;
+
+                       a = b = c = d = 0;
+
+                       num = sscanf(value, "%u.%u.%u.%u%c", &a, &b, &c, &d, &rest);
+                       if ((num == 0) || (num == 5) ||
+                           (a > 255) || (b > 255) || (c > 255) || (d > 255)) {
                                fr_strerror_printf("Failed to parse IPv4 address string \"%s\"", value);
                                return -1;
                        }
+
+                       out->addr.v4.s_addr = htonl((a << 24) | (b << 16) | (c << 8) | d);
+
                } else if (fr_inet_hton(out, AF_INET, value, fallback) < 0) return -1;
 
                return 0;
index 1d8876831ea631db1b867027c475d5620ded07ac..fc14c5210dc1dfb74b64f252e860fda7c8c8daaf 100644 (file)
@@ -9,6 +9,12 @@ match 0
 value ipaddr 127.0.0.1
 match 127.0.0.1
 
+#
+#  The rest of the octets should get filled out as 0.
+#
+value ipaddr 127.0
+match 127.0.0.0
+
 #  And canonicalized
 value ipaddr 127.0.0.001
 match 127.0.0.1
@@ -238,4 +244,4 @@ encode-dns-label www_foo.com
 match Invalid character 0x5f in label
 
 count
-match 118
+match 120