2021-11-20 20:33:49 +00:00
|
|
|
#! /bin/bash
|
|
|
|
# Support for Postfix aliases
|
|
|
|
|
|
|
|
# NOTE: LDAP doesn't appear to use this, but the docs page: "Use Cases | Forward-Only Mail-Server with LDAP"
|
|
|
|
# does have an example where /etc/postfix/virtual is referenced in addition to ldap config for Postfix `main.cf:virtual_alias_maps`.
|
|
|
|
# `setup-stack.sh:_setup_ldap` does not seem to configure for `/etc/postfix/virtual however.`
|
|
|
|
|
|
|
|
# NOTE: `accounts.sh` and `relay.sh:_populate_relayhost_map` also process on `postfix-virtual.cf`.
|
|
|
|
function _handle_postfix_virtual_config
|
|
|
|
{
|
|
|
|
: >/etc/postfix/virtual
|
|
|
|
: >/etc/postfix/regexp
|
|
|
|
|
|
|
|
if [[ -f /tmp/docker-mailserver/postfix-virtual.cf ]]
|
|
|
|
then
|
|
|
|
# fixing old virtual user file
|
|
|
|
if grep -q ",$" /tmp/docker-mailserver/postfix-virtual.cf
|
|
|
|
then
|
|
|
|
sed -i -e "s|, |,|g" -e "s|,$||g" /tmp/docker-mailserver/postfix-virtual.cf
|
|
|
|
fi
|
|
|
|
|
|
|
|
cp -f /tmp/docker-mailserver/postfix-virtual.cf /etc/postfix/virtual
|
|
|
|
|
|
|
|
# the `to` is important, don't delete it
|
|
|
|
# shellcheck disable=SC2034
|
|
|
|
while read -r FROM TO
|
|
|
|
do
|
|
|
|
UNAME=$(echo "${FROM}" | cut -d @ -f1)
|
|
|
|
DOMAIN=$(echo "${FROM}" | cut -d @ -f2)
|
|
|
|
|
|
|
|
# if they are equal it means the line looks like: "user1 other@domain.tld"
|
|
|
|
[[ ${UNAME} != "${DOMAIN}" ]] && echo "${DOMAIN}" >>/tmp/vhost.tmp
|
|
|
|
done < <(grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-virtual.cf || true)
|
|
|
|
else
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'debug' "'/tmp/docker-mailserver/postfix-virtual.cf' not provided - no mail alias/forward created"
|
2021-11-20 20:33:49 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function _handle_postfix_regexp_config
|
|
|
|
{
|
|
|
|
if [[ -f /tmp/docker-mailserver/postfix-regexp.cf ]]
|
|
|
|
then
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'trace' "Adding regexp alias file postfix-regexp.cf"
|
2021-11-20 20:33:49 +00:00
|
|
|
|
|
|
|
cp -f /tmp/docker-mailserver/postfix-regexp.cf /etc/postfix/regexp
|
2022-02-11 20:20:45 +00:00
|
|
|
|
|
|
|
if ! grep 'virtual_alias_maps.*pcre:/etc/postfix/regexp' /etc/postfix/main.cf
|
|
|
|
then
|
|
|
|
sed -i -E \
|
|
|
|
's|virtual_alias_maps(.*)|virtual_alias_maps\1 pcre:/etc/postfix/regexp|g' \
|
|
|
|
/etc/postfix/main.cf
|
|
|
|
fi
|
2021-11-20 20:33:49 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function _handle_postfix_aliases_config
|
|
|
|
{
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'trace' 'Configuring root alias'
|
2021-11-20 20:33:49 +00:00
|
|
|
|
2022-02-11 20:20:45 +00:00
|
|
|
echo "root: ${POSTMASTER_ADDRESS}" >/etc/aliases
|
2021-11-20 20:33:49 +00:00
|
|
|
|
|
|
|
if [[ -f /tmp/docker-mailserver/postfix-aliases.cf ]]
|
|
|
|
then
|
|
|
|
cat /tmp/docker-mailserver/postfix-aliases.cf >>/etc/aliases
|
|
|
|
else
|
2022-03-21 06:07:52 +00:00
|
|
|
_log 'trace' "'/tmp/docker-mailserver/postfix-aliases.cf' is not provided, it will be auto created."
|
2021-11-20 20:33:49 +00:00
|
|
|
: >/tmp/docker-mailserver/postfix-aliases.cf
|
|
|
|
fi
|
|
|
|
|
|
|
|
postalias /etc/aliases
|
|
|
|
}
|
|
|
|
|
|
|
|
# Other scripts should call this method, rather than the ones above:
|
|
|
|
function _create_aliases
|
|
|
|
{
|
|
|
|
_handle_postfix_virtual_config
|
|
|
|
_handle_postfix_regexp_config
|
|
|
|
_handle_postfix_aliases_config
|
|
|
|
}
|