I am a big fan of data privacy in the sense, that I want to be the only one hosting my data. The internet should only be used as a transmit zone, not as data storage. For that reason, I was looking for a method how to bring my mails to my NAS but without hosting a fully blown mail server and without the need of exposing it to the internet.
The mail provider I use does a fairly good job and I want to use his mail services (e.g., send & receive mails, filtering spam, …) but without the need to have all my mails stored on his data centres. In the past I used MS Outlook for this and downloaded my mails to my local computer with POP3. But that bears a few problems (or at least annoyances):
- The .PST files from Outlook grow rather big over time and I had to repair them every now and then.
- To get those files backed up, I either need to have them on the NAS (SMB Share – since those are backed up nightly), but then I can access my mails only in my LAN, since Outlook can’t handle those big PST files over slow VPN connections
- Having the files locally on my computer makes it usable even over slow VPN connections, but I need to back it up manually
- I can only use my computer and Outlook to access my mails
The basic idea I wanted to realise looked somewhat like this:
Searching the Internet, I found out that somebody had the same idea years ago. He described it here: https://web.archive.org/web/20200807233801/https://joel.porquet.org/wiki/hacking/getmail_dovecot/ .
He implemented it using Getmail and Dovecot. There are even some container images implementing this idea. The most current one (as of this writing) is probably this one https://github.com/tctlrd/getmail-container .
I liked the idea of a containerized solution but I decided against it for the following reasons:
- None of the images implemented it the way I wanted to use it.
- Most of the images were rather old and I’ve not seen any of it getting regular updates. The risk of security vulnerabilities gets too high, when you don’t update the software.
- I found an official container image, that gets updated frequently only for dovecot but not for Getmail6.
- I want to use the usernames and passwords of the users of my NAS also for the IMAP server. That would need a bit of a hack to get this into a container which I simply wasn’t in the mood to do.
My solution covers the following:
- Retrieving mails from the mail provider using IMAP IDLE, so I don’t have to poll every x minutes, but it rather works like a push connection until it times out.
- Every NAS user can define its own connections / mail accounts.
- Get mails from multiple mail providers / mail accounts per user.
- User accounts and passwords are taken from the Debian System using PAM.
- Mails are NOT stored in the regular $HOME directories of the system users.
- Config files for Getmail6 could be stored in the regular $HOME directories of the system users or elsewhere.
- Mail storage is on a different place (HDDs) than index files (SSD). Reason for this is speed and backup.
- Mails in dovecot are stored in Maildir++ structure (one file per mail; Subfolder possible).
- Full Text Search on server side included.
- Virtual folders on server side included.
- SSL for the local IMAP server on the NAS can be implemented.
- Full backup and restore of the mail data.
My main pieces of SW are Dovecot CE (IMAP server; see: https://www.dovecot.org/), Getmail6 (Python3 enabled port of the original Getmail; see: https://getmail6.org/) and Thunderbird (free e-mail client; also covers calendar and contacts; see: https://www.thunderbird.net/), but you could probably use any other e-Mail client capable of IMAP. This somehow excludes Outlook, since it is known since years for its weaknesses regarding IMAP.
I will first explain how to install the IMAP server (Dovecot) and make sure, we can connect to it using Thunderbird. When the IMAP server is up and running and especially the authentication topic is proven to be working, I am going to describe how to setup Getmail6 to retrieve mails.
I urge you to try every of those steps upfront in a dry-run; i.e., installing and configuring Dovecot, installing and configuring Getmail6 to retrieve mails from your mail provider (create yourself an additional account); create the special Outlook-Profile needed to transfer all the existing Mails from PST to the IMAP-Server, etc.
Prerequisites
Installation
Install the SW packages for Dovecot and Getmail using the Web-UI of OVM. The names of the packages are: dovecot-imap, dovecot-fts-xapian, getmail6.
Don’t use backports for Dovecot! I started with the backported version and got problems later with full text search (missing dependencies). This was resolved, after downgrading to the regular version from the stable branch. Since Dovecot even in this quite minimalistic installation involves several modules from the repository, I clearly recommend to stick with the stable version.
I use the following directory structure.
This is on a slow(er) disk (HDD):
/srv/additional-services/mail
├── dovecot
│ └── user
│ └── Maildir
├── getmailrc
│ └── user
│ └── state
This is the data, that goes to the long-term backup.
This is on a fast(er) disk (SSD):
In case of a disaster, the index files can (and should) easily be recreated. They need not be part of the backup.
Fallback
Latest now make a copy of /etc/docevcot.
Logging
To enable logging and define the path where to place the logfile, edit /etc/dovecot/10-logging.conf
Authentication
DE-activate SSL and allow plaintext passwords (for the moment). Authentication has enough pitfalls on its own. I don’t want the additional complexity of encryption right now.
Edit /etc/dovecot/10-auth.conf
Edit /etc/dovecot/10-ssl.conf
Mail Directory
By default Dovecot uses the $HOME directories of the system users, but that’s not what I want. Even if I use the system users for authentication, I’d rather follow the concept of the virtual users and want all the data belonging to this service to be below one central directory. Well, maybe except the index files. Those are located on a faster SSD instead of the slower HDD.
Using the system users for authentication is mainly, because I want to have the same password for all the services on my NAS and if one user changes its password, it should be changed for all the services without the need to manually keep several service specific files in sync.
One more word about directories. The directories containing the user specific data must also have the owner set to that user and this user must also have access rights for that directory. If that’s not the case, the mail service will throw errors at you, you might not at first associate with access rights and the ownership of those directories.
To set the path where Dovecot stores its internal state as well as mails and index files, edit /etc/dovecot/ 10-mail.conf:
mail_home=/srv/additional-services/mail/dovecot/%u
mail_location = maildir:/srv/additional-services/mail/dovecot/%u/Maildir:INDEX=/srv/fast-data/mail/indexes/%u
Note that %u will be substituted by the user name.
Mails will be retrieved with getmail and “delivered” (i.e. stored in the dovecot specific location below mail_location) by dovecot-lda. By default Dovecot will not automatically create any missing mailboxes or subfolders, when storing mails with dovecot-lda. The next line changes this.
Edit /etc/dovecot/15-lda.conf
First Login
Now let’s test the IMAP server. First check if the configuration contains any (syntactical) errors.
dovecot -n
This outputs the configuration, that differs from the default values. If there were any (syntactical) errors, it would tell us. Now let’s start the service:
systemctl start dovecot
To manually login issue the following command
telnet localhost imap
If everything works well, you should see a line similar to this:
OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ AUTH=PLAIN] Dovecot (Debian) ready.
Now we are talking directly to the open port of the freshly installed dovecot server. To test if the authentication works as expected enter the following command (of course replacing “user” and “password” with one of the regular users of your system).
If you see this, then Dovecot is working as expected. Now logout
Here’s a list of some commands to play with:
A LOGIN <username> <pass> # Log in
A LIST "" * # List all mail folders
A LIST INBOX * # List subfolders of INBOX
A SELECT INBOX # Set folder INBOX for the following commands
A FETCH 2 all # Get message 2
A FETCH 2 body[text] # get body of message 2
A LOGOUT # Ausloggen
Now stop Dovecot again.
systemctl stop dovecot