The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 2e5866c1ca2b983eb20ba3b41795da078a1e9679 $
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_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 
63 done:
64  curl_free(escaped);
65 
66  fr_value_box_list_remove(in, to_escape);
67  fr_dcursor_insert(out, to_escape);
68 
69  return XLAT_ACTION_DONE;
70 }
71 
72 /** xlat function to unescape URI encoded strings
73  *
74  */
76  UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request,
77  fr_value_box_list_t *in)
78 {
79  fr_value_box_t *to_unescape = fr_value_box_list_head(in);
80  int unescaped_len;
81  char *unescaped;
82 
83  fr_value_box_list_remove(in, to_unescape);
84 
85  unescaped = curl_easy_unescape(fr_curl_tmp_handle(), to_unescape->vb_strvalue, to_unescape->vb_length, &unescaped_len);
86  if (!unescaped) {
87  talloc_free(to_unescape);
88  return XLAT_ACTION_FAIL;
89  }
90 
91  /*
92  * Returned string the same length - nothing changed
93  */
94  if ((size_t)unescaped_len == to_unescape->vb_length) {
95  curl_free(unescaped);
96  fr_dcursor_insert(out, to_unescape);
97  return XLAT_ACTION_DONE;
98  }
99 
100  fr_value_box_clear_value(to_unescape);
101  fr_value_box_bstrndup(to_unescape, to_unescape, NULL, unescaped, unescaped_len, to_unescape->tainted);
102  curl_free(unescaped);
103  fr_dcursor_insert(out, to_unescape);
104 
105  return XLAT_ACTION_DONE;
106 }
#define UNUSED
Definition: build.h:313
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:75
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:821
CURL * fr_curl_tmp_handle(void)
Return a thread local curl easy handle.
Definition: base.c:280
talloc_free(reap)
@ FR_TYPE_STRING
String of printable characters.
Definition: merged_model.c:83
static bool done
Definition: radclient.c:80
bool required
Argument must be present, and non-empty.
Definition: xlat.h:146
#define XLAT_ARG_PARSER_TERMINATOR
Definition: xlat.h:166
xlat_action_t
Definition: xlat.h:35
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition: xlat.h:42
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition: xlat.h:41
Definition for a single argument consumend 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: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