Hi all, This patch against current master adds a new b64dec converter. It takes a base64 encoded string and returns its decoded binary representation.
This converter can be used to e.g. extract the username of a basic auth header to add it to the log: acl BASIC_AUTH hdr_beg(Authorization) "Basic " http-request capture hdr(Authorization),regsub(^Basic\ ,),b64dec if BASIC_AUTH I'm open for suggestions for a better name for the converter. base64_decode might work but doesn't suit the code formatting well and is pretty long... As a note to reviewers: please be aware that I'm not a C programmer at all and I am way outside of my comfort zone here. As such, this function might have unhandled edge-cases. I tried to model it according to the existing base64 converter and my understanding of how the converters are supposed to work but might have missed something. Once verified, I think this converter can be safely added to the supported stable versions of HAProxy. Cheers, Holger
>From b6d63d491a82d9297b649b0a4bf043b93e8161ad Mon Sep 17 00:00:00 2001 From: Holger Just <[email protected]> Date: Sat, 6 May 2017 00:56:53 +0200 Subject: [PATCH] MINOR: sample: Add b64dec sample converter Add "b64dec" as a new converter which can be used to decode a base64 encoded string into its binary representation. It performs the inverse operation of the "base64" converter. --- doc/configuration.txt | 4 ++++ src/sample.c | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/doc/configuration.txt b/doc/configuration.txt index 06a1a2af..25ab2772 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -12410,6 +12410,10 @@ base64 transfer binary content in a way that can be reliably transferred (eg: an SSL ID can be copied in a header). +b64dec + Converts (decodes) a base64 encoded input string to its binary + representation. It performs the inverse operation of base64(). + bool Returns a boolean TRUE if the input value of type signed integer is non-null, otherwise returns FALSE. Used in conjunction with and(), it can be diff --git a/src/sample.c b/src/sample.c index 71d4e32a..2e65e881 100644 --- a/src/sample.c +++ b/src/sample.c @@ -1481,6 +1481,23 @@ static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, v return 1; } +static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private) +{ + struct chunk *trash = get_trash_chunk(); + int bin_len; + + trash->len = 0; + bin_len = base64dec(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size); + if (bin_len < 0) + return 0; + + trash->len = bin_len; + smp->data.u.str = *trash; + smp->data.type = SMP_T_BIN; + smp->flags &= ~SMP_F_CONST; + return 1; +} + static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private) { struct chunk *trash = get_trash_chunk(); @@ -2715,6 +2732,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, { #endif { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR }, + { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN }, { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR }, -- 2.12.0

