The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
xlat.c
Go to the documentation of this file.
1/*
2 * This program is is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 84a4fcc4aa88fbc95e55d37a67e6545916338e31 $
19 * @file curl/xlat.c
20 * @brief Generic xlat functions dependent on libcurl
21 *
22 * @copyright 2024 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 */
24
25#include <freeradius-devel/util/value.h>
26#include <freeradius-devel/unlang/xlat.h>
27#include <freeradius-devel/curl/base.h>
28#include <freeradius-devel/curl/xlat.h>
29
30#include "base.h"
31
33 { .required = true, .concat = true, .type = FR_TYPE_STRING },
35};
36
38 { .required = true, .concat = true, .type = FR_TYPE_STRING },
40};
41
42/** xlat function to escape URI encoded strings
43 *
44 */
46 UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request,
47 fr_value_box_list_t *in)
48{
49 fr_value_box_t *to_escape = fr_value_box_list_pop_head(in);
50 char *escaped;
51
52 escaped = curl_easy_escape(fr_curl_tmp_handle(), to_escape->vb_strvalue, to_escape->vb_length);
53 if (!escaped) return XLAT_ACTION_FAIL;
54
55 /*
56 * Returned string the same length - nothing changed
57 */
58 if (strlen(escaped) == to_escape->vb_length) goto done;
59
60 fr_value_box_clear_value(to_escape);
61 fr_value_box_strdup(to_escape, to_escape, NULL, escaped, to_escape->tainted);
62
63done:
64 curl_free(escaped);
65 fr_dcursor_insert(out, to_escape);
66
67 return XLAT_ACTION_DONE;
68}
69
70/** xlat function to unescape URI encoded strings
71 *
72 */
74 UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request,
75 fr_value_box_list_t *in)
76{
77 fr_value_box_t *to_unescape = fr_value_box_list_pop_head(in);
78 int unescaped_len;
79 char *unescaped;
80
81 unescaped = curl_easy_unescape(fr_curl_tmp_handle(), to_unescape->vb_strvalue, to_unescape->vb_length, &unescaped_len);
82 if (!unescaped) {
83 talloc_free(to_unescape);
84 return XLAT_ACTION_FAIL;
85 }
86
87 /*
88 * Returned string the same length - nothing changed
89 */
90 if ((size_t)unescaped_len == to_unescape->vb_length) {
91 curl_free(unescaped);
92 fr_dcursor_insert(out, to_unescape);
93 return XLAT_ACTION_DONE;
94 }
95
96 fr_value_box_clear_value(to_unescape);
97 fr_value_box_bstrndup(to_unescape, to_unescape, NULL, unescaped, unescaped_len, to_unescape->tainted);
98 curl_free(unescaped);
99 fr_dcursor_insert(out, to_unescape);
100
101 return XLAT_ACTION_DONE;
102}
#define UNUSED
Definition build.h:315
xlat_action_t fr_curl_xlat_uri_escape(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
xlat function to escape URI encoded strings
Definition xlat.c:45
xlat_arg_parser_t const fr_curl_xlat_uri_args[]
Definition xlat.c:32
xlat_action_t fr_curl_xlat_uri_unescape(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
xlat function to unescape URI encoded strings
Definition xlat.c:73
xlat_arg_parser_t const fr_curl_xlat_safe_args[]
Definition xlat.c:37
static int fr_dcursor_insert(fr_dcursor_t *cursor, void *v)
Insert directly after the current item.
Definition dcursor.h:435
static fr_slen_t in
Definition dict.h:824
CURL * fr_curl_tmp_handle(void)
Return a thread local curl easy handle.
Definition base.c:267
talloc_free(reap)
@ FR_TYPE_STRING
String of printable characters.
static bool done
Definition radclient.c:80
bool required
Argument must be present, and non-empty.
Definition xlat.h:148
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:168
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
Definition for a single argument consumend by an xlat function.
Definition xlat.h:147
Master include file to access all functions and structures in the library.
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition value.c:3681
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:3927
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:4148
static size_t char ** out
Definition value.h:997
An xlat calling ctx.
Definition xlat_ctx.h:49