]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
use print xlat for double-quoted strings
authorAlan T. DeKok <aland@freeradius.org>
Mon, 4 Jul 2022 22:03:39 +0000 (18:03 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 4 Jul 2022 22:19:40 +0000 (18:19 -0400)
"foo%{Bar}"

produces "foo" plus the *printable* version of &Bar

"foo" + (string) &Bar

produces "foo" pluse &Bar *cast* to a string.

Both are useful.

src/lib/unlang/xlat_expr.c
src/tests/unit/xlat/cond_base.txt
src/tests/xlat/expr.txt

index e8a01849b791112a1532357e3a94f7bb82c9ef3a..eaf82795749ee9de422e400cbff49ea15f0b6d3f 100644 (file)
@@ -2156,11 +2156,24 @@ static ssize_t tokenize_field(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuff_
                if (quote != T_BARE_WORD) {
                        if (tmpl_rules_cast(vpt) != FR_TYPE_NULL) {
                                type = tmpl_rules_cast(vpt);
+                               MEM(cast = expr_cast_alloc(head, type));
+
                        } else {
                                type = FR_TYPE_STRING;
-                       }
 
-                       MEM(cast = expr_cast_alloc(head, type));
+                               /*
+                                *      The string is T_DOUBLE_QUOTED_STRING, or T_BACK_QUOTED_STRING,
+                                *      both of which have double-quoting rules.
+                                *
+                                *      'foo' can't contain an xlat.
+                                *      /foo/ is forbidden, as we don't expect a regex here.
+                                */
+                               MEM(cast = xlat_exp_alloc(head, XLAT_FUNC, "print", 5));
+                               MEM(cast->call.args = xlat_exp_head_alloc(cast));
+                               MEM(cast->call.func = xlat_func_find("print", 5));
+                               fr_assert(cast->call.func != NULL);
+                               cast->flags = cast->call.func->flags;
+                       }
 
                        xlat_func_append_arg(cast, node, false);
                        node = cast;
index 97b20c00157776a6dd452bb1a325543ce0c88d7c..c58e5b568556cbc6ca5959f5cfddb702b7378521 100644 (file)
@@ -239,7 +239,7 @@ match (&Session-Timeout == '10')
 #  @todo - yuck.  Suppress full path?
 #
 xlat_purify &Event-Timestamp == "January 1, 2012 %{User-Name}"
-match (&Event-Timestamp == %(cast:string "January 1, 2012 %{Request[0].User-Name}"))
+match (&Event-Timestamp == %(print:"January 1, 2012 %{Request[0].User-Name}"))
 
 # This one is NOT an expansion, so it's parsed into normal form
 xlat_purify &Event-Timestamp == 'January 1 2012'
@@ -493,7 +493,7 @@ match ((ipaddr)&PMIP6-Home-IPv4-HoA == &Framed-IP-Address)
 
 # but these are allowed
 xlat_purify <ether>&Tmp-uint64-0 == "%{module: foo}"
-match  ((ether)&Tmp-uint64-0 == %(cast:string "%{module: foo}"))
+match  ((ether)&Tmp-uint64-0 == %(print:"%{module: foo}"))
 
 xlat_purify <ipaddr>&Filter-Id == &Framed-IP-Address
 match ((ipaddr)&Filter-Id == &Framed-IP-Address)
@@ -522,7 +522,7 @@ match (&User-Name[n] == "bob")
 #match &Foo-Bar
 
 xlat_purify &Acct-Input-Octets > "%{Session-Timeout}"
-match (&Acct-Input-Octets > %(cast:string "%{Request[0].Session-Timeout}"))
+match (&Acct-Input-Octets > %(print:"%{Request[0].Session-Timeout}"))
 
 xlat_purify &Acct-Input-Octets > &Session-Timeout
 match (&Acct-Input-Octets > &Session-Timeout)
index 590d95625225020bbc637e79ef5c82a17a6ad450..cb4cbde2e5a56554611a6fedbec19f33cb99defc 100644 (file)
@@ -83,5 +83,42 @@ match ERROR expanding xlat: Program failed with status 1
 xlat_expr -&NAS-IP-Address
 match ERROR expanding xlat: Input is empty
 
+xlat_expr "foo" + (string)&Packet-Src-IP-Address
+match foo127.0.0.1
+
+xlat_expr "foo%{Packet-Src-IP-Address}"
+match foo127.0.0.1
+
+xlat_expr "foo" + (octets)&Packet-Src-IP-Address
+match 0x666f6f7f000001
+
+#
+#  @todo - fix escaping of "match" results?
+#  This just casts the octets to 'string', without
+#  any escaping.
+#
+xlat_expr "foo" + (string)&Packet-Authentication-Vector
+match foo
+
+xlat_expr "foo" + &Packet-Authentication-Vector
+match 0x666f6f00000000000000000000000000000000
+
+#
+#  Really "foo" + 16 octets of 0, but it isn't
+#  printed correctly.  See xlat_expr in unit_test_module.
+#
+xlat_expr "foo" + (string) &Packet-Authentication-Vector
+match foo
+
+#  this is "foo" + PRINTABLE version of &Packet-Authentication-Vector
+xlat_expr "foo%{Packet-Authentication-Vector}"
+match foo0x00000000000000000000000000000000
+
+# no escaping!
+xlat_expr 'foo%{Packet-Authentication-Vector}'
+match foo%{Packet-Authentication-Vector}
+
+
+
 xlat_expr (uint16) ((uint32) 1 + (uint8) 2)
 match 3