From: Alan T. DeKok Date: Thu, 26 May 2022 13:28:17 +0000 (-0400) Subject: print &Foo instead of %{Foo}, which is less confusing X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a5d99d711fd16f92e1066eafa38799a3bb4b0256;p=thirdparty%2Ffreeradius-server.git print &Foo instead of %{Foo}, which is less confusing --- diff --git a/src/lib/unlang/xlat_tokenize.c b/src/lib/unlang/xlat_tokenize.c index 34dfc07f126..07bb030d063 100644 --- a/src/lib/unlang/xlat_tokenize.c +++ b/src/lib/unlang/xlat_tokenize.c @@ -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); diff --git a/src/tests/unit/xlat/expr.txt b/src/tests/unit/xlat/expr.txt index 2fa458c3c5e..21ad4ea2c2d 100644 --- a/src/tests/unit/xlat/expr.txt +++ b/src/tests/unit/xlat/expr.txt @@ -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 diff --git a/src/tests/unit/xlat/purify.txt b/src/tests/unit/xlat/purify.txt index ff321664609..b4c25b82439 100644 --- a/src/tests/unit/xlat/purify.txt +++ b/src/tests/unit/xlat/purify.txt @@ -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