Sysadmining. All day. Every day.https://captainark.net/2016-03-06T00:00:00+01:00Postfix Admin2016-03-06T00:00:00+01:00Antoine Jouberttag:captainark.net,2016-03-06:postfix-admin.html<p>As I explained in <a href="https://www.captainark.net/setting-up-a-mail-server.html">this previous tutorial</a>, I've been running my own mail server without any issue for some time now.</p>
<p>However, every time I've wanted to add a domain, create a new mailbox or change a user's password, I've had to do it manually from a SQL shell. As fun as it may be, it does get old very fast, so I've decided to install a web frontend to manage this database.</p>
<p>After a bit a googling, I've settled on <a href="http://postfixadmin.sourceforge.net/">Postfix Admin</a>.</p>
<p>The latest stable version of Postfix Admin was released in 2009. Version 3.0 has been in the works for some time now and the project can be cloned from their <a href="https://github.com/postfixadmin/postfixadmin">Github repo</a>.</p>
<p>I've also tried <a href="http://www.vimbadmin.net/">ViMbAdmin</a>, but it felt a little heavy considering what I was going to use it for.</p>
<p>You'll need a web server with PHP support to run Postfix Admin. I personnaly run nginx with php5-fpm, but I won't explain how to configure it here. I'll simply explain how to migrate your current database to one managed with Postfix Admin with as little downtime as possible.</p>
<h1>Creating a new database</h1>
<p>Since the database managed by Postfix Admin does not use the same schema as the one we've created in my previous tutorial, we'll have to create a new one. We will give all privileges on that database to the same user as before, <code>'mail'@'localhost'</code>.</p>
<div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">DATABASE</span> <span class="n">mailnew</span><span class="p">;</span>
<span class="k">GRANT</span> <span class="k">SELECT</span> <span class="k">ON</span> <span class="n">mailnew</span><span class="p">.</span><span class="o">*</span> <span class="k">TO</span> <span class="s1">'mail'</span><span class="o">@</span><span class="s1">'localhost'</span><span class="p">;</span>
<span class="n">FLUSH</span> <span class="k">PRIVILEGES</span><span class="p">;</span>
</pre></div>
<p>At this point, you can clone the Postfix Admin project from Github and go through the installation process.</p>
<p>While editing the config.inc.php file (or config.local.php file if you've decided to copy it), make sure that the <code>database_name</code> option is set to use the <code>mailnew</code> database we've just created.</p>
<p>Also, make sure that the <code>encrypt</code> option is set to <code>dovecot:SHA512-CRYPT</code>.</p>
<p>The installation process will create all the necessary tables in the database.</p>
<p><strong>At this point, you'll have to recreate all domains, mailboxes and aliases that you have configured in your current mail database using the Postfix Admin interface.</strong></p>
<h1>Postfix configuration</h1>
<p>Once you're done with Postfix Admin, it's time to configure Postfix to use its schema.</p>
<p>First thing first, let's backup our current configuration :</p>
<div class="highlight"><pre><span></span>mkdir /etc/postfix/mysql-backup
cp -a /etc/postfix/mysql-virtual* /etc/postfix/mysql-backup/
</pre></div>
<p>Next, we have to edit the 3 files we've just backed-up. The only line that actually changes is the one beginning with <code>query</code>.</p>
<p>The first file is /etc/postfix/mysql-virtual-mailbox-domains.cf :</p>
<div class="highlight"><pre><span></span>user = mail
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT 1 FROM domain WHERE domain='%s' AND active='1'
</pre></div>
<p>The second one is /etc/postfix/mysql-virtual-mailbox-maps.cf :</p>
<div class="highlight"><pre><span></span>user = mail
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT 1 FROM mailbox WHERE username='%s' AND active='1'
</pre></div>
<p>And the last one is /etc/postfix/mysql-virtual-alias-maps.cf :</p>
<div class="highlight"><pre><span></span>user = mail
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT goto FROM alias WHERE address='%s' AND active='1'
</pre></div>
<h1>Dovecot configuration</h1>
<p>Same as with Postfix, we now need to configure Dovecot to use the Postfix Admin schema.</p>
<p>First, let's backup our current configuration :</p>
<div class="highlight"><pre><span></span>cp -a /etc/dovecot/sql.conf /etc/dovecot/sql.conf.bak
</pre></div>
<p>Next, we have to edit the /etc/dovecot/sql.conf file. The only line that changes is the one beginning with <code>password_query</code>.</p>
<div class="highlight"><pre><span></span>driver = mysql
connect = host=localhost dbname=mail user=mail password=mailpassword
default_pass_scheme = SHA512-CRYPT
password_query = SELECT username as user, password FROM mailbox WHERE username='%u' AND active='1';
</pre></div>
<h1>Migrating to the new schema</h1>
<p>We're done with the configuration part. Time to migrate to the new schema.</p>
<p>First, let's create a backup of our current mail database :</p>
<div class="highlight"><pre><span></span>mysqldump mail <span class="p">|</span> bzip2 > /home/user/mail.sql.bz2
</pre></div>
<p>Next, in a SQL shell, we're going to drop and recreate the mail database :</p>
<div class="highlight"><pre><span></span><span class="k">DROP</span> <span class="k">DATABASE</span> <span class="n">mail</span><span class="p">;</span>
<span class="k">CREATE</span> <span class="k">DATABASE</span> <span class="n">mail</span><span class="p">;</span>
</pre></div>
<p>We now have to dump the contents of the mailnew database into the newly created mail database :</p>
<div class="highlight"><pre><span></span>mysqldump mailnew <span class="p">|</span> mysql mail
</pre></div>
<p>Next, let's Postfix and Dovecot so that it starts using the new database schema :</p>
<div class="highlight"><pre><span></span>systemctl restart postfix
systemctl restart dovecot
</pre></div>
<p>At this point, Postfix and Dovecot are using the Postfix Admin schema in the mail database.</p>
<p>The last thing we have to do is to edit Postfix Admin's config.inc.php file to use the mail database as well instead of the mailnew database that it should be currently using.</p>
<h1>Cleanup</h1>
<p>Once you've confirmed that everything is working properly, you can delete the backup files we've created :</p>
<div class="highlight"><pre><span></span>rm -rf /etc/postfix/mysql-backup
rm /etc/dovecot/sql.conf.bak
</pre></div>
<p>You can drop the mailnew database as well :</p>
<div class="highlight"><pre><span></span><span class="k">DROP</span> <span class="k">DATABASE</span> <span class="n">mailnew</span><span class="p">;</span>
</pre></div>
<h1>Conclusion</h1>
<p>That's all ! As always, please do leave a comment if this article has been of any use to you !</p>My tmux configuration2016-02-02T00:00:00+01:00Antoine Jouberttag:captainark.net,2016-02-02:my-tmux-configuration.html<p><a href="https://tmux.github.io/">tmux</a> is a terminal mutiplexer. It lets you have multiples shells running in a single terminal emulator window and it keeps those shells running in the background should you need to close your terminal emulator.</p>
<p>I've played around with the configuration quite a bit to find settings that suit my needs. Here's what it ended up looking like :</p>
<p><a href="images/tmux_fullsize.png"><img alt="tmux" src="images/tmux.png" /></a></p>
<p>This screenshot was done on Mac OS X, using the Terminal app and this <a href="https://github.com/tomislav/osx-terminal.app-colors-solarized">Solarized theme</a>.</p>
<p>I figured I'd share my tmux configuration here !</p>
<h2>Installing tmux</h2>
<p>tmux is available on Debian. I suggest using the <a href="https://packages.debian.org/jessie-backports/tmux">jessie backports</a> version :</p>
<p><code>apt -t jessie-backports install tmux</code></p>
<p>tmux is also available on Mac OS X using <a href="http://brew.sh/">brew</a> :</p>
<p><code>brew install tmux</code></p>
<h2>tmux.conf</h2>
<p>I used screen before tmux, so I configured the prefix key on C-a instead of C-b. tmux has the advantage of being <em>much</em> simpler to configure than screen.</p>
<p>If you want to use this configuration, simply copy the following in ~/.tmux.conf. This file is read by default when tmux starts.</p>
<p>If you simply want to try it out, copy it in a file somewhere else and have tmux load with the -f parameter (<code>tmux -f ~/tmux-test.conf</code>).</p>
<div class="highlight"><pre><span></span># use utf8
set -g utf8
set-option -g status-utf8 on
set-window-option -g utf8 on
# do not wait on esc key
set-option -g escape-time 0
# completely disable automatic rename
set-window-option -g automatic-rename off
# basic settings
set -g default-terminal "screen-256color"
set -g aggressive-resize off
set-window-option -g xterm-keys on
#set-window-option -g mode-mouse off
# command history
set -g history-limit 10000
# messages
set -g message-bg default
set -g message-fg red
# no visual activity
set -g visual-activity off
set -g visual-bell off
# status bar
set-option -g status-justify centre
set-option -g status-bg default
set-option -g status-fg blue
set-option -g status-interval 5
set-option -g status-left-length 30
set-option -g status-left '#[fg=red][ #[fg=white]#H #[fg=red]]#[default]'
set-option -g status-right '#[fg=red][ #[fg=white]%R %d/%m #[fg=red]]#[default]'
# modes
set-option -g mode-bg default
set-option -g mode-fg blue
# inactive window format
set-window-option -g window-status-format '#I:#W#F'
set-window-option -g monitor-activity on
#set-window-option -g monitor-content on # not available in tmux 2.0
# activity in a window
set-window-option -g window-status-activity-attr dim
set-window-option -g window-status-activity-bg default
set-window-option -g window-status-activity-fg yellow
# content in a window # not available in tmux 2.0
#set-window-option -g window-status-content-attr dim
#set-window-option -g window-status-content-bg default
#set-window-option -g window-status-content-fg red
# active window format
set-window-option -g window-status-current-fg white
set-window-option -g window-status-current-bg default
set-window-option -g window-status-current-format '#[fg=red](#[default]#I:#W#F#[fg=red])#[default]'
# reload tmux configuration
unbind r
bind r source-file ~/.tmux.conf \; display "Configuration reloaded!"
# Screen-like keybinds
unbind C-b
set -g prefix ^A
set -g prefix2 ^Q
bind a send-prefix
bind q send-prefix
unbind c
bind c new-window
unbind ^C
bind ^C new-window
unbind n
bind n next-window
unbind ^N
bind ^N next-window
unbind A
bind A command-prompt "rename-window %%"
unbind p
bind p previous-window
unbind ^P
bind ^P previous-window
unbind a
bind a last-window
unbind ^A
bind ^A last-window
unbind [
bind Escape copy-mode
unbind w
bind w list-windows
unbind k
bind k confirm-before "kill-window"
unbind l
bind l refresh-client
unbind '"'
bind '"' choose-window
</pre></div>
<h2>Aliases</h2>
<p>I also use two functions with tmux (in ~/.bash_aliases).</p>
<p>The first one creates a new "mytmux" tmux session if one doesn't exist yet, opens 10 shells and selects the first one.</p>
<div class="highlight"><pre><span></span>mytmux<span class="o">()</span> <span class="o">{</span>
tmux has-session -t mytmux
<span class="k">if</span> <span class="o">[</span> <span class="nv">$?</span> !<span class="o">=</span> <span class="m">0</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
tmux new-session -s mytmux -n <span class="k">$(</span>hostname<span class="k">)</span> -d
tmux new-window -t mytmux:1 -n <span class="k">$(</span>hostname<span class="k">)</span>
tmux new-window -t mytmux:2 -n <span class="k">$(</span>hostname<span class="k">)</span>
tmux new-window -t mytmux:3 -n <span class="k">$(</span>hostname<span class="k">)</span>
tmux new-window -t mytmux:4 -n <span class="k">$(</span>hostname<span class="k">)</span>
tmux new-window -t mytmux:5 -n <span class="k">$(</span>hostname<span class="k">)</span>
tmux new-window -t mytmux:6 -n <span class="k">$(</span>hostname<span class="k">)</span>
tmux new-window -t mytmux:7 -n <span class="k">$(</span>hostname<span class="k">)</span>
tmux new-window -t mytmux:8 -n <span class="k">$(</span>hostname<span class="k">)</span>
tmux new-window -t mytmux:9 -n <span class="k">$(</span>hostname<span class="k">)</span>
tmux <span class="k">select</span>-window -t mytmux:0
<span class="k">fi</span>
tmux attach -t mytmux
<span class="o">}</span>
</pre></div>
<p>The second one changes the tmux window name whenever I ssh to a remote host, and switches the window name back to the name of my computer when I logout from the host.</p>
<div class="highlight"><pre><span></span><span class="k">if</span> <span class="o">[</span> -n <span class="s2">"</span><span class="nv">$TMUX</span><span class="s2">"</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
ssh<span class="o">()</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">[</span> <span class="nv">$#</span> -le <span class="m">2</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
tmux rename-window <span class="s2">"</span><span class="si">${</span><span class="p">@: -1</span><span class="si">}</span><span class="s2">"</span>
<span class="nb">command</span> ssh <span class="s2">"</span><span class="nv">$@</span><span class="s2">"</span>
tmux rename-window <span class="s2">"</span><span class="k">$(</span>hostname<span class="k">)</span><span class="s2">"</span>
<span class="k">else</span>
<span class="nb">command</span> ssh <span class="s2">"</span><span class="nv">$@</span><span class="s2">"</span>
<span class="k">fi</span>
<span class="o">}</span>
<span class="k">fi</span>
</pre></div>
<h2>Conclusion</h2>
<p>That's all ! As always, please do leave a comment if you've found something useful in this article !</p>Debian updates with Ansible2016-01-31T00:00:00+01:00Antoine Jouberttag:captainark.net,2016-01-31:debian-updates-with-ansible.html<p>I've recently bought a <a href="http://www8.hp.com/us/en/products/proliant-servers/product-detail.html?oid=5379860">HP Proliant Microserver Gen8</a> to play around with LXC and try new stuff.</p>
<p>From the 4 Debian machines I had to keep up-to-date, I now have 7, so it became quite time-consumming to manually SSH to each of them whenever an update became available.</p>
<p>I ended up looking at <a href="http://www.ansible.com/">Ansible</a> to speed up the process and, within an hour, I had a working playbook that updates the debian packages, pip packages and git repos installed on all of my servers with a single command.</p>
<p>I figured I'd share the playbook I use to update the Debian packages !</p>
<h2>The playbook</h2>
<p>I modified <a href="https://gist.github.com/maethor/380676f6b1cec8cc7439">this gist</a> to only use apt-get instead of both apt-get and aptitude.</p>
<div class="highlight"><pre><span></span><span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">hosts</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">all</span>
<span class="l l-Scalar l-Scalar-Plain">remote_user</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">admin</span>
<span class="l l-Scalar l-Scalar-Plain">become</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">yes</span>
<span class="l l-Scalar l-Scalar-Plain">become_method</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">sudo</span>
<span class="l l-Scalar l-Scalar-Plain">tasks</span><span class="p p-Indicator">:</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">update cache</span>
<span class="l l-Scalar l-Scalar-Plain">apt</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">update_cache=yes</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">list packages to upgrade (1/2)</span>
<span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">apt-get upgrade --show-upgraded --assume-no | grep '^ ' | sed 's/ //;s/ /\n/g'</span>
<span class="l l-Scalar l-Scalar-Plain">register</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">updates</span>
<span class="l l-Scalar l-Scalar-Plain">changed_when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">False</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">list packages to upgrade (2/2)</span>
<span class="l l-Scalar l-Scalar-Plain">debug</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">msg="{{ updates.stdout_lines | count }} packages to upgrade ({{ updates.stdout_lines | join(', ') }})"</span>
<span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">(updates.stdout_lines)</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">upgrade packages</span>
<span class="l l-Scalar l-Scalar-Plain">apt</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">upgrade=dist</span>
<span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">(updates.stdout_lines)</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">check what the new version is</span>
<span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">lsb_release -r | awk '{print $2}'</span>
<span class="l l-Scalar l-Scalar-Plain">changed_when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">False</span>
<span class="l l-Scalar l-Scalar-Plain">register</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">new_release</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">notify distribution version upgrade</span>
<span class="l l-Scalar l-Scalar-Plain">debug</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">msg="Debian has been upgraded from {{ ansible_lsb.release }} to {{ new_release.stdout }}"</span>
<span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">ansible_lsb.release != new_release.stdout</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">install the debian-goodies package if it is missing</span>
<span class="l l-Scalar l-Scalar-Plain">apt</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">name=debian-goodies state=present</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">list services to restart (1/2)</span>
<span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">checkrestart | grep ^service | awk '{print $2}'</span>
<span class="l l-Scalar l-Scalar-Plain">register</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">services</span>
<span class="l l-Scalar l-Scalar-Plain">changed_when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">False</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">list services to restart (2/2)</span>
<span class="l l-Scalar l-Scalar-Plain">debug</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">msg="{{ services.stdout_lines | count }} services to restart ({{ services.stdout_lines | join (', ') }})"</span>
<span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">(services.stdout_lines)</span>
<span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">cache cleanup</span>
<span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">apt-get autoclean</span>
</pre></div>
<h2>Conclusion</h2>
<p>That's all ! Please leave a comment if you've found this playbook helpful !</p>Private Git Repo2016-01-31T00:00:00+01:00Antoine Jouberttag:captainark.net,2016-01-31:private-git-repo.html<p>I've decided to migrate this blog to <a href="http://blog.getpelican.com/">Pelican</a>. I've been playing around with it over the week-end, and it turns out to be way easier to manage than <a href="https://jekyllrb.com/">Jekyll</a>. Themes are much easier to install and configure, so it ends up looking better as well !</p>
<p>Since I'm basically recreating this blog from scratch, I've decided to delete the old git repo that was hosting it and to create a new one.</p>
<p>Setting up your own private git repo is pretty easy to achieve and is already well-documented on the <a href="https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server">Git</a> website.</p>
<p>Every time I want to create a new repo, I've had time to forget how to do it and I end up looking for that page, so I figured I'd write a few lines on the subject.</p>
<p>In this tutorial, I'll configure a git repo on a distant server running Debian 8 (Jessie). This repo will be remotely accessible using SSH. Two users will be able to connect to it : me and the www-data user on my webserver.</p>
<h2>SSH Keys</h2>
<p>If you don't have one already, you'll need a ssh-key to connect to the git repo.</p>
<p>On your computer, in a shell, as your usual user :</p>
<div class="highlight"><pre><span></span>ssh-keygen -t rsa -b 3072
Generating public/private rsa key pair.
Enter file in which to save the key <span class="o">(</span>/home/user/.ssh/id_rsa<span class="o">)</span>:
Enter passphrase <span class="o">(</span>empty <span class="k">for</span> no passphrase<span class="o">)</span>:
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/id_rsa.pub.
The key fingerprint is:
<span class="o">[</span>Redacted<span class="o">]</span>
</pre></div>
<p>For security reasons, configuring a passphrase is recommended. On Mac OS X and most desktop environnements on Linux, you can store this passphrase for the duration of your session using the <code>ssh-add</code> command, so you won't have to type it every time you want to connect to a host.</p>
<p>On the server, we also have to create a ssh-key for the user that is running our webserver (you'll need to have sudo installed) :</p>
<div class="highlight"><pre><span></span>sudo -H -u www-data ssh-keygen -t rsa -b 3072
Generating public/private rsa key pair.
Enter file in which to save the key <span class="o">(</span>/var/www/.ssh/id_rsa<span class="o">)</span>:
Enter passphrase <span class="o">(</span>empty <span class="k">for</span> no passphrase<span class="o">)</span>:
Enter same passphrase again:
Your identification has been saved in /var/www/.ssh/id_rsa.
Your public key has been saved in /var/www/.ssh/id_rsa.pub.
The key fingerprint is:
<span class="o">[</span>Redacted<span class="o">]</span>
</pre></div>
<p>If you decide to configure a passphrase for that ssh-key, you'll have to type it every time you'll want to pull from your repo.</p>
<h2>Server management</h2>
<p>All of the commands in this section have to be run as root.</p>
<p>First thing first, we have to install the git package on the server that will be hosting our git repos :</p>
<div class="highlight"><pre><span></span>apt update <span class="o">&&</span> apt install git -y
</pre></div>
<p>Then, we have to create a user named git :</p>
<div class="highlight"><pre><span></span>useradd -s /usr/bin/git-shell -m -r git
</pre></div>
<p>This will create a system user (UID < 1000) with a /home/git home directory. If you want to host your git repos somewhere else on your filesystem, you should add a <code>-d /home/directory/for/git</code> in the previous command.</p>
<p>This user will use the git-shell shell. This limits remote connection to that user to git commands (like the rssh shell can limit remote connection to a user to scp or rsync commands).</p>
<p>We have to configure our system to allow the use of this shell :</p>
<div class="highlight"><pre><span></span><span class="nb">echo</span> <span class="s1">'/usr/bin/git-shell'</span> >> /etc/shells
</pre></div>
<p>From this point, you should have to following output if you try to SSH to your server with that user :</p>
<div class="highlight"><pre><span></span>ssh git@git.captainark.net
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have <span class="nb">read</span> and execute access.
Connection to git@git.captainark.net closed.
</pre></div>
<p>We now need to create the .ssh/authorized_keys file for the git user with the correct permissions :</p>
<div class="highlight"><pre><span></span>sudo -H -u git mkdir /home/git/.ssh <span class="o">&&</span> chmod <span class="m">700</span> /home/git/.ssh
sudo -H -u git touch /home/git/.ssh/authorized_keys <span class="o">&&</span> chmod <span class="m">600</span> /home/git/.ssh/authorized_keys
</pre></div>
<p>You can now copy/paste the content of the two <code>$HOME/.ssh/id_rsa.pub</code> files we've created earlier using the <code>ssh-keygen</code> command in <code>/home/git/.ssh/authorized_keys</code>.</p>
<p>The last thing we have to do is to create our first git repo. In this example, my project will be called 'captainarkdotnet' as it will be hosting this blog :</p>
<div class="highlight"><pre><span></span>sudo -H -u git mkdir /home/git/captainarkdotnet.git
<span class="nb">cd</span> /home/git/captainarkdotnet.git
sudo -H -u git git init --bare
</pre></div>
<p>The last command should give you the following output :</p>
<div class="highlight"><pre><span></span>Initialized empty Git repository in /home/git/captainarkdotnet.git/.git/
</pre></div>
<p>We're done with the server configuration. Let's now actually push stuff to our repo !</p>
<h3>Initial push</h3>
<p>The files for my blog are store in the ~/Documents/projects/captainarkdotnet on my computer. Before doing anything else, we first have to make sure that we currently are in that folder :</p>
<div class="highlight"><pre><span></span><span class="nb">cd</span> ~/Documents/projects/captainarkdotnet
</pre></div>
<p>Let's now push the content of that folder to our repo :</p>
<div class="highlight"><pre><span></span>git init
git add .
git commit -m <span class="s1">'initial commit'</span>
git remote add origin git@git.captainark.net:captainarkdotnet.git
git push origin master
</pre></div>
<p>Please note that you'll need to edit <strong>git.captainark.net</strong> to the FQDN or IP of your git server, and <strong>captainarkdotnet.git</strong> to the name of the git project on your server.</p>
<p>If everything went well, the last command should give you the following output :</p>
<div class="highlight"><pre><span></span>Counting objects: 69, <span class="k">done</span>.
Delta compression using up to <span class="m">4</span> threads.
Compressing objects: 100% <span class="o">(</span>64/64<span class="o">)</span>, <span class="k">done</span>.
Writing objects: 100% <span class="o">(</span>69/69<span class="o">)</span>, 1.01 MiB <span class="p">|</span> <span class="m">0</span> bytes/s, <span class="k">done</span>.
Total <span class="m">69</span> <span class="o">(</span>delta 15<span class="o">)</span>, reused <span class="m">0</span> <span class="o">(</span>delta 0<span class="o">)</span>
To git@git.captainark.net:captainarkdotnet.git
* <span class="o">[</span>new branch<span class="o">]</span> master -> master
</pre></div>
<p>That's it, we've now pushed our first commit to our server !</p>
<h2>First pull</h2>
<p>Alright, time to pull the files we've just pushed on our webserver. I personally store my web content in <code>/var/www</code> ; if you don't, you'll have to adjust the path accordingly :</p>
<div class="highlight"><pre><span></span><span class="nb">cd</span> /var/www
sudo -H -u www-data git clone git@git.captainark.net:captainarkdotnet.git
</pre></div>
<p>SSH will ask you to type 'yes' since it's the first time the www-data user connects to the server. If everything goes well, you should have the following output :</p>
<div class="highlight"><pre><span></span>Cloning into <span class="s1">'captainarkdotnet'</span>...
remote: Counting objects: 70, <span class="k">done</span>.
remote: Compressing objects: 100% <span class="o">(</span>65/65<span class="o">)</span>, <span class="k">done</span>.
remote: Total <span class="m">70</span> <span class="o">(</span>delta 16<span class="o">)</span>, reused <span class="m">0</span> <span class="o">(</span>delta 0<span class="o">)</span>
Receiving objects: 100% <span class="o">(</span>70/70<span class="o">)</span>, 1.01 MiB <span class="p">|</span> <span class="m">0</span> bytes/s, <span class="k">done</span>.
Resolving deltas: 100% <span class="o">(</span>16/16<span class="o">)</span>, <span class="k">done</span>.
Checking connectivity... <span class="k">done</span>.
</pre></div>
<h2>Conclusion</h2>
<p>That's it ! We now have a working private git repo ! I won't go into details into the git commands in this tutorial, but here's a quick overwiew of the ones I use the most :</p>
<ul>
<li><code>git add .</code> recursively adds all files from the directory to the repo ;</li>
<li><code>git commit -a -m 'This is a comment'</code> commits the current state of your local repo with the 'This is a comment' comment ;</li>
<li><code>git push</code> pushes your commits to the distant repo ;</li>
<li><code>git pull</code> pulls the latest version of the distant repo locally ;</li>
<li><code>git branch -av</code> shows all available branches for the repo ;</li>
<li><code>git checkout -b testing remotes/origin/testing</code> create a local 'testing' branch based on the remote 'remotes/origin/testing' branch ;</li>
<li>once a branch has been copied locally, you can switch to it with the <code>git checkout {branch}</code> command.</li>
</ul>
<p>For more information on git a command, use <code>man git-{command}</code> !</p>
<p>If you've found this tutorial in any way helpful, please feel free to leave a comment !</p>Flexget init script2015-05-05T00:00:00+02:00Antoine Jouberttag:captainark.net,2015-05-05:flexget-init-script.html<p>I've been using <a href="http://flexget.com/">Flexget</a> for the past two years or so as a download automator.</p>
<p>Since I wrote an <a href="http://flexget.com/wiki/Daemon/Startup#InsservscriptDebiancompatible">init script</a> for it a while back, and it is compatible with Debian Jessie / systemd, I figured I'd share it here.</p>
<h2>The script</h2>
<p>All of the following should be done as the root user.</p>
<p>First, create a /etc/default/flexget file with the following content :</p>
<div class="highlight"><pre><span></span><span class="c1"># Configuration file for /etc/init.d/flexget</span>
<span class="c1"># User to run flexget as.</span>
<span class="c1"># Daemon will not start if left empty.</span>
<span class="nv">FGUSER</span><span class="o">=</span><span class="s2">""</span>
<span class="c1"># Full path to the flexget config.yml file to use.</span>
<span class="c1"># Defaults to FGUSER $HOME/.flexget/config.yml</span>
<span class="nv">CONFIG</span><span class="o">=</span><span class="s2">""</span>
<span class="c1"># Path to the directory where flexget should log. Do not add trailing slash.</span>
<span class="c1"># Defaults to the FGUSER $HOME/.flexget directory</span>
<span class="nv">LOG</span><span class="o">=</span><span class="s2">""</span>
<span class="c1"># Log verbosity </span>
<span class="c1"># Available options : none critical error warning info verbose debug trace</span>
<span class="c1"># Defaults to info</span>
<span class="nv">LEVEL</span><span class="o">=</span><span class="s2">""</span>
</pre></div>
<p>Please note that the FGUSER variable needs to be defined for the daemon to start. It can be set to your current user, or you can run flexget as its own user.</p>
<p>You can create a flexget user with the following command :</p>
<div class="highlight"><pre><span></span>useradd -m -d /var/lib/flexget -r -s /bin/false flexget
</pre></div>
<p>Then, create the /etc/init.d/flexget file :</p>
<div class="highlight"><pre><span></span><span class="ch">#!/bin/bash</span>
<span class="c1">### BEGIN INIT INFO</span>
<span class="c1"># Provides: flexget</span>
<span class="c1"># Required-Start: $network $remote_fs</span>
<span class="c1"># Required-Stop: $network $remote_fs</span>
<span class="c1"># Should-Start: </span>
<span class="c1"># Should-Stop: </span>
<span class="c1"># Default-Start: 2 3 4 5</span>
<span class="c1"># Default-Stop: 0 1 6</span>
<span class="c1"># Short-Description: Flexget</span>
<span class="c1"># Description: FlexGet is a multipurpose automation tool </span>
<span class="c1"># for content like torrents, nzbs, podcasts,</span>
<span class="c1"># comics, series, movies, etc.</span>
<span class="c1">### END INIT INFO</span>
<span class="c1"># Author: Antoine Joubert, 19/01/2014</span>
<span class="nv">NAME</span><span class="o">=</span><span class="s2">"flexget"</span>
<span class="nv">DAEMON</span><span class="o">=</span><span class="s2">"/usr/local/bin/flexget"</span>
<span class="nv">SETTINGS</span><span class="o">=</span><span class="s2">"/etc/default/</span><span class="nv">$NAME</span><span class="s2">"</span>
<span class="nv">DESC</span><span class="o">=</span><span class="s2">"Flexget"</span>
<span class="nv">PIDFILE</span><span class="o">=</span><span class="s2">"/var/run/</span><span class="nv">$NAME</span><span class="s2">.pid"</span>
<span class="nb">set</span> -e
. /lib/lsb/init-functions
<span class="nb">unset</span> FGUSER CONFIG LOG LEVEL
<span class="c1"># Exit if flexget not installed</span>
<span class="k">if</span> <span class="o">[</span> ! -x <span class="s2">"</span><span class="nv">$DAEMON</span><span class="s2">"</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
log_action_msg <span class="s2">"</span><span class="nv">$DESC</span><span class="s2">: Could not find flexget executable. Exiting."</span>
<span class="nb">exit</span> 2
<span class="k">fi</span>
<span class="c1"># Read configuration variables</span>
<span class="k">if</span> <span class="o">[</span> -r /etc/default/<span class="nv">$NAME</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
. /etc/default/<span class="nv">$NAME</span>
<span class="k">else</span>
log_action_msg <span class="s2">"</span><span class="nv">$DESC</span><span class="s2">: /etc/default/</span><span class="nv">$NAME</span><span class="s2"> not found. Exiting."</span>
<span class="nb">exit</span> 2
<span class="k">fi</span>
<span class="c1"># Exit if FGUSER has not been set in /etc/default/flexget</span>
<span class="k">if</span> <span class="o">[</span> -z <span class="nv">$FGUSER</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
log_action_msg <span class="s2">"</span><span class="nv">$DESC</span><span class="s2">: FGUSER not set in /etc/default/</span><span class="nv">$NAME</span><span class="s2">. Exiting."</span>
<span class="nb">exit</span> 2
<span class="k">fi</span>
<span class="c1"># Function to verify if flexget is already running</span>
run_check<span class="o">()</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">[</span> -e <span class="nv">$PIDFILE</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
status_of_proc -p <span class="nv">$PIDFILE</span> <span class="nv">$DAEMON</span> <span class="nv">$NAME</span> > /dev/null <span class="o">&&</span> <span class="nv">RETVAL</span><span class="o">=</span><span class="m">0</span> <span class="o">||</span> <span class="nv">RETVAL</span><span class="o">=</span><span class="s2">"</span><span class="nv">$?</span><span class="s2">"</span>
<span class="k">else</span>
<span class="nv">RETVAL</span><span class="o">=</span><span class="s2">"2"</span>
<span class="k">fi</span>
<span class="o">}</span>
end_log<span class="o">()</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">[</span> <span class="nv">$RETVAL</span> -eq <span class="m">0</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
log_end_msg 0
<span class="k">return</span> 0
<span class="k">else</span>
log_end_msg 1
<span class="nb">exit</span> 1
<span class="k">fi</span>
<span class="o">}</span>
<span class="c1"># Function to define config file, log file and log level</span>
conf_check<span class="o">()</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">[</span> -z <span class="nv">$CONFIG</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
<span class="nv">OPTIONS</span><span class="o">=</span><span class="s2">"</span><span class="nv">$OPTIONS</span><span class="s2">"</span>
<span class="k">else</span>
<span class="nv">OPTIONS</span><span class="o">=</span><span class="s2">"-c </span><span class="nv">$CONFIG</span><span class="s2">"</span>
<span class="k">fi</span>
<span class="k">if</span> <span class="o">[</span> -z <span class="nv">$LOG</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
<span class="nv">OPTIONS</span><span class="o">=</span><span class="s2">"</span><span class="nv">$OPTIONS</span><span class="s2">"</span>
<span class="k">else</span>
<span class="nv">OPTIONS</span><span class="o">=</span><span class="s2">"</span><span class="nv">$OPTIONS</span><span class="s2"> -l </span><span class="nv">$LOG</span><span class="s2">/flexget.log"</span>
<span class="k">if</span> <span class="o">[</span> ! -d <span class="nv">$LOG</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
mkdir -p -m <span class="m">750</span> <span class="nv">$LOG</span>
chown <span class="nv">$FGUSER</span> <span class="nv">$LOG</span>
<span class="k">fi</span>
<span class="k">fi</span>
<span class="k">if</span> <span class="o">[</span> -z <span class="nv">$LEVEL</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
<span class="nv">OPTIONS</span><span class="o">=</span><span class="s2">"</span><span class="nv">$OPTIONS</span><span class="s2">"</span>
<span class="k">else</span>
<span class="nv">OPTIONS</span><span class="o">=</span><span class="s2">"</span><span class="nv">$OPTIONS</span><span class="s2"> -L </span><span class="nv">$LEVEL</span><span class="s2">"</span>
<span class="k">fi</span>
<span class="o">}</span>
start_flexget<span class="o">()</span> <span class="o">{</span>
run_check
<span class="k">if</span> <span class="o">[</span> <span class="nv">$RETVAL</span> <span class="o">=</span> <span class="m">0</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
log_action_msg <span class="s2">"</span><span class="nv">$DESC</span><span class="s2">: Already running with PID </span><span class="k">$(</span>cat <span class="nv">$PIDFILE</span><span class="k">)</span><span class="s2">. Aborting."</span>
<span class="nb">exit</span> 2
<span class="k">else</span>
conf_check
log_daemon_msg <span class="s2">"</span><span class="nv">$DESC</span><span class="s2">: Starting the daemon."</span>
start-stop-daemon --start --background --quiet --pidfile <span class="nv">$PIDFILE</span> --make-pidfile <span class="se">\</span>
--chuid <span class="nv">$FGUSER</span> --user <span class="nv">$FGUSER</span> --exec <span class="nv">$DAEMON</span> -- <span class="nv">$OPTIONS</span> daemon start
<span class="nv">RETVAL</span><span class="o">=</span><span class="nv">$?</span>
end_log
<span class="k">fi</span>
<span class="o">}</span>
stop_flexget<span class="o">()</span> <span class="o">{</span>
run_check
<span class="k">if</span> <span class="o">[</span> <span class="nv">$RETVAL</span> <span class="o">=</span> <span class="m">0</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
log_daemon_msg <span class="s2">"</span><span class="nv">$DESC</span><span class="s2">: Stopping the daemon."</span>
start-stop-daemon --stop --quiet --chuid <span class="s2">"</span><span class="nv">$FGUSER</span><span class="s2">"</span> --pidfile <span class="s2">"</span><span class="nv">$PIDFILE</span><span class="s2">"</span> --retry 30
<span class="nv">RETVAL</span><span class="o">=</span><span class="nv">$?</span>
<span class="o">[</span> -e <span class="s2">"</span><span class="nv">$PIDFILE</span><span class="s2">"</span> <span class="o">]</span> <span class="o">&&</span> rm -f <span class="s2">"</span><span class="nv">$PIDFILE</span><span class="s2">"</span>
end_log
<span class="k">else</span>
log_action_msg <span class="s2">"</span><span class="nv">$DESC</span><span class="s2">: Not currently running. Aborting."</span>
<span class="nb">exit</span> 2
<span class="k">fi</span>
<span class="o">}</span>
status_flexget<span class="o">()</span> <span class="o">{</span>
run_check
<span class="k">if</span> <span class="o">[</span> <span class="nv">$RETVAL</span> <span class="o">=</span> <span class="m">0</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
log_action_msg <span class="s2">"</span><span class="nv">$DESC</span><span class="s2">: Currently running with PID </span><span class="k">$(</span>cat <span class="nv">$PIDFILE</span><span class="k">)</span><span class="s2">."</span>
<span class="k">else</span>
log_action_msg <span class="s2">"</span><span class="nv">$DESC</span><span class="s2">: Not currently running."</span>
<span class="k">fi</span>
<span class="nb">exit</span> <span class="nv">$RETVAL</span>
<span class="o">}</span>
<span class="k">case</span> <span class="s2">"</span><span class="nv">$1</span><span class="s2">"</span> in
start<span class="o">)</span>
start_flexget
<span class="p">;;</span>
stop<span class="o">)</span>
stop_flexget
<span class="p">;;</span>
restart<span class="o">)</span>
stop_flexget <span class="o">&&</span> sleep <span class="m">2</span> <span class="o">&&</span> start_flexget
<span class="p">;;</span>
status<span class="o">)</span>
status_flexget
<span class="p">;;</span>
*<span class="o">)</span>
<span class="nb">echo</span> <span class="s2">"Usage: </span><span class="nv">$0</span><span class="s2"> {start|stop|restart|status}"</span>
<span class="p">;;</span>
<span class="k">esac</span>
<span class="nb">exit</span> 0
</pre></div>
<p>Then, give execution rights to the script :</p>
<div class="highlight"><pre><span></span>chmod +x /etc/init.d/flexget
</pre></div>
<p>And then, generate the necessary symlinks for the service to start on boot :</p>
<p><em>Debian Jessie</em></p>
<div class="highlight"><pre><span></span>systemctl <span class="nb">enable</span> flexget
</pre></div>
<p><em>Debian Wheezy</em></p>
<div class="highlight"><pre><span></span>insserv flexget
</pre></div>
<p>To start, stop or check if the daemon is running :</p>
<p><em>Debian Jessie</em></p>
<div class="highlight"><pre><span></span>systemctl start flexget
systemctl stop flexget
systemctl status flexget
</pre></div>
<p><em>Debian Wheezy / Jessie</em></p>
<div class="highlight"><pre><span></span>service flexget start
service flexget stop
service flexget status
</pre></div>
<p><em>Debian Wheezy</em></p>
<div class="highlight"><pre><span></span>/etc/init.d/flexget start
/etc/init.d/flexget stop
/etc/init.d/flexget status
</pre></div>
<h2>Conclusion</h2>
<p>That's all ! If you are using this script, please let me know in the comment section below !</p>Setting up a mail server2015-04-24T00:00:00+02:00Antoine Jouberttag:captainark.net,2015-04-24:setting-up-a-mail-server.html<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>Initial configuration</h2>
<h3>Installing the required packages</h3>
<p>First thing first, we need to install the packages we'll need for this configuration :</p>
<div class="highlight"><pre><span></span>apt update
apt install mysql-server mysql-client postfix postfix-mysql <span class="se">\</span>
postfix-policyd-spf-python dovecot-core dovecot-imapd dovecot-lmtpd <span class="se">\</span>
dovecot-mysql dovecot-sieve dovecot-managesieved dovecot-antispam <span class="se">\</span>
opendkim opendkim-tools monit opendmarc spamassassin spamc
</pre></div>
<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>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>
<div class="highlight"><pre><span></span><span class="o">[</span>client<span class="o">]</span>
<span class="nv">host</span> <span class="o">=</span> localhost
<span class="nv">user</span> <span class="o">=</span> root
<span class="nv">password</span> <span class="o">=</span> myverysecurepassword
<span class="nv">socket</span> <span class="o">=</span> /var/run/mysqld/mysqld.sock
</pre></div>
<p>Once it has been created, change the permissions on the file to make sure no other user can read it :</p>
<div class="highlight"><pre><span></span>chmod <span class="m">600</span> ~/.my.cnf
</pre></div>
<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>
<div class="highlight"><pre><span></span><span class="nb">echo</span> <span class="s1">'export MYSQL_PS1="[\u@\h] (\d)> "'</span> > ~/.bash_aliases
</pre></div>
<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>
<div class="highlight"><pre><span></span>:~$ mysql mysql
<span class="o">[</span>...<span class="o">]</span>
<span class="o">[</span>root@localhost<span class="o">]</span> <span class="o">(</span>mysql<span class="o">)</span>>
</pre></div>
<h2>Configuring the MySQL database</h2>
<h3>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>
<div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">DATABASE</span> <span class="n">mail</span><span class="p">;</span>
</pre></div>
<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>
<div class="highlight"><pre><span></span><span class="k">GRANT</span> <span class="k">SELECT</span> <span class="k">ON</span> <span class="n">mail</span><span class="p">.</span><span class="o">*</span> <span class="k">TO</span> <span class="s1">'mail'</span><span class="o">@</span><span class="s1">'localhost'</span> <span class="n">IDENTIFIED</span> <span class="k">BY</span> <span class="s1">'mailpassword'</span><span class="p">;</span>
<span class="n">FLUSH</span> <span class="k">PRIVILEGES</span><span class="p">;</span>
</pre></div>
<p>We are now going to create the necessary tables for our needs. Let's first use the mail database :</p>
<div class="highlight"><pre><span></span><span class="n">USE</span> <span class="n">mail</span><span class="p">;</span>
</pre></div>
<p>The first table we are going to create will contain the domains we will be using with our mail server :</p>
<div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="o">`</span><span class="n">virtual_domains</span><span class="o">`</span> <span class="p">(</span>
<span class="o">`</span><span class="n">id</span><span class="o">`</span> <span class="nb">INT</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="n">AUTO_INCREMENT</span><span class="p">,</span>
<span class="o">`</span><span class="n">name</span><span class="o">`</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="k">PRIMARY</span> <span class="k">KEY</span> <span class="p">(</span><span class="o">`</span><span class="n">id</span><span class="o">`</span><span class="p">)</span>
<span class="p">)</span> <span class="n">ENGINE</span><span class="o">=</span><span class="n">InnoDB</span> <span class="k">DEFAULT</span> <span class="n">CHARSET</span><span class="o">=</span><span class="n">utf8</span><span class="p">;</span>
</pre></div>
<p>Then, we are going to create the table that will contain our users and their password :</p>
<div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="o">`</span><span class="n">virtual_users</span><span class="o">`</span> <span class="p">(</span>
<span class="o">`</span><span class="n">id</span><span class="o">`</span> <span class="nb">INT</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="n">AUTO_INCREMENT</span><span class="p">,</span>
<span class="o">`</span><span class="n">domain_id</span><span class="o">`</span> <span class="nb">INT</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="o">`</span><span class="n">password</span><span class="o">`</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">106</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="o">`</span><span class="n">email</span><span class="o">`</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">120</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="k">PRIMARY</span> <span class="k">KEY</span> <span class="p">(</span><span class="o">`</span><span class="n">id</span><span class="o">`</span><span class="p">),</span>
<span class="k">UNIQUE</span> <span class="k">KEY</span> <span class="o">`</span><span class="n">email</span><span class="o">`</span> <span class="p">(</span><span class="o">`</span><span class="n">email</span><span class="o">`</span><span class="p">),</span>
<span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="p">(</span><span class="n">domain_id</span><span class="p">)</span> <span class="k">REFERENCES</span> <span class="n">virtual_domains</span><span class="p">(</span><span class="n">id</span><span class="p">)</span> <span class="k">ON</span> <span class="k">DELETE</span> <span class="k">CASCADE</span>
<span class="p">)</span> <span class="n">ENGINE</span><span class="o">=</span><span class="n">InnoDB</span> <span class="k">DEFAULT</span> <span class="n">CHARSET</span><span class="o">=</span><span class="n">utf8</span><span class="p">;</span>
</pre></div>
<p>Finally, the last table we are going to create will contain our mail aliases :</p>
<div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="o">`</span><span class="n">virtual_aliases</span><span class="o">`</span> <span class="p">(</span>
<span class="o">`</span><span class="n">id</span><span class="o">`</span> <span class="nb">INT</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="n">AUTO_INCREMENT</span><span class="p">,</span>
<span class="o">`</span><span class="n">domain_id</span><span class="o">`</span> <span class="nb">INT</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="o">`</span><span class="k">source</span><span class="o">`</span> <span class="nb">varchar</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="o">`</span><span class="n">destination</span><span class="o">`</span> <span class="nb">varchar</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="k">PRIMARY</span> <span class="k">KEY</span> <span class="p">(</span><span class="o">`</span><span class="n">id</span><span class="o">`</span><span class="p">),</span>
<span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="p">(</span><span class="n">domain_id</span><span class="p">)</span> <span class="k">REFERENCES</span> <span class="n">virtual_domains</span><span class="p">(</span><span class="n">id</span><span class="p">)</span> <span class="k">ON</span> <span class="k">DELETE</span> <span class="k">CASCADE</span>
<span class="p">)</span> <span class="n">ENGINE</span><span class="o">=</span><span class="n">InnoDB</span> <span class="k">DEFAULT</span> <span class="n">CHARSET</span><span class="o">=</span><span class="n">utf8</span><span class="p">;</span>
</pre></div>
<h3>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>
<div class="highlight"><pre><span></span><span class="k">INSERT</span> <span class="k">INTO</span> <span class="n">virtual_domains</span> <span class="p">(</span><span class="o">`</span><span class="n">name</span><span class="o">`</span><span class="p">)</span> <span class="k">VALUES</span> <span class="p">(</span><span class="s1">'captainark.net'</span><span class="p">);</span>
</pre></div>
<p>We can now create users associated with this domain in the virtual_users table :</p>
<div class="highlight"><pre><span></span><span class="k">INSERT</span> <span class="k">INTO</span> <span class="n">virtual_users</span> <span class="p">(</span><span class="o">`</span><span class="n">domain_id</span><span class="o">`</span><span class="p">,</span> <span class="o">`</span><span class="n">password</span><span class="o">`</span> <span class="p">,</span> <span class="o">`</span><span class="n">email</span><span class="o">`</span><span class="p">)</span> <span class="k">VALUES</span>
<span class="p">(</span><span class="s1">'1'</span><span class="p">,</span> <span class="n">ENCRYPT</span><span class="p">(</span><span class="s1">'notanactualpassword'</span><span class="p">,</span> <span class="n">CONCAT</span><span class="p">(</span><span class="s1">'$6$'</span><span class="p">,</span> <span class="k">SUBSTRING</span><span class="p">(</span><span class="n">SHA</span><span class="p">(</span><span class="n">RAND</span><span class="p">()),</span> <span class="o">-</span><span class="mi">16</span><span class="p">))),</span>
<span class="s1">'example@captainark.net'</span><span class="p">);</span>
</pre></div>
<p>This is not mandatory, but we can also create our first mail alias :</p>
<div class="highlight"><pre><span></span><span class="k">INSERT</span> <span class="k">INTO</span> <span class="n">virtual_aliases</span> <span class="p">(</span><span class="o">`</span><span class="n">domain_id</span><span class="o">`</span><span class="p">,</span> <span class="o">`</span><span class="k">source</span><span class="o">`</span><span class="p">,</span> <span class="o">`</span><span class="n">destination</span><span class="o">`</span><span class="p">)</span> <span class="k">VALUES</span>
<span class="p">(</span><span class="s1">'1'</span><span class="p">,</span> <span class="s1">'alias@captainark.net'</span><span class="p">,</span> <span class="s1">'example@captainark.net'</span><span class="p">);</span>
</pre></div>
<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>Configuring Postfix</h2>
<p>Next, we are going to configure <a href="http://www.postfix.org/">Postfix</a>.</p>
<h3>Configuration backup</h3>
<p>First, let's backup the original configuration files :</p>
<div class="highlight"><pre><span></span>cp /etc/postfix/main.cf /etc/postfix/main.cf.orig
cp /etc/postfix/master.cf /etc/postfix/master.cf.orig
</pre></div>
<h3>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>
<div class="highlight"><pre><span></span>groupadd -g <span class="m">5000</span> vmail
useradd -g vmail -u <span class="m">5000</span> vmail -d /var/mail -m -s /bin/false
</pre></div>
<h3>SSL certificates</h3>
<p>Next, we are going to create the folder where we will store the SSL certificates :</p>
<div class="highlight"><pre><span></span>mkdir /etc/postfix/ssl
chown root: /etc/postfix/ssl <span class="o">&&</span> chmod <span class="m">600</span> /etc/postfix/ssl
</pre></div>
<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>
<div class="highlight"><pre><span></span><span class="nb">cd</span> /etc/postfix/ssl
openssl rsa -in server-with-passphrase.key -out server.key
</pre></div>
<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>
<div class="highlight"><pre><span></span>wget -O /etc/postfix/ssl/sub.class1.server.ca.pem <span class="se">\</span>
http://www.startssl.com/certs/sub.class1.server.ca.pem
</pre></div>
<p>We now have to make sure that the permissions on those files are correct :</p>
<div class="highlight"><pre><span></span>chown root: /etc/postfix/ssl/* <span class="o">&&</span> chmod <span class="m">600</span> /etc/postfix/ssl/*
</pre></div>
<p>The last thing we have to do here is to generate Diffie-Hellman keys for Perfect Forward Secrecy (PFS) :</p>
<div class="highlight"><pre><span></span>openssl gendh -out /etc/postfix/dh_512.pem -2 512
openssl gendh -out /etc/postfix/dh_1024.pem -2 1024
</pre></div>
<h3>Postifx configuration</h3>
<p>First, let's edit the /etc/postfix/main.cf file. It should end up looking something like that :</p>
<div class="highlight"><pre><span></span>smtpd_banner = <span class="nv">$myhostname</span> ESMTP <span class="nv">$mail_name</span> (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:<span class="cp">${</span><span class="n">data_directory</span><span class="cp">}</span>/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 = <span class="nv">$smtpd_tls_CAfile</span>
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:<span class="cp">${</span><span class="n">data_directory</span><span class="cp">}</span>/smtp_scache
smtpd_milters =
non_smtpd_milters = <span class="nv">$smtpd_milters</span>
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 = <span class="nv">$myhostname</span>
smtpd_sasl_security_options = noanonymous
smtpd_sasl_tls_security_options = <span class="nv">$smtpd_sasl_security_options</span>
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
</pre></div>
<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>
<div class="highlight"><pre><span></span>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
</pre></div>
<p>You also have to add the following lines at the end of the file :</p>
<div class="highlight"><pre><span></span>dovecot unix - n n - - pipe
flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f <span class="cp">${</span><span class="n">sender</span><span class="cp">}</span> -d <span class="cp">${</span><span class="n">recipient</span><span class="cp">}</span>
</pre></div>
<h3>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>
<div class="highlight"><pre><span></span>user = mail
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT 1 FROM virtual_domains WHERE name='%s'
</pre></div>
<p>/etc/postfix/mysql-virtual-mailbox-maps.cf should contain the following lines :</p>
<div class="highlight"><pre><span></span>user = mail
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT 1 FROM virtual_users WHERE email='%s'
</pre></div>
<p>/etc/postfix/mysql-virtual-alias-maps.cf should contain the following lines :</p>
<div class="highlight"><pre><span></span>user = mail
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT destination FROM virtual_aliases WHERE source='%s'
</pre></div>
<p>Since these files contain a password, let's make sure they are not world-readable :</p>
<div class="highlight"><pre><span></span>chown root: /etc/postfix/mysql* <span class="o">&&</span> chmod <span class="m">600</span> /etc/postfix/mysql*
</pre></div>
<p>You can use the command postmap to confirm that everything is working properly :</p>
<div class="highlight"><pre><span></span>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
</pre></div>
<p>Let's restart postfix for our modifications to be taken into account :</p>
<div class="highlight"><pre><span></span>systemctl restart postfix
</pre></div>
<p>That's it for Postfix, for now ; Dovecot is next !</p>
<h2>Configuring Dovecot</h2>
<h3>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>
<div class="highlight"><pre><span></span>mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.orig
</pre></div>
<p>Next, we are going to create a new /etc/dovecot/dovecot.conf file. It should contain the following lines :</p>
<div class="highlight"><pre><span></span><span class="sx">!include_try /usr/share/dovecot/protocols.d/*.protocol</span>
<span class="n">protocols</span> <span class="p">=</span> <span class="n">imap</span> <span class="n">lmtp</span> <span class="n">sieve</span>
<span class="n">mail_location</span> <span class="p">=</span> <span class="n">maildir</span><span class="p">:</span><span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">mail</span><span class="o">/</span><span class="c">%d/%n</span>
<span class="n">mail_privileged_group</span> <span class="p">=</span> <span class="n">vmail</span>
<span class="n">mail_plugin_dir</span> <span class="p">=</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">dovecot</span><span class="o">/</span><span class="n">modules</span>
<span class="n">mail_plugins</span> <span class="p">=</span>
<span class="n">disable_plaintext_auth</span> <span class="p">=</span> <span class="n">yes</span>
<span class="n">auth_mechanisms</span> <span class="p">=</span> <span class="n">plain</span> <span class="n">login</span>
<span class="n">service</span> <span class="n">director</span> <span class="p">{</span>
<span class="n">unix_listener</span> <span class="n">login</span><span class="o">/</span><span class="n">director</span> <span class="p">{</span>
<span class="p">}</span>
<span class="n">fifo_listener</span> <span class="n">login</span><span class="o">/</span><span class="n">proxy</span><span class="o">-</span><span class="n">notify</span> <span class="p">{</span>
<span class="p">}</span>
<span class="n">unix_listener</span> <span class="n">director</span><span class="o">-</span><span class="n">userdb</span> <span class="p">{</span>
<span class="p">}</span>
<span class="n">inet_listener</span> <span class="p">{</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">namespace</span> <span class="n">inbox</span> <span class="p">{</span>
<span class="n">inbox</span> <span class="p">=</span> <span class="n">yes</span>
<span class="n">type</span> <span class="p">=</span> <span class="n">private</span>
<span class="n">mailbox</span> <span class="n">Drafts</span> <span class="p">{</span>
<span class="n">auto</span> <span class="p">=</span> <span class="n">subscribe</span>
<span class="n">special_use</span> <span class="p">=</span> <span class="o">\</span><span class="n">Drafts</span>
<span class="p">}</span>
<span class="n">mailbox</span> <span class="n">Junk</span> <span class="p">{</span>
<span class="n">auto</span> <span class="p">=</span> <span class="n">subscribe</span>
<span class="n">special_use</span> <span class="p">=</span> <span class="o">\</span><span class="n">Junk</span>
<span class="p">}</span>
<span class="n">mailbox</span> <span class="n">Sent</span> <span class="p">{</span>
<span class="n">auto</span> <span class="p">=</span> <span class="n">subscribe</span>
<span class="n">special_use</span> <span class="p">=</span> <span class="o">\</span><span class="n">Sent</span>
<span class="p">}</span>
<span class="n">mailbox</span> <span class="n">Trash</span> <span class="p">{</span>
<span class="n">auto</span> <span class="p">=</span> <span class="n">subscribe</span>
<span class="n">special_use</span> <span class="p">=</span> <span class="o">\</span><span class="n">Trash</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">service</span> <span class="n">imap</span><span class="o">-</span><span class="n">login</span> <span class="p">{</span>
<span class="n">inet_listener</span> <span class="n">imap</span> <span class="p">{</span>
<span class="n">port</span> <span class="p">=</span> <span class="mi">0</span>
<span class="p">}</span>
<span class="n">inet_listener</span> <span class="n">imaps</span> <span class="p">{</span>
<span class="n">port</span> <span class="p">=</span> <span class="mi">993</span>
<span class="n">ssl</span> <span class="p">=</span> <span class="n">yes</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">service</span> <span class="n">pop3</span><span class="o">-</span><span class="n">login</span> <span class="p">{</span>
<span class="n">inet_listener</span> <span class="n">pop3</span> <span class="p">{</span>
<span class="n">port</span> <span class="p">=</span> <span class="mi">0</span>
<span class="p">}</span>
<span class="n">inet_listener</span> <span class="n">pop3s</span> <span class="p">{</span>
<span class="n">port</span> <span class="p">=</span> <span class="mi">0</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">service</span> <span class="n">lmtp</span> <span class="p">{</span>
<span class="n">unix_listener</span> <span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">spool</span><span class="o">/</span><span class="n">postfix</span><span class="o">/</span><span class="n">private</span><span class="o">/</span><span class="n">dovecot</span><span class="o">-</span><span class="n">lmtp</span> <span class="p">{</span>
<span class="n">mode</span> <span class="p">=</span> <span class="mi">0600</span>
<span class="n">user</span> <span class="p">=</span> <span class="n">postfix</span>
<span class="n">group</span> <span class="p">=</span> <span class="n">postfix</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">service</span> <span class="n">imap</span> <span class="p">{</span>
<span class="p">}</span>
<span class="n">service</span> <span class="n">pop3</span> <span class="p">{</span>
<span class="p">}</span>
<span class="n">service</span> <span class="n">auth</span> <span class="p">{</span>
<span class="n">unix_listener</span> <span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">spool</span><span class="o">/</span><span class="n">postfix</span><span class="o">/</span><span class="n">private</span><span class="o">/</span><span class="n">auth</span> <span class="p">{</span>
<span class="n">mode</span> <span class="p">=</span> <span class="mi">0666</span>
<span class="n">user</span> <span class="p">=</span> <span class="n">postfix</span>
<span class="n">group</span> <span class="p">=</span> <span class="n">postfix</span>
<span class="p">}</span>
<span class="n">unix_listener</span> <span class="n">auth</span><span class="o">-</span><span class="n">userdb</span> <span class="p">{</span>
<span class="n">mode</span> <span class="p">=</span> <span class="mi">0600</span>
<span class="n">user</span> <span class="p">=</span> <span class="n">vmail</span>
<span class="p">}</span>
<span class="n">user</span> <span class="p">=</span> <span class="n">dovecot</span>
<span class="p">}</span>
<span class="n">service</span> <span class="n">auth</span><span class="o">-</span><span class="n">worker</span> <span class="p">{</span>
<span class="n">user</span> <span class="p">=</span> <span class="n">vmail</span>
<span class="p">}</span>
<span class="n">service</span> <span class="n">dict</span> <span class="p">{</span>
<span class="n">unix_listener</span> <span class="n">dict</span> <span class="p">{</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">ssl</span> <span class="p">=</span> <span class="n">required</span>
<span class="n">ssl_cert</span> <span class="p">=</span> <span class="o"></</span><span class="n">etc</span><span class="o">/</span><span class="n">postfix</span><span class="o">/</span><span class="n">ssl</span><span class="o">/</span><span class="n">server</span><span class="p">.</span><span class="n">crt</span>
<span class="n">ssl_key</span> <span class="p">=</span> <span class="o"></</span><span class="n">etc</span><span class="o">/</span><span class="n">postfix</span><span class="o">/</span><span class="n">ssl</span><span class="o">/</span><span class="n">server</span><span class="p">.</span><span class="n">key</span>
<span class="n">ssl_ca</span> <span class="p">=</span> <span class="o"></</span><span class="n">etc</span><span class="o">/</span><span class="n">postfix</span><span class="o">/</span><span class="n">ssl</span><span class="o">/</span><span class="n">sub</span><span class="p">.</span><span class="n">class1</span><span class="p">.</span><span class="n">server</span><span class="p">.</span><span class="n">ca</span><span class="p">.</span><span class="n">pem</span>
<span class="n">ssl_protocols</span> <span class="p">=</span> !<span class="n">SSLv2</span> !<span class="n">SSLv3</span>
<span class="n">ssl_cipher_list</span> <span class="p">=</span> <span class="n">AES128</span><span class="o">+</span><span class="n">EECDH</span><span class="p">:</span><span class="n">AES128</span><span class="o">+</span><span class="n">EDH</span><span class="p">:</span>!<span class="n">aNULL</span><span class="p">;</span>
<span class="n">protocol</span> <span class="n">lda</span> <span class="p">{</span>
<span class="n">mail_plugins</span> <span class="p">=</span> $<span class="n">mail_plugins</span> <span class="n">sieve</span>
<span class="p">}</span>
<span class="n">protocol</span> <span class="n">imap</span> <span class="p">{</span>
<span class="n">mail_plugins</span> <span class="p">=</span> $<span class="n">mail_plugins</span>
<span class="p">}</span>
<span class="n">protocol</span> <span class="n">lmtp</span> <span class="p">{</span>
<span class="n">mail_plugins</span> <span class="p">=</span> $<span class="n">mail_plugins</span> <span class="n">sieve</span>
<span class="p">}</span>
<span class="n">plugin</span> <span class="p">{</span>
<span class="n">sieve</span> <span class="p">=</span> <span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">mail</span><span class="o">/</span><span class="n">sieve</span><span class="o">/</span><span class="n">users</span><span class="o">/</span><span class="c">%u.sieve</span>
<span class="n">sieve_after</span> <span class="p">=</span> <span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">mail</span><span class="o">/</span><span class="n">sieve</span><span class="o">/</span><span class="n">after</span>
<span class="n">sieve_before</span> <span class="p">=</span> <span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">mail</span><span class="o">/</span><span class="n">sieve</span><span class="o">/</span><span class="n">before</span>
<span class="n">sieve_global_dir</span> <span class="p">=</span> <span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">dovecot</span><span class="o">/</span><span class="n">sieve</span><span class="o">/</span>
<span class="n">sieve_dir</span> <span class="p">=</span> <span class="o">~/</span><span class="n">sieve</span>
<span class="p">}</span>
<span class="n">passdb</span> <span class="p">{</span>
<span class="n">driver</span> <span class="p">=</span> <span class="n">sql</span>
<span class="n">args</span> <span class="p">=</span> <span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">dovecot</span><span class="o">/</span><span class="n">sql</span><span class="p">.</span><span class="n">conf</span>
<span class="p">}</span>
<span class="n">userdb</span> <span class="p">{</span>
<span class="n">driver</span> <span class="p">=</span> <span class="n">static</span>
<span class="n">args</span> <span class="p">=</span> <span class="n">uid</span><span class="p">=</span><span class="n">vmail</span> <span class="n">gid</span><span class="p">=</span><span class="n">vmail</span> <span class="n">home</span><span class="p">=</span><span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">mail</span><span class="o">/</span><span class="c">%d/%n</span>
<span class="p">}</span>
</pre></div>
<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>
<div class="highlight"><pre><span></span>mkdir -p /var/mail/captainark.net
chown vmail: /var/mail/captainark.net <span class="o">&&</span> chmod <span class="m">770</span> /var/mail/captainark.net
</pre></div>
<p>Dovecot will create the virtual users' folders automatically.</p>
<h3>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>
<div class="highlight"><pre><span></span>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';
</pre></div>
<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>
<div class="highlight"><pre><span></span>chown root: /etc/dovecot/sql.conf <span class="o">&&</span> chmod <span class="m">600</span> /etc/dovecot/sql.conf
</pre></div>
<h3>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>
<div class="highlight"><pre><span></span>mkdir -p /var/mail/sieve/before
mkdir /var/mail/sieve/after
mkdir /var/mail/sieve/users
chown -R vmail: /var/mail/sieve <span class="o">&&</span> chmod -R <span class="m">770</span> /var/mail/sieve
</pre></div>
<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>
<div class="highlight"><pre><span></span>require ["envelope", "fileinto", "imap4flags", "regex"];
if not header :regex "message-id" ".*@.*\." {
fileinto "Junk";
}
if header :contains "X-Spam-Level" "*****" {
fileinto "Junk";
}
</pre></div>
<p>Last thing we have to do is to change the permissions on the newly created file :</p>
<div class="highlight"><pre><span></span>chown vmail: /var/mail/sieve/before/filter.sieve <span class="o">&&</span> <span class="se">\</span>
chmod <span class="m">660</span> /var/mail/sieve/before/filter.sieve
</pre></div>
<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>
<div class="highlight"><pre><span></span>systemctl restart dovecot
</pre></div>
<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>Configuring SpamAssassin</h2>
<h3>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>The actual configuration</h3>
<p>SpamAssassin's configuration is pretty straightforward. First, let's edit the /etc/default/spamassassin file :</p>
<div class="highlight"><pre><span></span>ENABLED=1
[...]
CRON=1
</pre></div>
<p>Before the cron runs for the first time, we have to manually update SpamAssassin's ruleset :</p>
<div class="highlight"><pre><span></span>sa-learn
</pre></div>
<p>Next, as usual, let's back up the original configuration file :</p>
<div class="highlight"><pre><span></span>mv /etc/spamassassin/local.cf /etc/spamassassin/local.cf.orig
</pre></div>
<p>Let's create a new /etc/spamassassin/local.cf file with the following content :</p>
<div class="highlight"><pre><span></span>rewrite_header Subject [SPAM]
report_safe 0
required_score 5.0
use_bayes 1
bayes_auto_learn 1
whitelist_from *@captainark.net
</pre></div>
<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>
<div class="highlight"><pre><span></span>smtp inet n - - - - smtpd
-o content_filter=spamassassin
</pre></div>
<p>At the very end of the same file, we have to add the following lines :</p>
<div class="highlight"><pre><span></span>spamassassin unix - n n - - pipe
user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f <span class="cp">${</span><span class="n">sender</span><span class="cp">}</span> <span class="cp">${</span><span class="n">recipient</span><span class="cp">}</span>
</pre></div>
<p>Let's restart SpamAssassin and Postfix :</p>
<div class="highlight"><pre><span></span>systemctl restart postfix
systemctl restart spamassassin
</pre></div>
<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>
<div class="highlight"><pre><span></span>X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
myserver.captainark.net
X-Spam-Level:
</pre></div>
<h2>Configuring SPF</h2>
<h3>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>
<div class="highlight"><pre><span></span>captainark.net IN TXT "v=spf1 mx ~all"
</pre></div>
<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>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>
<div class="highlight"><pre><span></span>Header_Type = AR
Authserv_Id = "<server's FQDN>"
</pre></div>
<p>Then, let's edit the /etc/postfix/master.cf file and add the following lines at the end :</p>
<div class="highlight"><pre><span></span>policy-spf unix - n n - - spawn
user=nobody argv=/usr/bin/policyd-spf
</pre></div>
<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>
<div class="highlight"><pre><span></span>smtpd_recipient_restrictions =
[...]
reject_unauth_destination,
check_policy_service unix:private/policy-spf,
permit
</pre></div>
<p>We now have to restart postfix :</p>
<div class="highlight"><pre><span></span>systemctl restart postfix
</pre></div>
<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>
<div class="highlight"><pre><span></span><span class="nt">Authentication-Results</span><span class="o">:</span> <span class="nt">myserver</span><span class="nc">.captainark.net</span><span class="o">;</span> <span class="nt">spf</span><span class="o">=</span><span class="nt">pass</span> <span class="o">(</span><span class="nt">sender</span> <span class="nt">SPF</span> <span class="nt">authorized</span><span class="o">)</span>
<span class="cp">[</span><span class="nx">...</span><span class="cp">]</span> <span class="nt">receiver</span><span class="o">=</span><span class="nt">example</span><span class="k">@captainark</span><span class="nc">.net</span><span class="o">)</span>
</pre></div>
<h2>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>Software side</h3>
<p>First, let's backup the original configuration file and create a folder for the configuration files :</p>
<div class="highlight"><pre><span></span>mv /etc/opendkim.conf /etc/opendkim.conf.orig
mkdir /etc/opendkim.d
</pre></div>
<p>We now have to create a /etc/opendkim.conf file with the following content :</p>
<div class="highlight"><pre><span></span>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
</pre></div>
<p>Let's then create the necessary folders :</p>
<div class="highlight"><pre><span></span>mkdir -p /etc/opendkim.d/keys/captainark.net/
</pre></div>
<p>Now, we are going to create the /etc/opendkim.d/TrustedHosts file with the following content :</p>
<div class="highlight"><pre><span></span>localhost
127.0.0.1
::1
captainark.net
</pre></div>
<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>
<div class="highlight"><pre><span></span>mail._domainkey.captainark.net captainark.net:mail:/etc/opendkim.d/keys/captainark.net/mail.private
</pre></div>
<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>
<div class="highlight"><pre><span></span>*@captainark.net mail._domainkey.captainark.net
</pre></div>
<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>
<div class="highlight"><pre><span></span><span class="nb">cd</span> /etc/opendkim.d/keys/captainark.net/
opendkim-genkey -s mail -d captainark.net
</pre></div>
<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>
<div class="highlight"><pre><span></span>chown -R opendkim: /etc/opendkim.d/keys
chmod -R <span class="m">700</span> /etc/opendkim.d/keys
chmod <span class="m">600</span> /etc/opendkim.d/captainark.net/*
</pre></div>
<h3>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>
<div class="highlight"><pre><span></span>mkdir /var/spool/postfix/opendkim
chown opendkim: /var/spool/postfix/opendkim
</pre></div>
<p>We also have to add the postfix user to the opendkim group :</p>
<div class="highlight"><pre><span></span>useradd -G opendkim postfix
</pre></div>
<p>Now, let's edit the /etc/postfix/master.cf file, like so :</p>
<div class="highlight"><pre><span></span>smtpd_milters = unix:/opendkim/opendkim.sock
</pre></div>
<p>We now have to restart OpenDKIM and Postfix :</p>
<div class="highlight"><pre><span></span>systemctl restart opendkim
systemctl restart postfix
</pre></div>
<h3>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>
<div class="highlight"><pre><span></span>mail._domainkey IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkJq0CW3tl2XHZ1CN5XdbqRDU7KfXOJ70nlwI09bHmDU63/Yz3J5rl863S0t2ncVHfIudZANj0OaiJe5HRR7WCsjuNIhQFfPFGIWLNClpxqdQVQURI38sAGeyn7Ed/Cor1AiWABzFWzel0kvXILw8K/NTzxaAPeSa9ttwQEgSmowIDAQAB" ; ----- DKIM key mail for captainark.net
</pre></div>
<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>
<div class="highlight"><pre><span></span>==========================================================
Summary of Results
==========================================================
SPF check: pass
DomainKeys check: neutral
DKIM check: pass
Sender-ID check: pass
SpamAssassin check: ham
</pre></div>
<h2>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>Software side</h3>
<p>Once again, let's backup the original configuration file :</p>
<div class="highlight"><pre><span></span>mv /etc/opendmarc.conf /etc/opendmarc.conf.orig
</pre></div>
<p>We now have to create a /etc/opendmarc.conf file with the following content :</p>
<div class="highlight"><pre><span></span>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
</pre></div>
<h3>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>
<div class="highlight"><pre><span></span>mkdir /var/spool/postfix/opendmarc
chown opendmarc: /var/spool/postfix/opendmarc
</pre></div>
<p>We also have to add the postfix user to the opendmarc group :</p>
<div class="highlight"><pre><span></span>useradd -G opendmarc postfix
</pre></div>
<p>Now, let's edit the /etc/postfix/master.cf file, like so :</p>
<div class="highlight"><pre><span></span>smtpd_milters = unix:/opendkim/opendkim.sock, unix:/opendmarc/opendmarc.sock
</pre></div>
<p>We now have to restart OpenDMARC and Postfix :</p>
<div class="highlight"><pre><span></span>systemctl restart opendmarc
systemctl restart postfix
</pre></div>
<p>You should now see the following headers in your incoming emails :</p>
<div class="highlight"><pre><span></span><span class="nt">Authentication-Results</span><span class="o">:</span> <span class="nt">myserver</span><span class="nc">.captainark.net</span><span class="o">;</span> <span class="nt">dmarc</span><span class="o">=</span><span class="nt">pass</span> <span class="nt">header</span><span class="nc">.from</span><span class="o">=</span><span class="nt">gmail</span><span class="nc">.com</span>
</pre></div>
<h3>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>
<div class="highlight"><pre><span></span><span class="nt">_dmarc</span> <span class="nt">IN</span> <span class="nt">TXT</span> <span class="s2">"v=DMARC1; p=none; rua=mailto:postmaster@captainark.net; ruf=mailto:postmaster@captainark.net"</span>
</pre></div>
<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>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>
<div class="highlight"><pre><span></span>mv /etc/monit/monitrc /etc/monit/monitrc.orig
</pre></div>
<p>We now have to create a new /etc/monit/monitrc file with the following content :</p>
<div class="highlight"><pre><span></span>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/*
</pre></div>
<p>Then, we are going to create a /etc/monit/conf.d/mail file with the following content :</p>
<div class="highlight"><pre><span></span>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
</pre></div>
<p>Let's make sure that permissions on the file are correct :</p>
<div class="highlight"><pre><span></span>chown root: /etc/monit/conf.d/mail <span class="o">&&</span> chmod <span class="m">600</span> /etc/monit/conf.d/mail
</pre></div>
<p>Then, we have to reload the monit daemon :</p>
<div class="highlight"><pre><span></span>monit reload
</pre></div>
<p>Now, the <code>monit summary</code> command should have the following output :</p>
<div class="highlight"><pre><span></span>The Monit daemon 5.4 uptime: 3d 0h 41m
Process 'postfix' Running
Process 'dovecot' Running
Process 'spamassassin' Running
Process 'opendkim' Running
Process 'opendmarc' Running
</pre></div>
<h2>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>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>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>