diff --git a/cf-serverd/server_access.c b/cf-serverd/server_access.c index 8ef0aa5c892..52972136d1c 100644 --- a/cf-serverd/server_access.c +++ b/cf-serverd/server_access.c @@ -29,7 +29,7 @@ #include "strlist.h" #include "server.h" -#include /* FuzzySetMatch */ +#include /* IPRangeMatch */ #include /* StringMatchFull TODO REMOVE */ #include #include @@ -73,6 +73,8 @@ static bool access_CheckResource(const struct resource_acl *acl, const char *ipaddr, const char *hostname, const char *key, const char *username) { + assert(acl != NULL); + bool access = false; /* DENY by default */ bool have_match = false; /* No matching rule found yet */ @@ -90,7 +92,7 @@ static bool access_CheckResource(const struct resource_acl *acl, const char *rule = NULL; for (size_t i = 0; i < StrList_Len(acl->admit.ips); i++) { - if (FuzzySetMatch(StrList_At(acl->admit.ips, i), ipaddr) == 0 || + if (IPRangeMatch(StrList_At(acl->admit.ips, i), ipaddr) || /* Legacy regex matching, TODO DEPRECATE */ StringMatchFull(StrList_At(acl->admit.ips, i), ipaddr)) { @@ -196,7 +198,7 @@ static bool access_CheckResource(const struct resource_acl *acl, const char *rule = NULL; for (size_t i = 0; i < StrList_Len(acl->deny.ips); i++) { - if (FuzzySetMatch(StrList_At(acl->deny.ips, i), ipaddr) == 0 || + if (IPRangeMatch(StrList_At(acl->deny.ips, i), ipaddr) || /* Legacy regex matching, TODO DEPRECATE */ StringMatchFull(StrList_At(acl->deny.ips, i), ipaddr)) { diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index dcfd8621fbc..b4d218b7e58 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -28,668 +28,692 @@ #include #include -#define CF_ADDRSIZE 128 +#define IPV4_OCTETS 4 +#define IPV6_GROUPS 8 +/* Large enough for the binary form of both IPv4 and IPv6 addresses */ +#define BINARY_ADDR_SIZE sizeof(struct in6_addr) -/* Match two IP strings - with : or . in hex or decimal - s1 is the test string, and s2 is the reference e.g. - FuzzySetMatch("128.39.74.10/23","128.39.75.56") == 0 +/* Inclusive interval matched by one octet/group of a range pattern, + * e.g. "10-20", or "7" which is the interval 7-7. */ +typedef struct +{ + unsigned long from; + unsigned long to; +} Interval; + +/* Notation used by an address pattern, see IPRangeMatch() */ +typedef enum +{ + PATTERN_PREFIX, // "10.1.2.3", "10.1.2" + PATTERN_CIDR, // "10.1.2.0/24", "2001:db8::/32" + PATTERN_RANGE, // "10.1.2.1-50", "2001:db8:0-ff::1" +} PatternNotation; - Returns 0 on match. */ +/******************************************************************************/ +/* Parsing helpers */ +/******************************************************************************/ -/* TODO rename to AddrSubnetMatch() */ -int FuzzySetMatch(const char *s1, const char *s2) +/** + * Parses the unsigned number in [start, end), written in the given base + * (10 or 16). Unlike strtoul(), no sign, whitespace or "0x" is accepted. + * + * @return false if the input is empty, has invalid digits or exceeds max + */ +static bool ParseNumber(const char *start, const char *end, int base, + unsigned long max, unsigned long *value) { - short isCIDR = false, isrange = false, isv6 = false, isv4 = false; - char address[CF_ADDRSIZE]; + assert(start != NULL); + assert(end != NULL); + assert(start <= end); + assert(base == 10 || base == 16); - if (strcmp(s1, s2) == 0) + if (start == end) { - return 0; + return false; } - if (strstr(s1, "/") != 0) + unsigned long result = 0; + for (const char *c = start; c < end; c++) { - isCIDR = true; - } + const unsigned char ch = *c; + unsigned long digit; + if (isdigit(ch)) + { + digit = ch - '0'; + } + else if (base == 16 && isxdigit(ch)) + { + digit = tolower(ch) - 'a' + 10; + } + else + { + return false; + } - if (strstr(s1, "-") != 0) - { - isrange = true; + if (result > (max - digit) / base) + { + return false; + } + result = result * base + digit; } - if (strstr(s1, ".") != 0) - { - isv4 = true; - } + *value = result; + return true; +} - if (strstr(s1, ":") != 0) - { - isv6 = true; - } +/** + * Parses "N" or "FROM-TO" in [start, end) into an interval. + * + * @return false if a number is invalid, exceeds max, or FROM > TO + */ +static bool ParseInterval(const char *start, const char *end, int base, + unsigned long max, Interval *interval) +{ + assert(interval != NULL); - if (strstr(s2, ".") != 0) + const char *dash = memchr(start, '-', end - start); + if (dash == NULL) { - isv4 = true; + if (!ParseNumber(start, end, base, max, &interval->from)) + { + return false; + } + interval->to = interval->from; + return true; } - if (strstr(s2, ":") != 0) - { - isv6 = true; - } + return ParseNumber(start, dash, base, max, &interval->from) && + ParseNumber(dash + 1, end, base, max, &interval->to) && + interval->from <= interval->to; +} - if (isv4 && isv6) +/** + * Parses an IPv4 pattern of 1 to 4 '.'-separated octets, each of which is a + * decimal number or range, e.g. "10.0.0.1-50", "10.1-5" or "10.1.2.3". + * + * @return the number of octets parsed, or 0 if the pattern is invalid + */ +static size_t ParseIPv4Pattern(const char *pattern, Interval octets[IPV4_OCTETS]) +{ + const char *start = pattern; + for (size_t count = 0; count < IPV4_OCTETS; count++) { - /* This is just wrong */ - return -1; - } + const char *end = start + strcspn(start, "."); + if (!ParseInterval(start, end, 10, 255, &octets[count])) + { + return 0; + } - if (isCIDR && isrange) - { - Log(LOG_LEVEL_ERR, "Cannot mix CIDR notation with xxx-yyy range notation '%s'", s1); - return -1; + if (*end == '\0') + { + return count + 1; + } + start = end + 1; } - if (!(isv6 || isv4)) + return 0; // More than 4 octets +} + +/** + * Parses up to max ':'-separated hex groups (numbers or ranges) in + * [start, end). An empty input yields 0 groups. + */ +static bool ParseIPv6Groups(const char *start, const char *end, + Interval *groups, size_t max, size_t *count) +{ + assert(start <= end); + + *count = 0; + if (start == end) { - Log(LOG_LEVEL_ERR, "Not a valid address range - or not a fully qualified name '%s'", s1); - return -1; + return true; } - if (!(isrange || isCIDR)) + while (*count < max) { - if (strlen(s2) > strlen(s1)) + const char *sep = memchr(start, ':', end - start); + const char *group_end = (sep != NULL) ? sep : end; + if (!ParseInterval(start, group_end, 16, 0xFFFF, &groups[*count])) { - if (*(s2 + strlen(s1)) != '.') - { - return -1; // Because xxx.1 should not match xxx.12 in the same octet - } + return false; } + (*count)++; - return strncmp(s1, s2, strlen(s1)); /* do partial string match */ - } - - if (isv4) - { - if (isCIDR) + if (sep == NULL) { - struct sockaddr_in addr1, addr2; - unsigned long mask; - - // Checks that the addr to match is a valid address - // else bogus addresses will always match /0 range - if (inet_pton(AF_INET, s2, &addr2.sin_addr) != 1) { - Log(LOG_LEVEL_ERR, "Invalid reference IPv4: %s", s2); - return -1; - } - - address[0] = '\0'; - int ret = sscanf(s1, "%16[^/]/%lu", address, &mask); - if (ret != 2 || mask > 32) - { - Log(LOG_LEVEL_ERR, "Invalid IPv4 CIDR: %s", s1); - return -1; - } - else if (mask == 0) - { - return 0; /* /0 CIDR matches everything */ - } - - inet_pton(AF_INET, address, &addr1.sin_addr); - - unsigned long a1 = htonl(addr1.sin_addr.s_addr); - unsigned long a2 = htonl(addr2.sin_addr.s_addr); - - unsigned long shift = 32 - mask; - assert(shift < 32); /* Undefined behaviour if 32 */ - - a1 = a1 >> shift; - a2 = a2 >> shift; - - if (a1 == a2) - { - return 0; - } - else - { - return -1; - } + return true; } - else - { - long i, from = -1, to = -1, cmp = -1; - char buffer1[64], buffer2[64]; + start = sep + 1; + } - const char *sp1 = s1; - const char *sp2 = s2; + return false; // Too many groups +} - for (i = 0; i < 4; i++) - { - buffer1[0] = '\0'; - sscanf(sp1, "%63[^.]", buffer1); - buffer1[63] = '\0'; - - if (strlen(buffer1) == 0) - { - break; - } - - sp1 += strlen(buffer1); - if (*sp1 == '.') - { - sp1++; - } - - buffer2[0] = '\0'; - sscanf(sp2, "%63[^.]", buffer2); - buffer2[63] = '\0'; - - if (strlen(buffer2) == 0) - { - /* s2 has fewer octets than the pattern: no match */ - return -1; - } - - sp2 += strlen(buffer2); - if (*sp2 == '.') - { - sp2++; - } - - if (strstr(buffer1, "-")) - { - sscanf(buffer1, "%ld-%ld", &from, &to); - sscanf(buffer2, "%ld", &cmp); - - if ((from < 0) || (to < 0)) - { - Log(LOG_LEVEL_DEBUG, "Couldn't read range"); - return -1; - } - - if ((from > cmp) || (cmp > to)) - { - Log(LOG_LEVEL_DEBUG, "Out of range %ld > %ld > %ld, range '%s'", from, cmp, to, buffer2); - return -1; - } - } - else - { - sscanf(buffer1, "%ld", &from); - sscanf(buffer2, "%ld", &cmp); - - if (from != cmp) - { - Log(LOG_LEVEL_DEBUG, "Unequal"); - return -1; - } - } - - Log(LOG_LEVEL_DEBUG, "Matched octet '%s' with '%s'", buffer1, buffer2); - } +/** + * Parses an IPv6 range pattern into exactly 8 groups, expanding "::", + * e.g. "2001:db8:1-5::1". + */ +static bool ParseIPv6RangePattern(const char *pattern, Interval groups[IPV6_GROUPS]) +{ + const char *end = pattern + strlen(pattern); + const char *gap = strstr(pattern, "::"); + size_t count; - Log(LOG_LEVEL_DEBUG, "Matched IP range"); - return 0; + if (gap == NULL) + { + if (!ParseIPv6Groups(pattern, end, groups, IPV6_GROUPS, &count) || + count != IPV6_GROUPS) + { + Log(LOG_LEVEL_VERBOSE, + "Invalid IPv6 range '%s': expected 8 valid groups, or '::'", + pattern); + return false; } + return true; } - if (isv6) + if (StringContains(gap + 1, "::")) { - int i; - - if (isCIDR) - { - struct sockaddr_in6 addr1 = {0}; - struct sockaddr_in6 addr2 = {0}; - unsigned long mask; - - address[0] = '\0'; - int ret = sscanf(s1, "%40[^/]/%lu", address, &mask); - if (ret != 2 || mask > 128) - { - Log(LOG_LEVEL_ERR, "Invalid IPv6 CIDR: %s", s1); - return -1; - } - int blocks = mask / 8; - int remaining_bits = mask % 8; - - addr1.sin6_family = AF_INET6; - if (inet_pton(AF_INET6, address, &addr1.sin6_addr) != 1) - { - return -1; - } - addr2.sin6_family = AF_INET6; - if (inet_pton(AF_INET6, s2, &addr2.sin6_addr) != 1) - { - return -1; - } + Log(LOG_LEVEL_VERBOSE, + "Invalid IPv6 range '%s': '::' may only appear once", pattern); + return false; + } - for (i = 0; i < blocks; i++) - { - if (addr1.sin6_addr.s6_addr[i] != addr2.sin6_addr.s6_addr[i]) - { - return -1; - } - } + // "::" stands for at least one zero group + size_t head_count, tail_count; + Interval tail[IPV6_GROUPS]; + if (!ParseIPv6Groups(pattern, gap, groups, IPV6_GROUPS - 1, &head_count) || + !ParseIPv6Groups(gap + 2, end, tail, IPV6_GROUPS - 1 - head_count, &tail_count)) + { + Log(LOG_LEVEL_VERBOSE, + "Invalid IPv6 range '%s': invalid group, or more than 7 groups besides '::'", + pattern); + return false; + } - if (remaining_bits > 0) - { - uint8_t bitmask = (uint8_t)(0xFF << (8 - remaining_bits)); + const size_t zero_count = IPV6_GROUPS - head_count - tail_count; + for (size_t i = head_count; i < head_count + zero_count; i++) + { + groups[i] = (Interval) { .from = 0, .to = 0 }; + } + memcpy(groups + head_count + zero_count, tail, tail_count * sizeof(*tail)); + return true; +} - if ((addr1.sin6_addr.s6_addr[blocks] & bitmask) != - (addr2.sin6_addr.s6_addr[blocks] & bitmask)) - { - return -1; - } - } - return 0; - } - else - { - long i, from = -1, to = -1, cmp = -1; - char buffer1[64], buffer2[64]; +/** + * Parses a CIDR pattern "ADDRESS/BITS" of the given family. + * + * @param address receives the binary network address (BINARY_ADDR_SIZE bytes) + */ +static bool ParseCIDR(const char *pattern, int family, + unsigned char *address, unsigned long *bits) +{ + assert(family == AF_INET || family == AF_INET6); + const char *family_name = (family == AF_INET) ? "IPv4" : "IPv6"; - const char *sp1 = s1; - const char *sp2 = s2; + const char *slash = strchr(pattern, '/'); + if (slash == NULL) + { + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR '%s': missing '/'", + family_name, pattern); + return false; + } - for (i = 0; i < 8; i++) - { - buffer1[0] = '\0'; - sscanf(sp1, "%63[^:]", buffer1); - buffer1[63] = '\0'; - - sp1 += strlen(buffer1); - if (*sp1 == ':') - { - sp1++; - } - - buffer2[0] = '\0'; - sscanf(sp2, "%63[^:]", buffer2); - buffer2[63] = '\0'; - - sp2 += strlen(buffer2); - if (*sp2 == ':') - { - sp2++; - } - - if (strstr(buffer1, "-")) - { - sscanf(buffer1, "%lx-%lx", &from, &to); - sscanf(buffer2, "%lx", &cmp); - - if (from < 0 || to < 0) - { - return -1; - } - - if ((from >= cmp) || (cmp > to)) - { - Log(LOG_LEVEL_DEBUG, "%lx < %lx < %lx", from, cmp, to); - return -1; - } - } - else - { - sscanf(buffer1, "%ld", &from); - sscanf(buffer2, "%ld", &cmp); - - if (from != cmp) - { - return -1; - } - } - } + char address_str[CF_MAX_IP_LEN]; + const size_t address_len = slash - pattern; + if (address_len >= sizeof(address_str)) + { + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR '%s': address is too long", + family_name, pattern); + return false; + } + memcpy(address_str, pattern, address_len); + address_str[address_len] = '\0'; - return 0; - } + if (inet_pton(family, address_str, address) != 1) + { + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR '%s': '%s' is not a valid %s address", + family_name, pattern, address_str, family_name); + return false; } - return -1; + const unsigned long max_bits = (family == AF_INET) ? 32 : 128; + if (!ParseNumber(slash + 1, slash + strlen(slash), 10, max_bits, bits)) + { + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR '%s': mask must be a number from 0 to %lu", + family_name, pattern, max_bits); + return false; + } + return true; } -bool FuzzyHostParse(const char *arg2) +/** + * Determines the notation of an address pattern: CIDR if it contains a '/', + * range if it contains a '-', otherwise prefix. + * + * @param pattern the address pattern, e.g. "10.1.2.0/24" + * @param notation receives the notation of the pattern + * @return true on success, false (and logs an error) if the pattern mixes + * CIDR and range notation, in which case notation is not set + */ +static bool GetPatternNotation(const char *pattern, PatternNotation *notation) { - long start = -1, end = -1; - int n; + const bool is_cidr = StringContainsChar(pattern, '/'); + const bool is_range = StringContainsChar(pattern, '-'); - n = sscanf(arg2, "%ld-%ld", &start, &end); - - if (n != 2) + if (is_cidr && is_range) { - Log(LOG_LEVEL_ERR, - "HostRange syntax error: second arg should have X-Y format where X and Y are decimal numbers"); + Log(LOG_LEVEL_VERBOSE, + "Cannot mix CIDR notation with xxx-yyy range notation '%s'", pattern); return false; } + if (is_cidr) + { + *notation = PATTERN_CIDR; + } + else if (is_range) + { + *notation = PATTERN_RANGE; + } + else + { + *notation = PATTERN_PREFIX; + } return true; } -int FuzzyHostMatch(const char *arg0, const char *arg1, const char *refhost) +/******************************************************************************/ +/* Matching */ +/******************************************************************************/ + +/** + * Plain string prefix match, at octet boundaries, i.e. "10.1.2" matches + * "10.1.2.3", but "10.1.2.1" does not match "10.1.2.12". + */ +static bool MatchPrefix(const char *pattern, const char *address) { - char *sp, refbase[1024]; - long cmp = -1, start = -1, end = -1; - char buf1[CF_BUFSIZE], buf2[CF_BUFSIZE]; + const size_t len = strlen(pattern); + return StringEqualN(pattern, address, len) && + (address[len] == '\0' || address[len] == '.'); +} - strlcpy(refbase, refhost, sizeof(refbase)); - sp = refbase + strlen(refbase) - 1; +/* True if the first bits of a and b are equal */ +static bool LeadingBitsEqual(const unsigned char *a, const unsigned char *b, + unsigned long bits) +{ + const size_t whole_bytes = bits / 8; + const unsigned int remaining_bits = bits % 8; - while (isdigit((int) *sp)) + if (memcmp(a, b, whole_bytes) != 0) { - sp--; + return false; } - - sp++; - sscanf(sp, "%ld", &cmp); - *sp = '\0'; - - if (cmp < 0) + if (remaining_bits == 0) { - return 1; + return true; } - if (strlen(refbase) == 0) - { - return 1; - } + const unsigned char mask = (unsigned char) (0xFF << (8 - remaining_bits)); + return (a[whole_bytes] & mask) == (b[whole_bytes] & mask); +} - sscanf(arg1, "%ld-%ld", &start, &end); +static bool MatchCIDR(const char *pattern, int family, const char *address) +{ + const char *family_name = (family == AF_INET) ? "IPv4" : "IPv6"; - if ((cmp < start) || (cmp > end)) + unsigned char network[BINARY_ADDR_SIZE]; + unsigned long bits; + if (!ParseCIDR(pattern, family, network, &bits)) { - return 1; + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR: %s", family_name, pattern); + return false; } - strlcpy(buf1, refbase, CF_BUFSIZE); - strlcpy(buf2, arg0, CF_BUFSIZE); - - ToLowerStrInplace(buf1); - ToLowerStrInplace(buf2); - - if (strcmp(buf1, buf2) != 0) + // Checks that the address to match is valid, otherwise bogus addresses + // would always match a /0 range + unsigned char addr[BINARY_ADDR_SIZE]; + if (inet_pton(family, address, addr) != 1) { - return 1; + Log(LOG_LEVEL_VERBOSE, "Invalid %s address: %s", family_name, address); + return false; } - return 0; + return LeadingBitsEqual(network, addr, bits); } -bool FuzzyMatchParse(const char *s) +/** + * Matches an IPv4 range pattern. A pattern with fewer than 4 octets matches + * on the leading octets only, e.g. "10.1-5" matches "10.3.7.8". + */ +static bool MatchIPv4Range(const char *pattern, const char *address) { - short isCIDR = false, isrange = false, isv6 = false, isv4 = false, isADDR = false; - char address[CF_ADDRSIZE]; - int mask, count = 0; + Interval octets[IPV4_OCTETS]; + const size_t count = ParseIPv4Pattern(pattern, octets); + if (count == 0) + { + Log(LOG_LEVEL_DEBUG, "Invalid IPv4 range '%s'", pattern); + return false; + } - for (const char *sp = s; *sp != '\0'; sp++) /* Is this an address or hostname */ + const char *start = address; + for (size_t i = 0; i < count; i++) { - if (!isxdigit((int) *sp)) + const char *end = start + strcspn(start, "."); + unsigned long value; + if (!ParseNumber(start, end, 10, 255, &value)) { - isADDR = false; - break; + Log(LOG_LEVEL_VERBOSE, "Invalid IPv4 address '%s'", address); + return false; } - - if (*sp == ':') /* Catches any ipv6 address */ + if (value < octets[i].from || value > octets[i].to) { - isADDR = true; - break; + return false; } - if (isdigit((int) *sp)) /* catch non-ipv4 address - no more than 3 digits */ + if (*end == '\0') { - count++; - if (count > 3) + if (i + 1 < count) { - isADDR = false; - break; + Log(LOG_LEVEL_VERBOSE, + "IPv4 address '%s' has fewer octets than range '%s'", + address, pattern); + return false; } + return true; } - else - { - count = 0; - } + start = end + 1; } - if (!isADDR) + return true; +} + +static bool MatchIPv6Range(const char *pattern, const char *address) +{ + Interval groups[IPV6_GROUPS]; + if (!ParseIPv6RangePattern(pattern, groups)) { - return true; + Log(LOG_LEVEL_DEBUG, "Invalid IPv6 range '%s'", pattern); + return false; } - if (strstr(s, "/") != 0) + struct in6_addr addr; + if (inet_pton(AF_INET6, address, &addr) != 1) { - isCIDR = true; + return false; } - if (strstr(s, "-") != 0) + for (size_t i = 0; i < IPV6_GROUPS; i++) { - isrange = true; + const unsigned long value = (addr.s6_addr[2 * i] << 8) | addr.s6_addr[2 * i + 1]; + if (value < groups[i].from || value > groups[i].to) + { + return false; + } } - if (strstr(s, ".") != 0) + return true; +} + +/******************************************************************************/ +/* Public API */ +/******************************************************************************/ + +/** + * Checks whether an IPv4 or IPv6 address is matched by a pattern, e.g. + * IPRangeMatch("128.39.74.10/23", "128.39.75.56") is true. + * + * The pattern may be a CIDR ("10.0.0.0/8", "2001:db8::/32"), a range + * ("10.0.0.1-50", "2001:db8:0-ff::1") or an address prefix ("10.1.2"). + */ +bool IPRangeMatch(const char *pattern, const char *address) +{ + if (StringEqual(pattern, address)) { - isv4 = true; + return true; } - if (strstr(s, ":") != 0) + const bool is_v4 = (StringContainsChar(pattern, '.') || StringContainsChar(address, '.')); + const bool is_v6 = (StringContainsChar(pattern, ':') || StringContainsChar(address, ':')); + + if (is_v4 && is_v6) { - isv6 = true; + return false; // Mixed IPv4 and IPv6 } - if (isv4 && isv6) + PatternNotation notation; + if (!GetPatternNotation(pattern, ¬ation)) { - Log(LOG_LEVEL_ERR, "Mixture of IPv6 and IPv4 addresses"); return false; } - if (isCIDR && isrange) + if (!is_v4 && !is_v6) { - Log(LOG_LEVEL_ERR, "Cannot mix CIDR notation with xx-yy range notation"); + Log(LOG_LEVEL_VERBOSE, "Not a valid address range - or not a fully qualified name '%s'", pattern); return false; } - if (isv4 && isCIDR) + bool match = false; + switch (notation) { - if (strlen(s) > 4 + 3 * 4 + 1 + 2) /* xxx.yyy.zzz.mmm/cc */ - { - Log(LOG_LEVEL_ERR, "IPv4 address looks too long"); - return false; - } - - address[0] = '\0'; - mask = 0; - sscanf(s, "%16[^/]/%d", address, &mask); - - if (mask < 8) - { - Log(LOG_LEVEL_ERR, "Mask value %d in '%s' is less than 8", mask, s); - return false; - } - - if (mask > 30) - { - Log(LOG_LEVEL_ERR, "Mask value %d in '%s' is silly (> 30)", mask, s); - return false; - } + case PATTERN_PREFIX: + match = MatchPrefix(pattern, address); + break; + case PATTERN_CIDR: + match = MatchCIDR(pattern, is_v4 ? AF_INET : AF_INET6, address); + break; + case PATTERN_RANGE: + match = is_v4 ? MatchIPv4Range(pattern, address) : MatchIPv6Range(pattern, address); + break; } - if (isv4 && isrange) - { - long i, from = -1, to = -1; - char buffer1[64]; - - const char *sp1 = s; - - for (i = 0; i < 4; i++) - { - buffer1[0] = '\0'; - sscanf(sp1, "%63[^.]", buffer1); - buffer1[63] = '\0'; - - if (strlen(buffer1) == 0) - { - break; - } + return match; +} - sp1 += strlen(buffer1); - if (*sp1 == '.') - { - sp1++; - } +/** + * Checks that range has the form "X-Y", where X and Y are decimal numbers, + * as used by HostRangeMatch(). + */ +bool HostRangeIsValid(const char *range) +{ + long start = -1, end = -1; + int n; - if (strstr(buffer1, "-")) - { - sscanf(buffer1, "%ld-%ld", &from, &to); + n = sscanf(range, "%ld-%ld", &start, &end); - if ((from < 0) || (to < 0)) - { - Log(LOG_LEVEL_ERR, "Error in IP range - looks like address, or bad hostname"); - return false; - } + if (n != 2) + { + Log(LOG_LEVEL_ERR, + "HostRange syntax error: second arg should have X-Y format where X and Y are decimal numbers"); + return false; + } - if (to < from) - { - Log(LOG_LEVEL_ERR, "Bad IP range"); - return false; - } + return true; +} - } - } +/** + * Checks whether hostname is prefix followed by a number within range, + * e.g. HostRangeMatch("host", "1-10", "HOST5") is true. + * The comparison of the prefix is case-insensitive. + */ +bool HostRangeMatch(const char *prefix, const char *range, const char *hostname) +{ + // Split hostname into base name and trailing number, e.g. "host12" + const size_t len = strlen(hostname); + size_t base_len = len; + while (base_len > 0 && isdigit((unsigned char) hostname[base_len - 1])) + { + base_len--; } - if (isv6 && isCIDR) + if (base_len == 0 || base_len == len) { - char address[CF_ADDRSIZE]; - int mask; - - if (strlen(s) < 20) - { - Log(LOG_LEVEL_ERR, "IPv6 address looks too short"); - return false; - } + return false; // No base name, or no number + } - if (strlen(s) > 42) - { - Log(LOG_LEVEL_ERR, "IPv6 address looks too long"); - return false; - } + unsigned long number; + if (!ParseNumber(hostname + base_len, hostname + len, 10, LONG_MAX, &number)) + { + return false; // Number too large + } - address[0] = '\0'; - mask = 0; - sscanf(s, "%40[^/]/%d", address, &mask); + long start = -1, end = -1; + sscanf(range, "%ld-%ld", &start, &end); + if ((long) number < start || (long) number > end) + { + return false; + } - if (mask > 15) - { - Log(LOG_LEVEL_ERR, "IPv6 CIDR mask is too large"); - return false; - } + if (strlen(prefix) != base_len || + !StringEqualN_IgnoreCase(prefix, hostname, base_len)) + { + return false; } return true; } - /** - * Simple check to avoid writing to illegal memory addresses. - * NOT a proper test for valid IP. + * Checks that pattern is an address pattern understood by IPRangeMatch(). */ -static AddressType AddressTypeCheckValidity(char *s, AddressType address_type) +bool IPRangeIsValid(const char *pattern) { - if(NULL_OR_EMPTY(s)) + const bool is_v4 = StringContainsChar(pattern, '.'); + const bool is_v6 = StringContainsChar(pattern, ':'); + + if (is_v4 && is_v6) { - return ADDRESS_TYPE_OTHER; + Log(LOG_LEVEL_VERBOSE, "Mixture of IPv6 and IPv4 addresses in '%s'", pattern); + return false; } - if(strlen(s) >= CF_MAX_IP_LEN) + + if (!is_v4 && !is_v6) { - return ADDRESS_TYPE_OTHER; + Log(LOG_LEVEL_VERBOSE, "'%s' is not an IP address, range or subnet", pattern); + return false; } - return address_type; + + PatternNotation notation; + if (!GetPatternNotation(pattern, ¬ation)) + { + return false; + } + + bool valid = false; + if (notation == PATTERN_CIDR) + { + unsigned char network[BINARY_ADDR_SIZE]; + unsigned long bits; + valid = ParseCIDR(pattern, is_v4 ? AF_INET : AF_INET6, network, &bits); + } + else if (is_v4) // Range or prefix + { + Interval octets[IPV4_OCTETS]; + valid = (ParseIPv4Pattern(pattern, octets) > 0); + } + else if (notation == PATTERN_RANGE) // IPv6 + { + Interval groups[IPV6_GROUPS]; + valid = ParseIPv6RangePattern(pattern, groups); + } + else // IPv6 full address, or prefix of 1-7 groups + { + struct in6_addr addr; + Interval groups[IPV6_GROUPS - 1]; + size_t count; + valid = inet_pton(AF_INET6, pattern, &addr) == 1 || + (ParseIPv6Groups(pattern, pattern + strlen(pattern), groups, IPV6_GROUPS - 1, &count) && + count > 0); + } + + if (!valid) + { + Log(LOG_LEVEL_VERBOSE, "Invalid IP address, range or subnet '%s'", pattern); + } + return valid; } + /** * Parses "hostname:port" or "[hostname]:port", where hostname may also be * IPv4 or IPv6 address string. * * @param hostname will point to the hostname, or NULL if no or empty hostname * @param port will point to the port, or NULL if no or empty port + * @return the type of hostname: ADDRESS_TYPE_IPV4 if it is a valid IPv4 + * address, ADDRESS_TYPE_IPV6 if it contains a ':' (not validated), + * otherwise (hostname, invalid or too long) ADDRESS_TYPE_OTHER * @WARNING modifies #s to '\0' terminate hostname if followed by port. */ AddressType ParseHostPort(char *s, char **hostname, char **port) { s = TrimWhitespace(s); - if ( NULL_OR_EMPTY(s) ) + if (NULL_OR_EMPTY(s)) { *hostname = NULL; *port = NULL; return ADDRESS_TYPE_OTHER; } - AddressType address_type = ADDRESS_TYPE_OTHER; - char *h, *p; // hostname, port temporaries - - h = s; - p = NULL; + char *host = s; + char *port_str = NULL; + bool is_ipv6 = false; + bool classify = true; - char *first_colon = strchr(s, ':'); - char *first_dot = strchr(s, '.'); - - if (s[0] == '[') // [host or ip]:port + if (s[0] == '[') // [host or ip]:port { - h = s + 1; - p = strchr(h, ']'); - if (p != NULL) + host = s + 1; + char *closing = strchr(host, ']'); + if (closing != NULL) { - if (first_colon != NULL && first_colon < p) + *closing = '\0'; + is_ipv6 = StringContainsChar(host, ':'); + if (closing[1] == ':') { - address_type = ADDRESS_TYPE_IPV6; - } - else if (isdigit(h[0])) - { - address_type = ADDRESS_TYPE_IPV4; - } // (else it's other by default) - - *p = '\0'; // '\0' terminate host name - if (p[1] == ':') // move port* forward - { - p += 2; + port_str = closing + 2; } } - } - else if (first_colon == NULL) // localhost, 192.168.0.1 - { - if (isdigit(h[0])) + else { - address_type = ADDRESS_TYPE_IPV4; + classify = false; // Unterminated bracket } } - else if (first_dot == NULL || first_colon < first_dot) + else { - // If only one colon: (cfengine.com:222 or localhost:) - if (strchr(first_colon + 1, ':') == NULL) - { - *first_colon = '\0'; - p = first_colon + 1; - } - else // Multiple colons: + char *first_colon = strchr(s, ':'); + char *first_dot = strchr(s, '.'); + + if (first_colon != NULL) { - address_type = ADDRESS_TYPE_IPV6; + // "1.2.3.4:80", "cfengine.com:80", "localhost:80" or "localhost:" + if ((first_dot != NULL && first_dot < first_colon) || + !StringContainsChar(first_colon + 1, ':')) + { + *first_colon = '\0'; + port_str = first_colon + 1; + } + else // Multiple colons: IPv6 address, no port + { + is_ipv6 = true; + } } } - else // (first_dot < first_colon) : IPv4 or hostname + + *hostname = (host[0] != '\0') ? host : NULL; + *port = (port_str != NULL && port_str[0] != '\0') ? port_str : NULL; + + if (!classify || *hostname == NULL || strlen(*hostname) >= CF_MAX_IP_LEN) { - p = strchr(h, ':'); - if (p != NULL) - { - *p = '\0'; // '\0'-terminate hostname - p++; - } - if (isdigit(h[0])) - { - address_type = ADDRESS_TYPE_IPV4; - } + return ADDRESS_TYPE_OTHER; + } + if (is_ipv6) + { + return ADDRESS_TYPE_IPV6; } - *hostname = (h[0] != '\0') ? h : NULL; - *port = (p != NULL && p[0] != '\0') ? p : NULL; - - return AddressTypeCheckValidity(*hostname, address_type); + struct in_addr addr; + return (inet_pton(AF_INET, *hostname, &addr) == 1) + ? ADDRESS_TYPE_IPV4 + : ADDRESS_TYPE_OTHER; } diff --git a/libcfnet/addr_lib.h b/libcfnet/addr_lib.h index aba84c91ba0..6cf0fd7c8fa 100644 --- a/libcfnet/addr_lib.h +++ b/libcfnet/addr_lib.h @@ -27,11 +27,10 @@ #include -bool IsLoopbackAddress(const char *address); -int FuzzySetMatch(const char *s1, const char *s2); -bool FuzzyHostParse(const char *arg2); -int FuzzyHostMatch(const char *arg0, const char *arg1, const char *basename); -bool FuzzyMatchParse(const char *item); +bool IPRangeMatch(const char *pattern, const char *address); +bool IPRangeIsValid(const char *pattern); +bool HostRangeMatch(const char *prefix, const char *range, const char *hostname); +bool HostRangeIsValid(const char *range); typedef enum { ADDRESS_TYPE_OTHER, // Hostname or invalid diff --git a/libpromises/evalfunction.c b/libpromises/evalfunction.c index d42df8c6fca..c5f35be7757 100644 --- a/libpromises/evalfunction.c +++ b/libpromises/evalfunction.c @@ -6476,7 +6476,7 @@ static FnCallResult FnCallIPRange(EvalContext *ctx, ARG_UNUSED const Policy *pol const char *range = RlistScalarValue(finalargs); const Rlist *ifaces = finalargs->next; - if (!FuzzyMatchParse(range)) + if (!IPRangeIsValid(range)) { Log(LOG_LEVEL_VERBOSE, "%s(%s): argument is not a valid address range", @@ -6488,7 +6488,7 @@ static FnCallResult FnCallIPRange(EvalContext *ctx, ARG_UNUSED const Policy *pol ip != NULL; ip = ip->next) { - if (FuzzySetMatch(range, ip->name) == 0) + if (IPRangeMatch(range, ip->name)) { /* * MODE1: iprange(range) @@ -6548,7 +6548,7 @@ static FnCallResult FnCallIsIpInSubnet(ARG_UNUSED EvalContext *ctx, const char *range = RlistScalarValue(finalargs); const Rlist *ips = finalargs->next; - if (!FuzzyMatchParse(range)) + if (!IPRangeIsValid(range)) { Log(LOG_LEVEL_VERBOSE, "%s(%s): argument is not a valid address range", @@ -6560,7 +6560,7 @@ static FnCallResult FnCallIsIpInSubnet(ARG_UNUSED EvalContext *ctx, { const char *ip_s = RlistScalarValue(ip); - if (FuzzySetMatch(range, ip_s) == 0) + if (IPRangeMatch(range, ip_s)) { Log(LOG_LEVEL_DEBUG, "%s(%s): Match on IP '%s'", fp->name, range, ip_s); @@ -6578,12 +6578,12 @@ static FnCallResult FnCallHostRange(ARG_UNUSED EvalContext *ctx, ARG_UNUSED cons char *prefix = RlistScalarValue(finalargs); char *range = RlistScalarValue(finalargs->next); - if (!FuzzyHostParse(range)) + if (!HostRangeIsValid(range)) { return FnFailure(); } - return FnReturnContext(FuzzyHostMatch(prefix, range, VUQNAME) == 0); + return FnReturnContext(HostRangeMatch(prefix, range, VUQNAME)); } /*********************************************************************/ diff --git a/libpromises/item_lib.c b/libpromises/item_lib.c index fd697f328ff..8d36f3febcd 100644 --- a/libpromises/item_lib.c +++ b/libpromises/item_lib.c @@ -789,7 +789,7 @@ bool IsMatchItemIn(const Item *list, const char *item) CYCLE_DECLARE(ptr, slow, toggle); while (ptr != NULL) { - if (FuzzySetMatch(ptr->name, item) == 0 || + if (IPRangeMatch(ptr->name, item) || (IsRegex(ptr->name) && StringMatchFull(ptr->name, item))) { diff --git a/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.error.pass.cf b/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.cf similarity index 92% rename from tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.error.pass.cf rename to tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.cf index d1929a2a910..c23da722aea 100644 --- a/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.error.pass.cf +++ b/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.cf @@ -2,9 +2,6 @@ # # Test isipinsubnet() # -# This is an .error.pass.cf test, because isipinsubnet() is given -# 'not-an-ip-address' on purpose and logs an error. -# ####################################################### body common control { diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index 9600eecdeab..3a9d7cf8378 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -122,6 +122,11 @@ static void test_ParseHostPort() /***** CORNER CASES *****/ + ParseHostPort(strcpy(test_string, "[1.2.3.4"), &hostname, &port); + assert_string_equal(hostname, "1.2.3.4"); + assert_int_equal(port, NULL); + hostname = NULL; port = NULL; + ParseHostPort(strcpy(test_string, ""), &hostname, &port); assert_int_equal(hostname, NULL); assert_int_equal(port, NULL); @@ -143,42 +148,208 @@ static void test_ParseHostPort() hostname = NULL; port = NULL; } -static void test_FuzzySetMatch() +static void test_ParseHostPort_address_type() +{ + char *hostname, *port; + char test_string[64]; + +#define ASSERT_ADDRESS_TYPE(input, type) \ + assert_int_equal(ParseHostPort(strcpy(test_string, input), &hostname, &port), type) + + ASSERT_ADDRESS_TYPE("1.2.3.4", ADDRESS_TYPE_IPV4); + ASSERT_ADDRESS_TYPE("1.2.3.4:80", ADDRESS_TYPE_IPV4); + ASSERT_ADDRESS_TYPE("[1.2.3.4]:80", ADDRESS_TYPE_IPV4); + ASSERT_ADDRESS_TYPE("ffff::dd:12:34", ADDRESS_TYPE_IPV6); + ASSERT_ADDRESS_TYPE("[ffff::dd:12:34]:80", ADDRESS_TYPE_IPV6); + ASSERT_ADDRESS_TYPE("::ffff:192.0.2.128", ADDRESS_TYPE_IPV6); + ASSERT_ADDRESS_TYPE("fe80::1%eth0", ADDRESS_TYPE_IPV6); + ASSERT_ADDRESS_TYPE("www.cfengine.com:80", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("localhost", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("[1.2.3.4", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("", ADDRESS_TYPE_OTHER); + + /* Hostnames starting with a digit are not IPv4 addresses */ + ASSERT_ADDRESS_TYPE("1password.com", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("1password.com:80", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("[1password.com]:80", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("1.2.3", ADDRESS_TYPE_OTHER); + +#undef ASSERT_ADDRESS_TYPE +} + +#define ASSERT_MATCH(pattern, address) \ + assert_true(IPRangeMatch(pattern, address)) +#define ASSERT_NO_MATCH(pattern, address) \ + assert_false(IPRangeMatch(pattern, address)) + +static void test_IPRangeMatch_ipv4() +{ + /* Prefix */ + ASSERT_MATCH("1.2.3", "1.2.3.4"); + ASSERT_NO_MATCH("1.2.3", "1.2.34.5"); + ASSERT_NO_MATCH("1.2.3.4", "1.2.3.45"); + + /* Range */ + ASSERT_MATCH("10.0.0.1-50", "10.0.0.1"); + ASSERT_MATCH("10.0.0.1-50", "10.0.0.25"); + ASSERT_MATCH("10.0.0.1-50", "10.0.0.50"); + ASSERT_NO_MATCH("10.0.0.1-50", "10.0.0.0"); + ASSERT_NO_MATCH("10.0.0.1-50", "10.0.0.51"); + ASSERT_NO_MATCH("10.0.0.1-50", "10.0.1.25"); + ASSERT_MATCH("10.1-5", "10.3.7.8"); + ASSERT_NO_MATCH("10.1-5", "10.6.7.8"); + ASSERT_NO_MATCH("10.0.0.1-50", "10.0.0.abc"); + ASSERT_NO_MATCH("10.0.0.50-1", "10.0.0.25"); + ASSERT_NO_MATCH("10.x.0.1-50", "10.0.0.25"); + /* Octets that failed to parse used to reuse the previous octet's value */ + ASSERT_NO_MATCH("10.x.0.1-50", "10.10.0.25"); + ASSERT_NO_MATCH("10.0.5.1-50", "10.0.5.abc"); + + /* CIDR */ + ASSERT_MATCH("10.0.0.0/8", "10.200.3.4"); + ASSERT_NO_MATCH("10.0.0.0/8", "11.0.0.1"); + ASSERT_MATCH("8.8.8.8/31", "8.8.8.9"); + ASSERT_NO_MATCH("8.8.8.8/31", "8.8.8.10"); + ASSERT_MATCH("1.2.3.4/32", "1.2.3.4"); + ASSERT_MATCH("0.0.0.0/0", "1.2.3.4"); + ASSERT_NO_MATCH("0.0.0.0/0", "not-an-ip-address"); + ASSERT_NO_MATCH("1.2.3.4/33", "1.2.3.4"); + /* The pattern's address used to be read uninitialized when invalid */ + ASSERT_NO_MATCH("bogus.address/0", "1.2.3.4"); + ASSERT_NO_MATCH("1.2.3.999/24", "1.2.3.4"); + + /* Mixed notations */ + ASSERT_NO_MATCH("10.0.0.1-5/8", "10.0.0.1"); + ASSERT_NO_MATCH("10.0.0.0/8", "::1"); +} + +static void test_IPRangeMatch_ipv6() +{ + /* CIDR */ + ASSERT_MATCH("2001:db8::/32", "2001:db8:ffff::1"); + ASSERT_NO_MATCH("2001:db8::/32", "2001:db9::1"); + ASSERT_MATCH("2001:db8::/31", "2001:db9::1"); + ASSERT_MATCH("::/0", "fe80::1"); + ASSERT_NO_MATCH("2001:db8::/129", "2001:db8::1"); + + /* Range: both bounds are inclusive */ + ASSERT_MATCH("2001:0-ffff:0:0:0:0:0:1", "2001:0:0:0:0:0:0:1"); + ASSERT_MATCH("2001:0-ffff:0:0:0:0:0:1", "2001:ffff:0:0:0:0:0:1"); + ASSERT_MATCH("2001:10-20:0:0:0:0:0:1", "2001:10:0:0:0:0:0:1"); + ASSERT_MATCH("2001:10-20:0:0:0:0:0:1", "2001:20:0:0:0:0:0:1"); + ASSERT_NO_MATCH("2001:10-20:0:0:0:0:0:1", "2001:21:0:0:0:0:0:1"); + + /* Groups are hexadecimal */ + ASSERT_MATCH("2001:a-f:0:0:0:0:0:1", "2001:c:0:0:0:0:0:1"); + ASSERT_NO_MATCH("fe80:1-3:0:0:0:0:0:1", "fe81:2:0:0:0:0:0:1"); + ASSERT_NO_MATCH("2001:1-3:0:0:0:0:0:a", "2001:2:0:0:0:0:0:b"); + ASSERT_MATCH("2001:1-3:0:0:0:0:0:A", "2001:2:0:0:0:0:0:a"); + + /* "::" compression in pattern and address */ + ASSERT_MATCH("2001:db8:1-5::1", "2001:db8:3::1"); + ASSERT_MATCH("2001:db8:1-5::1", "2001:db8:3:0:0:0:0:1"); + ASSERT_MATCH("2001:db8:1-5:0:0:0:0:1", "2001:db8:3::1"); + ASSERT_NO_MATCH("2001:db8:1-5::1", "2001:db8:3::2"); + ASSERT_MATCH("::1-5", "::3"); + ASSERT_MATCH("fe80::0-ff:1", "fe80::12:1"); + + /* Invalid range patterns never match */ + ASSERT_NO_MATCH("2001:db8:1-5", "2001:db8:3::1"); + ASSERT_NO_MATCH("2001::db8::1-5", "2001::db8:0:0:3"); + ASSERT_NO_MATCH("2001:db8:1-10000::1", "2001:db8:3::1"); +} + +#undef ASSERT_MATCH +#undef ASSERT_NO_MATCH + +static void test_IPRangeMatch() { /* Sanity: exact and subnet matches still work. */ - assert_int_equal(FuzzySetMatch("128.39.74.10/23", "128.39.75.56"), 0); - assert_int_not_equal(FuzzySetMatch("128.39.74.10/24", "128.39.75.56"), 0); - assert_int_equal(FuzzySetMatch("1.2.3.4", "1.2.3.4"), 0); - assert_int_not_equal(FuzzySetMatch("1.2.3.4", "1.2.3.5"), 0); + assert_true(IPRangeMatch("128.39.74.10/23", "128.39.75.56")); + assert_false(IPRangeMatch("128.39.74.10/24", "128.39.75.56")); + assert_true(IPRangeMatch("1.2.3.4", "1.2.3.4")); + assert_false(IPRangeMatch("1.2.3.4", "1.2.3.5")); /* An address with fewer octets than the pattern used to walk the scan * pointer past the end of the string. Heap-allocate the address so that * the over-read is caught under ASan. It must simply not match. */ char *s = xstrdup("1"); - assert_int_not_equal(FuzzySetMatch("1.2.3.4", s), 0); + assert_false(IPRangeMatch("1.2.3.4", s)); free(s); s = xstrdup("1.2"); - assert_int_not_equal(FuzzySetMatch("1.2.3.4", s), 0); + assert_false(IPRangeMatch("1.2.3.4", s)); free(s); /* Same for an IPv6 range pattern against a short address. */ s = xstrdup("2001"); - assert_int_not_equal(FuzzySetMatch("2001:0-ffff:0:0:0:0:0:1", s), 0); + assert_false(IPRangeMatch("2001:0-ffff:0:0:0:0:0:1", s)); free(s); } -static void test_FuzzyMatchParse() +static void test_IPRangeIsValid() { - assert_true(FuzzyMatchParse("1.2.3.4")); - assert_true(FuzzyMatchParse("1-10.2.3.4")); + assert_true(IPRangeIsValid("1.2.3.4")); + assert_true(IPRangeIsValid("1-10.2.3.4")); + assert_true(IPRangeIsValid("1.2.3")); + assert_true(IPRangeIsValid("192.168.1.50-140")); + assert_true(IPRangeIsValid("0.0.0.0/0")); + assert_true(IPRangeIsValid("8.8.8.8/31")); + assert_true(IPRangeIsValid("1.2.3.4/32")); + assert_true(IPRangeIsValid("2001:db8::1")); + assert_true(IPRangeIsValid("2001:db8")); + assert_true(IPRangeIsValid("2001:db8::/64")); + assert_true(IPRangeIsValid("::/0")); + assert_true(IPRangeIsValid("2001:db8:1-5::1")); + assert_true(IPRangeIsValid("2001:0-ffff:0:0:0:0:0:1")); + + assert_false(IPRangeIsValid("")); + assert_false(IPRangeIsValid("localhost")); + assert_false(IPRangeIsValid("www.cfengine.com")); + assert_false(IPRangeIsValid("1.2.3.4.5")); + assert_false(IPRangeIsValid("1.2.3.")); + assert_false(IPRangeIsValid("1.2.3.256")); + assert_false(IPRangeIsValid("10.5-1.0.0")); + assert_false(IPRangeIsValid("10.0.0.0/33")); + assert_false(IPRangeIsValid("10.0.0.0/")); + assert_false(IPRangeIsValid("10.0.0/8")); + assert_false(IPRangeIsValid("1.2.3.4/8-9")); + assert_false(IPRangeIsValid("1.2.3.4:5::")); + assert_false(IPRangeIsValid("2001:db8::/129")); + assert_false(IPRangeIsValid("2001:db8:1-5")); + assert_false(IPRangeIsValid("2001::1::1-2")); + assert_false(IPRangeIsValid("2001:db8:5-1::1")); + assert_false(IPRangeIsValid("2001:db8:g::1")); /* A range with fewer octets used to read past the end of the string. */ char *s = xstrdup("1-2.3"); - FuzzyMatchParse(s); + IPRangeIsValid(s); free(s); } +static void test_HostRangeMatch() +{ + assert_true(HostRangeMatch("host", "1-10", "host1")); + assert_true(HostRangeMatch("host", "1-10", "host10")); + assert_true(HostRangeMatch("host", "1-10", "HOST5")); + assert_true(HostRangeMatch("Host", "1-10", "host5")); + assert_false(HostRangeMatch("host", "1-10", "host0")); + assert_false(HostRangeMatch("host", "1-10", "host11")); + assert_false(HostRangeMatch("host", "1-10", "hostx5")); + assert_false(HostRangeMatch("host", "1-10", "myhost5")); + assert_false(HostRangeMatch("host", "1-10", "host")); + assert_false(HostRangeMatch("host", "1-10", "host99999999999999999999999")); + + /* Host names that are empty or all digits used to be scanned backwards + * past the start of the buffer. */ + char *s = xstrdup(""); + assert_false(HostRangeMatch("host", "1-10", s)); + free(s); + + s = xstrdup("123"); + assert_false(HostRangeMatch("", "1-200", s)); + free(s); +} int main() { @@ -186,8 +357,12 @@ int main() const UnitTest tests[] = { unit_test(test_ParseHostPort), - unit_test(test_FuzzySetMatch), - unit_test(test_FuzzyMatchParse) + unit_test(test_ParseHostPort_address_type), + unit_test(test_IPRangeMatch), + unit_test(test_IPRangeMatch_ipv4), + unit_test(test_IPRangeMatch_ipv6), + unit_test(test_IPRangeIsValid), + unit_test(test_HostRangeMatch), }; return run_tests(tests);