Files
e4907e15bc refactor: Helper function _replace_by_env_in_file() (#3526)
* fix: Helper `_replace_by_env_in_file()` support delimiter parameter

Minor change to add support for usage with `saslauthd.conf` which differs with `:` for it's key/value delimiter.

Also adopts the`_escape_for_sed()` method, instead of the inline sed pattern (_which for some reason escaped `=` but not `(` + `)`, thus buggy if ever matching on input with those tokens_).

`_escape_for_sed()` likewise wasn't escaping for `(` + `)` or even `|`, which are required for proper escape support if using `sed -E` / `sed -r` with this method. Additionally added escaping support for `&` replacement segment token, which seems valid. Increased verbosity to better grok pattern matching expression, clarified escaping concern with `sed` delimiter since the project is not consistent there.

* chore: Preserve K/V white-space of original source

No need to check for a non-empty value to prepend a space (_since an empty value is used via sed anyway?_).

Can also be a bit DRY with the sed pattern, matching the key + delimiter (_and all white-space before/after the delimiter until the value_), then capture that for the replacement left-side value while only actually swapping the value for the ENV input value.

Should be an improvement, unless there is a scenario that would differ between `` and ` ` as valid value assignments?

* chore: Adopt `sedfile` + case-insensitive pattern matching

Both should be improvements for usage

Co-authored-by: Casper <casperklein@users.noreply.github.com>

* fix: Harden `_replace_by_env_in_file()` and update tests

- Split each ENV line at the first `=` only, `IFS='=' read` dropped a trailing `=` from values (e.g. base64 padded passwords).
- Use `/` as the `sed` delimiter, as it is the only delimiter escaped by `_escape_for_sed()` (values with `#` broke the expression).
- Revert `sedfile` to `sed -i`: most prefixed ENV have no matching key, so an unchanged file is expected and `sedfile` would log spurious errors.
- Drop the unused key/value delimiter parameter.
- Avoid trailing white-space when the value is empty.
- Tests: Expect the retained white-space, add edge-cases for `sed` tokens and a trailing `=`, compare LDAP settings white-space insensitive.

Signed-off-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com>

---------

Signed-off-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com>
Co-authored-by: Casper <casperklein@users.noreply.github.com>
Co-authored-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com>
2026-10-17 18:29:54 +02:00

216 lines
8.6 KiB
Bash

#!/bin/bash
function _escape() {
echo "${1//./\\.}"
}
# 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.
#
# @param ${1} = string to escape
# @output = prints the escaped string
function _escape_for_sed() {
# 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}"
}
# Returns input after filtering out lines that are:
# empty, white-space, comments (`#` as the first non-whitespace character)
function _get_valid_lines_from_file() {
_convert_crlf_to_lf_if_necessary "${1}"
_append_final_newline_if_missing "${1}"
grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true
}
# This is to sanitize configs from users that unknowingly introduced CRLF:
function _convert_crlf_to_lf_if_necessary() {
if [[ $(file "${1}") =~ 'CRLF' ]]; then
_log 'warn' "File '${1}' contains CRLF line-endings"
if [[ -w ${1} ]]; then
_log 'debug' 'Converting CRLF to LF'
sed -i 's|\r||g' "${1}"
else
_log 'warn' "File '${1}' is not writable - cannot change CRLF to LF"
fi
fi
}
# This is to sanitize configs from users that unknowingly removed the end-of-file LF:
function _append_final_newline_if_missing() {
# Correctly detect a missing final newline and fix it:
# https://stackoverflow.com/questions/38746/how-to-detect-file-ends-in-newline#comment82380232_25749716
# https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file/441200#441200
# https://unix.stackexchange.com/questions/159557/how-to-non-invasively-test-for-write-access-to-a-file
if [[ $(tail -c1 "${1}" | wc -l) -eq 0 ]]; then
# Avoid fixing when the destination is read-only:
if [[ -w ${1} ]]; then
printf '\n' >> "${1}"
_log 'info' "File '${1}' was missing a final newline - this has been fixed"
else
_log 'warn' "File '${1}' is missing a final newline - it is not writable, hence it was not fixed - the last line will not be processed!"
fi
fi
}
# 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() {
if [[ -f /etc/dms-settings ]]; then
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
}
# 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).
function _chown_var_mail() {
# Fix ownership of mismatched paths within 3 levels of /var/mail, recursively.
_log 'trace' 'Fixing /var/mail permissions'
find /var/mail -maxdepth 3 \( \! -user "${DMS_VMAIL_UID}" -o \! -group "${DMS_VMAIL_GID}" \) -exec chown -R -- "${DMS_VMAIL_UID}:${DMS_VMAIL_GID}" {} +
}
function _require_n_parameters_or_print_usage() {
local COUNT
COUNT=${1}
shift
[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; }
[[ ${#} -lt ${COUNT} ]] && { __usage ; exit 1 ; }
return 0
}
# 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
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() {
_adjust_mtime_for_postfix_maincf
postfix reload
}
# 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 selecting 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_' '/etc/postfix/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
function _replace_by_env_in_file() {
if [[ -z ${1:-} ]]; then
_dms_panic__invalid_value 'first argument unset' 'utils.sh:_replace_by_env_in_file'
elif [[ -z ${2:-} ]]; then
_dms_panic__invalid_value 'second argument unset' 'utils.sh:_replace_by_env_in_file'
elif [[ ! -f ${2} ]]; then
_dms_panic__invalid_value "file '${2}' does not exist" 'utils.sh:_replace_by_env_in_file'
fi
local ENV_PREFIX=${1} CONFIG_FILE=${2}
local ENV_LINE KEY VALUE ESCAPED_KEY ESCAPED_VALUE SEPARATOR
# Split at the first `=` only, `read` with `IFS='='` would drop a trailing `=` from the value:
while IFS= read -r ENV_LINE; do
KEY=${ENV_LINE%%=*}
VALUE=${ENV_LINE#*=}
KEY=${KEY#"${ENV_PREFIX}"} # strip prefix
ESCAPED_KEY=$(_escape_for_sed "${KEY,,}")
ESCAPED_VALUE=$(_escape_for_sed "${VALUE}")
# Retain the original white-space around `=`, unless the value is empty (avoids trailing white-space):
SEPARATOR='[[:space:]]*'
[[ -z ${VALUE} ]] && SEPARATOR=''
_log 'trace' "Setting value of '${KEY}' in '${CONFIG_FILE}' to '${VALUE}'"
# NOTE: The `/` delimiter is required, as it is the only delimiter escaped by `_escape_for_sed()`.
sed -i -E "s/^(${ESCAPED_KEY}[[:space:]]*=${SEPARATOR}).*/\1${ESCAPED_VALUE}/I" "${CONFIG_FILE}"
done < <(env | grep "^${ENV_PREFIX}")
}
# 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
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}
if [[ ! -v ${ENV_VAR_NAME} ]]; then
_log 'warn' "'${ENV_VAR_NAME}' is not set, but was expected to be"
return 1
fi
if [[ ! ${!ENV_VAR_NAME} =~ ^(0|1)$ ]]; then
_log 'warn' "The value of '${ENV_VAR_NAME}' (= '${!ENV_VAR_NAME}') is not 0 or 1, but was expected to be"
return 1
fi
return 0
}
# 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
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
_log 'warn' "The value of '${ENV_VAR_NAME}' is not an integer ('${!ENV_VAR_NAME}'), but was expected to be"
return 1
}
function _reload_rspamd() {
_log 'debug' "Reloading configuration for Rspamd via sending 'SIGHUP'"
supervisorctl signal SIGHUP rspamd
}