From 698ae03505383e59943eba992d05dd8c3b8a84e3 Mon Sep 17 00:00:00 2001 From: Joshua Tauberer Date: Sat, 27 Sep 2014 11:39:50 +0000 Subject: [PATCH] catch-all addresses should not have precedence over mail users Aliases have precedence over mail users. A catch-all address would grab mail intended for a mail user and send it elsewhere. This adds some SQL hackery to create dummy aliases for all mail users. fixes #200 closes #214 another way --- setup/mail-users.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/setup/mail-users.sh b/setup/mail-users.sh index e5289057..d18b6e04 100755 --- a/setup/mail-users.sh +++ b/setup/mail-users.sh @@ -93,9 +93,16 @@ query = SELECT 1 FROM users WHERE email='%s' EOF # SQL statement to rewrite an email address if an alias is present. +# Aliases have precedence over users, but that's counter-intuitive for +# catch-all aliases ("@domain.com") which should *not* catch mail users. +# To fix this, not only query the aliases table but also the users +# table, i.e. turn users into aliases from themselves to themselves. +# If there is both an alias and a user for the same address either +# might be returned by the UNION, so the whole query is wrapped in +# another select that prioritizes the alias definition. cat > /etc/postfix/virtual-alias-maps.cf << EOF; dbpath=$db_path -query = SELECT destination FROM aliases WHERE source='%s' +query = SELECT destination from (SELECT destination, 0 as priority FROM aliases WHERE source='%s' UNION SELECT email as destination, 1 as priority FROM users WHERE email='%s') ORDER BY priority LIMIT 1; EOF # Restart Services