change if style (#3361)

This commit is contained in:
Casper 2023-05-24 09:06:59 +02:00 committed by GitHub
parent 0e592aa911
commit cf74127f78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
58 changed files with 297 additions and 594 deletions

View file

@ -7,8 +7,7 @@ source /usr/local/bin/setup.d/getmail.sh
_setup_getmail
if [[ -d /var/lib/getmail ]]
then
if [[ -d /var/lib/getmail ]]; then
GETMAILDIR=/var/lib/getmail
else
mkdir -p /tmp/docker-mailserver/getmail

View file

@ -91,14 +91,12 @@ function _parse_options
function _maildel_request_if_missing
{
if [[ ${MAILDEL} -eq 0 ]]
then
if [[ ${MAILDEL} -eq 0 ]]; then
local MAILDEL_CHOSEN
read -r -p "Do you want to delete the mailbox as well (removing all mails)? [Y/n] " MAILDEL_CHOSEN
# TODO: Why would MAILDEL be set to true if MAILDEL_CHOSEN is empty?
if [[ ${MAILDEL_CHOSEN} =~ (y|Y|yes|Yes) ]] || [[ -z ${MAILDEL_CHOSEN} ]]
then
if [[ ${MAILDEL_CHOSEN} =~ (y|Y|yes|Yes) ]] || [[ -z ${MAILDEL_CHOSEN} ]]; then
MAILDEL=1
fi
fi

View file

@ -19,16 +19,14 @@ do
JAILS+=("${LIST}")
done
if [[ -z ${1} ]]
then
if [[ -z ${1} ]]; then
IPS_BANNED=0
for JAIL in "${JAILS[@]}"
do
BANNED_IPS=$(fail2ban-client status "${JAIL}" | grep -oP '(?<=Banned IP list:\s).+')
if [[ -n ${BANNED_IPS} ]]
then
if [[ -n ${BANNED_IPS} ]]; then
echo "Banned in ${JAIL}: ${BANNED_IPS}"
IPS_BANNED=1
fi
@ -43,11 +41,9 @@ else
( 'ban' )
shift
if [[ -n ${1} ]]
then
if [[ -n ${1} ]]; then
RESULT=$(fail2ban-client set custom banip "${@}")
if [[ ${RESULT} -gt 0 ]]
then
if [[ ${RESULT} -gt 0 ]]; then
echo "Banned custom IP: ${RESULT}"
else
_log 'error' "Banning '${*}' failed. Already banned?"
@ -61,8 +57,7 @@ else
( 'unban' )
shift
if [[ -n ${1} ]]
then
if [[ -n ${1} ]]; then
for JAIL in "${JAILS[@]}"
do

View file

@ -2,8 +2,7 @@
for FILE in /etc/getmailrc.d/getmailrc*
do
if ! pgrep -f "${FILE}$" &>/dev/null
then
if ! pgrep -f "${FILE}$" &>/dev/null; then
/usr/local/bin/getmail --getmaildir /var/lib/getmail --rcfile "${FILE}"
fi
done

View file

@ -71,12 +71,10 @@ function _quota_show_for
function _bytes_to_human_readable_size
{
# `-` represents a non-applicable value (eg: Like when `SIZE_LIMIT` is not set):
if [[ ${1:-} == '-' ]]
then
if [[ ${1:-} == '-' ]]; then
echo '~'
# Otherwise a value in KibiBytes (1024 bytes == 1k) is expected (Dovecots internal representation):
elif [[ ${1:-} =~ ^[0-9]+$ ]]
then
elif [[ ${1:-} =~ ^[0-9]+$ ]]; then
# kibibytes to bytes, converted to approproate IEC unit (eg: MiB):
echo $(( 1024 * ${1} )) | numfmt --to=iec
else
@ -105,8 +103,7 @@ function _alias_list_for_account
"${DATABASE_VIRTUAL}"
)
if grep --quiet --no-messages "${GREP_OPTIONS[@]}"
then
if grep --quiet --no-messages "${GREP_OPTIONS[@]}"; then
grep "${GREP_OPTIONS[@]}" | awk '{print $1;}' | sed ':a;N;$!ba;s/\n/, /g'
fi
}

View file

@ -3,8 +3,7 @@
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
if [[ -f /etc/dms-settings ]] && [[ $(_get_dms_env_value 'ENABLE_RSPAMD') -eq 1 ]]
then
if [[ -f /etc/dms-settings ]] && [[ $(_get_dms_env_value 'ENABLE_RSPAMD') -eq 1 ]]; then
/usr/local/bin/rspamd-dkim "${@}"
exit
fi
@ -63,8 +62,7 @@ while [[ ${#} -gt 0 ]]
do
case "${1}" in
( 'keysize' )
if [[ -n ${2+set} ]]
then
if [[ -n ${2+set} ]]; then
KEYSIZE="${2}"
shift
shift
@ -74,8 +72,7 @@ do
;;
( 'selector' )
if [[ -n ${2+set} ]]
then
if [[ -n ${2+set} ]]; then
# shellcheck disable=SC2034
SELECTOR="${2}"
shift
@ -86,8 +83,7 @@ do
;;
( 'domain' )
if [[ -n ${2+set} ]]
then
if [[ -n ${2+set} ]]; then
DOMAINS="${2}"
shift
shift
@ -112,8 +108,7 @@ function _generate_domains_config
# Generate the default vhost (equivalent to /etc/postfix/vhost),
# unless CLI arg DOMAINS provided an alternative list to use instead:
if [[ -z ${DOMAINS} ]]
then
if [[ -z ${DOMAINS} ]]; then
_obtain_hostname_and_domainname
# uses TMP_VHOST:
_vhost_collect_postfix_domains
@ -126,8 +121,7 @@ function _generate_domains_config
}
_generate_domains_config
if [[ ! -s ${DATABASE_VHOST} ]]
then
if [[ ! -s ${DATABASE_VHOST} ]]; then
_log 'warn' 'No entries found, no keys to make'
exit 0
fi
@ -136,8 +130,7 @@ while read -r DKIM_DOMAIN
do
mkdir -p "/tmp/docker-mailserver/opendkim/keys/${DKIM_DOMAIN}"
if [[ ! -f "/tmp/docker-mailserver/opendkim/keys/${DKIM_DOMAIN}/${SELECTOR}.private" ]]
then
if [[ ! -f "/tmp/docker-mailserver/opendkim/keys/${DKIM_DOMAIN}/${SELECTOR}.private" ]]; then
_log 'info' "Creating DKIM private key '/tmp/docker-mailserver/opendkim/keys/${DKIM_DOMAIN}/${SELECTOR}.private'"
opendkim-genkey \
@ -150,34 +143,29 @@ do
# write to KeyTable if necessary
KEYTABLEENTRY="${SELECTOR}._domainkey.${DKIM_DOMAIN} ${DKIM_DOMAIN}:${SELECTOR}:/etc/opendkim/keys/${DKIM_DOMAIN}/${SELECTOR}.private"
if [[ ! -f "/tmp/docker-mailserver/opendkim/KeyTable" ]]
then
if [[ ! -f "/tmp/docker-mailserver/opendkim/KeyTable" ]]; then
_log 'debug' 'Creating DKIM KeyTable'
echo "${KEYTABLEENTRY}" >/tmp/docker-mailserver/opendkim/KeyTable
else
if ! grep -q "${KEYTABLEENTRY}" "/tmp/docker-mailserver/opendkim/KeyTable"
then
if ! grep -q "${KEYTABLEENTRY}" "/tmp/docker-mailserver/opendkim/KeyTable"; then
echo "${KEYTABLEENTRY}" >>/tmp/docker-mailserver/opendkim/KeyTable
fi
fi
# write to SigningTable if necessary
SIGNINGTABLEENTRY="*@${DKIM_DOMAIN} ${SELECTOR}._domainkey.${DKIM_DOMAIN}"
if [[ ! -f /tmp/docker-mailserver/opendkim/SigningTable ]]
then
if [[ ! -f /tmp/docker-mailserver/opendkim/SigningTable ]]; then
_log 'debug' 'Creating DKIM SigningTable'
echo "*@${DKIM_DOMAIN} ${SELECTOR}._domainkey.${DKIM_DOMAIN}" >/tmp/docker-mailserver/opendkim/SigningTable
else
if ! grep -q "${SIGNINGTABLEENTRY}" /tmp/docker-mailserver/opendkim/SigningTable
then
if ! grep -q "${SIGNINGTABLEENTRY}" /tmp/docker-mailserver/opendkim/SigningTable; then
echo "${SIGNINGTABLEENTRY}" >>/tmp/docker-mailserver/opendkim/SigningTable
fi
fi
done < <(_get_valid_lines_from_file "${DATABASE_VHOST}")
# create TrustedHosts if missing
if [[ -d /tmp/docker-mailserver/opendkim ]] && [[ ! -f /tmp/docker-mailserver/opendkim/TrustedHosts ]]
then
if [[ -d /tmp/docker-mailserver/opendkim ]] && [[ ! -f /tmp/docker-mailserver/opendkim/TrustedHosts ]]; then
_log 'debug' 'Creating DKIM TrustedHosts'
echo "127.0.0.1" >/tmp/docker-mailserver/opendkim/TrustedHosts
echo "localhost" >>/tmp/docker-mailserver/opendkim/TrustedHosts

View file

@ -12,8 +12,7 @@ SENDER=${3}
# The case that the mail.log.1 file isn't readable shouldn't
# actually be possible with logrotate not rotating empty files..
# But you never know!
if [[ -r "/var/log/mail/mail.log.1" ]]
then
if [[ -r "/var/log/mail/mail.log.1" ]]; then
BODY=$(/usr/sbin/pflogsumm /var/log/mail/mail.log.1 --problems-first)
else
BODY="Error: Mail log not readable or not found: /var/log/mail/mail.log.1

View file

@ -10,16 +10,14 @@ USER=${3:-}
function __usage { _log 'info' "Usage: ${0} <add|del|list> <send|receive> [<email@domain.com>]" ; }
if [[ ${DIRECTION} =~ ^(send|receive)$ ]]
then
if [[ ${DIRECTION} =~ ^(send|receive)$ ]]; then
DATABASE="/tmp/docker-mailserver/postfix-${DIRECTION}-access.cf"
else
__usage
_exit_with_error "Unknown or missing second parameter '${DIRECTION}' - specify 'send' or 'receive'"
fi
if [[ -z ${USER} ]] && [[ ${COMMAND} != list ]]
then
if [[ -z ${USER} ]] && [[ ${COMMAND} != list ]]; then
read -r -p 'Provide a username: ' USER
[[ -z ${USER} ]] && _exit_with_error 'User must not be empty'
fi
@ -27,15 +25,13 @@ fi
case "${COMMAND}" in
( 'add' )
if [[ -f ${DATABASE} ]] && grep -q -F "${USER}" "${DATABASE}"
then
if [[ -f ${DATABASE} ]] && grep -q -F "${USER}" "${DATABASE}"; then
_exit_with_error "User '${USER}' already denied to ${DIRECTION} mails"
fi
echo -e "${USER} \t\t REJECT" >>"${DATABASE}"
if [[ ${DIRECTION} == 'send' ]]
then
if [[ ${DIRECTION} == 'send' ]]; then
CHECK='check_sender_access'
POSTFIX_OPTION='smtpd_sender_restrictions'
else
@ -45,16 +41,14 @@ case "${COMMAND}" in
# only adjust Postfix's `main.cf` if we haven't adjusted it before
STRING_TO_BE_ADDED="${CHECK} texthash:/tmp/docker-mailserver/postfix-${DIRECTION}-access.cf"
if ! grep -q "${STRING_TO_BE_ADDED}" /etc/postfix/main.cf
then
if ! grep -q "${STRING_TO_BE_ADDED}" /etc/postfix/main.cf; then
sed -i -E "s|^(${POSTFIX_OPTION} =)(.*)|\1 ${STRING_TO_BE_ADDED},\2|" /etc/postfix/main.cf
_reload_postfix
fi
;;
( 'del' )
if ! sed -i "/^$(_escape "${USER}").*/d" "${DATABASE}" 2>/dev/null
then
if ! sed -i "/^$(_escape "${USER}").*/d" "${DATABASE}" 2>/dev/null; then
_exit_with_error "User '${USER}' not found"
fi
;;

View file

@ -88,8 +88,7 @@ function _parse_arguments
( 'keytype' )
[[ -n ${2:-} ]] || _exit_with_error "No keytype provided after 'keytype' argument"
if [[ ${2} == 'rsa' ]] || [[ ${2} == 'ed25519' ]]
then
if [[ ${2} == 'rsa' ]] || [[ ${2} == 'ed25519' ]]; then
KEYTYPE=${2}
_log 'debug' "Keytype set to '${KEYTYPE}'"
else
@ -146,8 +145,7 @@ function _parse_arguments
shift 2
done
if [[ ${KEYTYPE} == 'ed25519' ]] && [[ ${KEYSIZE} -ne 2048 ]]
then
if [[ ${KEYTYPE} == 'ed25519' ]] && [[ ${KEYSIZE} -ne 2048 ]]; then
_exit_with_error "Chosen keytype does not accept the 'keysize' argument"
fi
@ -160,8 +158,7 @@ function _create_keys
# in other functions (after this function was called).
BASE_DIR='/tmp/docker-mailserver/rspamd/dkim'
if [[ ${KEYTYPE} == 'rsa' ]]
then
if [[ ${KEYTYPE} == 'rsa' ]]; then
local BASE_FILE_NAME="${BASE_DIR}/${KEYTYPE}-${KEYSIZE}-${SELECTOR}-${DOMAIN}"
KEYTYPE_OPTIONS=('-b' "${KEYSIZE}")
_log 'info' "Creating DKIM keys of type '${KEYTYPE}' and lenght '${KEYSIZE}' with selector '${SELECTOR}' for domain '${DOMAIN}'"
@ -198,11 +195,9 @@ function _create_keys
function _check_permissions
{
# shellcheck disable=SC2310
if ! __do_as_rspamd_user ls "${BASE_DIR}" >/dev/null
then
if ! __do_as_rspamd_user ls "${BASE_DIR}" >/dev/null; then
_log 'warn' "The Rspamd user ('_rspamd') seems to be unable to list files in the keys directory ('${BASE_DIR}') - Rspamd may experience permission errors later"
elif ! __do_as_rspamd_user cat "${PRIVATE_KEY_FILE}" >/dev/null
then
elif ! __do_as_rspamd_user cat "${PRIVATE_KEY_FILE}" >/dev/null; then
_log 'warn' "The Rspamd user ('_rspamd') seems to be unable to read the private key file - Rspamd may experience permission errors later"
else
_log 'debug' 'Permissions on files and directories seem ok'
@ -212,8 +207,7 @@ function _check_permissions
function _setup_default_signing_conf
{
local DEFAULT_CONFIG_FILE='/etc/rspamd/override.d/dkim_signing.conf'
if [[ -f ${DEFAULT_CONFIG_FILE} ]]
then
if [[ -f ${DEFAULT_CONFIG_FILE} ]]; then
_log 'debug' "'${DEFAULT_CONFIG_FILE}' exists, not supplying a default"
else
_log 'info' "Supplying a default configuration ('${DEFAULT_CONFIG_FILE}')"
@ -250,8 +244,7 @@ function _transform_public_key_file_to_dns_record_contents
grep -o '".*"' "${PUBLIC_KEY_FILE}" | tr -d '"\n' >>"${PUBLIC_KEY_DNS_FILE}"
echo '' >>"${PUBLIC_KEY_DNS_FILE}"
if ! _log_level_is '(warn|error)'
then
if ! _log_level_is '(warn|error)'; then
_log 'info' "Here is the content of the TXT DNS record ${SELECTOR}._domainkey.${DOMAIN} that you need to create:\n"
cat "${PUBLIC_KEY_DNS_FILE}"
printf '\n'
@ -261,8 +254,7 @@ function _transform_public_key_file_to_dns_record_contents
function _final_steps
{
# We need to restart Rspamd so the changes take effect immediately.
if ! supervisorctl restart rspamd
then
if ! supervisorctl restart rspamd; then
_log 'warn' 'Could not restart Rspamd via Supervisord'
fi

View file

@ -19,16 +19,14 @@ function __usage { echo "Usage: ${0} -i <replace/delete operation> <file>" ; }
HASHTOOL='sha1sum'
SKIP_ERROR=0
if [[ ${#} -lt 3 ]]
then
if [[ ${#} -lt 3 ]]; then
_log 'error' 'At least three parameters must be given'
__usage
exit 1
fi >&2
[[ -f /CONTAINER_START ]] && SKIP_ERROR=1 # hide error if container was restarted
if [[ ${1} == '--strict' ]] # show error every time
then
if [[ ${1} == '--strict' ]]; then # show error every time
SKIP_ERROR=0
shift
fi
@ -41,8 +39,7 @@ sed "${@}"
NEW=$(${HASHTOOL} "${FILE}")
# fail if file was not modified
if [[ ${OLD} == "${NEW}" ]] && [[ ${SKIP_ERROR} -eq 0 ]]
then
if [[ ${OLD} == "${NEW}" ]] && [[ ${SKIP_ERROR} -eq 0 ]]; then
_log 'error' "No difference after call to 'sed' in 'sedfile' (sed ${*})" >&2
exit 1
fi

View file

@ -56,8 +56,7 @@ function _validate_parameters
function _quota_request_if_missing
{
if [[ -z ${QUOTA} ]]
then
if [[ -z ${QUOTA} ]]; then
read -r -p 'Enter quota (e.g. 10M): ' QUOTA
echo
[[ -z "${QUOTA}" ]] && _exit_with_error 'Quota must not be empty (use 0 for unlimited quota)'
@ -66,8 +65,7 @@ function _quota_request_if_missing
function _quota_unit_is_valid
{
if ! grep -qE "^([0-9]+(B|k|M|G|T)|0)\$" <<< "${QUOTA}"
then
if ! grep -qE "^([0-9]+(B|k|M|G|T)|0)\$" <<< "${QUOTA}"; then
__usage
_exit_with_error 'Invalid quota format. e.g. 302M (B (byte), k (kilobyte), M (megabyte), G (gigabyte) or T (terabyte))'
fi

View file

@ -155,8 +155,7 @@ function _main
( show-mail-logs ) cat /var/log/mail/mail.log ;;
( login )
shift 2
if [[ -z ${1:-} ]]
then
if [[ -z ${1:-} ]]; then
/bin/bash
else
/bin/bash -c "${@}"
@ -171,8 +170,7 @@ function _main
esac
}
if [[ -z ${1:-} ]]
then
if [[ -z ${1:-} ]]; then
_usage
else
_main "${@}"