Skip to content

Commit

Permalink
Convert hex xlat to new async API
Browse files Browse the repository at this point in the history
  • Loading branch information
pwdng committed Dec 4, 2018
1 parent 16cdb7a commit 4fdd3a7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 64 deletions.
70 changes: 28 additions & 42 deletions src/lib/server/xlat_func.c
Expand Up @@ -258,59 +258,45 @@ static ssize_t xlat_integer(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen,
/** Print data as hex, not as VALUE.
*
*/
static ssize_t xlat_hex(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen,
UNUSED void const *mod_inst, UNUSED void const *xlat_inst,
REQUEST *request, char const *fmt)
static xlat_action_t hex_xlat(TALLOC_CTX *ctx, fr_cursor_t *out,
REQUEST *request, UNUSED void const *xlat_inst, UNUSED void *xlat_thread_inst,
fr_value_box_t **in)
{
size_t i;
VALUE_PAIR *vp;
uint8_t const *p;
size_t len;
fr_value_box_t dst;
uint8_t const *buff = NULL;

while (isspace((int) *fmt)) fmt++;

if ((xlat_fmt_get_vp(&vp, request, fmt) < 0) || !vp) {
error:
return -1;
}
char *buff, *buff_p;
uint8_t const *p, *end;
fr_value_box_t* vb;

/*
* The easy case.
* If there's no input, there's no output
*/
if (vp->vp_type == FR_TYPE_OCTETS) {
p = vp->vp_octets;
len = vp->vp_length;
if (!*in) return XLAT_ACTION_DONE;

/*
* Cast the fr_value_box_t of the VP to an octets string and
* print that as hex.
* Concatenate all input
*/
} else {
if (fr_value_box_cast(request, &dst, FR_TYPE_OCTETS, NULL, &vp->data) < 0) {
RPEDEBUG("Invalid cast");
goto error;
}
len = (size_t)dst.datum.length;
p = buff = dst.vb_octets;
if (fr_value_box_list_concat(ctx, *in, in, FR_TYPE_OCTETS, true) < 0) {
RPEDEBUG("Failed concatenating input");
return XLAT_ACTION_FAIL;
}

rad_assert(p);
p = (*in)->vb_octets;
end = p + (*in)->vb_length;

/*
* Don't truncate the data.
*/
if (outlen < (len * 2)) {
talloc_const_free(buff);
goto error;
}
buff = buff_p = talloc_array(NULL, char, ((*in)->vb_length * 2) + 1);

for (i = 0; i < len; i++) {
snprintf((*out) + (2 * i), 3, "%02x", p[i]);
while (p < end) {
snprintf(buff_p, 3, "%02x", *(p++));
buff_p += 2;
}
talloc_const_free(buff);

return len * 2;
*buff_p = '\0';

MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL, false));
fr_value_box_bstrsteal(vb, vb, NULL, buff, false);

fr_cursor_append(out, vb);

return XLAT_ACTION_DONE;
}

/** Return the tag of an attribute reference
Expand Down Expand Up @@ -2629,7 +2615,6 @@ int xlat_init(void)
XLAT_REGISTER(integer);
XLAT_REGISTER(strlen);
XLAT_REGISTER(length);
XLAT_REGISTER(hex);
XLAT_REGISTER(tag);
XLAT_REGISTER(xlat);
XLAT_REGISTER(map);
Expand Down Expand Up @@ -2670,6 +2655,7 @@ int xlat_init(void)
xlat_async_register(NULL, "base64", base64_xlat, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
xlat_async_register(NULL, "concat", concat_xlat, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
xlat_async_register(NULL, "bin", bin_xlat, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
xlat_async_register(NULL, "hex", hex_xlat, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
xlat_async_register(NULL, "md5", md5_xlat, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
xlat_async_register(NULL, "rand", rand_xlat, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
xlat_async_register(NULL, "string", string_xlat, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
Expand Down
34 changes: 20 additions & 14 deletions src/tests/keywords/hex
Expand Up @@ -19,16 +19,16 @@ update request {
}

update request {
Tmp-String-0 := "%{hex:Tmp-String-0}"
Tmp-String-1 := "%{hex:Tmp-Octets-0}"
Tmp-String-2 := "%{hex:Tmp-IP-Address-0}"
Tmp-String-3 := "%{hex:Tmp-Date-0}"
Tmp-String-4 := "%{hex:Tmp-Integer-0}"
Tmp-String-5 := "%{hex:Tmp-Cast-Abinary}"
Tmp-String-6 := "%{hex:Tmp-Cast-Ifid}"
Tmp-String-7 := "%{hex:Tmp-Cast-IPv6Addr}"
Tmp-String-8 := "%{hex:Tmp-Cast-IPv6Prefix}"
Tmp-String-9 := "%{hex:Tmp-Cast-Byte}"
Tmp-String-0 := "%{hex:%{Tmp-String-0}}"
Tmp-String-1 := "%{hex:%{Tmp-Octets-0}}"
Tmp-String-2 := "%{hex:%{Tmp-IP-Address-0}}"
Tmp-String-3 := "%{hex:%{Tmp-Date-0}}"
Tmp-String-4 := "%{hex:%{Tmp-Integer-0}}"
Tmp-String-5 := "%{hex:%{Tmp-Cast-Abinary}}"
Tmp-String-6 := "%{hex:%{Tmp-Cast-Ifid}}"
Tmp-String-7 := "%{hex:%{Tmp-Cast-IPv6Addr}}"
Tmp-String-8 := "%{hex:%{Tmp-Cast-IPv6Prefix}}"
Tmp-String-9 := "%{hex:%{Tmp-Cast-Byte}}"
}

# String
Expand Down Expand Up @@ -82,10 +82,11 @@ if (Tmp-String-9 != '3a') {
}

update request {
Tmp-String-0 := "%{hex:Tmp-Cast-Short}"
Tmp-String-1 := "%{hex:Tmp-Cast-Ether}"
Tmp-String-2 := "%{hex:Tmp-Cast-Integer64}"
Tmp-String-3 := "%{hex:Tmp-Cast-IPv4Prefix}"
Tmp-String-0 := "%{hex:%{Tmp-Cast-Short}}"
Tmp-String-1 := "%{hex:%{Tmp-Cast-Ether}}"
Tmp-String-2 := "%{hex:%{Tmp-Cast-Integer64}}"
Tmp-String-3 := "%{hex:%{Tmp-Cast-IPv4Prefix}}"
Tmp-String-4 := "%{hex:%{Tmp-Octets-9}}"
}

# short
Expand All @@ -108,4 +109,9 @@ if (Tmp-String-3 != '203938373e') {
test_fail
}

# Empty input
if (Tmp-String-4 != '') {
test_fail
}

success
6 changes: 3 additions & 3 deletions src/tests/keywords/pap
Expand Up @@ -49,7 +49,7 @@ update {
# Hex encoded SSHA password
#
update {
control:Password-With-Header += "{ssha}%{sha1:%{request:User-Password}%{&request:Tmp-String-0}}%{hex:&request:Tmp-String-0}"
control:Password-With-Header += "{ssha}%{hex:%{sha1:%{request:User-Password}%{&request:Tmp-String-0}}}%{hex:%{&request:Tmp-String-0}}"
}

pap.authorize
Expand All @@ -68,7 +68,7 @@ update {
# Base64 encoded SSHA password
#
update {
control:Tmp-String-1 := "%{sha1:%{request:User-Password}%{&request:Tmp-String-0}}%{hex:&request:Tmp-String-0}"
control:Tmp-String-1 := "%{hex:%{sha1:%{request:User-Password}%{&request:Tmp-String-0}}}%{hex:%{&request:Tmp-String-0}}"
}

# To Binary
Expand Down Expand Up @@ -101,7 +101,7 @@ update {
# Base64 of Base64 encoded SSHA password
#
update {
control:Tmp-String-1 := "%{sha1:%{request:User-Password}%{&request:Tmp-String-0}}%{hex:&request:Tmp-String-0}"
control:Tmp-String-1 := "%{hex:%{sha1:%{request:User-Password}%{&request:Tmp-String-0}}}%{hex:%{request:Tmp-String-0}}"
}

# To Binary
Expand Down
6 changes: 3 additions & 3 deletions src/tests/keywords/pap-ssha2
Expand Up @@ -21,7 +21,7 @@ update {
# Hex encoded SSHA2-512 password
#
update {
control:Password-With-Header += "{ssha512}%{sha512:%{request:User-Password}%{&request:Tmp-String-0}}%{hex:&request:Tmp-String-0}"
control:Password-With-Header += "{ssha512}%{sha512:%{request:User-Password}%{&request:Tmp-String-0}}%{hex:%{&request:Tmp-String-0}}"
}

pap.authorize
Expand All @@ -40,7 +40,7 @@ update {
# Base64 encoded SSHA2-512 password
#
update {
control:Tmp-String-1 := "%{sha512:%{request:User-Password}%{&request:Tmp-String-0}}%{hex:&request:Tmp-String-0}"
control:Tmp-String-1 := "%{sha512:%{request:User-Password}%{&request:Tmp-String-0}}%{hex:%{&request:Tmp-String-0}}"
}

# To Binary
Expand Down Expand Up @@ -73,7 +73,7 @@ update {
# Base64 of Base64 encoded SSHA2-512 password
#
update {
control:Tmp-String-1 := "%{sha512:%{request:User-Password}%{&request:Tmp-String-0}}%{hex:&request:Tmp-String-0}"
control:Tmp-String-1 := "%{sha512:%{request:User-Password}%{&request:Tmp-String-0}}%{hex:%{&request:Tmp-String-0}}"
}

# To Binary
Expand Down
10 changes: 8 additions & 2 deletions src/tests/modules/linelog/linelog-escapes.unlang
Expand Up @@ -30,8 +30,11 @@ linelog_escapes
update request {
Tmp-String-0 := `/bin/sh -c "tail -n2 $ENV{MODULE_TEST_DIR}/test_escapes.log"`
}
update request {
Tmp-String-1 := "%{hex:%{&Tmp-String-0}}"
}
# Fixme? Should probably be 666f6f5c6e626172
if ("%{hex:&Tmp-String-0}" == '666f6f0a626172') {
if (Tmp-String-1 == '666f6f0a626172') {
test_pass
}
else {
Expand Down Expand Up @@ -60,7 +63,10 @@ linelog_escapes
update request {
Tmp-String-0 := `/bin/sh -c "tail -n2 $ENV{MODULE_TEST_DIR}/test_escapes.log"`
}
if ("%{hex:&Tmp-String-0}" == '666f6f0a626172') {
update request {
Tmp-String-1 := "%{hex:%{&Tmp-String-0}}"
}
if (Tmp-String-1 == '666f6f0a626172') {
test_pass
}
else {
Expand Down

0 comments on commit 4fdd3a7

Please sign in to comment.