chore(housekeeping): Normalize how config files filter out unwanted lines (#2619)

* chore(`aliases.sh`): Filepath to local var `DATABASE_VIRTUAL`

* chore(`accounts.sh`): Filepath to local var `DATABASE_ACCOUNTS`

* chore(`accounts.sh`): Filepath to local var `DATABASE_VIRTUAL`

* chore(`accounts.sh`): Filepath to local var `DATABASE_DOVECOT_MASTERS`

* chore(`bin/open-dkim`): Filepaths to local vars (accounts,virtual,vhost)

* chore(`relay.sh`): Filepath to local var `DATABASE_SASL_PASSWD`

* chore: Rename method

Prior PR feedback suggested a better helper method name.

* chore: Normalize filtering config lines as input for iterating

* chore: Remove `_is_comment` helper method

No longer serving a purpose with more appropriate filter method for pre-processing the entire config file.
This commit is contained in:
Brennan Kinney 2022-06-07 01:07:30 +12:00 committed by GitHub
parent fa8bfdc22a
commit 54904aa02c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 51 additions and 60 deletions

View file

@ -8,5 +8,5 @@ DATABASE='/tmp/docker-mailserver/postfix-virtual.cf'
[[ -f ${DATABASE} ]] || _exit_with_error "No 'postfix-virtual.cf' file" [[ -f ${DATABASE} ]] || _exit_with_error "No 'postfix-virtual.cf' file"
[[ -s ${DATABASE} ]] || _exit_with_error "Empty 'postfix-virtual.cf' - no aliases have been added" [[ -s ${DATABASE} ]] || _exit_with_error "Empty 'postfix-virtual.cf' - no aliases have been added"
grep -v "^\s*$\|^\s*\#" "${DATABASE}" _get_valid_lines_from_file "${DATABASE}"
exit 0 exit 0

View file

@ -16,6 +16,6 @@ while read -r LINE
do do
USER=$(echo "${LINE}" | cut -d'|' -f1) USER=$(echo "${LINE}" | cut -d'|' -f1)
echo "* ${USER}" echo "* ${USER}"
done < "${DATABASE}" done < <(_get_valid_lines_from_file "${DATABASE}")
exit 0 exit 0

View file

@ -47,7 +47,7 @@ do
else else
echo echo
fi fi
done < <(_filter_to_valid_lines "${DATABASE}") done < <(_get_valid_lines_from_file "${DATABASE}")
exit 0 exit 0

View file

@ -98,23 +98,26 @@ do
esac esac
done done
touch /tmp/vhost.dkim.tmp DATABASE_ACCOUNTS='/tmp/docker-mailserver/postfix-accounts.cf'
DATABASE_VIRTUAL='/tmp/docker-mailserver/postfix-virtual.cf'
DATABASE_VHOST='/tmp/vhost'
TMP_VHOST='/tmp/vhost.dkim.tmp'
touch "${TMP_VHOST}"
if [[ -z ${DOMAINS} ]] if [[ -z ${DOMAINS} ]]
then then
# getting domains FROM mail accounts # getting domains FROM mail accounts
if [[ -f /tmp/docker-mailserver/postfix-accounts.cf ]] if [[ -f ${DATABASE_ACCOUNTS} ]]
then then
# shellcheck disable=SC2034 # shellcheck disable=SC2034
while IFS=$'|' read -r LOGIN PASS while IFS=$'|' read -r LOGIN PASS
do do
DOMAIN=$(echo "${LOGIN}" | cut -d @ -f2) DOMAIN=$(echo "${LOGIN}" | cut -d @ -f2)
echo "${DOMAIN}" >>/tmp/vhost.dkim.tmp echo "${DOMAIN}" >>"${TMP_VHOST}"
done < <(grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-accounts.cf || true) done < <(_get_valid_lines_from_file "${DATABASE_ACCOUNTS}")
fi fi
# getting domains FROM mail aliases # getting domains FROM mail aliases
if [[ -f /tmp/docker-mailserver/postfix-virtual.cf ]] if [[ -f ${DATABASE_VIRTUAL} ]]
then then
# shellcheck disable=SC2034 # shellcheck disable=SC2034
while read -r FROM TO while read -r FROM TO
@ -122,17 +125,17 @@ then
UNAME=$(echo "${FROM}" | cut -d @ -f1) UNAME=$(echo "${FROM}" | cut -d @ -f1)
DOMAIN=$(echo "${FROM}" | cut -d @ -f2) DOMAIN=$(echo "${FROM}" | cut -d @ -f2)
[[ ${UNAME} != "${DOMAIN}" ]] && echo "${DOMAIN}" >>/tmp/vhost.dkim.tmp [[ ${UNAME} != "${DOMAIN}" ]] && echo "${DOMAIN}" >>"${TMP_VHOST}"
done < <(grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-virtual.cf || true) done < <(_get_valid_lines_from_file "${DATABASE_VIRTUAL}")
fi fi
else else
tr ',' '\n' <<< "${DOMAINS}" >/tmp/vhost.dkim.tmp tr ',' '\n' <<< "${DOMAINS}" >"${TMP_VHOST}"
fi fi
sort < /tmp/vhost.dkim.tmp | uniq >/tmp/vhost sort < "${TMP_VHOST}" | uniq >"${DATABASE_VHOST}"
rm /tmp/vhost.dkim.tmp rm "${TMP_VHOST}"
if [[ ! -s /tmp/vhost ]] if [[ ! -s ${DATABASE_VHOST} ]]
then then
_log 'warn' 'No entries found, no keys to make' _log 'warn' 'No entries found, no keys to make'
exit 0 exit 0
@ -179,7 +182,7 @@ do
echo "${SIGNINGTABLEENTRY}" >>/tmp/docker-mailserver/opendkim/SigningTable echo "${SIGNINGTABLEENTRY}" >>/tmp/docker-mailserver/opendkim/SigningTable
fi fi
fi fi
done < <(grep -vE '^(\s*$|#)' /tmp/vhost) done < <(_get_valid_lines_from_file "${DATABASE_VHOST}")
# create TrustedHosts if missing # create TrustedHosts if missing
if [[ -d /tmp/docker-mailserver/opendkim ]] && [[ ! -f /tmp/docker-mailserver/opendkim/TrustedHosts ]] if [[ -d /tmp/docker-mailserver/opendkim ]] && [[ ! -f /tmp/docker-mailserver/opendkim/TrustedHosts ]]

View file

@ -16,18 +16,19 @@ function _create_accounts
[[ ${ENABLE_LDAP} -eq 1 ]] && return 0 [[ ${ENABLE_LDAP} -eq 1 ]] && return 0
local DATABASE_ACCOUNTS='/tmp/docker-mailserver/postfix-accounts.cf'
_create_masters _create_masters
if [[ -f /tmp/docker-mailserver/postfix-accounts.cf ]] if [[ -f ${DATABASE_ACCOUNTS} ]]
then then
_log 'trace' "Checking file line endings" _log 'trace' "Checking file line endings"
sed -i 's|\r||g' /tmp/docker-mailserver/postfix-accounts.cf sed -i 's|\r||g' "${DATABASE_ACCOUNTS}"
_log 'trace' "Regenerating postfix user list" _log 'trace' "Regenerating postfix user list"
echo "# WARNING: this file is auto-generated. Modify /tmp/docker-mailserver/postfix-accounts.cf to edit the user list." > /etc/postfix/vmailbox echo "# WARNING: this file is auto-generated. Modify ${DATABASE_ACCOUNTS} to edit the user list." > /etc/postfix/vmailbox
# checking that /tmp/docker-mailserver/postfix-accounts.cf ends with a newline # checking that ${DATABASE_ACCOUNTS} ends with a newline
# shellcheck disable=SC1003 # shellcheck disable=SC1003
sed -i -e '$a\' /tmp/docker-mailserver/postfix-accounts.cf sed -i -e '$a\' "${DATABASE_ACCOUNTS}"
chown dovecot:dovecot "${DOVECOT_USERDB_FILE}" chown dovecot:dovecot "${DOVECOT_USERDB_FILE}"
chmod 640 "${DOVECOT_USERDB_FILE}" chmod 640 "${DOVECOT_USERDB_FILE}"
@ -92,7 +93,7 @@ function _create_accounts
fi fi
echo "${DOMAIN}" >>/tmp/vhost.tmp echo "${DOMAIN}" >>/tmp/vhost.tmp
done < <(grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-accounts.cf) done < <(_get_valid_lines_from_file "${DATABASE_ACCOUNTS}")
_create_dovecot_alias_dummy_accounts _create_dovecot_alias_dummy_accounts
fi fi
@ -107,16 +108,15 @@ function _create_accounts
# for more details on this method # for more details on this method
function _create_dovecot_alias_dummy_accounts function _create_dovecot_alias_dummy_accounts
{ {
if [[ -f /tmp/docker-mailserver/postfix-virtual.cf ]] && [[ ${ENABLE_QUOTAS} -eq 1 ]] local DATABASE_VIRTUAL='/tmp/docker-mailserver/postfix-virtual.cf'
if [[ -f ${DATABASE_VIRTUAL} ]] && [[ ${ENABLE_QUOTAS} -eq 1 ]]
then then
# adding aliases to Dovecot's userdb # adding aliases to Dovecot's userdb
# ${REAL_FQUN} is a user's fully-qualified username # ${REAL_FQUN} is a user's fully-qualified username
local ALIAS REAL_FQUN DOVECOT_USERDB_LINE local ALIAS REAL_FQUN DOVECOT_USERDB_LINE
while read -r ALIAS REAL_FQUN while read -r ALIAS REAL_FQUN
do do
# ignore comments
[[ ${ALIAS} == \#* ]] && continue
# alias is assumed to not be a proper e-mail # alias is assumed to not be a proper e-mail
# these aliases do not need to be added to Dovecot's userdb # these aliases do not need to be added to Dovecot's userdb
[[ ! ${ALIAS} == *@* ]] && continue [[ ! ${ALIAS} == *@* ]] && continue
@ -130,7 +130,7 @@ function _create_dovecot_alias_dummy_accounts
REAL_USERNAME=$(cut -d '@' -f 1 <<< "${REAL_FQUN}") REAL_USERNAME=$(cut -d '@' -f 1 <<< "${REAL_FQUN}")
REAL_DOMAINNAME=$(cut -d '@' -f 2 <<< "${REAL_FQUN}") REAL_DOMAINNAME=$(cut -d '@' -f 2 <<< "${REAL_FQUN}")
if ! grep -q "${REAL_FQUN}" /tmp/docker-mailserver/postfix-accounts.cf if ! grep -q "${REAL_FQUN}" "${DATABASE_ACCOUNTS}"
then then
_log 'debug' "Alias '${ALIAS}' is non-local (or mapped to a non-existing account) and will not be added to Dovecot's userdb" _log 'debug' "Alias '${ALIAS}' is non-local (or mapped to a non-existing account) and will not be added to Dovecot's userdb"
continue continue
@ -141,7 +141,7 @@ function _create_dovecot_alias_dummy_accounts
# ${REAL_ACC[0]} => real account name (e-mail address) == ${REAL_FQUN} # ${REAL_ACC[0]} => real account name (e-mail address) == ${REAL_FQUN}
# ${REAL_ACC[1]} => password hash # ${REAL_ACC[1]} => password hash
# ${REAL_ACC[2]} => optional user attributes # ${REAL_ACC[2]} => optional user attributes
IFS='|' read -r -a REAL_ACC < <(grep "${REAL_FQUN}" /tmp/docker-mailserver/postfix-accounts.cf) IFS='|' read -r -a REAL_ACC < <(grep "${REAL_FQUN}" "${DATABASE_ACCOUNTS}")
if [[ -z ${REAL_ACC[1]} ]] if [[ -z ${REAL_ACC[1]} ]]
then then
@ -165,7 +165,7 @@ function _create_dovecot_alias_dummy_accounts
else else
echo "${DOVECOT_USERDB_LINE}" >>"${DOVECOT_USERDB_FILE}" echo "${DOVECOT_USERDB_LINE}" >>"${DOVECOT_USERDB_FILE}"
fi fi
done < /tmp/docker-mailserver/postfix-virtual.cf done < <(_get_valid_lines_from_file "${DATABASE_VIRTUAL}")
fi fi
} }
@ -175,16 +175,17 @@ function _create_masters
{ {
: >"${DOVECOT_MASTERDB_FILE}" : >"${DOVECOT_MASTERDB_FILE}"
if [[ -f /tmp/docker-mailserver/dovecot-masters.cf ]] local DATABASE_DOVECOT_MASTERS='/tmp/docker-mailserver/dovecot-masters.cf'
if [[ -f ${DATABASE_DOVECOT_MASTERS} ]]
then then
_log 'trace' "Checking file line endings" _log 'trace' "Checking file line endings"
sed -i 's|\r||g' /tmp/docker-mailserver/dovecot-masters.cf sed -i 's|\r||g' "${DATABASE_DOVECOT_MASTERS}"
_log 'trace' "Regenerating dovecot masters list" _log 'trace' "Regenerating dovecot masters list"
# checking that /tmp/docker-mailserver/dovecot-masters.cf ends with a newline # checking that ${DATABASE_DOVECOT_MASTERS} ends with a newline
# shellcheck disable=SC1003 # shellcheck disable=SC1003
sed -i -e '$a\' /tmp/docker-mailserver/dovecot-masters.cf sed -i -e '$a\' "${DATABASE_DOVECOT_MASTERS}"
chown dovecot:dovecot "${DOVECOT_MASTERDB_FILE}" chown dovecot:dovecot "${DOVECOT_MASTERDB_FILE}"
chmod 640 "${DOVECOT_MASTERDB_FILE}" chmod 640 "${DOVECOT_MASTERDB_FILE}"
@ -209,7 +210,6 @@ function _create_masters
else else
echo "${DOVECOT_MASTERDB_LINE}" >>"${DOVECOT_MASTERDB_FILE}" echo "${DOVECOT_MASTERDB_LINE}" >>"${DOVECOT_MASTERDB_FILE}"
fi fi
done < <(_get_valid_lines_from_file "${DATABASE_DOVECOT_MASTERS}")
done < <(grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/dovecot-masters.cf)
fi fi
} }

View file

@ -11,15 +11,17 @@ function _handle_postfix_virtual_config
: >/etc/postfix/virtual : >/etc/postfix/virtual
: >/etc/postfix/regexp : >/etc/postfix/regexp
if [[ -f /tmp/docker-mailserver/postfix-virtual.cf ]] local DATABASE_VIRTUAL=/tmp/docker-mailserver/postfix-virtual.cf
if [[ -f ${DATABASE_VIRTUAL} ]]
then then
# fixing old virtual user file # fixing old virtual user file
if grep -q ",$" /tmp/docker-mailserver/postfix-virtual.cf if grep -q ",$" "${DATABASE_VIRTUAL}"
then then
sed -i -e "s|, |,|g" -e "s|,$||g" /tmp/docker-mailserver/postfix-virtual.cf sed -i -e "s|, |,|g" -e "s|,$||g" "${DATABASE_VIRTUAL}"
fi fi
cp -f /tmp/docker-mailserver/postfix-virtual.cf /etc/postfix/virtual cp -f "${DATABASE_VIRTUAL}" /etc/postfix/virtual
# the `to` is important, don't delete it # the `to` is important, don't delete it
# shellcheck disable=SC2034 # shellcheck disable=SC2034
@ -30,9 +32,9 @@ function _handle_postfix_virtual_config
# if they are equal it means the line looks like: "user1 other@domain.tld" # if they are equal it means the line looks like: "user1 other@domain.tld"
[[ ${UNAME} != "${DOMAIN}" ]] && echo "${DOMAIN}" >>/tmp/vhost.tmp [[ ${UNAME} != "${DOMAIN}" ]] && echo "${DOMAIN}" >>/tmp/vhost.tmp
done < <(grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-virtual.cf || true) done < <(_get_valid_lines_from_file "${DATABASE_VIRTUAL}")
else else
_log 'debug' "'/tmp/docker-mailserver/postfix-virtual.cf' not provided - no mail alias/forward created" _log 'debug' "'${DATABASE_VIRTUAL}' not provided - no mail alias/forward created"
fi fi
} }

View file

@ -92,16 +92,11 @@ function _relayhost_sasl
echo "${SASL_PASSWD}" >> /etc/postfix/sasl_passwd echo "${SASL_PASSWD}" >> /etc/postfix/sasl_passwd
fi fi
if [[ -f /tmp/docker-mailserver/postfix-sasl-password.cf ]] local DATABASE_SASL_PASSWD='/tmp/docker-mailserver/postfix-sasl-password.cf'
if [[ -f ${DATABASE_SASL_PASSWD} ]]
then then
# Add domain-specific auth from config file: # Add domain-specific auth from config file:
while read -r LINE _get_valid_lines_from_file "${DATABASE_SASL_PASSWD}" >> /etc/postfix/sasl_passwd
do
if ! _is_comment "${LINE}"
then
echo "${LINE}" >> /etc/postfix/sasl_passwd
fi
done < /tmp/docker-mailserver/postfix-sasl-password.cf
# Only relevant when providing this user config (unless users append elsewhere too) # Only relevant when providing this user config (unless users append elsewhere too)
postconf 'smtp_sender_dependent_authentication = yes' postconf 'smtp_sender_dependent_authentication = yes'

View file

@ -7,19 +7,11 @@ function _escape
# Returns input after filtering out lines that are: # Returns input after filtering out lines that are:
# empty, white-space, comments (`#` as the first non-whitespace character) # empty, white-space, comments (`#` as the first non-whitespace character)
function _filter_to_valid_lines function _get_valid_lines_from_file
{ {
grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true
} }
# TODO: Only used by `relay.sh`, will be removed in future.
# Similar to _filter_to_valid_lines, but only returns a status code
# to indicate invalid line(s):
function _is_comment
{
grep -q -E "^\s*$|^\s*#" <<< "${1}"
}
# Provide the name of an environment variable to this function # Provide the name of an environment variable to this function
# and it will return its value stored in /etc/dms-settings # and it will return its value stored in /etc/dms-settings
function _get_dms_env_value function _get_dms_env_value

View file

@ -1162,8 +1162,7 @@ function _setup_fetchmail_parallel
# Just the server settings that need to be added to the specific rc.d file # Just the server settings that need to be added to the specific rc.d file
echo "${LINE}" >>"${FETCHMAILRCD}/fetchmail-${COUNTER}.rc" echo "${LINE}" >>"${FETCHMAILRCD}/fetchmail-${COUNTER}.rc"
fi fi
# delete commented lines before parsing done < <(_get_valid_lines_from_file "${FETCHMAILRC}")
done < <(sed '/^[[:space:]]*#/d' "${FETCHMAILRC}")
rm "${DEFAULT_FILE}" rm "${DEFAULT_FILE}"
} }