Skip to content

Dovecot and Postfix

The Dovecot connector migrates a self-hosted server on which Dovecot serves IMAP and ManageSieve and Postfix handles SMTP. It reads every mailbox over IMAP and every Sieve script over ManageSieve, signed in through a Dovecot master user, so no user password is needed. Accounts and their password hashes come from Dovecot’s user database, and aliases, mailing lists, catch-all addresses and domain aliases from the Postfix maps, both read through host access. Products built on Dovecot and Postfix with a connector of their own, such as mailcow or Mail-in-a-Box, use their own page.

Both cutover strategies work with this source. A zero-downtime migration suits it best: the operator controls the server’s network and ports, so the Stalwart proxy can sit in front of it, and Dovecot and Postfix keep accepting direct connections while they trust the proxy, which keeps the source settings simple. A one-shot migration moves each domain at its MX change instead, and needs none of the proxy mode settings.

Mail is copied over IMAP with the folder tree of the personal namespace; messages flagged as deleted are skipped. Sieve scripts are copied over ManageSieve when a ManageSieve URL is set, and the active script stays active. Vacation replies and filters exist on this source only as Sieve scripts, so they move with the scripts. The source has no calendars, contacts, files or sender identities for Vandelay to read.

Accounts come from the user database, a passwd-file by default. Each entry gives the account’s address, its login forms (the address and the bare user name), whether it is disabled (nologin), its quota from quota_rule, userdb_quota_rule or quota_storage_size, and its password hash. The passwd-file holds no display names. A hash is carried when Stalwart supports its scheme, as described in Password hashes; with a CSV user database, no hash is read and the account receives a temporary password or is left to the external identity provider.

Directory data comes from Postfix’s virtual_alias_maps. A key with a single recipient that is an account becomes an alias of that account, and a key with several recipients, or with one recipient that is not an account, becomes a mailing list. An @domain key whose only recipient is @other.example becomes a domain alias, and one whose only recipient is an address becomes the domain’s catch-all. Keys without a domain are expanded over the mydestination and myorigin domains. With a CSV user database, the Postfix maps are not read: aliases come only from the CSV’s aliases column, and no lists, catch-all addresses or domain aliases are migrated.

DKIM keys are carried, in a server-scope project, from OpenDKIM when “OpenDKIM KeyTable” names OpenDKIM’s KeyTable file, the file that the KeyTable setting of opendkim.conf points to, not opendkim.conf itself: Vandelay reads the table and the key files it names, with the selector from the table. Keys written inline in the KeyTable are not read.

Vandelay signs in as each account with a Dovecot master user, an account that may open any mailbox with its own password. Create it on the source server before testing access. The same steps are printed by vandelay migrate guide dovecot-master and shown on the Source: access step of the web UI and the TUI.

  1. Add the master user to a password file that only Dovecot can read. doveadm pw prompts for the password and prints its hash, and >> keeps any master users the file already holds:

    Terminal window
    echo "vandelay:$(doveadm pw -s SHA512-CRYPT)" >> /etc/dovecot/master-users
    chown root:dovecot /etc/dovecot/master-users
    chmod 640 /etc/dovecot/master-users
  2. Add the master password database to the Dovecot configuration. With result_success = continue, Dovecot still looks the target account up after the master user has authenticated, so a login for an account that does not exist fails. For Dovecot 2.4:

    auth_master_user_separator = *
    passdb master-users {
    driver = passwd-file
    passwd_file_path = /etc/dovecot/master-users
    master = yes
    result_success = continue
    }

    For Dovecot 2.3:

    auth_master_user_separator = *
    passdb {
    driver = passwd-file
    master = yes
    args = /etc/dovecot/master-users
    result_success = continue
    }
  3. When the ACL plugin is enabled, make Dovecot check the rights of the account being opened rather than those of the master user, who has no rights on other users’ folders. Without this setting, the master login succeeds but lists no folders. For Dovecot 2.4, acl_user = %{user}; for Dovecot 2.3:

    plugin {
    acl_user = %u
    }
  4. Reload Dovecot with doveadm reload, and check a master login with doveadm auth test '[email protected]*vandelay'.

The separator * is the default “Master login format” in Vandelay, so the login that Vandelay sends for a user database entry [email protected] is [email protected]*vandelay. The master user form and its alternatives are described in Master user. The master user exists only for the migration, and is removed when the source is decommissioned.

Vandelay reads the user database, the Postfix configuration and the OpenDKIM files from the source’s file system, either on the source itself or over SSH, as described in Host access. Without host access, discovery of a passwd-file user database fails. The user that Vandelay signs in as must be able to read:

  • The user database, /etc/dovecot/users unless “Dovecot user database file” names another one. A path that contains Dovecot variables, such as %d, cannot be read; such a setup lists its accounts in a CSV user database instead. A user database of more than 200,000 entries is refused, and discovery fails with more than 200000 entries.
  • The Postfix configuration directory, with main.cf and the files and tables its virtual_alias_maps, virtual_mailbox_maps and related parameters name.
  • The OpenDKIM KeyTable and every key file it names, when DKIM keys are carried.

Vandelay also runs doveconf on the host to read the ACL settings, and the shared-folder dictionary when the ACL plugin is enabled, so that folder sharing can be reported. doveconf does not run against a mounted root other than /, and the shares are then reported as not listed.

The Vandelay host must reach the source’s IMAP port and, for Sieve scripts, its ManageSieve port (4190 by convention) directly. In a zero-downtime migration, these are the source’s own addresses and never the proxy’s: Vandelay’s sessions must not be held or closed by a switch. A self-signed IMAP certificate is accepted with a certificate pin.

In a zero-downtime migration, the source sees two kinds of connections that it did not see before: client connections from the proxy, and mail relayed from Stalwart for the accounts still on the source. Vandelay configures nothing on the source for this, so the settings below are made by hand. They are the reference for the other Postfix-based systems, whose pages add only their own differences.

The examples use 203.0.113.10 for the proxy and 203.0.113.20 for Stalwart. dovecot --version shows the installed version, and doveconf -n prints the effective configuration.

The proxy announces each client’s address to this source with the IMAP ID command and with XCLIENT on POP3, ManageSieve and SMTP. Dovecot and Postfix accept the announcement only from a trusted address. Without the trust, logins still succeed, but both servers record the proxy as the client, which affects logging and any policy based on the client address, such as brute-force protection that may end up blocking the proxy. Trust only the proxy’s address, so that no other host can present a forged client address.

In Dovecot, add the proxy’s address to the trusted login networks, in dovecot.conf or a file under conf.d. The setting has the same name in Dovecot 2.3 and 2.4:

login_trusted_networks = 203.0.113.10/32

Dovecot also treats connections from these networks as secured, so it accepts plaintext authentication from them even without TLS. Listing only the proxy’s address keeps that exception to the proxy.

In Postfix, allow the proxy to send XCLIENT, in main.cf or with postconf -e:

smtpd_authorized_xclient_hosts = 203.0.113.10/32

Trusting the announcements does not change how other connections are served: Dovecot and Postfix keep accepting direct connections, which is also how Vandelay keeps reading the source during the migration. To confirm that the trust is in effect, sign in through the proxy once it runs and read the Dovecot login line: its rip= field shows the client’s address when the announcement is honoured, and the proxy’s address when it is not.

The proxy’s destination for this source can send a PROXY protocol header instead of XCLIENT. Dovecot then trusts the header through haproxy_trusted_networks and haproxy = yes on each inet_listener that receives proxied connections. Such a listener disconnects every client that is not in haproxy_trusted_networks, so Vandelay must then read the source through a listener without the flag, set as “Old server internal address” (proxy.source_internal).

From the moment the proxy goes live, Stalwart receives the domain’s inbound mail and relays every message for an account still on the source to Postfix on port 25, in an ordinary SMTP session from Stalwart’s own address. Postfix already delivers mail for its own domains, so the delivery path needs no change.

The checks need attention. The message has already been checked by Stalwart as the public mail exchanger, and Postfix, a policy service or a content filter would now check SPF, DKIM, DMARC or the client’s reputation again, against Stalwart’s address. These checks frequently fail, and the message is rejected or classified as spam. Stalwart’s address is therefore exempted from them on the source.

The broad form adds Stalwart to mynetworks, appended to the existing value:

mynetworks = 127.0.0.0/8 [::1]/128 203.0.113.20/32

This grants more than the exemption: a host in mynetworks passes every restriction that starts with permit_mynetworks, which in the default smtpd_relay_restrictions includes relaying mail to any destination. Stalwart needs no relay right, because it only delivers mail for the source’s own domains.

The narrow form lists Stalwart’s address in a check_client_access table that returns OK, placed at the start of the restriction lists that perform the checks, such as smtpd_client_restrictions and the part of smtpd_recipient_restrictions before the SPF policy service. The Postfix access(5) manual describes the table format.

Content filters such as rspamd or SpamAssassin score the message independently of the Postfix restrictions, so Stalwart’s address must also be trusted there, as the filter’s documentation describes for internal relay hosts. A greylisting service delays relayed mail in the same way and needs the same exemption. When reject_non_fqdn_helo_hostname is enabled, Stalwart must be exempted from it too, in case it greets with an internal host name.

This step is optional. While the migration lasts, mail that accounts on the source send to other domains can leave through Stalwart, so that all outgoing mail uses one path and one sending address. Stalwart must then accept relayed mail from the source’s address, as described in Outbound relay for the source. Postfix sends mail for remote domains to Stalwart when Stalwart is its relay host, while local and virtual domains keep their transports:

relayhost = [203.0.113.20]:25

The brackets stop Postfix from looking up MX records for the relay host. A deployment that already routes with transport_maps adds the same next hop there, for remote domains.

Mail from one account on the source to another is delivered locally by Postfix, also when the recipient has already switched to Stalwart. Vandelay copies such mail to Stalwart with its catch-up passes, as described in Delta passes and catch-up, so it arrives with a delay that depends on the catch-up schedule.

A transport entry for each switched account delivers this mail to Stalwart directly instead. The entries go into a table named by transport_maps:

transport_maps = hash:/etc/postfix/transport

The table holds one line per switched account, added after the account’s switch:

[email protected] smtp:[203.0.113.20]:25

The table is compiled with postmap /etc/postfix/transport, and takes effect on postfix reload. A deployment that already sets transport_maps adds the entries to its existing table.

Every change in this section takes effect with doveadm reload and postfix reload, and none of them changes how existing clients are served: the trust settings only add what the proxy may announce, and the relay exemption only relaxes checks for one address. They can be applied ahead of time or when the proxy goes live. Once the proxy is gone, they are removed together with the master user: login_trusted_networks and the master passdb from Dovecot, and smtpd_authorized_xclient_hosts, Stalwart’s exemption, any relayhost and the transport entries from Postfix.

The source’s endpoints are “IMAP URL” (source.imap, --imap-url), as imaps://host[:port] for implicit TLS or imap://host[:port] for STARTTLS, and “ManageSieve URL” (source.sieve, --sieve-url), as sieve://host[:port] or sieves://host[:port]. Without a ManageSieve URL, Sieve scripts are not copied.

The master user is “Master user” (source.master.name, --master-user) and “Master, admin or service account password” (source.master.password, --master-password), a secret reference. A master user is optional for this connector, but without one every account needs its own password, which only a CSV user database can provide.

Host access is “Server file access” (source.host.kind, --host-access) with its root directory or SSH destination. The files are set by the advanced fields:

  • “Dovecot user database” (source.userdb.driver, --userdb): passwd-file, or csv for an accounts CSV on the Vandelay host.
  • “Dovecot user database file” (source.userdb.path, --userdb-path).
  • “Postfix configuration directory” (source.postfix, --postfix), or false when the host runs no Postfix.
  • “OpenDKIM KeyTable” (source.opendkim, --opendkim), the path of the OpenDKIM KeyTable, empty when DKIM is signed elsewhere.
  • “Default domain” (source.default_domain, --default-domain), for passwd-file users written without a domain that match no Postfix virtual mailbox.

The following excerpt reads a Dovecot 2.4 server over SSH, with the master password in a file of the project directory:

[source]
system = "dovecot"
imap = "imaps://mail.example.org"
sieve = "sieve://mail.example.org:4190"
postfix = "/etc/postfix"
opendkim = "/etc/opendkim/KeyTable"
cleartext = "forbid"
[source.master]
name = "vandelay"
password = "file:secrets/master-password"
form = "*"
[source.host]
kind = "ssh"
destination = "[email protected]"
[source.userdb]
driver = "passwd-file"
path = "/etc/dovecot/users"

The following command sets the same source:

Terminal window
vandelay migrate source \
--system=dovecot \
--imap-url=imaps://mail.example.org \
--sieve-url=sieve://mail.example.org:4190 \
--master-user=vandelay \
--master-password=file:secrets/master-password \
--host-access=ssh \
--opendkim=/etc/opendkim/KeyTable

Discovery and the copy report what this source has but Stalwart does not receive. Pre-flight lists the discovery losses under “Items that cannot be migrated”, and the “Losses” table of “Verify and report” lists them all.

  • Forwarding. A Postfix alias that forwards an account’s mail is not carried as a redirect rule. The loss names the targets, for example Postfix virtual_alias_maps forwarding is not migrated; recreate it as a redirect rule: [email protected]; the source also delivered a local copy, and the rule is recreated on Stalwart by the user or the administrator.
  • Folder sharing. With the ACL plugin, each share is reported as <owner>: shares IMAP folders with <member>; folder sharing is not migrated, so share them again on the target. When the shares cannot be listed, for example without host access or on Dovecot 2.4, whose acl_sharing_map Vandelay does not read, one loss says so with the reason.
  • Accounts on another server. A user database entry whose login Dovecot refers or proxies to another server (nologin with host, proxy, or proxy_maybe to another host) keeps its mailbox there. It is left out, with a loss such as [email protected]: mailbox on backend2.example.org (Dovecot proxy, /etc/dovecot/users:12); migrate it from that server. A proxy_maybe entry counts as local only when its host is the host of the IMAP URL or an address that name resolves to, its port is absent or the IMAP URL’s port, and its destuser is absent or the entry’s own name. These accounts are migrated by a second project whose source is the server that holds them, into the same Stalwart target.
  • DKIM keys that cannot be read. A key file that is missing, unreadable to the host access user, not a usable private key, or a key written inline in the KeyTable, is reported per domain and selector, for example example.org: DKIM key mail is not carried (the host access user cannot read /etc/opendkim/keys/example.org/mail.private); .... The loss explains the choices: make the key readable and discover again, or set the domain to generate or skip in “DKIM keys” and publish the new key’s record. This matters only in a server-scope project; an account-scope project carries no DKIM keys and generates none. A domain with several selectors in the KeyTable is not carried, because the SigningTable is not read.
  • Postfix entries that cannot be represented. Alias keys that are not addresses, recipients of the form @domain in a list, keys repeated in the maps, and tables that cannot be read are each reported with their file and line. Vandelay reads the source file of hash:, btree:, lmdb:, cdb:, dbm:, sdbm: and texthash: tables at an absolute path; tables of other types, such as mysql:, ldap:, pcre: or regexp:, are reported as <parameter>: the lookup table "<table>" cannot be read; its entries are not migrated.

The access test or pre-flight reports <address>: signed in as the master user (login separator) but no folder was listed, followed by a hint about acl_user. The master login succeeded, but the ACL plugin checks the master user’s rights, and the master user has none on the account’s folders. Set acl_user as described in Master user, reload Dovecot and run the test again.

the passwd-file userdb needs host access to the source server means that no host access is configured; set “Server file access”. <path>: the userdb path uses Dovecot variables, which are not supported; use a CSV userdb means that the user database is split per domain or user, which Vandelay cannot enumerate; list the accounts in a CSV user database instead.

the login "<login>" contains the master user separator '*' means that an account’s login or the master user’s name contains the separator. Choose another separator in “Master login format” and set the same character in auth_master_user_separator, or use authzid.

NAMESPACE failed (<reason>); without it the folders of other users and shared folders cannot be told apart, so the account would not be imported means that the source refused the IMAP NAMESPACE command for the master login. Allow it for that login, and check the master user’s ACL and namespace configuration.