2016-01-31 16:11:44 +01:00
<?xml version="1.0" encoding="utf-8"?>
2016-05-22 15:22:00 +02:00
<feed xmlns= "http://www.w3.org/2005/Atom" > <title > Sysadmining. All day. Every day.</title> <link href= "https://captainark.net/" rel= "alternate" > </link> <link href= "https://captainark.net/rss.xml" rel= "self" > </link> <id > https://captainark.net/</id> <updated > 2016-03-26T00:00:00+01:00</updated> <entry > <title > WebDAV with nginx</title> <link href= "https://captainark.net/webdav-with-nginx.html" rel= "alternate" > </link> <published > 2016-03-26T00:00:00+01:00</published> <author > <name > Antoine Joubert</name> </author> <id > tag:captainark.net,2016-03-26:webdav-with-nginx.html</id> <summary type= "html" > < p> This website has been hosted on an < a href="https://www.online.net"> Online.net< /a> dedicated server since its creation. I've been one of their customers for the past 3 years now, and I still don't have anything bad to say about them.< /p>
2016-03-26 15:36:09 +01:00
< p> They recently upgraded their personnal range, and I took the opportunity to upgrade from a single server running all of my services to 2 servers running LXC containers that are hosting my services.< /p>
< p> It took me 2 days to migrate everything, but it was worth it. If I decide to switch servers again, I'll have to migrate the containers instead of the services themselves. Considering they are stored on a separate BTRFS volume, it shouldn't take me more than a few hours at most.< /p>
< p> During the migration, I realized that I needed to make files that were hosted on one server accessible to the other. I could have gone with CIFS or NFS, but I wanted to have encryption built-in instead of having to rely on a VPN for that. Since I figured it was a good opportunity to learn something new, I ended up going with WebDAV.< /p>
< p> In this tutorial, I'll explain how I've configured a read-only WebDAV share using < a href="https://www.nginx.com/"> nginx< /a> and < a href="https://letsencrypt.org/"> Let'sEncrypt< /a> SSL certificates between two Debian Jessie containers.< /p>
< h2> Server 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 -t jessie-backports install nginx letsencrypt
apt install apache2-utils
< /pre> < /div>
< h3> Getting our first certificate from letsencrypt< /h3>
< h4> letsencrypt configuration< /h4>
< p> Let's create a configuration file for letsencrypt :< /p>
< div class="highlight"> < pre> < span> < /span> mkdir /etc/letsencrypt
< span class="nb"> echo< /span> < span class="s1"> & #39;rsa-key-size = 3072< /span>
< span class="s1"> renew-by-default< /span>
< span class="s1"> text = True< /span>
< span class="s1"> agree-tos = True< /span>
< span class="s1"> renew-by-default = True< /span>
< span class="s1"> authenticator = webroot< /span>
< span class="s1"> email = admin@example.com< /span>
< span class="s1"> webroot-path = /var/www/letsencrypt/& #39;< /span> & gt; /etc/letsencrypt/cli.ini
< /pre> < /div>
< p> < em> Please do modify admin@example.com by your actual e-mail address.< /em> < /p>
< p> We also need to create the directory structure where letsencrypt ACME challenge temporary files will be stored :< /p>
< div class="highlight"> < pre> < span> < /span> mkdir -p /var/www/letsencrypt/.well-known
< /pre> < /div>
< h4> nginx configuration< /h4>
< p> We now need to configure nginx by adding the following in the < code> /etc/nginx/sites-available/default< /code> file, anywhere in the < code> server{}< /code> block that is configured to listen on port 80.< /p>
< div class="highlight"> < pre> < span> < /span> location /.well-known/acme-challenge {
root /var/www/letsencrypt;
}
< /pre> < /div>
< p> Let's make sure that we haven't done anything wrong :< /p>
< div class="highlight"> < pre> < span> < /span> nginx -t
< /pre> < /div>
< p> The command should give you the following output :< /p>
< div class="highlight"> < pre> < span> < /span> < span class="n"> nginx< /span> < span class="o"> :< /span> < span class="n"> the< /span> < span class="n"> configuration< /span> < span class="n"> file< /span> < span class="sr"> /etc/nginx/< /span> < span class="n"> nginx< /span> < span class="o"> .< /span> < span class="na"> conf< /span> < span class="n"> syntax< /span> < span class="k"> is< /span> < span class="n"> ok< /span>
< span class="n"> nginx< /span> < span class="o"> :< /span> < span class="n"> configuration< /span> < span class="n"> file< /span> < span class="sr"> /etc/nginx/< /span> < span class="n"> nginx< /span> < span class="o"> .< /span> < span class="na"> conf< /span> < span class="n"> test< /span> < span class="k"> is< /span> < span class="n"> successful< /span>
< /pre> < /div>
< p> If that's the case, you can safely reload the nginx daemon :< /p>
< div class="highlight"> < pre> < span> < /span> nginx -s reload
< /pre> < /div>
< h4> Certificate request< /h4>
< p> Now that letsencrypt and nginx are properly configured, we can request our certificate from letsencrypt :< /p>
< div class="highlight"> < pre> < span> < /span> letsencrypt --config /etc/letsencrypt/cli.ini certonly -w /var/www/letsencrypt -d www.example.com
< /pre> < /div>
< p> < em> Please do modify www.example.com by your server's FQDN, and please note that the letsencrypt servers need to be able to resolve that name to your server's IP.< /em> < /p>
< p> If everything goes well, your certificates will be generated and stored in the /etc/letsencrypt folder.< /p>
< h3> WebDAV configuration< /h3>
< p> Now that we've obtained our certificate from letsencrypt, we can begin configuring nginx.< /p>
< p> First, we need to comment two SSL directives from the default nginx configuration :< /p>
< div class="highlight"> < pre> < span> < /span> sed -i & #39;/ssl_/ s/^/#/& #39; /etc/nginx/nginx.conf
< /pre> < /div>
< p> Let's now create a < code> /etc/nginx/conf.d/ssl.conf< /code> with the following content :< /p>
< div class="highlight"> < pre> < span> < /span> < span class="nt"> ssl_session_timeout< /span> < span class="nt"> 1d< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_session_cache< /span> < span class="nt"> shared< /span> < span class="nd"> :SSL:50m< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_session_tickets< /span> < span class="nt"> off< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_certificate< /span> < span class="o"> /< /span> < span class="nt"> etc< /span> < span class="o"> /< /span> < span class="nt"> letsencrypt< /span> < span class="o"> /< /span> < span class="nt"> live< /span> < span class="o"> /< /span> < span class="nt"> www< /span> < span class="nc"> .example.com< /span> < span class="o"> /< /span> < span class="nt"> fullchain< /span> < span class="nc"> .pem< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_certificate_key< /span> < span class="o"> /< /span> < span class="nt"> etc< /span> < span class="o"> /< /span> < span class="nt"> letsencrypt< /span> < span class="o"> /< /span> < span class="nt"> live< /span> < span class="o"> /< /span> < span class="nt"> www< /span> < span class="nc"> .example.com< /span> < span class="o"> /< /span> < span class="nt"> privkey< /span> < span class="nc"> .pem< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_trusted_certificate< /span> < span class="o"> /< /span> < span class="nt"> etc< /span> < span class="o"> /< /span> < span class="nt"> letsencrypt< /span> < span class="o"> /< /span> < span class="nt"> live< /span> < span class="o"> /< /span> < span class="nt"> www< /span> < span class="nc"> .example.com< /span> < span class="o"> /< /span> < span class="nt"> fullchain< /span> < span class="nc"> .pem< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_dhparam< /span> < span class="o"> /< /span> < span class="nt"> etc< /span> < span class="o"> /< /span> < span class="nt"> nginx< /span> < span class="o"> /< /span> < span class="nt"> ssl< /span> < span class="o"> /< /span> < span class="nt"> dhparam< /span> < span class="nc"> .pem< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_protocols< /span> < span class="nt"> TLSv1< /span> < span class="nt"> TLSv1< /span> < span class="nc"> .1< /span> < span class="nt"> TLSv1< /span> < span class="nc"> .2< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_ciphers< /span> < span class="s1"> & #39;ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS& #39;< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_prefer_server_ciphers< /span> < span class="nt"> on< /span> < span class="o"> ;< /span>
< span class="nt"> add_header< /span> < span class="nt"> Strict-Transport-Security< /span> < span class="nt"> max-age< /span> < span class="o"> =< /span> < span class="nt"> 15768000< /span> < span class="o"> ;< /span>
< span class="nt"> add_header< /span> < span class="nt"> X-Frame-Options< /span> < span class="nt"> DENY< /span> < span class="o"> ;< /span>
< span class="nt"> add_header< /span> < span class="nt"> X-Content-Type-Options< /span> < span class="nt"> nosniff< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_stapling< /span> < span class="nt"> on< /span> < span class="o"> ;< /span>
< span class="nt"> ssl_stapling_verify< /span> < span class="nt"> on< /span> < span class="o"> ;< /span>
< span class="nt"> resolver< /span> < span class="nt"> 127< /span> < span class="nc"> .0.0.1< /span> < span class="nt"> valid< /span> < span class="o"> =< /span> < span class="nt"> 300s< /span> < span class="o"> ;< /span>
< span class="nt"> resolver_timeout< /span> < span class="nt"> 5s< /span> < span class="o"> ;< /span>
< /pre> < /div>
< p> < em> This configuration will work if you're using a single certificate on your server. If not, you'll have to remove the < code> ssl_certificate< /code> , < code> ssl_certificate_key< /code> and < code> ssl_trusted_certificate< /code> directives from this file and move them to the correct < code> server{}< /code> block.< /em> < /p>
< p> We now need to generate a < code> dhparam.pem< /code> file :< /p>
< div class="highlight"> < pre> < span> < /span> mkdir /etc/nginx/ssl < span class="o"> & amp;& amp;< /span> chmod < span class="m"> 700< /span> /etc/nginx/ssl
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 3072
chmod < span class="m"> 600< /span> /etc/nginx/ssl/dhparam.pem
< /pre> < /div>
< p> Let's now generate a HTTP basic authentication file. This example creates a user named example :< /p>
< div class="highlight"> < pre> < span> < /span> mkdir /etc/nginx/auth
htpasswd -c /etc/nginx/auth/webdav example
New password:
Re-type new password:
Adding password for user user
< /pre> < /div>
< p> This file has to be readable by the user running your webserver. For security reasons, we'll make it readable only by him :< /p>
< div class="highlight"> < pre> < span> < /span> chown -R www-data:nogroup /etc/nginx/auth
chmod 700 /etc/nginx/auth
chmod 400 /etc/nginx/auth/webdav
< /pre> < /div>
< p> Let's now modify our < code> /etc/nginx/sites-available/default< /code> file with the following content :< /p>
< div class="highlight"> < pre> < span> < /span> server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name & quot;& quot;;
return 444;
}
server {
listen 443 default_server ssl http2;
listen [::]:443 default_server ipv6only=on ssl http2;
server_name & quot;& quot;;
return 444;
}
< /pre> < /div>
< p> We now have to create a < code> /etc/nginx/sites-available/example< /code> file that will contain our actual webdav configuration. This example makes a < code> data< /code> folder stored in < code> /var/www/< /code> accessible.< /p>
< div class="highlight"> < pre> < span> < /span> < span class="nt"> server< /span> < span class="p"> {< /span>
< span class="n"> listen< /span> < span class="m"> 80< /span> < span class="p"> ;< /span>
< span class="n"> listen< /span> < span class="cp"> [< /span> < span class="p"> ::< /span> < span class="cp"> ]< /span> < span class="o"> :< /span> < span class="m"> 80< /span> < span class="p"> ;< /span>
< span class="n"> server_name< /span> < span class="n"> www< /span> < span class="o"> .< /span> < span class="n"> example< /span> < span class="o"> .< /span> < span class="n"> com< /span> < span class="p"> ;< /span>
< span class="n"> return< /span> < span class="m"> 301< /span> < span class="n"> https< /span> < span class="o"> ://< /span> < span class="err"> $< /span> < span class="n"> server_name< /span> < span class="err"> $< /span> < span class="n"> request_uri< /span> < span class="p"> ;< /span>
< span class="p"> }< /span>
< span class="nt"> server< /span> < span class="p"> {< /span>
< span class="n"> listen< /span> < span class="m"> 443< /span> < span class="n"> ssl< /span> < span class="n"> http2< /span> < span class="p"> ;< /span>
< span class="n"> listen< /span> < span class="cp"> [< /span> < span class="p"> ::< /span> < span class="cp"> ]< /span> < span class="o"> :< /span> < span class="m"> 443< /span> < span class="n"> ssl< /span> < span class="n"> http2< /span> < span class="p"> ;< /span>
< span class="n"> server_name< /span> < span class="n"> www< /span> < span class="o"> .< /span> < span class="n"> example< /span> < span class="o"> .< /span> < span class="n"> com< /span> < span class="p"> ;< /span>
< span class="n"> root< /span> < span class="o"> /< /span> < span class="n"> var< /span> < span class="o"> /< /span> < span class="n"> www< /span> < span class="p"> ;< /span>
< span class="n"> location< /span> < span class="o"> /< /span> < span class="err"> {< /span>
< span class="n"> index< /span> < span class="n"> index< /span> < span class="o"> .< /span> < span class="n"> html< /span> < span class="p"> ;< /span>
< span class="p"> }< /span>
< span class="nt"> location< /span> < span class="o"> /< /span> < span class="nc"> .well-known< /span> < span class="o"> /< /span> < span class="nt"> acme-challenge< /span> < span class="p"> {< /span>
< span class="n"> root< /span> < span class="o"> /< /span> < span class="n"> var< /span> < span class="o"> /< /span> < span class="n"> www< /span> < span class="o"> /< /span> < span class="n"> letsencrypt< /span> < span class="p"> ;< /span>
< span class="p"> }< /span>
< span class="nt"> location< /span> < span class="o"> /< /span> < span class="nt"> data< /span> < span class="p"> {< /span>
< span class="n"> client_body_temp_path< /span> < span class="o"> /< /span> < span class="n"> tmp< /span> < span class="p"> ;< /span>
< span class="n"> dav_methods< /span> < span class="n"> PUT< /span> < span class="n"> DELETE< /span> < span class="n"> MKCOL< /span> < span class="n"> COPY< /span> < span class="n"> MOVE< /span> < span class="p"> ;< /span>
< span class="n"> dav_ext_methods< /span> < span class="n"> PROPFIND< /span> < span class="n"> OPTIONS< /span> < span class="p"> ;< /span>
< span class="n"> create_full_put_path< /span> < span class="n"> on< /span> < span class="p"> ;< /span>
< span class="n"> dav_access< /span> < span class="n"> user< /span> < span class="o"> :< /span> < span class="n"> r< /span> < span class="n"> group< /span> < span class="o"> :< /span> < span class="n"> r< /span> < span class="p"> ;< /span>
< span class="n"> auth_basic< /span> < span class="s2"> & quot;Restricted access& quot;< /span> < span class="p"> ;< /span>
< span class="n"> auth_basic_user_file< /span> < span class="n"> auth< /span> < span class="o"> /< /span> < span class="n"> webdav< /span> < span class="p"> ;< /span>
< span class="n"> limit_except< /span> < span class="n"> GET< /span> < span class="err"> {< /span>
< span class="n"> allow< /span> < span class="o"> & lt;< /span> < span class="n"> YOUR< /span> < span class="n"> IP< /span> < span class="n"> HERE< /span> < span class="o"> & gt;< /span> < span class="p"> ;< /span>
< span class="n"> deny< /span> < span class="n"> all< /span> < span class="p"> ;< /span>
< span class="p"> }< /span>
< span class="err"> }< /span>
< span class="err"> }< /span>
< /pre> < /div>
< p> The last thing we have to do is to create a symlink so that nginx will load our configuration :< /p>
< div class="highlight"> < pre> < span> < /span> ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example
< /pre> < /div>
< p> Like before, let's make sure our configuration is correct and then reload the daemon :< /p>
< div class="highlight"> < pre> < span> < /span> nginx -t
nginx -s reload
< /pre> < /div>
< p> That's it for the WebDAV configuration server-side !< /p>
< h3> nginx monitoring< /h3>
< p> If you're using monit, you can easily monitor the nginx daemon by copying the following in < code> /etc/monit/conf.d/nginx< /code> :< /p>
< div class="highlight"> < pre> < span> < /span> check process nginx
with pidfile & quot;/run/nginx.pid& quot;
start program = & quot;/bin/systemctl start nginx& quot;
stop program = & quot;/bin/systemctl stop nginx& quot;
alert monit@example.com
< /pre> < /div>
< h3> Certificates auto-renewal< /h3>
< p> This goes beyond the scope of the article, but since letsencrypt certficates are only valid for 3 months, you'll need to renew them regularily. You can do so manually or you can setup a cron that does it for you.< /p>
< p> I personnaly use the following script :< /p>
< table class="highlighttable"> < tr> < td class="linenos"> < div class="linenodiv"> < pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17< /pre> < /div> < /td> < td class="code"> < div class="highlight"> < pre> < span> < /span> < span class="ch"> #!/bin/bash< /span>
< span class="nv"> PRG< /span> < span class="o"> =< /span> < span class="s2"> & quot;/usr/bin/letsencrypt& quot;< /span>
< span class="nv"> CONFIG< /span> < span class="o"> =< /span> < span class="s2"> & quot;/etc/letsencrypt/cli.ini& quot;< /span>
< span class="nv"> MAILDEST< /span> < span class="o"> =< /span> < span class="s2"> & quot;admin@example.com& quot;< /span>
< span class="nv"> GLOBAL< /span> < span class="o"> =< /span> 0
< span class="c1"> # www.example.com< /span>
< span class="nv"> $PRG< /span> --config < span class="nv"> $CONFIG< /span> certonly -w /var/www/letsencrypt -d www.example.com
< span class="o"> [[< /span> < span class="nv"> $?< /span> !< span class="o"> =< /span> < span class="m"> 0< /span> < span class="o"> ]]< /span> < span class="o"> & amp;& amp;< /span> < span class="nv"> GLOBAL< /span> < span class="o"> =< /span> < span class="k"> $((< /span> < span class="nv"> $GLOBAL< /span> < span class="o"> +< /span> < span class="m"> 1< /span> < span class="k"> ))< /span>
< span class="k"> if< /span> < span class="o"> [[< /span> < span class="nv"> $GLOBAL< /span> < span class="o"> ==< /span> < span class="m"> 0< /span> < span class="o"> ]]< /span> < span class="p"> ;< /span> < span class="k"> then< /span>
/usr/sbin/nginx -s reload
< span class="k"> else< /span>
< span class="nb"> echo< /span> < span class="s2"> & quot;Something went wrong while renewing the certificates on < /span> < span class="k"> $(< /span> hostname -f< span class="k"> )< /span> < span class="s2"> < /span>
< span class="s2"> Manual action needed.& quot;< /span> < span class="p"> |< /span> mail -s < span class="s2"> & quot;Letsencrypt error on < /span> < span class="k"> $(< /span> hostname -f< span class="k"> )< /span> < span class="s2"> & quot;< /span> < span class="nv"> $MAILDEST< /span>
< span class="k"> fi< /span>
< /pre> < /div>
< /td> < /tr> < /table>
< p> You can add multiple domains in the script. As long as you add all 3 lines for each domain, it will not automatically reload nginx if one or more certificate could not be renewed and will send an e-mail to the address configured in the < code> MAILDEST< /code> variable.< /p>
< p> You can configure this script in the root user crontab using the < code> crontab -e< /code> command :< /p>
< div class="highlight"> < pre> < span> < /span> ## LETSENCRYPT CERTIFICATE AUTORENEWAL
30 03 01 */2 * /root/bin/tlsrenew
< /pre> < /div>
< p> This will run the script every two months, on the first day of the month, at 3:30 AM.< /p>
< h2> Client configuration< /h2>
< h3> Installing the required packages< /h3>
< p> A single package is required to mount a webdav volume on Debian :< /p>
< div class="highlight"> < pre> < span> < /span> apt update & amp;& amp; apt install davfs2
< /pre> < /div>
< h3> Mounting the share manually< /h3>
< p> If like me, you want to mount your webdav share in a LXC container, you'll first need to make sure that the following line is present in its configuration file :< /p>
< div class="highlight"> < pre> < span> < /span> lxc.cgroup.devices.allow = c 10:229 rwm
< /pre> < /div>
< p> You'll also need to create the < code> /dev/fuse< /code> node in the container :< /p>
< div class="highlight"> < pre> < span> < /span> mknod /dev/fuse c 10 229
< /pre> < /div>
< p> In any case, we have to edit the < code> /etc/davfs2/secrets< /code> file to add the mount point, username and password that will be used to mount the share :< /p>
< div class="highlight"> < pre> < span> < /span> echo & #39;/data webdav notanactualpassword& #39; & gt;& gt; /etc/davfs2/secrets
< /pre> < /div>
< p> Once that's done, we can mount our share with the following command :< /p>
< div class="highlight"> < pre> < span> < /span> mount -t davfs https://www.example.com/data /data -o ro,dir_mode=750,file_mode=640,uid=root,gid=root
< /pre> < /div>
< p> You might need to edit the parameters depending on which users you want to make the share available to.< /p>
< h3> Mouting the share on boot< /h3>
< p> A davfs volume can be mounted via the < code> /etc/fstab< /code> file, but I decided to use monit instead so that the volume would be mounted again automatically should my WebDAV server reboot.< /p>
< p> In order to do so, I first created a < code> davfs.txt< /code> file in the < code> /var/www/data< /code> folder on my WebDAV server :< /p>
< div class="highlight"> < pre> < span> < /span> touch /var/www/data/davfs.txt
< /pre> < /div>
< p> I then created the following < code> /root/bin/mount_davfs< /code> script :< /p>
< table class="highlighttable"> < tr> < td class="linenos"> < div class="linenodiv"> < pre> 1
2
3
4< /pre> < /div> < /td> < td class="code"> < div class="highlight"> < pre> < span> < /span> < span class="ch"> #!/bin/bash< /span>
mknod /dev/fuse c < span class="m"> 10< /span> 229
mount -t davfs https://www.example.com/data /data -o ro,dir_mode< span class="o"> =< /span> 750,file_mode< span class="o"> =< /span> 640,uid< span class="o"> =< /span> root,gid< span class="o"> =< /span> root
< /pre> < /div>
< /td> < /tr> < /table>
< p> The last thing I did was create a < code> /etc/monit/conf.d/davfs< /code> file with the following content :< /p>
< div class="highlight"> < pre> < span> < /span> check file davfs with path /data/davfs.txt
alert monit@example.com
if does not exist then exec & quot;/root/bin/mount_davfs& quot;
< /pre> < /div>
< p> That way, if monit notices that the < code> /data/davfs.txt< /code> file becomes inaccessible for some reason, it will try remouting the share.< /p>
< h2> Conclusion< /h2>
2016-05-22 15:22:00 +02:00
< p> That's all ! Hopefully this has been useful to someone. Please do comment below if you have any question or if this has been helpful !< /p> </summary> </entry> <entry > <title > MySQL backup script</title> <link href= "https://captainark.net/mysql-backup-script.html" rel= "alternate" > </link> <published > 2016-03-13T00:00:00+01:00</published> <author > <name > Antoine Joubert</name> </author> <id > tag:captainark.net,2016-03-13:mysql-backup-script.html</id> <summary type= "html" > < p> I wrote a MySQL database backup script a while back. I known they are more than enough of them already floating around the internet, but hey, I figured I'd share it here anyway.< /p>
2016-03-13 18:10:10 +01:00
< h2> The script< /h2>
< p> For the script to work, you'll need to edit a few variable to match your configuration.< /p>
< ul>
< li> < code> BACKUPDIR< /code> is the path of the directory where you want your backups to be stored.< /li>
< li> < code> BACKUPUSR< /code> is the user that will connect to MySQL to dump the databases. It should have access to all you databases without needing a password.< /li>
< li> < code> EXCLUDELIST< /code> is a list of databases that should not be backed-up. Leaving it as is is probably fine.< /li>
< /ul>
< div class="highlight"> < pre> < span> < /span> < span class="ch"> #!/bin/bash< /span>
< span class="nv"> BACKUPDIR< /span> < span class="o"> =< /span> < span class="s2"> & quot;/home/user/backup& quot;< /span>
< span class="nv"> BACKUPUSR< /span> < span class="o"> =< /span> < span class="s2"> & quot;user& quot;< /span>
< span class="nv"> EXCLUDELIST< /span> < span class="o"> =< /span> < span class="s2"> & quot;^Databases< /span> $< span class="s2"> |^information_schema< /span> $< span class="s2"> |^mysql< /span> $< span class="s2"> |^performance_schema< /span> $< span class="s2"> & quot;< /span>
sqlbk< span class="o"> ()< /span> < span class="o"> {< /span>
< span class="k"> for< /span> each in < span class="k"> $(< /span> mysqlshow < span class="p"> |< /span> awk < span class="s1"> & #39;/[[:alnum:]]/{print $2}& #39;< /span> < span class="k"> )< /span> < span class="p"> ;< /span> < span class="k"> do< /span>
< span class="k"> if< /span> < span class="o"> [[< /span> < span class="nv"> $each< /span> < span class="o"> =< /span> ~ < span class="nv"> $EXCLUDELIST< /span> < span class="o"> ]]< /span> < span class="p"> ;< /span> < span class="k"> then< /span>
< span class="nb"> true< /span>
< span class="k"> else< /span>
mysqldump < span class="nv"> $each< /span> < span class="p"> |< /span> bzip2 & gt; < span class="si"> ${< /span> < span class="nv"> BACKUPDIR< /span> < span class="si"> }< /span> /< span class="si"> ${< /span> < span class="nv"> each< /span> < span class="si"> }< /span> .sql.bz2
chown < span class="si"> ${< /span> < span class="nv"> BACKUPUSR< /span> < span class="si"> }< /span> : < span class="si"> ${< /span> < span class="nv"> BACKUPDIR< /span> < span class="si"> }< /span> /< span class="si"> ${< /span> < span class="nv"> each< /span> < span class="si"> }< /span> .sql.bz2 < span class="o"> & amp;& amp;< /span> chmod < span class="m"> 600< /span> < span class="si"> ${< /span> < span class="nv"> BACKUPDIR< /span> < span class="si"> }< /span> /< span class="si"> ${< /span> < span class="nv"> each< /span> < span class="si"> }< /span> .sql.bz2
< span class="k"> fi< /span>
< span class="k"> done< /span>
< span class="o"> }< /span>
2016-03-20 22:30:57 +01:00
< span class="o"> [[< /span> -e /etc/init.d/mysql < span class="o"> ]]< /span> < span class="o"> & amp;& amp;< /span> sqlbk
2016-03-13 18:10:10 +01:00
< /pre> < /div>
< p> I personnaly have this script running once a week, in my user's personnal crontab (editable using the < code> crontab -e< /code> command) :< /p>
< div class="highlight"> < pre> < span> < /span> ## WEEKLY DATABASE BACKUP
@weekly /home/user/bin/backupdb
< /pre> < /div>
< h1> Conclusion< /h1>
< p> You've probably noticed that the script erases the previous backup when a new one is made.< /p>
< p> I don't need to keep multiple versions of the same database backup on my servers because they are all saved remotely on a daily basis using < a href="http://rsnapshot.org/"> Rsnapshot< /a> . I'll probably write an article on the subject in the future.< /p>
2016-05-22 15:22:00 +02:00
< p> As usual, feedback is always appreciated !< /p> </summary> </entry> <entry > <title > Postfix Admin</title> <link href= "https://captainark.net/postfix-admin.html" rel= "alternate" > </link> <published > 2016-03-06T00:00:00+01:00</published> <author > <name > Antoine Joubert</name> </author> <id > tag:captainark.net,2016-03-06:postfix-admin.html</id> <summary type= "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>
2016-03-13 17:39:32 +01:00
< 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>
2016-03-21 22:41:15 +01:00
< span class="k"> GRANT< /span> < span class="k"> ALL< /span> < span class="k"> PRIVILEGES< /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"> & #39;mail& #39;< /span> < span class="o"> @< /span> < span class="s1"> & #39;localhost& #39;< /span> < span class="p"> ;< /span>
2016-03-13 17:39:32 +01:00
< 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>
2016-03-13 17:46:57 +01:00
< 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>
2016-03-13 17:39:32 +01:00
< 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=& #39;%s& #39; AND active=& #39;1& #39;
< /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=& #39;%s& #39; AND active=& #39;1& #39;
< /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=& #39;%s& #39; AND active=& #39;1& #39;
< /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=& #39;%u& #39; AND active=& #39;1& #39;;
< /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 & gt; /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>
2016-03-13 17:50:22 +01:00
< p> Next, let's restart Postfix and Dovecot so that they start using the new database schema :< /p>
2016-03-13 17:39:32 +01:00
< 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>
2016-05-22 15:22:00 +02:00
< p> That's all ! As always, please do leave a comment if this article has been of any use to you !< /p> </summary> </entry> <entry > <title > My tmux configuration</title> <link href= "https://captainark.net/my-tmux-configuration.html" rel= "alternate" > </link> <published > 2016-02-02T00:00:00+01:00</published> <author > <name > Antoine Joubert</name> </author> <id > tag:captainark.net,2016-02-02:my-tmux-configuration.html</id> <summary type= "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>
2016-02-02 23:16:42 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> # use utf8
2016-02-02 23:16:42 +01:00
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 & quot;screen-256color& quot;
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 & #39;#[fg=red][ #[fg=white]#H #[fg=red]]#[default]& #39;
set-option -g status-right & #39;#[fg=red][ #[fg=white]%R %d/%m #[fg=red]]#[default]& #39;
# modes
set-option -g mode-bg default
set-option -g mode-fg blue
# inactive window format
set-window-option -g window-status-format & #39;#I:#W#F& #39;
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 & #39;#[fg=red](#[default]#I:#W#F#[fg=red])#[default]& #39;
# reload tmux configuration
unbind r
bind r source-file ~/.tmux.conf \; display & quot;Configuration reloaded!& quot;
# 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 & quot;rename-window %%& quot;
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 & quot;kill-window& quot;
unbind l
bind l refresh-client
unbind & #39;& quot;& #39;
bind & #39;& quot;& #39; 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mytmux< span class="o"> ()< /span> < span class="o"> {< /span>
2016-02-02 23:16:42 +01:00
tmux has-session -t mytmux
2016-02-02 23:20:20 +01:00
< 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>
2016-02-02 23:16:42 +01:00
tmux attach -t mytmux
2016-02-02 23:20:20 +01:00
< span class="o"> }< /span>
2016-02-02 23:16:42 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="k"> if< /span> < span class="o"> [< /span> -n < span class="s2"> & quot;< /span> < span class="nv"> $TMUX< /span> < span class="s2"> & quot;< /span> < span class="o"> ]< /span> < span class="p"> ;< /span> < span class="k"> then< /span>
2016-02-02 23:20:20 +01:00
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"> & quot;< /span> < span class="si"> ${< /span> < span class="p"> @: -1< /span> < span class="si"> }< /span> < span class="s2"> & quot;< /span>
< span class="nb"> command< /span> ssh < span class="s2"> & quot;< /span> < span class="nv"> $@< /span> < span class="s2"> & quot;< /span>
tmux rename-window < span class="s2"> & quot;< /span> < span class="k"> $(< /span> hostname< span class="k"> )< /span> < span class="s2"> & quot;< /span>
2016-02-02 23:16:42 +01:00
< span class="k"> else< /span>
2016-02-02 23:20:20 +01:00
< span class="nb"> command< /span> ssh < span class="s2"> & quot;< /span> < span class="nv"> $@< /span> < span class="s2"> & quot;< /span>
< span class="k"> fi< /span>
< span class="o"> }< /span>
< span class="k"> fi< /span>
2016-02-02 23:16:42 +01:00
< /pre> < /div>
< h2> Conclusion< /h2>
2016-05-22 15:22:00 +02:00
< p> That's all ! As always, please do leave a comment if you've found something useful in this article !< /p> </summary> </entry> <entry > <title > Debian updates with Ansible</title> <link href= "https://captainark.net/debian-updates-with-ansible.html" rel= "alternate" > </link> <published > 2016-01-31T00:00:00+01:00</published> <author > <name > Antoine Joubert</name> </author> <id > tag:captainark.net,2016-01-31:debian-updates-with-ansible.html</id> <summary type= "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>
2016-01-31 16:11:44 +01:00
< 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>
2016-06-01 22:59:17 +02:00
< 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>
2016-01-31 16:11:44 +01:00
< span class="l l-Scalar l-Scalar-Plain"> tasks< /span> < span class="p p-Indicator"> :< /span>
2016-06-01 22:59:17 +02:00
2016-01-31 16:11:44 +01:00
< 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>
2016-06-01 22:59:17 +02:00
2016-01-31 16:11:44 +01:00
< 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>
2016-06-02 19:45:14 +02:00
< 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 -s -V | awk & #39;/=& gt;/{print $1}& #39;< /span>
2016-01-31 16:11:44 +01:00
< 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>
2016-06-01 22:59:17 +02:00
2016-01-31 16:11:44 +01:00
< 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=& quot;{{ updates.stdout_lines | count }} packages to upgrade ({{ updates.stdout_lines | join(& #39;, & #39;) }})& quot;< /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>
2016-06-01 22:59:17 +02:00
2016-01-31 16:11:44 +01:00
< 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>
2016-06-01 22:59:17 +02:00
2016-01-31 16:11:44 +01:00
< 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 & #39;{print $2}& #39;< /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>
2016-06-01 22:59:17 +02:00
2016-01-31 16:11:44 +01:00
< 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=& quot;Debian has been upgraded from {{ ansible_lsb.release }} to {{ new_release.stdout }}& quot;< /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>
2016-06-01 22:59:17 +02:00
2016-05-31 20:45:06 +02:00
< 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"> /wheezy/ install the debian-goodies package if it is missing< /span>
2016-01-31 16:11:44 +01:00
< 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>
2016-05-31 20:45:06 +02:00
< 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_distribution_release == & #39;wheezy& #39;< /span>
2016-06-01 22:59:17 +02:00
2016-05-31 20:45:06 +02:00
< 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"> /jessie/ install the needrestart 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=needrestart state=present default_release=jessie-backports< /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_distribution_release == & #39;jessie& #39;< /span>
2016-06-01 22:59:17 +02:00
< 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"> /wheezy/ list services to restart (1/3)< /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 | awk & #39;/^service/{print $2}& #39;< /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"> wheezy_services< /span>
2016-01-31 16:11:44 +01:00
< 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>
2016-05-31 20:45:06 +02:00
< 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_distribution_release == & #39;wheezy& #39;< /span>
2016-06-01 22:59:17 +02:00
< 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"> /jessie/ list services to restart (1/3)< /span>
2016-06-04 00:37:01 +02:00
< span class="l l-Scalar l-Scalar-Plain"> shell< /span> < span class="p p-Indicator"> :< /span> < span class="l l-Scalar l-Scalar-Plain"> needrestart -blrl | awk & #39;/^NEEDRESTART-SVC/{print $2}& #39;< /span>
2016-06-01 22:59:17 +02:00
< span class="l l-Scalar l-Scalar-Plain"> register< /span> < span class="p p-Indicator"> :< /span> < span class="l l-Scalar l-Scalar-Plain"> jessie_services< /span>
2016-05-31 20:45:06 +02:00
< 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"> when< /span> < span class="p p-Indicator"> :< /span> < span class="l l-Scalar l-Scalar-Plain"> ansible_distribution_release != & #39;wheezy& #39;< /span>
2016-06-01 22:59:17 +02:00
< 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"> merge services list (2/3)< /span>
< span class="l l-Scalar l-Scalar-Plain"> set_fact< /span> < span class="p p-Indicator"> :< /span>
< span class="l l-Scalar l-Scalar-Plain"> services< /span> < span class="p p-Indicator"> :< /span> < span class="s"> & quot;{{< /span> < span class="nv"> < /span> < span class="s"> wheezy_services< /span> < span class="nv"> < /span> < span class="s"> if< /span> < span class="nv"> < /span> < span class="s"> ansible_distribution_release< /span> < span class="nv"> < /span> < span class="s"> ==< /span> < span class="nv"> < /span> < span class="s"> & #39;wheezy& #39;< /span> < span class="nv"> < /span> < span class="s"> else< /span> < span class="nv"> < /span> < span class="s"> jessie_services< /span> < span class="nv"> < /span> < span class="s"> }}& quot;< /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 (3/3)< /span>
2016-01-31 16:11:44 +01:00
< 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=& quot;{{ services.stdout_lines | count }} services to restart ({{ services.stdout_lines | join (& #39;, & #39;) }})& quot;< /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>
2016-06-01 22:59:17 +02:00
2016-01-31 16:11:44 +01:00
< 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>
2016-05-22 15:22:00 +02:00
< p> That's all ! Please leave a comment if you've found this playbook helpful !< /p> </summary> </entry> <entry > <title > Private Git Repo</title> <link href= "https://captainark.net/private-git-repo.html" rel= "alternate" > </link> <published > 2016-01-31T00:00:00+01:00</published> <author > <name > Antoine Joubert</name> </author> <id > tag:captainark.net,2016-01-31:private-git-repo.html</id> <summary type= "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>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> ssh-keygen -t rsa -b 3072
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> sudo -H -u www-data ssh-keygen -t rsa -b 3072
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> apt update < span class="o"> & amp;& amp;< /span> apt install git -y
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Then, we have to create a user named git :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> useradd -s /usr/bin/git-shell -m -r git
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> This will create a system user (UID & lt; 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="nb"> echo< /span> < span class="s1"> & #39;/usr/bin/git-shell& #39;< /span> & gt;& gt; /etc/shells
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> From this point, you should have to following output if you try to SSH to your server with that user :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> ssh git@git.captainark.net
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> sudo -H -u git mkdir /home/git/.ssh < span class="o"> & amp;& amp;< /span> chmod < span class="m"> 700< /span> /home/git/.ssh
2016-01-31 16:11:44 +01:00
sudo -H -u git touch /home/git/.ssh/authorized_keys < span class="o"> & amp;& amp;< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> sudo -H -u git mkdir /home/git/captainarkdotnet.git
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> Initialized empty Git repository in /home/git/captainarkdotnet.git/.git/
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="nb"> cd< /span> ~/Documents/projects/captainarkdotnet
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Let's now push the content of that folder to our repo :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> git init
2016-01-31 16:11:44 +01:00
git add .
git commit -m < span class="s1"> & #39;initial commit& #39;< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> Counting objects: 69, < span class="k"> done< /span> .
2016-01-31 16:11:44 +01:00
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 -& gt; 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="nb"> cd< /span> /var/www
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> Cloning into < span class="s1"> & #39;captainarkdotnet& #39;< /span> ...
2016-01-31 16:11:44 +01:00
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>
2016-05-22 15:22:00 +02:00
< p> If you've found this tutorial in any way helpful, please feel free to leave a comment !< /p> </summary> </entry> <entry > <title > Flexget init script</title> <link href= "https://captainark.net/flexget-init-script.html" rel= "alternate" > </link> <published > 2015-05-05T00:00:00+02:00</published> <author > <name > Antoine Joubert</name> </author> <id > tag:captainark.net,2015-05-05:flexget-init-script.html</id> <summary type= "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>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="c1"> # Configuration file for /etc/init.d/flexget< /span>
2016-01-31 16:11:44 +01:00
< 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"> & quot;& quot;< /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"> & quot;& quot;< /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"> & quot;& quot;< /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"> & quot;& quot;< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> useradd -m -d /var/lib/flexget -r -s /bin/false flexget
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Then, create the /etc/init.d/flexget file :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="ch"> #!/bin/bash< /span>
2016-01-31 16:11:44 +01:00
< 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"> & quot;flexget& quot;< /span>
< span class="nv"> DAEMON< /span> < span class="o"> =< /span> < span class="s2"> & quot;/usr/local/bin/flexget& quot;< /span>
2016-03-01 19:32:16 +01:00
< span class="nv"> SETTINGS< /span> < span class="o"> =< /span> < span class="s2"> & quot;/etc/default/< /span> < span class="nv"> $NAME< /span> < span class="s2"> & quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="nv"> DESC< /span> < span class="o"> =< /span> < span class="s2"> & quot;Flexget& quot;< /span>
2016-03-01 19:32:16 +01:00
< span class="nv"> PIDFILE< /span> < span class="o"> =< /span> < span class="s2"> & quot;/var/run/< /span> < span class="nv"> $NAME< /span> < span class="s2"> .pid& quot;< /span>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< span class="k"> if< /span> < span class="o"> [< /span> ! -x < span class="s2"> & quot;< /span> < span class="nv"> $DAEMON< /span> < span class="s2"> & quot;< /span> < span class="o"> ]< /span> < span class="p"> ;< /span> < span class="k"> then< /span>
log_action_msg < span class="s2"> & quot;< /span> < span class="nv"> $DESC< /span> < span class="s2"> : Could not find flexget executable. Exiting.& quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="nb"> exit< /span> 2
< span class="k"> fi< /span>
< span class="c1"> # Read configuration variables< /span>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
< span class="k"> else< /span>
2016-03-01 19:32:16 +01:00
log_action_msg < span class="s2"> & quot;< /span> < span class="nv"> $DESC< /span> < span class="s2"> : /etc/default/< /span> < span class="nv"> $NAME< /span> < span class="s2"> not found. Exiting.& quot;< /span>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< 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"> & quot;< /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.& quot;< /span>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< 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> & gt; /dev/null < span class="o"> & amp;& amp;< /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"> & quot;< /span> < span class="nv"> $?< /span> < span class="s2"> & quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="k"> else< /span>
< span class="nv"> RETVAL< /span> < span class="o"> =< /span> < span class="s2"> & quot;2& quot;< /span>
< span class="k"> fi< /span>
< span class="o"> }< /span>
end_log< span class="o"> ()< /span> < span class="o"> {< /span>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< 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"> & quot;< /span> < span class="nv"> $OPTIONS< /span> < span class="s2"> & quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="k"> else< /span>
2016-03-01 19:32:16 +01:00
< span class="nv"> OPTIONS< /span> < span class="o"> =< /span> < span class="s2"> & quot;-c < /span> < span class="nv"> $CONFIG< /span> < span class="s2"> & quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="k"> fi< /span>
2016-03-01 19:32:16 +01:00
< 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"> & quot;< /span> < span class="nv"> $OPTIONS< /span> < span class="s2"> & quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="k"> else< /span>
2016-03-01 19:32:16 +01:00
< span class="nv"> OPTIONS< /span> < span class="o"> =< /span> < span class="s2"> & quot;< /span> < span class="nv"> $OPTIONS< /span> < span class="s2"> -l < /span> < span class="nv"> $LOG< /span> < span class="s2"> /flexget.log& quot;< /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>
2016-01-31 16:11:44 +01:00
< span class="k"> fi< /span>
< span class="k"> fi< /span>
2016-03-01 19:32:16 +01:00
< 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"> & quot;< /span> < span class="nv"> $OPTIONS< /span> < span class="s2"> & quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="k"> else< /span>
2016-03-01 19:32:16 +01:00
< span class="nv"> OPTIONS< /span> < span class="o"> =< /span> < span class="s2"> & quot;< /span> < span class="nv"> $OPTIONS< /span> < span class="s2"> -L < /span> < span class="nv"> $LEVEL< /span> < span class="s2"> & quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="k"> fi< /span>
< span class="o"> }< /span>
start_flexget< span class="o"> ()< /span> < span class="o"> {< /span>
run_check
2016-03-01 19:32:16 +01:00
< 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"> & quot;< /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.& quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="nb"> exit< /span> 2
< span class="k"> else< /span>
conf_check
2016-03-01 19:32:16 +01:00
log_daemon_msg < span class="s2"> & quot;< /span> < span class="nv"> $DESC< /span> < span class="s2"> : Starting the daemon.& quot;< /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
2016-01-31 16:11:44 +01:00
< 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
2016-03-01 19:32:16 +01:00
< 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"> & quot;< /span> < span class="nv"> $DESC< /span> < span class="s2"> : Stopping the daemon.& quot;< /span>
start-stop-daemon --stop --quiet --chuid < span class="s2"> & quot;< /span> < span class="nv"> $FGUSER< /span> < span class="s2"> & quot;< /span> --pidfile < span class="s2"> & quot;< /span> < span class="nv"> $PIDFILE< /span> < span class="s2"> & quot;< /span> --retry 30
2016-01-31 16:11:44 +01:00
< span class="nv"> RETVAL< /span> < span class="o"> =< /span> < span class="nv"> $?< /span>
2016-03-01 19:32:16 +01:00
< span class="o"> [< /span> -e < span class="s2"> & quot;< /span> < span class="nv"> $PIDFILE< /span> < span class="s2"> & quot;< /span> < span class="o"> ]< /span> < span class="o"> & amp;& amp;< /span> rm -f < span class="s2"> & quot;< /span> < span class="nv"> $PIDFILE< /span> < span class="s2"> & quot;< /span>
2016-01-31 16:11:44 +01:00
end_log
< span class="k"> else< /span>
2016-03-01 19:32:16 +01:00
log_action_msg < span class="s2"> & quot;< /span> < span class="nv"> $DESC< /span> < span class="s2"> : Not currently running. Aborting.& quot;< /span>
2016-01-31 16:11:44 +01:00
< 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
2016-03-01 19:32:16 +01:00
< 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"> & quot;< /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"> .& quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="k"> else< /span>
2016-03-01 19:32:16 +01:00
log_action_msg < span class="s2"> & quot;< /span> < span class="nv"> $DESC< /span> < span class="s2"> : Not currently running.& quot;< /span>
2016-01-31 16:11:44 +01:00
< span class="k"> fi< /span>
2016-03-01 19:32:16 +01:00
< span class="nb"> exit< /span> < span class="nv"> $RETVAL< /span>
2016-01-31 16:11:44 +01:00
< span class="o"> }< /span>
< span class="k"> case< /span> < span class="s2"> & quot;< /span> < span class="nv"> $1< /span> < span class="s2"> & quot;< /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"> & amp;& amp;< /span> sleep < span class="m"> 2< /span> < span class="o"> & amp;& amp;< /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"> & quot;Usage: < /span> < span class="nv"> $0< /span> < span class="s2"> {start|stop|restart|status}& quot;< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> chmod +x /etc/init.d/flexget
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> And then, generate the necessary symlinks for the service to start on boot :< /p>
< p> < em> Debian Jessie< /em> < /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> systemctl < span class="nb"> enable< /span> flexget
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> < em> Debian Wheezy< /em> < /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> insserv flexget
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> To start, stop or check if the daemon is running :< /p>
< p> < em> Debian Jessie< /em> < /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> systemctl start flexget
2016-01-31 16:11:44 +01:00
systemctl stop flexget
systemctl status flexget
< /pre> < /div>
< p> < em> Debian Wheezy / Jessie< /em> < /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> service flexget start
2016-01-31 16:11:44 +01:00
service flexget stop
service flexget status
< /pre> < /div>
< p> < em> Debian Wheezy< /em> < /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> /etc/init.d/flexget start
2016-01-31 16:11:44 +01:00
/etc/init.d/flexget stop
/etc/init.d/flexget status
< /pre> < /div>
< h2> Conclusion< /h2>
2016-05-22 15:22:00 +02:00
< p> That's all ! If you are using this script, please let me know in the comment section below !< /p> </summary> </entry> <entry > <title > Setting up a mail server</title> <link href= "https://captainark.net/setting-up-a-mail-server.html" rel= "alternate" > </link> <published > 2015-04-24T00:00:00+02:00</published> <author > <name > Antoine Joubert</name> </author> <id > tag:captainark.net,2015-04-24:setting-up-a-mail-server.html</id> <summary type= "html" > < p> In this first tutorial, I'll explain how I've configured my mail server using the following :< /p>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> apt update
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="o"> [< /span> client< span class="o"> ]< /span>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> chmod < span class="m"> 600< /span> ~/.my.cnf
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="nb"> echo< /span> < span class="s1"> & #39;export MYSQL_PS1=& quot;[\u@\h] (\d)& gt; & quot;& #39;< /span> & gt; ~/.bash_aliases
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> :~$ mysql mysql
2016-01-31 16:11:44 +01:00
< 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> & gt;
< /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>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< 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"> & #39;mail& #39;< /span> < span class="o"> @< /span> < span class="s1"> & #39;localhost& #39;< /span> < span class="n"> IDENTIFIED< /span> < span class="k"> BY< /span> < span class="s1"> & #39;mailpassword& #39;< /span> < span class="p"> ;< /span>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="n"> USE< /span> < span class="n"> mail< /span> < span class="p"> ;< /span>
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> The first table we are going to create will contain the domains we will be using with our mail server :< /p>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< 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"> & #39;captainark.net& #39;< /span> < span class="p"> );< /span>
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> We can now create users associated with this domain in the virtual_users table :< /p>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
< span class="p"> (< /span> < span class="s1"> & #39;1& #39;< /span> < span class="p"> ,< /span> < span class="n"> ENCRYPT< /span> < span class="p"> (< /span> < span class="s1"> & #39;notanactualpassword& #39;< /span> < span class="p"> ,< /span> < span class="n"> CONCAT< /span> < span class="p"> (< /span> < span class="s1"> & #39;$6$& #39;< /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"> & #39;example@captainark.net& #39;< /span> < span class="p"> );< /span>
< /pre> < /div>
< p> This is not mandatory, but we can also create our first mail alias :< /p>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
< span class="p"> (< /span> < span class="s1"> & #39;1& #39;< /span> < span class="p"> ,< /span> < span class="s1"> & #39;alias@captainark.net& #39;< /span> < span class="p"> ,< /span> < span class="s1"> & #39;example@captainark.net& #39;< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> cp /etc/postfix/main.cf /etc/postfix/main.cf.orig
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> groupadd -g < span class="m"> 5000< /span> vmail
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mkdir /etc/postfix/ssl
2016-01-31 16:11:44 +01:00
chown root: /etc/postfix/ssl < span class="o"> & amp;& amp;< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="nb"> cd< /span> /etc/postfix/ssl
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> wget -O /etc/postfix/ssl/sub.class1.server.ca.pem < span class="se"> \< /span>
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> chown root: /etc/postfix/ssl/* < span class="o"> & amp;& amp;< /span> chmod < span class="m"> 600< /span> /etc/postfix/ssl/*
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> The last thing we have to do here is to generate Diffie-Hellman keys for Perfect Forward Secrecy (PFS) :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> openssl gendh -out /etc/postfix/dh_512.pem -2 512
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> smtpd_banner = < span class="nv"> $myhostname< /span> ESMTP < span class="nv"> $mail_name< /span> (Debian/GNU)
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> submission inet n - - - - smtpd
2016-01-31 16:11:44 +01:00
-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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> dovecot unix - n n - - pipe
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> user = mail
2016-01-31 16:11:44 +01:00
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT 1 FROM virtual_domains WHERE name=& #39;%s& #39;
< /pre> < /div>
< p> /etc/postfix/mysql-virtual-mailbox-maps.cf should contain the following lines :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> user = mail
2016-01-31 16:11:44 +01:00
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT 1 FROM virtual_users WHERE email=& #39;%s& #39;
< /pre> < /div>
< p> /etc/postfix/mysql-virtual-alias-maps.cf should contain the following lines :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> user = mail
2016-01-31 16:11:44 +01:00
password = mailpassword
hosts = 127.0.0.1
dbname = mail
query = SELECT destination FROM virtual_aliases WHERE source=& #39;%s& #39;
< /pre> < /div>
< p> Since these files contain a password, let's make sure they are not world-readable :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> chown root: /etc/postfix/mysql* < span class="o"> & amp;& amp;< /span> chmod < span class="m"> 600< /span> /etc/postfix/mysql*
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> You can use the command postmap to confirm that everything is working properly :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> postmap -q captainark.net mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> systemctl restart postfix
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.orig
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Next, we are going to create a new /etc/dovecot/dovecot.conf file. It should contain the following lines :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="sx"> !include_try /usr/share/dovecot/protocols.d/*.protocol< /span>
2016-01-31 16:11:44 +01:00
< 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"> & lt;/< /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"> & lt;/< /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"> & lt;/< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mkdir -p /var/mail/captainark.net
2016-01-31 16:11:44 +01:00
chown vmail: /var/mail/captainark.net < span class="o"> & amp;& amp;< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> driver = mysql
2016-01-31 16:11:44 +01:00
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=& #39;%u& #39;;
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> chown root: /etc/dovecot/sql.conf < span class="o"> & amp;& amp;< /span> chmod < span class="m"> 600< /span> /etc/dovecot/sql.conf
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mkdir -p /var/mail/sieve/before
2016-01-31 16:11:44 +01:00
mkdir /var/mail/sieve/after
mkdir /var/mail/sieve/users
chown -R vmail: /var/mail/sieve < span class="o"> & amp;& amp;< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> require [& quot;envelope& quot;, & quot;fileinto& quot;, & quot;imap4flags& quot;, & quot;regex& quot;];
2016-01-31 16:11:44 +01:00
if not header :regex & quot;message-id& quot; & quot;.*@.*\.& quot; {
fileinto & quot;Junk& quot;;
}
if header :contains & quot;X-Spam-Level& quot; & quot;*****& quot; {
fileinto & quot;Junk& quot;;
}
< /pre> < /div>
< p> Last thing we have to do is to change the permissions on the newly created file :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> chown vmail: /var/mail/sieve/before/filter.sieve < span class="o"> & amp;& amp;< /span> < span class="se"> \< /span>
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> systemctl restart dovecot
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> ENABLED=1
2016-01-31 16:11:44 +01:00
[...]
CRON=1
< /pre> < /div>
< p> Before the cron runs for the first time, we have to manually update SpamAssassin's ruleset :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> sa-learn
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Next, as usual, let's back up the original configuration file :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mv /etc/spamassassin/local.cf /etc/spamassassin/local.cf.orig
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Let's create a new /etc/spamassassin/local.cf file with the following content :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> rewrite_header Subject [SPAM]
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> smtp inet n - - - - smtpd
2016-01-31 16:11:44 +01:00
-o content_filter=spamassassin
< /pre> < /div>
< p> At the very end of the same file, we have to add the following lines :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> spamassassin unix - n n - - pipe
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> systemctl restart postfix
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> captainark.net IN TXT & quot;v=spf1 mx ~all& quot;
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> Header_Type = AR
2016-01-31 16:11:44 +01:00
Authserv_Id = & quot;& lt;server& #39;s FQDN& gt;& quot;
< /pre> < /div>
< p> Then, let's edit the /etc/postfix/master.cf file and add the following lines at the end :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> policy-spf unix - n n - - spawn
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> smtpd_recipient_restrictions =
2016-01-31 16:11:44 +01:00
[...]
reject_unauth_destination,
check_policy_service unix:private/policy-spf,
permit
< /pre> < /div>
< p> We now have to restart postfix :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> systemctl restart postfix
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
< 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mv /etc/opendkim.conf /etc/opendkim.conf.orig
2016-01-31 16:11:44 +01:00
mkdir /etc/opendkim.d
< /pre> < /div>
< p> We now have to create a /etc/opendkim.conf file with the following content :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> AutoRestart Yes
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mkdir -p /etc/opendkim.d/keys/captainark.net/
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Now, we are going to create the /etc/opendkim.d/TrustedHosts file with the following content :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> localhost
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mail._domainkey.captainark.net captainark.net:mail:/etc/opendkim.d/keys/captainark.net/mail.private
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> *@captainark.net mail._domainkey.captainark.net
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="nb"> cd< /span> /etc/opendkim.d/keys/captainark.net/
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> chown -R opendkim: /etc/opendkim.d/keys
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mkdir /var/spool/postfix/opendkim
2016-01-31 16:11:44 +01:00
chown opendkim: /var/spool/postfix/opendkim
< /pre> < /div>
< p> We also have to add the postfix user to the opendkim group :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> useradd -G opendkim postfix
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Now, let's edit the /etc/postfix/master.cf file, like so :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> smtpd_milters = unix:/opendkim/opendkim.sock
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> We now have to restart OpenDKIM and Postfix :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> systemctl restart opendkim
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mail._domainkey IN TXT & quot;v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkJq0CW3tl2XHZ1CN5XdbqRDU7KfXOJ70nlwI09bHmDU63/Yz3J5rl863S0t2ncVHfIudZANj0OaiJe5HRR7WCsjuNIhQFfPFGIWLNClpxqdQVQURI38sAGeyn7Ed/Cor1AiWABzFWzel0kvXILw8K/NTzxaAPeSa9ttwQEgSmowIDAQAB& quot; ; ----- DKIM key mail for captainark.net
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> ==========================================================
2016-01-31 16:11:44 +01:00
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 & amp; 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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mv /etc/opendmarc.conf /etc/opendmarc.conf.orig
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> We now have to create a /etc/opendmarc.conf file with the following content :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> AutoRestart Yes
2016-01-31 16:11:44 +01:00
AutoRestartRate 10/1h
UMask 0002
Syslog true
AuthservID & quot;& lt;your server& #39;s FQDN& gt;& quot;
TrustedAuthservIDs & quot;& lt;your server& #39;s FQDN& gt;& quot;
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mkdir /var/spool/postfix/opendmarc
2016-01-31 16:11:44 +01:00
chown opendmarc: /var/spool/postfix/opendmarc
< /pre> < /div>
< p> We also have to add the postfix user to the opendmarc group :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> useradd -G opendmarc postfix
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Now, let's edit the /etc/postfix/master.cf file, like so :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> smtpd_milters = unix:/opendkim/opendkim.sock, unix:/opendmarc/opendmarc.sock
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> We now have to restart OpenDMARC and Postfix :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> systemctl restart opendmarc
2016-01-31 16:11:44 +01:00
systemctl restart postfix
< /pre> < /div>
< p> You should now see the following headers in your incoming emails :< /p>
2016-03-01 19:32:16 +01:00
< 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>
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> < span class="nt"> _dmarc< /span> < span class="nt"> IN< /span> < span class="nt"> TXT< /span> < span class="s2"> & quot;v=DMARC1; p=none; rua=mailto:postmaster@captainark.net; ruf=mailto:postmaster@captainark.net& quot;< /span>
2016-01-31 16:11:44 +01:00
< /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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> mv /etc/monit/monitrc /etc/monit/monitrc.orig
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> We now have to create a new /etc/monit/monitrc file with the following content :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> set daemon 30
2016-01-31 16:11:44 +01:00
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> check process postfix
2016-01-31 16:11:44 +01:00
with pidfile & quot;/var/spool/postfix/pid/master.pid& quot;
start program = & quot;/bin/systemctl start postfix& quot;
stop program = & quot;/bin/systemctl stop postfix& quot;
alert monit@captainark.net
group mail
check process dovecot
with pidfile & quot;/run/dovecot/master.pid& quot;
start program = & quot;/bin/systemctl start dovecot& quot;
stop program = & quot;/bin/systemctl stop dovecot& quot;
alert monit@captainark.net
group mail
depends on postfix
check process spamassassin
with pidfile & quot;/run/spamassassin.pid& quot;
start program = & quot;/bin/systemctl start spamassassin& quot;
stop program = & quot;/bin/systemctl stop spamassassin& quot;
alert monit@captainark.net
group mail
depends on postfix, dovecot
check process opendkim
with pidfile & quot;/run/opendkim/opendkim.pid& quot;
start program = & quot;/bin/systemctl start opendkim& quot;
stop program = & quot;/bin/systemctl stop opendkim& quot;
alert monit@captainark.net
group mail
depends on postfix, dovecot
check process opendmarc
with pidfile & quot;/run/opendmarc/opendmarc.pid& quot;
start program = & quot;/bin/systemctl start opendmarc& quot;
stop program = & quot;/bin/systemctl stop opendmarc& quot;
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>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> chown root: /etc/monit/conf.d/mail < span class="o"> & amp;& amp;< /span> chmod < span class="m"> 600< /span> /etc/monit/conf.d/mail
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Then, we have to reload the monit daemon :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> monit reload
2016-01-31 16:11:44 +01:00
< /pre> < /div>
< p> Now, the < code> monit summary< /code> command should have the following output :< /p>
2016-03-01 19:32:16 +01:00
< div class="highlight"> < pre> < span> < /span> The Monit daemon 5.4 uptime: 3d 0h 41m
2016-01-31 16:11:44 +01:00
Process & #39;postfix& #39; Running
Process & #39;dovecot& #39; Running
Process & #39;spamassassin& #39; Running
Process & #39;opendkim& #39; Running
Process & #39;opendmarc& #39; 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> </summary> </entry> </feed>