From: Arran Cudbard-Bell Date: Thu, 17 May 2018 07:55:06 +0000 (+0600) Subject: Convert Base64 xlat to new API X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fcdce5cbbea1e1500ee46f4b14e2593b0b58fc3c;p=thirdparty%2Ffreeradius-server.git Convert Base64 xlat to new API --- diff --git a/src/main/xlat_func.c b/src/main/xlat_func.c index aed7a3c8f5d..c9a0e9a4bd9 100644 --- a/src/main/xlat_func.c +++ b/src/main/xlat_func.c @@ -32,6 +32,7 @@ RCSID("$Id$") #include #include #include +#include #ifdef HAVE_OPENSSL_EVP_H # include #endif @@ -1323,33 +1324,46 @@ static ssize_t pairs_xlat(TALLOC_CTX *ctx, char **out, size_t outlen, * * Example: "%{base64:foo}" == "Zm9v" */ -static ssize_t base64_xlat(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 xlat_base64(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 inlen; - uint8_t *p; - TALLOC_CTX *tmp_ctx = NULL; - ssize_t ret; - - VALUE_FROM_FMT(tmp_ctx, p, inlen, request, fmt); - + size_t alen; + ssize_t elen; + char *buff; + fr_value_box_t *vb; /* - * We can accurately calculate the length of the output string - * if it's larger than outlen, the output would be useless so abort. + * If there's no input, there's no output */ - if ((FR_BASE64_ENC_LENGTH(inlen) + 1) > outlen) { - REDEBUG("xlat failed"); + if (!in) return XLAT_ACTION_DONE; - talloc_free(tmp_ctx); + if (fr_value_box_list_concat(ctx, *in, in, FR_TYPE_OCTETS, true) < 0) { + RPEDEBUG("Failed concatenating input"); + return XLAT_ACTION_FAIL; + } - return -1; + MEM(vb = fr_value_box_alloc_null(ctx)); + alen = FR_BASE64_ENC_LENGTH((*in)->vb_length); + MEM(buff = talloc_array(ctx, char, alen + 1)); + + elen = fr_base64_encode(buff, alen + 1, (*in)->vb_octets, (*in)->vb_length); + if (elen < 0) { + RPEDEBUG("Base64 encoding failed"); + talloc_free(vb); + return XLAT_ACTION_FAIL; } - ret = fr_base64_encode(*out, outlen, p, inlen); - talloc_free(tmp_ctx); + rad_assert(elen <= alen); - return ret; + if (fr_value_box_bstrsnteal(vb, vb, NULL, &buff, elen, false) < 0) { + RPEDEBUG("Failed assigning encoded data buffer to box"); + talloc_free(vb); + return XLAT_ACTION_FAIL; + } + + fr_cursor_append(out, vb); + + return XLAT_ACTION_DONE; } /** Convert base64 to hex @@ -2149,7 +2163,7 @@ static xlat_action_t xlat_concat(TALLOC_CTX *ctx, fr_cursor_t *out, buff = fr_value_box_list_asprint(result, *in, ",", '\0'); if (!buff) goto error; - fr_value_box_strsteal(result, result, NULL, buff, fr_value_box_list_tainted(*in)); + fr_value_box_bstrsteal(result, result, NULL, buff, fr_value_box_list_tainted(*in)); fr_cursor_insert(out, result); @@ -2463,7 +2477,7 @@ int xlat_init(void) xlat_register(NULL, "hmacsha1", hmac_sha1_xlat, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true); xlat_register(NULL, "pairs", pairs_xlat, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true); - xlat_register(NULL, "base64", base64_xlat, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true); + xlat_register(NULL, "base64tohex", base64_to_hex_xlat, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true); xlat_register(NULL, "explode", explode_xlat, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true); @@ -2477,6 +2491,7 @@ int xlat_init(void) rad_assert(c != NULL); c->internal = true; + xlat_async_register(NULL, "base64", xlat_base64, NULL, NULL, NULL, NULL, NULL, NULL, NULL); xlat_async_register(NULL, "concat", xlat_concat, NULL, NULL, NULL, NULL, NULL, NULL, NULL); xlat_async_register(NULL, "bin", xlat_bin, NULL, NULL, NULL, NULL, NULL, NULL, NULL); diff --git a/src/tests/keywords/base64 b/src/tests/keywords/base64 index e1b198b1e69..5ca090c28a1 100644 --- a/src/tests/keywords/base64 +++ b/src/tests/keywords/base64 @@ -19,16 +19,16 @@ update request { } update request { - &Tmp-String-0 := "%{base64:&Tmp-String-0}" - &Tmp-String-1 := "%{base64:&Tmp-Octets-0}" - &Tmp-String-2 := "%{base64:&Tmp-IP-Address-0}" - &Tmp-String-3 := "%{base64:&Tmp-Date-0}" - &Tmp-String-4 := "%{base64:&Tmp-Integer-0}" - &Tmp-String-5 := "%{base64:&Tmp-Cast-Abinary}" - &Tmp-String-6 := "%{base64:&Tmp-Cast-Ifid}" - &Tmp-String-7 := "%{base64:&Tmp-Cast-IPv6Addr}" - &Tmp-String-8 := "%{base64:&Tmp-Cast-IPv6Prefix}" - &Tmp-String-9 := "%{base64:&Tmp-Cast-Byte}" + &Tmp-String-0 := "%{base64:%{Tmp-String-0}}" + &Tmp-String-1 := "%{base64:%{Tmp-Octets-0}}" + &Tmp-String-2 := "%{base64:%{Tmp-IP-Address-0}}" + &Tmp-String-3 := "%{base64:%{Tmp-Date-0}}" + &Tmp-String-4 := "%{base64:%{Tmp-Integer-0}}" + &Tmp-String-5 := "%{base64:%{Tmp-Cast-Abinary}}" + &Tmp-String-6 := "%{base64:%{Tmp-Cast-Ifid}}" + &Tmp-String-7 := "%{base64:%{Tmp-Cast-IPv6Addr}}" + &Tmp-String-8 := "%{base64:%{Tmp-Cast-IPv6Prefix}}" + &Tmp-String-9 := "%{base64:%{Tmp-Cast-Byte}}" } # String - bin 0x39383730 @@ -82,10 +82,10 @@ if (&Tmp-String-9[0] != 'Og==') { } update request { - &Tmp-String-0 := "%{base64:&Tmp-Cast-Short}" - &Tmp-String-1 := "%{base64:&Tmp-Cast-Ether}" - &Tmp-String-2 := "%{base64:&Tmp-Cast-Integer64}" - &Tmp-String-3 := "%{base64:&Tmp-Cast-IPv4Prefix}" + &Tmp-String-0 := "%{base64:%{Tmp-Cast-Short}}" + &Tmp-String-1 := "%{base64:%{Tmp-Cast-Ether}}" + &Tmp-String-2 := "%{base64:%{Tmp-Cast-Integer64}}" + &Tmp-String-3 := "%{base64:%{Tmp-Cast-IPv4Prefix}}" } # short - bin 0x373b diff --git a/src/tests/keywords/pap b/src/tests/keywords/pap index 94a1d73b7b1..a490e9ec845 100644 --- a/src/tests/keywords/pap +++ b/src/tests/keywords/pap @@ -31,7 +31,7 @@ update { Tmp-String-1 := "{clear}%{User-Password}" } update { - control:Password-With-Header := "%{base64:&request:Tmp-String-1}" + control:Password-With-Header := "%{base64:%{request:Tmp-String-1}}" } pap.authorize pap.authenticate { @@ -78,7 +78,7 @@ update { # To Base64 update { - control:Tmp-String-1 := "%{base64:&control:Tmp-Octets-0}" + control:Tmp-String-1 := "%{base64:%{control:Tmp-Octets-0}}" } update { @@ -111,11 +111,11 @@ update { # To Base64 update { - control:Tmp-String-1 := "{ssha}%{base64:&control:Tmp-Octets-0}" + control:Tmp-String-1 := "{ssha}%{base64:%{control:Tmp-Octets-0}}" } update { - control:Password-With-Header += "%{base64:&control:Tmp-String-1}" + control:Password-With-Header += "%{base64:%{control:Tmp-String-1}}" } pap.authorize diff --git a/src/tests/keywords/pap-ssha2 b/src/tests/keywords/pap-ssha2 index 7e37a6db09c..3bfa316ad27 100644 --- a/src/tests/keywords/pap-ssha2 +++ b/src/tests/keywords/pap-ssha2 @@ -50,7 +50,7 @@ update { # To Base64 update { - control:Tmp-String-1 := "%{base64:&control:Tmp-Octets-0}" + control:Tmp-String-1 := "%{base64:%{&control:Tmp-Octets-0}}" } update { @@ -83,11 +83,11 @@ update { # To Base64 update { - control:Tmp-String-1 := "{ssha512}%{base64:&control:Tmp-Octets-0}" + control:Tmp-String-1 := "{ssha512}%{base64:%{control:Tmp-Octets-0}}" } update { - control:Password-With-Header += "%{base64:&control:Tmp-String-1}" + control:Password-With-Header += "%{base64:%{&control:Tmp-String-1}}" } pap.authorize