Dwelling/homepage/views/articles/setting_up_a_mail_server.pug

531 lines
34 KiB
Plaintext
Raw Normal View History

2021-02-10 01:01:50 +04:00
extends article.pug
block append head
2023-02-21 18:57:20 +04:00
link(rel='canonical' href='https://arav.su/stuff/article/setting_up_a_mail_server')
2022-05-19 17:08:22 +04:00
2021-02-10 01:01:50 +04:00
block article
header
h2 Setting up a mail server
2021-02-10 01:01:50 +04:00
div.menu
a(href='/stuff#articles') Go back to articles list
time(datetime='2022-07-17') 17 July 2022
2021-02-10 01:01:50 +04:00
nav
h3 Contents
ol
li #[a(href='#art-1') Introduction]
li #[a(href='#art-2') Installing]
li #[a(href='#art-3') Postfix SMTP server]
ol
li #[a(href='#art-3-1') main.cf]
li #[a(href='#art-3-2') master.cf]
li #[a(href='#art-3-3') User aliases]
li #[a(href='#art-3-4') Starting Postfix]
li #[a(href='#art-4') Dovecot POP3/IMAP server with Sieve mail filter]
ol
li #[a(href='#art-4-1') Configuration files]
ol
li #[a(href='#art-4-1-1') dovecot.conf]
li #[a(href='#art-4-1-2') 10-auth.conf]
li #[a(href='#art-4-1-3') 10-logging.conf]
li #[a(href='#art-4-1-4') 10-mail.conf]
li #[a(href='#art-4-1-5') 10-master.conf]
li #[a(href='#art-4-1-6') 10-ssl.conf]
li #[a(href='#art-4-1-7') 15-lda.conf]
li #[a(href='#art-4-1-8') 15-mailboxes.conf]
li #[a(href='#art-4-1-9') 20-managesieve.conf]
li #[a(href='#art-4-1-10') 90-sieve.conf]
li #[a(href='#art-4-1-11') auth-system.conf.ext]
2021-02-10 01:01:50 +04:00
li #[a(href='#art-5') SpamAssassin spam filter]
ol
li #[a(href='#art-5-1') Updating built-in rule set]
li #[a(href='#art-5-2') Bayesian classifier training]
2021-02-10 01:01:50 +04:00
li #[a(href='#art-6') OpenDKIM signing and verifying filter]
ol
li #[a(href='#art-6-1') opendkim.conf]
li #[a(href='#art-6-2') Generating keys]
li #[a(href='#art-6-3') Populating KeyTable and SigningTable]
li #[a(href='#art-6-4') internal-hosts file]
li #[a(href='#art-6-5') Starting OpenDKIM]
li #[a(href='#art-7') OpenDMARC email policy filter]
li #[a(href='#art-8') DNS records]
ol
li #[a(href='#art-8-1') MX and A/AAAA]
li #[a(href='#art-8-2') PTR]
li #[a(href='#art-8-3') SPF]
li #[a(href='#art-8-4') DMARC]
li #[a(href='#art-8-5') DKIM]
li #[a(href='#art-9') Setting up a ClamAV antivirus]
li #[a(href='#art-10') Greylisting with postgrey]
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h3#art-1 #[a(href='#art-1') 1. Introduction]
p E-mail itself is a system consisting of several parts. A main E-mail protocol — SMTP is used for exchanging of mail between servers. For a user to manage his mail IMAP and POP3 protocols are used. Difference between IMAP and POP3 is that IMAP manages mail stored on a server, while POP3, once fetched mail, deletes it from server.
p As SMTP server I use Postfix. For IMAP (I don't use POP3 because I need access from multiple devices) I use Dovecot. Dovecot is also being used for SASL authentication in Postfix. And Dovecot supports Sieve protocol, through its implementation called Pigeonhole, that allows users to write their own message filtering rules.
p I decided to make use of greylisting. Even though I don't have a problem with spam after training of SpamAssassin. Luckily, it is very easy.
2021-02-10 01:01:50 +04:00
p Server is configured in a simple way using PAM (real system users) with user's passwords and with mail stored in ~/Maildir.
p But, as you know, there is a major problem called spam. And in order to withstand this problem there was mechanisms created. So that no one could impersonate you DKIM, DMARC and SPF were created. To ensure encrypted connection a MTA-STS (RFC8461) standard was made. To resist incoming spam anti-spam systems like SpamAssassin or spamd are used. Also it is good to check incoming mail for viruses, there are many antiviruses and I use one called ClamAV.
p I have mentioned DKIM (DomainKeys Identified Mail), it is used to sign mail going out that it is indeed comes from your server, and verify incoming mail. And for that I use OpenDKIM implementation.
p DMARC (Domain-based Message Authentication, Reporting and Conformance) is a policy that tells the server what to do if received mail failed some checks. To perform DMARC policy checks I use OpenDMARC implementation. OpenDMARC also performs SPF policy checks.
p SPF (Sender Policy Framework) allows the server to check that an incoming mail came from an IP address set in SPF DNS record for domain it originates from.
p Many servers will reject mail from a dynamic IP-address, so a static address is a must. But, from my experience, GMail, Yandex and Rambler will at least place my message to Junk directory. On Gmail I get to Inbox most of the time btw. There is such thing used against spam called block lists, and Spamhaus is the biggest player here. So, if a server uses such lists I get rejected at the stage of establishing a connection with no chance to get through anti-spam system.
p If you have a static address then in Spamhaus you can submit your address for deletion.
2023-02-21 18:57:20 +04:00
p Also, while not stated by RFCs, some servers will still reject your mail if your IP-address doesn't have a PTR DNS record set, and some will expect a PTR record to be a hostname of your server, like mail.example.org, or like my the.arav.su.
p There is another problem known as not trusted top level domain, like mine .su domain. :) Because of how cheap it is, it is popular among spammers.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h3#art-2 #[a(href='#art-2') 2. Installing]
p You need to install following packages: #[code postfix], #[code dovecot], #[code pigeonhole] (or could be named as #[code dovecot-sieve]), #[code clamav], #[code opendkim], #[code opendmarc], #[code spamassassin], and #[code postgrey].
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h3#art-3 #[a(href='#art-3') 3. Postfix SMTP server]
p Its configuration files are in directory #[code /etc/postfix]. There are two configuration files we'll work with. The first one is a #[code main.cf] file. Then we configure services in #[code master.cf]. Also I'll show you how to make aliases for users.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h4#art-3-1 #[a(href='#art-3-1') 3.1. main.cf]
p So, you have #[code main.cf] opened, first we need to make changes in an existing configuration:
2021-02-10 01:01:50 +04:00
pre
| myhostname = mail.example.org
| mydomain = example.org
| myorigin = $mydomain
p #[code myhostname] should be a subdomain that points to an IP of a mail server. This hostname is usually an A DNS record even if you have only one IP-address it is discuraged making it a CNAME.
p #[code mydomain] is your domain. In #[code myorigin] we set a domain name from what sent mail appear. Yes, you can use other options as a variable to reduce copy-pasting.
pre
2021-02-10 01:01:50 +04:00
| inet_interfaces = all
p In #[code inet_interfaces] we list interfaces we listen on. In this case we listen on all available interfaces.
pre
2021-02-10 01:01:50 +04:00
| mydestination = $myhostname, localhost, $mydomain, mail.$mydomain
p #[code mydestination] is a list of domains our server deliver mail for.
pre
2021-02-10 01:01:50 +04:00
| local_recipient_maps = unix:passwd.byname $alias_maps
p Here we tell Postfix where to look for names of local recipients.
pre
2021-02-10 01:01:50 +04:00
| mynetworks = localhost, 192.168.0.0/24
p It is a list of trusted remote clients allowed to relay mail through our server.
pre
2021-02-10 01:01:50 +04:00
| alias_maps = hash:/etc/postfix/aliases
| alias_database = $alias_maps
p For database of aliases we use #[code /etc/postfix/aliases] file in a special format we learn about later.
pre
2021-02-10 01:01:50 +04:00
| recipient_delimiter = +
p A symbol used to separate username and an extension.
pre
2021-02-10 01:01:50 +04:00
| home_mailbox = Maildir/
p Here we use a Maildir/ mailbox style when every message is stored in a separated file.
pre
2021-02-10 01:01:50 +04:00
| mailbox_transport = lmtp:unix:private/dovecot-lmtp
p Here we tell Postfix what LMTP (Local Mail Transport Protocol) server to use.
pre
2021-02-10 01:01:50 +04:00
| inet_protocols = ipv4
p Here we leave only IPv4 support, if you have an IPv6 address then you may want to add #[code ipv6].
p So here we are done with an existing configuration and ready to make our additions to add encryption, milters, configure SASL authentication, restrictions and make some tweaks.
p And we start with configuring milters. Milter stands for mail filter, a special protocol originated in Sendmail SMTP server.
pre
| milter_default_action = accept
| milter_protocol = 6
| smtpd_milters =
| unix:/var/spool/opendmarc/opendmarc.sock
| unix:/var/spool/opendkim/opendkim.sock
| inet 192.168.0.54:7357
| non_smtpd_milters = $smtpd_milters
p #[code milter_default_action] specifies default action of a milter to accept messages.
p #[code milter_protocol] specifies protocol version used by milters, current is 6.
p #[code smtpd_milters] is a list of milters the messages will go through. You can connect to milter with internet protocol with #[code inet:], with a unix socket #[code unix:]. The last one with port 7357 is a ClamAV.
p Now lets add some tweaks:
pre
| biff = no
| strict_rfc821_envelopes = yes
| disable_vrfy_command = yes
| smtpd_helo_required = yes
| smtpd_delay_reject = yes
|
| mailbox_size_limit = 0
| message_size_limit = 52428800
p #[code biff] set to no to disable local service for new mail notifications. It is a server, we don't need them here.
p #[code strict_rfc821_envelopes] set to yes require addresses to be enclosed with <>.
p Disabling VRFY command with #[code disable_vrfy_command] prevents some email addresses harvesting techniques.
p #[code smtpd_helo_required] requires remote client to send HELO or EHLO command. This may stop some poorly written spam bots.
p #[code smtpd_delay_reject] makes Postfix wait for RCPT TO command before evaluating some restrictions.
p #[code mailbox_size_limit] and #[code message_size_limit] sets maximum size of a whole mailbox and of each email. Here I set no limit for a mailbox, and max of 50MiB for an email message.
p And now it's time for SASL configuration:
pre
| smtpd_sasl_type = dovecot
| smtps_sasl_path = private/auth
| smtps_sasl_auth_enable = yes
| smtpd_sasl_security_options = noanonymous
| smtpd_sasl_local_domain = $mydomain
| broken_sasl_auth_clients = no
p It's pretty clear. We chose dovecot as our SASL server, set path to it that lies within /var/spool directory, and then enabled it. Then we prohibit anonymous connections. Add our domain for SASL authentication, and deny access for the clients with obsolete version of AUTH command.
p It's time for encryption, lets specify a list of high (strong) ciphers to use:
pre
| tls_high_cipherlist = ECDHE:DHE:kGOST:!aNULL:!eNULL:!RC4:!MD5:!3DES:!AES128:!CAMELLIA128:!ECDHE-RSA-AES256-SHA:!ECDHE-ECDSA-AES256-SHA
p I took this ciphers set from #[a(href="https://pub.nethence.com/security/ciphers") https://pub.nethence.com/security/ciphers].
p Now lets configure encryption for outgoing connections.
pre
| smtp_use_tls = yes
| smtp_tls_security_level = encrypt
| smtp_tls_note_starttls_offer = yes
| smtp_tls_connection_reuse = yes
| smtp_tls_key_file = /etc/letsencrypt/live/example.org/privkey.pem
| smtp_tls_cert_file = /etc/letsencrypt/live/example.org/fullchain.pem
| smtp_tls_mandatory_protocols = !SSLv2,!SSLv3,!TLSv1,!TLSv1.1
| smtp_tls_protocols = $smtp_tls_mandatory_protocols
| smtp_tls_mandatory_ciphers = high
p First we enable it with a #[code smtp_use_tls]. Enforce use of encryption with (#[code smtp_tls_security_level]) set to encrypt. Noting servers of our ability to use encryption with option #[code smtp_tls_note_starttls_offer]. Reuse connection instead of opening the new one each time.
p #[code smtp_tls_key_file] and #[code smtp_tls_cert_file] are paths to our TLS key and certificate.
p In #[code smtp_tls_mandatory_protocols] and #[code smtp_tls_protocols] we disallow old vulnerable protocols to use. Leave only TLS version 1.2 and 1.3 available.
p In #[code smtp_tls_mandatory_ciphers] we declare to use only good secure cyphers we previously set.
p For incoming connections everything is the same, just a few more options added:
pre
| smtpd_use_tls = yes
| smtpd_tls_security_level = encrypt
| smtpd_tls_auth_only = yes
| smtpd_tls_key_file = /etc/letsencrypt/live/example.org/privkey.pem
| smtpd_tls_cert_file = /etc/letsencrypt/live/example.org/fullchain.pem
| smtpd_tls_mandatory_protocols = $smtp_tls_mandatory_protocols
| smtpd_tls_protocols = $smtpd_tls_mandatory_protocols
| smtpd_tls_mandatory_ciphers = $smtp_tls_mandatory_ciphers
| smtpd_tls_loglevel = 1
| smtpd_tls_received_header = yes
| smtpd_tls_session_cache_timeout = 3600s
| tls_random_source = dev:/dev/urandom
p #[code smtpd_tls_auth_only] allows authorisation only on encrypted connections.
p #[code smtpd_tls_loglevel] set to 1 to log a summary of a TLS handshake.
p #[code smtpd_tls_received_header] makes Postfix include information about the protocol and cypher used to a #[code Received:] header.
p #[code smtpd_tls_session_cache_timeout] is for how long to store session. #[code tls_random_source] is for setting an entropy source.
p The final part is my "favorite". :) The restrictions! There is a set of them for each stage the message falls through. Here are the ones I configured:#[code smtpd_helo_restrictions], #[code smtpd_relay_restrictions], #[code smtpd_data_restrictions], #[code smtpd_sender_restrictions], and #[code smtpd_recipient_restrictions].
p So lets roll. This my working restrictions setup:
pre
| smtpd_helo_restrictions =
| reject_unknown_helo_hostname,
| reject_invalid_helo_hostname,
| reject_non_fqdn_helo_hostname
pre
| smtpd_data_restrictions =
| reject_multi_recipient_bounce,
| reject_unauth_pipelining
pre
| smtpd_sender_restrictions =
| permit_sasl_authenticated,
| reject_non_fqdn_sender,
| reject_unknown_sender_domain
pre
| smtpd_recipient_restrictions =
| reject_unknown_recipient_domain,
| reject_non_fqdn_recipient
p In order to explain what every restriction does I'd have to copy-paste from #[code man 5 postconf]. :)
p The first set of restrictions are for HELO or EHLO command, that we force the client to send with priorly set option #[code smtpd_helo_required] to yes. Here #[code reject_unknown_helo_hostname] rejects hostnames that doesn't have DNS A or MX records. #[code reject_invalid_helo_hostname] rejects malformed hostnames, and #[code reject_non_fqdn_helo_hostname] ensures that the hostname is a fully-qualified domain name.
p The second one are for DATA command. And here man page is better than me at explaining it. Here is the link for #[a(href='http://www.postfix.org/postconf.5.html#reject_multi_recipient_bounce') reject_multi_recipient_bounce] and #[a(href='http://www.postfix.org/postconf.5.html#reject_unauth_pipelining') reject_unauth_pipelining]. Or just look them up in #[code man 5 postconf]. All I can say is that it is better to have them than not to. :)
p The rest is simpler, #[code permit_sasl_authenticated] in #[code smtpd_sender_restrictions] accepts the senders that were authenticated by SASL (e.g. Dovecot or Cyrus). And all the #[code reject_unknown_*] and #[code reject_non_fqdn_*] has the same meaning as for theirs *_helo_* counterparts, just used in theirs specific places.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h4#art-3-2 #[a(href='#art-3-2') 3.2. master.cf]
p The following services are needed: #[code smtp], #[code submission], #[code smtps], and we add #[code spamassassin] service. The rest in this file left untouchable.
2021-02-10 01:01:50 +04:00
pre
| smtp inet n - n - - smtpd
| -o content_filter=spamassassin
2022-10-26 01:50:10 +04:00
| submission inet n - n - - smtpd
| -o syslog_name=postfix/submission
| -o smtpd_tls_security_level=encrypt
| -o smtpd_sasl_auth_enable=yes
| -o smtpd_tls_auth_only=yes
| -o milter_macro_daemon_name=ORIGINATING
| -o content_filter=spamassassin
2021-02-10 01:01:50 +04:00
| smtps inet n - n - - smtpd
| -o content_filter=spamassassin
| -o syslog_name=postfix/smtps
| -o smtpd_tls_wrappermode=yes
| -o smtpd_sasl_auth_enable=yes
2021-02-10 01:01:50 +04:00
|
2022-10-26 01:50:10 +04:00
| spamassassin unix - n n - - pipe
2021-02-10 01:01:50 +04:00
| user=spamd argv=/bin/vendor_perl/spamc
| -e /sbin/sendmail -oi -f ${sender} ${recipient}
p #[code smtp] is listening on port 25, and #[code smtps] on port 465. #[code submission] is listening on port 587 and is used by mail client to send mail.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h4#art-3-3 #[a(href='#art-3-3') 3.3. User aliases]
p User aliases are in #[code aliases] file. They has a form "#[code <alias>: <username>]", e.g. #[code me: arav]. Where #[code username] may be other alias. After editing you need to run #[code newaliases] program to update #[code aliases.db] file.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h4#art-3-4 #[a(href='#art-3-4') 3.4. Starting Postfix]
2021-02-10 01:01:50 +04:00
p To start a Postfix service on systemd-based Linux distro run #[code systemctl start postfix]. To make Postfix run on every boot run #[code systemctl enable postfix].
2021-02-10 19:56:44 +04:00
h3#art-4 #[a(href='#art-4') 4. Dovecot POP3/IMAP server with Sieve mail filter]
p As I stated in the Introduction I use only IMAP, since it keeps mail on a server.
p First we need to create a #[code /etc/dovecot] directory and copy example configuration there, but we don't need everything. Example configuration lies in a #[code /usr/share/doc/dovecot/example-config] directory.
p Copy #[code dovecot.conf] file and #[code conf.d] directory.
p We will need #[code dovecot.conf] and files from a #[code conf.d] directory: 10-auth.conf, 10-mail.conf, 10-ssl.conf, 15-mailboxes.conf, 20-lmtp.conf, 90-sieve.conf, 10-logging.conf, 10-master.conf, 15-lda.conf, 20-managesieve.conf, auth-system.conf.ext.
p Here I will leave a brief necessary explanation of options, since you will see theirs documentation when will be editing configs.
h4#art-4-1 #[a(href='#art-4-1') 4.1. Configuration files]
h5#art-4-1-1 #[a(href='#art-4-1-1') 4.1.1. dovecot.conf]
pre
| protocols = imap lmtp sieve
p Here we activate IMAP for access to mailbox, LMTP for local message delivery from Postfix and Sieve protocol for user-defined custom filters.
pre
| listen = *
p Here we simply tell Dovecot to listen on all available interfaces.
pre
| base_dir = /var/run/dovecot
p Uncomment setting to set a base directory for runtime files.
pre
| instance_name = dovecot
p A new for this instance. In case you run multiple you can set a different names for them.
pre
| login_greeting = Dovecot ready.
p A greeting message for the clients.
pre
| login_trusted_networks = 192.168.0.0/24 192.168.1.0/24
p A list of trusted network ranges.
pre
| shutdown_clients = yes
p I chose to force closing client connections on master process shutdown.
p Here we are done with #[code dovecot.conf] file and let's go to #[code conf.d] directory.
h5#art-4-1-2 #[a(href='#art-4-1-2') 4.1.2. 10-auth.conf]
pre
| disable_plaintext_auth = yes
p It disables login without encryption.
pre
| auth_realms = example.org
p A list of realms for SASL authentication. Just leave here your domain.
pre
| auth_username_format = %Ln
p I chose to drop domain.
pre
| auth_mechanisms = plain
p I use a plain mechanism.
p And, finally, at the bottom leave #[code auth-system.conf.ext] include directive.
h5#art-4-1-3 #[a(href='#art-4-1-3') 4.1.3. 10-logging.conf]
p Simply uncomment #[code log_path = syslog] there.
h5#art-4-1-4 #[a(href='#art-4-1-4') 4.1.4. 10-mail.conf]
p In #[code 10-mail.conf] we set following:
pre
| mail_location = maildir:~/Maildir
p We use Maildir/ scheme and store it in user's home directory.
p Next set #[code mail_server_admin = admin@%d], or whatever name you want. Here #[code %d] will expand to a domain.
h5#art-4-1-5 #[a(href='#art-4-1-5') 4.1.5. 10-master.conf]
p In section #[code service imap-login] uncomment everything in #[code inet_listener] sub-sections for IMAP and IMAPS.
p If you want to use POP3 protocol, then do the same for #[code service pop3-login] section.
p Go to #[code service lmtp] section and add following sub-section to enable LMTP service on a UNIX socket:
pre
| unix_listener /var/spool/postfix/private/dovecot-lmtp {
| group = postfix
| user = postfix
| mode = 0666
| }
p Then go to #[code service auth] section and add following UNIX listener:
pre
| unix_listener /var/spool/postfix/private/auth {
| user = postfix
| mode = 0666
| }
h5#art-4-1-6 #[a(href='#art-4-1-6') 4.1.6. 10-ssl.conf]
p Set #[code ssl = required] to enable and enforce encryption.
p In #[code ssl_cert] and #[code ssl_key] set paths to your TLS certificate and key.
p Next you will see #[code ssl_key_password] where you type in your certificate's password if it is set.
p And if you run your own PKI then you can specify your CA cert in #[code ssl_ca].
p Next good thing to do is to generate Diffie-Hellmann parameters file and set it in #[code ssl_dh] option. How to do it is written in a help comment for this option.
p Next set a cipher list #[code ssl_cipher_list] to a list you previously specified in Postfix's #[code main.cf] file.
p And finally #[code ssl_prefer_server_ciphers = yes].
h5#art-4-1-7 #[a(href='#art-4-1-7') 4.1.7. 15-lda.conf]
p Here we simply uncomment and set #[code mail_plugins = $mail_plugins sieve] in #[code protocol lda] section.
h5#art-4-1-8 #[a(href='#art-4-1-8') 4.1.8. 15-mailboxes.conf]
p Here in a namespace inbox leave Drafts, Junk, Trash, and Sent mailboxes. Additionally, for every mailbox I added #[code auto = subscribe] options so they will appear in a mail client.
h5#art-4-1-9 #[a(href='#art-4-1-9') 4.1.9. 20-managesieve.conf]
p Uncomment #[code protocols] option, #[code service managesieve-login], and #[code service managesieve] sections.
p Within #[code service managesieve-login] section uncomment fully #[code inet_listener sieve] sub-section. And next you can tweak #[code service_count], #[code process_min_avail] and #[code vsz_limit] options to your taste.
h5#art-4-1-10 #[a(href='#art-4-1-10') 4.1.10. 90-sieve.conf]
p Here I just have #[code sieve_before = /var/lib/dovecot/sieve.d/] option uncommented, the rest is default.
h5#art-4-1-11 #[a(href='#art-4-1-11') 4.1.11. auth-system.conf.ext]
p Here is default except for in passdb I added #[code failure_show_msg=yes] in #[code args].
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h3#art-5 #[a(href='#art-5') 5. SpamAssassin spam filter]
p First, we'll work with a #[code local.cf] file to configure SpamAssassin.
p I didn't any fancy tweaking here, didn't make any custom rules. I just changed options presented in file.
p I left #[code rewrite_header] option commented since I don't want to append anything to a Subject header of spam messages.
p Option #[code report_safe] I set to 2 to save spam messages as a text/plain attachment instead of modifying original message.
p #[code trusted_networks] sets networks and hosts that are considered trusted, i.e. not spammers.
p #[code lock_method] left to be flock, since I don't use NFS.
p #[code required_score] left to be its default value of 5.0.
p I use Bayesian classifier, so options #[code use_bayes] and #[code bayes_auto_learn] are set to 1.
p I chose to normalise charset to UTF-8 with option #[code normalize_charset 1].
p Next we move to #[code *.pre] files.
p In #[code init.pre] I commented out URIDNSBL plugin, since I don't use them.
p In #[code v342.pre] I enabled #[code FromNameSpoof] and #[code Phishing] plugins.
h4#art-5-1 #[a(href='#art-5-1') 5.1. Updating built-in rule set]
p I sometimes run #[code sa-update] util to update built-in rules and pre-compile them with #[code sa-compile] util. After that restart SpamAssassin.
p And so SpamAssassin make use of compiled rules ensure that a plugin Rule2XSBody in a #[code v320.pre] is uncommented.
2022-01-23 21:07:04 +04:00
h4#art-5-2 #[a(href='#art-5-2') 5.2. Bayesian classifier training]
p After you set up SpamAssassin for the first time you have to train Bayesian classifier. It will start to work after 200 messages will be examined.
p For training use #[code sa-learn] utility and use #[code --ham] and #[code --spam] to mark messages as normal mail and spam. I additionaly have to specify a path to database with #[code --dbpath /var/lib/spamassassin/.spamassassin], otherwise it will complain, so try first without it.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h3#art-6 #[a(href='#art-6') 6. OpenDKIM signing and verifying filter]
p On ArchLinux I got a problem that OpenDKIM is unable to write in #[code /run], so I created #[code /var/spool/opendkim] directory for it.
p After configuring it, you need to add DNS record with your public key, it is covered in #[a href='#art-8-5'] section.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h4#art-6-1 #[a(href='#art-6-1') 6.1. opendkim.conf]
p Well, that's main config file:
2021-02-10 01:01:50 +04:00
pre
| KeyTable refile:/etc/opendkim/keytable
| SigningTable refile:/etc/opendkim/signingtable
| InternalHosts refile:/etc/opendkim/internal-hosts
|
| Socket local:/var/spool/opendkim/opendkim.sock
| PidFile /var/spool/opendkim/opendkim.pid
| UMask 000
| UserID opendkim:opendkim
|
| Mode sv
| SubDomains yes
|
| Canonicalization relaxed/simple
|
| Syslog yes
| SyslogSuccess yes
| LogWhy yes
|
| SoftwareHeader yes
p I myself set up a multi-domain variant just in case. So, here we have two main tables: #[code KeyTable] and #[code SigningTable]. Those files tells OpenDKIM where to find keys and what domains to sign. You may use one key for all domains or generate keys for each domain.
p #[code InternalHosts] tells OpenDKIM what hosts should be signed rather than verified.
p #[code Socket] tells where to listen to connections, in this case we use UNIX sockets.
p #[code Mode] selects operating mode(s). In our case we have two modes: (s)igner and (v)erifier.
p #[code SubDomains] set to yes tells that we allow subdomains of our domains to be signed and verified.
p #[code Canonicalization] selects the canonicalization method(s) to be used with signing. We set relaxed for header and simple for body. I don't fully understand it and just use what suggested.
p Below are logging options that tells to write in syslog.
p With #[code SoftwareHeader] set to yes OpenDKIM will be always adding "DKIM-Filter" header field.
2021-02-10 19:56:44 +04:00
h4#art-6-2 #[a(href='#art-6-2') 6.2. Generating keys]
2021-02-10 01:01:50 +04:00
pre
| opendkim-genkey -r -s myselector -b 2048 -d example.com
p This command will generate a key pair stored in files "myselector.private" and "myselector.txt" for a given domain.
p #[code -r] restricts the key to emails use only. #[code -s] is a name of selector.#[code -b] is the size of the key in bits. #[code -d] is our domain.
p Name of a selector is usually a #[code mail], but that's just what I use, you can choose whatever you want.
2021-02-10 19:56:44 +04:00
h4#art-6-3 #[a(href='#art-6-3') 6.3. Populating KeyTable and SigningTable]
2021-02-10 01:01:50 +04:00
p KeyTable has following structure (a line per domain):
pre
| myselector._domainkey.example.com example.com:myselector:/etc/opendkim/myselector.private
2022-01-23 21:07:04 +04:00
p And SigningTable this one:
2021-02-10 01:01:50 +04:00
pre
| *@example.com myselector._domainkey.example.com
2021-02-10 19:56:44 +04:00
h4#art-6-4 #[a(href='#art-6-4') 6.4. internal-hosts file]
2021-02-10 01:01:50 +04:00
p As stated above in this file we put hosts whose mail should be signed rather than verified. And its structure is the following:
pre
| 127.0.0.1
| 192.168.0.0/24
p #[code 127.0.0.1] is necessary to be there according to a manual.
2021-02-10 19:56:44 +04:00
h4#art-6-5 #[a(href='#art-6-5') 6.5. Starting OpenDKIM]
2021-02-10 01:01:50 +04:00
p #[code systemctl start opendkim] and #[code systemctl enable opendkim] to start and enable OpenDKIM service to run on OS start up if you got Poetteringed just like me. :)
2021-02-10 19:56:44 +04:00
h3#art-7 #[a(href='#art-7') 7. OpenDMARC email policy filter]
2021-02-10 01:01:50 +04:00
p Its configuration lies in #[code /etc/opendmarc/opendmarc.conf] and is fully documented. Here are the options I changed:
pre
| AuthservID OpenDMARC
| FailureReports true
| FailureReportsBcc admin@example.org
| FailureReportsSentBy admin@example.org
| IgnoreAuthenticatedClient yes
| RejectFailures true
| RequiredHeaders yes
| Socket unix:/var/spool/opendmarc/opendmarc.sock
| SoftwareHeader true
| SPFSelfValidate true
| Syslog true
| TrustedAuthservIDs mail.example.org,example.org
| UMask 002
p What's in a #[code Socket] option should be added to Postfix's #[code smtpd_milters] and #[code non_smtpd_milters].
p Creating DMARC DNS record covered in <a href="#art-8-4">8.4</a>.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h3#art-8 #[a(href='#art-8') 8. DNS records]
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h4#art-8-1 #[a(href='#art-8-1') 8.1. MX and A/AAAA]
p It's good to have a dedicated A (IPv4 address) or AAAA (IPv6 address) record for a mail server's hostname instead of a CNAME record so other servers won't need to do two DNS requests. Hostname is usually mail.example.org if there's just one server, you can call it whatever you want. Remind you that we set it in Postfix in #[code myhostname] option.
2021-02-10 01:01:50 +04:00
p And A record looks like this:
pre
| mail&nbsp;&nbsp;IN&nbsp;&nbsp;86400&nbsp;&nbsp;A&nbsp;&nbsp;203.0.113.4
p Where #[code mail] is a hostname, 86400 is a TTL of a record in seconds.
p Next we need to add a MX (mail exchanger) record that looks like this:
pre
| &nbsp;&nbsp;MX 10 mail.example.org.
p Here 10 is a priority of a record. The lower a number the higher a priority.
p A period at the end of the hostnames is necessary in DNS records.
2021-02-10 19:56:44 +04:00
h4#art-8-2 #[a(href='#art-8-2') 8.2. PTR]
2021-02-10 01:01:50 +04:00
p PTR is a reverse DNS record that stands for pointer and is used to &ldquo;bind&rdquo; a hostname to IP-address. Mail servers looks for this record and check so this name equals to a hostname provided in EHLO. Most servers will reject your mail if your PTR looks something like 1.2.3.4.pppoe.someisp.net or not set at all.
p There are three ways to set this record: ask your hosting or internet-provider, or get your own Autonomous System (:^)).
p Example of this record:
pre
| 1 IN PTR mail.example.org.
2021-02-10 19:56:44 +04:00
h4#art-8-3 #[a(href='#art-8-3') 8.3. SPF]
2021-02-10 01:01:50 +04:00
p SPF stands for Sender Policy Framework and in my case it looks exactly like this:
pre
| v=spf1 +a +mx -all
p So, #[code v] is a version of a protocol. #[code +a +mx] means that only servers specified in the A and MX DNS records could send email, and #[code -all] that no one else could do that.
2021-02-10 19:56:44 +04:00
h4#art-8-4 #[a(href='#art-8-4') 8.4. DMARC]
2021-02-10 01:01:50 +04:00
p DMARC stands for Domain-based Message Authentication Reporting and Conformance. And its DNS record could be like this one that I use:
pre
| _dmarc IN TXT "v=DMARC1; p=reject; rua=mailto:admin@example.org; ruf=mailto:admin@example.org; adkim=s; aspf=s"
2021-02-10 01:01:50 +04:00
p #[code v] is a version of a protocol.
p #[code p] is a default policy that could be set to #[code none], #[code quarantine] and #[code reject]. I chose to #[code reject] mail that comes from &laquo;me&rdquo; if there's something wrong with a origin of a message. If you could get email from subdomains then you need to set #[code sp] as well.
p #[code rua] is an address for the reports and #[code ruf] is for the forensic reports. #[code aspf] verifies that an address in the MAIL FROM command and #[code From:] header matches example.org in strict (s) mode, and in relaxed (default, r) mode matches domain or its subdomains. For #[code adkim] is the same except in this case sender domain name should match a domain in #[code d=domain] in a #[code DKIM-Signature] header.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h4#art-8-5 #[a(href='#art-8-5') 8.5. DKIM]
2021-02-10 01:01:50 +04:00
p In 5.2 we generated a key pair for our domain and now we'll take what's inside a #[code myselector.txt] file and add it to our DNS.
p DKIM DNS record looks like this:
pre
| myselector._domainkey IN TXT ( "v=DKIMv1; k=rsa; s=email; p=&lt;public key goes here&gt;"
| "&lt;public key continues here&gt;" )
p Brackets are used in case a content of a record doesn't fit on one line, and it won't fit.
2021-02-10 01:01:50 +04:00
2021-02-10 19:56:44 +04:00
h3#art-9 #[a(href='#art-9') 9. Setting up a ClamAV antivirus]
2021-02-10 01:01:50 +04:00
p All you need to make it work together with Postfix is to add #[code /run/clamav/milter.sock] to #[code smtpd_milters] and #[code non_smtpd_milters] options in Postfix, also make some changes in configs of ClamAV.
p In #[code clamav-milter.conf] you need the following:
pre
| MilterSocket unix:/run/clamav/milter.sock
| ClamdSocket unix:/run/clamav/clamd.ctl
p Also, in case you need ClamAV to add headers also in case a message is free of viruses add #[code AddHeader Add] or #[code AddHeader Replace] option. The difference between them is detaily described in config file itself.
p Before starting ClamAV you need to update its virus definitions with #[code freshclam] util. Also, enable and start #[code clamav-freshclam] systemd service to keep definitions recent.
p I don't know how it is in other distros, but, for whatever reason, an Arch Linux's package doesn't come with a systemd service file for the ClamAV milter. So I just copy it here from <a href="https://wiki.archlinux.org/index.php/ClamAV#Using_the_milter" rel="noopener noreferrer" target="_blank">ArchWiki</a>:
2021-02-10 01:01:50 +04:00
pre
| [Unit]
| Description='ClamAV Milter'
| After=clamav-daemon.service
|
| [Service]
| Type=forking
| ExecStart=/usr/bin/clamav-milter --config-file /etc/clamav/clamav-milter.conf
|
| [Install]
| WantedBy=multi-user.target
p Save it as #[code /usr/lib/systemd/system/clamav-milter.service] and run #[code systemctl daemon-reload].
p Next you need to enable and start #[code clamav-daemon] and #[code clamav-milter].
h3#art-10 #[a(href='#art-10') 10. Greylisting with postgrey]
p #[code postgrey] is being configured primarily by passing arguments you configure in its service file. I go by defaults. So it is listening on #[code 127.0.0.1:10030].
p So, to set a listen port pass this option: #[code --inet=127.0.0.1:10030].
p To configure greylisting duration use option #[code --delay=seconds]. #[code --max-age=N] to delete entries older than N days since the last time that they have been seen.
p Look #[code perldoc postgrey] for help.
p There are two configuaration files named #[code postgrey_whitelist_clients] and #[code postgrey_whitelist_recipients]. Dunno about other distros, but in ArchLinux they are in #[code /etc/postfix]. Could be in #[code /etc/postgrey] in other distros.
p #[code postgrey_whitelist_clients] is list of trusted SMTP servers that will not be greylisted. Also there comes some problematic servers that will not attempt to send a mail again, or have a large pool of addresses. This file comes pre-filled with some known server such as #[code gmail.com] and #[code outlook.com].
p #[code postgrey_whitelist_recipients] contains a list of recipients for which mail won't be greylisted. By default there are #[code postmaster] and #[code abuse] recipients listed.
p In postfix's #[code main.cf] you need just add this last line:
pre
| smtpd_recipient_restrictions =
| reject_unknown_recipient_domain,
| reject_non_fqdn_recipient,
| #[b check_policy_service inet:127.0.0.1:10030]