How to quickly install an IMAP server with SSL encryption
I wanted to get myself a very simple IMAP server going on my home server. I also wanted the connection to be encrypted. Here’s how to achieve that.
Please note that these instructions were tested under Debian at home, for what it’s worth, but the basic of it should apply to any distribution that offers the UW IMAP server.
(Yes, I know, uw-imap sucks big time. But if you only need a simple server you can set up quickly, it will do.)
1. What packages to install
First, you need to install xinetd, because it provides a management infrastructure for the particular IMAP daemon I intend to use. Then, you need uw-imapd. Finally, you need openssl for generating encryption certificates.
All the above can be achieved like this on Debian:
apt-get install uw-imapd xinetd openssl
You may already have some of these packages installed.
uw-imapd package in Debian includes secure support, so there’s no need to install the additional uw-imapd-ssl, which is empty and kept around only for legacy purposes.2. Runtime configuration
Once it’s installed, you will want to enable it for use. It’s best to run this very simple IMAP server within the xinetd infrastructure, because it simplifies usage a lot.
Therefore, you simply go and edit /etc/xinetd.d/imap, which was installed automatically, and make sure it contains the following:
service imap
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/sbin/imapd
log_on_success += HOST DURATION
log_on_failure += HOST
}
/etc/xinetd.d/imaps, even though it may also have been installed automatically. Well, actually, check it to make sure it has disable = yes.3. Set up encryption
I definitely want encryption going, since I don’t want anybody snooping on my emails.
For secure connections (TLS), you will need to generate certificates. Here’s the magic for generating a self-signed certificate:
openssl req -new -x509 -nodes -days 365 \ -out /etc/ssl/certs/imapd.pem \ -keyout /etc/ssl/certs/imapd.pem
You will be prompted for a series of informations regarding your machine. Of particular importance is the Common Name, which MUST match the FQDN for the machine:
Country Name (2 letter code) [AU]:US State or Province Name (full name) [Some-State]:CA Locality Name (eg, city) []:Los Angeles Organization Name (eg, company) [Internet Widgits Pty Ltd]:Himmelstein Organizational Unit Name (eg, section) []:Admin Common Name (eg, YOUR name) []:imap.mydomain.com Email Address []:root@localhost
Next, it’s a good idea to also generate additional certificates, one per IP that you will use to access the IMAP service (I mean IP of the server, in case you have multiple interfaces). Even if you have only one public IP, it’s good to do this, because if you ever attempt to connect with the IP instead of the FQDN and you didn’t, you will get annoying warnings. So issue the magic again:
openssl req -new -x509 -nodes -days 365 \ -out /etc/ssl/certs/imapd-XXX.XXX.XXX.XXX.pem \ -keyout /etc/ssl/certs/imapd-XXX.XXX.XXX.XXX.pem
Use your public IP instead of the XXX stuff. You will be asked about the same details as above.
These certificates are good for one year (365 days). After that you will be notified that the certificate expired when you try to connect from the email client, and you will be denied access. At which time you can either go refresh it using this same openssl operations, or you could chose more than one year in the first place.
4. Running the server
Now you can finally start your IMAP server, via xinetd, like this:
/etc/init.d/xinetd restart
Using netstat, you should notice xinetd listening to port 143.
It’s probably a good idea to set xinetd up to start automatically when you reboot the machine or change runlevel. Usually this gets taken care automatically when xinetd is installed, but it wouldn’t hurt to make sure. I can recommend a tool such as sysv-rc-conf for this.
5. Setting up user accounts
You should now handle user authentication. As described in /usr/share/doc/libc-client2002ddebian/md5.txt, which in turn is referred from /usr/share/doc/uw-imapd, user/password pairs are defined in the text file /etc/cram-md5.pwd. They are simple entries in cleartext, in the following format:
username password
xinetd after each modification!Since the password file is in cleartext, it’s a good idea to chown it to root:root and chmod it 400. You can also do chattr +i, but make sure you remember about it, otherwise you could run amok trying to figure out why you can’t change it. :)
Also make sure you have enough space in the home dir.
6. The email client setup
Finally, the client setup. I was using Thunderbird. I created a new IMAP account and went into Tools / Account Settings / Server settings. I entered the server name (or IP, if you wish), port 143, and my username. (Entering the password will depend on whether you use the Master Password or not.)
Under Security Settings a bit lower on the page, choose “TLS” and check “Use secure authentication”.
You will get a warning upon the first access of the INBOX, saying the certificate hasn’t been endorsed by any official authority (which is true, since we generated it ourselves). Choose to accept it forever, and you will not be bothered again until it expires, which depends on how long you made it good for.
