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: 99fcba1fbc2de4196c4eac9f1e08ecacfc144447 $
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 
35  { .required = true, .concat = true, .type = FR_TYPE_STRING },
37 };
38 
40  { .required = true, .concat = true, .type = FR_TYPE_STRING },
42 };
43 
44 /** xlat function to escape URI encoded strings
45  *
46  */
48  UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request,
49  fr_value_box_list_t *in)
50 {
51  fr_value_box_t *to_escape = fr_value_box_list_head(in);
52  char *escaped;
53 
54  escaped = curl_easy_escape(fr_curl_tmp_handle(), to_escape->vb_strvalue, to_escape->vb_length);
55  if (!escaped) return XLAT_ACTION_FAIL;
56 
57  /*
58  * Returned string the same length - nothing changed
59  */
60  if (strlen(escaped) == to_escape->vb_length) goto done;
61 
62  fr_value_box_clear_value(to_escape);
63  fr_value_box_strdup(to_escape, to_escape, NULL, escaped, to_escape->tainted);
64 
65 done:
66  curl_free(escaped);
67 
68  fr_value_box_list_remove(in, to_escape);
69  fr_dcursor_insert(out, to_escape);
70 
71  return XLAT_ACTION_DONE;
72 }
73 
74 /** xlat function to unescape URI encoded strings
75  *
76  */
78  UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request,
79  fr_value_box_list_t *in)
80 {
81  fr_value_box_t *to_unescape = fr_value_box_list_head(in);
82  int unescaped_len;
83  char *unescaped;
84 
85  fr_value_box_list_remove(in, to_unescape);
86 
87  unescaped = curl_easy_unescape(fr_curl_tmp_handle(), to_unescape->vb_strvalue, to_unescape->vb_length, &unescaped_len);
88  if (!unescaped) {
89  talloc_free(to_unescape);
90  return XLAT_ACTION_FAIL;
91  }
92 
93  /*
94  * Returned string the same length - nothing changed
95  */
96  if ((size_t)unescaped_len == to_unescape->vb_length) {
97  curl_free(unescaped);
98  fr_dcursor_insert(out, to_unescape);
99  return XLAT_ACTION_DONE;
100  }
101 
102  fr_value_box_clear_value(to_unescape);
103  fr_value_box_bstrndup(to_unescape, to_unescape, NULL, unescaped, unescaped_len, to_unescape->tainted);
104  curl_free(unescaped);
105  fr_dcursor_insert(out, to_unescape);
106 
107  return XLAT_ACTION_DONE;
108 }
#define UNUSED
Definition: build.h:313
xlat_action_t fr_curl_xlat_uri_escape(UNUSED TALLOC_CTX *ctx, UNUSED 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:47
int fr_curl_xlat_refs
Definition: xlat.c:32
xlat_arg_parser_t const fr_curl_xlat_uri_args[]
Definition: xlat.c:34
xlat_arg_parser_t const fr_curl_xlat_safe_args[]
Definition: xlat.c:39
xlat_action_t fr_curl_xlat_uri_unescape(UNUSED TALLOC_CTX *ctx, UNUSED 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:77
static int fr_dcursor_insert(fr_dcursor_t *cursor, void *v)
Insert directly after the current item.
Definition: dcursor.h:434
static fr_slen_t in
Definition: dict.h:645
CURL * fr_curl_tmp_handle(void)
Return a thread local curl easy handle.
Definition: base.c:222
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:3630
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:3876
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:4097
static size_t char ** out
Definition: value.h:984
An xlat calling ctx.
Definition: xlat_ctx.h:42