diff --git a/src/lib/util/pair.c b/src/lib/util/pair.c index 00ff98103669..7d09d6c3bf50 100644 --- a/src/lib/util/pair.c +++ b/src/lib/util/pair.c @@ -651,36 +651,40 @@ int fr_pair_steal_prepend(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t return 0; } -/** Mark malformed or unrecognised attributed as unknown +/** Mark malformed attribute as raw + * + * @param[in] vp to mark as raw. + * @param[in] data to parse. + * @param[in] data_len of data to parse. * - * @param vp to change fr_dict_attr_t of. * @return - * - 0 on success (or if already unknown). + * - 0 on success * - -1 on failure. */ -int fr_pair_to_unknown(fr_pair_t *vp) +int fr_pair_raw_from_pair(fr_pair_t *vp, uint8_t const *data, size_t data_len) { fr_dict_attr_t *unknown; PAIR_VERIFY(vp); - if (vp->da->flags.is_unknown) return 0; + if (!fr_cond_assert(vp->da->flags.is_unknown == false)) return -1; if (!fr_cond_assert(vp->da->parent != NULL)) return -1; unknown = fr_dict_unknown_afrom_da(vp, vp->da); if (!unknown) return -1; - unknown->flags.is_raw = 1; - fr_dict_unknown_free(&vp->da); /* Only frees unknown attributes */ vp->da = unknown; fr_assert(vp->da->type == FR_TYPE_OCTETS); fr_value_box_init(&vp->data, FR_TYPE_OCTETS, NULL, true); + fr_pair_value_memdup(vp, data, data_len, true); + return 0; } + /** Iterate over pairs with a specified da * * @param[in] list to iterate over. @@ -2414,7 +2418,7 @@ int fr_pair_value_copy(fr_pair_t *dst, fr_pair_t *src) { if (!fr_cond_assert(src->data.type != FR_TYPE_NULL)) return -1; - if (dst->data.type != FR_TYPE_NULL) fr_value_box_clear(&dst->data); + if (dst->data.type != FR_TYPE_NULL) fr_value_box_clear_value(&dst->data); fr_value_box_copy(dst, &dst->data, &src->data); /* @@ -2840,7 +2844,7 @@ int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tai if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1; - fr_value_box_clear(&vp->data); /* Free any existing buffers */ + fr_value_box_clear_value(&vp->data); /* Free any existing buffers */ ret = fr_value_box_memdup(vp, &vp->data, vp->da, src, len, tainted); if (ret == 0) PAIR_VERIFY(vp); diff --git a/src/lib/util/pair.h b/src/lib/util/pair.h index 2a9a59b3bb2c..52efb9e15b1c 100644 --- a/src/lib/util/pair.h +++ b/src/lib/util/pair.h @@ -453,7 +453,7 @@ int fr_pair_steal_append(TALLOC_CTX *nctx, fr_pair_list_t *list, fr_pair_t *vp) int fr_pair_steal_prepend(TALLOC_CTX *nctx, fr_pair_list_t *list, fr_pair_t *vp) CC_HINT(nonnull); /* Searching and list modification */ -int fr_pair_to_unknown(fr_pair_t *vp) CC_HINT(nonnull); +int fr_pair_raw_from_pair(fr_pair_t *vp, uint8_t const *data, size_t data_len) CC_HINT(nonnull); bool fr_pair_matches_da(void const *item, void const *uctx) CC_HINT(nonnull); diff --git a/src/lib/util/pair_tests.c b/src/lib/util/pair_tests.c index 559855d84472..20e652b96ae7 100644 --- a/src/lib/util/pair_tests.c +++ b/src/lib/util/pair_tests.c @@ -198,9 +198,10 @@ static void test_fr_pair_steal(void) TEST_CHECK(talloc_parent(vp) == autofree); } -static void test_fr_pair_to_unknown(void) +static void test_fr_pair_raw_from_pair(void) { fr_pair_t *vp; + uint8_t value = 0; TEST_CASE("Allocate a new attribute fr_pair_afrom_da"); TEST_CHECK((vp = fr_pair_afrom_da(autofree, fr_dict_attr_test_octets)) != NULL); @@ -209,10 +210,10 @@ static void test_fr_pair_to_unknown(void) PAIR_VERIFY(vp); TEST_CASE("Converting regular 'vp' as unkown"); - TEST_CHECK(fr_pair_to_unknown(vp) == 0); + TEST_CHECK(fr_pair_raw_from_pair(vp, &value, 1) == 0); - TEST_CASE("Checking if a real 'unkown' vp"); - TEST_CHECK(vp && vp->da->flags.is_unknown == true); + TEST_CASE("Checking if a real 'raw' vp"); + TEST_CHECK(vp && vp->vp_raw); } static void test_fr_pair_dcursor_by_da_init(void) @@ -1393,7 +1394,7 @@ TEST_LIST = { { "fr_dcursor_iter_by_da_init", test_fr_pair_dcursor_by_da_init }, { "fr_pair_dcursor_by_ancestor_init", test_fr_pair_dcursor_by_ancestor_init }, { "fr_pair_dcursor_value_init", test_fr_pair_dcursor_value_init }, - { "fr_pair_to_unknown", test_fr_pair_to_unknown }, + { "fr_pair_raw_from_pair", test_fr_pair_raw_from_pair }, { "fr_pair_find_by_da_idx", test_fr_pair_find_by_da_idx }, { "fr_pair_find_by_child_num_idx", test_fr_pair_find_by_child_num_idx }, { "fr_pair_find_by_da_nested", test_fr_pair_find_by_da_nested }, diff --git a/src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c b/src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c index 0cae0f6ea5ed..45eec88c9a5d 100644 --- a/src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c +++ b/src/modules/rlm_eap/types/rlm_eap_fast/eap_fast.c @@ -461,8 +461,7 @@ ssize_t eap_fast_decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_ ret = fr_value_box_from_network(vp, &vp->data, vp->vp_type, vp->da, &FR_DBUFF_TMP(p, (size_t)len), len, true); if (ret != len) { - fr_pair_to_unknown(vp); - fr_pair_value_memdup(vp, p, len, true); + fr_pair_raw_from_pair(vp, p, len); } fr_pair_append(out, vp); p += len; diff --git a/src/modules/rlm_eap/types/rlm_eap_ttls/ttls.c b/src/modules/rlm_eap/types/rlm_eap_ttls/ttls.c index ebdfc80f17c8..7e7caade8ef4 100644 --- a/src/modules/rlm_eap/types/rlm_eap_ttls/ttls.c +++ b/src/modules/rlm_eap/types/rlm_eap_ttls/ttls.c @@ -271,8 +271,7 @@ static ssize_t eap_ttls_decode_pair(request_t *request, TALLOC_CTX *ctx, fr_dcur goto error; } - fr_pair_to_unknown(vp); - fr_pair_value_memdup(vp, p, value_len, true); + fr_pair_raw_from_pair(vp, p, value_len); } /* diff --git a/src/protocols/dhcpv4/decode.c b/src/protocols/dhcpv4/decode.c index 9e9286c72e43..33f3f6f5f1ae 100644 --- a/src/protocols/dhcpv4/decode.c +++ b/src/protocols/dhcpv4/decode.c @@ -266,12 +266,13 @@ static ssize_t decode_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t default: slen = fr_value_box_from_network(vp, &vp->data, vp->vp_type, da, - &FR_DBUFF_TMP(p, end - p), end - p, true); + &FR_DBUFF_TMP(p, end - p), end - p, true); if (slen < 0) { raw: FR_PROTO_TRACE("decoding as unknown type"); - if (fr_pair_to_unknown(vp) < 0) return -1; - fr_pair_value_memdup(vp, p, end - p, true); + if (fr_pair_raw_from_pair(vp, p, (end - p)) < 0) { + return -1; + } p = end; break; }