From: Arran Cudbard-Bell Date: Mon, 5 Mar 2018 10:24:31 +0000 (+0600) Subject: Various casting fixes X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df28703c711ea99e6190c98fe65bdbef206eb3bc;p=thirdparty%2Ffreeradius-server.git Various casting fixes --- diff --git a/share/dictionary.freeradius.internal b/share/dictionary.freeradius.internal index b29a0684b72..6852b1066c8 100644 --- a/share/dictionary.freeradius.internal +++ b/share/dictionary.freeradius.internal @@ -620,6 +620,12 @@ ATTRIBUTE Tmp-Signed-7 1867 signed ATTRIBUTE Tmp-Signed-8 1868 signed ATTRIBUTE Tmp-Signed-9 1869 signed +ATTRIBUTE Tmp-Ethernet-0 1870 ether +ATTRIBUTE Tmp-Ethernet-1 1871 ether +ATTRIBUTE Tmp-Ethernet-2 1872 ether +ATTRIBUTE Tmp-Ethernet-3 1873 ether +ATTRIBUTE Tmp-Ethernet-4 1874 ether + # 1870 - 1890, unused # @@ -628,9 +634,9 @@ ATTRIBUTE Tmp-Signed-9 1869 signed ATTRIBUTE Tmp-String-Tagged-0 1891 string has_tag ATTRIBUTE Tmp-String-Tagged-1 1892 string has_tag -ATTRIBUTE Log-Message 1893 string -ATTRIBUTE Log-Level 1894 integer -ATTRIBUTE Log-Type 1895 integer +ATTRIBUTE Log-Message 1894 string +ATTRIBUTE Log-Level 1895 integer +ATTRIBUTE Log-Type 1896 integer # Range: 1900-1909 # WiMAX server-side attributes. diff --git a/src/lib/util/value.c b/src/lib/util/value.c index 0f16db3c41d..5798edd978e 100644 --- a/src/lib/util/value.c +++ b/src/lib/util/value.c @@ -1649,7 +1649,10 @@ static inline int fr_value_box_cast_to_ipv4prefix(TALLOC_CTX *ctx, fr_value_box_ } default: - break; + fr_strerror_printf("Invalid cast from %s to %s. Unsupported", + fr_int2str(dict_attr_types, src->type, ""), + fr_int2str(dict_attr_types, dst_type, "")); + return -1; } dst->vb_ip.af = AF_INET; @@ -1835,7 +1838,10 @@ static inline int fr_value_box_cast_to_ipv6prefix(TALLOC_CTX *ctx, fr_value_box_ break; default: - break; + fr_strerror_printf("Invalid cast from %s to %s. Unsupported", + fr_int2str(dict_attr_types, src->type, ""), + fr_int2str(dict_attr_types, dst_type, "")); + return -1; } dst->vb_ip.af = AF_INET6; @@ -1845,6 +1851,46 @@ static inline int fr_value_box_cast_to_ipv6prefix(TALLOC_CTX *ctx, fr_value_box_ return 0; } +/** Convert any supported type to a bool + * + * Allowed input types are: + * - FR_TYPE_STRING ("00:11:22:33:44:55") + * - FR_TYPE_OCTETS (0x001122334455) + * + * + * @param ctx unused. + * @param dst Where to write result of casting. + * @param dst_type to cast to. + * @param dst_enumv enumeration values. + * @param src Input data. + */ +static inline int fr_value_box_cast_to_ethernet(TALLOC_CTX *ctx, fr_value_box_t *dst, + fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, + fr_value_box_t const *src) +{ + switch (src->type) { + case FR_TYPE_STRING: + if (fr_value_box_from_str(ctx, dst, &dst_type, dst_enumv, + src->vb_strvalue, src->datum.length, '\0', false) < 0) return -1; + break; + + case FR_TYPE_OCTETS: + if (fr_value_box_fixed_size_from_ocets(dst, dst_type, dst_enumv, src) < 0) return -1; + break; + + default: + fr_strerror_printf("Invalid cast from %s to %s. Unsupported", + fr_int2str(dict_attr_types, src->type, ""), + fr_int2str(dict_attr_types, dst_type, "")); + return -1; + } + + dst->type = FR_TYPE_ETHERNET; + dst->enumv = dst_enumv; + + return 0; +} + /** Convert any supported type to a bool * * Allowed input types are: @@ -1866,9 +1912,11 @@ static inline int fr_value_box_cast_to_bool(TALLOC_CTX *ctx, fr_value_box_t *dst src->vb_strvalue, src->datum.length, '\0', false) < 0) return -1; break; - default: - break; + fr_strerror_printf("Invalid cast from %s to %s. Unsupported", + fr_int2str(dict_attr_types, src->type, ""), + fr_int2str(dict_attr_types, dst_type, "")); + return -1; } dst->type = FR_TYPE_BOOL; @@ -1905,9 +1953,15 @@ static inline int fr_value_box_cast_to_uint8(TALLOC_CTX *ctx, fr_value_box_t *ds src->vb_strvalue, src->datum.length, '\0', false) < 0) return -1; break; - default: + case FR_TYPE_OCTETS: if (fr_value_box_fixed_size_from_ocets(dst, dst_type, dst_enumv, src) < 0) return -1; break; + + default: + fr_strerror_printf("Invalid cast from %s to %s. Unsupported", + fr_int2str(dict_attr_types, src->type, ""), + fr_int2str(dict_attr_types, dst_type, "")); + return -1; } dst->type = FR_TYPE_UINT8; @@ -1957,9 +2011,15 @@ static inline int fr_value_box_cast_to_uint16(TALLOC_CTX *ctx, fr_value_box_t *d src->vb_strvalue, src->datum.length, '\0', false) < 0) return -1; break; - default: + case FR_TYPE_OCTETS: if (fr_value_box_fixed_size_from_ocets(dst, dst_type, dst_enumv, src) < 0) return -1; break; + + default: + fr_strerror_printf("Invalid cast from %s to %s. Unsupported", + fr_int2str(dict_attr_types, src->type, ""), + fr_int2str(dict_attr_types, dst_type, "")); + return -1; } dst->type = FR_TYPE_UINT16; @@ -2027,9 +2087,15 @@ static inline int fr_value_box_cast_to_uint32(TALLOC_CTX *ctx, fr_value_box_t *d src->vb_strvalue, src->datum.length, '\0', false) < 0) return -1; break; - default: + case FR_TYPE_OCTETS: if (fr_value_box_fixed_size_from_ocets(dst, dst_type, dst_enumv, src) < 0) return -1; break; + + default: + fr_strerror_printf("Invalid cast from %s to %s. Unsupported", + fr_int2str(dict_attr_types, src->type, ""), + fr_int2str(dict_attr_types, dst_type, "")); + return -1; } dst->type = FR_TYPE_UINT32; @@ -2111,9 +2177,15 @@ static inline int fr_value_box_cast_to_uint64(TALLOC_CTX *ctx, fr_value_box_t *d src->vb_strvalue, src->datum.length, '\0', false) < 0) return -1; break; - default: + case FR_TYPE_OCTETS: if (fr_value_box_fixed_size_from_ocets(dst, dst_type, dst_enumv, src) < 0) return -1; break; + + default: + fr_strerror_printf("Invalid cast from %s to %s. Unsupported", + fr_int2str(dict_attr_types, src->type, ""), + fr_int2str(dict_attr_types, dst_type, "")); + return -1; } dst->type = FR_TYPE_UINT64; @@ -2191,7 +2263,11 @@ int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, case FR_TYPE_IFID: case FR_TYPE_COMBO_IP_ADDR: case FR_TYPE_COMBO_IP_PREFIX: + break; + case FR_TYPE_ETHERNET: + return fr_value_box_cast_to_ethernet(ctx, dst, dst_type, dst_enumv, src); + case FR_TYPE_BOOL: return fr_value_box_cast_to_bool(ctx, dst, dst_type, dst_enumv, src); diff --git a/src/tests/keywords/ethernet b/src/tests/keywords/ethernet new file mode 100644 index 00000000000..6aa5238187f --- /dev/null +++ b/src/tests/keywords/ethernet @@ -0,0 +1,35 @@ +update request { + Tmp-Ethernet-0 := "00:11:22:33:44:54" + Tmp-Ethernet-0 += "00:11:22:33:44:55" + Tmp-Octets-0 := 0x001122334456 +} + +if (&Tmp-Ethernet-0[0] != 00:11:22:33:44:54) { + test_fail +} + +if (&Tmp-Ethernet-0[1] != 00:11:22:33:44:55) { + test_fail +} + +update request { + &Tmp-Ethernet-0 += &Tmp-Octets-0[0] +} + +if (&Tmp-Ethernet-0[2] != 00:11:22:33:44:56) { + test_fail +} + +# NULL assignment +update request { + &Tmp-Ethernet-0 += "%{Calling-Station-ID[42]}" +} + +# Null xlat expansion results in zero length string, which with current behaviour +# results in all zeros ethernet address. +if (&Tmp-Ethernet-0[3] != 00:00:00:00:00:00) { + test_fail +} + + +success