scripts: add wrapper to update Postfix configuration safely (follow up) (#3503)
This commit is contained in:
parent
f89cbac21c
commit
43a122fe18
|
@ -92,39 +92,43 @@ function _vhost_ldap_support() {
|
||||||
# /etc/aliases is handled by `alias.sh` and uses `postalias` to update the Postfix alias database. No need for `postmap`.
|
# /etc/aliases is handled by `alias.sh` and uses `postalias` to update the Postfix alias database. No need for `postmap`.
|
||||||
# http://www.postfix.org/postalias.1.html
|
# http://www.postfix.org/postalias.1.html
|
||||||
|
|
||||||
# Add an key with an value to Postfix's main configuration file
|
# Add a key with a value to Postfix's main configuration file
|
||||||
# or update an existing key. An already existing key can be updated
|
# or update an existing key. An already existing key can be updated
|
||||||
# by either appending to the existing value (default) or by prepending.
|
# by either appending to the existing value (default) or by prepending.
|
||||||
#
|
#
|
||||||
# @param ${1} = key name in Postfix's main configuration file
|
# @param ${1} = key name in Postfix's main configuration file
|
||||||
# @param ${2} = new value (appended or prepended)
|
# @param ${2} = new value (appended or prepended)
|
||||||
# @param ${3} = "append" (default) or "prepend" [OPTIONAL]
|
# @param ${3} = action "append" (default) or "prepend" [OPTIONAL]
|
||||||
function _add_to_or_update_postfix_main() {
|
function _add_to_or_update_postfix_main() {
|
||||||
local KEY=${1:?Key name is required}
|
local KEY=${1:?Key name is required}
|
||||||
local NEW_VALUE=${2:?New value is required}
|
local NEW_VALUE=${2:?New value is required}
|
||||||
local ACTION=${3:-append}
|
local ACTION=${3:-append}
|
||||||
|
local CURRENT_VALUE
|
||||||
|
|
||||||
# If entry does not exist, add it - otherwise update with ACTION:
|
# Get current value from /etc/postfix/main.cf
|
||||||
if ! grep -q -E "^${KEY}" /etc/postfix/main.cf; then
|
_adjust_mtime_for_postfix_maincf
|
||||||
|
CURRENT_VALUE=$(postconf -h "${KEY}" 2>/dev/null)
|
||||||
|
|
||||||
|
# If key does not exist or value is empty, add it - otherwise update with ACTION:
|
||||||
|
if [[ -z ${CURRENT_VALUE} ]]; then
|
||||||
postconf "${KEY} = ${NEW_VALUE}"
|
postconf "${KEY} = ${NEW_VALUE}"
|
||||||
else
|
else
|
||||||
KEY=$(_escape_for_sed "${KEY}")
|
# If $NEW_VALUE is already present --> nothing to do, skip.
|
||||||
NEW_VALUE=$(_escape_for_sed "${NEW_VALUE}")
|
if [[ " ${CURRENT_VALUE} " == *" ${NEW_VALUE} "* ]]; then
|
||||||
local SED_STRING
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
case "${ACTION}" in
|
case "${ACTION}" in
|
||||||
('append')
|
('append')
|
||||||
SED_STRING="/${NEW_VALUE}/! s|^(${KEY} *=.*)|\1 ${NEW_VALUE}|g"
|
postconf "${KEY} = ${CURRENT_VALUE} ${NEW_VALUE}"
|
||||||
;;
|
;;
|
||||||
('prepend')
|
('prepend')
|
||||||
SED_STRING="/${NEW_VALUE}/! s|^(${KEY}) *= *(.*)|\1 = ${NEW_VALUE} \2|g"
|
postconf "${KEY} = ${NEW_VALUE} ${CURRENT_VALUE}"
|
||||||
;;
|
;;
|
||||||
(*)
|
(*)
|
||||||
_log 'error' "Action '${3}' in _add_to_or_update_postfix_main is unknown"
|
_log 'error' "Action '${3}' in _add_to_or_update_postfix_main is unknown"
|
||||||
return 1
|
return 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
sed -i -E "${SED_STRING}" /etc/postfix/main.cf
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ function _escape() {
|
||||||
echo "${1//./\\.}"
|
echo "${1//./\\.}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# TODO: Not in use currently. Maybe in the future: https://github.com/docker-mailserver/docker-mailserver/pull/3484/files#r1299410851
|
||||||
# Replaces a string so that it can be used inside
|
# Replaces a string so that it can be used inside
|
||||||
# `sed` safely.
|
# `sed` safely.
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in New Issue