]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
print &Foo instead of %{Foo}, which is less confusing
authorAlan T. DeKok <aland@freeradius.org>
Thu, 26 May 2022 13:28:17 +0000 (09:28 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 3 Jun 2022 11:15:40 +0000 (07:15 -0400)
src/lib/unlang/xlat_tokenize.c
src/tests/unit/xlat/expr.txt
src/tests/unit/xlat/purify.txt

index 34dfc07f1265441562a700fb4974d1850a225559..07bb030d06313e4c673c03a05ec83afc4266e6bb 100644 (file)
@@ -1218,6 +1218,20 @@ ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_head_t const *head, xlat_exp_t
                        xlat_print(out, tmpl_xlat(node->vpt), fr_value_escape_by_quote[node->quote]);
                        goto done;
                }
+
+               // attr or list
+               fr_assert(tmpl_is_list(node->vpt) || tmpl_is_attr(node->vpt));
+               fr_assert(talloc_parent(node->vpt) == node);
+
+               /*
+                *      We prefer the name from the configuration file, otherwise when we get
+                *      &User-Name on input, the tmpl_printer will print the full path, as
+                *      &request[0].User-Name.
+                */
+               if (node->vpt->name[0] == '&') {
+                       FR_SBUFF_IN_STRCPY_RETURN(out, node->fmt);
+                       goto done;
+               }
                break;
 
        case XLAT_ONE_LETTER:
@@ -1231,7 +1245,7 @@ ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_head_t const *head, xlat_exp_t
                 */
                if (node->call.func->print) {
                        slen = node->call.func->print(out, node, node->call.inst->data, e_rules);
-                       if (slen < 0) goto error;
+                       if (slen < 0) return slen;
                        goto done;
                }
                break;
@@ -1253,13 +1267,8 @@ ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_head_t const *head, xlat_exp_t
 
        switch (node->type) {
        case XLAT_TMPL:
-               fr_assert(tmpl_is_list(node->vpt) || tmpl_is_attr(node->vpt));
-               fr_assert(talloc_parent(node->vpt) == node);
                slen = tmpl_attr_print(out, node->vpt, TMPL_ATTR_REF_PREFIX_NO);
-               if (slen < 0) {
-               error:
-                       return slen;
-               }
+               if (slen < 0) return slen;
                break;
 #ifdef HAVE_REGEX
        case XLAT_REGEX:
@@ -1280,7 +1289,7 @@ ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_head_t const *head, xlat_exp_t
 
                if (xlat_exp_head(node->call.args)) {
                        slen = xlat_print(out, node->call.args, &xlat_escape);
-                       if (slen < 0) goto error;
+                       if (slen < 0) return slen;
                }
                break;
 
@@ -1290,17 +1299,17 @@ ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_head_t const *head, xlat_exp_t
 
                if (xlat_exp_head(node->call.args)) {
                        slen = xlat_print(out, node->call.args, &xlat_escape);
-                       if (slen < 0) goto error;
+                       if (slen < 0) return slen;
                }
                break;
 
        case XLAT_ALTERNATE:
                slen = xlat_print(out, node->alternate[0], &xlat_escape);
-               if (slen < 0) goto error;
+               if (slen < 0) return slen;
 
                FR_SBUFF_IN_STRCPY_LITERAL_RETURN(out, ":-");
                slen = xlat_print(out, node->alternate[1], &xlat_escape);
-               if (slen < 0) goto error;
+               if (slen < 0) return slen;
                break;
 
                fr_assert_fail(NULL);
index 2fa458c3c5e9c78803ddba588c14e4d3fe97031d..21ad4ea2c2d1c2c1a694012add56e27c24988791 100644 (file)
@@ -24,10 +24,10 @@ xlat_expr (2 + 3) * 4 + 5
 match (((2 + 3) * 4) + 5)
 
 xlat_expr &NAS-Port + 5
-match (%{NAS-Port} + 5)
+match (&NAS-Port + 5)
 
 xlat_expr &Framed-IP-Address & 0xffff0000
-match (%{Framed-IP-Address} & 0xffff0000)
+match (&Framed-IP-Address & 0xffff0000)
 
 #xlat_expr %{Framed-IP-Address} + 4
 #match (%{Framed-IP-Address} + 0.0.0.4)
@@ -36,16 +36,16 @@ xlat_expr 1 < 4
 match (1 < 4)
 
 xlat_expr &Service-Type == Framed-User
-match (%{Service-Type} == Framed-User)
+match (&Service-Type == Framed-User)
 
-#xlat_expr 1 + (&Service-Type == Framed-User)
-#match (1 + (%{Service-Type} == Framed-User))
+xlat_expr 1 + (&Service-Type == Framed-User)
+match (1 + (&Service-Type == Framed-User))
 
 #
 #  Strings of various forms
 #
 xlat_expr &Filter-Id == "foo"
-match (%{Filter-Id} == "foo")
+match (&Filter-Id == "foo")
 
 xlat_expr "foo" == "bar"
 match ("foo" == "bar")
@@ -53,20 +53,20 @@ match ("foo" == "bar")
 # note '/' is a prefix, not "divide by 24".
 # and a useless cast is removed
 xlat_expr &Framed-IP-Address < (ipv4prefix) 192.168.0.0/24
-match (%{Framed-IP-Address} < 192.168.0.0/24)
+match (&Framed-IP-Address < 192.168.0.0/24)
 
 xlat_expr &Framed-IP-Address < (ipv4prefix) 192.168.0.0
-match (%{Framed-IP-Address} < 192.168.0.0/32)
+match (&Framed-IP-Address < 192.168.0.0/32)
 
 #
 #  For IP addresses, the other side is automatically upcast to a prefix
 #
 xlat_expr &Framed-IP-Address < 192.168.0.0/24
-match  (%{Framed-IP-Address} < 192.168.0.0/24)
+match  (&Framed-IP-Address < 192.168.0.0/24)
 
 #  same as above, but swap the order
 xlat_expr (ipv4prefix) 192.168.0.0/24 > &Framed-IP-Address
-match (192.168.0.0/24 > %{Framed-IP-Address})
+match (192.168.0.0/24 > &Framed-IP-Address)
 
 #
 #  Logical && and ||
@@ -88,7 +88,7 @@ match ((1 < 2) || (4 > 3) || (4 == 4) || (1 < 2))
 #  Other stuff
 #
 xlat_expr &Filter-Id
-match %{Filter-Id}
+match &Filter-Id
 
 xlat_expr %{md5:foo} + "foo"
 match  (%{md5:foo} + "foo")
@@ -134,4 +134,4 @@ match ((1 + 2) * (3 + 4))
 
 
 count
-match 57
+match 59
index ff3216646090afb99d8aec28261c948ac7bd605e..b4c25b82439b206c223d25e82d81792f11403866 100644 (file)
@@ -27,16 +27,16 @@ xlat_purify (2 + 3) * 4 + 5
 match 25
 
 xlat_purify &NAS-Port + 5
-match (%{NAS-Port} + 5)
+match (&NAS-Port + 5)
 
 xlat_purify &Framed-IP-Address & 0xffff0000
-match (%{Framed-IP-Address} & 0xffff0000)
+match (&Framed-IP-Address & 0xffff0000)
 
 #
 #  Can't parse or cast RHS to nothing.
 #
 xlat_purify &Framed-IP-Address + 4
-match (%{Framed-IP-Address} + 4)
+match (&Framed-IP-Address + 4)
 
 xlat_purify 1 < 4
 match yes
@@ -47,17 +47,17 @@ match yes
 #  * the tokenizer needs to track offsets, so it can return the offset which cause the error.
 #
 xlat_purify &Service-Type == Framed-User
-match (%{Service-Type} == Framed-User)
+match (&Service-Type == Framed-User)
 
 #xlat_purify 1 + (&Service-Type == Framed-User)
-#match (1 + (%{Service-Type} == Framed-User))
+#match (1 + (&Service-Type == Framed-User))
 
 #
 #  Strings of various forms
 #
 
 xlat_purify &Filter-Id == "foo"
-match (%{Filter-Id} == "foo")
+match (&Filter-Id == "foo")
 
 xlat_purify "foo" == "bar"
 match no
@@ -65,21 +65,21 @@ match no
 # note '/' is a prefix, not "divide by 24".
 # and a useless cast is removed
 xlat_purify &Framed-IP-Address < (ipv4prefix) 192.168.0.0/24
-match (%{Framed-IP-Address} < 192.168.0.0/24)
+match (&Framed-IP-Address < 192.168.0.0/24)
 
 xlat_purify &Framed-IP-Address < (ipv4prefix) 192.168.0.0
-match (%{Framed-IP-Address} < 192.168.0.0/32)
+match (&Framed-IP-Address < 192.168.0.0/32)
 
 #
 #  For IP addresses, the other side is automatically upcast to a prefix
 #
 
 xlat_purify &Framed-IP-Address < 192.168.0.0/24
-match (%{Framed-IP-Address} < 192.168.0.0/24)
+match (&Framed-IP-Address < 192.168.0.0/24)
 
 #  same as above, but swap the order
 xlat_purify (ipv4prefix) 192.168.0.0/24 > &Framed-IP-Address
-match (192.168.0.0/24 > %{Framed-IP-Address})
+match (192.168.0.0/24 > &Framed-IP-Address)
 
 #
 #  Logical && and ||
@@ -91,7 +91,7 @@ xlat_purify 2 || (1 > 4)
 match yes
 
 xlat_purify &Filter-Id
-match %{Filter-Id}
+match &Filter-Id
 
 xlat_purify %{md5:foo} + "bar"
 match 0xacbd18db4cc2f85cedef654fccc4a4d8626172
@@ -132,28 +132,25 @@ xlat_purify 1 < 2 < 3
 match yes
 
 xlat_purify &Service-Type == 1
-match (%{Service-Type} == 1)
+match (&Service-Type == 1)
 
 #
 #  Convert the RHS to a simpler version
 #
 xlat_purify &Service-Type == (1 + 2)
-match (%{Service-Type} == 3)
+match (&Service-Type == 3)
 
-#
-#  This is so wrong...
-#
-#xlat_purify &Reply-Message == "foo"
-#match ERROR offset 1: Failed resolving attribute in expansion: Message
+xlat_purify &Reply-Message == "foo"
+match (&Reply-Message == "foo")
 
 #
 #  Strings are single quoted
 #
 xlat_purify &Filter-Id == ("foo" + "bar")
-match (%{Filter-Id} == 'foobar')
+match (&Filter-Id == 'foobar')
 
 xlat_purify !&User-Name
-match !%{User-Name}
+match !&User-Name
 
 xlat_purify (1 < 2)
 match yes
@@ -165,4 +162,4 @@ xlat_purify !true
 match no
 
 count
-match 67
+match 69