No backscatter email alias relay
Preface
Setting up an email relay that aliases addresses in various domains to a specific offsite user doesn’t have to mean backscatter. Here is one solution.
What you get
- Redirect mail from certain domains you control (e.g.
domain1.example.com
anddomain2.example.com
) to a specific user in another domain (for exampleyou@example.net
) for any number of aliases indomain1.example.com
anddomain2.example.com
. - Keep spam to a minimum (to the extent possible with a VPS with 1GB of RAM; better options are available with more RAM).
- Avoid backscatter even with maliciously crafted email intended to take advantage of the bounce mechanism when you use a relay and it rejects mail your server didn’t.
Caveats
- You need a local user with which to receive bounces so that you have the choice of manually deciding to redirect or delete, as appropriate.
- This does mean this isn’t a viable solution for high volume mail servers.
Prerequisites
- This article is based on using Postfix on Ubuntu 20.04. Other versions of Linux may have a different default Postfix configuration and thus these instructions may need adjusting on non-Debian/Ubuntu systems or even older (or later) versions of Debian/Ubuntu.
- A server with an acceptable public IP (i.e. not on blacklists or having a low reputation with the final destination email server) for sending mail or you need an email relay.
- The server must be able to receive mail on port 25 and to send to same, or receive mail on port 25 and be able to use an email relay to send (many VPS providers do no allow sending mail directly to the internet (specifically port 25 outgoing is often blocked), and those that do often have low reputation IP blocks due to abuse or poorly configured servers being abused unbeknownst the server operator).
- The server must be able to receive traffic on port 80 (HTTP) (or you will have to adjust the instruction for another means than Certbot standalone mode for obtaining SSL certificates).
- The server must have at least 1GB of RAM.
- Some knowledge of Linux system administration (this guide assumes a certain level of working knowledge and ability to troubleshoot errors while attempting to follow the guide).
- Knowledge of regular expression (regexp) syntax and use.
Packages
- certbot
- mutt
- opendmarc
- opendkim
- postfix
- postfix-policyd-spf-perl
- spamass-milter
Install the packages
sudo apt install -y certbot mutt opendmarc opendkim postfix postfix-policyd-spf-perl spamass-milter
Configuration
OpenDKIM
In the file
/etc/default/opendkim
, comment (that is make sure the line begins with#
) all lines beginning withSOCKET=
Add a line as below:
SOCKET=inet:8896@127.0.0.1
In the file
/etc/opendkim.conf
make sure the line containingAuthservID
matches your hostname. If your hostname wasmail.example.com
then it should containAuthservID mail.example.com
In the same file, make sure lines contain
Domain
,KeyFile
, andSelector
are commentedIn the same file, make the line contain
Mode
beMode v
In the same file, set the
Socket
line to beSocket inet:8896@127.0.0.1
In the same file, make sure the line with
TrustAnchorFile
is commentedAdd the following lines:
DNSTimeout 8 On-BadSignature r On-DNSError t
Once you have saved the above files issue:
systemctl restart opendkim
OpenDMARC
In the file
/etc/default/opendmarc
, comment (that is make sure the line begins with#
) all lines beginning withSOCKET=
Add a line as below:
SOCKET=inet:8897@127.0.0.1
In the file
/etc/opendmarc.conf
, make sure the line containingAuthservID
matches your hostname. If your hostname weremail.example.com
, then it should containAuthservID mail.example.com
In the same file, set one line to be
RejectFailures true
In the same file, set the
Socket
line to beSocket inet:8897@127.0.0.1
In the same file, set make sure the
TrustAuthservIDs
line isTrustAuthservIDs HOSTNAME
In the same file, add the following lines:
SPFIgnoreResults false SPFSelfValidate true RequiredHeaders true
Once you have saved the above files issue:
systemctl restart opendmarc
Spamass-milter and SpamAssassin
In the file
/etc/default/spamassassin
, change the line withCRON=0
toCRON=1
In the file
/etc/default/spamass-milter
replace the lines beginning withOPTIONS=
with the following snippet:# Default, use the spamass-milter user as the default user, ignore # messages from localhost OPTIONS="-u spamass-milter -i 127.0.0.1" # Reject emails with spamassassin scores > 3. #OPTIONS="${OPTIONS} -r 15" OPTIONS="${OPTIONS} -r 3" # Do not modify Subject:, Content-Type: or body. OPTIONS="${OPTIONS} -m" # Scan messages up to Postfix max size OPTIONS="${OPTIONS} -- -s 10240000"
In the file
/etc/spamassassin/local.cf
, comment the line containingrewrite_header
In the same file, uncomment and set the
required_score
line to berequired_score 3.0
In the same file, comment the line containing
use_bayes 1
(bayesian filtering is better used when not using spamassasin as a prequeue milter)In the same file, set the line
bayes_auto_learn 1
tobayes_auto_learn 0
Once you have saved the above files issue:
systemctl enable spamassassin spamass-milter systemctl restart spamass-milter spamassassin
Certbot
Assuming your mail server is
mail.example.com
and you have port 80 (HTTP) on the server open in your firewall (if any), issue the command:sudo certbot certonly --standalone -d mail.example.com
and answer the prompts.
Postfix
NB: Includes configuration of postfix-policyd-spf-perl
Add the following lines to
/etc/postfix/master.cf
:spfcheck unix - n n - 0 spawn user=policyd-spf argv=/usr/sbin/postfix-policyd-spf-perl
Edit
/etc/postfix/main.cf
to look like the example below (leaving comments that come with the default configuration, if you wish), assuming your mail server ismail.example.com
, you are required to use email relayrelay.example.com
, your final destination email address is in theexample.net
domain, you are relaying mail original sent todomain1.example.com
anddomain2.example.com
, and the final local user for postmaster mail is nameduser1
:myorigin = mail.example.com myhostname = mail.example.com smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu) biff = no append_dot_mydomain = no readme_directory = no compatibility_level = 2 smtpd_tls_cert_file=/etc/letsencrypt/live/mail.example.com/fullchain.pem smtpd_tls_key_file=/etc/letsencrypt/live/mail.example.com/privkey.pem smtpd_tls_security_level=may smtp_tls_CApath=/etc/ssl/certs smtp_tls_security_level=may smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache smtpd_relay_restrictions = permit_mynetworks reject_unauth_destination check_policy_service unix:private/spfcheck alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases mydestination = $myhostname, localhost.lxd, localhost, mail relayhost = [relay.example.com] relay_domains = example.net mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all inet_protocols = all virtual_alias_domains = domain1.example.com domain2.example.com virtual_alias_maps = regexp:/etc/postfix/virtual luser_relay = user1@localhost local_recipient_maps = milter_protocol = 6 smtpd_milters = inet:127.0.0.1:8896,inet:127.0.0.1:8897,unix:/spamass/spamass.sock milter_default_action = tempfail notify_classes = resource, software, 2bounce bounce_notice_recipient = postmaster@localhost 2bounce_notice_recipient = postmaster@localhost default_transport = local:$myhostname sender_dependent_relayhost_maps = hash:/etc/postfix/sender_relay_transport_map spfcheck_time_limit = 3600
Edit
/etc/aliases
to contain at least, assuming the final local user for postmaster mail is nameduser1
and your final destination mail address for email generated on the server ismailuser@example.net
:postmaster: user1 root: mailuser@example.net
Issue the command:
sudo newaliases
Create the file
/etc/postfix/sender_relay_transport_map
with the following contents (using the same assumptions as above):MAILER-DAEMON@mail.example.com local:mail.example.com MAILER-DAEMON@localhost local:mail.example.com
Issue the command:
sudo postmap hash:/etc/postfix/sender_relay_transport_map
Create the file
/etc/postfix/virtual
as appropriate. An example which redirectsuserX
orinfo
in any domain in thevirtual_alias_domains
above tomailuser@example.net
:/^user.@.*/ mailuser@example.net /^info@.*/ mailuser@example.net
Issue the commands:
sudo postfix check sudo postfix reload
Testing and regular checks
- Check your system’s logs to verify all is well.
- You should send mail to the users in your aliased domains (e.g.
domain1.example.com
ordomain2.example.com
) to verify valid mail is redirected to your final destination user (e.g.mailuser@example.net
). - You should also periodically check
/var/log/mail.log
and/var/log/mail.err
to verify what happens with spam or otherwise unwanted mail. - You will also need to regularly login as your local user (e.g.
user1
onmail.example.com
) and check mail (e.g. usingmutt
) to deal with any bounced mail (even though bounces should be rare).