Skip to content

Commit

Permalink
Preserve quotation characters when creating templates
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Feb 27, 2015
1 parent e6e10f6 commit 80f18b5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/include/tmpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ typedef struct value_pair_tmpl_t {
//!< attribute, just the string id for
//!< the attribute.
size_t len; //!< Name length.
char quote; //!< Quotation character for "name"

#ifdef HAVE_REGEX
bool iflag; //!< regex - case insensitive (if operand is used in regex comparison)
Expand Down
17 changes: 13 additions & 4 deletions src/main/tmpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ size_t tmpl_prints(char *buffer, size_t bufsize, value_pair_tmpl_t const *vpt, D
return strlen(buffer);
}

c = '\'';
c = vpt->quote;
break;

case TMPL_TYPE_EXEC:
Expand Down Expand Up @@ -1230,7 +1230,7 @@ size_t tmpl_prints(char *buffer, size_t bufsize, value_pair_tmpl_t const *vpt, D

case TMPL_TYPE_DATA:
return vp_data_prints_value(buffer, bufsize, vpt->tmpl_data_type, values,
&vpt->tmpl_data_value, vpt->tmpl_data_length, '\'');
&vpt->tmpl_data_value, vpt->tmpl_data_length, vpt->quote);
}

if (bufsize <= 3) {
Expand Down Expand Up @@ -1270,6 +1270,7 @@ ssize_t tmpl_afrom_str(TALLOC_CTX *ctx, value_pair_tmpl_t **out, char const *nam
request_refs_t request_def, pair_lists_t list_def, bool do_escape)
{
bool do_xlat;
char quote;
char const *p;
ssize_t slen;
PW_TYPE data_type = PW_TYPE_STRING;
Expand All @@ -1282,21 +1283,27 @@ ssize_t tmpl_afrom_str(TALLOC_CTX *ctx, value_pair_tmpl_t **out, char const *nam
* If we can parse it as an attribute, it's an attribute.
* Otherwise, treat it as a literal.
*/
quote = '\0';

slen = tmpl_afrom_attr_str(ctx, &vpt, name, request_def, list_def, true, (name[0] == '&'));
if ((name[0] == '&') && (slen <= 0)) return slen;
if (slen > 0) break;
/* FALL-THROUGH */
goto parse;

case T_SINGLE_QUOTED_STRING:
quote = '\'';

parse:
if (cf_new_escape && do_escape) {
slen = value_data_from_str(ctx, &data, &data_type, NULL, name, inlen, '\'');
slen = value_data_from_str(ctx, &data, &data_type, NULL, name, inlen, quote);
rad_assert(slen >= 0);

vpt = tmpl_alloc(ctx, TMPL_TYPE_LITERAL, data.strvalue, talloc_array_length(data.strvalue) - 1);
talloc_free(data.ptr);
} else {
vpt = tmpl_alloc(ctx, TMPL_TYPE_LITERAL, name, inlen);
}
vpt->quote = quote;
slen = vpt->len;
break;

Expand Down Expand Up @@ -1335,13 +1342,15 @@ ssize_t tmpl_afrom_str(TALLOC_CTX *ctx, value_pair_tmpl_t **out, char const *nam
vpt = tmpl_alloc(ctx, TMPL_TYPE_XLAT, data.strvalue, talloc_array_length(data.strvalue) - 1);
} else {
vpt = tmpl_alloc(ctx, TMPL_TYPE_LITERAL, data.strvalue, talloc_array_length(data.strvalue) - 1);
vpt->quote = '"';
}
talloc_free(data.ptr);
} else {
if (do_xlat) {
vpt = tmpl_alloc(ctx, TMPL_TYPE_XLAT, name, inlen);
} else {
vpt = tmpl_alloc(ctx, TMPL_TYPE_LITERAL, name, inlen);
vpt->quote = '"';
}
}
slen = vpt->len;
Expand Down
36 changes: 21 additions & 15 deletions src/tests/unit/condition.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ data &Framed-IP-Address <= 192.168.0.0/16

# string attributes must be string
condition User-Name == "bob"
data &User-Name == 'bob'
data &User-Name == "bob"

condition User-Name == `bob`
data &User-Name == `bob`
Expand Down Expand Up @@ -279,13 +279,13 @@ condition 0
data false

condition true && (User-Name == "bob")
data &User-Name == 'bob'
data &User-Name == "bob"

condition false && (User-Name == "bob")
data false

condition false || (User-Name == "bob")
data &User-Name == 'bob'
data &User-Name == "bob"

condition true || (User-Name == "bob")
data true
Expand Down Expand Up @@ -318,7 +318,7 @@ condition ("foo" == "%{sql: foo}")
data foo == "%{sql: foo}"

condition ("foo bar" == "%{sql: foo}")
data 'foo bar' == "%{sql: foo}"
data "foo bar" == "%{sql: foo}"

condition ("foo" == "bar")
data false
Expand All @@ -332,7 +332,7 @@ data false
# in it, so it reverts back to a literal.
#
condition (&User-Name == "bob")
data &User-Name == 'bob'
data &User-Name == "bob"

condition (&User-Name == "%{sql: blah}")
data &User-Name == "%{sql: blah}"
Expand Down Expand Up @@ -425,7 +425,7 @@ data ERROR offset 1 Cannot use attribute reference on right side of condition
# is on the LHS of the condition.
#
condition <string>"foo" == &User-Name
data &User-Name == 'foo'
data &User-Name == "foo"

condition <integer>"%{expr: 1 + 1}" < &NAS-Port
data &NAS-Port > "%{expr: 1 + 1}"
Expand Down Expand Up @@ -465,28 +465,34 @@ data <ipaddr>&Class == &Framed-IP-Address
# Tags of zero mean restrict to attributes with no tag
#
condition &Tunnel-Password:0 == "Hello"
data &Tunnel-Password:0 == 'Hello'
data &Tunnel-Password:0 == "Hello"

condition &Tunnel-Password:1 == "Hello"
data &Tunnel-Password:1 == "Hello"

#
# Single quoted strings are left as-is
#
condition &Tunnel-Password:1 == 'Hello'
data &Tunnel-Password:1 == 'Hello'

#
# zero offset into arrays get parsed and ignored
#
condition &User-Name[0] == "bob"
data &User-Name[0] == 'bob'
data &User-Name[0] == "bob"

condition &User-Name[1] == "bob"
data &User-Name[1] == 'bob'
data &User-Name[1] == "bob"

condition &User-Name[n] == "bob"
data &User-Name[n] == 'bob'
data &User-Name[n] == "bob"

condition &Tunnel-Password:1[0] == "Hello"
data &Tunnel-Password:1[0] == 'Hello'
data &Tunnel-Password:1[0] == "Hello"

condition &Tunnel-Password:1[3] == "Hello"
data &Tunnel-Password:1[3] == 'Hello'
data &Tunnel-Password:1[3] == "Hello"

#
# This is allowed for pass2-fixups. Foo-Bar MAY be an attribute.
Expand Down Expand Up @@ -634,7 +640,7 @@ condition &User-Name !~ /^foo\nbar$/
data !&User-Name =~ /^foo\nbar$/

condition &User-Name == "@|\\"
data &User-Name == '@|\\'
data &User-Name == "@|\\"

#condition &User-Name != "foo\nbar"
#data !&User-Name == 'foobar'
condition &User-Name != "foo\nbar"
data !&User-Name == "foo\nbar"

0 comments on commit 80f18b5

Please sign in to comment.