Fix #180: Rename authentication methods

This is a really dirt fix for issue #180. It will replace "well-known"
enumerated plugin names with SASL mechanism names. While this is more of
a "bandage" than the actual proper fix, I have currently found no better
ways to do it.

Most likely this issue will rise up again with another SASL plugin and
we will deal with it then. At this stage, it should cover most of the
use cases transparently.
This commit is contained in:
Bojan Čekrlić 2024-04-21 16:38:54 +01:00
parent a26f85d79d
commit 4b46b1d173
4 changed files with 97 additions and 4 deletions

View file

@ -418,7 +418,7 @@ postfix_setup_xoauth2_post_setup() {
# Ubuntu/Debian have renamed pluginviewer to saslpluginviewer so this fails with those distros.
plugin_viewer="saslpluginviewer"
fi
other_plugins="$(${plugin_viewer} -c | grep Plugin | cut -d\ -f2 | cut -c2- | rev | cut -c2- | rev | grep -v EXTERNAL | grep -v sasl-xoauth2 | tr '\n' ',' | rev | cut -c2- | rev)"
other_plugins="$(${plugin_viewer} -c | grep Plugin | cut -d\ -f2 | cut -c2- | rev | cut -c2- | rev | grep -v EXTERNAL | grep -v sasl-xoauth2 | tr '\n' ',' | rev | cut -c2- | rev | convert_plugin_names_to_filter_names)"
do_postconf -e "smtp_sasl_mechanism_filter=${other_plugins}"
fi
}

View file

@ -227,4 +227,61 @@ zone_info_dir() {
return
}
###################################################################
# Remove leading and trailing whitespace from string
###################################################################
trim() {
local var
IFS='' read -d -r var
#var="$(<&1)"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
printf '%s' "${var}"
}
###################################################################
# Potential fix for #180. Plugin names do not neccessarily match
# filter names.
#
# This is an utility method which converts SASL plugin names into
# filter names. There's no reliable way to guess this, so the names
# have been hardcoded here.
#
# INPUT:
# The method expects as an input a list of plugin names, comma
# separated.
#
# OUTPUT:
# The list of plugin names, comma separated.
###################################################################
convert_plugin_names_to_filter_names() {
local line first value lowercase
while IFS=$',' read -ra line; do
for value in "${line[@]}"; do
value="$(printf '%s' "${value}" | trim)"
if [[ -z "${value}" ]]; then
continue;
fi
if [[ -z "${first}" ]]; then
first="0"
else
printf '%s' ','
fi
lowercase="${value,,}"
if [[ "${lowercase}" == "digestmd5" ]]; then
printf '%s' 'DIGEST-MD5'
elif [[ "${lowercase}" == "crammd5" ]]; then
printf '%s' 'CRAM-MD5'
else
printf '%s' "${value}"
fi
done
done
}
export reset green yellow orange orange_emphasis lightblue red gray emphasis underline

View file

@ -0,0 +1,35 @@
#!/usr/bin/env bats
load /code/scripts/common.sh
assert_equals() {
local expected="$1"
local actual="$2"
if [[ "${expected}" != "${actual}" ]]; then
echo "Expected: \"${expected}\". Got: \"${actual}\"." >&2
exit 1
fi
}
@test "check if trim works properly" {
assert_equals "bar" "$(echo "bar" | trim)"
assert_equals "foo bar" "$(echo "foo bar" | trim)"
assert_equals "foo bar" "$(echo " foo bar" | trim)"
assert_equals "foo bar" "$(echo "foo bar " | trim)"
assert_equals "foo bar" "$(echo " foo bar " | trim)"
assert_equals "foo bar" "$(printf '%s' " foo bar" | trim)"
assert_equals "foo bar" "$(printf '%s' $'\t\tfoo bar\r\n' | trim)"
assert_equals "foo bar" "$(printf '%s' $' foo bar\r\n' | trim)"
}
@test "check if convert_plugin_names_to_filter_names works" {
assert_equals "foo" "$(echo "foo" | convert_plugin_names_to_filter_names)"
assert_equals "foo,bar" "$(echo "foo,bar" | convert_plugin_names_to_filter_names)"
assert_equals "foo,bar,baz" "$(echo "foo, bar, baz," | convert_plugin_names_to_filter_names)"
assert_equals "DIGEST-MD5" "$(echo "digestmd5" | convert_plugin_names_to_filter_names)"
assert_equals "CRAM-MD5" "$(echo "crammd5" | convert_plugin_names_to_filter_names)"
assert_equals "DIGEST-MD5,ntlm,CRAM-MD5,plain,login,anonymous" "$(echo "digestmd5,ntlm,crammd5,plain,login,anonymous" | convert_plugin_names_to_filter_names)"
assert_equals "DIGEST-MD5,ntlm,CRAM-MD5,plain,login,anonymous" "$(echo "DIGESTMD5,ntlm,CRAMMD5,plain,login,anonymous" | convert_plugin_names_to_filter_names)"
}

View file

@ -1,4 +1,5 @@
ARG ALPINE_VERSION=latest
# Changed this to specific alpine version so it doesn't get refreshed / pulled from Docker hub every time.
ARG ALPINE_VERSION=3.19
FROM alpine:${ALPINE_VERSION} as build
ARG SASL_XOAUTH2_REPO_URL=https://github.com/tarickb/sasl-xoauth2.git