Debian 10: System monitoring using e-mail (Exim as a smarthost)
Recently, IT infrastructure monitoring tools have been springing up like mushrooms after a rain. But let’s take a step back and look at a traditional and very basic way to monitor a system - using e-mail.
Yes, you heard right; that internet app invented in the 1960s!
For some events and incidents on a Debian system, the superuser of the system is still, by default, informed via e-mail. For example, mdadm
shoots an e-mail to root
when there is a software RAID array degradation. But by default, these e-mails are just dumped to a mail spool file, never inspected. This is really bad when your hard disk just died, and when you were never informed that the other one in the RAID array died too just a couple of months ago!
It has been 8 years since I wrote last about how to configure my favorite Mail Transport Agent, Exim. Back then, as an e-mail service provider, I wanted to run Exim as a production MTA; receiving, sending, and relaying e-mail in the internet. It was a daunting task, to say the least.
This time, however, I just want to run Exim locally, behind a firewall. It should receive locally submitted e-mail, and rather than dump the e-mails to a local mail spool file, forward it to a remote e-mail address, where I can read it on my phone. This mode of an MTA is called smarthost.
A smarthost is a local e-mail MTA/server which receives SMTP messages to any e-mail address locally, and forwards them as an authenticated client to the next MTA (that is why it must be “smart” - it must have the right credentials). The submission must be authenticated (submitting username and password), because it is unlikely that your local IP address from your ISP is not in any e-mail blacklist. And internet-facing MTAs will refuse to accept e-mails from such IP addresses.
It was not so straightforward to accomplish this, as always. But it is totally doable, and in a short time, too. This blog post documents the steps for Debian 10. They should continue to work in later Debian versions.
You will need:
- A working e-mail account at an e-mail provider of your choice (SMTP server FQDN, port number, username and password). It is advisable that you generate a dedicated username and password. Do not use your primary username and password! If the credentials leak or are stolen (they will be stored on the local hard drive, readable only by
root
), then you can simply disable the affected e-mail account without further impact. Furthermore, for security, we are going to require that the SMTP server offers a port featuring TLS-on-connect, as suggested by RFC-8314 (“cleartext considered obsolete”). - Debian 10 (newer should work too, because the fundamentals rarely change)
- Internet connection
- 30 minutes of time
- root access. Do all the following steps as root.
First, make sure that the FQDN of the SMTP server of your e-mail provider is not a DNS alias, otherwise domain matching inside of Exim will not work (Exim often uses reverse-DNS lookups to find actual FQDNs). Check this by entering:
host smtp.example.com
If this prints "smtp.example.com" is an alias for "mail.example.com"
, then resolve all aliases and choose the final FQDN. Another way to get the final FQDN is by doing a reverse DNS lookup on the server’s IP address, e.g. by running:
dig -x <ip address>
Use the printed FQDN to the right of “PTR”. In our example, this is mail.example.com
.
Now, let’s actually start by installing Exim (exim4-daemon-light
is enough):
apt install exim4
Next, configure exim4:
dpkg-reconfigure exim4-config
At the prompts, do the following:
- Select the option “mail sent by smarthost; no local mail”
- For “System mail name” leave the pre-filled hostname or PQDN
- “IP-addresses to listen on”: leave leave the default “127.0.0.1;::1”.
- “Other destinations for which mail is accepted”: leave the pre-filled hostname or PQDN.
- “Visible domain name for local users”: leave the pre-filled hostname or PQDN.
- For “IP address or host name of the outgoing smarthost” enter the final FQDN which you have found previously, plus a double colon
::
, plus the port number. For example:mail.example.com::465
- “Keep number of DNS-queries minimal”: leave the default “No”.
- “Split configuration into small files”: leave the default “No”.
Next, generate certificates for Exim by running the command below. These will be used for the TLS connections. To test, you can leave defaults for all the questions which the following command asks you. Later, you could upgrade to more professional certificates:
/usr/share/doc/exim4-base/examples/exim-gencert
Next, define the recipient e-mail address as an alias of the system user root
. Add the following line to /etc/aliases
:
root: recipient@example.com
The remote SMTP server may reject mail without a proper “From:” address. Usually, the server expects the address in the “From:” field to have the same domain name as the MTA itself. For this reason, add the following line to /etc/email-addresses
.
root: sender@example.com
Next, add credentials for the SMTP submission to the file /etc/exim4/passwd.client
in the format <FQDN>:<username>:<password>
For example:
mail.example.com:username:hackme
The default configuration of Exim shipped in the Debian package is excellent and very flexible. But TLS is not enabled by default, and so we still need to make a few adaptations to the default config file.
Add the following lines to /etc/exim4/exim4.conf.localmacros
. Create the file if it doesn’t exist:
# Enable TLS
MAIN_TLS_ENABLE = 1
# Require TLS for all remote hosts (STARTTLS or TLS-on-connect)
REMOTE_SMTP_SMARTHOST_HOSTS_REQUIRE_TLS = *
# Require TLS-on-connect for RFC-8314
REMOTE_SMTP_SMARTHOST_REQUIRE_PROTOCOL = smtps
Next, make the following adaption to the file /etc/exim4/exim4.conf.template
. After .ifdef REMOTE_SMTP_SMARTHOST_HOSTS_REQUIRE_TLS
….endif
add the following:
.ifdef REMOTE_SMTP_SMARTHOST_REQUIRE_PROTOCOL
protocol = REMOTE_SMTP_SMARTHOST_REQUIRE_PROTOCOL
.endif
Run update-exim4.conf
and systemctl restart exim4
. The final configuration file is written to /var/lib/exim4/config.autogenerated
.
Testing with an Exim test instance
To test your setup, you can start a test instance of Exim in the local root console, listening on port 26. It will use the same configuration file, but runs in parallel and independently from the already running Exim daemon. Run as root:
exim -bd -d -oX 26
Then, you can use swaks
(from the swaks
Debian package) to send a test e-mail to the test instance. The output will be very verbose, so you can easily debug:
swaks --from root@localhost --to root@localhost --port 26
This will send an e-mail to the address looked up under the key “root” in the file /etc/aliases
(in our case, recipient@example.com
). The “From:” header address will be looked up under the key “root” in the file /etc/email-addresses
(in our case, sender@example.com
).
See if you got the e-mail in the target inbox. If yes, then testing with the production Exim daemon should work too.
Testing with the Exim daemon
Observe the output of …
tail -f /var/log/exim4/mainlog
… and then again send a test e-mail using swaks
, but this time to the default port 25:
swaks --from root@localhost --to root@localhost
If you got the e-mail, then congratulations! You will now receive e-mails directed at the local root
user. You can easily extend this for other, unprivileged users of the system.
To test the entire chain and make sure that you will always be informed by important events, you could send a test e-mail in periodic intervals. But, this is a topic for a future blog post!
Scripting
You could wrap the above swaks
command in a shell script to send e-mail from other scripts. For example:
#!/bin/sh
swaks --from root@localhost --to root@localhost --header "Subject: [$(hostname)] $1" --body "Body: $2"
Warning: You could also use the older tools sendmail
or mail
to write a similar script, but both directly call exim4
under the hood, which is fine as long as you don’t call such a script from a systemd
unit (e.g. a service or a timer). Because seemingly, Exim, when called from the command line to submit e-mail, forks and detaches a short-lived process in order to deliver the e-mail to the target MTA. But systemd
kills all sub-processes as soon as the main process exits. The process lives just long enough that the e-mail is put into Exim’s queue, but it is never executed. swaks
on the other hand delivers the mail to Exim via SMTP and the delivery process is not subject to be killed by systemd
.
You should also rate-limit Exim to protect against DoS attacks, but this is also a topic for a future blog post!