1
0
mirror of https://github.com/docker-mailserver/docker-mailserver.git synced 2025-08-08 23:06:49 +02:00

refactoring: split helper functions into smaller scripts (#2420)

This commit is contained in:
Georg Lauterbach
2022-02-21 11:56:57 +01:00
committed by GitHub
parent 2927cc47c7
commit b61dfe1e24
41 changed files with 389 additions and 396 deletions

View File

@ -34,6 +34,6 @@ def retrieve_data():
return None
# No match == 'None', we convert to empty string for
# existing error handling by `helper-functions.sh`:
# existing error handling:
result = retrieve_data() or ''
print(result)

View File

@ -1,7 +1,7 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
@ -36,14 +36,14 @@ function __usage
EMAIL="${1}"
RECIPIENT="${2}"
[[ -z ${EMAIL} ]] && { __usage ; errex 'No alias specified' ; }
[[ -z ${RECIPIENT} ]] && { __usage ; errex 'No recipient specified' ; }
[[ -z ${EMAIL} ]] && { __usage ; _errex 'No alias specified' ; }
[[ -z ${RECIPIENT} ]] && { __usage ; _errex 'No recipient specified' ; }
grep \
-qi "^$(escape "${EMAIL}")[a-zA-Z@.\ ]*$(escape "${RECIPIENT}")" \
"${DATABASE}" 2>/dev/null && errex "Alias \"${EMAIL} ${RECIPIENT}\" already exists"
-qi "^$(_escape "${EMAIL}")[a-zA-Z@.\ ]*$(_escape "${RECIPIENT}")" \
"${DATABASE}" 2>/dev/null && _errex "Alias \"${EMAIL} ${RECIPIENT}\" already exists"
if grep -qi "^$(escape "${EMAIL}")" "${DATABASE}" 2>/dev/null
if grep -qi "^$(_escape "${EMAIL}")" "${DATABASE}" 2>/dev/null
then
sed -i "/${EMAIL}/s/$/,${RECIPIENT}/" "${DATABASE}"
else

View File

@ -2,8 +2,8 @@
# shellcheck disable=SC2094
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
@ -39,12 +39,12 @@ FULL_EMAIL="${1}"
shift
PASSWD="${*}"
[[ -z ${FULL_EMAIL} ]] && { __usage ; errex 'No username specified' ; }
[[ ${FULL_EMAIL} =~ .*\@.* ]] || { __usage ; errex 'Username must include the domain' ; }
[[ -z ${FULL_EMAIL} ]] && { __usage ; _errex 'No username specified' ; }
[[ ${FULL_EMAIL} =~ .*\@.* ]] || { __usage ; _errex 'Username must include the domain' ; }
touch "${DATABASE}"
create_lock # Protect config file with lock to avoid race conditions
if grep -qi "^$(escape "${FULL_EMAIL}")|" "${DATABASE}"
_create_lock # Protect config file with lock to avoid race conditions
if grep -qi "^$(_escape "${FULL_EMAIL}")|" "${DATABASE}"
then
echo "User '${FULL_EMAIL}' already exists."
exit 1
@ -54,7 +54,7 @@ if [[ -z ${PASSWD} ]]
then
read -r -s -p "Enter Password: " PASSWD
echo
[[ -z ${PASSWD} ]] && errex "Password must not be empty"
[[ -z ${PASSWD} ]] && _errex "Password must not be empty"
fi
HASH="$(doveadm pw -s SHA512-CRYPT -u "${FULL_EMAIL}" -p "${PASSWD}")"

View File

@ -1,7 +1,7 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-relaymap.cf}
@ -32,8 +32,8 @@ DOMAIN="${1}"
HOST="${2}"
PORT="${3}"
[[ -z ${DOMAIN} ]] && { __usage ; errex 'No domain specified' ; }
[[ -z ${HOST} ]] && { __usage ; errex 'No relay host specified' ; }
[[ -z ${DOMAIN} ]] && { __usage ; _errex 'No domain specified' ; }
[[ -z ${HOST} ]] && { __usage ; _errex 'No relay host specified' ; }
[[ -z ${PORT} ]] && PORT=25
if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null

View File

@ -1,7 +1,7 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-sasl-password.cf}
@ -13,14 +13,14 @@ DOMAIN="${1}"
USER="${2}"
PASSWD="${3}"
[[ -z ${DOMAIN} ]] && { __usage ; errex 'No domain specified' ; }
[[ -z ${USER} ]] && { __usage ; errex 'No username specified' ; }
[[ -z ${DOMAIN} ]] && { __usage ; _errex 'No domain specified' ; }
[[ -z ${USER} ]] && { __usage ; _errex 'No username specified' ; }
if [[ -z ${PASSWD} ]]
then
read -r -s -p "Enter Password: " PASSWD
echo
[[ -z ${PASSWD} ]] && errex 'Password must not be empty'
[[ -z ${PASSWD} ]] && _errex 'Password must not be empty'
fi
if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null

View File

@ -1,7 +1,7 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
@ -12,8 +12,8 @@ function __usage { echo "Usage: delalias <alias@domain> <recipient@other>" ; }
[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }
[[ -z ${EMAIL} ]] && { __usage ; errex "Error: No alias specified" ; }
[[ -z ${RECIPIENT} ]] && { __usage ; errex "Error: No recipient specified" ; }
[[ -z ${EMAIL} ]] && { __usage ; _errex "Error: No alias specified" ; }
[[ -z ${RECIPIENT} ]] && { __usage ; _errex "Error: No recipient specified" ; }
[[ -s ${DATABASE} ]] || exit 0
sed -i \

View File

@ -5,8 +5,8 @@
# ? the same file in the same pipeline", which is a result of ${DATABASE}
# ? being used below. (This disables the message file-wide.)
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
ALIAS_DATABASE="/tmp/docker-mailserver/postfix-virtual.cf"
@ -67,14 +67,14 @@ do
* )
__usage
errex "The option ${OPT} is unknown."
_errex "The option ${OPT} is unknown."
;;
esac
done
shift $((OPTIND-1))
[[ -z ${*} ]] && { __usage ; errex "No user specifed" ; }
[[ -z ${*} ]] && { __usage ; _errex "No user specifed" ; }
[[ -s ${DATABASE} ]] || exit 0
if ! ${MAILDEL}
@ -86,7 +86,7 @@ then
fi
fi
create_lock # Protect config file with lock to avoid race conditions
_create_lock # Protect config file with lock to avoid race conditions
for EMAIL in "${@}"
do
@ -97,7 +97,7 @@ do
# ${EMAIL} must not contain /s and other syntactic characters
UNESCAPED_EMAIL="${EMAIL}"
EMAIL=$(escape "${EMAIL}")
EMAIL=$(_escape "${EMAIL}")
if [[ -f ${DATABASE} ]]
then
@ -155,6 +155,6 @@ use 'sudo docker exec mailserver rm -R /var/mail/${DOMAIN}/${USER}'"
ERROR=true
fi
${ERROR} && errex 'See the messages above.'
${ERROR} && _errex 'See the messages above.'
done
exit 0

View File

@ -1,7 +1,7 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/dovecot-quotas.cf}
USER_DATABASE=${USER_DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
@ -12,13 +12,13 @@ function __usage { echo "Usage: delquota <username@domain>" ; }
USER="${1}"
[[ -z ${USER} ]] && { __usage ; errex "No username specified" ; }
[[ ${USER} =~ .*\@.* ]] || { __usage ; errex "Username must include the domain"; }
[[ -z ${USER} ]] && { __usage ; _errex "No username specified" ; }
[[ ${USER} =~ .*\@.* ]] || { __usage ; _errex "Username must include the domain"; }
if ! grep -qE "^${USER}\|" "${USER_DATABASE}"
then
__usage
errex "user ${USER} does not exist"
_errex "user ${USER} does not exist"
fi
[[ -s ${DATABASE} ]] || exit 0

View File

@ -1,7 +1,7 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-relaymap.cf}
@ -9,7 +9,7 @@ DOMAIN="${1}"
function usage { echo "Usage: excluderelayhost <domain>" ; }
[[ -z ${DOMAIN} ]] && { usage ; errex "no domain specified" ; }
[[ -z ${DOMAIN} ]] && { usage ; _errex "no domain specified" ; }
if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null
then

View File

@ -1,7 +1,7 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
if ! IPTABLES_OUTPUT=$(iptables -L -n 2>&1)
then
@ -75,7 +75,7 @@ else
*)
usage
errex "unknown command: ${1}"
_errex "unknown command: ${1}"
;;
esac

View File

@ -1,12 +1,12 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
[[ -f ${DATABASE} ]] || errex "Error: No postfix-virtual.cf file"
[[ -s ${DATABASE} ]] || errex "Error: Empty postfix-virtual.cf - no aliases have been added"
[[ -f ${DATABASE} ]] || _errex "Error: No postfix-virtual.cf file"
[[ -s ${DATABASE} ]] || _errex "Error: Empty postfix-virtual.cf - no aliases have been added"
grep -v "^\s*$\|^\s*\#" "${DATABASE}"
exit 0

View File

@ -1,11 +1,11 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
# suppress error output, e.g. when listmailuser runs in a fresh container (DMS not running)
# shellcheck source=/dev/null
. /etc/dms-settings 2>/dev/null
source /etc/dms-settings 2>/dev/null
function dovecot_quota_to_hr()
{
@ -23,8 +23,8 @@ function dovecot_quota_to_hr()
DATABASE="/tmp/docker-mailserver/postfix-accounts.cf"
ALIASES="/tmp/docker-mailserver/postfix-virtual.cf"
[[ -f ${DATABASE} ]] || errex "Error: No postfix-accounts.cf file"
[[ -s ${DATABASE} ]] || errex "Error: Empty postfix-accounts.cf - no accounts have been added"
[[ -f ${DATABASE} ]] || _errex "Error: No postfix-accounts.cf file"
[[ -s ${DATABASE} ]] || _errex "Error: Empty postfix-accounts.cf - no accounts have been added"
while read -r LINE
do

View File

@ -1,13 +1,13 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
HOSTNAME=${1}
RECIPIENT=${2}
SENDER=${3}
[[ -x /usr/sbin/pflogsumm ]] || errex "Critical: /usr/sbin/pflogsumm not found"
[[ -x /usr/sbin/pflogsumm ]] || _errex "Critical: /usr/sbin/pflogsumm not found"
# The case that the mail.log.1 file isn't readable shouldn't
# actually be possible with logrotate not rotating empty files..

View File

@ -1,7 +1,7 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
_notify 'inf' 'Printing environment variables. Make sure no sensitive data is copied.'
cat /etc/dms-settings

14
target/bin/quota-warning Executable file
View File

@ -0,0 +1,14 @@
#! /bin/bash
# Report a quota usage warning to an user
PERCENT="${1}"
USER="${2}"
DOMAIN="${3}"
cat << EOF | /usr/lib/dovecot/dovecot-lda -d "${USER}" -o "plugin/quota=maildir:User quota:noenforcing"
From: postmaster@${DOMAIN}
Subject: quota warning
Your mailbox is now ${PERCENT}% full.
EOF

View File

@ -1,7 +1,7 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
set -o errexit
set -o pipefail
@ -11,7 +11,7 @@ HOSTNAME=${1}
RECIPIENT=${2}
SENDER=${3}
[[ -x /usr/sbin/pflogsumm ]] || errex "Critical: /usr/sbin/pflogsumm not found"
[[ -x /usr/sbin/pflogsumm ]] || _errex "Critical: /usr/sbin/pflogsumm not found"
# shellcheck disable=SC2046
BODY=$(gzip -cdfq $(ls -tr /var/log/mail/mail.log*) | /usr/sbin/pflogsumm --problems_first -d yesterday)

View File

@ -1,14 +1,14 @@
#! /bin/bash
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
MODE="${1}"
USER="${3}"
function usage { echo "Usage: ${0} <add|del|list> <send|receive> [<email@domain.com>]" ; }
[[ -z ${MODE} ]] && errex "missing parameters: <add|del|list> <send|receive> [<email@domain.com>]"
[[ -z ${MODE} ]] && _errex "missing parameters: <add|del|list> <send|receive> [<email@domain.com>]"
case ${2} in
send)
@ -19,7 +19,7 @@ case ${2} in
;;
*)
usage
errex "missing parameters. Specify \"send\" or \"receive\""
_errex "missing parameters. Specify \"send\" or \"receive\""
;;
esac
@ -27,12 +27,12 @@ if [[ -z ${USER} ]] && [[ ${MODE} != list ]]
then
read -r -p "User(user@domain.com): " USER
echo
[[ -z ${USER} ]] && errex "User must not be empty"
[[ -z ${USER} ]] && _errex "User must not be empty"
fi
case ${MODE} in
add)
grep -qi "^$(escape "${USER}")" "${DATABASE}" 2>/dev/null && errex "User \"${USER}\" already denied to ${2} mails"
grep -qi "^$(_escape "${USER}")" "${DATABASE}" 2>/dev/null && _errex "User \"${USER}\" already denied to ${2} mails"
if [[ ! -f ${DATABASE} ]]
then
@ -48,7 +48,7 @@ case ${MODE} in
;;
del)
sed -ie "/^$(escape "${USER}")/d" "${DATABASE}" 2>/dev/null || errex "User \"${USER}\" not found."
sed -ie "/^$(_escape "${USER}")/d" "${DATABASE}" 2>/dev/null || _errex "User \"${USER}\" not found."
;;
list)
@ -56,7 +56,7 @@ case ${MODE} in
;;
*)
usage
errex "missing mode. Specify \"add\", \"del\" or \"list\""
_errex "missing mode. Specify \"add\", \"del\" or \"list\""
;;
esac

View File

@ -5,8 +5,8 @@
# ? being used below. (This disables the message file-wide.)
# shellcheck disable=SC2094
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/dovecot-quotas.cf}
USER_DATABASE=${USER_DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
@ -17,36 +17,36 @@ QUOTA="${*}"
function usage { echo "Usage: setquota <user@domain> [<quota>]" ; }
[[ -z ${USER} ]] && { usage ; errex "no username specified" ; }
[[ ${USER} =~ .*\@.* ]] || { usage ; errex "username must include the domain" ; }
[[ -z ${USER} ]] && { usage ; _errex "no username specified" ; }
[[ ${USER} =~ .*\@.* ]] || { usage ; _errex "username must include the domain" ; }
if ! grep -qE "^${USER}\|" "${USER_DATABASE}"
then
usage; errex "user ${USER} does not exist"
usage; _errex "user ${USER} does not exist"
fi
# check quota
if [[ -n ${QUOTA} ]] && ! echo "${QUOTA}" | grep -qE "^([0-9]+(B|k|M|G|T)|0)\$"
then
usage
errex "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))"
_errex "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))"
fi
create_lock # Protect config file with lock to avoid race conditions
_create_lock # Protect config file with lock to avoid race conditions
touch "${DATABASE}"
if [[ -z ${QUOTA} ]]
then
read -r -s "Enter quota (e.g. 10M): " QUOTA
echo
[[ -z "${QUOTA}" ]] && errex "Quota must not be empty. Use 0 for unlimited quota"
[[ -z "${QUOTA}" ]] && _errex "Quota must not be empty. Use 0 for unlimited quota"
fi
# check quota
if [[ -n ${QUOTA} ]] && ! echo "${QUOTA}" | grep -qE "^([0-9]+(B|k|M|G|T)|0)\$"
then
usage
errex "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))"
_errex "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))"
fi
delquota "${USER}"

View File

@ -5,8 +5,8 @@
# ? being used below. (This disables the message file-wide.)
# shellcheck disable=SC2094
# shellcheck source=../scripts/helper-functions.sh
. /usr/local/bin/helper-functions.sh
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
@ -16,18 +16,18 @@ PASSWD="${*}"
function usage { echo "Usage: updatemailuser <user@domain.tld> [password]" ; }
[[ -z ${USER} ]] && { usage ; errex "no username specified" ; }
[[ -z ${USER} ]] && { usage ; _errex "no username specified" ; }
if [[ -z ${PASSWD} ]]
then
read -r -s -p "Enter Password: " PASSWD
echo
[[ -z ${PASSWD} ]] && errex "Password must not be empty"
[[ -z ${PASSWD} ]] && _errex "Password must not be empty"
fi
HASH="$(doveadm pw -s SHA512-CRYPT -u "${USER}" -p "${PASSWD}")"
touch "${DATABASE}"
create_lock # Protect config file with lock to avoid race conditions
grep -qi "^$(escape "${USER}")|" "${DATABASE}" 2>/dev/null || errex "User \"${USER}\" does not exist"
_create_lock # Protect config file with lock to avoid race conditions
grep -qi "^$(_escape "${USER}")|" "${DATABASE}" 2>/dev/null || _errex "User \"${USER}\" does not exist"
sed -i "s ^""${USER}""|.* ""${USER}""|""${HASH}"" " "${DATABASE}"

View File

@ -3,6 +3,6 @@
set -e
# shellcheck source=/dev/null
. /etc/dms-settings
source /etc/dms-settings
find /var/lib/amavis/virusmails/ -type f -mtime +"${VIRUSMAILS_DELETE_DELAY}" -delete