The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
xlat.c
Go to the documentation of this file.
1/*
2 * This program 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: 1f38486f764438020d24e650363c2f71fae70acd $
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) {
54 talloc_free(to_escape);
55 return XLAT_ACTION_FAIL;
56 }
57
58 /*
59 * Returned string the same length - nothing changed
60 */
61 if (strlen(escaped) == to_escape->vb_length) goto done;
62
63 fr_value_box_clear_value(to_escape);
64 MEM(fr_value_box_strdup(to_escape, to_escape, NULL, escaped, to_escape->tainted) >= 0);
65
66done:
67 curl_free(escaped);
68 fr_dcursor_insert(out, to_escape);
69
70 return XLAT_ACTION_DONE;
71}
72
73/** xlat function to unescape URI encoded strings
74 *
75 */
77 UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request,
78 fr_value_box_list_t *in)
79{
80 fr_value_box_t *to_unescape = fr_value_box_list_pop_head(in);
81 int unescaped_len;
82 char *unescaped;
83
84 unescaped = curl_easy_unescape(fr_curl_tmp_handle(), to_unescape->vb_strvalue, to_unescape->vb_length, &unescaped_len);
85 if (!unescaped) {
86 talloc_free(to_unescape);
87 return XLAT_ACTION_FAIL;
88 }
89
90 /*
91 * Returned string the same length - nothing changed
92 */
93 if ((size_t)unescaped_len == to_unescape->vb_length) {
94 curl_free(unescaped);
95 fr_dcursor_insert(out, to_unescape);
96 return XLAT_ACTION_DONE;
97 }
98
99 fr_value_box_clear_value(to_unescape);
100 MEM(fr_value_box_bstrndup(to_unescape, to_unescape, NULL, unescaped, unescaped_len, to_unescape->tainted) >= 0);
101 curl_free(unescaped);
102 fr_dcursor_insert(out, to_unescape);
103
104 return XLAT_ACTION_DONE;
105}
#define UNUSED
Definition build.h:336
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:76
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
#define MEM(x)
Definition debug.h:36
static fr_slen_t in
Definition dict.h:882
talloc_free(hp)
CURL * fr_curl_tmp_handle(void)
Return a thread local curl easy handle.
Definition base.c:276
@ FR_TYPE_STRING
String of printable characters.
static bool done
Definition radclient.c:80
unsigned int required
Argument must be present, and non-empty.
Definition xlat.h:146
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:170
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 consumed by an xlat function.
Definition xlat.h:145
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:4331
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:4619
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:4838
static size_t char ** out
Definition value.h:1030
An xlat calling ctx.
Definition xlat_ctx.h:49