I've used an internal mail server since my first proper House server which was based on Windows 2003 SBS and included an Exchange server instance. This was never internet facing, although I could access via VPN when away, and it gave me a single location for email, calendar, contacts and tasks which I could connect to via PC, laptop and mobile.
It was also the seed that got me building a GeekLab when the motherboard blew, thankfully a few weeks after I'd taken a P2V image of the server as an experiment as to what this VMware stuff was all about.
I kept this running way beyond it's official retirement date since it was internal only and it just worked, eventually replacing with a set of Ubuntu servers running Samba for AD, Postfix & Dovecot for mail and Nextcloud for Calendar & Tasks and Contacts.
The Ubuntu servers are themselves approaching their retirement date. While an in-place upgrade is possible I want to make some fundamental changes so I'm going to build new servers, migrate the data and then retire the old.
Mail Server Build
The new mail server is going to be based on Ubuntu 24.04 LTS and I'll actually be building three of them.
- A sandpit server for proving the config that will only route internally but will be a permanent host for any other sandpit and dev VM that I want to configure with mail services or mail-based alerting
- A test server that will be fully configured and capable of routing externally and used to prove config before making it live
- The live server itself
Each server should be built in the same way, with the idea that by the time I'm doing Live I should have a proven and documented process that I've tried at least twice and have every confidence in. I like that theory anyway.
The server build itself will be based on my Ubuntu 24.04 Deployment Automation, but since I don't intend to build mail servers on a regular basis the install and configuration of the mail components will be manual.
Creating the server is simply a case of adding the hostname to the Ansible inventory:
vars:
config_landscape_tier: "S"
esx_network: Hall-Sandpit
sandpit_linux:
hosts:
pat.sandpit.example.com:
esx_ovf_source: U2404-base-200/X_U2404-base-2.ovf
network_type: "static"
And then running the launch script for the build automation and waiting a few minutes for the server to arrive.
richard@ansibler:~/Projects/automation/ansible/playbooks/newubuntu24$ ./ubuntu24.sh pat sandpit.example.com
Mailstore disk
Unlike Postfix which only needs to store messages while they're being processed, Dovecot will be storing messages for as long as users want to keep them, which may be years. I don't like using /var for permanent storage of data. To quote the Linux Filesytem Hierarchy Standard "/var contains variable data files. This includes spool directories and files, administrative and logging data, and transient and temporary files."
On my server layout the /var directory is also on the root filesystem and I really don't want that getting filled as Bad Things happen (maybe a future change if I start to see issues with this). So instead I add extra disk to the server VM which appears as /dev/sdc which is then formatted with a single partition /dev/sdc1 with a btrfs filesystem. While this can of course be done from the command line I cheated on this build and rebooted the VM using a desktop LiveCD and and GParted to do the formatting. I suppose I should work out the proper way of doing at and I'll amend this section accordingly.
I'm going to use subvolumes to allow for snapshots to be taken for backup, which is one of the things I really like about btrfs. My GeekLab datastore for VMs is made of two mirrored disks on a RAID controller so I don't need to configure mirroring in btrfs (which is really easy to do!).
Taking inspiration from Ubuntu the root subvolume is @dovecot and this is mounted to /srv/dovecot with permissions 650 vmail:vmail
Postfix Install
Installing Postfix is simply a case of logging into our newly built server as a user with admin rights and installing the postfix package. My users are also defined in a Samba 4 Active Directory so I'll need the postfix-ldap package to be able to check if inbound messages are for a valid mail-enabled user and deliver to Dovecot or route elsewhere.
bofh@pat:~$ sudo apt install postfix postfix-ldap
The libnsl2 package is also installed as a dependency. installation will prompt for some parameters during the install which set the defaults for a new server. As this is a replacement for an existing email server well be changing these during configuration as shown later.
| Parameter | Value |
| General Mail Configuration | 3: Internet with Smarthost |
| System mail name | pat.example.com |
| SMTP relay host | out-block.example.com |
As soon as the configuration completed I stopped and disabled the postfix service as there's no point in having it running until we're ready to configure it.
The installation also creates a postfix system user and group so that most Postfix processes can run with minimal privilages. The appropriate file permissions are set during installation for us. On Ubuntu 24.04 LTS Postfix uses /var/spool/postfix for processing messages which only takes a couple of megabytes on a small server like these with a handful of active accounts so I'll keep that location.
Dovecot Install
Installing Dovecot is a little more involved as we need to configure the filesystem to be used for the mail store and an additional user to own them. As with Postfix we'll need to access a Samba 4 Active Directory so in addition to dovecot-core we'll also need the dovecot-ldap package to enable that. Users will access their mailboxes via IMAP4 which is handled by dovecot-imapd, I have no need to offer POP3 to them. To enable the use of sieve mail rules we'll need the dovecot-sieve and dovecot-managesieved packages and finally the dovecot-lmtpd package to enable Postfix to deliver inbound mail using LMTP.
bofh@pat:~$ sudo apt install dovecot-core dovecot-imapd dovecot-ldap dovecot-managesieved dovecot-sieve dovecot-lmtpd
The packages libexttextcat-2.0-0, libexttextcat-data, liblua5.4-0 and ssl-cert were all installed as dependencies and Dovecot doesn't prompt for any input during the installation. Dovecot also gets its own dovecot system user and dovecot group as well as a dovenull user and group for running most of its processes with minimal privilages.
Virtual mail user
On a normal, or maybe I should say traditional Linux (and unix) server users would be logging in directly to do their work so every mail user would also be a "real" user. However this is a dedicated email server and I really don't want people to be able to login and do stuff. To keep Dovecot happy (and this is how the book says to do it too) I need to create an imaginary user who will technically own all email and let Dovecot handle email accounts. For this I need a real user and group which in both cases I'll call it vmail as per the official examples but you can choose anything you like as long as you set the appropriate config in /etc/dovecot/ conf.d/10-mail.conf, conf.d/auth-ldap.conf.ext and conf.d/auth-passwdfile.conf.ext files and of course the mail store filesystem itself.
root@pat:~$ groupadd -g 5000 vmail
bofh@pat:~$ useradd -u 5000 -g vmail -c 'Dovecot MailDirs' -d /srv/dovecot -N -s /usr/sbin/nologin vmail
Nobody ever needs to login as the vmail user.
Filesystem Configuration
With the data disk mounted at /srv/dovecot we can now create the top level directories and assign the file permissions:
| Location | Permissions | Purpose |
| /srv/dovecot | 750 vmail:vmail | Base directory for our mailbox store |
| /srv/dovecot/backup | 750 root:root | For mounting snapshots during backup |
| /srv/dovecot/users | 750 vmail:vmail | User mailboxes |
| /srv/dovecot/public | 750 vmail:vmail | Public mailboxes |
| /srv/dovecot/shared | 750 vmail:vmail | Index and control files for mailbox shares |
| /srv/dovecot/sieve | 750 vmail:vmail | Global files for sieve filters |
I did briefly play with having each of the dovecot directories as separate subvolumes. While this might have some advantages for a large server, I couldn't see any benefit for a tiny test server or even the main house server that will be built next using the same pattern.
The backup process uses a script that create a btrfs read-only snapshot and mounts it under /srv/dovecot/backup for the duration of the backup job and then drops it once it completes which should mean we get a consistent backup of the mail store. I've also noticed that using a snapshot increases backup throughput by about 10%.
Dovecot Configuration
For preparing the migration this is [hopefully] easy. All the config is in git so I just need to merge my current branch for the sandpit host with the branch for this one, resolve the difference and we're done.
Current version: Dovecot 2.3.21.1 on Ubuntu 20.04 LTS
New version: Dovecot 2.3.21 on Ubuntu 24.04 LTS
The Dovecot configuration is spread over approximately 40 files and about a quarter of them touched during this configuration.
There are lots of different ways you can configure Dovecot. I'm keeping it relatively simple:
- Mail stored in filesystem layout in /srv/dovecot
- Internal human servers in an LDAP - Samba AD
- Other accounts for service held in a userdb file
- Users access mailboxes via IMAPS only (TLS encrypted on port 993)
While it is possible to configure encrypted mail storage using a couple of different approaches in Dovecot itself or using an encrypted filesystem this has implications for support and troubleshooting that I have yet to work through. Since email sent over the internet is inherently insecure anyway if anything sensitive has to be sent then using the OpenPGP end-to-end encryption in Thunderbird is much better in my opinion.
conf.d/10-auth.conf
Enables the required authentication methods by including the config of other files
Mostly changes to comments so kept the installed version. Uncommented the includes for auth-ldap.conf.ext and auth-passwdfile.conf.ext
conf.d/10-logging.conf
Configure the log files.
The default is syslog and I may well revert back to that later. For now I'm keeping the file method just a personal preference.
conf.d/10-mail.conf
I want to keep mail files in a structure that resembles the mail items and folders so that if I have to do a restore for somebody it's (hopefully) easier to find the missing messages. We also use the shared namespace so some common mailboxes are available to selected users with individual tracking of read indicators.
The default location is the older mbox file. Iv'e found Maildir with the fs layout is much more manageable, especially if somebody needs messages restored. The actual path to a users mail directory is specified in the user config.
The default inbox namespace for users mailbox is unchanged apart from specifying / as the hierarchy separator.
# Use the default namespace for saving subscriptions.
The shared namespace is already defined in comments so just needs to be enabled and also set to use FS layout. The INDEXPVT sets the location of the per-user store for which items have been read. I originally had a public namespace configured but it has fallen out of use so I'm not going to enable it in the new servers at this time.
This last change enables the vmail user and group we created to own the mailboxes.
conf.d/10-master.conf
This configures the services. I've left the IMAP service enabled for local users for now but will probably remove that to force all email connections to be encrypted rather than allowing people to make a bad choice.
This enables support for HAproxy protocol so Dovecots gets the real IP of external users accessing via the internet. It also defines a dedicated port for HAproxy to use. The internal address of the HAproxy server also need to be specified in conf.d/30-haproxy.conf otherwise it'll refuse to accept.
Enable a lmtp socket for Postfix to deliver mail to mailboxes.
Enable an auth socket too so Postfix can use Dovecot as its authentication service for sending email.
conf.d/10-ssl.conf
This controls TLS to enable secure communications.
Set the path to files for the private and signed public keys
This is a significant comment noting a change in the default so that only TLSv1.2 and TLS1.3 are now enabled by default.
conf.d/20-imap.conf
IMAP-specific settings
Enable the imap_acl plugin so we can configure permissions on shared mailboxes
conf.d/20-lmtp.conf
LMTP-specific settings
Enable the sieve plugin so incoming email can be filtered and sent to direct to folders of the users choice
conf.d/20-managesieve.conf
Configures the sieve management service
This is the default port for sieve management.
conf.d/30-haproxy.conf
Enable HAproxy support for external access. This is a custom file that is not in the default installation.
The IP address, apart from being illegal for the purposes of documentation, is the internal interface of the proxy host that connects to Dovecot
conf.d/90-acl.conf
Enable ACL support
Set the location of the shared dict
conf.d/90-sieve.conf
Configuration for the sieve service
Set the location of :global include scripts in sieve configurations
conf.d/auth-ldap.conf.ext
This sets the configuration to use for LDAP-based users
Override some of the output from the LDAP query using an override_fields, including setting the location of user mailboxes to /srv/dovecot/users/ [domain] / [name] and again force Mailder format with the FS layout
conf.d/auth-passwdfile.conf.ext
This enables the use of a file for non-LDAP email users such as applications that need to send notifications and test users.
The local users are stored in the /etc/dovecot/userdb/users file - which supports encryption of course. And just like the LDAP user configuration above we set the location of the mailboxes and storage layout. There was also a configuration here for a separate user database for public users which is no longer required.
userdb/users
Test users and those for other services that need to send alert or notification emails
The file format is very specific. Username followed by : and then the encrytion type in {} followed by the (encrypted?)password and then finally 6 more : to finish. Each user needs to be on a separate line too. The Dovecot doco has details of the supported encryption types and how to use use them, including using the dovadm command to do this.
For a live server I would expect passwords to always be encrypted. For a sandpit server that is blocked from sending to the internet then plain-text passwords are more convenient but will probably annoy auditors is you do this in real life.
dovecot-ldap.conf.ext
Internal users are specified in a Samba LDAP so I need to to connect to this using a auth bind so that Dovecot can get a pass/fail on username password. Dovecot just passes these to Samba and lets it make the decision if they're valid of not.
Since this is a Samba AD domain then we need to use sAMAccountName to find the correct user. I run an internal certificate authority for my GeekLab hosts and the build automation distributes the root certificate for this so we just need to set tls_ca_cert_file to point to the deployed certificate.
At this point, and providing the TLS certificate has been signed and uploaded, the Dovecot part of the server is ready to start and we can test out migrating the mailboxes.
Postfix Config
To be able to send emails via this host we need to configure the Postfix part of the server. The config for this is also held in a git repo it should also be just case of merging in the existing config and making a few adjustments. There's no data to migrate and the only storage it needs is queues for mail in transit and normally these will be very short lived.
Current version: Postfix 3.4.13 on Ubuntu 20.04 LTS
New version: Postfix 3.8.6 on Ubuntu 24.04 LTS
The Postfix config is held in 14 files although only 6 need attention. The configuration needs to:
- Accept email for mail-enabled accounts in the [sub-]domain and pass to Dovecot
- Send mail for other [sub-]domains to another mail server if submission was authenticated
- Optionally send mail to other domains via an external mail server if the submission was authenticated.
- User email submission using submissions (TLS encrypted on port 465)
So anything on the local subnet can send a message to
main.cf
Virtually the entire file shows a merge conflict as I reordered the parameters, but most of the values don't need to change.
The old server had compatibility level = 2. I don't see the point in keeping this and will make whatever changes are needed to the config to bring it up to date.
The hostname of the mail server is duplicated but this is a recommendation.
Set the log file. The debug isn't active and is a relic from the original migration from Exchange that I've left as a reminder in case it's needed in future.
Update: If something goes wrong (hint: it did!) then postfix doesn't always write to maillog. But if it's using syslog then it behaves properly, and with journctl it's now just as easy to get info from the syslog so I'm switching back to standard now.
We can use the same certificates that were created for Dovecot since this is running on the same host
This enables the use of SASL authentication using Dovecot to check if users are permitted using its combination of LDAP and local files that we configured earlier.
The virtual_mailbox_domains set the domain we're accepting email for - anything else is routed using either transport maps or as a last resort the virtual_transport. In this example the virtual_tansport uses a custom service added to master.cf to route to an external mail gateway which is set here to a nonexistent host called out-block to trap all outbound mail and stop them going anywhere. In Test and Live it gets routed via a specific account on an external mail provider that handles all external traffic.
I have 3 transport maps defined for routing mail internally
- users - test & service accounts defined in Dovecot userdb files
- ldap.cf - real humans
- default-relay - other known servers
Each of these is documented below under userdb/users, transport/ldap.cf and transport/default-relay respectively
The relayhost parameter must be explicitly set to empty to force routing via the transport maps above
To connect to external mail relays I use an authenticated connection. These parameters enable that and specify where to find the credentials needed via a password map at /etc/postfix/connectors/outbound
An additional configuration for Postfix 3.6 is adding restrictions that were previously configured in master.cf to the main.cf file so they're all in the one pace together.
These state that any email must be from an authenticated user. The reject clause is a fail-safe that forces the email to be rejected if no previous restriction allows it to be accepted
main.cf.proto
Kept all the text from the new install and and don't downgrade anything to the old version.
master.cf
I enable encrypted submissions (aka smtps) over TLS for accepting email and also a connection for outbound mail using submissions with TLS encryption.
This was previously known as smtps (and ssmtp) but has now been renamed submissions so I'm going to adopt the new name. It's a side-effect of one of the less than brilliant decisions about internet standards but happily it seems that common sense has prevailed.
This enables secure TLS connections to be made to the server to submit mail for delivery on port 465 (/etc/services specifies the submissions port) and to require the connection is authenticated. The smtp_sasl* parameters in main.cf passes authentication requests to Dovecot to handle, which in our configuration means checking its userdb files or Samba LDAP users.
The configuration of whether a user can submit email for delivery to this server or have it relayed to another is now specified in main.cf via the mua_relay_restrictions and mua_recipient_restrictions parameters just to keep this file a bit tidier in complex environments. On this server I allow anyone who can authenticate to do either.
This is an outbound connection called relay-smtps and requires the connection is encrypted and using TLS. The destination host and port is not specified here, only the method. Mail servers normally use SMTP over port 25 which only supports STARTTLS at best - and is susceptable to interference too.
# HAproxy connections
#
192.168.1.418:10465 inet n - y - - smtpd
-o syslog_name=postfix/10465
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o smtpd_sasl_type=dovecot
-o smtpd_sasl_path=private/auth
-o smtpd_upstream_proxy_protocol=haproxy
This enables support for external access for mail submission from an internet user via an HAproxy server, with Postfix getting the real IP address of the connecting client via the haproxy protocol for logging (and ban-hammering if needed). For the new host I've removed the IP address as forgetting to update that causes Postfix to fail on start - and with nothing in the logs if you've set maillog to a file. Which is somewhat annoying...
I don't expose SMTP to the internet - and most residential ISPs block that anyway in the never-ending battle against the spammers. I'll cover this in a separate article.
The above were all originally active by default and are now inactive by default. Since I don't use them I'm going with the new default.
userdb/users
This file must contain the same users as match the userdb/users file of local users maintained in Dovecot. This file is not directly usable by Postfix and needs to be hashed to users.db using postmap [path]/users first. Postfix will then pick up the changes automatically.
This is used as a transport map in main.cf and tells Postfix to route email for the listed users to Dovecot using the LMTP connection.
transport/ldap.cf
This configuration is the second transport map used for internal routing and performs an LDAP search to find the user. If successful it returns a transport map for that user
The LDAP host to use is specified in server_host and this is an encrypted LDAPS connection to a Samba server on port 636.
We use a bind to the LDAP using the same user and password as used for Dovecot and search for users in the same place too.
The query_filter is looking for a user with the sAMAccountName matches the local part of the email address and the mail properly of that user matches the full email address and is also a member of the MailPostfix group. If such a user exists in the LDAP the sAMAccountName is used to return a transport map specifying a LMTP connection to a unix socket at private/dovecot-lmtp to pass the message to Dovecot
transport/default-relay
The final transport map to be checked handles routing to other mail servers.
Anything destined for any other example.com account gets routed to hermes.example.com to deal with. Anything else is routed via the last entry of * which will attempt to route to a mail server which doesn't exist forcing a failure. On the Test and Live servers this goes via an the Mythic Beasts mail service via an authenticated connection using SMTPS or submissions depending on which manual you read - they're both the same thing.
connectors/outbound
This file holds connection info for external connections. It should have permisions 640 root:root since the credentials are not encrypted. This file also needs to be converted to a lookup table using postmap after any changed e.g. postmap outbound
In this example any connection specified as [xxx.example.com]:465 in other Postfix configuration files will be attempted with senderusername as the username and SecretPasswordHere as the password. The first part of each entry in this file must be an exact match, so if the connect you want to store credentials for is specified as both xxx.example.com and [xxx.example.com]:465 in the config you'd need an entry here for both variations.
The remaining files (master.cf.proto, post-install, postfix-files and postfix-script) only have changes to comments by the packagers so they were all left alone.
Next Steps
With the server (allegedly) ready the next step is to start the services and fix any issues and then test it works and I can send mail between test users on this server, other internal servers and then to other external accounts.
Once I'm happy it actually works I can then migrate mailboxes and update users to connect to the new server instead. This is covered in Migrating Dovecot mailboxes to a new server.