1449 lines
49 KiB
HTML
1449 lines
49 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en-us">
|
|
<head>
|
|
|
|
<meta charset="utf-8" />
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
|
|
|
|
|
|
|
<meta name="twitter:card" content="summary"/>
|
|
|
|
|
|
|
|
|
|
<meta name="twitter:title" content="Setting up a mail server"/>
|
|
<meta name="twitter:description" content=""/>
|
|
<meta name="twitter:site" content="@"/>
|
|
|
|
|
|
|
|
<meta property="og:title" content="Setting up a mail server · Sysadmining. All day. Every day." />
|
|
<meta property="og:site_name" content="Sysadmining. All day. Every day." />
|
|
<meta property="og:url" content="https://www.captainark.net/2015/04/24/setting-up-a-mail-server/" />
|
|
|
|
|
|
|
|
<meta property="og:image" content="/images/cover.jpg"/>
|
|
|
|
|
|
|
|
|
|
<meta property="og:description" content="" />
|
|
<meta property="og:type" content="article" />
|
|
<meta property="article:published_time" content="2015-04-24T00:00:00+01:00" />
|
|
|
|
|
|
|
|
|
|
<title>Setting up a mail server · Sysadmining. All day. Every day.</title>
|
|
|
|
|
|
<meta name="description" content="In this first tutorial, I&rsquo;ll explain how I&rsquo;ve configured my mail server using the following :
|
|
A server running Linux Debian (jessie) ; Postfix ; Po" />
|
|
|
|
|
|
<meta name="HandheldFriendly" content="True" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
|
|
<link rel="shortcut icon" href="/images/favicon.ico">
|
|
<link rel="apple-touch-icon" href="/images/apple-touch-icon.png" />
|
|
|
|
<link rel="stylesheet" type="text/css" href="/css/screen.css" />
|
|
<link rel="stylesheet" type="text/css" href="/css/nav.css" />
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/solarized-light.min.css">
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
|
|
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
|
|
|
|
|
|
|
|
<link href="/index.xml" rel="alternate" type="application/rss+xml" title="Sysadmining. All day. Every day." />
|
|
|
|
|
|
|
|
<meta name="generator" content="Hugo 0.53" />
|
|
|
|
<link rel="canonical" href="https://www.captainark.net/2015/04/24/setting-up-a-mail-server/" />
|
|
|
|
|
|
|
|
|
|
<script type="application/ld+json">
|
|
{
|
|
"@context": "https://schema.org",
|
|
"@type": "Article",
|
|
"publisher": {
|
|
"@type": "Organization",
|
|
"name": ,
|
|
"logo": https://www.captainark.net/images/logo.png
|
|
},
|
|
"author": {
|
|
"@type": "Person",
|
|
"name": ,
|
|
|
|
"image": {
|
|
"@type": "ImageObject",
|
|
"url": https://www.captainark.net/images/author.jpg,
|
|
"width": 250,
|
|
"height": 250
|
|
},
|
|
|
|
"url": https://www.captainark.net,
|
|
"sameAs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
],
|
|
"description": Geek | Gamer | TV Shows Aficionado
|
|
|
|
},
|
|
"headline": Setting up a mail server,
|
|
"name": Setting up a mail server,
|
|
"wordCount": 4133,
|
|
"timeRequired": "PT20M",
|
|
"inLanguage": {
|
|
"@type": "Language",
|
|
"alternateName": en
|
|
},
|
|
"url": https://www.captainark.net/2015/04/24/setting-up-a-mail-server/,
|
|
"datePublished": 2015-04-24T00:00Z,
|
|
"dateModified": 2015-04-24T00:00Z,
|
|
|
|
|
|
"description": ,
|
|
"mainEntityOfPage": {
|
|
"@type": "WebPage",
|
|
"@id": https://www.captainark.net/2015/04/24/setting-up-a-mail-server/
|
|
}
|
|
}
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
(function(f, a, t, h, o, m){
|
|
a[h]=a[h]||function(){
|
|
(a[h].q=a[h].q||[]).push(arguments)
|
|
};
|
|
o=f.createElement('script'),
|
|
m=f.getElementsByTagName('script')[0];
|
|
o.async=1; o.src=t; o.id='fathom-script';
|
|
m.parentNode.insertBefore(o,m)
|
|
})(document, window, '//stats.captainark.net/tracker.js', 'fathom');
|
|
fathom('set', 'siteId', 'UAIPU');
|
|
fathom('trackPageview');
|
|
</script>
|
|
|
|
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fork-awesome@1.1.5/css/fork-awesome.min.css" integrity="sha256-P64qV9gULPHiZTdrS1nM59toStkgjM0dsf5mK/UwBV4=" crossorigin="anonymous">
|
|
|
|
|
|
</head>
|
|
<body class="nav-closed">
|
|
|
|
<div class="nav">
|
|
<h3 class="nav-title">Menu</h3>
|
|
<a href="#" class="nav-close">
|
|
<span class="hidden">Close</span>
|
|
</a>
|
|
<ul>
|
|
|
|
|
|
|
|
<h3>This site</h3>
|
|
<li class="nav-opened" role="presentation">
|
|
<a href="/">Home</a>
|
|
</li>
|
|
|
|
|
|
<li class="nav-opened" role="presentation">
|
|
<a href="/about">About</a>
|
|
</li>
|
|
|
|
|
|
<li class="nav-opened" role="presentation">
|
|
<a href="/resume">Resume</a>
|
|
</li>
|
|
|
|
<h3>Other services</h3>
|
|
<li class="nav-opened" role="presentation">
|
|
<a href="https://git.captainark.net">Gitea</a>
|
|
</li>
|
|
|
|
|
|
<li class="nav-opened" role="presentation">
|
|
<a href="https://pics.captainark.net">Chevereto</a>
|
|
</li>
|
|
|
|
|
|
<li class="nav-opened" role="presentation">
|
|
<a href="https://paste.captainark.net">Privatebin</a>
|
|
</li>
|
|
|
|
|
|
<li class="nav-opened" role="presentation">
|
|
<a href="https://chat.captainark.net">Rocket.Chat</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
<a class="subscribe-button icon-feed" href="/index.xml">Subscribe</a>
|
|
|
|
</div>
|
|
<span class="nav-cover"></span>
|
|
|
|
|
|
<div class="site-wrapper">
|
|
|
|
|
|
|
|
<header class="main-header post-head no-cover">
|
|
<nav class="main-nav clearfix">
|
|
|
|
|
|
|
|
<a class="blog-logo" href="https://www.captainark.net/"><img src="/images/logo.png" alt="Home" /></a>
|
|
|
|
|
|
<a class="menu-button" href="#"><span class="burger">☰</span><span class="word">Menu</span></a>
|
|
|
|
</nav>
|
|
</header>
|
|
|
|
|
|
|
|
<main class="content" role="main">
|
|
|
|
|
|
|
|
|
|
<article class="post post">
|
|
|
|
<header class="post-header">
|
|
<h1 class="post-title">Setting up a mail server</h1>
|
|
<small></small>
|
|
|
|
<section class="post-meta">
|
|
|
|
<time class="post-date" datetime="2015-04-24T00:00:00+01:00">
|
|
24 April 2015
|
|
</time>
|
|
|
|
|
|
</section>
|
|
</header>
|
|
|
|
<section class="post-content">
|
|
|
|
|
|
<p>In this first tutorial, I’ll explain how I’ve configured my mail server using the following :</p>
|
|
|
|
<ul>
|
|
<li>A server running Linux Debian (jessie) ;</li>
|
|
<li>Postfix ;</li>
|
|
<li>Postfix-policyd-spf-python ;</li>
|
|
<li>Dovecot ;</li>
|
|
<li>Spamassassin ;</li>
|
|
<li>OpenDKIM ;</li>
|
|
<li>OpenDMARC ;</li>
|
|
<li>Monit ;</li>
|
|
<li>Rainloop.</li>
|
|
</ul>
|
|
|
|
<p>I’m assuming you have some basic knowledge of Linux and DNS configuration.</p>
|
|
|
|
<p>You can host this server at home, but you might have issues with your ISP not allowing outbound traffic on TCP port 25, and your emails might be considered to be spam by other providers if your IP is dynamic and/or you can’t configure a reverse DNS record on it.</p>
|
|
|
|
<p>The cheapest VMs from <a href="https://www.digitalocean.com/?refcode=1cd69e4c3389">DigitalOcean</a> or <a href="http://www.vultr.com/?ref=6804947">Vultr</a> are powerful enough to have this configuration running smoothly.</p>
|
|
|
|
<p>We’ll also need a SSL certificate for this configuration. You can create an auto-signed one or get a free valid one from <a href="http://www.startssl.com/">StartSSL</a>. For the purpose of this tutorial, I’ll consider you’ve chosen the latter.</p>
|
|
|
|
<p>You’ll also need a domain name. I’ve chosen <a href="http://www.namecheap.com/?aff=85990">Namecheap</a> as a registrar. I won’t go into details on how to configure it, but you’ll need at the very least a A record on your server’s IP as well as a MX record pointing to it.</p>
|
|
|
|
<p>I use the captainark.net domain as an example throughout this tutorial. You’ll have to use your actual domain for your configuration to work !</p>
|
|
|
|
<p><em>Note: links in this section are sponsored.</em></p>
|
|
|
|
<h2 id="initial-configuration">Initial configuration</h2>
|
|
|
|
<h3 id="installing-the-required-packages">Installing the required packages</h3>
|
|
|
|
<p>First thing first, we need to install the packages we’ll need for this configuration :</p>
|
|
|
|
<pre><code class="language-bash">apt update
|
|
|
|
apt install mysql-server mysql-client postfix postfix-mysql \
|
|
postfix-policyd-spf-python dovecot-core dovecot-imapd dovecot-lmtpd \
|
|
dovecot-mysql dovecot-sieve dovecot-managesieved dovecot-antispam \
|
|
opendkim opendkim-tools monit opendmarc spamassassin spamc
|
|
</code></pre>
|
|
|
|
<p>During its installation, Postfix will prompt you with configuration questions. Choose “Internet Site”, and when asked about your System mail name, provide it with your server’s FQDN (it should be the output of the <code>hostname -f</code> command on your server).</p>
|
|
|
|
<p>You’ll also have to set-up a password for the MySQL root user.</p>
|
|
|
|
<h3 id="additional-configuration">Additional configuration</h3>
|
|
|
|
<p>The PTR records on your server’s IPv4 and/or IPv6 should match your server’s FQDN (a <code>dig -x</code> on your server’s IP should match a <code>hostname -f</code> on your server).</p>
|
|
|
|
<p>You’ll have to open the following TCP ports on your server for this configuration to work : 25, 465, 587 and 993.</p>
|
|
|
|
<p>If you don’t want to have to remember the root user MySQL password, you can create a .my.cnf file in your current user home directory containing the following lines :</p>
|
|
|
|
<pre><code class="language-bash">[client]
|
|
host = localhost
|
|
user = root
|
|
password = myverysecurepassword
|
|
socket = /var/run/mysqld/mysqld.sock
|
|
</code></pre>
|
|
|
|
<p>Once it has been created, change the permissions on the file to make sure no other user can read it :</p>
|
|
|
|
<pre><code class="language-bash">chmod 600 ~/.my.cnf
|
|
</code></pre>
|
|
|
|
<p>I also like to change the default MySQL shell to see what database I’m using at any given time. Since I use bash, I achieve this the following way :</p>
|
|
|
|
<pre><code class="language-bash">echo 'export MYSQL_PS1="[\u@\h] (\d)> "' > ~/.bash_aliases
|
|
</code></pre>
|
|
|
|
<p>You’ll have to logout from the current shell for the modification to be taken into account (if you’re using SSH, log out and back into your server).</p>
|
|
|
|
<p>You should now be able to log into MySQL without specifying a password, and it should look like this :</p>
|
|
|
|
<pre><code class="language-bash">:~$ mysql mysql
|
|
[...]
|
|
[root@localhost] (mysql)>
|
|
</code></pre>
|
|
|
|
<h2 id="configuring-the-mysql-database">Configuring the MySQL database</h2>
|
|
|
|
<h3 id="initial-configuration-1">Initial configuration</h3>
|
|
|
|
<p>We now need to configure the MySQL database Postfix and Dovecot will be using. In this tutorial, we’ll be calling it “mail”, but you can name it whatever you want.</p>
|
|
|
|
<p>First, in a mysql shell, let’s create the MySQL database :</p>
|
|
|
|
<pre><code class="language-sql">CREATE DATABASE mail;
|
|
</code></pre>
|
|
|
|
<p>Now, we are going to create the user that Postfix and Dovecot will be using to access the database. We will only be granting this user select permission :</p>
|
|
|
|
<pre><code class="language-sql">GRANT SELECT ON mail.* TO 'mail'@'localhost' IDENTIFIED BY 'mailpassword';
|
|
FLUSH PRIVILEGES;
|
|
</code></pre>
|
|
|
|
<p>We are now going to create the necessary tables for our needs. Let’s first use the mail database :</p>
|
|
|
|
<pre><code class="language-sql">USE mail;
|
|
</code></pre>
|
|
|
|
<p>The first table we are going to create will contain the domains we will be using with our mail server :</p>
|
|
|
|
<pre><code class="language-sql">CREATE TABLE `virtual_domains` (
|
|
`id` INT NOT NULL AUTO_INCREMENT,
|
|
`name` VARCHAR(50) NOT NULL,
|
|
PRIMARY KEY (`id`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
</code></pre>
|
|
|
|
<p>Then, we are going to create the table that will contain our users and their password :</p>
|
|
|
|
<pre><code class="language-sql">CREATE TABLE `virtual_users` (
|
|
`id` INT NOT NULL AUTO_INCREMENT,
|
|
`domain_id` INT NOT NULL,
|
|
`password` VARCHAR(106) NOT NULL,
|
|
`email` VARCHAR(120) NOT NULL,
|
|
PRIMARY KEY (`id`),
|
|
UNIQUE KEY `email` (`email`),
|
|
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
</code></pre>
|
|
|
|
<p>Finally, the last table we are going to create will contain our mail aliases :</p>
|
|
|
|
<pre><code class="language-sql">CREATE TABLE `virtual_aliases` (
|
|
`id` INT NOT NULL AUTO_INCREMENT,
|
|
`domain_id` INT NOT NULL,
|
|
`source` varchar(100) NOT NULL,
|
|
`destination` varchar(100) NOT NULL,
|
|
PRIMARY KEY (`id`),
|
|
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
</code></pre>
|
|
|
|
<h3 id="domains-users-and-aliases-management">Domains, users and aliases management</h3>
|
|
|
|
<p>We are now going to add data to the tables we have created.</p>
|
|
|
|
<p>First, let’s add a domain to the virtual_domains table :</p>
|
|
|
|
<pre><code class="language-sql">INSERT INTO virtual_domains (`name`) VALUES ('captainark.net');
|
|
</code></pre>
|
|
|
|
<p>We can now create users associated with this domain in the virtual_users table :</p>
|
|
|
|
<pre><code class="language-sql">INSERT INTO virtual_users (`domain_id`, `password` , `email`) VALUES
|
|
('1', ENCRYPT('notanactualpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))),
|
|
'example@captainark.net');
|
|
</code></pre>
|
|
|
|
<p>This is not mandatory, but we can also create our first mail alias :</p>
|
|
|
|
<pre><code class="language-sql">INSERT INTO virtual_aliases (`domain_id`, `source`, `destination`) VALUES
|
|
('1', 'alias@captainark.net', 'example@captainark.net');
|
|
</code></pre>
|
|
|
|
<p>Now, all messages sent to alias@captainark.net will be forwarded to example@captainark.net.</p>
|
|
|
|
<p>Use the same syntax to create additional domains, users and aliases. If you have more than one domains configured, be sure to associate your users and aliases with the correct domain_id.</p>
|
|
|
|
<h2 id="configuring-postfix">Configuring Postfix</h2>
|
|
|
|
<p>Next, we are going to configure <a href="http://www.postfix.org/">Postfix</a>.</p>
|
|
|
|
<h3 id="configuration-backup">Configuration backup</h3>
|
|
|
|
<p>First, let’s backup the original configuration files :</p>
|
|
|
|
<pre><code class="language-bash">cp /etc/postfix/main.cf /etc/postfix/main.cf.orig
|
|
cp /etc/postfix/master.cf /etc/postfix/master.cf.orig
|
|
</code></pre>
|
|
|
|
<h3 id="user-and-group-creation">User and group creation</h3>
|
|
|
|
<p>We are now going to create a user and group called vmail that will be used by both Postfix and Dovecot :</p>
|
|
|
|
<pre><code class="language-bash">groupadd -g 5000 vmail
|
|
useradd -g vmail -u 5000 vmail -d /var/mail -m -s /bin/false
|
|
</code></pre>
|
|
|
|
<h3 id="ssl-certificates">SSL certificates</h3>
|
|
|
|
<p>Next, we are going to create the folder where we will store the SSL certificates :</p>
|
|
|
|
<pre><code class="language-bash">mkdir /etc/postfix/ssl
|
|
chown root: /etc/postfix/ssl && chmod 600 /etc/postfix/ssl
|
|
</code></pre>
|
|
|
|
<p>Purists will probably want to store their certificates in /etc/ssl/private. If you choose to do so, you’ll have to adapt the path of those files for the remainder of this tutorial.</p>
|
|
|
|
<p>If you’ve decided to create a certificate with StartSSL, you’ll end up with two files, a .crt and a .key. I’ll name those files server.crt and server-with-passphrase.key. Put both these files in the folder we’ve just created.</p>
|
|
|
|
<p>Now, let’s remove the passphrase from the key :</p>
|
|
|
|
<pre><code class="language-bash">cd /etc/postfix/ssl
|
|
openssl rsa -in server-with-passphrase.key -out server.key
|
|
</code></pre>
|
|
|
|
<p>You’ll be prompted for the passphrase you chose during the certificate generation.</p>
|
|
|
|
<p>Next, we have to download the appropriate intermediate certificate :</p>
|
|
|
|
<pre><code class="language-bash">wget -O /etc/postfix/ssl/sub.class1.server.ca.pem \
|
|
http://www.startssl.com/certs/sub.class1.server.ca.pem
|
|
</code></pre>
|
|
|
|
<p>We now have to make sure that the permissions on those files are correct :</p>
|
|
|
|
<pre><code class="language-bash">chown root: /etc/postfix/ssl/* && chmod 600 /etc/postfix/ssl/*
|
|
</code></pre>
|
|
|
|
<p>The last thing we have to do here is to generate Diffie-Hellman keys for Perfect Forward Secrecy (PFS) :</p>
|
|
|
|
<pre><code class="language-bash">openssl gendh -out /etc/postfix/dh_512.pem -2 512
|
|
openssl gendh -out /etc/postfix/dh_1024.pem -2 1024
|
|
</code></pre>
|
|
|
|
<h3 id="postifx-configuration">Postifx configuration</h3>
|
|
|
|
<p>First, let’s edit the /etc/postfix/main.cf file. It should end up looking something like that :</p>
|
|
|
|
<pre><code>smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
|
|
biff = no
|
|
|
|
broken_sasl_auth_clients = yes
|
|
config_directory = /etc/postfix
|
|
disable_vrfy_command = yes
|
|
smtpd_data_restrictions = reject_unauth_pipelining, permit
|
|
smtpd_helo_required = yes
|
|
|
|
queue_directory = /var/spool/postfix
|
|
append_dot_mydomain = no
|
|
readme_directory = no
|
|
|
|
smtpd_use_tls=yes
|
|
smtpd_tls_auth_only = yes
|
|
smtpd_tls_cert_file=/etc/postfix/ssl/server.crt
|
|
smtpd_tls_key_file=/etc/postfix/ssl/server.key
|
|
smtpd_tls_CAfile=/etc/postfix/ssl/sub.class1.server.ca.pem
|
|
smtpd_tls_mandatory_protocols=!SSLv2,!SSLv3
|
|
smtpd_tls_protocols=!SSLv2,!SSLv3
|
|
smtpd_tls_mandatory_ciphers=high
|
|
smtpd_tls_dh1024_param_file = /etc/postfix/dh_1024.pem
|
|
smtpd_tls_dh512_param_file = /etc/postfix/dh_512.pem
|
|
smtpd_tls_eecdh_grade = strong
|
|
smtpd_tls_loglevel = 1
|
|
smtpd_tls_received_header = yes
|
|
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
|
|
|
|
tls_preempt_cipherlist = yes
|
|
tls_random_source = dev:/dev/urandom
|
|
|
|
smtpd_data_restrictions = reject_unauth_pipelining, permit
|
|
smtpd_helo_required = yes
|
|
|
|
smtp_tls_CAfile = $smtpd_tls_CAfile
|
|
smtp_tls_mandatory_protocols=!SSLv2,!SSLv3
|
|
smtp_tls_protocols=!SSLv2,!SSLv3
|
|
smtp_tls_security_level = may
|
|
smtp_tls_loglevel = 1
|
|
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
|
|
|
|
smtpd_milters =
|
|
non_smtpd_milters = $smtpd_milters
|
|
milter_protocol = 2
|
|
milter_default_action = accept
|
|
|
|
smtpd_recipient_restrictions =
|
|
reject_invalid_hostname,
|
|
reject_non_fqdn_hostname,
|
|
reject_non_fqdn_sender,
|
|
reject_non_fqdn_recipient,
|
|
reject_unknown_sender_domain,
|
|
reject_unknown_recipient_domain,
|
|
permit_mynetworks,
|
|
permit_sasl_authenticated,
|
|
reject_unauth_destination,
|
|
permit
|
|
|
|
smtpd_sasl_auth_enable = yes
|
|
smtpd_sasl_local_domain = $myhostname
|
|
smtpd_sasl_security_options = noanonymous
|
|
smtpd_sasl_tls_security_options = $smtpd_sasl_security_options
|
|
smtpd_tls_auth_only = yes
|
|
smtpd_sasl_type = dovecot
|
|
smtpd_sasl_path = private/auth
|
|
|
|
myhostname = myserver.captainark.net ### CHANGE THIS
|
|
alias_maps = hash:/etc/aliases
|
|
alias_database = hash:/etc/aliases
|
|
myorigin = /etc/mailname
|
|
mydestination = localhost, myserver.captainark.net ### CHANGE THIS
|
|
relayhost =
|
|
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
|
|
mailbox_size_limit = 0
|
|
recipient_delimiter = +
|
|
default_transport = smtp
|
|
relay_transport = smtp
|
|
inet_interfaces = all
|
|
inet_protocols = all
|
|
|
|
virtual_transport = lmtp:unix:private/dovecot-lmtp
|
|
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
|
|
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
|
|
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf
|
|
</code></pre>
|
|
|
|
<p>The variable “myhostname” has to be defined to you server’s FQDN. The file /etc/mailname should contain your server’s FQDN as well.</p>
|
|
|
|
<p>Next, we need to edit the /etc/postfix/master.cf file. You need to uncomment the following lines :</p>
|
|
|
|
<pre><code>submission inet n - - - - smtpd
|
|
-o syslog_name=postfix/submission
|
|
-o smtpd_tls_security_level=encrypt
|
|
-o tls_preempt_cipherlist=yes
|
|
-o smtpd_sasl_auth_enable=yes
|
|
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
|
|
|
|
smtps inet n - - - - smtpd
|
|
-o syslog_name=postfix/smtps
|
|
-o smtpd_tls_wrappermode=yes
|
|
-o smtpd_sasl_auth_enable=yes
|
|
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
|
|
</code></pre>
|
|
|
|
<p>You also have to add the following lines at the end of the file :</p>
|
|
|
|
<pre><code>dovecot unix - n n - - pipe
|
|
flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f ${sender} -d ${recipient}
|
|
</code></pre>
|
|
|
|
<h3 id="mysql-access-for-postfix">MySQL access for Postfix</h3>
|
|
|
|
<p>We now need to allow Postfix to connect to the MySQL database we have created earlier. To that end, we must create three files.</p>
|
|
|
|
<p>/etc/postfix/mysql-virtual-mailbox-domains.cf should contain the following lines :</p>
|
|
|
|
<pre><code>user = mail
|
|
password = mailpassword
|
|
hosts = 127.0.0.1
|
|
dbname = mail
|
|
query = SELECT 1 FROM virtual_domains WHERE name='%s'
|
|
</code></pre>
|
|
|
|
<p>/etc/postfix/mysql-virtual-mailbox-maps.cf should contain the following lines :</p>
|
|
|
|
<pre><code>user = mail
|
|
password = mailpassword
|
|
hosts = 127.0.0.1
|
|
dbname = mail
|
|
query = SELECT 1 FROM virtual_users WHERE email='%s'
|
|
</code></pre>
|
|
|
|
<p>/etc/postfix/mysql-virtual-alias-maps.cf should contain the following lines :</p>
|
|
|
|
<pre><code>user = mail
|
|
password = mailpassword
|
|
hosts = 127.0.0.1
|
|
dbname = mail
|
|
query = SELECT destination FROM virtual_aliases WHERE source='%s'
|
|
</code></pre>
|
|
|
|
<p>Since these files contain a password, let’s make sure they are not world-readable :</p>
|
|
|
|
<pre><code class="language-bash">chown root: /etc/postfix/mysql* && chmod 600 /etc/postfix/mysql*
|
|
</code></pre>
|
|
|
|
<p>You can use the command postmap to confirm that everything is working properly :</p>
|
|
|
|
<pre><code class="language-bash">postmap -q captainark.net mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
|
|
|
|
postmap -q example@captainark.net mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
|
|
|
|
postmap -q alias@captainark.net mysql:/etc/postfix/mysql-virtual-alias-maps.cf
|
|
</code></pre>
|
|
|
|
<p>Let’s restart postfix for our modifications to be taken into account :</p>
|
|
|
|
<pre><code class="language-bash">systemctl restart postfix
|
|
</code></pre>
|
|
|
|
<p>That’s it for Postfix, for now ; Dovecot is next !</p>
|
|
|
|
<h2 id="configuring-dovecot">Configuring Dovecot</h2>
|
|
|
|
<h3 id="dovecot-global-configuration">Dovecot global configuration</h3>
|
|
|
|
<p>By default, on Debian, <a href="http://www.dovecot.org/">Dovecot</a> uses multiple configuration files in /etc/dovecot/conf.d. I found it annoying to maintain, and I ended up only using the /etc/doveconf.conf file.</p>
|
|
|
|
<p>As always, let’s start by backing up the original configuration file :</p>
|
|
|
|
<pre><code class="language-bash">mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.orig
|
|
</code></pre>
|
|
|
|
<p>Next, we are going to create a new /etc/dovecot/dovecot.conf file. It should contain the following lines :</p>
|
|
|
|
<pre><code>!include_try /usr/share/dovecot/protocols.d/*.protocol
|
|
protocols = imap lmtp sieve
|
|
|
|
mail_location = maildir:/var/mail/%d/%n
|
|
mail_privileged_group = vmail
|
|
mail_plugin_dir = /usr/lib/dovecot/modules
|
|
mail_plugins =
|
|
|
|
disable_plaintext_auth = yes
|
|
auth_mechanisms = plain login
|
|
|
|
service director {
|
|
unix_listener login/director {
|
|
}
|
|
fifo_listener login/proxy-notify {
|
|
}
|
|
unix_listener director-userdb {
|
|
}
|
|
inet_listener {
|
|
}
|
|
}
|
|
|
|
namespace inbox {
|
|
inbox = yes
|
|
type = private
|
|
mailbox Drafts {
|
|
auto = subscribe
|
|
special_use = \Drafts
|
|
}
|
|
mailbox Junk {
|
|
auto = subscribe
|
|
special_use = \Junk
|
|
}
|
|
mailbox Sent {
|
|
auto = subscribe
|
|
special_use = \Sent
|
|
}
|
|
mailbox Trash {
|
|
auto = subscribe
|
|
special_use = \Trash
|
|
}
|
|
}
|
|
|
|
service imap-login {
|
|
inet_listener imap {
|
|
port = 0
|
|
}
|
|
inet_listener imaps {
|
|
port = 993
|
|
ssl = yes
|
|
}
|
|
}
|
|
|
|
service pop3-login {
|
|
inet_listener pop3 {
|
|
port = 0
|
|
}
|
|
inet_listener pop3s {
|
|
port = 0
|
|
}
|
|
}
|
|
|
|
service lmtp {
|
|
unix_listener /var/spool/postfix/private/dovecot-lmtp {
|
|
mode = 0600
|
|
user = postfix
|
|
group = postfix
|
|
}
|
|
}
|
|
|
|
service imap {
|
|
}
|
|
|
|
service pop3 {
|
|
}
|
|
|
|
service auth {
|
|
unix_listener /var/spool/postfix/private/auth {
|
|
mode = 0666
|
|
user = postfix
|
|
group = postfix
|
|
}
|
|
unix_listener auth-userdb {
|
|
mode = 0600
|
|
user = vmail
|
|
}
|
|
user = dovecot
|
|
}
|
|
|
|
service auth-worker {
|
|
user = vmail
|
|
}
|
|
|
|
service dict {
|
|
unix_listener dict {
|
|
}
|
|
}
|
|
|
|
ssl = required
|
|
ssl_cert = </etc/postfix/ssl/server.crt
|
|
ssl_key = </etc/postfix/ssl/server.key
|
|
ssl_ca = </etc/postfix/ssl/sub.class1.server.ca.pem
|
|
ssl_protocols = !SSLv2 !SSLv3
|
|
ssl_cipher_list = AES128+EECDH:AES128+EDH:!aNULL;
|
|
protocol lda {
|
|
mail_plugins = $mail_plugins sieve
|
|
}
|
|
|
|
protocol imap {
|
|
mail_plugins = $mail_plugins
|
|
}
|
|
protocol lmtp {
|
|
mail_plugins = $mail_plugins sieve
|
|
}
|
|
|
|
plugin {
|
|
sieve = /var/mail/sieve/users/%u.sieve
|
|
sieve_after = /var/mail/sieve/after
|
|
sieve_before = /var/mail/sieve/before
|
|
sieve_global_dir = /var/lib/dovecot/sieve/
|
|
sieve_dir = ~/sieve
|
|
}
|
|
|
|
passdb {
|
|
driver = sql
|
|
args = /etc/dovecot/sql.conf
|
|
}
|
|
userdb {
|
|
driver = static
|
|
args = uid=vmail gid=vmail home=/var/mail/%d/%n
|
|
}
|
|
</code></pre>
|
|
|
|
<p>Dovecot will use the same SSL certificate as Postfix.</p>
|
|
|
|
<p>Using this configuration, your virtual users’ emails will be stored in /var/mail/$domain/$user/ and will be owned by the vmail user.</p>
|
|
|
|
<p>For this to work, we have to create the domain folder :</p>
|
|
|
|
<pre><code class="language-bash">mkdir -p /var/mail/captainark.net
|
|
chown vmail: /var/mail/captainark.net && chmod 770 /var/mail/captainark.net
|
|
</code></pre>
|
|
|
|
<p>Dovecot will create the virtual users’ folders automatically.</p>
|
|
|
|
<h3 id="dovecot-access-to-the-mysql-database">Dovecot access to the MySQL database</h3>
|
|
|
|
<p>We now need to allow Dovecot to connect to the mail database we have populated earlier. To do so, we are going to create a /etc/dovecot/sql.conf file with the following content :</p>
|
|
|
|
<pre><code>driver = mysql
|
|
connect = host=localhost dbname=mail user=mail password=mailpassword
|
|
default_pass_scheme = SHA512-CRYPT
|
|
password_query = SELECT email as user, password FROM virtual_users WHERE email='%u';
|
|
</code></pre>
|
|
|
|
<p>You’ll have to change the password to the one you have defined earlier. Since this file contains a password, let’s make sure it’s not world-readable :</p>
|
|
|
|
<pre><code class="language-bash">chown root: /etc/dovecot/sql.conf && chmod 600 /etc/dovecot/sql.conf
|
|
</code></pre>
|
|
|
|
<h3 id="configuring-sieve">Configuring Sieve</h3>
|
|
|
|
<p>The last thing we need to configure here is sieve. The idea is to have all messages flagged as spam automatically moved to the mailbox Junk folder.</p>
|
|
|
|
<p>To do so, let’s first create the required folders :</p>
|
|
|
|
<pre><code class="language-bash">mkdir -p /var/mail/sieve/before
|
|
mkdir /var/mail/sieve/after
|
|
mkdir /var/mail/sieve/users
|
|
chown -R vmail: /var/mail/sieve && chmod -R 770 /var/mail/sieve
|
|
</code></pre>
|
|
|
|
<p>If you want to have sieve rules for a specific user, simply create $user@$domain.sieve file in the users folder (example@captainark.net in my case).</p>
|
|
|
|
<p>All .sieve files in the before folder will be used for all your virtual users, before their individual configuration ; the .sieve files in the after folder will be used, well, you guessed it, after.</p>
|
|
|
|
<p>Let’s create a filter.sieve file in the /var/mail/sieve/before folder with the following content :</p>
|
|
|
|
<pre><code>require ["envelope", "fileinto", "imap4flags", "regex"];
|
|
|
|
if not header :regex "message-id" ".*@.*\." {
|
|
fileinto "Junk";
|
|
}
|
|
|
|
if header :contains "X-Spam-Level" "*****" {
|
|
fileinto "Junk";
|
|
}
|
|
</code></pre>
|
|
|
|
<p>Last thing we have to do is to change the permissions on the newly created file :</p>
|
|
|
|
<pre><code class="language-bash">chown vmail: /var/mail/sieve/before/filter.sieve && \
|
|
chmod 660 /var/mail/sieve/before/filter.sieve
|
|
</code></pre>
|
|
|
|
<p>That’s all ; now, all email we receive that is flagged as spam by SpamAssassin will be moved to the Junk folder.</p>
|
|
|
|
<p>Let’s restart dovecot :</p>
|
|
|
|
<pre><code class="language-bash">systemctl restart dovecot
|
|
</code></pre>
|
|
|
|
<p>We now have a working mail server !</p>
|
|
|
|
<p>To connect to it and access your mailbox, configure your email client as follow :</p>
|
|
|
|
<ul>
|
|
<li>Username: example@captainark.net ;</li>
|
|
<li>Password: the password you chose for your virtual user ;</li>
|
|
<li>IMAP: your server’s FQDN, port 993 (SSL/TLS with normal password) ;</li>
|
|
<li>SMTP: your server’s FQDN, port 465 (SSL/TLS with normal password).</li>
|
|
</ul>
|
|
|
|
<h2 id="configuring-spamassassin">Configuring SpamAssassin</h2>
|
|
|
|
<h3 id="the-alternatives">The alternatives</h3>
|
|
|
|
<p>Next thing we have to do is to configure the actual anti-spam. I tried a few, but I ended up sticking with <a href="http://spamassassin.apache.org/">SpamAssassin</a>. Here’s why :</p>
|
|
|
|
<ul>
|
|
<li><a href="http://dspam.nuclearelephant.com/">DSPAM</a> is <a href="http://sourceforge.net/p/dspam/mailman/message/32585111/">no longer maintained</a> and <a href="https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=754810">has been removed from Debian Jessie</a> ;</li>
|
|
<li><a href="https://rspamd.com/">Rspamd</a> is interesting, has been <a href="https://packages.debian.org/source/jessie/rspamd">integrated in Debian Jessie</a>, but is poorly documented at this time ;</li>
|
|
<li><a href="http://bogofilter.sourceforge.net/">Bogofilter</a> does not seem to have the greatest server integration.</li>
|
|
</ul>
|
|
|
|
<h3 id="the-actual-configuration">The actual configuration</h3>
|
|
|
|
<p>SpamAssassin’s configuration is pretty straightforward. First, let’s edit the /etc/default/spamassassin file :</p>
|
|
|
|
<pre><code>ENABLED=1
|
|
[...]
|
|
CRON=1
|
|
</code></pre>
|
|
|
|
<p>Before the cron runs for the first time, we have to manually update SpamAssassin’s ruleset :</p>
|
|
|
|
<pre><code class="language-bash">sa-learn
|
|
</code></pre>
|
|
|
|
<p>Next, as usual, let’s back up the original configuration file :</p>
|
|
|
|
<pre><code class="language-bash">mv /etc/spamassassin/local.cf /etc/spamassassin/local.cf.orig
|
|
</code></pre>
|
|
|
|
<p>Let’s create a new /etc/spamassassin/local.cf file with the following content :</p>
|
|
|
|
<pre><code>rewrite_header Subject [SPAM]
|
|
report_safe 0
|
|
required_score 5.0
|
|
use_bayes 1
|
|
bayes_auto_learn 1
|
|
|
|
whitelist_from *@captainark.net
|
|
</code></pre>
|
|
|
|
<p>Next, to have Postfix send incoming emails through SpamAssassin, we have to edit the /etc/postfix/master.cf file. At the very beginning, we have to add a line under the smtp definition :</p>
|
|
|
|
<pre><code>smtp inet n - - - - smtpd
|
|
-o content_filter=spamassassin
|
|
</code></pre>
|
|
|
|
<p>At the very end of the same file, we have to add the following lines :</p>
|
|
|
|
<pre><code>spamassassin unix - n n - - pipe
|
|
user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}
|
|
</code></pre>
|
|
|
|
<p>Let’s restart SpamAssassin and Postfix :</p>
|
|
|
|
<pre><code class="language-bash">systemctl restart postfix
|
|
systemctl restart spamassassin
|
|
</code></pre>
|
|
|
|
<p>That’s all for SpamAssassin ! To check if it is working, send yourself an email from another provider. You should see the following headers in it :</p>
|
|
|
|
<pre><code>X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
|
|
myserver.captainark.net
|
|
X-Spam-Level:
|
|
</code></pre>
|
|
|
|
<h2 id="configuring-spf">Configuring SPF</h2>
|
|
|
|
<h3 id="allowing-your-server-to-send-emails-for-your-domain">Allowing your server to send emails for your domain</h3>
|
|
|
|
<p><a href="http://www.openspf.org/">SPF</a> (Sender Policy Framework) is a mechanism that confirms that your server’s IP is allowed to send emails for your domain. Technically, it is a TXT DNS record which looks something like this :</p>
|
|
|
|
<pre><code>captainark.net IN TXT "v=spf1 mx ~all"
|
|
</code></pre>
|
|
|
|
<p>This DNS record lets other mail servers know that hosts that have a MX record for my domain are also allowed to send emails for it.</p>
|
|
|
|
<p>For more information on SPF syntax, you can consult the <a href="http://www.openspf.org/SPF_Record_Syntax">official documentation</a>.</p>
|
|
|
|
<p>Without a properly configured SPF record, other mail servers might flag your emails as spam or outright drop them.</p>
|
|
|
|
<h3 id="checking-spf-record-for-inbound-mail">Checking SPF record for inbound mail</h3>
|
|
|
|
<p>Now that we have set up our own SPF record, let’s configure Postfix to check that other mail servers communicating with us have done the same.</p>
|
|
|
|
<p>First, let’s add the two following lines at the end of /etc/postfix-policyd-spf-python/policyd-spf.conf :</p>
|
|
|
|
<pre><code>Header_Type = AR
|
|
Authserv_Id = "<server's FQDN>"
|
|
</code></pre>
|
|
|
|
<p>Then, let’s edit the /etc/postfix/master.cf file and add the following lines at the end :</p>
|
|
|
|
<pre><code>policy-spf unix - n n - - spawn
|
|
user=nobody argv=/usr/bin/policyd-spf
|
|
</code></pre>
|
|
|
|
<p>Let’s now edit the /etc/postfix/main.cf. In the “smtpd_recipient_restrictions” section, add the “check_policy_service” line as seen below :</p>
|
|
|
|
<pre><code>smtpd_recipient_restrictions =
|
|
[...]
|
|
reject_unauth_destination,
|
|
check_policy_service unix:private/policy-spf,
|
|
permit
|
|
</code></pre>
|
|
|
|
<p>We now have to restart postfix :</p>
|
|
|
|
<pre><code class="language-bash">systemctl restart postfix
|
|
</code></pre>
|
|
|
|
<p>Our server is now checking other mail server’s SPF records.</p>
|
|
|
|
<p>To make sure that it is working, send yourself an email from another provider. You should see the following header in it :</p>
|
|
|
|
<pre><code>Authentication-Results: myserver.captainark.net; spf=pass (sender SPF authorized)
|
|
[...] receiver=example@captainark.net)
|
|
</code></pre>
|
|
|
|
<h2 id="configuring-opendkim">Configuring OpenDKIM</h2>
|
|
|
|
<p><a href="http://www.dkim.org/">DKIM</a> (DomainKeys Identified Mail) is a mechanism that validates a domain name identity for an email through cryptographic authentication.</p>
|
|
|
|
<p>While not mandatory, setting up DKIM improves the odds of emails sent from your server not being flagged as spam by other providers.</p>
|
|
|
|
<p>With this configuration, OpenDKIM will also check the key for inbound emails.</p>
|
|
|
|
<h3 id="software-side">Software side</h3>
|
|
|
|
<p>First, let’s backup the original configuration file and create a folder for the configuration files :</p>
|
|
|
|
<pre><code class="language-bash">mv /etc/opendkim.conf /etc/opendkim.conf.orig
|
|
mkdir /etc/opendkim.d
|
|
</code></pre>
|
|
|
|
<p>We now have to create a /etc/opendkim.conf file with the following content :</p>
|
|
|
|
<pre><code>AutoRestart Yes
|
|
AutoRestartRate 10/1h
|
|
UMask 002
|
|
Syslog yes
|
|
SyslogSuccess Yes
|
|
LogWhy Yes
|
|
|
|
OversignHeaders From
|
|
AlwaysAddARHeader yes
|
|
|
|
Canonicalization relaxed/simple
|
|
|
|
ExternalIgnoreList refile:/etc/opendkim.d/TrustedHosts
|
|
InternalHosts refile:/etc/opendkim.d/dkim/TrustedHosts
|
|
KeyTable refile:/etc/opendkim.d/dkim/KeyTable
|
|
SigningTable refile:/etc/opendkim.d/dkim/SigningTable
|
|
|
|
Mode sv
|
|
PidFile /run/opendkim/opendkim.pid
|
|
SignatureAlgorithm rsa-sha256
|
|
|
|
UserID opendkim:opendkim
|
|
|
|
Socket local:/var/spool/postfix/opendkim/opendkim.sock
|
|
</code></pre>
|
|
|
|
<p>Let’s then create the necessary folders :</p>
|
|
|
|
<pre><code class="language-bash">mkdir -p /etc/opendkim.d/keys/captainark.net/
|
|
</code></pre>
|
|
|
|
<p>Now, we are going to create the /etc/opendkim.d/TrustedHosts file with the following content :</p>
|
|
|
|
<pre><code>localhost
|
|
127.0.0.1
|
|
::1
|
|
captainark.net
|
|
</code></pre>
|
|
|
|
<p>This file contains the hosts and domains for which OpenDKIM should sign emails.</p>
|
|
|
|
<p>Next, let’s create the /etc/opendkim.d/KeyTable :</p>
|
|
|
|
<pre><code>mail._domainkey.captainark.net captainark.net:mail:/etc/opendkim.d/keys/captainark.net/mail.private
|
|
</code></pre>
|
|
|
|
<p>This file tells OpenDKIM which key it should use for each selector.</p>
|
|
|
|
<p>Finally, let’s create the /etc/opendkim.d/SigningTable file :</p>
|
|
|
|
<pre><code>*@captainark.net mail._domainkey.captainark.net
|
|
</code></pre>
|
|
|
|
<p>This file tells OpenDKIM which selector it should use for each domain.</p>
|
|
|
|
<p>We now have to generate the private/public key pair for our domain :</p>
|
|
|
|
<pre><code class="language-bash">cd /etc/opendkim.d/keys/captainark.net/
|
|
opendkim-genkey -s mail -d captainark.net
|
|
</code></pre>
|
|
|
|
<p>This creates two files ; mail.private contains our private key, mail.txt contains our public key.</p>
|
|
|
|
<p>Let’s change the permissions on those files :</p>
|
|
|
|
<pre><code class="language-bash">chown -R opendkim: /etc/opendkim.d/keys
|
|
chmod -R 700 /etc/opendkim.d/keys
|
|
chmod 600 /etc/opendkim.d/captainark.net/*
|
|
</code></pre>
|
|
|
|
<h3 id="postfix-integration">Postfix integration</h3>
|
|
|
|
<p>The last thing we have to do is to configure Postfix to communicate with OpenDKIM.</p>
|
|
|
|
<p>First, let’s create the necessary folders :</p>
|
|
|
|
<pre><code class="language-bash">mkdir /var/spool/postfix/opendkim
|
|
chown opendkim: /var/spool/postfix/opendkim
|
|
</code></pre>
|
|
|
|
<p>We also have to add the postfix user to the opendkim group :</p>
|
|
|
|
<pre><code class="language-bash">useradd -G opendkim postfix
|
|
</code></pre>
|
|
|
|
<p>Now, let’s edit the /etc/postfix/master.cf file, like so :</p>
|
|
|
|
<pre><code>smtpd_milters = unix:/opendkim/opendkim.sock
|
|
</code></pre>
|
|
|
|
<p>We now have to restart OpenDKIM and Postfix :</p>
|
|
|
|
<pre><code class="language-bash">systemctl restart opendkim
|
|
systemctl restart postfix
|
|
</code></pre>
|
|
|
|
<h3 id="dns-side">DNS side</h3>
|
|
|
|
<p>For DKIM to work, you have to configure a DNS TXT record in your zone. This record was automatically generated by OpenDKIM in the mail.txt file mentioned earlier :</p>
|
|
|
|
<pre><code>mail._domainkey IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkJq0CW3tl2XHZ1CN5XdbqRDU7KfXOJ70nlwI09bHmDU63/Yz3J5rl863S0t2ncVHfIudZANj0OaiJe5HRR7WCsjuNIhQFfPFGIWLNClpxqdQVQURI38sAGeyn7Ed/Cor1AiWABzFWzel0kvXILw8K/NTzxaAPeSa9ttwQEgSmowIDAQAB" ; ----- DKIM key mail for captainark.net
|
|
</code></pre>
|
|
|
|
<p>All you have to do is to copy and paste this record in your DNS zone file.</p>
|
|
|
|
<p>To make sure that OpenDKIM is working, you can send an empty email to <a href="mailto:check-auth@verifier.port25.com">check-auth@verifier.port25.com</a>. You should receive a response with the following content :</p>
|
|
|
|
<pre><code>==========================================================
|
|
Summary of Results
|
|
==========================================================
|
|
SPF check: pass
|
|
DomainKeys check: neutral
|
|
DKIM check: pass
|
|
Sender-ID check: pass
|
|
SpamAssassin check: ham
|
|
</code></pre>
|
|
|
|
<h2 id="configuring-opendmarc">Configuring OpenDMARC</h2>
|
|
|
|
<p><a href="http://dmarc.org/">DMARC</a> (Domain-based Message Authentication, Reporting & Conformance) standardizes SPF and DKIM authentication mechanisms.</p>
|
|
|
|
<p>It lets the owner of a domain name indicate that his email is protected by SPF and/or DKIM and what other providers should do with emails that do not pass those checks.</p>
|
|
|
|
<h3 id="software-side-1">Software side</h3>
|
|
|
|
<p>Once again, let’s backup the original configuration file :</p>
|
|
|
|
<pre><code class="language-bash">mv /etc/opendmarc.conf /etc/opendmarc.conf.orig
|
|
</code></pre>
|
|
|
|
<p>We now have to create a /etc/opendmarc.conf file with the following content :</p>
|
|
|
|
<pre><code>AutoRestart Yes
|
|
AutoRestartRate 10/1h
|
|
UMask 0002
|
|
Syslog true
|
|
|
|
AuthservID "<your server's FQDN>"
|
|
TrustedAuthservIDs "<your server's FQDN>"
|
|
IgnoreHosts /etc/opendkim.d/TrustedHosts
|
|
|
|
RejectFailures false
|
|
|
|
UserID opendmarc:opendmarc
|
|
PidFile /run/opendmarc.pid
|
|
Socket local:/var/spool/postfix/opendmarc/opendmarc.sock
|
|
</code></pre>
|
|
|
|
<h3 id="postfix-integration-1">Postfix integration</h3>
|
|
|
|
<p>The last thing we have to do is to configure Postfix to communicate with OpenDMARC.</p>
|
|
|
|
<p>First, let’s create the necessary folders :</p>
|
|
|
|
<pre><code class="language-bash">mkdir /var/spool/postfix/opendmarc
|
|
chown opendmarc: /var/spool/postfix/opendmarc
|
|
</code></pre>
|
|
|
|
<p>We also have to add the postfix user to the opendmarc group :</p>
|
|
|
|
<pre><code class="language-bash">useradd -G opendmarc postfix
|
|
</code></pre>
|
|
|
|
<p>Now, let’s edit the /etc/postfix/master.cf file, like so :</p>
|
|
|
|
<pre><code>smtpd_milters = unix:/opendkim/opendkim.sock, unix:/opendmarc/opendmarc.sock
|
|
</code></pre>
|
|
|
|
<p>We now have to restart OpenDMARC and Postfix :</p>
|
|
|
|
<pre><code class="language-bash">systemctl restart opendmarc
|
|
systemctl restart postfix
|
|
</code></pre>
|
|
|
|
<p>You should now see the following headers in your incoming emails :</p>
|
|
|
|
<pre><code>Authentication-Results: myserver.captainark.net; dmarc=pass header.from=gmail.com
|
|
</code></pre>
|
|
|
|
<h3 id="dns-side-1">DNS side</h3>
|
|
|
|
<p>DMARC, like SPF and DKIM, is based on DNS TXT records.</p>
|
|
|
|
<p>Here is how I configured it for the captainark.net domain :</p>
|
|
|
|
<pre><code>_dmarc IN TXT "v=DMARC1; p=none; rua=mailto:postmaster@captainark.net; ruf=mailto:postmaster@captainark.net"
|
|
</code></pre>
|
|
|
|
<p>This tells other providers to not reject or quarantine emails should a SPF or DKIM check fail, but to send a daily report of those checks to postmaster@captainark.net.</p>
|
|
|
|
<p>For more information on the DMARC syntax, here is an <a href="https://support.google.com/a/answer/2466563?hl=en">article from Google</a>.</p>
|
|
|
|
<h2 id="configuring-monit">Configuring Monit</h2>
|
|
|
|
<p><a href="http://mmonit.com/monit/">Monit</a> is a daemon that makes sure that other daemons are running. If they crash, it restarts them automatically. Is is not directly related to a mail server per say, but it’s pretty easy to set up.</p>
|
|
|
|
<p>First, as always, let’s backup the original configuration file :</p>
|
|
|
|
<pre><code class="language-bash">mv /etc/monit/monitrc /etc/monit/monitrc.orig
|
|
</code></pre>
|
|
|
|
<p>We now have to create a new /etc/monit/monitrc file with the following content :</p>
|
|
|
|
<pre><code>set daemon 30
|
|
set logfile syslog facility log_daemon
|
|
|
|
set httpd port 2812 and
|
|
use address localhost
|
|
allow localhost
|
|
|
|
set mailserver localhost
|
|
with timeout 30 seconds
|
|
using hostname myserver.captainark.net
|
|
|
|
set mail-format { from: monit@captainark.net }
|
|
|
|
include /etc/monit/conf.d/*
|
|
</code></pre>
|
|
|
|
<p>Then, we are going to create a /etc/monit/conf.d/mail file with the following content :</p>
|
|
|
|
<pre><code>check process postfix
|
|
with pidfile "/var/spool/postfix/pid/master.pid"
|
|
start program = "/bin/systemctl start postfix"
|
|
stop program = "/bin/systemctl stop postfix"
|
|
alert monit@captainark.net
|
|
group mail
|
|
|
|
check process dovecot
|
|
with pidfile "/run/dovecot/master.pid"
|
|
start program = "/bin/systemctl start dovecot"
|
|
stop program = "/bin/systemctl stop dovecot"
|
|
alert monit@captainark.net
|
|
group mail
|
|
depends on postfix
|
|
|
|
check process spamassassin
|
|
with pidfile "/run/spamassassin.pid"
|
|
start program = "/bin/systemctl start spamassassin"
|
|
stop program = "/bin/systemctl stop spamassassin"
|
|
alert monit@captainark.net
|
|
group mail
|
|
depends on postfix, dovecot
|
|
|
|
check process opendkim
|
|
with pidfile "/run/opendkim/opendkim.pid"
|
|
start program = "/bin/systemctl start opendkim"
|
|
stop program = "/bin/systemctl stop opendkim"
|
|
alert monit@captainark.net
|
|
group mail
|
|
depends on postfix, dovecot
|
|
|
|
check process opendmarc
|
|
with pidfile "/run/opendmarc/opendmarc.pid"
|
|
start program = "/bin/systemctl start opendmarc"
|
|
stop program = "/bin/systemctl stop opendmarc"
|
|
alert monit@captainark.net
|
|
group mail
|
|
depends on postfix, dovecot
|
|
</code></pre>
|
|
|
|
<p>Let’s make sure that permissions on the file are correct :</p>
|
|
|
|
<pre><code class="language-bash">chown root: /etc/monit/conf.d/mail && chmod 600 /etc/monit/conf.d/mail
|
|
</code></pre>
|
|
|
|
<p>Then, we have to reload the monit daemon :</p>
|
|
|
|
<pre><code class="language-bash">monit reload
|
|
</code></pre>
|
|
|
|
<p>Now, the <code>monit summary</code> command should have the following output :</p>
|
|
|
|
<pre><code>The Monit daemon 5.4 uptime: 3d 0h 41m
|
|
|
|
Process 'postfix' Running
|
|
Process 'dovecot' Running
|
|
Process 'spamassassin' Running
|
|
Process 'opendkim' Running
|
|
Process 'opendmarc' Running
|
|
</code></pre>
|
|
|
|
<h2 id="configuring-rainloop">Configuring Rainloop</h2>
|
|
|
|
<p><a href="http://www.rainloop.net/">Rainloop</a> is a web-based email client. I won’t go into details on how to configure it in this tutorial ; here’s a link to the <a href="http://www.rainloop.net/docs/installation/">official documentation</a>.</p>
|
|
|
|
<p>You’ll need a web server with PHP 5.3+ to run Rainloop. You do not have to run Rainloop on the same host as your mail server. No database is required.</p>
|
|
|
|
<h2 id="conclusion">Conclusion</h2>
|
|
|
|
<p>We now have a mail server that should be running pretty smoothly. It could still be improved by setting up things such as greylisting or virus detection.</p>
|
|
|
|
<p>If you have found this tutorial useful, if you’ve found an error in it or if you have any question, please feel free to leave a comment below or to contact me on <a href="https://twitter.com/captainark">Twitter</a>.</p>
|
|
|
|
<h2 id="references">References</h2>
|
|
|
|
<p>Here are the tutorials I used to set up my own mail server :</p>
|
|
|
|
<ul>
|
|
<li><a href="http://sealedabstract.com/code/nsa-proof-your-e-mail-in-2-hours/">A complete tutorial on setting up a mail server</a></li>
|
|
<li><a href="https://docs.raccoon.io/mail-server-setup-with-postfix-dovecot/">Another complete tutorial</a></li>
|
|
<li><a href="https://www.digitalocean.com/community/tutorials/how-to-configure-a-mail-server-using-postfix-dovecot-mysql-and-spamassasin">A third tutorial from DigitalOcean</a></li>
|
|
<li><a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-dkim-with-postfix-on-debian-wheezy">A tutorial on setting up OpenDKIM</a></li>
|
|
<li><a href="https://guillaume.vaillant.me/?p=481">A tutorial on setting up OpenDMARC</a> (in french)</li>
|
|
</ul>
|
|
|
|
</section>
|
|
|
|
|
|
<footer class="post-footer">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<figure class="author-image">
|
|
<a class="img" href="https://www.captainark.net/" style="background-image: url(/images/author.jpg)"><span class="hidden">Antoine Joubert's Picture</span></a>
|
|
</figure>
|
|
|
|
|
|
<section class="author">
|
|
<h4><a href="https://www.captainark.net/">Antoine Joubert</a></h4>
|
|
|
|
<p>Geek | Gamer | TV Shows Aficionado</p>
|
|
|
|
<div class="author-meta">
|
|
<span class="author-location icon-location">Angers, France</span>
|
|
<span class="author-link icon-link"><a href="https://www.captainark.net">https://www.captainark.net</a></span>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- isso -->
|
|
<script data-isso="https://www.captainark.net/comments/" src="https://www.captainark.net/comments/js/embed.min.js"></script>
|
|
<noscript>Please enable JavaScript to view comments</noscript>
|
|
<section id="isso-thread"></section>
|
|
<!-- end isso -->
|
|
|
|
|
|
|
|
|
|
</footer>
|
|
</article>
|
|
|
|
</main>
|
|
|
|
|
|
<aside class="read-next">
|
|
|
|
<a class="read-next-story" style="no-cover" href="/2015/05/25/flexget-init-script/">
|
|
<section class="post">
|
|
<h2>Flexget init script</h2>
|
|
|
|
</section>
|
|
</a>
|
|
|
|
|
|
</aside>
|
|
|
|
|
|
|
|
|
|
<center>
|
|
<a class="fa-icons" href="mailto:contact@captainark.net">
|
|
<span class="fa-stack fa-lg">
|
|
<i class="fa fa-circle fa-stack-2x"></i>
|
|
<i class="fa fa-envelope fa-stack-1x fa-inverse"></i>
|
|
</span>
|
|
</a>
|
|
<a class="fa-icons" href="https://twitter.com/captainark">
|
|
<span class="fa-stack fa-lg">
|
|
<i class="fa fa-circle fa-stack-2x"></i>
|
|
<i class="fa fa-twitter fa-stack-1x fa-inverse"></i>
|
|
</span>
|
|
</a>
|
|
<a class="fa-icons" href="https://social.captainark.net/users/captainark">
|
|
<span class="fa-stack fa-lg">
|
|
<i class="fa fa-circle fa-stack-2x"></i>
|
|
<i class="fa fa-mastodon-alt fa-stack-1x fa-inverse"></i>
|
|
</span>
|
|
</a>
|
|
<a class="fa-icons" href="https://github.com/captainark">
|
|
<span class="fa-stack fa-lg">
|
|
<i class="fa fa-circle fa-stack-2x"></i>
|
|
<i class="fa fa-github fa-stack-1x fa-inverse"></i>
|
|
</span>
|
|
</a>
|
|
<a class="fa-icons" href="https://www.last.fm/user/captainark">
|
|
<span class="fa-stack fa-lg">
|
|
<i class="fa fa-circle fa-stack-2x"></i>
|
|
<i class="fa fa-lastfm fa-stack-1x fa-inverse"></i>
|
|
</span>
|
|
</a>
|
|
<a class="fa-icons" href="https://steamcommunity.com/id/captainark">
|
|
<span class="fa-stack fa-lg">
|
|
<i class="fa fa-circle fa-stack-2x"></i>
|
|
<i class="fa fa-steam fa-stack-1x fa-inverse"></i>
|
|
</span>
|
|
</a>
|
|
<a class="fa-icons" href="https://www.twitch.tv/captainark">
|
|
<span class="fa-stack fa-lg">
|
|
<i class="fa fa-circle fa-stack-2x"></i>
|
|
<i class="fa fa-twitch fa-stack-1x fa-inverse"></i>
|
|
</span>
|
|
</a>
|
|
</center>
|
|
|
|
|
|
<footer class="site-footer clearfix">
|
|
<section class="copyright"><a href="">Sysadmining. All day. Every day.</a> © 2015 - 2019</section>
|
|
|
|
<section class="poweredby">Proudly generated by <a class="icon-hugo" href="http://gohugo.io">HUGO</a>, with <a class="icon-theme" href="https://github.com/vjeantet/hugo-theme-casper">Casper</a> theme</section>
|
|
|
|
</footer>
|
|
</div>
|
|
<script type="text/javascript" src="/js/jquery.js"></script>
|
|
<script type="text/javascript" src="/js/jquery.fitvids.js"></script>
|
|
<script type="text/javascript" src="/js/index.js"></script>
|
|
</body>
|
|
</html>
|
|
|