From: Arran Cudbard-Bell Date: Thu, 20 Oct 2022 02:40:34 +0000 (-0400) Subject: Add ability to set static headers to rlm_rest X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c12f8ecd257d8692f3efc15a32d9524314c9aca;p=thirdparty%2Ffreeradius-server.git Add ability to set static headers to rlm_rest --- diff --git a/raddb/mods-available/rest b/raddb/mods-available/rest index caa08b22bf6..de426d2adbb 100644 --- a/raddb/mods-available/rest +++ b/raddb/mods-available/rest @@ -201,6 +201,7 @@ rest { # variables set like http_proxy. # | `method` | HTTP method to use, one of 'get', 'post', 'put', 'patch', # 'delete' or any custom HTTP method. + # | `header` | A custom header in the format '
: '. # | `body` | The format of the HTTP body sent to the remote server. # May be 'none', 'post' or 'json', defaults to 'none'. # | `data` | Send custom freeform data in the HTTP body. `Content-type` diff --git a/src/lib/util/talloc.h b/src/lib/util/talloc.h index 0d1a85254b6..275a7999129 100644 --- a/src/lib/util/talloc.h +++ b/src/lib/util/talloc.h @@ -74,7 +74,7 @@ talloc_foreach(vpt_m, vpt) { */ #define talloc_foreach(_array, _iter) \ for (__typeof__(_array[0]) _iter, *_p = (void *)(_array), *_end = (void *)((_array) + talloc_array_length(_array)); \ - (_p < _end) && (_iter = *((void **)(_p))); \ + (_p < _end) && (_iter = *((void **)(uintptr_t)(_p))); \ _p = (__typeof__(_p))((__typeof__(_array))_p) + 1) typedef int(* fr_talloc_free_func_t)(void *fire_ctx, void *uctx); diff --git a/src/modules/rlm_rest/rest.c b/src/modules/rlm_rest/rest.c index 28098dded63..4851d132e80 100644 --- a/src/modules/rlm_rest/rest.c +++ b/src/modules/rlm_rest/rest.c @@ -1734,9 +1734,6 @@ int rest_request_config(module_ctx_t const *mctx, rlm_rest_section_t const *sect char const *option = "unknown"; char const *content_type; - fr_pair_t *header; - fr_dcursor_t headers; - char buffer[512]; fr_assert(candle); @@ -1805,22 +1802,43 @@ int rest_request_config(module_ctx_t const *mctx, rlm_rest_section_t const *sect ctx->headers = curl_slist_append(ctx->headers, buffer); if (!ctx->headers) goto error_header; - for (header = fr_pair_dcursor_by_da_init(&headers, &request->control_pairs, attr_rest_http_header); - header; - header = fr_dcursor_next(&headers)) { - header = fr_dcursor_remove(&headers); - if (!strchr(header->vp_strvalue, ':')) { - RWDEBUG("Invalid HTTP header \"%s\" must be in format ': '. Skipping...", - header->vp_strvalue); - talloc_free(header); - continue; + /* + * Add in the section headers + */ + if (section->headers) { + talloc_foreach(section->headers, header) { + RINDENT(); + RDEBUG3("%pV", fr_box_strvalue_buffer(header)); + REXDENT(); + + ctx->headers = curl_slist_append(ctx->headers, header); } - RINDENT(); - RDEBUG3("%pV", &header->data); - REXDENT(); + } - ctx->headers = curl_slist_append(ctx->headers, header->vp_strvalue); - talloc_free(header); + /* + * Add in dynamic headers from the request + */ + { + fr_pair_t *header; + fr_dcursor_t headers; + + for (header = fr_pair_dcursor_by_da_init(&headers, &request->control_pairs, attr_rest_http_header); + header; + header = fr_dcursor_next(&headers)) { + header = fr_dcursor_remove(&headers); + if (!strchr(header->vp_strvalue, ':')) { + RWDEBUG("Invalid HTTP header \"%s\" must be in format ': '. Skipping...", + header->vp_strvalue); + talloc_free(header); + continue; + } + RINDENT(); + RDEBUG3("%pV", &header->data); + REXDENT(); + + ctx->headers = curl_slist_append(ctx->headers, header->vp_strvalue); + talloc_free(header); + } } /* diff --git a/src/modules/rlm_rest/rest.h b/src/modules/rlm_rest/rest.h index a196a1b9d8b..1cf5a76fed0 100644 --- a/src/modules/rlm_rest/rest.h +++ b/src/modules/rlm_rest/rest.h @@ -125,6 +125,7 @@ typedef struct { http_body_type_t force_to; //!< Override the Content-Type header in the response //!< to force decoding as a particular type. + char const **headers; //!< Custom headers to set (optional). char const *data; //!< Custom body data (optional). bool auth_is_set; //!< Whether a value was provided for auth_str. diff --git a/src/modules/rlm_rest/rlm_rest.c b/src/modules/rlm_rest/rlm_rest.c index 9d45cfbae83..978cf92a75c 100644 --- a/src/modules/rlm_rest/rlm_rest.c +++ b/src/modules/rlm_rest/rlm_rest.c @@ -90,6 +90,7 @@ static const CONF_PARSER section_config[] = { { FR_CONF_OFFSET("uri", FR_TYPE_STRING | FR_TYPE_XLAT, rlm_rest_section_t, uri), .dflt = "" }, { FR_CONF_OFFSET("proxy", FR_TYPE_STRING, rlm_rest_section_t, proxy), .func = rest_proxy_parse }, { FR_CONF_OFFSET("method", FR_TYPE_STRING, rlm_rest_section_t, method_str), .dflt = "GET" }, + { FR_CONF_OFFSET("header", FR_TYPE_STRING | FR_TYPE_MULTI, rlm_rest_section_t, headers) }, { FR_CONF_OFFSET("body", FR_TYPE_STRING, rlm_rest_section_t, body_str), .dflt = "none" }, { FR_CONF_OFFSET("data", FR_TYPE_STRING | FR_TYPE_XLAT, rlm_rest_section_t, data) }, { FR_CONF_OFFSET("force_to", FR_TYPE_STRING, rlm_rest_section_t, force_to_str) }, @@ -117,6 +118,9 @@ static const CONF_PARSER xlat_config[] = { /* User authentication */ { FR_CONF_OFFSET_IS_SET("auth", FR_TYPE_VOID, rlm_rest_section_t, auth), .func = cf_table_parse_int, .uctx = &(cf_table_parse_ctx_t){ .table = http_auth_table, .len = &http_auth_table_len }, .dflt = "none" }, + + { FR_CONF_OFFSET("header", FR_TYPE_STRING | FR_TYPE_MULTI, rlm_rest_section_t, headers) }, + { FR_CONF_OFFSET("username", FR_TYPE_STRING | FR_TYPE_XLAT, rlm_rest_section_t, username) }, { FR_CONF_OFFSET("password", FR_TYPE_STRING | FR_TYPE_XLAT, rlm_rest_section_t, password) }, { FR_CONF_OFFSET("require_auth", FR_TYPE_BOOL, rlm_rest_section_t, require_auth), .dflt = "no" },