From 37ca0f9ba99c6e651d43862373a0e3921fc99661 Mon Sep 17 00:00:00 2001 From: Casper Date: Fri, 26 May 2023 01:01:41 +0200 Subject: [PATCH] Change 'function' style (#3364) --- setup.sh | 18 ++++------ target/bin/addalias | 6 ++-- target/bin/adddovecotmasteruser | 6 ++-- target/bin/addmailuser | 6 ++-- target/bin/addrelayhost | 12 +++---- target/bin/addsaslpassword | 12 +++---- target/bin/delalias | 6 ++-- target/bin/deldovecotmasteruser | 6 ++-- target/bin/delmailuser | 15 +++----- target/bin/delquota | 9 ++--- target/bin/excluderelaydomain | 9 ++--- target/bin/fail2ban | 3 +- target/bin/listalias | 6 ++-- target/bin/listdovecotmasteruser | 6 ++-- target/bin/listmailuser | 18 ++++------ target/bin/open-dkim | 6 ++-- target/bin/rspamd-dkim | 24 +++++-------- target/bin/setquota | 15 +++----- target/bin/setup | 9 ++--- target/bin/updatedovecotmasteruser | 6 ++-- target/bin/updatemailuser | 6 ++-- target/scripts/build/packages.sh | 27 +++++--------- target/scripts/check-for-changes.sh | 15 +++----- target/scripts/helpers/accounts.sh | 9 ++--- target/scripts/helpers/aliases.sh | 12 +++---- target/scripts/helpers/change-detection.sh | 6 ++-- target/scripts/helpers/database/db.sh | 21 ++++------- .../helpers/database/manage/dovecot-quotas.sh | 3 +- .../database/manage/postfix-accounts.sh | 18 ++++------ .../database/manage/postfix-virtual.sh | 3 +- target/scripts/helpers/dns.sh | 6 ++-- target/scripts/helpers/error.sh | 15 +++----- target/scripts/helpers/index.sh | 3 +- target/scripts/helpers/lock.sh | 6 ++-- target/scripts/helpers/log.sh | 12 +++---- target/scripts/helpers/network.sh | 6 ++-- target/scripts/helpers/postfix.sh | 12 +++---- target/scripts/helpers/relay.sh | 21 ++++------- target/scripts/helpers/ssl.sh | 27 +++++--------- target/scripts/helpers/utils.sh | 30 ++++++---------- target/scripts/start-mailserver.sh | 3 +- target/scripts/startup/check-stack.sh | 15 +++----- target/scripts/startup/daemons-stack.sh | 18 ++++------ target/scripts/startup/setup-stack.sh | 18 ++++------ .../scripts/startup/setup.d/dmarc_dkim_spf.sh | 9 ++--- target/scripts/startup/setup.d/dovecot.sh | 24 +++++-------- target/scripts/startup/setup.d/fetchmail.sh | 9 ++--- target/scripts/startup/setup.d/getmail.sh | 3 +- target/scripts/startup/setup.d/ldap.sh | 3 +- target/scripts/startup/setup.d/log.sh | 12 +++---- target/scripts/startup/setup.d/mail_state.sh | 3 +- target/scripts/startup/setup.d/networking.sh | 12 +++---- target/scripts/startup/setup.d/postfix.sh | 15 +++----- target/scripts/startup/setup.d/saslauthd.sh | 3 +- .../scripts/startup/setup.d/security/misc.sh | 24 +++++-------- .../startup/setup.d/security/rspamd.sh | 36 +++++++------------ .../startup/setup.d/security/spoofing.sh | 3 +- target/scripts/startup/variables-stack.sh | 18 ++++------ test/helper/common.bash | 3 +- test/linting/lint.sh | 12 +++---- 60 files changed, 233 insertions(+), 466 deletions(-) diff --git a/setup.sh b/setup.sh index b517d8bb..71739686 100755 --- a/setup.sh +++ b/setup.sh @@ -27,8 +27,7 @@ RESET=$(echo -ne '\e[0m') set -euEo pipefail shopt -s inherit_errexit 2>/dev/null || true -function _show_local_usage -{ +function _show_local_usage() { # shellcheck disable=SC2059 printf '%s' "${ORANGE}OPTIONS${RESET} ${LBLUE}Config path, container or image adjustments${RESET} @@ -69,8 +68,7 @@ function _show_local_usage " } -function _get_absolute_script_directory -{ +function _get_absolute_script_directory() { if dirname "$(readlink -f "${0}")" &>/dev/null; then DIR=$(dirname "$(readlink -f "${0}")") elif realpath -e -L "${0}" &>/dev/null; then @@ -79,8 +77,7 @@ function _get_absolute_script_directory fi } -function _set_default_config_path -{ +function _set_default_config_path() { if [[ -d "${DIR}/config" ]]; then # legacy path (pre v10.2.0) DEFAULT_CONFIG_PATH="${DIR}/config" @@ -89,8 +86,7 @@ function _set_default_config_path fi } -function _handle_config_path -{ +function _handle_config_path() { if [[ -z ${DESIRED_CONFIG_PATH} ]]; then # no desired config path if [[ -n ${CONTAINER_NAME} ]]; then @@ -111,8 +107,7 @@ function _handle_config_path fi } -function _run_in_new_container -{ +function _run_in_new_container() { # start temporary container with specified image if ! ${CRI} history -q "${IMAGE_NAME}" &>/dev/null; then echo "Image '${IMAGE_NAME}' not found. Pulling ..." @@ -124,8 +119,7 @@ function _run_in_new_container "${IMAGE_NAME}" "${@}" } -function _main -{ +function _main() { _get_absolute_script_directory _set_default_config_path diff --git a/target/bin/addalias b/target/bin/addalias index a58bd7f8..60f8aa03 100755 --- a/target/bin/addalias +++ b/target/bin/addalias @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 2 "${@}" local MAIL_ALIAS="${1}" @@ -14,8 +13,7 @@ function _main || _exit_with_error "'${MAIL_ALIAS}' is already an alias for recipient: '${RECIPIENT}'" } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}addalias${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} diff --git a/target/bin/adddovecotmasteruser b/target/bin/adddovecotmasteruser index 79bc51ad..41041db2 100755 --- a/target/bin/adddovecotmasteruser +++ b/target/bin/adddovecotmasteruser @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" @@ -14,8 +13,7 @@ function _main _manage_accounts_dovecotmaster_create "${MAIL_ACCOUNT}" "${PASSWD}" } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}adddovecotmasteruser${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} diff --git a/target/bin/addmailuser b/target/bin/addmailuser index 2b6f3556..15691c89 100755 --- a/target/bin/addmailuser +++ b/target/bin/addmailuser @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" @@ -18,8 +17,7 @@ function _main # where the actual account is created in Dovecot. Expect a delay. } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}addmailuser${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} diff --git a/target/bin/addrelayhost b/target/bin/addrelayhost index 5463f15e..b8ac2836 100755 --- a/target/bin/addrelayhost +++ b/target/bin/addrelayhost @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 2 "${@}" local DOMAIN="${1}" @@ -15,8 +14,7 @@ function _main _add_relayhost } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}addrelayhost${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} @@ -47,8 +45,7 @@ ${ORANGE}EXIT STATUS${RESET} " } -function _validate_parameters -{ +function _validate_parameters() { [[ -z ${DOMAIN} ]] && { __usage ; _exit_with_error 'No domain specified' ; } [[ -z ${HOST} ]] && { __usage ; _exit_with_error 'No relay host specified' ; } [[ -z ${PORT} ]] && PORT=25 @@ -56,8 +53,7 @@ function _validate_parameters # Config is for sender dependent relay-host mapping, # current support restricts senders to domain scope (port is also enforced). -function _add_relayhost -{ +function _add_relayhost() { local SENDER="@${DOMAIN}" local RELAY_HOST_ENTRY="[${HOST}]:${PORT}" local DATABASE_RELAY='/tmp/docker-mailserver/postfix-relaymap.cf' diff --git a/target/bin/addsaslpassword b/target/bin/addsaslpassword index 27dd67cd..9461c112 100755 --- a/target/bin/addsaslpassword +++ b/target/bin/addsaslpassword @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 2 "${@}" local DOMAIN="${1}" @@ -16,8 +15,7 @@ function _main _add_relayhost_credentials } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}addsaslpassword${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} @@ -46,8 +44,7 @@ ${ORANGE}EXIT STATUS${RESET} " } -function _validate_parameters -{ +function _validate_parameters() { [[ -z ${DOMAIN} ]] && { __usage ; _exit_with_error 'No domain specified' ; } [[ -z ${RELAY_ACCOUNT} ]] && { __usage ; _exit_with_error 'No relay account specified' ; } _password_request_if_missing @@ -58,8 +55,7 @@ function _validate_parameters # # NOTE: This command does not support providing a relay-host # as the lookup key, it only supports a lookup via sender domain. -function _add_relayhost_credentials -{ +function _add_relayhost_credentials() { local SENDER="@${DOMAIN}" local RELAY_HOST_ENTRY_AUTH="${RELAY_ACCOUNT}:${PASSWD}" local DATABASE_PASSWD='/tmp/docker-mailserver/postfix-sasl-password.cf' diff --git a/target/bin/delalias b/target/bin/delalias index c8b27745..c3e57545 100755 --- a/target/bin/delalias +++ b/target/bin/delalias @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 2 "${@}" local MAIL_ALIAS="${1}" @@ -13,8 +12,7 @@ function _main _manage_virtual_aliases_delete "${MAIL_ALIAS}" "${RECIPIENT}" } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}delalias${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} diff --git a/target/bin/deldovecotmasteruser b/target/bin/deldovecotmasteruser index efd4e781..2ef20276 100755 --- a/target/bin/deldovecotmasteruser +++ b/target/bin/deldovecotmasteruser @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 1 "${@}" # Actual command to perform: @@ -15,8 +14,7 @@ function _main done } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}deldovecotmasteruser${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} diff --git a/target/bin/delmailuser b/target/bin/delmailuser index 117a054c..974a2375 100755 --- a/target/bin/delmailuser +++ b/target/bin/delmailuser @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 1 "${@}" # Tests expect early exit without error if no DB exists: @@ -37,8 +36,7 @@ function _main done } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}delmailuser${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} @@ -71,8 +69,7 @@ ${ORANGE}EXIT STATUS${RESET} " } -function _parse_options -{ +function _parse_options() { while getopts ":yY" OPT do case "${OPT}" in @@ -89,8 +86,7 @@ function _parse_options done } -function _maildel_request_if_missing -{ +function _maildel_request_if_missing() { 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 @@ -102,8 +98,7 @@ function _maildel_request_if_missing fi } -function _remove_maildir -{ +function _remove_maildir() { local MAIL_ACCOUNT=${1} local LOCAL_PART="${MAIL_ACCOUNT%@*}" diff --git a/target/bin/delquota b/target/bin/delquota index 1282a827..de3759c8 100755 --- a/target/bin/delquota +++ b/target/bin/delquota @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" @@ -13,8 +12,7 @@ function _main _manage_dovecot_quota_delete "${MAIL_ACCOUNT}" } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}delquota${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} @@ -37,8 +35,7 @@ ${ORANGE}EXIT STATUS${RESET} " } -function _validate_parameters -{ +function _validate_parameters() { _arg_expect_mail_account _account_should_already_exist } diff --git a/target/bin/excluderelaydomain b/target/bin/excluderelaydomain index d6bdc2a9..311ab433 100755 --- a/target/bin/excluderelaydomain +++ b/target/bin/excluderelaydomain @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 1 "${@}" local DOMAIN="${1}" @@ -12,8 +11,7 @@ function _main _exclude_domain_from_relayhosts } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}excluderelayhost${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} @@ -44,8 +42,7 @@ ${ORANGE}EXIT STATUS${RESET} # Config is for sender dependent relay-host mapping, # excludes appending a sender from the real generated mapping in `helpers/relay.sh`. -function _exclude_domain_from_relayhosts -{ +function _exclude_domain_from_relayhosts() { local SENDER="@${DOMAIN}" local DATABASE_RELAY='/tmp/docker-mailserver/postfix-relaymap.cf' diff --git a/target/bin/fail2ban b/target/bin/fail2ban index 1aa2c1da..71dbd3c0 100755 --- a/target/bin/fail2ban +++ b/target/bin/fail2ban @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function __usage -{ +function __usage() { echo "Usage: ./setup.sh fail2ban [ ]" echo " ./setup.sh fail2ban log" } diff --git a/target/bin/listalias b/target/bin/listalias index 69779407..efb48fd2 100755 --- a/target/bin/listalias +++ b/target/bin/listalias @@ -3,14 +3,12 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { local DATABASE_VIRTUAL='/tmp/docker-mailserver/postfix-virtual.cf' _list_entries "${DATABASE_VIRTUAL}" } -function _list_entries -{ +function _list_entries() { local DATABASE=${1} _db_should_exist_with_content "${DATABASE}" diff --git a/target/bin/listdovecotmasteruser b/target/bin/listdovecotmasteruser index 1f6f6123..214befd3 100755 --- a/target/bin/listdovecotmasteruser +++ b/target/bin/listdovecotmasteruser @@ -3,14 +3,12 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { local DATABASE_DOVECOT_MASTERS='/tmp/docker-mailserver/dovecot-masters.cf' _list_entries "${DATABASE_DOVECOT_MASTERS}" } -function _list_entries -{ +function _list_entries() { local DATABASE=${1} _db_should_exist_with_content "${DATABASE}" diff --git a/target/bin/listmailuser b/target/bin/listmailuser index 8f5561d7..c1ab9d8e 100755 --- a/target/bin/listmailuser +++ b/target/bin/listmailuser @@ -7,16 +7,14 @@ source /usr/local/bin/helpers/index.sh # shellcheck source=/dev/null source /etc/dms-settings 2>/dev/null -function _main -{ +function _main() { local DATABASE_ACCOUNTS='/tmp/docker-mailserver/postfix-accounts.cf' local DATABASE_VIRTUAL='/tmp/docker-mailserver/postfix-virtual.cf' _list_entries "${DATABASE_ACCOUNTS}" } -function _list_entries -{ +function _list_entries() { local DATABASE=${1} _db_should_exist_with_content "${DATABASE}" @@ -29,8 +27,7 @@ function _list_entries done < <(_get_valid_lines_from_file "${DATABASE}") } -function _format_list_item -{ +function _format_list_item() { local LINE=${1} local MAIL_ACCOUNT @@ -49,8 +46,7 @@ function _format_list_item echo "${ACCOUNT_ENTRY}" } -function _quota_show_for -{ +function _quota_show_for() { local MAIL_ACCOUNT=${1} [[ ${ENABLE_QUOTAS} -ne 1 ]] && return 0 @@ -68,8 +64,7 @@ function _quota_show_for echo "( ${CURRENT_SIZE} / ${SIZE_LIMIT} ) [${PERCENT_USED}]" } -function _bytes_to_human_readable_size -{ +function _bytes_to_human_readable_size() { # `-` represents a non-applicable value (eg: Like when `SIZE_LIMIT` is not set): if [[ ${1:-} == '-' ]]; then echo '~' @@ -83,8 +78,7 @@ function _bytes_to_human_readable_size } # Returns a comma delimited list of aliases associated to a recipient (ideally the recipient is a mail account): -function _alias_list_for_account -{ +function _alias_list_for_account() { local GREP_OPTIONS local MAIL_ACCOUNT=${1} diff --git a/target/bin/open-dkim b/target/bin/open-dkim index 995d7359..c3e65678 100755 --- a/target/bin/open-dkim +++ b/target/bin/open-dkim @@ -12,8 +12,7 @@ KEYSIZE=4096 SELECTOR=mail DOMAINS= -function __usage -{ +function __usage() { printf '%s' "${PURPLE}OPEN-DKIM${RED}(${YELLOW}8${RED}) ${ORANGE}NAME${RESET} @@ -102,8 +101,7 @@ done DATABASE_VHOST='/tmp/vhost.dkim' # Prepare a file with one domain per line: -function _generate_domains_config -{ +function _generate_domains_config() { local TMP_VHOST='/tmp/vhost.dkim.tmp' # Generate the default vhost (equivalent to /etc/postfix/vhost), diff --git a/target/bin/rspamd-dkim b/target/bin/rspamd-dkim index 8093dd39..e4250d8b 100755 --- a/target/bin/rspamd-dkim +++ b/target/bin/rspamd-dkim @@ -11,8 +11,7 @@ shopt -s inherit_errexit # shellcheck source=/dev/null source /etc/dms-settings -function __usage -{ +function __usage() { _log 'trace' 'Showing usage message now' echo -e "${PURPLE}RSPAMD-DKIM${RED}(${YELLOW}8${RED}) @@ -65,16 +64,14 @@ ${ORANGE}EXIT STATUS${RESET} " } -function __do_as_rspamd_user -{ +function __do_as_rspamd_user() { local COMMAND=${1:?Command required when using __do_as_rspamd_user} _log 'trace' "Running '${*}' as user '_rspamd' now" shift 1 su -l '_rspamd' -s "$(command -v "${COMMAND}")" -- "${@}" } -function _parse_arguments -{ +function _parse_arguments() { KEYTYPE='rsa' KEYSIZE='2048' SELECTOR='mail' @@ -152,8 +149,7 @@ function _parse_arguments return 0 } -function _create_keys -{ +function _create_keys() { # Note: Variables not marked with `local` are used # in other functions (after this function was called). BASE_DIR='/tmp/docker-mailserver/rspamd/dkim' @@ -192,8 +188,7 @@ function _create_keys fi } -function _check_permissions -{ +function _check_permissions() { # shellcheck disable=SC2310 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" @@ -204,8 +199,7 @@ function _check_permissions fi } -function _setup_default_signing_conf -{ +function _setup_default_signing_conf() { local DEFAULT_CONFIG_FILE='/etc/rspamd/override.d/dkim_signing.conf' if [[ -f ${DEFAULT_CONFIG_FILE} ]]; then _log 'debug' "'${DEFAULT_CONFIG_FILE}' exists, not supplying a default" @@ -237,8 +231,7 @@ EOF fi } -function _transform_public_key_file_to_dns_record_contents -{ +function _transform_public_key_file_to_dns_record_contents() { _log 'trace' 'Transforming DNS zone format to DNS record content now' : >"${PUBLIC_KEY_DNS_FILE}" grep -o '".*"' "${PUBLIC_KEY_FILE}" | tr -d '"\n' >>"${PUBLIC_KEY_DNS_FILE}" @@ -251,8 +244,7 @@ function _transform_public_key_file_to_dns_record_contents fi } -function _final_steps -{ +function _final_steps() { # We need to restart Rspamd so the changes take effect immediately. if ! supervisorctl restart rspamd; then _log 'warn' 'Could not restart Rspamd via Supervisord' diff --git a/target/bin/setquota b/target/bin/setquota index 9c0bf06e..5b2bba41 100755 --- a/target/bin/setquota +++ b/target/bin/setquota @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" @@ -15,8 +14,7 @@ function _main _manage_dovecot_quota_update "${MAIL_ACCOUNT}" "${QUOTA}" } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}setquota${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} @@ -43,8 +41,7 @@ ${ORANGE}EXIT STATUS${RESET} " } -function _validate_parameters -{ +function _validate_parameters() { # MAIL_ACCOUNT _arg_expect_mail_account _account_should_already_exist @@ -54,8 +51,7 @@ function _validate_parameters _quota_unit_is_valid } -function _quota_request_if_missing -{ +function _quota_request_if_missing() { if [[ -z ${QUOTA} ]]; then read -r -p 'Enter quota (e.g. 10M): ' QUOTA echo @@ -63,8 +59,7 @@ function _quota_request_if_missing fi } -function _quota_unit_is_valid -{ +function _quota_unit_is_valid() { 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))' diff --git a/target/bin/setup b/target/bin/setup index 49546f70..29782b6f 100755 --- a/target/bin/setup +++ b/target/bin/setup @@ -5,8 +5,7 @@ set -euE -o pipefail # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _usage -{ +function _usage() { # shellcheck disable=SC2059 printf '%s' "${PURPLE}SETUP${RED}(${YELLOW}1${RED}) @@ -82,15 +81,13 @@ ${ORANGE}EXAMPLES${RESET} " } -function _invalid_command -{ +function _invalid_command() { echo "The command '${*}' is invalid. Use \`setup help\` to get an overview of all commands." >&2 exit 2 } -function _main -{ +function _main() { case ${1:-} in ( email ) diff --git a/target/bin/updatedovecotmasteruser b/target/bin/updatedovecotmasteruser index bc89f882..eeff21a5 100755 --- a/target/bin/updatedovecotmasteruser +++ b/target/bin/updatedovecotmasteruser @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" @@ -14,8 +13,7 @@ function _main _manage_accounts_dovecotmaster_update "${MAIL_ACCOUNT}" "${PASSWD}" } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}updatedovecotmasteruser${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} diff --git a/target/bin/updatemailuser b/target/bin/updatemailuser index 5d2f8012..ce45533e 100755 --- a/target/bin/updatemailuser +++ b/target/bin/updatemailuser @@ -3,8 +3,7 @@ # shellcheck source=../scripts/helpers/index.sh source /usr/local/bin/helpers/index.sh -function _main -{ +function _main() { _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" @@ -14,8 +13,7 @@ function _main _manage_accounts_update "${MAIL_ACCOUNT}" "${PASSWD}" } -function __usage -{ +function __usage() { printf '%s' "${PURPLE}updatemailuser${RED}(${YELLOW}8${RED}) ${ORANGE}USAGE${RESET} diff --git a/target/scripts/build/packages.sh b/target/scripts/build/packages.sh index 8258066e..9f863899 100644 --- a/target/scripts/build/packages.sh +++ b/target/scripts/build/packages.sh @@ -10,8 +10,7 @@ source /usr/local/bin/helpers/log.sh _log_level_is 'trace' && QUIET='-y' || QUIET='-qq' -function _pre_installation_steps -{ +function _pre_installation_steps() { _log 'info' 'Starting package installation' _log 'debug' 'Running pre-installation steps' @@ -25,8 +24,7 @@ function _pre_installation_steps apt-get "${QUIET}" upgrade } -function _install_postfix -{ +function _install_postfix() { _log 'debug' 'Installing Postfix' _log 'warn' 'Applying workaround for Postfix bug (see https://github.com//issues/2023#issuecomment-855326403)' @@ -42,8 +40,7 @@ function _install_postfix rm /etc/rsyslog.d/postfix.conf } -function _install_packages -{ +function _install_packages() { _log 'debug' 'Installing all packages now' declare -a ANTI_VIRUS_SPAM_PACKAGES @@ -93,8 +90,7 @@ function _install_packages "${MAIL_PROGRAMS_PACKAGES[@]}" } -function _install_dovecot -{ +function _install_dovecot() { declare -a DOVECOT_PACKAGES DOVECOT_PACKAGES=( @@ -131,8 +127,7 @@ function _install_dovecot apt-get "${QUIET}" --no-install-recommends install "${DOVECOT_PACKAGES[@]}" } -function _install_rspamd -{ +function _install_rspamd() { _log 'trace' 'Adding Rspamd package signatures' local DEB_FILE='/etc/apt/sources.list.d/rspamd.list' local RSPAMD_PACKAGE_NAME @@ -160,8 +155,7 @@ function _install_rspamd apt-get "${QUIET}" --no-install-recommends install "${RSPAMD_PACKAGE_NAME}" 'redis-server' } -function _install_fail2ban -{ +function _install_fail2ban() { local FAIL2BAN_DEB_URL='https://github.com/fail2ban/fail2ban/releases/download/1.0.2/fail2ban_1.0.2-1.upstream1_all.deb' local FAIL2BAN_DEB_ASC_URL="${FAIL2BAN_DEB_URL}.asc" local FAIL2BAN_GPG_FINGERPRINT='8738 559E 26F6 71DF 9E2C 6D9E 683B F1BE BD0A 882C' @@ -201,8 +195,7 @@ function _install_fail2ban # v6.18 contains fixes for Google and Microsoft OAuth support. # using pip to install getmail. # TODO This can be removed when the base image is updated to Debian 12 (Bookworm) -function _install_getmail -{ +function _install_getmail() { _log 'debug' 'Installing getmail6' apt-get "${QUIET}" --no-install-recommends install python3-pip pip3 install --no-cache-dir 'getmail6~=6.18.12' @@ -212,8 +205,7 @@ function _install_getmail apt-get "${QUIET}" autoremove } -function _remove_data_after_package_installations -{ +function _remove_data_after_package_installations() { _log 'debug' 'Deleting sensitive files (secrets)' rm /etc/postsrsd.secret @@ -221,8 +213,7 @@ function _remove_data_after_package_installations rm /etc/cron.daily/00logwatch } -function _post_installation_steps -{ +function _post_installation_steps() { _log 'debug' 'Running post-installation steps (cleanup)' apt-get "${QUIET}" clean rm -rf /var/lib/apt/lists/* diff --git a/target/scripts/check-for-changes.sh b/target/scripts/check-for-changes.sh index cfcc78d9..58f0d96f 100755 --- a/target/scripts/check-for-changes.sh +++ b/target/scripts/check-for-changes.sh @@ -30,8 +30,7 @@ _log_with_date 'trace' "Using postmaster address '${POSTMASTER_ADDRESS}'" _log_with_date 'debug' "Changedetector is ready" -function _check_for_changes -{ +function _check_for_changes() { # get chksum and check it, no need to lock config yet _monitored_files_checksums >"${CHKSUM_FILE}.new" cmp --silent -- "${CHKSUM_FILE}" "${CHKSUM_FILE}.new" @@ -65,8 +64,7 @@ function _check_for_changes fi } -function _get_changed_files -{ +function _get_changed_files() { local CHKSUM_CURRENT=${1} local CHKSUM_NEW=${2} @@ -81,8 +79,7 @@ function _get_changed_files grep -Fxvf "${CHKSUM_CURRENT}" "${CHKSUM_NEW}" | sed -r 's/^\S+[[:space:]]+//' } -function _reload_amavis -{ +function _reload_amavis() { if [[ ${CHANGED} =~ ${DMS_DIR}/postfix-accounts.cf ]] || [[ ${CHANGED} =~ ${DMS_DIR}/postfix-virtual.cf ]]; then # /etc/postfix/vhost was updated, amavis must refresh it's config by # reading this file again in case of new domains, otherwise they will be ignored. @@ -92,8 +89,7 @@ function _reload_amavis # Also note that changes are performed in place and are not atomic # We should fix that and write to temporary files, stop, swap and start -function _postfix_dovecot_changes -{ +function _postfix_dovecot_changes() { local DMS_DIR=/tmp/docker-mailserver # Regenerate accounts via `helpers/accounts.sh`: @@ -141,8 +137,7 @@ function _postfix_dovecot_changes _chown_var_mail_if_necessary } -function _ssl_changes -{ +function _ssl_changes() { local REGEX_NEVER_MATCH='(?\!)' # _setup_ssl is required for: diff --git a/target/scripts/helpers/accounts.sh b/target/scripts/helpers/accounts.sh index 397290a3..3637ff4f 100644 --- a/target/scripts/helpers/accounts.sh +++ b/target/scripts/helpers/accounts.sh @@ -9,8 +9,7 @@ DOVECOT_USERDB_FILE=/etc/dovecot/userdb DOVECOT_MASTERDB_FILE=/etc/dovecot/masterdb -function _create_accounts -{ +function _create_accounts() { : >/etc/postfix/vmailbox : >"${DOVECOT_USERDB_FILE}" @@ -98,8 +97,7 @@ function _create_accounts # # see https://github.com/docker-mailserver/docker-mailserver/pull/2248#issuecomment-953313852 # for more details on this method -function _create_dovecot_alias_dummy_accounts -{ +function _create_dovecot_alias_dummy_accounts() { local DATABASE_VIRTUAL='/tmp/docker-mailserver/postfix-virtual.cf' if [[ -f ${DATABASE_VIRTUAL} ]] && [[ ${ENABLE_QUOTAS} -eq 1 ]]; then @@ -157,8 +155,7 @@ function _create_dovecot_alias_dummy_accounts # Support Dovecot master user: https://doc.dovecot.org/configuration_manual/authentication/master_users/ # Supporting LDAP users requires `auth_bind = yes` in `dovecot-ldap.conf.ext`, see docker-mailserver/docker-mailserver/pull/2535 for details -function _create_masters -{ +function _create_masters() { : >"${DOVECOT_MASTERDB_FILE}" local DATABASE_DOVECOT_MASTERS='/tmp/docker-mailserver/dovecot-masters.cf' diff --git a/target/scripts/helpers/aliases.sh b/target/scripts/helpers/aliases.sh index 16b9e599..9690aae5 100644 --- a/target/scripts/helpers/aliases.sh +++ b/target/scripts/helpers/aliases.sh @@ -6,8 +6,7 @@ # `setup-stack.sh:_setup_ldap` does not seem to configure for `/etc/postfix/virtual however.` # NOTE: `accounts.sh` and `relay.sh:_populate_relayhost_map` also process on `postfix-virtual.cf`. -function _handle_postfix_virtual_config -{ +function _handle_postfix_virtual_config() { : >/etc/postfix/virtual local DATABASE_VIRTUAL=/tmp/docker-mailserver/postfix-virtual.cf @@ -24,8 +23,7 @@ function _handle_postfix_virtual_config fi } -function _handle_postfix_regexp_config -{ +function _handle_postfix_regexp_config() { : >/etc/postfix/regexp if [[ -f /tmp/docker-mailserver/postfix-regexp.cf ]]; then @@ -41,8 +39,7 @@ function _handle_postfix_regexp_config fi } -function _handle_postfix_aliases_config -{ +function _handle_postfix_aliases_config() { _log 'trace' 'Configuring root alias' echo "root: ${POSTMASTER_ADDRESS}" >/etc/aliases @@ -55,8 +52,7 @@ function _handle_postfix_aliases_config } # Other scripts should call this method, rather than the ones above: -function _create_aliases -{ +function _create_aliases() { _handle_postfix_virtual_config _handle_postfix_regexp_config _handle_postfix_aliases_config diff --git a/target/scripts/helpers/change-detection.sh b/target/scripts/helpers/change-detection.sh index 24ea0301..ddda89b0 100644 --- a/target/scripts/helpers/change-detection.sh +++ b/target/scripts/helpers/change-detection.sh @@ -12,8 +12,7 @@ CHKSUM_FILE=/tmp/docker-mailserver-config-chksum # Once container startup scripts complete, take a snapshot of # the config state via storing a list of files content hashes. -function _prepare_for_change_detection -{ +function _prepare_for_change_detection() { _log 'debug' 'Setting up configuration checksum file' _log 'trace' "Creating '${CHKSUM_FILE}'" @@ -22,8 +21,7 @@ function _prepare_for_change_detection # Returns a list of changed files, each line is a value pair of: # -function _monitored_files_checksums -{ +function _monitored_files_checksums() { # If a wildcard path pattern (or an empty ENV) would yield an invalid path # or no results, `shopt -s nullglob` prevents it from being added. shopt -s nullglob diff --git a/target/scripts/helpers/database/db.sh b/target/scripts/helpers/database/db.sh index 8520f7cb..88717885 100644 --- a/target/scripts/helpers/database/db.sh +++ b/target/scripts/helpers/database/db.sh @@ -18,8 +18,7 @@ DATABASE_PASSWD="${DMS_CONFIG}/postfix-sasl-password.cf" DATABASE_RELAY="${DMS_CONFIG}/postfix-relaymap.cf" # Individual scripts with convenience methods to manage operations easier: -function _db_import_scripts -{ +function _db_import_scripts() { # This var is stripped by shellcheck from source paths below, # like the shellcheck source-path above, it shouold match this scripts # parent directory, with the rest of the relative path in the source lines: @@ -35,8 +34,7 @@ function _db_entry_add_or_append { _db_operation 'append' "${@}" ; } # Only us function _db_entry_add_or_replace { _db_operation 'replace' "${@}" ; } function _db_entry_remove { _db_operation 'remove' "${@}" ; } -function _db_operation -{ +function _db_operation() { local DB_ACTION=${1} local DATABASE=${2} local KEY=${3} @@ -126,8 +124,7 @@ function _db_operation } # Internal method for: _db_operation -function __db_list_already_contains_value -{ +function __db_list_already_contains_value() { # Avoids accidentally matching a substring (case-insensitive acceptable): # 1. Extract the current value of the entry (`\1`), # 2. Value list support: Split values into separate lines (`\n`+`g`) at V_DELIMITER, @@ -140,8 +137,7 @@ function __db_list_already_contains_value # Internal method for: _db_operation + _db_has_entry_with_key # References global vars `DATABASE_*`: -function __db_get_delimiter_for -{ +function __db_get_delimiter_for() { local DATABASE=${1} case "${DATABASE}" in @@ -171,8 +167,7 @@ function __db_get_delimiter_for # `\` can escape these (`/` exists in postfix-account.cf base64 encoded pw hash), # But otherwise care should be taken with `\`, which should be forbidden for input here? # NOTE: Presently only `.` is escaped with `\` via `_escape`. -function __escape_sed_replacement -{ +function __escape_sed_replacement() { # Matches any `/` or `&`, and escapes them with `\` (`\\\1`): sed 's/\([/&]\)/\\\1/g' <<< "${ENTRY}" } @@ -181,8 +176,7 @@ function __escape_sed_replacement # Validation Methods # -function _db_has_entry_with_key -{ +function _db_has_entry_with_key() { local KEY=${1} local DATABASE=${2} @@ -202,8 +196,7 @@ function _db_has_entry_with_key grep --quiet --no-messages --ignore-case "^${KEY_LOOKUP}" "${DATABASE}" } -function _db_should_exist_with_content -{ +function _db_should_exist_with_content() { local DATABASE=${1} [[ -f ${DATABASE} ]] || _exit_with_error "'${DATABASE}' does not exist" diff --git a/target/scripts/helpers/database/manage/dovecot-quotas.sh b/target/scripts/helpers/database/manage/dovecot-quotas.sh index 802eb78f..098d17d4 100644 --- a/target/scripts/helpers/database/manage/dovecot-quotas.sh +++ b/target/scripts/helpers/database/manage/dovecot-quotas.sh @@ -3,8 +3,7 @@ # Manage DB writes for: DATABASE_QUOTA # Logic to perform for requested operations handled here: -function _manage_dovecot_quota -{ +function _manage_dovecot_quota() { local ACTION=${1} local MAIL_ACCOUNT=${2} # Only for ACTION 'update': diff --git a/target/scripts/helpers/database/manage/postfix-accounts.sh b/target/scripts/helpers/database/manage/postfix-accounts.sh index 07d484f4..8a2a6133 100644 --- a/target/scripts/helpers/database/manage/postfix-accounts.sh +++ b/target/scripts/helpers/database/manage/postfix-accounts.sh @@ -5,8 +5,7 @@ # - DATABASE_DOVECOT_MASTERS # Logic to perform for requested operations handled here: -function _manage_accounts -{ +function _manage_accounts() { local ACTION=${1} local DATABASE=${2} local MAIL_ACCOUNT=${3} @@ -60,8 +59,7 @@ function _manage_accounts_dovecotmaster_delete { _manage_accounts 'delete' "${DA # - Calling external method '__usage' as part of error handling. # Also used by setquota, delquota -function _arg_expect_mail_account -{ +function _arg_expect_mail_account() { [[ -z ${MAIL_ACCOUNT} ]] && { __usage ; _exit_with_error 'No account specified' ; } # Dovecot Master accounts are validated (they are not email addresses): @@ -71,8 +69,7 @@ function _arg_expect_mail_account [[ ${MAIL_ACCOUNT} =~ .*\@.* ]] || { __usage ; _exit_with_error "'${MAIL_ACCOUNT}' should include the domain (eg: user@example.com)" ; } } -function _account_should_not_exist_yet -{ +function _account_should_not_exist_yet() { __account_already_exists && _exit_with_error "'${MAIL_ACCOUNT}' already exists" if [[ -f ${DATABASE_VIRTUAL} ]] && grep -q "^${MAIL_ACCOUNT}" "${DATABASE_VIRTUAL}"; then _exit_with_error "'${MAIL_ACCOUNT}' is already defined as an alias" @@ -80,20 +77,17 @@ function _account_should_not_exist_yet } # Also used by delmailuser, setquota, delquota -function _account_should_already_exist -{ +function _account_should_already_exist() { ! __account_already_exists && _exit_with_error "'${MAIL_ACCOUNT}' does not exist" } -function __account_already_exists -{ +function __account_already_exists() { local DATABASE=${DATABASE:-"${DATABASE_ACCOUNTS}"} _db_has_entry_with_key "${MAIL_ACCOUNT}" "${DATABASE}" } # Also used by addsaslpassword -function _password_request_if_missing -{ +function _password_request_if_missing() { if [[ -z ${PASSWD} ]]; then read -r -s -p 'Enter Password: ' PASSWD echo diff --git a/target/scripts/helpers/database/manage/postfix-virtual.sh b/target/scripts/helpers/database/manage/postfix-virtual.sh index cc40800e..c2ff5488 100644 --- a/target/scripts/helpers/database/manage/postfix-virtual.sh +++ b/target/scripts/helpers/database/manage/postfix-virtual.sh @@ -11,8 +11,7 @@ # mail to an alias address. # Logic to perform for requested operations handled here: -function _manage_virtual_aliases -{ +function _manage_virtual_aliases() { local ACTION=${1} local MAIL_ALIAS=${2} local RECIPIENT=${3} diff --git a/target/scripts/helpers/dns.sh b/target/scripts/helpers/dns.sh index 72fecec0..688ac508 100644 --- a/target/scripts/helpers/dns.sh +++ b/target/scripts/helpers/dns.sh @@ -2,15 +2,13 @@ # Outputs the DNS label count (delimited by `.`) for the given input string. # Useful for determining an FQDN like `mail.example.com` (3), vs `example.com` (2). -function _get_label_count -{ +function _get_label_count() { awk -F '.' '{ print NF }' <<< "${1}" } # Sets HOSTNAME and DOMAINNAME globals used throughout the scripts, # and any subprocesses called that intereact with it. -function _obtain_hostname_and_domainname -{ +function _obtain_hostname_and_domainname() { # Normally this value would match the output of `hostname` which mirrors `/proc/sys/kernel/hostname`, # However for legacy reasons, the system ENV `HOSTNAME` was replaced here with `hostname -f` instead. # diff --git a/target/scripts/helpers/error.sh b/target/scripts/helpers/error.sh index a75cef9c..3af736df 100644 --- a/target/scripts/helpers/error.sh +++ b/target/scripts/helpers/error.sh @@ -1,7 +1,6 @@ #!/bin/bash -function _exit_with_error -{ +function _exit_with_error() { if [[ -n ${1+set} ]]; then _log 'error' "${1}" else @@ -19,8 +18,7 @@ function _exit_with_error # PANIC_TYPE => (Internal value for matching). You should use the convenience methods below based on your panic type. # PANIC_INFO => Provide your own message string to insert into the error message for that PANIC_TYPE. # PANIC_SCOPE => Optionally provide a string for debugging to better identify/locate the source of the panic. -function dms_panic -{ +function dms_panic() { local PANIC_TYPE=${1:-} local PANIC_INFO=${2:-} local PANIC_SCOPE=${3:-} @@ -76,8 +74,7 @@ function _dms_panic__general { dms_panic 'general' "${1:-}" "${2:-}" # `dms_panic` methods should be preferred if your failure type is supported. trap "exit 1" SIGUSR1 SCRIPT_PID=${$} -function _shutdown -{ +function _shutdown() { _log 'error' "${1:-_shutdown called without message}" _log 'error' 'Shutting down' @@ -91,13 +88,11 @@ function _shutdown # # This is mostly useful for debugging. It also helps when using something like `set -eE`, # as it shows where the script aborts. -function _trap_err_signal -{ +function _trap_err_signal() { trap '__log_unexpected_error "${FUNCNAME[0]:-}" "${BASH_COMMAND:-}" "${LINENO:-}" "${?:-}"' ERR # shellcheck disable=SC2317 - function __log_unexpected_error - { + function __log_unexpected_error() { local MESSAGE="Unexpected error occured :: script = ${SCRIPT:-${0}} " MESSAGE+=" | function = ${1:-none (global)}" MESSAGE+=" | command = ${2:-?}" diff --git a/target/scripts/helpers/index.sh b/target/scripts/helpers/index.sh index 1e919513..8d876739 100644 --- a/target/scripts/helpers/index.sh +++ b/target/scripts/helpers/index.sh @@ -3,8 +3,7 @@ # shellcheck source-path=target/scripts/helpers # This file serves as a single import for all helpers -function _import_scripts -{ +function _import_scripts() { local PATH_TO_SCRIPTS='/usr/local/bin/helpers' source "${PATH_TO_SCRIPTS}/accounts.sh" diff --git a/target/scripts/helpers/lock.sh b/target/scripts/helpers/lock.sh index 8c708225..7156c3ec 100644 --- a/target/scripts/helpers/lock.sh +++ b/target/scripts/helpers/lock.sh @@ -7,8 +7,7 @@ SCRIPT_NAME=$(basename "$0") # prevent removal by other instances of docker-mailserver LOCK_ID=$(uuid) -function _create_lock -{ +function _create_lock() { LOCK_FILE="/tmp/docker-mailserver/${SCRIPT_NAME}.lock" while [[ -e "${LOCK_FILE}" ]] do @@ -28,8 +27,7 @@ function _create_lock echo "${LOCK_ID}" >"${LOCK_FILE}" } -function _remove_lock -{ +function _remove_lock() { LOCK_FILE="${LOCK_FILE:-"/tmp/docker-mailserver/${SCRIPT_NAME}.lock"}" [[ -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}"; then # Ensure we don't delete a lock that's not ours diff --git a/target/scripts/helpers/log.sh b/target/scripts/helpers/log.sh index a31d2408..d771a3f5 100644 --- a/target/scripts/helpers/log.sh +++ b/target/scripts/helpers/log.sh @@ -42,8 +42,7 @@ RESET=$(echo -ne '\e[0m') # If the first argument is not set or invalid, an error # message is logged. Likewise when the second argument # is missing. Both failures will return with exit code '1'. -function _log -{ +function _log() { if [[ -z ${1+set} ]]; then _log 'error' "Call to '_log' is missing a valid log level" return 1 @@ -106,8 +105,7 @@ function _log } # Like `_log` but adds a timestamp in front of the message. -function _log_with_date -{ +function _log_with_date() { _log "${1}" "$(date '+%Y-%m-%d %H:%M:%S') ${2}" } @@ -115,8 +113,7 @@ function _log_with_date # it is set. Otherwise, try to query the common environment # variables file. If this does not yield a value either, # use the default log level. -function _get_log_level_or_default -{ +function _get_log_level_or_default() { if [[ -n ${LOG_LEVEL+set} ]]; then echo "${LOG_LEVEL}" elif [[ -e /etc/dms-settings ]] && grep -q -E "^LOG_LEVEL='[a-z]+'" /etc/dms-settings; then @@ -128,7 +125,6 @@ function _get_log_level_or_default # This function checks whether the log level is the one # provided as the first argument. -function _log_level_is -{ +function _log_level_is() { [[ $(_get_log_level_or_default) =~ ^${1}$ ]] } diff --git a/target/scripts/helpers/network.sh b/target/scripts/helpers/network.sh index d424cb23..afbe5a98 100644 --- a/target/scripts/helpers/network.sh +++ b/target/scripts/helpers/network.sh @@ -1,7 +1,6 @@ #!/bin/bash -function _mask_ip_digit -{ +function _mask_ip_digit() { if [[ ${1} -ge 8 ]]; then MASK=255 elif [[ ${1} -le 0 ]]; then @@ -21,8 +20,7 @@ function _mask_ip_digit # like 1.2.3.4/16 to subnet with cidr suffix # like 1.2.0.0/16. # Assumes correct IP and subnet are provided. -function _sanitize_ipv4_to_subnet_cidr -{ +function _sanitize_ipv4_to_subnet_cidr() { local DIGIT_PREFIX_LENGTH="${1#*/}" declare -a MASKED_DIGITS DIGITS diff --git a/target/scripts/helpers/postfix.sh b/target/scripts/helpers/postfix.sh index 918b7c41..47e57733 100644 --- a/target/scripts/helpers/postfix.sh +++ b/target/scripts/helpers/postfix.sh @@ -17,8 +17,7 @@ # Should not be a concern for most types used by `docker-mailserver`: texthash, ldap, pcre, tcp, unionmap, unix. # The only other type in use by `docker-mailserver` is the hash type for /etc/aliases, which `postalias` handles. -function _create_postfix_vhost -{ +function _create_postfix_vhost() { # `main.cf` configures `virtual_mailbox_domains = /etc/postfix/vhost` # NOTE: Amavis also consumes this file. local DATABASE_VHOST='/etc/postfix/vhost' @@ -29,8 +28,7 @@ function _create_postfix_vhost } # Filter unique values into a proper DATABASE_VHOST config: -function _create_vhost -{ +function _create_vhost() { : >"${DATABASE_VHOST}" if [[ -f ${TMP_VHOST} ]]; then @@ -40,8 +38,7 @@ function _create_vhost } # Collects domains from configs (DATABASE_) into TMP_VHOST -function _vhost_collect_postfix_domains -{ +function _vhost_collect_postfix_domains() { local DATABASE_ACCOUNTS='/tmp/docker-mailserver/postfix-accounts.cf' local DATABASE_VIRTUAL='/tmp/docker-mailserver/postfix-virtual.cf' local DOMAIN UNAME @@ -75,8 +72,7 @@ function _vhost_collect_postfix_domains # - `main.cf:mydestination` setting removes `$mydestination` as an LDAP bugfix. # - `main.cf:virtual_mailbox_domains` uses `/etc/postfix/vhost`, but may # conditionally include a 2nd table (ldap:/etc/postfix/ldap-domains.cf). -function _vhost_ldap_support -{ +function _vhost_ldap_support() { [[ ${ACCOUNT_PROVISIONER} == 'LDAP' ]] && echo "${DOMAINNAME}" >>"${TMP_VHOST}" } diff --git a/target/scripts/helpers/relay.sh b/target/scripts/helpers/relay.sh index 24c69b0e..4a1d8641 100644 --- a/target/scripts/helpers/relay.sh +++ b/target/scripts/helpers/relay.sh @@ -53,15 +53,13 @@ # That shouldn't be a breaking change, as long as the mapping is maintained correctly. # TODO: RELAY_HOST should consider dropping `[]` and require the user to include that? # Future refactor for _populate_relayhost_map may warrant dropping these two ENV in favor of DEFAULT_RELAY_HOST? -function _env_relay_host -{ +function _env_relay_host() { echo "[${RELAY_HOST}]:${RELAY_PORT:-25}" } # Responsible for `postfix-sasl-password.cf` support: # `/etc/postfix/sasl_passwd` example at end of file. -function _relayhost_sasl -{ +function _relayhost_sasl() { if [[ ! -f /tmp/docker-mailserver/postfix-sasl-password.cf ]] \ && [[ -z ${RELAY_USER} || -z ${RELAY_PASSWORD} ]] then @@ -108,8 +106,7 @@ function _relayhost_sasl # to a separate transport (which can drop the `relayhost` setting) would be more appropriate. # TODO: With `sender_dependent_default_transport_maps`, we can extract out the excluded domains and route them through a separate transport. # while deprecating that support in favor of a transport config, similar to what is offered currently via sasl_passwd and relayhost_map. -function _populate_relayhost_map -{ +function _populate_relayhost_map() { # Create the relayhost_map config file: : >/etc/postfix/relayhost_map chown root:root /etc/postfix/relayhost_map @@ -142,8 +139,7 @@ function _populate_relayhost_map # map to a different relay-host, or use a separate transport (needs feature support added). # Args: - function _list_domain_parts - { + function _list_domain_parts() { [[ -f $2 ]] && sed -n -r "/${MATCH_VALID}/ ${1}" "${2}" } # Matches and outputs (capture group via `/\1/p`) the domain part (value of address after `@`) in the config file. @@ -167,16 +163,14 @@ function _populate_relayhost_map postconf 'sender_dependent_relayhost_maps = texthash:/etc/postfix/relayhost_map' } -function _relayhost_configure_postfix -{ +function _relayhost_configure_postfix() { postconf \ 'smtp_sasl_auth_enable = yes' \ 'smtp_sasl_security_options = noanonymous' \ 'smtp_tls_security_level = encrypt' } -function _setup_relayhost -{ +function _setup_relayhost() { _log 'debug' 'Setting up Postfix Relay Hosts' if [[ -n ${DEFAULT_RELAY_HOST} ]]; then @@ -194,8 +188,7 @@ function _setup_relayhost fi } -function _rebuild_relayhost -{ +function _rebuild_relayhost() { if [[ -n ${RELAY_HOST} ]]; then _relayhost_sasl _populate_relayhost_map diff --git a/target/scripts/helpers/ssl.sh b/target/scripts/helpers/ssl.sh index da2c7756..6a7610ad 100644 --- a/target/scripts/helpers/ssl.sh +++ b/target/scripts/helpers/ssl.sh @@ -1,7 +1,6 @@ #!/bin/bash -function _setup_dhparam -{ +function _setup_dhparam() { local DH_SERVICE=$1 local DH_DEST=$2 local DH_CUSTOM='/tmp/docker-mailserver/dhparams.pem' @@ -18,8 +17,7 @@ function _setup_dhparam fi } -function _setup_ssl -{ +function _setup_ssl() { _log 'debug' 'Setting up SSL' local POSTFIX_CONFIG_MAIN='/etc/postfix/main.cf' @@ -31,8 +29,7 @@ function _setup_ssl mkdir -p "${DMS_TLS_PATH}" # Primary certificate to serve for TLS - function _set_certificate - { + function _set_certificate() { local POSTFIX_KEY_WITH_FULLCHAIN=${1} local DOVECOT_KEY=${1} local DOVECOT_CERT=${1} @@ -60,8 +57,7 @@ function _setup_ssl } # Enables supporting two certificate types such as ECDSA with an RSA fallback - function _set_alt_certificate - { + function _set_alt_certificate() { local COPY_KEY_FROM_PATH=$1 local COPY_CERT_FROM_PATH=$2 local PRIVATE_KEY_ALT="${DMS_TLS_PATH}/fallback_key" @@ -88,8 +84,7 @@ function _setup_ssl "${DOVECOT_CONFIG_SSL}" } - function _apply_tls_level - { + function _apply_tls_level() { local TLS_CIPHERS_ALLOW=$1 local TLS_PROTOCOL_IGNORE=$2 local TLS_PROTOCOL_MINIMUM=$3 @@ -113,8 +108,7 @@ function _setup_ssl # Extracts files `key.pem` and `fullchain.pem`. # `_extract_certs_from_acme` is located in `helpers/ssl.sh` # NOTE: See the `SSL_TYPE=letsencrypt` case below for more details. - function _traefik_support - { + function _traefik_support() { if [[ -f /etc/letsencrypt/acme.json ]]; then # Variable only intended for troubleshooting via debug output local EXTRACTED_DOMAIN @@ -379,8 +373,7 @@ function _setup_ssl # Identify a valid letsencrypt FQDN folder to use. -function _find_letsencrypt_domain -{ +function _find_letsencrypt_domain() { local LETSENCRYPT_DOMAIN if [[ -n ${SSL_DOMAIN} ]] && [[ -e /etc/letsencrypt/live/$(_strip_wildcard_prefix "${SSL_DOMAIN}")/fullchain.pem ]]; then @@ -398,8 +391,7 @@ function _find_letsencrypt_domain } # Verify the FQDN folder also includes a valid private key (`privkey.pem` for Certbot, `key.pem` for extraction by Traefik) -function _find_letsencrypt_key -{ +function _find_letsencrypt_key() { local LETSENCRYPT_KEY local LETSENCRYPT_DOMAIN=${1} @@ -419,8 +411,7 @@ function _find_letsencrypt_key echo "${LETSENCRYPT_KEY}" } -function _extract_certs_from_acme -{ +function _extract_certs_from_acme() { local CERT_DOMAIN=${1} if [[ -z ${CERT_DOMAIN} ]]; then _log 'warn' "_extract_certs_from_acme | CERT_DOMAIN is empty" diff --git a/target/scripts/helpers/utils.sh b/target/scripts/helpers/utils.sh index da093deb..59e0f63b 100644 --- a/target/scripts/helpers/utils.sh +++ b/target/scripts/helpers/utils.sh @@ -1,21 +1,18 @@ #!/bin/bash -function _escape -{ +function _escape() { echo "${1//./\\.}" } # Returns input after filtering out lines that are: # empty, white-space, comments (`#` as the first non-whitespace character) -function _get_valid_lines_from_file -{ +function _get_valid_lines_from_file() { grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true } # Provide the name of an environment variable to this function # and it will return its value stored in /etc/dms-settings -function _get_dms_env_value -{ +function _get_dms_env_value() { if [[ -f /etc/dms-settings ]]; then grep "^${1}=" /etc/dms-settings | cut -d "'" -f 2 else @@ -30,8 +27,7 @@ function _get_dms_env_value # # `helpers/accounts.sh:_create_accounts` (mkdir, cp) appears to be the only writer to # /var/mail folders (used during startup and change detection handling). -function _chown_var_mail_if_necessary -{ +function _chown_var_mail_if_necessary() { # fix permissions, but skip this if 3 levels deep the user id is already set if find /var/mail -maxdepth 3 -a \( \! -user 5000 -o \! -group 5000 \) | read -r; then _log 'trace' 'Fixing /var/mail permissions' @@ -39,8 +35,7 @@ function _chown_var_mail_if_necessary fi } -function _require_n_parameters_or_print_usage -{ +function _require_n_parameters_or_print_usage() { local COUNT COUNT=${1} shift @@ -55,15 +50,13 @@ function _require_n_parameters_or_print_usage # After we modify the config explicitly, we can safely assume (reasonably) # that the write stream has completed, and it is safe to read the config. # https://github.com/docker-mailserver/docker-mailserver/issues/2985 -function _adjust_mtime_for_postfix_maincf -{ +function _adjust_mtime_for_postfix_maincf() { if [[ $(( $(date '+%s') - $(stat -c '%Y' '/etc/postfix/main.cf') )) -lt 2 ]]; then touch -d '2 seconds ago' /etc/postfix/main.cf fi } -function _reload_postfix -{ +function _reload_postfix() { _adjust_mtime_for_postfix_maincf postfix reload } @@ -92,8 +85,7 @@ function _reload_postfix # # 1. No first and second argument is supplied # 2. The second argument is a path to a file that does not exist -function _replace_by_env_in_file -{ +function _replace_by_env_in_file() { if [[ -z ${1+set} ]]; then _dms_panic__invalid_value 'first argument unset' 'utils.sh:_replace_by_env_in_file' elif [[ -z ${2+set} ]]; then @@ -123,8 +115,7 @@ function _replace_by_env_in_file # is not zero or one. # # @param ${1} = name of the ENV variable to check -function _env_var_expect_zero_or_one -{ +function _env_var_expect_zero_or_one() { local ENV_VAR_NAME=${1:?ENV var name must be provided to _env_var_expect_zero_or_one} [[ ${!ENV_VAR_NAME} =~ ^(0|1)$ ]] && return 0 @@ -138,8 +129,7 @@ function _env_var_expect_zero_or_one # is not an integer. # # @param ${1} = name of the ENV variable to check -function _env_var_expect_integer -{ +function _env_var_expect_integer() { local ENV_VAR_NAME=${1:?ENV var name must be provided to _env_var_expect_integer} [[ ${!ENV_VAR_NAME} =~ ^-?[0-9][0-9]*$ ]] && return 0 diff --git a/target/scripts/start-mailserver.sh b/target/scripts/start-mailserver.sh index 2ad4c2da..6a22e12d 100755 --- a/target/scripts/start-mailserver.sh +++ b/target/scripts/start-mailserver.sh @@ -27,8 +27,7 @@ source /usr/local/bin/daemons-stack.sh # ? >> Registering functions # ------------------------------------------------------------ -function _register_functions -{ +function _register_functions() { _log 'debug' 'Registering functions' # ? >> Checks diff --git a/target/scripts/startup/check-stack.sh b/target/scripts/startup/check-stack.sh index 05b76c45..acefa55d 100644 --- a/target/scripts/startup/check-stack.sh +++ b/target/scripts/startup/check-stack.sh @@ -2,14 +2,12 @@ declare -a FUNCS_CHECK -function _register_check_function -{ +function _register_check_function() { FUNCS_CHECK+=("${1}") _log 'trace' "${1}() registered" } -function _check -{ +function _check() { _log 'info' 'Checking configuration' for FUNC in "${FUNCS_CHECK[@]}" do @@ -17,8 +15,7 @@ function _check done } -function _check_improper_restart -{ +function _check_improper_restart() { _log 'debug' 'Checking for improper restart' if [[ -f /CONTAINER_START ]]; then @@ -27,8 +24,7 @@ function _check_improper_restart fi } -function _check_hostname -{ +function _check_hostname() { _log 'debug' 'Checking that hostname/domainname is provided or overridden' _log 'debug' "Domain has been set to ${DOMAINNAME}" @@ -40,8 +36,7 @@ function _check_hostname fi } -function _check_log_level -{ +function _check_log_level() { if [[ ${LOG_LEVEL} == 'trace' ]] \ || [[ ${LOG_LEVEL} == 'debug' ]] \ || [[ ${LOG_LEVEL} == 'info' ]] \ diff --git a/target/scripts/startup/daemons-stack.sh b/target/scripts/startup/daemons-stack.sh index ae88dc24..d8d6f808 100644 --- a/target/scripts/startup/daemons-stack.sh +++ b/target/scripts/startup/daemons-stack.sh @@ -2,14 +2,12 @@ declare -a DAEMONS_START -function _register_start_daemon -{ +function _register_start_daemon() { DAEMONS_START+=("${1}") _log 'trace' "${1}() registered" } -function _start_daemons -{ +function _start_daemons() { _log 'info' 'Starting daemons' for FUNCTION in "${DAEMONS_START[@]}" @@ -18,8 +16,7 @@ function _start_daemons done } -function _default_start_daemon -{ +function _default_start_daemon() { _log 'debug' "Starting ${1:?}" local RESULT @@ -47,19 +44,16 @@ function _start_daemon_rspamd_redis { _default_start_daemon 'rspamd-redis' ; function _start_daemon_rsyslog { _default_start_daemon 'rsyslog' ; } function _start_daemon_update_check { _default_start_daemon 'update-check' ; } -function _start_daemon_saslauthd -{ +function _start_daemon_saslauthd() { _default_start_daemon "saslauthd_${SASLAUTHD_MECHANISMS}" } -function _start_daemon_postfix -{ +function _start_daemon_postfix() { _adjust_mtime_for_postfix_maincf _default_start_daemon 'postfix' } -function _start_daemon_fetchmail -{ +function _start_daemon_fetchmail() { if [[ ${FETCHMAIL_PARALLEL} -eq 1 ]]; then local COUNTER=0 for _ in /etc/fetchmailrc.d/fetchmail-*.rc diff --git a/target/scripts/startup/setup-stack.sh b/target/scripts/startup/setup-stack.sh index e31b8464..a83ae5c6 100644 --- a/target/scripts/startup/setup-stack.sh +++ b/target/scripts/startup/setup-stack.sh @@ -2,14 +2,12 @@ declare -a FUNCS_SETUP -function _register_setup_function -{ +function _register_setup_function() { FUNCS_SETUP+=("${1}") _log 'trace' "${1}() registered" } -function _setup -{ +function _setup() { # Requires `shopt -s globstar` because of `**` which in # turn is required as we're decending through directories for FILE in /usr/local/bin/setup.d/**/*.sh @@ -28,8 +26,7 @@ function _setup _prepare_for_change_detection } -function _early_supervisor_setup -{ +function _early_supervisor_setup() { SUPERVISOR_LOGLEVEL="${SUPERVISOR_LOGLEVEL:-warn}" if ! grep -q "loglevel = ${SUPERVISOR_LOGLEVEL}" /etc/supervisor/supervisord.conf; then @@ -56,8 +53,7 @@ function _early_supervisor_setup return 0 } -function _setup_timezone -{ +function _setup_timezone() { [[ -n ${TZ} ]] || return 0 _log 'debug' "Setting timezone to '${TZ}'" @@ -78,8 +74,7 @@ function _setup_timezone fi } -function _setup_apply_fixes_after_configuration -{ +function _setup_apply_fixes_after_configuration() { _log 'trace' 'Removing leftover PID files from a stop/start' find /var/run/ -not -name 'supervisord.pid' -name '*.pid' -delete touch /dev/shm/supervisor.sock @@ -93,8 +88,7 @@ function _setup_apply_fixes_after_configuration rm -rf /var/mail-state/spool-postfix/{dev,etc,lib,pid,usr,private/auth} } -function _run_user_patches -{ +function _run_user_patches() { local USER_PATCHES='/tmp/docker-mailserver/user-patches.sh' if [[ -f ${USER_PATCHES} ]]; then diff --git a/target/scripts/startup/setup.d/dmarc_dkim_spf.sh b/target/scripts/startup/setup.d/dmarc_dkim_spf.sh index 85de88cd..20b88ea5 100644 --- a/target/scripts/startup/setup.d/dmarc_dkim_spf.sh +++ b/target/scripts/startup/setup.d/dmarc_dkim_spf.sh @@ -6,8 +6,7 @@ # # The OpenDKIM milter must come before the OpenDMARC milter in Postfix's # `smtpd_milters` milters options. -function _setup_opendkim -{ +function _setup_opendkim() { if [[ ${ENABLE_OPENDKIM} -eq 1 ]]; then _log 'debug' 'Configuring DKIM' @@ -54,8 +53,7 @@ function _setup_opendkim # # The OpenDMARC milter must come after the OpenDKIM milter in Postfix's # `smtpd_milters` milters options. -function _setup_opendmarc -{ +function _setup_opendmarc() { if [[ ${ENABLE_OPENDMARC} -eq 1 ]]; then # TODO When disabling SPF is possible, add a check whether DKIM and SPF is disabled # for DMARC to work, you should have at least one enabled @@ -83,8 +81,7 @@ function _setup_opendmarc # Configures the SPF check inside Postfix's configuration via policyd-spf. When # using Rspamd, you will likely want to turn that off. -function _setup_policyd_spf -{ +function _setup_policyd_spf() { if [[ ${ENABLE_POLICYD_SPF} -eq 1 ]]; then _log 'debug' 'Configuring policyd-spf' cat >>/etc/postfix/master.cf <= 0 ; COUNTER-- )) @@ -169,8 +164,7 @@ function _setup_dovecot_local_user _create_accounts } -function _setup_dovecot_inet_protocols -{ +function _setup_dovecot_inet_protocols() { [[ ${DOVECOT_INET_PROTOCOLS} == 'all' ]] && return 0 _log 'trace' 'Setting up DOVECOT_INET_PROTOCOLS option' @@ -189,13 +183,11 @@ function _setup_dovecot_inet_protocols sedfile -i "s|^#listen =.*|listen = ${PROTOCOL}|g" /etc/dovecot/dovecot.conf } -function _setup_dovecot_dhparam -{ +function _setup_dovecot_dhparam() { _setup_dhparam 'Dovecot' '/etc/dovecot/dh.pem' } -function _setup_dovecot_hostname -{ +function _setup_dovecot_hostname() { _log 'debug' 'Applying hostname to Dovecot' sed -i "s|^#hostname =.*$|hostname = '${HOSTNAME}'|g" /etc/dovecot/conf.d/15-lda.conf } diff --git a/target/scripts/startup/setup.d/fetchmail.sh b/target/scripts/startup/setup.d/fetchmail.sh index 99b74d84..1d5b9570 100644 --- a/target/scripts/startup/setup.d/fetchmail.sh +++ b/target/scripts/startup/setup.d/fetchmail.sh @@ -1,7 +1,6 @@ #!/bin/bash -function _setup_fetchmail -{ +function _setup_fetchmail() { if [[ ${ENABLE_FETCHMAIL} -eq 1 ]]; then _log 'trace' 'Enabling and configuring Fetchmail' @@ -23,8 +22,7 @@ function _setup_fetchmail fi } -function _setup_fetchmail_parallel -{ +function _setup_fetchmail_parallel() { if [[ ${FETCHMAIL_PARALLEL} -eq 1 ]]; then _log 'trace' 'Enabling and configuring Fetchmail parallel' mkdir /etc/fetchmailrc.d/ @@ -35,8 +33,7 @@ function _setup_fetchmail_parallel # # The sole purpose for this is to work around what is known # as the Fetchmail IMAP idle issue. - function _fetchmailrc_split - { + function _fetchmailrc_split() { local FETCHMAILRC='/etc/fetchmailrc' local FETCHMAILRCD='/etc/fetchmailrc.d' local DEFAULT_FILE="${FETCHMAILRCD}/defaults" diff --git a/target/scripts/startup/setup.d/getmail.sh b/target/scripts/startup/setup.d/getmail.sh index cf4547d4..ede7561e 100644 --- a/target/scripts/startup/setup.d/getmail.sh +++ b/target/scripts/startup/setup.d/getmail.sh @@ -1,7 +1,6 @@ #!/bin/bash -function _setup_getmail -{ +function _setup_getmail() { if [[ ${ENABLE_GETMAIL} -eq 1 ]]; then _log 'trace' 'Preparing Getmail configuration' diff --git a/target/scripts/startup/setup.d/ldap.sh b/target/scripts/startup/setup.d/ldap.sh index 36c60fad..349fd63f 100644 --- a/target/scripts/startup/setup.d/ldap.sh +++ b/target/scripts/startup/setup.d/ldap.sh @@ -1,7 +1,6 @@ #!/bin/bash -function _setup_ldap -{ +function _setup_ldap() { _log 'debug' 'Setting up LDAP' _log 'trace' 'Checking for custom configs' diff --git a/target/scripts/startup/setup.d/log.sh b/target/scripts/startup/setup.d/log.sh index b87c6130..eef76a3f 100644 --- a/target/scripts/startup/setup.d/log.sh +++ b/target/scripts/startup/setup.d/log.sh @@ -1,7 +1,6 @@ #!/bin/bash -function _setup_logs_general -{ +function _setup_logs_general() { _log 'debug' 'Setting up general log files' # File/folder permissions are fine when using docker volumes, but may be wrong @@ -11,8 +10,7 @@ function _setup_logs_general chown syslog:root /var/log/mail } -function _setup_logrotate -{ +function _setup_logrotate() { _log 'debug' 'Setting up logrotate' LOGROTATE='/var/log/mail/mail.log\n{\n compress\n copytruncate\n delaycompress\n' @@ -42,8 +40,7 @@ function _setup_logrotate echo -e "${LOGROTATE}}" >/etc/logrotate.d/maillog } -function _setup_mail_summary -{ +function _setup_mail_summary() { local ENABLED_MESSAGE ENABLED_MESSAGE="Enabling Postfix log summary reports with recipient '${PFLOGSUMM_RECIPIENT}'" @@ -80,8 +77,7 @@ EOF esac } -function _setup_logwatch -{ +function _setup_logwatch() { echo 'LogFile = /var/log/mail/freshclam.log' >>/etc/logwatch/conf/logfiles/clam-update.conf echo "MailFrom = ${LOGWATCH_SENDER}" >>/etc/logwatch/conf/logwatch.conf echo "Mailer = \"sendmail -t -f ${LOGWATCH_SENDER}\"" >>/etc/logwatch/conf/logwatch.conf diff --git a/target/scripts/startup/setup.d/mail_state.sh b/target/scripts/startup/setup.d/mail_state.sh index aa59ac62..c3f56564 100644 --- a/target/scripts/startup/setup.d/mail_state.sh +++ b/target/scripts/startup/setup.d/mail_state.sh @@ -2,8 +2,7 @@ # Consolidate all states into a single directory # (/var/mail-state) to allow persistence using docker volumes -function _setup_save_states -{ +function _setup_save_states() { local DEST DESTDIR STATEDIR SERVICEDIR SERVICEDIRS SERVICEFILE SERVICEFILES STATEDIR='/var/mail-state' diff --git a/target/scripts/startup/setup.d/networking.sh b/target/scripts/startup/setup.d/networking.sh index d8669ac3..8bf019e9 100644 --- a/target/scripts/startup/setup.d/networking.sh +++ b/target/scripts/startup/setup.d/networking.sh @@ -1,13 +1,11 @@ #!/bin/bash -function _setup_mailname -{ +function _setup_mailname() { _log 'debug' "Setting up mailname and creating '/etc/mailname'" echo "${DOMAINNAME}" >/etc/mailname } -function _setup_docker_permit -{ +function _setup_docker_permit() { _log 'debug' 'Setting up PERMIT_DOCKER option' local CONTAINER_IP CONTAINER_NETWORK @@ -29,14 +27,12 @@ function _setup_docker_permit CONTAINER_NETWORKS+=("${IP}") done < <(ip -o -4 addr show type veth | grep -E -o '[0-9\.]+/[0-9]+') - function __clear_postfix_mynetworks - { + function __clear_postfix_mynetworks() { _log 'trace' "Clearing Postfix's 'mynetworks'" postconf "mynetworks =" } - function __add_to_postfix_mynetworks - { + function __add_to_postfix_mynetworks() { local NETWORK_TYPE=$1 local NETWORK=$2 diff --git a/target/scripts/startup/setup.d/postfix.sh b/target/scripts/startup/setup.d/postfix.sh index 3ae6741e..5a57ee7c 100644 --- a/target/scripts/startup/setup.d/postfix.sh +++ b/target/scripts/startup/setup.d/postfix.sh @@ -7,8 +7,7 @@ # @param ${2} = message function __postfix__log { _log "${1:-}" "(Postfix setup) ${2:-}" ; } -function _setup_postfix_early -{ +function _setup_postfix_early() { _log 'debug' 'Configuring Postfix (early setup)' __postfix__log 'trace' 'Applying hostname and domainname' @@ -66,8 +65,7 @@ EOF fi } -function _setup_postfix_late -{ +function _setup_postfix_late() { _log 'debug' 'Configuring Postfix (late setup)' __postfix__log 'trace' 'Configuring user access' @@ -91,8 +89,7 @@ function _setup_postfix_late __postfix__setup_override_configuration } -function __postfix__setup_override_configuration -{ +function __postfix__setup_override_configuration() { __postfix__log 'debug' 'Overriding / adjusting configuration with user-supplied values' if [[ -f /tmp/docker-mailserver/postfix-main.cf ]]; then @@ -122,8 +119,7 @@ function __postfix__setup_override_configuration fi } -function _setup_SRS -{ +function _setup_SRS() { _log 'debug' 'Setting up SRS' postconf 'sender_canonical_maps = tcp:localhost:10001' @@ -131,8 +127,7 @@ function _setup_SRS postconf 'recipient_canonical_maps = tcp:localhost:10002' postconf 'recipient_canonical_classes = envelope_recipient,header_recipient' - function __generate_secret - { + function __generate_secret() { ( umask 0077 dd if=/dev/urandom bs=24 count=1 2>/dev/null | base64 -w0 >"${1}" diff --git a/target/scripts/startup/setup.d/saslauthd.sh b/target/scripts/startup/setup.d/saslauthd.sh index bbe3358c..12f00726 100644 --- a/target/scripts/startup/setup.d/saslauthd.sh +++ b/target/scripts/startup/setup.d/saslauthd.sh @@ -1,8 +1,7 @@ #!/bin/bash -function _setup_saslauthd -{ +function _setup_saslauthd() { _log 'debug' 'Setting up SASLAUTHD' if [[ ! -f /etc/saslauthd.conf ]]; then diff --git a/target/scripts/startup/setup.d/security/misc.sh b/target/scripts/startup/setup.d/security/misc.sh index 863a3afc..fe09c8a4 100644 --- a/target/scripts/startup/setup.d/security/misc.sh +++ b/target/scripts/startup/setup.d/security/misc.sh @@ -1,7 +1,6 @@ #!/bin/bash -function _setup_security_stack -{ +function _setup_security_stack() { _log 'debug' 'Setting up Security Stack' __setup__security__postgrey @@ -23,8 +22,7 @@ function _setup_security_stack __setup__security__amavis } -function __setup__security__postgrey -{ +function __setup__security__postgrey() { if [[ ${ENABLE_POSTGREY} -eq 1 ]]; then _log 'debug' 'Enabling and configuring Postgrey' @@ -52,8 +50,7 @@ function __setup__security__postgrey fi } -function __setup__security__postscreen -{ +function __setup__security__postscreen() { _log 'debug' 'Configuring Postscreen' sed -i \ -e "s|postscreen_dnsbl_action = enforce|postscreen_dnsbl_action = ${POSTSCREEN_ACTION}|" \ @@ -69,8 +66,7 @@ function __setup__security__postscreen fi } -function __setup__security__spamassassin -{ +function __setup__security__spamassassin() { if [[ ${ENABLE_SPAMASSASSIN} -eq 1 ]]; then _log 'debug' 'Enabling and configuring SpamAssassin' @@ -157,8 +153,7 @@ EOF fi } -function __setup__security__clamav -{ +function __setup__security__clamav() { if [[ ${ENABLE_CLAMAV} -eq 1 ]]; then _log 'debug' 'Enabling and configuring ClamAV' @@ -197,8 +192,7 @@ function __setup__security__clamav fi } -function __setup__security__fail2ban -{ +function __setup__security__fail2ban() { if [[ ${ENABLE_FAIL2BAN} -eq 1 ]]; then _log 'debug' 'Enabling and configuring Fail2Ban' @@ -221,8 +215,7 @@ function __setup__security__fail2ban fi } -function __setup__security__amavis -{ +function __setup__security__amavis() { if [[ ${ENABLE_AMAVIS} -eq 1 ]]; then _log 'debug' 'Configuring Amavis' if [[ -f /tmp/docker-mailserver/amavis.cf ]]; then @@ -257,8 +250,7 @@ function __setup__security__amavis } # We can use Sieve to move spam emails to the "Junk" folder. -function _setup_spam_to_junk -{ +function _setup_spam_to_junk() { if [[ ${MOVE_SPAM_TO_JUNK} -eq 1 ]]; then _log 'debug' 'Spam emails will be moved to the Junk folder' cat >/usr/lib/dovecot/sieve-global/after/spam_to_junk.sieve << EOF diff --git a/target/scripts/startup/setup.d/security/rspamd.sh b/target/scripts/startup/setup.d/security/rspamd.sh index 6c0f3903..e56433b0 100644 --- a/target/scripts/startup/setup.d/security/rspamd.sh +++ b/target/scripts/startup/setup.d/security/rspamd.sh @@ -1,8 +1,7 @@ #!/bin/bash # Function called during global setup to handle the complete setup of Rspamd. -function _setup_rspamd -{ +function _setup_rspamd() { if _env_var_expect_zero_or_one 'ENABLE_RSPAMD' && [[ ${ENABLE_RSPAMD} -eq 1 ]]; then _log 'debug' 'Enabling and configuring Rspamd' __rspamd__log 'trace' '---------- Setup started ----------' @@ -36,8 +35,7 @@ function __rspamd__log { _log "${1:-}" "(Rspamd setup) ${2:-}" ; } # @param ${2} = `true` when you want to enable the module (default), # `false` when you want to disable the module [OPTIONAL] # @param ${3} = whether to use `local` (default) or `override` [OPTIONAL] -function __rspamd__helper__enable_disable_module -{ +function __rspamd__helper__enable_disable_module() { local MODULE=${1:?Module name must be provided} local ENABLE_MODULE=${2:-true} local LOCAL_OR_OVERRIDE=${3:-local} @@ -61,8 +59,7 @@ EOF # Run miscellaneous early setup tasks and checks, such as creating files needed at runtime # or checking for other anti-spam/anti-virus software. -function __rspamd__run_early_setup_and_checks -{ +function __rspamd__run_early_setup_and_checks() { # Note: Variables not marked with `local` are # used in other functions as well. RSPAMD_LOCAL_D='/etc/rspamd/local.d' @@ -105,8 +102,7 @@ function __rspamd__run_early_setup_and_checks # Sets up Redis. In case the user does not use a dedicated Redis instance, we # supply a configuration for our local Redis instance which is started later. -function __rspamd__setup_redis -{ +function __rspamd__setup_redis() { if _env_var_expect_zero_or_one 'ENABLE_RSPAMD_REDIS' && [[ ${ENABLE_RSPAMD_REDIS} -eq 1 ]]; then __rspamd__log 'debug' 'Internal Redis is enabled, adding configuration' cat >"${RSPAMD_LOCAL_D}/redis.conf" << "EOF" @@ -136,8 +132,7 @@ EOF # Adjust Postfix's configuration files. We only need to append Rspamd at the end of # `smtpd_milters` in `/etc/postfix/main.cf`. -function __rspamd__setup_postfix -{ +function __rspamd__setup_postfix() { __rspamd__log 'debug' "Adjusting Postfix's configuration" postconf 'rspamd_milter = inet:localhost:11332' @@ -146,8 +141,7 @@ function __rspamd__setup_postfix } # If ClamAV is enabled, we will integrate it into Rspamd. -function __rspamd__setup_clamav -{ +function __rspamd__setup_clamav() { if _env_var_expect_zero_or_one 'ENABLE_CLAMAV' && [[ ${ENABLE_CLAMAV} -eq 1 ]]; then __rspamd__log 'debug' 'Enabling ClamAV integration' sedfile -i -E 's|^(enabled).*|\1 = true;|g' "${RSPAMD_LOCAL_D}/antivirus.conf" @@ -169,8 +163,7 @@ function __rspamd__setup_clamav # We disable the modules listed in `DISABLE_MODULES` as we believe these modules # are not commonly used and the average user does not need them. As a consequence, # disabling them saves resources. -function __rspamd__setup_default_modules -{ +function __rspamd__setup_default_modules() { __rspamd__log 'debug' 'Disabling default modules' # This array contains all the modules we disable by default. They @@ -197,8 +190,7 @@ function __rspamd__setup_default_modules # 1. enabling auto-learn for the classifier-bayes module # 2. setting up sieve scripts that detect when a user is moving e-mail # from or to the "Junk" folder, and learning them as ham or spam. -function __rspamd__setup_learning -{ +function __rspamd__setup_learning() { if _env_var_expect_zero_or_one 'RSPAMD_LEARN' && [[ ${RSPAMD_LEARN} -eq 1 ]]; then __rspamd__log 'debug' 'Setting up intelligent learning of spam and ham' @@ -241,8 +233,7 @@ EOF # Sets up greylisting with the greylisting module (see # https://rspamd.com/doc/modules/greylisting.html). -function __rspamd__setup_greylisting -{ +function __rspamd__setup_greylisting() { if _env_var_expect_zero_or_one 'RSPAMD_GREYLISTING' && [[ ${RSPAMD_GREYLISTING} -eq 1 ]]; then __rspamd__log 'debug' 'Enabling greylisting' sedfile -i -E "s|(enabled =).*|\1 true;|g" "${RSPAMD_LOCAL_D}/greylist.conf" @@ -255,8 +246,7 @@ function __rspamd__setup_greylisting # https://www.rspamd.com/doc/modules/hfilter.html). This module is mainly # used for hostname checks, and whether or not a reverse-DNS check # succeeds. -function __rspamd__setup_hfilter_group -{ +function __rspamd__setup_hfilter_group() { local MODULE_FILE="${RSPAMD_LOCAL_D}/hfilter_group.conf" if _env_var_expect_zero_or_one 'RSPAMD_HFILTER' && [[ ${RSPAMD_HFILTER} -eq 1 ]]; then __rspamd__log 'debug' 'Hfilter (group) module is enabled' @@ -278,8 +268,7 @@ function __rspamd__setup_hfilter_group # Parses `RSPAMD_CUSTOM_COMMANDS_FILE` and executed the directives given by the file. # To get a detailed explanation of the commands and how the file works, visit # https://docker-mailserver.github.io/docker-mailserver/edge/config/security/rspamd/#with-the-help-of-a-custom-file -function __rspamd__handle_user_modules_adjustments -{ +function __rspamd__handle_user_modules_adjustments() { # Adds an option with a corresponding value to a module, or, in case the option # is already present, overwrites it. # @@ -293,8 +282,7 @@ function __rspamd__handle_user_modules_adjustments # While this function is currently bound to the scope of `__rspamd__handle_user_modules_adjustments`, # it is written in a versatile way (taking 4 arguments instead of assuming `ARGUMENT2` / `ARGUMENT3` # are set) so that it may be used elsewhere if needed. - function __add_or_replace - { + function __add_or_replace() { local MODULE_FILE=${1:?Module file name must be provided} local MODULE_LOG_NAME=${2:?Module log name must be provided} local OPTION=${3:?Option name must be provided} diff --git a/target/scripts/startup/setup.d/security/spoofing.sh b/target/scripts/startup/setup.d/security/spoofing.sh index 4a569ff4..7c38821d 100644 --- a/target/scripts/startup/setup.d/security/spoofing.sh +++ b/target/scripts/startup/setup.d/security/spoofing.sh @@ -1,7 +1,6 @@ #!/bin/bash -function _setup_spoof_protection -{ +function _setup_spoof_protection() { if [[ ${SPOOF_PROTECTION} -eq 1 ]]; then _log 'trace' 'Enabling and configuring spoof protection' diff --git a/target/scripts/startup/variables-stack.sh b/target/scripts/startup/variables-stack.sh index f180c7a4..e59119ab 100644 --- a/target/scripts/startup/variables-stack.sh +++ b/target/scripts/startup/variables-stack.sh @@ -3,8 +3,7 @@ # shellcheck disable=SC2034 declare -A VARS -function _early_variables_setup -{ +function _early_variables_setup() { _obtain_hostname_and_domainname __environment_variables_backwards_compatibility __environment_variables_general_setup @@ -13,8 +12,7 @@ function _early_variables_setup # This function handles variables that are deprecated. This allows a # smooth transition period, without the need of removing a variable # completely with a single version. -function __environment_variables_backwards_compatibility -{ +function __environment_variables_backwards_compatibility() { if [[ ${ENABLE_LDAP:-0} -eq 1 ]]; then _log 'warn' "'ENABLE_LDAP=1' is deprecated (and will be removed in v13.0.0) => use 'ACCOUNT_PROVISIONER=LDAP' instead" ACCOUNT_PROVISIONER='LDAP' @@ -32,8 +30,7 @@ function __environment_variables_backwards_compatibility # This function sets almost all environment variables. This involves setting # a default if no value was provided and writing the variable and its value # to the VARS map. -function __environment_variables_general_setup -{ +function __environment_variables_general_setup() { _log 'debug' 'Handling general environment variable setup' # these variables must be defined first @@ -143,8 +140,7 @@ function __environment_variables_general_setup } # This function handles environment variables related to LDAP. -function _environment_variables_ldap -{ +function _environment_variables_ldap() { _log 'debug' 'Setting LDAP-related environment variables now' VARS[LDAP_BIND_DN]="${LDAP_BIND_DN:=}" @@ -156,8 +152,7 @@ function _environment_variables_ldap # This function handles environment variables related to SASLAUTHD # and, if activated, variables related to SASLAUTHD and LDAP. -function _environment_variables_saslauthd -{ +function _environment_variables_saslauthd() { _log 'debug' 'Setting SASLAUTHD-related environment variables now' VARS[SASLAUTHD_MECHANISMS]="${SASLAUTHD_MECHANISMS:=pam}" @@ -210,8 +205,7 @@ function _environment_variables_saslauthd # This function Writes the contents of the `VARS` map (associative array) # to locations where they can be sourced from (e.g. `/etc/dms-settings`) # or where they can be used by Bash directly (e.g. `/root/.bashrc`). -function _environment_variables_export -{ +function _environment_variables_export() { _log 'debug' "Exporting environment variables now (creating '/etc/dms-settings')" : >/root/.bashrc # make DMS variables available in login shells and their subprocesses diff --git a/test/helper/common.bash b/test/helper/common.bash index 46c0b997..34b5f97c 100644 --- a/test/helper/common.bash +++ b/test/helper/common.bash @@ -396,8 +396,7 @@ function _container_is_running() { # # @param ${1} = directory # @param ${2} = number of files that should be in ${1} -function _count_files_in_directory_in_container() -{ +function _count_files_in_directory_in_container() { local DIRECTORY=${1:?No directory provided} local NUMBER_OF_LINES=${2:?No line count provided} diff --git a/test/linting/lint.sh b/test/linting/lint.sh index 032691e1..3bcb04cd 100755 --- a/test/linting/lint.sh +++ b/test/linting/lint.sh @@ -16,8 +16,7 @@ SHELLCHECK_VERSION='0.9.0' # shellcheck source=./../../target/scripts/helpers/log.sh source "${REPOSITORY_ROOT}/target/scripts/helpers/log.sh" -function _eclint -{ +function _eclint() { if docker run --rm --tty \ --volume "${REPOSITORY_ROOT}:/ci:ro" \ --workdir "/ci" \ @@ -31,8 +30,7 @@ function _eclint fi } -function _hadolint -{ +function _hadolint() { if docker run --rm --tty \ --volume "${REPOSITORY_ROOT}:/ci:ro" \ --workdir "/ci" \ @@ -46,8 +44,7 @@ function _hadolint fi } -function _shellcheck -{ +function _shellcheck() { local F_SH F_BIN F_BATS # File paths for shellcheck: @@ -119,8 +116,7 @@ function _shellcheck fi } -function _main -{ +function _main() { case "${1:-}" in ( 'eclint' ) _eclint ;; ( 'hadolint' ) _hadolint ;;