Files
Georg Lauterbach ace208c9c9 fix(postfix): deliver mail to accounts on a domain with a catch-all alias (#4860)
* fix(postfix): deliver mail to accounts on a domain with a catch-all alias

Postfix resolves `virtual_alias_maps` before `virtual_mailbox_maps`, so a
catch-all alias (`@example.com`) in `postfix-virtual.cf` also captured the
mail of every account on that domain. The documented workaround (an alias
to itself, `addr addr`) cannot be created with `setup alias add`, as the
CLI rejects an alias that is also an account.

Add that alias for each account of a catch-all domain to the generated
`/etc/postfix/virtual`, at startup and when `postfix-accounts.cf` changes.
`postfix-virtual.cf` is left untouched, so the CLI guard stays, the Dovecot
quota dummy accounts are unaffected, and entries added by hand are kept.
Only applies to `ACCOUNT_PROVISIONER=FILE`.

Also fix the overlap checks of `setup alias add` and `setup email add`:
they used a prefix `grep`, so `user@example.com` was rejected when
`user@example.com.au` existed. They now use `_db_has_entry_with_key`,
which compares the whole address.

Closes #4856
2026-11-18 14:48:00 +01:00

50 lines
2.1 KiB
Bash

#!/bin/bash
# Manage DB writes for: DATABASE_VIRTUAL
# A virtual alias may be any of `user@domain`, `user`, `@domain`.
# Recipients are local (internal services), hosted (managed accounts), remote (third-party MTA), or aliases themselves,
# An alias may redirect mail to one or more recipients. If a recipient is an alias Postfix will recursively resolve it.
#
# WARNING: Support for multiple and recursive recipients may not be well supported by this projects scripts/features.
# One of those features is Dovecot Quota support, which uses a naive workaround for supporting quota checks for inbound
# mail to an alias address.
# Logic to perform for requested operations handled here:
function _manage_virtual_aliases() {
local ACTION=${1}
local MAIL_ALIAS=${2}
local RECIPIENT=${3}
# Validation error handling expects that the caller has defined a '__usage' method:
[[ -z ${MAIL_ALIAS} ]] && { __usage ; _exit_with_error 'No alias specified' ; }
[[ -z ${RECIPIENT} ]] && { __usage ; _exit_with_error 'No recipient specified' ; }
local DATABASE_VIRTUAL='/tmp/docker-mailserver/postfix-virtual.cf'
case "${ACTION}" in
# Associate RECIPIENT to MAIL_ALIAS:
( 'update' )
if _db_has_entry_with_key "${MAIL_ALIAS}" "${DATABASE_ACCOUNTS}"; then
_exit_with_error "'${MAIL_ALIAS}' is already defined as an account"
fi
_db_entry_add_or_append "${DATABASE_VIRTUAL}" "${MAIL_ALIAS}" "${RECIPIENT}"
;;
# Removes RECIPIENT from MAIL_ALIAS - or all aliases when MAIL_ALIAS='_':
# NOTE: If a matched alias has no additional recipients, it is also removed.
( 'delete' )
[[ ${MAIL_ALIAS} == '_' ]] && MAIL_ALIAS='\S\+'
_db_entry_remove "${DATABASE_VIRTUAL}" "${MAIL_ALIAS}" "${RECIPIENT}"
;;
( * ) # This should not happen if using convenience wrapper methods:
_exit_with_error "Unsupported Action: '${ACTION}'"
;;
esac
}
# Convenience wrappers:
function _manage_virtual_aliases_update { _manage_virtual_aliases 'update' "${@}" ; } # addalias
function _manage_virtual_aliases_delete { _manage_virtual_aliases 'delete' "${@}" ; } # delalias, delmailuser