User Tools

Site Tools


computing:mailserver

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
computing:mailserver [2022/12/11 03:51] oemb1905computing:mailserver [2023/08/06 18:42] (current) oemb1905
Line 2: Line 2:
   * **mailserver**    * **mailserver** 
   * **Jonathan Haack**   * **Jonathan Haack**
-  * **Haack's Networking** +  * **Haack's Networking**  
-  * **webmaster@haacksnetworking.org**+  * **webmaster@haacksnetworking.org**   
  
 ------------------------------------------- -------------------------------------------
- +  
-//mailserver//      +//mailserver//  
  
 ------------------------------------------- -------------------------------------------
Line 62: Line 62:
 Now, set up the server block for your mail server's website: Now, set up the server block for your mail server's website:
  
-  sudo nano /etc/apache2/sites-available/mail.example.com.conf+  sudo nano /etc/nginx/conf.d/mail.example.com.conf
   sudo mkdir -p /usr/share/nginx/html/   sudo mkdir -p /usr/share/nginx/html/
      
Line 83: Line 83:
   sudo certbot certonly -a nginx --agree-tos --no-eff-email --staple-ocsp --email email@email.com -d mail.example.com   sudo certbot certonly -a nginx --agree-tos --no-eff-email --staple-ocsp --email email@email.com -d mail.example.com
      
-Now, let's configure Dovecot and submission on 587 and 465 by editing ''sudo nano /etc/postfix/master.cf'' and adding these blocks, which among other things, will allow submission to use TLS.+Now, let's configure postfix to work together with Dovecot/submission on 587 and 465 and to use TLS by editing ''sudo nano /etc/postfix/master.cf'' as follows:
  
   submission     inet        -    y    -    -    smtpd   submission     inet        -    y    -    -    smtpd
Line 121: Line 121:
   smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1   smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
  
-Note that when upgrading postfixplease select "No configuration" as otherwise it will overwrite out configurations. +Nowwe can install dovecot and configure it to use IMAP, change the default mailbox location, and add dovecot to the mail group:
- +
  
- --- //[[jonathan@haacksnetworking.org|oemb1905]] 2022/12/05 21:03//+  sudo apt install dovecot-core dovecot-imapd 
 +  sudo nano /etc/dovecot/dovecot.conf 
 +  <protocols = imap> 
 +  sudo nano /etc/dovecot/conf.d/10-mail.conf 
 +  <mail_location = maildir:~/Maildir> 
 +  sudo adduser dovecot mail 
 +   
 +We will now configure dovecot to use lmtp and in so doing use spam sieve and other modules: 
 + 
 +  sudo apt install dovecot-lmtpd 
 +  sudo nano /etc/dovecot/dovecot.conf 
 +  <protocols = imap lmtp> 
 + 
 +Now, we need to edit ''sudo nano /etc/dovecot/conf.d/10-master.conf'' and make sure that dovecot can leverage lmtp: 
 + 
 +  service lmtp { 
 +    unix_listener /var/spool/postfix/private/dovecot-lmtp { 
 +     mode = 0600 
 +     user = postfix 
 +     group = postfix 
 +    } 
 +   } 
 + 
 +Similarly, we need to edit postfix for lmtp: 
 + 
 +  sudo nano /etc/postfix/main.cf 
 +  <mailbox_transport = lmtp:unix:private/dovecot-lmtp> 
 +  <smtputf8_enable = no> 
 + 
 +Next, let's configure dovecot authorization: 
 + 
 +  sudo nano /etc/dovecot/conf.d/10-auth.conf 
 +  <disable_plaintext_auth = yes> 
 +  <auth_username_format = %n> 
 +  <auth_mechanisms = plain login> 
 +   
 +Now, configure SSL/TLS encryption in dovecot: 
 + 
 +  sudo nano /etc/dovecot/conf.d/10-ssl.conf 
 +  <ssl = required> 
 +  <ssl_cert = </etc/letsencrypt/live/mail.example.com/fullchain.pem> 
 +  <ssl_key = </etc/letsencrypt/live/mail.example.com/privkey.pem> 
 +  <ssl_prefer_server_ciphers = yes> 
 +  <ssl_min_protocol = TLSv1.2> 
 +   
 +SASL configuration by editing ''sudo nano /etc/dovecot/conf.d/10-master.conf'' and adding this block: 
 + 
 +  service auth { 
 +    unix_listener /var/spool/postfix/private/auth { 
 +      mode = 0660 
 +      user = postfix 
 +      group = postfix 
 +    } 
 +  } 
 +   
 +If you have errors or can't connect your email client at this point, you can test your handshakes as follows: 
 + 
 +  openssl s_client -connect mail.example.com:465 
 +  openssl s_client -starttls smtp -connect mail.example.com:25 
 +   
 +Now it is time to setup an spf policy agent so that the incoming email that is received checks for validity of spf records. **Do not confuse this with creating an spf TXT record for your outgoing email.** 
 + 
 +  sudo apt install postfix-policyd-spf-python 
 +  sudo nano /etc/postfix/master.cf 
 +  <policyd-spf  unix  -                               spawn> 
 +  <user=policyd-spf argv=/usr/bin/policyd-spf> 
 +  sudo nano /etc/postfix/main.cf 
 +  <policyd-spf_time_limit = 3600> 
 +  <smtpd_recipient_restrictions => 
 +   <permit_mynetworks,> 
 +   <permit_sasl_authenticated,> 
 +   <reject_unauth_destination,> 
 +   <check_policy_service unix:private/policyd-spf> 
 + 
 +Now, it is time to set up DKIM on your server. After creating the DKIM record/key on your server, you will need to create a corresponding TXT record for it to establish that anything over smtp with that signature is, in fact, you/your server. 
 + 
 +  sudo apt install opendkim opendkim-tools 
 +  sudo adduser postfix opendkim 
 +  sudo nano /etc/opendkim.conf 
 +  <Canonicalization   relaxed/simple> 
 +  <Mode               sv> 
 +  <SubDomains         no> 
 +  <Nameservers     8.8.8.8,1.1.1.1> 
 +  <KeyTable           refile:/etc/opendkim/key.table> 
 +  <SigningTable       refile:/etc/opendkim/signing.table> 
 +  <ExternalIgnoreList  /etc/opendkim/trusted.hosts> 
 +  <InternalHosts       /etc/opendkim/trusted.hosts> 
 + 
 +Now that the configuration for DKIM is ready, let's create the keys and content for the locations specified above: 
 + 
 +  sudo mkdir -p /etc/opendkim/keys 
 +  sudo chown -R opendkim:opendkim /etc/opendkim 
 +  sudo chmod 711 /etc/opendkim/keys 
 +  sudo nano /etc/opendkim/signing.table 
 +  <*@example.com      default._domainkey.example.com> 
 +  <*@*.example.com    default._domainkey.example.com> 
 +  sudo nano /etc/opendkim/key.table 
 +  <default._domainkey.example.com     example.com:default:/etc/opendkim/keys/example.com/default.private> 
 +  sudo nano /etc/opendkim/trusted.hosts 
 +  <.domain.com> 
 +  sudo mkdir /etc/opendkim/keys/example.com 
 +  sudo opendkim-genkey -b 2048 -d example.com -D /etc/opendkim/keys/example.com -s default -v 
 +  sudo chown opendkim:opendkim /etc/opendkim/keys/example.com/default.private 
 +  sudo chmod 600 /etc/opendkim/keys/example.com/default.private 
 + 
 +It's now time to create the corresponding TXT record for this DKIM key. To do that, display the key with ''sudo cat /etc/opendkim/keys/example.com/default.txt'' and then copy everything between the parentheses into your TXT record with ''default._domainkey'' as the host. After the DKIM TXT record caches, test it as follows: 
 + 
 +  sudo opendkim-testkey -d example.com -s default -vvv 
 +   
 +Note that that output will display "key not secure" unless you configure DNSSEC, which this tutorial has not done. It's now time to configure postfix to leverage this DKIM key. 
 + 
 +  sudo mkdir /var/spool/postfix/opendkim 
 +  sudo chown opendkim:postfix /var/spool/postfix/opendkim 
 +  sudo nano /etc/opendkim.conf 
 +  <Socket    local:/var/spool/postfix/opendkim/opendkim.sock> 
 +  sudo nano /etc/default/opendkim 
 +  <SOCKET="local:/var/spool/postfix/opendkim/opendkim.sock"> 
 +  sudo nano /etc/postfix/main.cf 
 +  <milter_default_action = accept> 
 +  <milter_protocol = 6> 
 +  <smtpd_milters = local:opendkim/opendkim.sock> 
 +  <non_smtpd_milters = $smtpd_milters> 
 + 
 +It's now a good time to test your email quality with [[https://mail-tester.com|Mail Tester]] to see if you got a 10/10 score. When upgrading postfix on the server, select "No configuration" as otherwise it will overwrite the configurations above. If you need help with creating spf, dmarc, or dkim TXT records, see [[https://wiki.haacksnetworking.org/doku.php?id=computing:spfdkim|spfdkim]]. Another optional setting is to reject incoming email that lacks a PTR (reverse DNS) record.  
 + 
 +  sudo nano /etc/postfix/main.cf 
 +  <smtpd_sender_restrictions => 
 +    <permit_mynetworks> 
 +    <permit_sasl_authenticated> 
 +    <reject_unknown_reverse_client_hostname> 
 +     
 +To set up email header and/or body checks to prevent spam: 
 + 
 +  sudo apt install postfix-pcre 
 +  sudo nano /etc/postfix/main.cf 
 +  <header_checks = pcre:/etc/postfix/header_checks> 
 +  <body_checks = pcre:/etc/postfix/body_checks> 
 +   
 +You will then need to configure the files with whatever strings you expect spam headers or bodies to have, and either reject them and/or discard them. You will also need to rebuild the indexes. 
 + 
 +  sudo nano /etc/postfix/header_checks 
 +  </free mortgage quote/     REJECT> 
 +  </repair your credit/     DISCARD> 
 +  sudo postmap /etc/postfix/header_checks 
 +  sudo nano /etc/postfix/body_checks 
 +  </free mortgage quote/     REJECT> 
 +  </repair your credit/      DISCARD> 
 +  sudo postmap /etc/postfix/body_checks 
 + 
 +In general, be careful of setting your own TXT records for dmarc and spf with p=reject and -all because recipient's incoming email servers might forward the email on to another server, which will then appear to not originate from the proper location. Setting p=quarantine and ~all are good options in the middle for how servers should treat your email (or those trying to look like your email). As far as how you receive email is concerned, be careful in making your incoming server's rules too strict, otherwise you will never see emails arrive from your friends who might not have DNS records set up as strictly as your settings require. Lastly, you may optionally set up dmarc verification and reporting with openDMARC. 
 + 
 +  sudo apt install opendmarc 
 +  <no to db configure> 
 +  sudo nano /etc/opendmarc.conf 
 +  <AuthservID OpenDMARC> 
 +  <TrustedAuthservIDs mail.yourdomain.com> 
 +  <RejectFailures true> 
 +  <IgnoreAuthenticatedClients true> 
 +  <SPFSelfValidate true> 
 +  <Socket local:/var/spool/postfix/opendmarc/opendmarc.sock> 
 +  sudo mkdir -p /var/spool/postfix/opendmarc 
 +  sudo chown opendmarc:opendmarc /var/spool/postfix/opendmarc -R 
 +  sudo chmod 750 /var/spool/postfix/opendmarc/ -R 
 +  sudo adduser postfix opendmarc 
 +  sudo systemctl restart opendmarc 
 +   
 +Now, configure postfix to work with openDMARC. Add the openDMARC socket to the milter block you created earlier. 
 + 
 +  sudo nano /etc/postfix/main.cf 
 +  <milter_default_action = accept> 
 +  <milter_protocol = 6> 
 +  <smtpd_milters = local:opendkim/opendkim.sock,local:opendmarc/opendmarc.sock> 
 +  <non_smtpd_milters = $smtpd_milters> 
 +  sudo systemctl restart postfix 
 +   
 +This about covers everything. The only missing part is how to get past picky microsoft users and/or automate or simplify account creation. Okay, to view and/or delete messages from postfix mailq: 
 + 
 +  mailq 
 +  postcat -q E900C4780073 
 +  postsuper -d E900C4780073 
 +  postsuper -d ALL 
 +   
 +If you have issues, it's good to be familiar with some different uses of the ''dig'' command to test your records. Here's how to check dmarc, dkim, spf, and ptr. The ''+short'' is optional, of course. I also included how you can verify your dkim key as well. 
 +   
 +  dig txt +short _dmarc.jonathanhaack.com 
 +  dig txt +short _dmarc.haacksnetworking.org 
 +  dig default._domainkey.jonathanhaack.com txt 
 +  dig default._domainkey.haacksnetworking.org txt 
 +  dig txt +short jonathanhaack.com 
 +  dig txt +short haacksnetworking.org 
 +  dig -x 8.28.86.130 +short 
 +  dig -x 8.28.86.125 +short 
 +  sudo opendkim-testkey -d jonathanhaack.com -s default -vvv 
 +  sudo opendkim-testkey -d haacksnetworking.org -s default -vvv 
 +   
 +Also, please note that the above applies to clients connecting to the domain. If you intend to also host websites/content on the same host as the mail server, then you will also need to set up dmarc, spf, and mx records for the subdomain, mail.example.com. You will not need to setup dkim nor change the PTR. To test the validity of the command line email set up, ssh into your server and send an email as follows: 
 + 
 +  echo "Hi, I am testing the subdomain email health." | mail -s "CLI Email Test" oemb1905@jonathanhaack.com 
 +   
 +Setting up dovecot-sieve.  
 + 
 +  sudo apt install dovecot-sieve dovecot-managesieved 
 +  sudo nano /etc/dovecot/dovecot.conf 
 + 
 +Set to: 
 + 
 +  protocols = imap lmtp sieve 
 +   
 +Then, open 
 + 
 +  sudo nano /etc/dovecot/conf.d/15-lda.conf 
 + 
 +Set to: 
 + 
 +  protocol lda { 
 +    mail_plugins = $mail_plugins sieve 
 +  } 
 +   
 +Finally, 
 + 
 +  sudo nano /etc/dovecot/conf.d/20-lmtp.conf 
 + 
 +Which should be: 
 + 
 +  protocol lmtp { 
 +    mail_plugins = quota sieve 
 +  } 
 +   
 +Restart your services ''systemctl restart dovecot postfix'' and it should be active. I was having trouble with Nextcloud mail because it could not locate the default / expected IMAP folders. To mitigate that, set them up to be created automatically as follows: 
 + 
 +  sudo nano /etc/dovecot/conf.d/15-mailboxes.conf 
 + 
 +An example block: 
 + 
 +  mailbox Drafts { 
 +    auto = create 
 +    special_use = \Drafts 
 +  } 
 + 
 +Simply add the ''auto = create'' to whichever directories you need.  
 +   
 + --- //[[jonathan@haacksnetworking.org|oemb1905]] 2023/08/06 18:39//
computing/mailserver.1670730678.txt.gz · Last modified: 2022/12/11 03:51 by oemb1905