]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
fixes and tests for run-time regular expressions
authorAlan T. DeKok <aland@freeradius.org>
Wed, 1 Jun 2022 14:23:58 +0000 (10:23 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 3 Jun 2022 11:15:57 +0000 (07:15 -0400)
src/lib/unlang/xlat_expr.c
src/tests/xlat/expr.txt

index 79d4df9a7fbfb4539673926ef23cd28053743209..b0adcef4c5468d5e31846c5c95727e9688ee6ca2 100644 (file)
@@ -405,7 +405,7 @@ static int xlat_instantiate_regex(xlat_inst_ctx_t const *xctx)
        /*
         *      The RHS is more then just one regex node, it has to be dynamically expanded.
         */
-       if (xlat_exp_next(rhs->group, regex)) {
+       if (xlat_exp_next(rhs->group, regex) || !tmpl_contains_regex(regex->vpt)) {
                inst->xlat = talloc_steal(inst, rhs);
                return 0;
        }
@@ -525,7 +525,20 @@ static xlat_action_t xlat_regex_resume(TALLOC_CTX *ctx, fr_dcursor_t *out,
         *      LHS should already have been expanded.  RHS was just expanded by us.
         */
        lhs = fr_dlist_head(in);
+
+       /*
+        *      Because we expanded the RHS ourselves, the "concat"
+        *      flag to the RHS argument is ignored.  So we just
+        *      concatenate it here.
+        */
        rhs = fr_dlist_head(&rctx->list);
+       if (fr_value_box_list_concat_in_place(rctx, rhs, &rctx->list, FR_TYPE_STRING,
+                                             FR_VALUE_BOX_LIST_FREE, true, SIZE_MAX) < 0) {
+               RPEDEBUG("Failed concatenating regular expression string");
+               return XLAT_ACTION_FAIL;
+       }
+
+       fr_assert(rhs != NULL);
 
        slen = regex_compile(rctx, &preg, rhs->vb_strvalue, rhs->vb_length,
                             NULL, true, true); /* no flags, allow subcaptures, at runtime */
@@ -549,7 +562,7 @@ static xlat_action_t xlat_regex_op(TALLOC_CTX *ctx, fr_dcursor_t *out,
        /*
         *      Just run precompiled regexes.
         */
-       if (!tmpl_is_regex_xlat(inst->regex->vpt)) {
+       if (inst->regex) {
                preg = tmpl_regex(inst->regex->vpt);
 
                return xlat_regex_match(ctx, request, lhs, &preg, out, op);
index 959330d8cb9478357877c583716e208fe421dc99..2c54e2251b2407d63451bdf281cc3a28c4b4c5d2 100644 (file)
@@ -30,6 +30,21 @@ match 2
 xlat_expr (uint32) 1 << 31
 match 2147483648
 
+xlat_expr %n
+match {0}
+
+xlat_expr &Packet-Src-IP-Address =~ /^127.0/
+match true
+
+xlat_expr &Packet-Src-IP-Address =~ /^128.0/
+match false
+
+xlat_expr &Packet-Src-IP-Address =~ /foo %{Packet-Dst-IP-Address} foo/
+match false
+
+xlat_expr &Packet-Src-IP-Address =~ /%{Packet-Dst-IP-Address}/
+match true
+
 # @todo - failure?
 xlat_expr (uint32) 1 << 32
 match NULL