2022-10-17 10:40:09 +02:00
|
|
|
#!/bin/bash
|
2022-02-21 11:56:57 +01:00
|
|
|
|
2023-05-26 01:01:41 +02:00
|
|
|
function _escape() {
|
2022-02-21 11:56:57 +01:00
|
|
|
echo "${1//./\\.}"
|
|
|
|
}
|
|
|
|
|
2023-09-04 11:24:10 +12:00
|
|
|
# Replaces a string so that it can be used inside the `sed` regex segment safely.
|
|
|
|
# WARNING: Only for use with `sed -E` / `sed -r` due to escaping additional tokens,
|
|
|
|
# which are valid tokens when escaped in basic regex mode.
|
2023-08-22 10:03:41 +02:00
|
|
|
#
|
|
|
|
# @param ${1} = string to escape
|
|
|
|
# @output = prints the escaped string
|
|
|
|
function _escape_for_sed() {
|
2023-09-04 11:24:10 +12:00
|
|
|
# Escapes all special tokens:
|
|
|
|
# - `/` should represent the sed delimiter (caution when using sed with a non-default delimiter).
|
|
|
|
# - `]` should be the first char, while `.` must not be the 2nd.
|
|
|
|
# - Within the `[ ... ]` scope, no escaping is needed regardless of basic vs extended regexp mode.
|
|
|
|
local REGEX_BASIC='][/\$*.^'
|
|
|
|
# In this mode (`-E` / `-r`), these tokens no longer require a preceding `\`, so must also be escaped:
|
|
|
|
local REGEX_EXTENDED='|?)(}{+'
|
|
|
|
# The replacement segment is compatible but most tokens do not require escaping.
|
|
|
|
# `&` is a token for this segment, and is compatible with escaping in the regex segment:
|
|
|
|
local TOKEN_REPLACEMENT='&'
|
|
|
|
local REGEX_SEGMENT="${REGEX_BASIC}${REGEX_EXTENDED}${TOKEN_REPLACEMENT}"
|
|
|
|
|
|
|
|
# Output: `\\` prepends a `\` to a token matched by the regex segment (`&`)
|
|
|
|
local PREPEND_ESCAPE='\\&'
|
|
|
|
|
|
|
|
# Full sed expression: sed -E 's/[][/\$*.^|?)(}{+&]/\\&/g'
|
|
|
|
sed -E "s/[${REGEX_SEGMENT}]/${PREPEND_ESCAPE}/g" <<< "${1:?String to escape for sed is required}"
|
2023-08-22 10:03:41 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:02:52 +12:00
|
|
|
# Returns input after filtering out lines that are:
|
|
|
|
# empty, white-space, comments (`#` as the first non-whitespace character)
|
2023-05-26 01:01:41 +02:00
|
|
|
function _get_valid_lines_from_file() {
|
2022-06-06 11:02:52 +12:00
|
|
|
grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:10:01 +02:00
|
|
|
# Provide the name of an environment variable to this function
|
|
|
|
# and it will return its value stored in /etc/dms-settings
|
2023-05-26 01:01:41 +02:00
|
|
|
function _get_dms_env_value() {
|
2023-05-24 09:06:59 +02:00
|
|
|
if [[ -f /etc/dms-settings ]]; then
|
2023-04-24 14:35:19 +02: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 17:10:01 +02:00
|
|
|
}
|
2022-06-08 10:09:19 +12: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-26 01:01:41 +02:00
|
|
|
function _chown_var_mail_if_necessary() {
|
2022-06-08 10:09:19 +12:00
|
|
|
# fix permissions, but skip this if 3 levels deep the user id is already set
|
2023-05-24 09:06:59 +02:00
|
|
|
if find /var/mail -maxdepth 3 -a \( \! -user 5000 -o \! -group 5000 \) | read -r; then
|
2022-06-08 10:09:19 +12:00
|
|
|
_log 'trace' 'Fixing /var/mail permissions'
|
|
|
|
chown -R 5000:5000 /var/mail || return 1
|
|
|
|
fi
|
|
|
|
}
|
2022-10-29 11:04:35 +02:00
|
|
|
|
2023-05-26 01:01:41 +02:00
|
|
|
function _require_n_parameters_or_print_usage() {
|
2022-10-29 11:04:35 +02:00
|
|
|
local COUNT
|
|
|
|
COUNT=${1}
|
|
|
|
shift
|
|
|
|
|
|
|
|
[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }
|
|
|
|
[[ ${#} -lt ${COUNT} ]] && { __usage ; exit 1 ; }
|
2023-04-24 14:35:19 +02:00
|
|
|
return 0
|
2022-10-29 11:04:35 +02:00
|
|
|
}
|
2023-01-13 10:10:58 +13: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-26 01:01:41 +02:00
|
|
|
function _adjust_mtime_for_postfix_maincf() {
|
2023-05-24 09:06:59 +02:00
|
|
|
if [[ $(( $(date '+%s') - $(stat -c '%Y' '/etc/postfix/main.cf') )) -lt 2 ]]; then
|
2023-01-13 10:10:58 +13:00
|
|
|
touch -d '2 seconds ago' /etc/postfix/main.cf
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-05-26 01:01:41 +02:00
|
|
|
function _reload_postfix() {
|
2023-01-13 10:10:58 +13:00
|
|
|
_adjust_mtime_for_postfix_maincf
|
|
|
|
postfix reload
|
|
|
|
}
|
2023-02-05 12:39:05 +01: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
|
2023-09-04 11:24:10 +12:00
|
|
|
# @param ${1} = Prefix for selecting environment variables
|
|
|
|
# @param ${2} = File in which substitutions should take place
|
|
|
|
# @param ${3} = The key/value assignment operator used (default: `=`) [OPTIONAL]
|
2023-02-05 12:39:05 +01:00
|
|
|
#
|
|
|
|
# ## 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:
|
2023-09-04 11:24:10 +12:00
|
|
|
# `_replace_by_env_in_file 'POSTFIX_' '/etc/postfix/main.cf`
|
2023-02-05 12:39:05 +01:00
|
|
|
#
|
|
|
|
# ## 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-26 01:01:41 +02:00
|
|
|
function _replace_by_env_in_file() {
|
2023-05-24 09:06:59 +02:00
|
|
|
if [[ -z ${1+set} ]]; then
|
2023-04-18 23:38:46 +02:00
|
|
|
_dms_panic__invalid_value 'first argument unset' 'utils.sh:_replace_by_env_in_file'
|
2023-05-24 09:06:59 +02:00
|
|
|
elif [[ -z ${2+set} ]]; then
|
2023-04-18 23:38:46 +02:00
|
|
|
_dms_panic__invalid_value 'second argument unset' 'utils.sh:_replace_by_env_in_file'
|
2023-05-24 09:06:59 +02:00
|
|
|
elif [[ ! -f ${2} ]]; then
|
2023-04-18 23:38:46 +02:00
|
|
|
_dms_panic__invalid_value "file '${2}' does not exist" 'utils.sh:_replace_by_env_in_file'
|
2023-02-05 12:39:05 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
local ENV_PREFIX=${1} CONFIG_FILE=${2}
|
2023-09-04 11:24:10 +12:00
|
|
|
local KV_DELIMITER=${3:-'='}
|
2023-02-05 12:39:05 +01:00
|
|
|
local ESCAPED_VALUE ESCAPED_KEY
|
|
|
|
|
2023-09-04 11:24:10 +12:00
|
|
|
while IFS="${KV_DELIMITER}" read -r KEY VALUE; do
|
2023-02-05 12:39:05 +01:00
|
|
|
KEY=${KEY#"${ENV_PREFIX}"} # strip prefix
|
2023-09-04 11:24:10 +12:00
|
|
|
ESCAPED_KEY=$(_escape_for_sed "${KEY,,}")
|
|
|
|
ESCAPED_VALUE=$(_escape_for_sed "${VALUE}")
|
2023-02-05 12:39:05 +01:00
|
|
|
[[ -n ${ESCAPED_VALUE} ]] && ESCAPED_VALUE=" ${ESCAPED_VALUE}"
|
|
|
|
_log 'trace' "Setting value of '${KEY}' in '${CONFIG_FILE}' to '${VALUE}'"
|
2023-09-04 11:24:10 +12:00
|
|
|
sed -i -E "s#^${ESCAPED_KEY}[[:space:]]*${KV_DELIMITER}.*#${ESCAPED_KEY} ${KV_DELIMITER}${ESCAPED_VALUE}#g" "${CONFIG_FILE}"
|
2023-02-05 12:39:05 +01:00
|
|
|
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 12:22:54 +02: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-26 01:01:41 +02: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 12:22:54 +02: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-26 01:01:41 +02: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 12:22:54 +02: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
|
|
|
|
}
|