26 RCSID(
"$Id: e58c54a68364483fb532fc50c523008da92f3047 $")
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/rad_assert.h>
33 #define REQUEST_DATA_REGEX (0xadbeef00)
35 typedef struct regcapture {
55 void regex_sub_to_request(
REQUEST *request, regex_t **preg,
char const *value,
size_t len,
56 regmatch_t rxmatch[],
size_t nmatch)
58 regcapture_t *old_sc, *new_sc;
66 DEBUG4(
"Clearing %zu matches", old_sc->nmatch);
72 if (nmatch == 0)
return;
77 DEBUG4(
"Adding %zu matches", nmatch);
82 MEM(new_sc = talloc(request, regcapture_t));
84 MEM(new_sc->rxmatch = talloc_memdup(new_sc, rxmatch,
sizeof(rxmatch[0]) * nmatch));
85 talloc_set_type(new_sc->rxmatch, regmatch_t[]);
87 MEM(p = talloc_array(new_sc,
char, len + 1));
88 memcpy(p, value, len);
91 new_sc->nmatch = nmatch;
94 if (!(*preg)->precompiled) {
95 new_sc->preg = talloc_steal(new_sc, *preg);
100 new_sc->preg = *preg;
103 request_data_add(request, request, REQUEST_DATA_REGEX, new_sc,
true,
false,
false);
119 int regex_request_to_sub(TALLOC_CTX *ctx,
char **out,
REQUEST *request, uint32_t num)
127 RDEBUG4(
"No subcapture data found");
132 ret = pcre_get_substring(cap->value, (
int *)cap->rxmatch, (
int)cap->nmatch, num, &p);
134 case PCRE_ERROR_NOMEMORY:
140 case PCRE_ERROR_NOSUBSTRING:
141 RDEBUG4(
"%i/%zu Not found", num, cap->nmatch);
155 p = (
char *)talloc_get_type_abort(p, uint8_t);
156 talloc_set_type(p,
char *);
157 talloc_steal(ctx, p);
158 memcpy(out, &p,
sizeof(*out));
160 RDEBUG4(
"%i/%zu Found: %s (%zu)", num, cap->nmatch, p, talloc_array_length(p));
178 int regex_request_to_sub_named(TALLOC_CTX *ctx,
char **out,
REQUEST *request,
char const *
name)
186 RDEBUG4(
"No subcapture data found");
191 ret = pcre_get_named_substring(cap->preg->compiled, cap->value,
192 (
int *)cap->rxmatch, (
int)cap->nmatch, name, &p);
194 case PCRE_ERROR_NOMEMORY:
200 case PCRE_ERROR_NOSUBSTRING:
201 RDEBUG4(
"No named capture group \"%s\"", name);
215 p = (
char *)talloc_get_type_abort(p, uint8_t);
216 talloc_set_type(p,
char *);
217 talloc_steal(ctx, p);
218 memcpy(out, &p,
sizeof(*out));
220 RDEBUG4(
"Found \"%s\": %s (%zu)", name, p, talloc_array_length(p));
238 int regex_request_to_sub(TALLOC_CTX *ctx,
char **out,
REQUEST *request, uint32_t num)
247 RDEBUG4(
"No subcapture data found");
257 if ((num >= cap->nmatch) || (cap->rxmatch[num].rm_eo == -1) || (cap->rxmatch[num].rm_so == -1)) {
258 RDEBUG4(
"%i/%zu Not found", num, cap->nmatch);
266 rad_assert(cap->rxmatch[num].rm_eo <= (regoff_t)talloc_array_length(cap->value));
267 rad_assert(cap->rxmatch[num].rm_so <= (regoff_t)talloc_array_length(cap->value));
269 start = cap->value + cap->rxmatch[num].rm_so;
270 len = cap->rxmatch[num].rm_eo - cap->rxmatch[num].rm_so;
272 RDEBUG4(
"%i/%zu Found: %.*s (%zu)", num, cap->nmatch, (
int)len, start, len);
273 MEM(p = talloc_array(ctx,
char, len + 1));
274 memcpy(p, start, len);
void * request_data_reference(REQUEST *request, void *unique_ptr, int unique_int)
Get opaque data from a request without removing it.
void * request_data_get(REQUEST *request, void *unique_ptr, int unique_int)
Get opaque data from a request.
int request_data_add(REQUEST *request, void *unique_ptr, int unique_int, void *opaque, bool free_on_replace, bool free_on_parent, bool persist)
Add opaque data to a REQUEST.