renamed function _errex -> _exit_with_error (#2497)

This commit is contained in:
Georg Lauterbach 2022-03-21 15:01:07 +01:00 committed by GitHub
parent 37c8e44566
commit b7bf40ac21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 45 additions and 45 deletions

View file

@ -36,12 +36,12 @@ function __usage
EMAIL="${1}" EMAIL="${1}"
RECIPIENT="${2}" RECIPIENT="${2}"
[[ -z ${EMAIL} ]] && { __usage ; _errex 'No alias specified' ; } [[ -z ${EMAIL} ]] && { __usage ; _exit_with_error 'No alias specified' ; }
[[ -z ${RECIPIENT} ]] && { __usage ; _errex 'No recipient specified' ; } [[ -z ${RECIPIENT} ]] && { __usage ; _exit_with_error 'No recipient specified' ; }
grep \ grep \
-qi "^$(_escape "${EMAIL}")[a-zA-Z@.\ ]*$(_escape "${RECIPIENT}")" \ -qi "^$(_escape "${EMAIL}")[a-zA-Z@.\ ]*$(_escape "${RECIPIENT}")" \
"${DATABASE}" 2>/dev/null && _errex "Alias \"${EMAIL} ${RECIPIENT}\" already exists" "${DATABASE}" 2>/dev/null && _exit_with_error "Alias \"${EMAIL} ${RECIPIENT}\" already exists"
if grep -qi "^$(_escape "${EMAIL}")" "${DATABASE}" 2>/dev/null if grep -qi "^$(_escape "${EMAIL}")" "${DATABASE}" 2>/dev/null
then then

View file

@ -39,8 +39,8 @@ FULL_EMAIL="${1}"
shift shift
PASSWD="${*}" PASSWD="${*}"
[[ -z ${FULL_EMAIL} ]] && { __usage ; _errex 'No username specified' ; } [[ -z ${FULL_EMAIL} ]] && { __usage ; _exit_with_error 'No username specified' ; }
[[ ${FULL_EMAIL} =~ .*\@.* ]] || { __usage ; _errex 'Username must include the domain' ; } [[ ${FULL_EMAIL} =~ .*\@.* ]] || { __usage ; _exit_with_error 'Username must include the domain' ; }
touch "${DATABASE}" touch "${DATABASE}"
_create_lock # Protect config file with lock to avoid race conditions _create_lock # Protect config file with lock to avoid race conditions
@ -54,7 +54,7 @@ if [[ -z ${PASSWD} ]]
then then
read -r -s -p "Enter Password: " PASSWD read -r -s -p "Enter Password: " PASSWD
echo echo
[[ -z ${PASSWD} ]] && _errex "Password must not be empty" [[ -z ${PASSWD} ]] && _exit_with_error "Password must not be empty"
fi fi
HASH="$(doveadm pw -s SHA512-CRYPT -u "${FULL_EMAIL}" -p "${PASSWD}")" HASH="$(doveadm pw -s SHA512-CRYPT -u "${FULL_EMAIL}" -p "${PASSWD}")"

View file

@ -32,8 +32,8 @@ DOMAIN="${1}"
HOST="${2}" HOST="${2}"
PORT="${3}" PORT="${3}"
[[ -z ${DOMAIN} ]] && { __usage ; _errex 'No domain specified' ; } [[ -z ${DOMAIN} ]] && { __usage ; _exit_with_error 'No domain specified' ; }
[[ -z ${HOST} ]] && { __usage ; _errex 'No relay host specified' ; } [[ -z ${HOST} ]] && { __usage ; _exit_with_error 'No relay host specified' ; }
[[ -z ${PORT} ]] && PORT=25 [[ -z ${PORT} ]] && PORT=25
if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null

View file

@ -13,14 +13,14 @@ DOMAIN="${1}"
USER="${2}" USER="${2}"
PASSWD="${3}" PASSWD="${3}"
[[ -z ${DOMAIN} ]] && { __usage ; _errex 'No domain specified' ; } [[ -z ${DOMAIN} ]] && { __usage ; _exit_with_error 'No domain specified' ; }
[[ -z ${USER} ]] && { __usage ; _errex 'No username specified' ; } [[ -z ${USER} ]] && { __usage ; _exit_with_error 'No username specified' ; }
if [[ -z ${PASSWD} ]] if [[ -z ${PASSWD} ]]
then then
read -r -s -p "Enter Password: " PASSWD read -r -s -p "Enter Password: " PASSWD
echo echo
[[ -z ${PASSWD} ]] && _errex 'Password must not be empty' [[ -z ${PASSWD} ]] && _exit_with_error 'Password must not be empty'
fi fi
if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null

View file

@ -12,8 +12,8 @@ function __usage { echo "Usage: delalias <alias@domain> <recipient@other>" ; }
[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }
[[ -z ${EMAIL} ]] && { __usage ; _errex "Error: No alias specified" ; } [[ -z ${EMAIL} ]] && { __usage ; _exit_with_error "Error: No alias specified" ; }
[[ -z ${RECIPIENT} ]] && { __usage ; _errex "Error: No recipient specified" ; } [[ -z ${RECIPIENT} ]] && { __usage ; _exit_with_error "Error: No recipient specified" ; }
[[ -s ${DATABASE} ]] || exit 0 [[ -s ${DATABASE} ]] || exit 0
sed -i \ sed -i \

View file

@ -67,14 +67,14 @@ do
* ) * )
__usage __usage
_errex "The option ${OPT} is unknown." _exit_with_error "The option ${OPT} is unknown."
;; ;;
esac esac
done done
shift $((OPTIND-1)) shift $((OPTIND-1))
[[ -z ${*} ]] && { __usage ; _errex "No user specifed" ; } [[ -z ${*} ]] && { __usage ; _exit_with_error "No user specifed" ; }
[[ -s ${DATABASE} ]] || exit 0 [[ -s ${DATABASE} ]] || exit 0
if ! ${MAILDEL} if ! ${MAILDEL}
@ -155,6 +155,6 @@ use 'sudo docker exec mailserver rm -R /var/mail/${DOMAIN}/${USER}'"
ERROR=true ERROR=true
fi fi
${ERROR} && _errex 'See the messages above.' ${ERROR} && _exit_with_error 'See the messages above.'
done done
exit 0 exit 0

View file

@ -12,13 +12,13 @@ function __usage { echo "Usage: delquota <username@domain>" ; }
USER="${1}" USER="${1}"
[[ -z ${USER} ]] && { __usage ; _errex "No username specified" ; } [[ -z ${USER} ]] && { __usage ; _exit_with_error "No username specified" ; }
[[ ${USER} =~ .*\@.* ]] || { __usage ; _errex "Username must include the domain"; } [[ ${USER} =~ .*\@.* ]] || { __usage ; _exit_with_error "Username must include the domain"; }
if ! grep -qE "^${USER}\|" "${USER_DATABASE}" if ! grep -qE "^${USER}\|" "${USER_DATABASE}"
then then
__usage __usage
_errex "user ${USER} does not exist" _exit_with_error "user ${USER} does not exist"
fi fi
[[ -s ${DATABASE} ]] || exit 0 [[ -s ${DATABASE} ]] || exit 0

View file

@ -9,7 +9,7 @@ DOMAIN="${1}"
function usage { echo "Usage: excluderelayhost <domain>" ; } function usage { echo "Usage: excluderelayhost <domain>" ; }
[[ -z ${DOMAIN} ]] && { usage ; _errex "no domain specified" ; } [[ -z ${DOMAIN} ]] && { usage ; _exit_with_error "no domain specified" ; }
if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null if grep -qi "^@${DOMAIN}" "${DATABASE}" 2>/dev/null
then then

View file

@ -75,7 +75,7 @@ else
*) *)
usage usage
_errex "unknown command: ${1}" _exit_with_error "unknown command: ${1}"
;; ;;
esac esac

View file

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

View file

@ -23,8 +23,8 @@ function dovecot_quota_to_hr()
DATABASE="/tmp/docker-mailserver/postfix-accounts.cf" DATABASE="/tmp/docker-mailserver/postfix-accounts.cf"
ALIASES="/tmp/docker-mailserver/postfix-virtual.cf" ALIASES="/tmp/docker-mailserver/postfix-virtual.cf"
[[ -f ${DATABASE} ]] || _errex "Error: No postfix-accounts.cf file" [[ -f ${DATABASE} ]] || _exit_with_error "Error: No postfix-accounts.cf file"
[[ -s ${DATABASE} ]] || _errex "Error: Empty postfix-accounts.cf - no accounts have been added" [[ -s ${DATABASE} ]] || _exit_with_error "Error: Empty postfix-accounts.cf - no accounts have been added"
while read -r LINE while read -r LINE
do do

View file

@ -7,7 +7,7 @@ HOSTNAME=${1}
RECIPIENT=${2} RECIPIENT=${2}
SENDER=${3} SENDER=${3}
[[ -x /usr/sbin/pflogsumm ]] || _errex "Critical: /usr/sbin/pflogsumm not found" [[ -x /usr/sbin/pflogsumm ]] || _exit_with_error "Critical: /usr/sbin/pflogsumm not found"
# The case that the mail.log.1 file isn't readable shouldn't # The case that the mail.log.1 file isn't readable shouldn't
# actually be possible with logrotate not rotating empty files.. # actually be possible with logrotate not rotating empty files..

View file

@ -11,7 +11,7 @@ HOSTNAME=${1}
RECIPIENT=${2} RECIPIENT=${2}
SENDER=${3} SENDER=${3}
[[ -x /usr/sbin/pflogsumm ]] || _errex "Critical: /usr/sbin/pflogsumm not found" [[ -x /usr/sbin/pflogsumm ]] || _exit_with_error "Critical: /usr/sbin/pflogsumm not found"
# shellcheck disable=SC2046 # shellcheck disable=SC2046
BODY=$(gzip -cdfq $(ls -tr /var/log/mail/mail.log*) | /usr/sbin/pflogsumm --problems_first -d yesterday) BODY=$(gzip -cdfq $(ls -tr /var/log/mail/mail.log*) | /usr/sbin/pflogsumm --problems_first -d yesterday)

View file

@ -8,7 +8,7 @@ USER="${3}"
function usage { echo "Usage: ${0} <add|del|list> <send|receive> [<email@domain.com>]" ; } 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} ]] && _exit_with_error "missing parameters: <add|del|list> <send|receive> [<email@domain.com>]"
case ${2} in case ${2} in
send) send)
@ -19,7 +19,7 @@ case ${2} in
;; ;;
*) *)
usage usage
_errex "missing parameters. Specify \"send\" or \"receive\"" _exit_with_error "missing parameters. Specify \"send\" or \"receive\""
;; ;;
esac esac
@ -27,12 +27,12 @@ if [[ -z ${USER} ]] && [[ ${MODE} != list ]]
then then
read -r -p "User(user@domain.com): " USER read -r -p "User(user@domain.com): " USER
echo echo
[[ -z ${USER} ]] && _errex "User must not be empty" [[ -z ${USER} ]] && _exit_with_error "User must not be empty"
fi fi
case ${MODE} in case ${MODE} in
add) 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 && _exit_with_error "User \"${USER}\" already denied to ${2} mails"
if [[ ! -f ${DATABASE} ]] if [[ ! -f ${DATABASE} ]]
then then
@ -48,7 +48,7 @@ case ${MODE} in
;; ;;
del) del)
sed -ie "/^$(_escape "${USER}")/d" "${DATABASE}" 2>/dev/null || _errex "User \"${USER}\" not found." sed -ie "/^$(_escape "${USER}")/d" "${DATABASE}" 2>/dev/null || _exit_with_error "User \"${USER}\" not found."
;; ;;
list) list)
@ -56,7 +56,7 @@ case ${MODE} in
;; ;;
*) *)
usage usage
_errex "missing mode. Specify \"add\", \"del\" or \"list\"" _exit_with_error "missing mode. Specify \"add\", \"del\" or \"list\""
;; ;;
esac esac

View file

@ -17,19 +17,19 @@ QUOTA="${*}"
function usage { echo "Usage: setquota <user@domain> [<quota>]" ; } function usage { echo "Usage: setquota <user@domain> [<quota>]" ; }
[[ -z ${USER} ]] && { usage ; _errex "no username specified" ; } [[ -z ${USER} ]] && { usage ; _exit_with_error "no username specified" ; }
[[ ${USER} =~ .*\@.* ]] || { usage ; _errex "username must include the domain" ; } [[ ${USER} =~ .*\@.* ]] || { usage ; _exit_with_error "username must include the domain" ; }
if ! grep -qE "^${USER}\|" "${USER_DATABASE}" if ! grep -qE "^${USER}\|" "${USER_DATABASE}"
then then
usage; _errex "user ${USER} does not exist" usage; _exit_with_error "user ${USER} does not exist"
fi fi
# check quota # check quota
if [[ -n ${QUOTA} ]] && ! echo "${QUOTA}" | grep -qE "^([0-9]+(B|k|M|G|T)|0)\$" if [[ -n ${QUOTA} ]] && ! echo "${QUOTA}" | grep -qE "^([0-9]+(B|k|M|G|T)|0)\$"
then then
usage usage
_errex "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))" _exit_with_error "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))"
fi fi
_create_lock # Protect config file with lock to avoid race conditions _create_lock # Protect config file with lock to avoid race conditions
@ -39,14 +39,14 @@ if [[ -z ${QUOTA} ]]
then then
read -r -s "Enter quota (e.g. 10M): " QUOTA read -r -s "Enter quota (e.g. 10M): " QUOTA
echo echo
[[ -z "${QUOTA}" ]] && _errex "Quota must not be empty. Use 0 for unlimited quota" [[ -z "${QUOTA}" ]] && _exit_with_error "Quota must not be empty. Use 0 for unlimited quota"
fi fi
# check quota # check quota
if [[ -n ${QUOTA} ]] && ! echo "${QUOTA}" | grep -qE "^([0-9]+(B|k|M|G|T)|0)\$" if [[ -n ${QUOTA} ]] && ! echo "${QUOTA}" | grep -qE "^([0-9]+(B|k|M|G|T)|0)\$"
then then
usage usage
_errex "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))" _exit_with_error "invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))"
fi fi
delquota "${USER}" delquota "${USER}"

View file

@ -16,18 +16,18 @@ PASSWD="${*}"
function usage { echo "Usage: updatemailuser <user@domain.tld> [password]" ; } function usage { echo "Usage: updatemailuser <user@domain.tld> [password]" ; }
[[ -z ${USER} ]] && { usage ; _errex "no username specified" ; } [[ -z ${USER} ]] && { usage ; _exit_with_error "no username specified" ; }
if [[ -z ${PASSWD} ]] if [[ -z ${PASSWD} ]]
then then
read -r -s -p "Enter Password: " PASSWD read -r -s -p "Enter Password: " PASSWD
echo echo
[[ -z ${PASSWD} ]] && _errex "Password must not be empty" [[ -z ${PASSWD} ]] && _exit_with_error "Password must not be empty"
fi fi
HASH="$(doveadm pw -s SHA512-CRYPT -u "${USER}" -p "${PASSWD}")" HASH="$(doveadm pw -s SHA512-CRYPT -u "${USER}" -p "${PASSWD}")"
touch "${DATABASE}" touch "${DATABASE}"
_create_lock # Protect config file with lock to avoid race conditions _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" grep -qi "^$(_escape "${USER}")|" "${DATABASE}" 2>/dev/null || _exit_with_error "User \"${USER}\" does not exist"
sed -i "s ^""${USER}""|.* ""${USER}""|""${HASH}"" " "${DATABASE}" sed -i "s ^""${USER}""|.* ""${USER}""|""${HASH}"" " "${DATABASE}"

View file

@ -1,12 +1,12 @@
#! /bin/bash #! /bin/bash
function _errex function _exit_with_error
{ {
if [[ -n ${1+set} ]] if [[ -n ${1+set} ]]
then then
_log 'error' "${1}" _log 'error' "${1}"
else else
_log 'error' "Call to '_errex' is missing a message to log" _log 'error' "Call to '_exit_with_error' is missing a message to log"
fi fi
_log 'error' 'Aborting' _log 'error' 'Aborting'

View file

@ -31,7 +31,7 @@ function _create_lock
function _remove_lock function _remove_lock
{ {
LOCK_FILE="${LOCK_FILE:-"/tmp/docker-mailserver/${SCRIPT_NAME}.lock"}" LOCK_FILE="${LOCK_FILE:-"/tmp/docker-mailserver/${SCRIPT_NAME}.lock"}"
[[ -z "${LOCK_ID}" ]] && _errex "Cannot remove ${LOCK_FILE} as there is no LOCK_ID set" [[ -z "${LOCK_ID}" ]] && _exit_with_error "Cannot remove ${LOCK_FILE} as there is no LOCK_ID set"
if [[ -e "${LOCK_FILE}" ]] && grep -q "${LOCK_ID}" "${LOCK_FILE}" # Ensure we don't delete a lock that's not ours if [[ -e "${LOCK_FILE}" ]] && grep -q "${LOCK_ID}" "${LOCK_FILE}" # Ensure we don't delete a lock that's not ours
then then
rm -f "${LOCK_FILE}" rm -f "${LOCK_FILE}"