26 RCSID(
"$Id: 1fea13a1577208380035032ea8f74338ab376ca3 $")
28 #include <freeradius-devel/server/regex.h>
29 #include <freeradius-devel/server/request_data.h>
30 #include <freeradius-devel/util/debug.h>
34 #define REQUEST_DATA_REGEX (0xadbeef00)
37 #if defined(HAVE_REGEX_PCRE) || defined(HAVE_REGEX_PCRE2)
40 fr_regmatch_t *regmatch;
57 void regex_sub_to_request(
request_t *request, regex_t **preg, fr_regmatch_t **regmatch)
59 fr_regcapture_t *old_rc, *new_rc;
66 DEBUG4(
"Clearing %zu matches", old_rc->regmatch->used);
72 if (!regmatch || ((*regmatch)->used == 0))
return;
77 DEBUG4(
"Adding %zu matches", (*regmatch)->used);
82 MEM(new_rc = talloc(request, fr_regcapture_t));
87 #if defined(HAVE_REGEX_PCRE) || defined(HAVE_REGEX_PCRE2)
88 if (!(*preg)->precompiled) {
89 new_rc->preg = talloc_steal(new_rc, *preg);
99 new_rc->regmatch = talloc_steal(new_rc, *regmatch);
105 # if defined(HAVE_REGEX_PCRE2)
124 pcre2_match_data *match_data;
128 RDEBUG4(
"No subcapture data found");
132 match_data = talloc_get_type_abort(rc->regmatch->match_data, pcre2_match_data);
134 ret = pcre2_substring_length_bynumber(match_data, num, &len);
136 case PCRE2_ERROR_NOMEMORY:
143 case PCRE2_ERROR_NOSUBSTRING:
144 RDEBUG4(
"%i/%zu Not found", num + 1, rc->regmatch->used);
154 MEM(
buff = talloc_array(ctx,
char, ++len));
155 pcre2_substring_copy_bynumber(match_data, num, (PCRE2_UCHAR *)
buff, &len);
157 RDEBUG4(
"%i/%zu Found: %pV (%zu)", num + 1, rc->regmatch->used,
179 int regex_request_to_sub_named(TALLOC_CTX *ctx,
char **
out,
request_t *request,
char const *
name)
185 pcre2_match_data *match_data;
189 RDEBUG4(
"No subcapture data found");
193 match_data = rc->regmatch->match_data;
195 ret = pcre2_substring_length_byname(match_data, (PCRE2_UCHAR
const *)
name, &len);
197 case PCRE2_ERROR_NOMEMORY:
204 case PCRE2_ERROR_NOSUBSTRING:
215 MEM(
buff = talloc_array(ctx,
char, ++len));
216 pcre2_substring_copy_byname(match_data, (PCRE2_UCHAR
const *)
name, (PCRE2_UCHAR *)
buff, &len);
227 # elif defined(HAVE_REGEX_PCRE)
248 RDEBUG4(
"No subcapture data found");
253 ret = pcre_get_substring(rc->regmatch->subject,
254 (
int *)rc->regmatch->match_data, (
int)rc->regmatch->used, num, &p);
256 case PCRE_ERROR_NOMEMORY:
263 case PCRE_ERROR_NOSUBSTRING:
264 RDEBUG4(
"%i/%zu Not found", num + 1, rc->regmatch->used);
274 talloc_set_type(p,
char);
275 p = talloc_steal(ctx, p);
277 RDEBUG4(
"%i/%zu Found: %pV (%zu)", num + 1, rc->regmatch->used,
280 memcpy(
out, &p,
sizeof(*
out));
299 int regex_request_to_sub_named(TALLOC_CTX *ctx,
char **
out,
request_t *request,
char const *
name)
308 RDEBUG4(
"No subcapture data found");
313 rc = talloc_get_type_abort(rd, fr_regcapture_t);
314 ret = pcre_get_named_substring(rc->preg->compiled, rc->regmatch->subject,
315 (
int *)rc->regmatch->match_data, (
int)rc->regmatch->used,
name, &p);
317 case PCRE_ERROR_NOMEMORY:
324 case PCRE_ERROR_NOSUBSTRING:
339 talloc_set_type(p,
char);
340 talloc_steal(ctx, p);
345 memcpy(
out, &p,
sizeof(*
out));
371 regmatch_t *match_data;
375 RDEBUG4(
"No subcapture data found");
379 match_data = rc->regmatch->match_data;
386 if ((num >= rc->regmatch->used) || (match_data[num].rm_eo == -1) || (match_data[num].rm_so == -1)) {
387 RDEBUG4(
"%i/%zu Not found", num + 1, rc->regmatch->used);
395 fr_assert(match_data[num].rm_eo <= (regoff_t)talloc_array_length(rc->regmatch->subject));
396 fr_assert(match_data[num].rm_so <= (regoff_t)talloc_array_length(rc->regmatch->subject));
398 start = rc->regmatch->subject + match_data[num].rm_so;
399 len = match_data[num].rm_eo - match_data[num].rm_so;
void * request_data_get(request_t *request, void const *unique_ptr, int unique_int)
Get opaque data from a request.
void * request_data_reference(request_t *request, void const *unique_ptr, int unique_int)
Get opaque data from a request without removing it.
#define request_data_talloc_add(_request, _unique_ptr, _unique_int, _type, _opaque, _free_on_replace, _free_on_parent, _persist)
Add opaque data to a request_t.
static char buff[sizeof("18446744073709551615")+3]
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
#define fr_box_strvalue_buffer(_val)
static size_t char ** out