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:

sandpit:
  vars:
    config_landscape_tier: "S"
    esx_network: Hall-Sandpit
  children:
    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

#!include auth-deny.conf.ext
#!include auth-master.conf.ext

!include auth-ldap.conf.ext
!include auth-passwdfile.conf.ext
!include auth-system.conf.ext

#!include auth-sql.conf.ext
#!include auth-ldap.conf.ext
#!include auth-passwdfile.conf.ext
#!include auth-checkpassword.conf.ext
#!include auth-static.conf.ext

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.

#log_path = syslog
log_path = /var/log/dovecot.log

# Log file to use for informational messages. Defaults to log_path.
#info_log_path =
info_log_path = /var/log/dovecot-info.log

# Log file to use for debug messages. Defaults to info_log_path.
#debug_log_path =
debug_log_path = /var/log/dovecot-debug.log

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.

 

#mail_location = mbox:~/mail:INBOX=/var/mail/%u
mail_location = maildir:~/Maildir:LAYOUT=fs

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.

 

namespace inbox {
# Namespace type: private, shared or public
type = private

# Hierarchy separator to use. You should use the same separator for all
# namespaces or some clients get confused. '/' is usually a good one.
# The default however depends on the underlying mail storage format.
separator = /

The default inbox namespace for users mailbox is unchanged apart from specifying / as the hierarchy separator.

 

namespace {
type = shared
separator = /

# Mailboxes are visible under "shared/user@domain/"
# %%n, %%d and %%u are expanded to the destination user.
prefix = Shared/%%u/

# Mail location for other users' mailboxes. Note that %variables and ~/
# expands to the logged in user's data. %%n, %%d, %%u and %%h expand to the
# destination user's data.
#location = maildir:%%h/Maildir:INDEX=~/Maildir/shared/%%u
location = maildir:%%h/Maildir:INDEXPVT=~/indexpvt/shared/%%u:LAYOUT=fs

# Use the default namespace for saving subscriptions.
subscriptions = no

# List the shared/ namespace only if there are visible shared mailboxes.
#list = children
list = yes
}

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.

 

# System user and group used to access mails. If you use multiple, userdb
# can override these by returning uid or gid fields. You can use either numbers
# or names. <doc/wiki/UserIds.txt>
mail_uid = vmail
mail_gid = vmail

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.

service imap-login {
 
 
inet_listener imaps_haproxy {
port = 10993
ssl = yes
haproxy = yes
}
}

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.

 

service lmtp {
# unix_listener lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
user = postfix
group = postfix
mode = 0600
}

Enable a lmtp socket for Postfix to deliver mail to mailboxes.

 

service auth {
 
# Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
user = postfix
group = postfix
mode = 0666
}
}

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.

ssl_key = </etc/ssl/private/server-key.pem
ssl_cert = </etc/ssl/certs/server-chain.pem

Set the path to files for the private and signed public keys

 

# Dovecot also recognizes values ANY and LATEST. ANY matches with any protocol
# version, and LATEST matches with the latest version supported by library.
#ssl_min_protocol = TLSv1.2

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

 


protocol imap {
# Space separated list of plugins to load (default is global mail_plugins).
mail_plugins = $mail_plugins imap_acl

}

Enable the imap_acl plugin so we can configure permissions on shared mailboxes

conf.d/20-lmtp.conf

LMTP-specific settings

protocol lmtp {
# Space separated list of plugins to load (default is global mail_plugins).
mail_plugins = $mail_plugins sieve
}

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

protocols = $protocols sieve

# Service definitions

service managesieve-login {
inet_listener sieve {
port = 4190
}
}

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.

##
## HAproxy specific settings
##

haproxy_trusted_networks = 192.168.1.418
haproxy_timeout = 3s

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

plugin {
#acl = vfile:/etc/dovecot/global-acls:cache_secs=300
acl = vfile
}
 
# To let users LIST mailboxes shared by other users, Dovecot needs a
# shared mailbox dictionary. For example:
plugin {
#acl_shared_dict = file:/var/lib/dovecot/shared-mailboxes
acl_shared_dict = file:/srv/dovecot/shared/sharedmailboxes.db
}

Set the location of the shared dict

conf.d/90-sieve.conf

Configuration for the sieve service

plugin {
 
 
#sieve_global =
sieve_global = /srv/dovecot/sieve/
}

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

 

userdb {
driver = ldap
args = /etc/dovecot/dovecot-ldap.conf.ext
 
# Default fields can be used to specify defaults that LDAP may override
#default_fields = home=/home/virtual/%u

override_fields = uid=vmail gid=vmail home=/srv/dovecot/users/%d/%n mail=maildir:~/Maildir:LAYOUT=fs
}
 

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.


passdb {
driver = passwd-file
# args = scheme=CRYPT username_format=%u /etc/dovecot/users
args = username_format=%u /etc/dovecot/userdb/users
}

userdb {
driver = passwd-file
# args = username_format=%u /etc/dovecot/users
args = username_format=%u /etc/dovecot/userdb/users

# Default fields that can be overridden by passwd-file
# default_fields = quota_rule=*:storage=1G

# Override fields from passwd-file
# override_fields = home=/home/virtual/%u
override_fields = uid=vmail gid=vmail home=/srv/dovecot/users/%d/%n mail=maildir:~/Maildir:LAYOUT=fs
}

 

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

# Dovecot Users - Local mailboxes
# Ensure users also in Postfix transport table testusers
dovetest1@domain:{PLAIN}doveishZZ9::::::
dovetest2@domain:{PLAIN}doveishZZ9::::::

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.

# LDAP URIs to use. You can use this instead of hosts list. Note that this
# setting isn't supported by all LDAP libraries.
uris = ldaps://sambaserver.example.com:636

# Distinguished Name - the username used to login to the LDAP server.
# Leave it commented out to bind anonymously (useful with auth_bind=yes).
dn = CN=DovecotBindUser,CN=Users,DC=domain,DC=example,DC=com

# Password for LDAP server, if dn is specified.
dnpass = SecretDovecotLDAPpassword


# Use TLS to connect to the LDAP server.
tls_ca_cert_file = /etc/ssl/certs/My_RootCA.crt
 

auth_bind = yes

 

# LDAP base. %variables can be used here.
# For example: dc=mail, dc=example, dc=org
base = OU=SBSUsers,OU=Users,OU=MyBusiness,DC=domain,DC=example,DC=com


#user_filter = (&(objectClass=posixAccount)(uid=%u))
user_filter = (&(objectClass=user)(sAMAccountName=%n))


#pass_filter = (&(objectClass=posixAccount)(uid=%u))
pass_filter = (&(objectClass=user)(sAMAccountName=%n))

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 This email address is being protected from spambots. You need JavaScript enabled to view it. If it used an authenticated connection - which means I've created an account for it - it can also send email to This email address is being protected from spambots. You need JavaScript enabled to view it. and possibly even to This email address is being protected from spambots. You need JavaScript enabled to view it. if I've enabled external access for the mail server. I usually block the ability to send email externally for sandpit domains just to prevent accidents.

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.

# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 3.6 on
# fresh installs.
compatibility_level = 3.6

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.

myhostname = pat.domain.example.com
mydestination = $myhostname, pat.domain.example.com, localhost.domain.example.com, localhost

The hostname of the mail server is duplicated but this is a recommendation.

#===================
# Housekeeping

#maillog_file = /var/log/postfix.log
#debug_peer_list = 192.168.24.418

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.

#===================
# TLS parameters - Server


smtpd_tls_cert_file=/etc/ssl/certs/server-chain.pem
smtpd_tls_key_file=/etc/ssl/private/server-key.pem
 

We can use the same certificates that were created for Dovecot since this is running on the same host

#====================
# User Authentication
# SASL against Dovecot. Known users will then be able to login and relay.

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes

smtpd_sasl_security_options = noanonymous

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.

#=====================
# Define mail domains

virtual_mailbox_domains = domain.example.com

virtual_alias_maps = hash:/etc/postfix/userdb/aliases

# Transport maps will route mail to appropriate destination
# virtual_transport will route unknowns back to MB to handle public aliases in CC:
# Also include entry in transport maps wth : for nexthop to stop it overriding
 
virtual_transport = relay-smtps:[out-block.domain.example.com]:465

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.

#=====================
# Define transport maps to route mail.

transport_maps =
hash:/etc/postfix/userdb/users
ldap:/etc/postfix/transport/ldap.cf
hash:/etc/postfix/transport/default-relay

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

#=====================
# Relay outbound traffic via transport map instead so we can use SMTPS without breaking other routing.

relayhost =

The relayhost parameter must be explicitly set to empty to force routing via the transport maps above

#=====================
# Authentication for mail servers, eg upstream.

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/connectors/outbound
smtp_sasl_security_options =

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.

#===================
# Client restrictions in master.cf, new style for 3.6
# Used for: submissions
mua_recipient_restrictions = permit_sasl_authenticated,reject
mua_relay_restrictions = permit_sasl_authenticated,reject

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.

 

# Choose one: enable submissions for loopback clients only, or for any client.
#127.0.0.1:submissions inet n - y - - smtpd
submissions inet n - y - - smtpd
-o syslog_name=postfix/submissions
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
# -o local_header_rewrite_clients=static:all
# -o smtpd_reject_unlisted_recipient=no
# Instead of specifying complex smtpd_<xxx>_restrictions here,
# specify "smtpd_<xxx>_restrictions=$mua_<xxx>_restrictions"
# here, and specify mua_<xxx>_restrictions in main.cf (where
# "<xxx>" is "client", "helo", "sender", "relay", or "recipient").
# -o smtpd_client_restrictions=
# -o smtpd_helo_restrictions=
# -o smtpd_sender_restrictions=
# -o smtpd_relay_restrictions=
# -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o smtpd_relay_restrictions=$mua_relay_restrictions
# -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o smtpd_recipient_restrictions=$mua_recipient_restrictions
# -o milter_macro_daemon_name=ORIGINATING

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.

relay-smtps unix - - n - - smtp
# Connect to SMTPS Server
-o smtp_tls_security_level=encrypt
-o smtp_tls_wrappermode=yes

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. 

 

# Other external delivery methods.
#
#ifmail unix - n n - - pipe
# flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
#bsmtp unix - n n - - pipe
# flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
#scalemail-backend unix - n n - 2 pipe
# flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
#mailman unix - n n - - pipe
# flags=FRX user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py ${nexthop} ${user}

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.

 

# Dovecot local users - target= lmtp:unix:private/dovecot-lmtp
# Must match dovecot local users eg /etc/dovecot/userdb/users
# Compile with postmap to be effective.


# Validation users during commisioning
dovetest10@domain lmtp:unix:private/dovecot-lmtp
dovetest20@domain lmtp:unix:private/dovecot-lmtp

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

# Generate a transport map entry for requested email, if user exists and is member of MailPostfix group

# AD server
server_host = ldaps://sambaserver.example.com:636

# Samba needs start_tls or it must set ldap server require strong auth = no
# start_tls = yes

# AD needs a bind
bind = yes
bind_dn = CN=DovecotBindUser,CN=Users,DC=domain,DC=example,DC=com
bind_pw = SecretDovecotLDAPpassword

# Search real users
search_base = OU=SBSUsers,OU=Users,OU=MyBusiness,DC=domain,DC=example,DC=com

# Return lmtp parameter if user exists
query_filter = (&(objectClass=user) (sAMAccountName=%u) (mail=%s) (memberOf=CN=MailPostfix,OU=Security Groups,OU=MyBusiness,DC=domain,DC=example,DC=com))
result_attribute = sAMAccountName
result_format = lmtp:unix:private/dovecot-lmtp

 

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.

# Postfix
# Internal mail routing connections
# Default transport to replace relayhost setting with
# user@domain target

# Virtual_transport handles unknown domain.example.com users
domain.example.com :

# Other internal domains
example.com smtp:[hermes.example.com]
.example.com smtp:

# All others via Mythic Beasts
* relay-smtps:[out-block.example.com]:465

 

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

# Postfix
# Credentials for outbound connections
# {[Full connection]:port} username:password
[xxx.example.com]:465 senderusername:SecretPasswordHere

 

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.