uint8_t *session_id;
VALUE_PAIR *vp;
- if (eap_crypto_tls_session_id(request->parent->reply, request, tls_session->ssl,
- &session_id, eap_session->type,
- sessid_prf_label, sessid_prf_label_len) < 0) return -1;
-
MEM(pair_add_reply(&vp, attr_eap_session_id) >= 0);
- fr_pair_value_memsteal(vp, session_id, true);
+ if (eap_crypto_tls_session_id(vp, request, tls_session->ssl,
+ &session_id, eap_session->type,
+ sessid_prf_label, sessid_prf_label_len) < 0) {
+ pair_delete_reply(vp);
+ return -1;
+ }
+ fr_pair_value_memdup_buffer_shallow(vp, session_id, false);
RINDENT();
RDEBUG2("&reply:%pP", vp);
*/
#define pair_update_session_state(_attr, _da) fr_pair_update_by_da(request->state_ctx, _attr, &request->state, _da)
+/** Delete a VALUE_PAIR in a list
+ *
+ * @param[in] _list to delete the pair from.
+ * @param[in] _pair May be a VALUE_PAIR or fr_dict_attr_t.
+ */
+#define pair_delete(_list, _pair) \
+ _Generic((_pair), \
+ fr_dict_attr_t const * : fr_pair_delete_by_da(_list, (fr_dict_attr_t const *)_pair), \
+ fr_dict_attr_t * : fr_pair_delete_by_da(_list, (fr_dict_attr_t const *)_pair), \
+ VALUE_PAIR * : fr_pair_delete(_list, (VALUE_PAIR const *)_pair) \
+ )
-/** Return or allocate a VALUE_PAIR in the request list
+/** Delete a VALUE_PAIR in the request list
*
- * @param[in] _da #fr_dict_attr_t of the pair(s) to be deleted.
+ * @param[in] _pair #fr_dict_attr_t of the pair(s) to be deleted.
* @return
* - >0 the number of pairs deleted.
* - 0 if no pairs were deleted.
*/
-#define pair_delete_request(_da) fr_pair_delete_by_da(&request->packet->vps, _da)
+#define pair_delete_request(_pair) pair_delete(&request->packet->vps, _pair)
+
/** Delete a VALUE_PAIR in the reply list
*
- * @param[in] _da #fr_dict_attr_t of the pair(s) to be deleted.
+ * @param[in] _pair #fr_dict_attr_t of the pair(s) to be deleted.
* @return
* - >0 the number of pairs deleted.
* - 0 if no pairs were deleted.
*/
-#define pair_delete_reply(_da) fr_pair_delete_by_da(&request->reply->vps, _da)
+#define pair_delete_reply(_pair) pair_delete(&request->reply->vps, _pair)
/** Delete a VALUE_PAIR in the control list
*
- * @param[in] _da #fr_dict_attr_t of the pair(s) to be deleted.
+ * @param[in] _pair #fr_dict_attr_t of the pair(s) to be deleted.
* @return
* - >0 the number of pairs deleted.
* - 0 if no pairs were deleted.
*/
-#define pair_delete_control(_da) fr_pair_delete_by_da(&request->control, _da)
+#define pair_delete_control(_pair) pair_delete(&request->control, _pair)
/** Delete a VALUE_PAIR in the session_state list
*
- * @param[in] _da #fr_dict_attr_t of the pair(s) to be deleted.
+ * @param[in] _pair #fr_dict_attr_t of the pair(s) to be deleted.
* @return
* - >0 the number of pairs deleted.
* - 0 if no pairs were deleted.
*/
-#define pair_delete_session_state(_da) fr_pair_delete_by_da(&request->state, _da)
+#define pair_delete_session_state(_pair) pair_delete(&request->state, _pair)
{
VALUE_PAIR *vp;
size_t len;
- uint8_t *buff, *p;
+ uint8_t *p;
if (!resp) {
REDEBUG("No OCSP response available");
return -1;
}
- MEM(p = buff = talloc_array(request, uint8_t, len)); /* Alloc in context of request - steal later */
+ MEM(pair_update_request(&vp, attr_tls_ocsp_response) >= 0);
+ MEM(fr_pair_value_mem_alloc(vp, &p, len, true));
len = i2d_OCSP_RESPONSE(resp, &p);
if (len <= 0) {
REDEBUG("Failed serialising OCSP response");
+ pair_delete_request(vp);
return -1;
}
- MEM(pair_update_request(&vp, attr_tls_ocsp_response) >= 0);
- fr_pair_value_memsteal(vp, buff, true);
RDEBUG2("Serializing OCSP response");
RINDENT();
* then we just want to copy the new value in unmolested.
*/
if ((vp->op == T_OP_REG_EQ) || (vp->op == T_OP_REG_NE)) {
- fr_pair_value_bstrsteal(vp, expanded);
+ fr_pair_value_bstrdup_buffer(vp, expanded, vp->vp_tainted);
+ talloc_free(expanded);
return 0;
}
return cnt;
}
+/** Remove VALUE_PAIR from a list
+ *
+ * @param[in] list of value pairs to remove VP from.
+ * @param[in] vp to remove
+ */
+void fr_pair_delete(VALUE_PAIR **list, VALUE_PAIR const *vp)
+{
+ fr_cursor_t cursor;
+ VALUE_PAIR *cvp;
+
+ for (cvp = fr_cursor_init(&cursor, list);
+ cvp;
+ cvp = fr_cursor_next(&cursor)) {
+ if (cvp == vp) {
+ fr_cursor_free_item(&cursor);
+ return;
+ }
+ }
+}
+
/** Order attributes by their da, and tag
*
* Useful where attributes need to be aggregated, but not necessarily
return ret;
}
-/** Append a talloced buffer to an existing "string" type value pair
- *
- * @param[in,out] vp to update.
- * @param[in] src data to copy.
- * @param[in] tainted Whether the value came from a trusted source.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_pair_value_bstr_append_buffer(VALUE_PAIR *vp, char const *src, bool tainted)
-{
- int ret;
-
- if (!fr_cond_assert(vp->da->type == FR_TYPE_STRING)) return -1;
-
- ret = fr_value_box_bstr_append_buffer(vp, &vp->data, src, tainted);
- if (ret == 0) {
- vp->type = VT_DATA;
- VP_VERIFY(vp);
- }
-
- return ret;
-}
-
-/** Reparent an allocated char buffer to a VALUE_PAIR
- *
- * @param[in,out] vp to update
- * @param[in] src buffer to steal.
- */
-int fr_pair_value_bstrsteal(VALUE_PAIR *vp, char *src)
-{
- if (!fr_cond_assert(vp->da->type == FR_TYPE_STRING)) return -1;
-
- fr_value_box_clear(&vp->data); /* Free any existing buffers */
- fr_value_box_bstrsteal(vp, &vp->data, vp->da, src, false);
- vp->type = VT_DATA;
-
- VP_VERIFY(vp);
-
- return 0;
-}
-
/** Pre-allocate a memory buffer for a "octets" type value pair
*
* @note Will clear existing values (including buffers).
return ret;
}
-/** Reparent an allocated octet buffer to a VALUE_PAIR
- *
- * @note Will clear existing values (including buffers).
- *
- * @param[in,out] vp to update
- * @param[in] src buffer to steal.
- * @param[in] tainted Whether the value came from a trusted source.
- */
-int fr_pair_value_memsteal(VALUE_PAIR *vp, uint8_t const *src, bool tainted)
-{
- if (!fr_cond_assert(vp->da->type == FR_TYPE_OCTETS)) return -1;
-
- fr_value_box_clear(&vp->data); /* Free any existing buffers */
- fr_value_box_memsteal(vp, &vp->data, vp->da, src, tainted);
- vp->type = VT_DATA;
-
- VP_VERIFY(vp);
-
- return 0;
-}
-
/** Print the value of an attribute to a string
*
* @param[out] out Where to write the string.
int fr_pair_delete_by_da(VALUE_PAIR **head, fr_dict_attr_t const *da);
+void fr_pair_delete(VALUE_PAIR **list, VALUE_PAIR const *vp);
+
/* functions for FR_TYPE_GROUP */
fr_pair_list_t *fr_pair_group_get_sublist(VALUE_PAIR *head);
int fr_pair_value_bstrn_append(VALUE_PAIR *vp, char const *src, size_t len, bool tainted);
int fr_pair_value_bstr_append_buffer(VALUE_PAIR *vp, char const *src, bool tainted);
-
-int fr_pair_value_bstrsteal(VALUE_PAIR *vp, char *src);
/** @} */
/** @name Assign and manipulate octets strings
int fr_pair_value_mem_append(VALUE_PAIR *vp, uint8_t *src, size_t len, bool tainted);
int fr_pair_value_mem_append_buffer(VALUE_PAIR *vp, uint8_t *src, bool tainted);
-
-int fr_pair_value_memsteal(VALUE_PAIR *vp, uint8_t const *src, bool tainted);
/** @} */
/* Printing functions */
return fr_value_box_bstrn_append(ctx, dst, src, len - 1, tainted);
}
-/** Steal a nul terminated talloced buffer into a specified ctx, and assign to a #fr_value_box_t
- *
- * Steal a talloced nul terminated buffer, setting fields in the dst value box appropriately.
- *
- * The buffer must be \0 terminated, or an error will be returned.
- *
- * @param[in] ctx to allocate any new buffers in.
- * @param[in] dst to assign new buffer to.
- * @param[in] enumv Aliases for values.
- * @param[in] src a talloced nul terminated buffer.
- * @param[in] tainted Whether the value came from a trusted source.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_value_box_bstrsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
- char *src, bool tainted)
-{
- size_t len;
- char *str;
-
- len = talloc_array_length(src);
- if ((len == 0) || (src[len - 1] != '\0')) {
- fr_strerror_printf("Input buffer empty or not \\0 terminated");
- return -1;
- }
-
- str = talloc_steal(ctx, src);
- if (!str) {
- fr_strerror_printf("Failed stealing string buffer");
- return -1;
- }
- talloc_set_type(str, char);
-
- fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted);
- dst->vb_strvalue = str;
- dst->vb_length = len - 1;
-
- return 0;
-}
-
/** Pre-allocate an octets buffer for filling by the caller
*
* @note Buffer will not be zeroed, as it's assumed the caller will be filling it.
return fr_value_box_mem_append(ctx, dst, src, talloc_array_length(src), tainted);
}
-/** Steal a talloced buffer into a specified ctx, and assign to a #fr_value_box_t
- *
- * Steal a talloced buffer, setting fields in the dst value box appropriately.
- *
- * @param[in] ctx to allocate any new buffers in.
- * @param[in] dst to assign new buffer to.
- * @param[in] enumv Aliases for values.
- * @param[in] src a talloced nul terminated buffer.
- * @param[in] tainted Whether the value came from a trusted source.
- */
-void fr_value_box_memsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
- uint8_t const *src, bool tainted)
-{
- (void) talloc_get_type_abort_const(src, uint8_t);
-
- src = talloc_steal(ctx, src); /* steal can never fail according to talloc docs */
- talloc_set_type(src, uint8_t);
-
- fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted);
- dst->vb_octets = src;
- dst->datum.length = talloc_array_length(src);
-}
-
/** Increment a boxed value
*
* Implements safe integer overflow.
int fr_value_box_bstrn_append(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted);
int fr_value_box_bstr_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, bool tainted);
-
-int fr_value_box_bstrsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
- char *src, bool tainted);
/** @} */
/** @name Assign and manipulate octets strings
uint8_t const *src, size_t len, bool tainted);
int fr_value_box_mem_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, uint8_t const *src, bool tainted);
-
-
-void fr_value_box_memsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
- uint8_t const *src, bool tainted);
/** @} */
void fr_value_box_increment(fr_value_box_t *vb);
return 0;
}
- len = xlat_aeval(request, &expanded, request, sqluser, NULL, NULL);
+ MEM(pair_update_request(&vp, inst->sql_user) >= 0);
+ len = xlat_aeval(vp, &expanded, request, sqluser, NULL, NULL);
if (len < 0) {
+ pair_delete_request(vp);
return -1;
}
/*
* Replace any existing SQL-User-Name with outs
*/
- MEM(pair_update_request(&vp, inst->sql_user) >= 0);
- fr_pair_value_bstrsteal(vp, expanded);
+ fr_pair_value_bstrdup_buffer_shallow(vp, expanded, true);
+ MEM(fr_pair_value_bstr_realloc(vp, NULL, len) == 0);
RDEBUG2("SQL-User-Name set to '%pV'", &vp->data);
return 0;
if (!str || !*str) return rcode;
- if (xlat_aeval(request, &expanded, request, str, NULL, NULL) < 0) return rcode;
-
MEM(pair_add_request(&vp, attr_module_success_message) == 0);
- fr_pair_value_bstrsteal(vp, expanded);
+ if (xlat_aeval(request, &expanded, request, str, NULL, NULL) < 0) {
+ pair_delete_request(vp);
+ return rcode;
+ }
+ fr_pair_value_bstrdup_buffer_shallow(vp, expanded, true);
return rcode;
}