This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
email [2014/11/24 02:11] ben |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Email ====== | ||
- | ===== Overview ===== | ||
- | The aim is to put together a simple but secure mail setup for '' | ||
- | |||
- | * Sending and receiving mail: postfix | ||
- | * Reading mail: dovecot (imap), roundcube (https) | ||
- | * Security: tls, ssl, sasl, ldap | ||
- | * content filtering: amavisd, clamav, spamassassin | ||
- | |||
- | The primary MX will be [[Shadow]] and the intention is to install a backup MX at a later date. | ||
- | |||
- | ===== Installation ===== | ||
- | Set the following global USE flags: | ||
- | |||
- | {{USEFlag| | ||
- | {{EnableFlag|ldap}} | ||
- | {{EnableFlag|sasl}} | ||
- | {{EnableFlag|ssl}} | ||
- | }} | ||
- | |||
- | For sending and receiving mail | ||
- | * {{Package|direct=yes|mail-mta/ | ||
- | * {{Package|direct=yes|net-mail/ | ||
- | |||
- | For content filtering: | ||
- | * {{Package|direct=yes|mail-filter/ | ||
- | * {{Package|direct=yes|app-antivirus/ | ||
- | * {{Package|direct=yes|mail-filter/ | ||
- | * {{Package|direct=yes|dev-perl/ | ||
- | * {{Package|direct=yes|dev-python/ | ||
- | * {{Package|direct=yes|mail-filter/ | ||
- | * {{Package|direct=yes|dev-perl/ | ||
- | * {{Package|direct=yes|mail-filter/ | ||
- | |||
- | ===== Configuration ===== | ||
- | ==== Postfix ==== | ||
- | These configuration files are stored in subversion, under [[https:// | ||
- | |||
- | {{File|/ | ||
- | - Set the proper host and domainname for the server | ||
- | myhostname = shadow.sihnon.net | ||
- | mydomain = sihnon.net | ||
- | |||
- | - Where locally generated mail appears to come from | ||
- | myorigin = $mydomain | ||
- | |||
- | - Which domains we are the final destination for | ||
- | mydestination = $myhostname, | ||
- | |||
- | - Listen on all interfaces | ||
- | inet_interfaces = all | ||
- | |||
- | - Define the list of trusted network addresses, able to relay mail | ||
- | mynetworks = cidr:/ | ||
- | |||
- | - Masquerade incoming and outgoing mail to sihnon.net, no other hosts handle mail for this domain | ||
- | asquerade_domains = sihnon.net | ||
- | masquerade_classes = envelope_sender, | ||
- | |||
- | - Allow mail of the form user+extension@mydomain | ||
- | recipient_delimiter = + | ||
- | |||
- | - Don't show the software version in the banner | ||
- | smtpd_banner = $myhostname ESMTP $mail_name | ||
- | |||
- | - Get alias information from LDAP | ||
- | alias_maps = ldap:/ | ||
- | |||
- | - Mail Delivery | ||
- | local_recipient_maps = | ||
- | |||
- | - Sasl | ||
- | smtpd_sasl_auth_enable = yes | ||
- | smtpd_sasl_type = dovecot | ||
- | smtpd_sasl_path = private/ | ||
- | smtpd_sasl_security_options = noanonymous | ||
- | broken_sasl_auth_clients = yes | ||
- | |||
- | - Use TLS when relaying mail | ||
- | smtp_use_tls = yes | ||
- | smtp_tls_note_starttls_offer = yes | ||
- | |||
- | - Use TLS for receiving mail | ||
- | smtpd_use_tls = yes | ||
- | smtpd_tls_auth_only = yes | ||
- | smtpd_tls_key_file = / | ||
- | smtpd_tls_cert_file = / | ||
- | smtpd_tls_CAfile = / | ||
- | smtpd_tls_loglevel = 1 | ||
- | smtpd_tls_received_header = yes | ||
- | smtpd_tls_session_cache_timeout = 3600s | ||
- | tls_random_source = dev:/ | ||
- | |||
- | - Restrictions | ||
- | smtpd_recipient_restrictions = permit_sasl_authenticated, | ||
- | - Prevent spammers harvesting email addresses | ||
- | disable_vrfy_command = yes | ||
- | - Be strict about well formed connections and messages | ||
- | strict_rfc821_envelopes = yes | ||
- | smtpd_helo_required = yes | ||
- | smtpd_delay_reject = yes | ||
- | |||
- | - Content filtering | ||
- | empty_address_recipient = MAILER-DAEMON | ||
- | queue_minfree = 120000000 | ||
- | smtpd_delay_reject = yes | ||
- | - Route all mail through amavis running on the local machine, using the smtp-amavis transport defined in master.cf | ||
- | content_filter = smtp-amavis: | ||
- | |||
- | - Alerts in a user's shell when there' | ||
- | biff = no | ||
- | </ | ||
- | |||
- | The '' | ||
- | {{File|/ | ||
- | 127.0.0.0/8 Localhost | ||
- | 10.0.0.0/24 Local | ||
- | 10.0.2.0/24 Wireless | ||
- | 10.10.0.0/ | ||
- | 10.10.1.0/ | ||
- | 78.86.202.79 | ||
- | 78.86.202.81 | ||
- | 78.86.202.83 | ||
- | 78.86.202.85 | ||
- | 78.86.202.87 | ||
- | 78.86.202.89 | ||
- | 78.86.202.91 | ||
- | 78.86.202.93 | ||
- | </ | ||
- | |||
- | The '' | ||
- | {{File|/ | ||
- | server_host = ldap:// | ||
- | server_port = 389 | ||
- | start_tls = yes | ||
- | version = 3 | ||
- | |||
- | bind_dn = cn=Postfix, | ||
- | bind_pw = ****** | ||
- | |||
- | scope = sub | ||
- | search_bash = dc=sihnon, | ||
- | </ | ||
- | |||
- | === Delivery using Dovecot === | ||
- | In order to have dovecot handle the mail delivery itself, we need to add a record to the '' | ||
- | |||
- | * '' | ||
- | * '' | ||
- | |||
- | {{File|/ | ||
- | dovecot | ||
- | flags=DRhu user=mail: | ||
- | </ | ||
- | |||
- | Then tell postfix to use the dovecot transport instead of local for mail delivery | ||
- | |||
- | {{File|/ | ||
- | - Transport | ||
- | - we use mailbox_transport rather than local_transport here so that local(8) can | ||
- | - process aliases for us, and then send mails to dovecot for delivery to the end | ||
- | - users only | ||
- | mailbox_transport = dovecot | ||
- | dovecot_destination_recipient_limit = 1 | ||
- | </ | ||
- | |||
- | To deliver mail using the recipient user's uid/gid, the deliver binary needs to be run as root using setuid. This carries a security implication, | ||
- | {{Root|< | ||
- | chown root:mail / | ||
- | chmod 0754 / | ||
- | chmod u+s / | ||
- | </ | ||
- | |||
- | === Delivery using Procmail === | ||
- | Using procmail has the advantage that users can implement server-side mail filtering before mails are delivered to the inbox. The main disadvantage of using procmail alone is that the dovecot indexes won't get updated as new mail arrives. The solution is to have procmail invoke dovecot' | ||
- | |||
- | This requires a slightly different setup than the dovecot approach above: deliver is invoked by the user rather than '' | ||
- | |||
- | {{Note|The maildir needs to be created in advance, otherwise deliver will create an mbox instead of a maildir. Still looking for a way to correct this behaviour.}} | ||
- | |||
- | Setup postfix to deliver through postfix: | ||
- | {{File|/ | ||
- | - Pass all mail to procmail; the -p ensures the user's procmailrc is consulted before the system default | ||
- | mailbox_command = / | ||
- | </ | ||
- | |||
- | Setup the default procmailrc, which hands all mail straight off to deliver | ||
- | {{File|/ | ||
- | - Use dovecot to deliver mail to a maildir in / | ||
- | SHELL="/ | ||
- | SENDMAIL="/ | ||
- | DELIVER="/ | ||
- | MAILDIR="/ | ||
- | DEFAULT=" | ||
- | |||
- | - Remove the leading space on the next line, required for wiki formatting. | ||
- | :0 | ||
- | | $DELIVER -d $LOGNAME | ||
- | |||
- | - vim: set filetype=procmail: | ||
- | </ | ||
- | |||
- | Set appropriate permissions on ''/ | ||
- | {{Root|< | ||
- | chgrp mailusers / | ||
- | chmod g+w / | ||
- | chmod +t / | ||
- | </ | ||
- | |||
- | The '' | ||
- | |||
- | === Finishing up === | ||
- | Start postfix up, and make sure it starts on boot. | ||
- | {{Root|< | ||
- | rc-update add postfix default | ||
- | / | ||
- | </ | ||
- | |||
- | === SPF === | ||
- | We will set up SPF to ensure that other sites know which mail servers are allowed to send mail for the sihnon.net domain. All local machines will be configured to route mail through the mail gateways here, which will be marked as trusted. To set up SPF for the local network, add the following records to DNS. | ||
- | |||
- | {{File|/ | ||
- | ; SOA | ||
- | sihnon.net. IN MX 10 mail.sihnon.net. | ||
- | |||
- | ; SPF | ||
- | sihnon.net. IN TXT " | ||
- | mail IN TXT " | ||
- | shadow | ||
- | </ | ||
- | |||
- | {{File|/ | ||
- | ; SOA | ||
- | jellybean.sihnon.net. | ||
- | |||
- | ; SPF | ||
- | jellybean.sihnon.net. | ||
- | </ | ||
- | |||
- | We can also set up postfix to check SPF records for inbound mail too. Add the following configuration to postfix: | ||
- | {{File|/ | ||
- | policy | ||
- | user=nobody argv=/ | ||
- | </ | ||
- | |||
- | Then configure postfix to reject mail from invalid mail servers by adding the following to '' | ||
- | {{File|/ | ||
- | smtpd_recipient_restrictions = permit_sasl_authenticated, | ||
- | </ | ||
- | |||
- | Then simply reload postfix for the changes to take effect: | ||
- | {{Root|< | ||
- | / | ||
- | </ | ||
- | |||
- | ==== Content filtering ==== | ||
- | Every mail passing through the system will also be scanned for spam and viruses. Amavis is a mail content filter that handles talking to both SpamAssassin and ClamAV and dealing with mails as appropriate. | ||
- | |||
- | === Amavis === | ||
- | [[http:// | ||
- | |||
- | {{File|/ | ||
- | $mydomain = ' | ||
- | $myhostname = ' | ||
- | |||
- | - Where to send mails after they' | ||
- | $forward_method = ' | ||
- | - Same, but for notification mails rather than the original mails | ||
- | $notify_method = $forward_method; | ||
- | |||
- | - Read a list of local networks from a file | ||
- | @mynetworks_maps = read_array('/ | ||
- | |||
- | - Logging | ||
- | $DO_SYSLOG = 1; | ||
- | $syslog_ident = ' | ||
- | $syslog_facility = ' | ||
- | $syslog_priority = ' | ||
- | $log_level = 2; | ||
- | |||
- | - Which addresses to send notifications to for certain types of blocked mail | ||
- | $virus_admin = " | ||
- | $spam_admin = " | ||
- | $dsn_bcc = " | ||
- | |||
- | - Whom mail alerts appear to have come from | ||
- | $virus_admin = " | ||
- | $spam_admin = " | ||
- | $dsn_bcc = " | ||
- | $virus_admin = " | ||
- | $spam_admin = " | ||
- | $dsn_bcc = " | ||
- | |||
- | - Use multiple directories in the quarantine | ||
- | $quarantine_subdir_levels = 1; | ||
- | - Where to quarantine mails | ||
- | $virus_quarantine_method | ||
- | $spam_quarantine_method | ||
- | $banned_files_quarantine_method | ||
- | $bad_header_quarantine_method | ||
- | $virus_quarantine_to | ||
- | $banned_quarantine_to | ||
- | $bad_header_quarantine_to = ' | ||
- | $spam_quarantine_to | ||
- | |||
- | - Add a header to the mail, showing it was scanned by us | ||
- | $X_HEADER_TAG = ' | ||
- | $remove_existing_x_scanned_headers = 0; | ||
- | |||
- | - Tag message subjects if nasties are found | ||
- | $sa_spam_subject_tag = ' | ||
- | $undecipherable_subject_tag = ' | ||
- | |||
- | - spamassassin scoring | ||
- | $sa_local_tests_only = 0; | ||
- | $sa_mail_body_size_limit = 400*1024; | ||
- | $sa_tag_level_deflt | ||
- | $sa_tag2_level_deflt = 6.31; | ||
- | $sa_kill_level_deflt = $sa_tag2_level_deflt; | ||
- | $sa_dsn_cutoff_level = 9; | ||
- | |||
- | - Allow recipient delimiters | ||
- | $recipient_delimiter = ' | ||
- | |||
- | - Enable full debugging for mails from these addresses | ||
- | @debug_sender_maps = ( [[" | ||
- | </ | ||
- | |||
- | Also, go through the configuration file and enable/ | ||
- | |||
- | Amavis makes use of a list of local, " | ||
- | {{File|/ | ||
- | - Loopback | ||
- | 127.0.0.1 | ||
- | - Local networks | ||
- | 10.0.0.0/24 | ||
- | 10.0.2.0/24 | ||
- | - VPN | ||
- | 10.10.0.0/ | ||
- | 10.10.1.0/ | ||
- | - Public networks | ||
- | 78.86.202.79 | ||
- | 78.86.202.81 | ||
- | 78.86.202.83 | ||
- | 78.86.202.85 | ||
- | 78.86.202.87 | ||
- | 78.86.202.89 | ||
- | 78.86.202.91 | ||
- | 78.86.202.93 | ||
- | </ | ||
- | |||
- | Start amavis up, and ensure it runs on boot: | ||
- | {{Root|< | ||
- | / | ||
- | rc-update add amavisd default | ||
- | </ | ||
- | |||
- | === Virus scanning === | ||
- | Edit the clam configuration files to include the settings listed below; these are fairly self explanatory but the comments in the file explain them well. The full configuration files are stored in subversion under [[https:// | ||
- | |||
- | {{File|/ | ||
- | LogFile / | ||
- | LogTime yes | ||
- | LogSyslog yes | ||
- | LogFacility LOG_MAIL | ||
- | PidFile / | ||
- | LocalSocket / | ||
- | FixStaleSocket yes | ||
- | User clamav | ||
- | AllowSupplementaryGroups yes | ||
- | </ | ||
- | |||
- | {{File|/ | ||
- | LogSyslog yes | ||
- | LogFacility LOG_MAIL | ||
- | PidFile / | ||
- | DatabaseOwner clamav | ||
- | AllowSupplementaryGroups yes | ||
- | DatabaseMirror db.uk.clamav.net | ||
- | DatabaseMirror database.clamav.net | ||
- | ScriptedUpdates yes | ||
- | NotifyClamd / | ||
- | </ | ||
- | |||
- | Clamd runs under its own user account but will need access to the files processed by amavis; add it to the amavis group to ensure it can. | ||
- | {{Root|< | ||
- | gpasswd -a clamav amavis | ||
- | </ | ||
- | |||
- | Now start clam, and ensure it starts on every boot with the following: | ||
- | {{Root|< | ||
- | / | ||
- | rc-update add clamd default | ||
- | </ | ||
- | |||
- | === Spam filtering === | ||
- | Certain plugins are disabled in spamassassin by default, so we will want to enable these. | ||
- | {{File|/ | ||
- | loadplugin Mail:: | ||
- | loadplugin Mail:: | ||
- | </ | ||
- | {{File|/ | ||
- | loadplugin Mail:: | ||
- | </ | ||
- | |||
- | Then configure spamassassin: | ||
- | {{File|/ | ||
- | rewrite_header Subject {SPAM} | ||
- | use_bayes 1 | ||
- | bayes_auto_learn 1 | ||
- | |||
- | use_razor2 1 | ||
- | use_pyzor 1 | ||
- | |||
- | ok_languages en | ||
- | ok_locales en | ||
- | |||
- | - Reduce the scores for mail from DK/DKIM verified domains | ||
- | - since they are less likely to be spam | ||
- | score DK_VERIFIED -1.5 | ||
- | score DK_POLICY_SIGNSOME 0 | ||
- | score DK_POLICY_TESTING | ||
- | score DKIM_VERIFIED -1.5 | ||
- | </ | ||
- | |||
- | In order to use razor and pyzor, we need to configure them for the user account which will be running them - '' | ||
- | {{Command|< | ||
- | sudo su -s /bin/bash -l amavis | ||
- | razor-admin -create | ||
- | razor-admin -register -user=postmaster@sihnon.net | ||
- | pyzor discover | ||
- | exit | ||
- | </ | ||
- | |||
- | ==== Dovecot ==== | ||
- | Dovecot is fairly simple to use, just install and configure it, then start it up. | ||
- | |||
- | Dovecot ships with a useful command shows which configuration options have been changed from the defaults values. This is useful for debugging, or documenting which options have been set. | ||
- | {{Root|< | ||
- | dovecot -n | ||
- | </ | ||
- | |||
- | These configuration files are stored in subversion, under [[https:// | ||
- | |||
- | Dovecot has been configured with the following settings changed: | ||
- | {{File|/ | ||
- | protocols: imaps | ||
- | listen: 78.86.202.85 | ||
- | ssl_cert_file: | ||
- | ssl_key_file: | ||
- | login_dir: / | ||
- | login_executable: | ||
- | first_valid_uid: | ||
- | last_valid_uid: | ||
- | mail_privileged_group: | ||
- | mail_location: | ||
- | mail_debug: yes | ||
- | auth default: | ||
- | passdb: | ||
- | driver: ldap | ||
- | args: / | ||
- | userdb: | ||
- | driver: prefetch | ||
- | userdb: | ||
- | driver: ldap | ||
- | args: / | ||
- | socket: | ||
- | type: listen | ||
- | client: | ||
- | path: / | ||
- | mode: 432 | ||
- | user: postfix | ||
- | group: mail | ||
- | master: | ||
- | path: / | ||
- | mode: 384 | ||
- | user: mail | ||
- | group: mail | ||
- | </ | ||
- | |||
- | The '' | ||
- | {{File|/ | ||
- | - connection details | ||
- | uris = ldap:// | ||
- | ldap_version = 3 | ||
- | tls = yes | ||
- | tls_ca_cert_file = / | ||
- | |||
- | - authenticate users by looking up the password in the directory using a proxy user | ||
- | auth_bind = no | ||
- | dn = cn=dovecot, | ||
- | dnpass = ****** | ||
- | |||
- | - Directory details | ||
- | base = ou=People, | ||
- | scope = subtree | ||
- | user_arrs = homeDirectory=home, | ||
- | - we're matching against local users only, so use the local part of the email address given by postfix | ||
- | user_filter = (& | ||
- | pass_attrs = uid=user, | ||
- | pass_filter = (& | ||
- | default_pass_scheme = SSHA | ||
- | |||
- | </ | ||
- | |||
- | Start dovecot up, and make sure it starts on boot: | ||
- | {{Root|< | ||
- | rc-update add dovecot default | ||
- | / | ||
- | </ | ||
- | |||
- | ==== Webmail ==== | ||
- | [[http:// | ||
- | |||
- | Check out a copy of the latest release above the webroot, then symlink it into the webroot itself: | ||
- | {{Command|< | ||
- | cd / | ||
- | svn co https:// | ||
- | ln -s roundcube-svn html/ | ||
- | </ | ||
- | |||
- | Use the installer to set up the configuration, | ||
- | {{File|/ | ||
- | $rcmail_config[[' | ||
- | $rcmail_config[[' | ||
- | $rcmail_config[[' | ||
- | $rcmail_config[[' | ||
- | $rcmail_config[[' | ||
- | $rcmail_config[[' | ||
- | $rcmail_config[[' | ||
- | $rcmail_config[[' | ||
- | </ | ||
- | |||
- | === Upgrading === | ||
- | Change the current directory to the new branch, eg | ||
- | {{Command|< | ||
- | svn switch https:// | ||
- | </ | ||
- | |||
- | Then run the updater script to make sure the configuration file is up to date: | ||
- | {{Command|< | ||
- | ./ | ||
- | </ | ||
- | |||
- | ==== New mail alerts ==== | ||
- | '' | ||
- | |||
- | ===== Troubleshooting ===== | ||
- | <s>If you see errors like the following, the problem is related to the filesystem permissions - check that the users dovecot runs under has access to the mail directories.</ | ||
- | |||
- | < | ||
- | |||
- | ===== See also ===== |