2022-10-17 08:40:09 +00:00
|
|
|
#!/bin/bash
|
2022-02-21 10:56:57 +00:00
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _escape() {
|
2022-02-21 10:56:57 +00:00
|
|
|
echo "${1//./\\.}"
|
|
|
|
}
|
|
|
|
|
2023-08-22 08:03:41 +00:00
|
|
|
# Replaces a string so that it can be used inside
|
|
|
|
# `sed` safely.
|
|
|
|
#
|
|
|
|
# @param ${1} = string to escape
|
|
|
|
# @output = prints the escaped string
|
|
|
|
function _escape_for_sed() {
|
|
|
|
sed -E 's/[]\/$*.^[]/\\&/g' <<< "${1:?String to escape for sed is required}"
|
|
|
|
}
|
|
|
|
|
2022-06-05 23:02:52 +00:00
|
|
|
# Returns input after filtering out lines that are:
|
|
|
|
# empty, white-space, comments (`#` as the first non-whitespace character)
|
2023-05-25 23:01:41 +00:00
|
|
|
function _get_valid_lines_from_file() {
|
2022-06-05 23:02:52 +00:00
|
|
|
grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true
|
|
|
|
}
|
|
|
|
|
2022-04-05 15:10:01 +00:00
|
|
|
# Provide the name of an environment variable to this function
|
|
|
|
# and it will return its value stored in /etc/dms-settings
|
2023-05-25 23:01:41 +00:00
|
|
|
function _get_dms_env_value() {
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ -f /etc/dms-settings ]]; then
|
2023-04-24 12:35:19 +00:00
|
|
|
grep "^${1}=" /etc/dms-settings | cut -d "'" -f 2
|
|
|
|
else
|
|
|
|
_log 'warn' "Call to '_get_dms_env_value' but '/etc/dms-settings' is not present"
|
|
|
|
return 1
|
|
|
|
fi
|
2022-04-05 15:10:01 +00:00
|
|
|
}
|
2022-06-07 22:09:19 +00:00
|
|
|
|
|
|
|
# TODO: `chown -R 5000:5000 /var/mail` has existed since the projects first commit.
|
|
|
|
# It later received a depth guard to apply the fix only when it's relevant for a dir.
|
|
|
|
# Assess if this still appropriate, it appears to be problematic for some LDAP users.
|
|
|
|
#
|
|
|
|
# `helpers/accounts.sh:_create_accounts` (mkdir, cp) appears to be the only writer to
|
|
|
|
# /var/mail folders (used during startup and change detection handling).
|
2023-05-25 23:01:41 +00:00
|
|
|
function _chown_var_mail_if_necessary() {
|
2022-06-07 22:09:19 +00:00
|
|
|
# fix permissions, but skip this if 3 levels deep the user id is already set
|
2023-05-24 07:06:59 +00:00
|
|
|
if find /var/mail -maxdepth 3 -a \( \! -user 5000 -o \! -group 5000 \) | read -r; then
|
2022-06-07 22:09:19 +00:00
|
|
|
_log 'trace' 'Fixing /var/mail permissions'
|
|
|
|
chown -R 5000:5000 /var/mail || return 1
|
|
|
|
fi
|
|
|
|
}
|
2022-10-29 09:04:35 +00:00
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _require_n_parameters_or_print_usage() {
|
2022-10-29 09:04:35 +00:00
|
|
|
local COUNT
|
|
|
|
COUNT=${1}
|
|
|
|
shift
|
|
|
|
|
|
|
|
[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }
|
|
|
|
[[ ${#} -lt ${COUNT} ]] && { __usage ; exit 1 ; }
|
2023-04-24 12:35:19 +00:00
|
|
|
return 0
|
2022-10-29 09:04:35 +00:00
|
|
|
}
|
2023-01-12 21:10:58 +00:00
|
|
|
|
|
|
|
# NOTE: Postfix commands that read `main.cf` will stall execution,
|
|
|
|
# until the config file has not be written to for at least 2 seconds.
|
|
|
|
# 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
|
2023-05-25 23:01:41 +00:00
|
|
|
function _adjust_mtime_for_postfix_maincf() {
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ $(( $(date '+%s') - $(stat -c '%Y' '/etc/postfix/main.cf') )) -lt 2 ]]; then
|
2023-01-12 21:10:58 +00:00
|
|
|
touch -d '2 seconds ago' /etc/postfix/main.cf
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function _reload_postfix() {
|
2023-01-12 21:10:58 +00:00
|
|
|
_adjust_mtime_for_postfix_maincf
|
|
|
|
postfix reload
|
|
|
|
}
|
2023-02-05 11:39:05 +00:00
|
|
|
|
|
|
|
# Replaces values in configuration files given a set of specific environment
|
|
|
|
# variables. The environment variables follow a naming pattern, whereby every
|
|
|
|
# variable that is taken into account has a given prefix. The new value in the
|
|
|
|
# configuration will be the one the environment variable had at the time of
|
|
|
|
# calling this function.
|
|
|
|
#
|
|
|
|
# @option --shutdown-on-error = shutdown in case an error is detected
|
|
|
|
# @param ${1} = prefix for environment variables
|
|
|
|
# @param ${2} = file in which substitutions should take place
|
|
|
|
#
|
|
|
|
# ## Example
|
|
|
|
#
|
|
|
|
# If you want to set a new value for `readme_directory` in Postfix's `main.cf`,
|
|
|
|
# you can set the environment variable `POSTFIX_README_DIRECTORY='/new/dir/'`
|
|
|
|
# (`POSTFIX_` is an arbitrary prefix, you can choose the one you like),
|
|
|
|
# and then call this function:
|
|
|
|
# `_replace_by_env_in_file 'POSTFIX_' 'PATH TO POSTFIX's main.cf>`
|
|
|
|
#
|
|
|
|
# ## Panics
|
|
|
|
#
|
|
|
|
# This function will panic, i.e. shut down the whole container, if:
|
|
|
|
#
|
|
|
|
# 1. No first and second argument is supplied
|
|
|
|
# 2. The second argument is a path to a file that does not exist
|
2023-05-25 23:01:41 +00:00
|
|
|
function _replace_by_env_in_file() {
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ -z ${1+set} ]]; then
|
2023-04-18 21:38:46 +00:00
|
|
|
_dms_panic__invalid_value 'first argument unset' 'utils.sh:_replace_by_env_in_file'
|
2023-05-24 07:06:59 +00:00
|
|
|
elif [[ -z ${2+set} ]]; then
|
2023-04-18 21:38:46 +00:00
|
|
|
_dms_panic__invalid_value 'second argument unset' 'utils.sh:_replace_by_env_in_file'
|
2023-05-24 07:06:59 +00:00
|
|
|
elif [[ ! -f ${2} ]]; then
|
2023-04-18 21:38:46 +00:00
|
|
|
_dms_panic__invalid_value "file '${2}' does not exist" 'utils.sh:_replace_by_env_in_file'
|
2023-02-05 11:39:05 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
local ENV_PREFIX=${1} CONFIG_FILE=${2}
|
|
|
|
local ESCAPED_VALUE ESCAPED_KEY
|
|
|
|
|
2023-05-25 23:39:39 +00:00
|
|
|
while IFS='=' read -r KEY VALUE; do
|
2023-02-05 11:39:05 +00:00
|
|
|
KEY=${KEY#"${ENV_PREFIX}"} # strip prefix
|
|
|
|
ESCAPED_KEY=$(sed -E 's#([\=\&\|\$\.\*\/\[\\^]|\])#\\\1#g' <<< "${KEY,,}")
|
|
|
|
ESCAPED_VALUE=$(sed -E 's#([\=\&\|\$\.\*\/\[\\^]|\])#\\\1#g' <<< "${VALUE}")
|
|
|
|
[[ -n ${ESCAPED_VALUE} ]] && ESCAPED_VALUE=" ${ESCAPED_VALUE}"
|
|
|
|
_log 'trace' "Setting value of '${KEY}' in '${CONFIG_FILE}' to '${VALUE}'"
|
|
|
|
sed -i -E "s#^${ESCAPED_KEY}[[:space:]]*=.*#${ESCAPED_KEY} =${ESCAPED_VALUE}#g" "${CONFIG_FILE}"
|
|
|
|
done < <(env | grep "^${ENV_PREFIX}")
|
|
|
|
}
|
scripts: Rspamd stabilization pt. 1 (#3261)
* added checks whether OpenDKIM/OpenDMARC/policyd-spf are enabled
* added functions to check if VAR is 0/0 or an int
and also added tests.
I also adjusted the test file to not run in a container, because there
is no need. This also decreases test time, which, in turn, increases
maintainers' happiness.
* added more checks to Rspamd setup
I added the helpers from the previous commit to the Rspamd setup to make
the whole setup more robust, and indicate to the user that an ENV
variable's value is incorrect.
While we did not issues for this in the past, I believe it to be
worthwhile for the future.
* added canonical directory for users to place files in
This dir is canonical with DMS's optional configuration dirs, as it
lives in well-known volume mounts. Hence, users will not need to adjust
`/etc/rspamd/override.d` manually anymore, or mount a volume to this
place.
The docs explain this now, but the DKIM page needs a slight update on
this too I guess. I will follow-up here.
* misc minor improvements
* use variables for common directories
2023-04-23 10:22:54 +00:00
|
|
|
|
|
|
|
# Check if an environment variable's value is zero or one. This aids in checking variables
|
|
|
|
# that act as "booleans" for enabling or disabling a service, configuration option, etc.
|
|
|
|
#
|
|
|
|
# This function will log a warning and return with exit code 1 in case the variable's value
|
|
|
|
# is not zero or one.
|
|
|
|
#
|
|
|
|
# @param ${1} = name of the ENV variable to check
|
2023-05-25 23:01:41 +00:00
|
|
|
function _env_var_expect_zero_or_one() {
|
scripts: Rspamd stabilization pt. 1 (#3261)
* added checks whether OpenDKIM/OpenDMARC/policyd-spf are enabled
* added functions to check if VAR is 0/0 or an int
and also added tests.
I also adjusted the test file to not run in a container, because there
is no need. This also decreases test time, which, in turn, increases
maintainers' happiness.
* added more checks to Rspamd setup
I added the helpers from the previous commit to the Rspamd setup to make
the whole setup more robust, and indicate to the user that an ENV
variable's value is incorrect.
While we did not issues for this in the past, I believe it to be
worthwhile for the future.
* added canonical directory for users to place files in
This dir is canonical with DMS's optional configuration dirs, as it
lives in well-known volume mounts. Hence, users will not need to adjust
`/etc/rspamd/override.d` manually anymore, or mount a volume to this
place.
The docs explain this now, but the DKIM page needs a slight update on
this too I guess. I will follow-up here.
* misc minor improvements
* use variables for common directories
2023-04-23 10:22:54 +00:00
|
|
|
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
|
|
|
|
_log 'warn' "The value of '${ENV_VAR_NAME}' is not zero or one ('${!ENV_VAR_NAME}'), but was expected to be"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if an environment variable's value is an integer.
|
|
|
|
#
|
|
|
|
# This function will log a warning and return with exit code 1 in case the variable's value
|
|
|
|
# is not an integer.
|
|
|
|
#
|
|
|
|
# @param ${1} = name of the ENV variable to check
|
2023-05-25 23:01:41 +00:00
|
|
|
function _env_var_expect_integer() {
|
scripts: Rspamd stabilization pt. 1 (#3261)
* added checks whether OpenDKIM/OpenDMARC/policyd-spf are enabled
* added functions to check if VAR is 0/0 or an int
and also added tests.
I also adjusted the test file to not run in a container, because there
is no need. This also decreases test time, which, in turn, increases
maintainers' happiness.
* added more checks to Rspamd setup
I added the helpers from the previous commit to the Rspamd setup to make
the whole setup more robust, and indicate to the user that an ENV
variable's value is incorrect.
While we did not issues for this in the past, I believe it to be
worthwhile for the future.
* added canonical directory for users to place files in
This dir is canonical with DMS's optional configuration dirs, as it
lives in well-known volume mounts. Hence, users will not need to adjust
`/etc/rspamd/override.d` manually anymore, or mount a volume to this
place.
The docs explain this now, but the DKIM page needs a slight update on
this too I guess. I will follow-up here.
* misc minor improvements
* use variables for common directories
2023-04-23 10:22:54 +00:00
|
|
|
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
|
|
|
|
_log 'warn' "The value of '${ENV_VAR_NAME}' is not an integer ('${!ENV_VAR_NAME}'), but was expected to be"
|
|
|
|
return 1
|
|
|
|
}
|