From c7dec1e33086bbaf8609b0db6bc8799da5d5bb5b Mon Sep 17 00:00:00 2001 From: Casper Date: Mon, 1 Nov 2021 12:09:47 +0100 Subject: [PATCH] fix(listmailuser): Don't query quota, if ENABLE_QUOTAS is not 1 Co-authored-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> --- target/bin/listmailuser | 19 ++++++++++++------ target/bin/print-environment | 2 +- target/scripts/startup/setup-stack.sh | 20 +++++++++--------- test/tests.bats | 29 +++++++++++++++++---------- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/target/bin/listmailuser b/target/bin/listmailuser index 0581aa3d..c59e1ed6 100755 --- a/target/bin/listmailuser +++ b/target/bin/listmailuser @@ -3,6 +3,9 @@ # shellcheck source=../scripts/helper-functions.sh . /usr/local/bin/helper-functions.sh +# suppress error output, e.g. when listmailuser runs in a fresh container (DMS not running) +. /etc/dms-settings 2> /dev/null + function dovecot_quota_to_hr() { if [ "${1}" == "-" ] @@ -23,12 +26,16 @@ while read -r LINE do USER=$(echo "${LINE}" | cut -d'|' -f1) - # ${QUOTA[0]} => current size - # ${QUOTA[1]} => configured size limit - # ${QUOTA[2]} => usage in percent - IFS=' ' read -r -a QUOTA <<< "$(doveadm quota get -u "${USER}" | tail +2 | awk '{ if ($3 == "STORAGE") { print $4" "$5" "$6 } }')" - - echo "* ${USER} ( $(dovecot_quota_to_hr "${QUOTA[0]}") / $(dovecot_quota_to_hr "${QUOTA[1]}") ) [${QUOTA[2]}%]" + if [[ "${ENABLE_QUOTAS}" -eq 1 ]] + then + # ${QUOTA[0]} => current size + # ${QUOTA[1]} => configured size limit + # ${QUOTA[2]} => usage in percent + IFS=' ' read -r -a QUOTA <<< "$(doveadm quota get -u "${USER}" | tail +2 | awk '{ if ($3 == "STORAGE") { print $4" "$5" "$6 } }')" + echo "* ${USER} ( $(dovecot_quota_to_hr "${QUOTA[0]}") / $(dovecot_quota_to_hr "${QUOTA[1]}") ) [${QUOTA[2]}%]" + else + echo "* ${USER}" + fi if [[ -f ${ALIASES} ]] && grep -q "${USER}" "${ALIASES}" then diff --git a/target/bin/print-environment b/target/bin/print-environment index 062c4622..a24e35a2 100644 --- a/target/bin/print-environment +++ b/target/bin/print-environment @@ -4,4 +4,4 @@ . /usr/local/bin/helper-functions.sh _notify 'inf' 'Printing environment variables. Make sure no sensitive data is copied.' -grep ^export /root/.bashrc | sed 's|export ||' +cat /etc/dms-settings diff --git a/target/scripts/startup/setup-stack.sh b/target/scripts/startup/setup-stack.sh index c6e48ea9..adb9dd1a 100644 --- a/target/scripts/startup/setup-stack.sh +++ b/target/scripts/startup/setup-stack.sh @@ -67,20 +67,22 @@ function _setup_default_vars PFLOGSUMM_RECIPIENT="${PFLOGSUMM_RECIPIENT:=${REPORT_RECIPIENT}}" LOGWATCH_RECIPIENT="${LOGWATCH_RECIPIENT:=${REPORT_RECIPIENT}}" + VARS[LOGWATCH_RECIPIENT]="${LOGWATCH_RECIPIENT}" + VARS[PFLOGSUMM_RECIPIENT]="${PFLOGSUMM_RECIPIENT}" + VARS[PFLOGSUMM_SENDER]="${PFLOGSUMM_SENDER}" + VARS[PFLOGSUMM_TRIGGER]="${PFLOGSUMM_TRIGGER}" + VARS[POSTMASTER_ADDRESS]="${POSTMASTER_ADDRESS}" + VARS[REPORT_RECIPIENT]="${REPORT_RECIPIENT}" + VARS[REPORT_SENDER]="${REPORT_SENDER}" + + : >/root/.bashrc # make DMS variables available in login shells and their subprocesses + : >/etc/dms-settings # this file can be sourced by other scripts local VAR for VAR in "${!VARS[@]}" do echo "export ${VAR}='${VARS[${VAR}]}'" >>/root/.bashrc + echo "${VAR}='${VARS[${VAR}]}'" >>/etc/dms-settings done - - { - echo "export PFLOGSUMM_SENDER='${PFLOGSUMM_SENDER}'" - echo "export PFLOGSUMM_TRIGGER='${PFLOGSUMM_TRIGGER}'" - echo "export PFLOGSUMM_RECIPIENT='${PFLOGSUMM_RECIPIENT}'" - echo "export POSTMASTER_ADDRESS='${POSTMASTER_ADDRESS}'" - echo "export REPORT_RECIPIENT='${REPORT_RECIPIENT}'" - echo "export REPORT_SENDER='${REPORT_SENDER}'" - } >>/root/.bashrc } # File/folder permissions are fine when using docker volumes, but may be wrong diff --git a/test/tests.bats b/test/tests.bats index 9eaf64c0..963853b1 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -17,23 +17,24 @@ setup_file() { -v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \ -v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \ -v "$(pwd)/test/onedir":/var/mail-state \ + -e AMAVIS_LOGLEVEL=2 \ + -e DMS_DEBUG=0 \ -e ENABLE_CLAMAV=1 \ - -e SPOOF_PROTECTION=1 \ + -e ENABLE_MANAGESIEVE=1 \ + -e ENABLE_QUOTAS=1 \ -e ENABLE_SPAMASSASSIN=1 \ + -e ENABLE_SRS=1 \ + -e PERMIT_DOCKER=host \ -e REPORT_RECIPIENT=user1@localhost.localdomain \ -e REPORT_SENDER=report1@mail.my-domain.com \ + -e SA_KILL=3.0 \ + -e SA_SPAM_SUBJECT="SPAM: " \ -e SA_TAG=-5.0 \ -e SA_TAG2=2.0 \ - -e SA_KILL=3.0 \ - -e AMAVIS_LOGLEVEL=2 \ - -e SA_SPAM_SUBJECT="SPAM: " \ - -e VIRUSMAILS_DELETE_DELAY=7 \ - -e ENABLE_SRS=1 \ -e SASL_PASSWD="external-domain.com username:password" \ - -e ENABLE_MANAGESIEVE=1 \ - -e PERMIT_DOCKER=host \ - -e DMS_DEBUG=0 \ + -e SPOOF_PROTECTION=1 \ -e SSL_TYPE='snakeoil' \ + -e VIRUSMAILS_DELETE_DELAY=7 \ -h mail.my-domain.com \ --cap-add=SYS_PTRACE \ --tty \ @@ -660,8 +661,14 @@ EOF assert_success } -@test "checking accounts: listmailuser" { - run docker exec mail /bin/sh -c "listmailuser | head -n 1" +@test "checking accounts: listmailuser (quotas disabled)" { + run docker exec mail /bin/sh -c "echo 'ENABLE_QUOTAS=0' >> /etc/dms-settings && listmailuser | head -n 1" + assert_success + assert_output '* user1@localhost.localdomain' +} + +@test "checking accounts: listmailuser (quotas enabled)" { + run docker exec mail /bin/sh -c "sed -i '/ENABLE_QUOTAS=0/d' /etc/dms-settings; listmailuser | head -n 1" assert_success assert_output '* user1@localhost.localdomain ( 12K / ~ ) [0%]' }