mjl- / mox

modern full-featured open source secure mail server for low-maintenance self-hosted email
https://www.xmox.nl
MIT License
3.55k stars 100 forks source link

Feature Suggestion: Disabling Domains and Accounts #175

Open RobSlgm opened 4 months ago

RobSlgm commented 4 months ago

Description

I'm thrilled to share that I recently migrated some email domains from a Postfix/Dovecot setup to Mox. It's been a pleasure using this modern solution over the past few weeks. While making the switch, I gained some insights that I'd like to propose as potential improvements.

Currently, when migrating domains between email servers using Mox, there's a challenge:

Proposed Solution: Disabling Domains and Accounts

This feature introduces two flags for disabling domains and accounts during migration:

  1. Disable Flag for Mailboxes/Accounts
    • When set, Mox will:
      • Reject incoming emails from external senders.
        • Disallow sending emails from the associated mailboxes.
        • Mimic the behavior of a non-existent email address.
    • Important: This flag should NOT affect:
      • Importing/exporting emails.
      • IMAP access (to check migration with a mail client; or using tools such imapsync).
      • Access to the admin pages or mox commands
  2. Disable Flag for Domains
    • When set, Mox will:
      • Configure the domain with necessary keys (like current behavior).
      • NOT establish or allow external connections.
    • This effectively disables all newly created accounts/mailboxes within the domain, inheriting the behavior of the "disable flag for mailboxes" described above.
    • Purpose:
      • Allows setting up the correct DNS entries (DKIM, etc.) and preparing the DNS configuration before activating the domain.
      • Bulk data migration

Benefits

This feature simplifies domain migrations by:

mjl- commented 3 months ago

Thanks @RobSlgm, functionality to help with migrations makes sense.

Indeed, preventing incoming/outgoing SMTP, while allowing access to messages with IMAP (for copying data) or direct import/export seems useful.

For SMTP, we would reject outgoing messages from disabled accounts/domains during submission. For incoming messages, I'm not sure if it is all that great to have the default behaviour: it would cause messages to be permanently rejected, i.e. senders would get a bounce and not retry again. If messages are coming in, presumably the switch has been made and messages should be accepted. If messages should be rejected, perhaps a temporary error code would be better, so senders will retry up to a day (can be the time needed to get the setup ready). For disabled domains, we wouldn't send any outgoing TLS/DMARC reports.

Perhaps it's enough to only mark domains as disabled? All addresses for that domain at any account would automatically be treated as disabled. An account could have an address at an already-migrated domain (that accepts incoming and outgoing deliveries), and another address that is still disabled. Or are there advantages to marking only some address/accounts of a domain as disabled? Did you migrate accounts one at a time? I would think you'ld enable the domain about when you update the MX record.

We may also want to provide more help during the DNS migration. We currently provide a (pretty big) list of DNS records to set. Easy for new domains, but domains that will be migrated will already have records. We may want to provide some guidance on the order in which to make changes, also keeping DNS TTLs and possibly any MTA-STS policy lifetime.

What do migrations commonly look like? I think my migration went like this:

Perhaps there are good IMAP syncing tools to migrate messages. If it has full sync support, it can incrementally copy over any messages that are still arriving at the old SMTP server.

DNS-related changes to make for a migration:

Preparation:

Switch:

After:

Curious if you had the same or different approach. How did you verify the domain config at the new mox machine was correct? I can imagine you would want to briefly activate a domain to see if incoming messages will be accepted properly, without updating the MX record yet.

Anyway, suggestions for DNS changes could just be documentation. At a later stage, it would be nice to query the existing DNS config, suggest the specific changes to make, and possibly even making them (with a click of the button). But we need DNS updating capabilities first, that's still a way off.

For now, just marking a domain as disabled for sending/receiving email seems like a good first step.

RobSlgm commented 3 months ago

For SMTP, we would reject outgoing messages from disabled accounts/domains during submission. For incoming messages, I'm not sure if it is all that great to have the default behaviour: it would cause messages to be permanently rejected, i.e. senders would get a bounce and not retry again. If messages are coming in, presumably the switch has been made and messages should be accepted. If messages should be rejected, perhaps a temporary error code would be better, so senders will retry up to a day (can be the time needed to get the setup ready). For disabled domains, we wouldn't send any outgoing TLS/DMARC reports.

Agree. temporary error is the right way for incoming mail.

Disabled domain should also not request ACME certificates, the ACME challenge should be done after activation.

Perhaps it's enough to only mark domains as disabled? All addresses for that domain at any account would automatically be treated as disabled. An account could have an address at an already-migrated domain (that accepts incoming and outgoing deliveries), and another address that is still disabled. Or are there advantages to marking only some address/accounts of a domain as disabled? Did you migrate accounts one at a time? I would think you'ld enable the domain about when you update the MX record.

Yes, as described it makes more sense to disable domains only. No, because I was confused about an other issue:

The big obstacle wasn't the DNS switch-over, but supporting the users. My old and new server setup has a main domain for the mail server while all the mailboxes are related to other domains. I choose a new main domain for Mox to simplify the migration as I found no working scenario for an in-place migration (i.e. using the same main domain).

After setting password was done individually for each user I was wishing to be able to disable the IMAP access on mailbox level on the old system. Some users had still access on a different mail client to the old mailbox. So the suggestion should properly be: Disable mailboxes or accounts for IMAP access.

What do migrations commonly look like? I think my migration went like this:

  • Reduce DNS TTLs for relevant DNS records.
  • Set up new machine with new mox instance.
  • Add/update most DNS records for domain.
  • Create accounts.
  • Switch over MX record. Should be picked up quickly with short TTLs.
  • Shut down old SMTP server.
  • New messages now start coming in at new machine.
  • Copy messages from old to new machine. Import messages into accounts.
  • More DNS changes (e.g. request DMARC reports).

Perhaps there are good IMAP syncing tools to migrate messages. If it has full sync support, it can incrementally copy over any messages that are still arriving at the old SMTP server.

A tool named imapsync does incremental copy very well, but unfortunately it works on single mailboxes only and takes (at least in my use-case) too long. It further requires the password of each mailbox which IMHO makes it unsuitable for a full domain migration.

The current mox import is quite fast and doesn't require user passwords. It works on single folders, which had the side benefit that some legacy naming of folders (e.g. mail clients who created standard folders using the local language) could be fixed by the migration script. It also allowed to merge mailboxes. mox import doesn't handle incremental import (no duplicate checks) so the timing of the switch over between old and new server was critical.

The migration of alias-definition requires with the current state of implementation some workarounds.

Curious if you had the same or different approach. How did you verify the domain config at the new mox machine was correct? I can imagine you would want to briefly activate a domain to see if incoming messages will be accepted properly, without updating the MX record yet.

See above. New server with some small domain to verify the setup and perform tests. Scripting - if and where possible - for the other domains. It'd helped that the old server had no DANE, MTS_STA or auto-discovery setup, so much fewer conflicting DNS records.

For now, just marking a domain as disabled for sending/receiving email seems like a good first step.

Thanks!

DanielG commented 3 months ago

Hi Robert,

On Mon, Jun 10, 2024 at 11:47:27PM -0700, Robert Bill wrote:

Perhaps there are good IMAP syncing tools to migrate messages. If it has full sync support, it can incrementally copy over any messages that are still arriving at the old SMTP server.

A tool named imapsync does incremental copy very well, but unfortunately it works on single mailboxes only and takes (at least in my use-case) too long.

FYI I use https://guilhem.org/interimap/ for offline mail, but it also works very well for keeping my mox test mailbox in-sync with my old mail setup while I'm still evaluating.

It further requires the password of each mailbox which IMHO makes it unsuitable for a full domain migration.

With interimap you can have it ssh onto the mail server and/or launch a dovecot imap process directly, so no passwords on the old system are necessary with proper permission setup. On the mox side it's still necessary right now, but I would expect it should be possible to support a doveadm exec imap style imap connection in mox too if Michiel agrees this is useful.

That would solve the migration/sync problem in my mind (NOTIFY support would be nice-to-have still ;)

--Daniel

naddika commented 3 months ago

I'm about to migrate my Postfix emails onto Mox as well. I've made a backup of /var/vmail directory, which is Maildir, wherein my emails are stored. Do I understand correctly that simply restoring, or uploading, them in Mox, via UI, be enough for to perform migration?

RobSlgm commented 3 months ago

I'm about to migrate my Postfix emails onto Mox as well. I've made a backup of /var/vmail directory, which is Maildir, wherein my emails are stored. Do I understand correctly that simply restoring, or uploading, them in Mox, via UI, be enough for to perform migration?

@naddika That's my understanding, but not what I did and referred above. I used a bash script iterating the /var/vmail directory to generate mox import statements for each relevant folder.

./mox import maildir somebody Inbox /var/vmail/example,com/somebody/
./mox import maildir somebody Junk /var/vmail/example,com/somebody/.Junk/
./mox import maildir somebody Drafts /var/vmail/example,com/somebody/.Entw\&APw-rfe/

So re-directions (see last line) are possible and even merging folders, e.g ./mox import maildir somebody Inbox /var/vmail/example.org/otheraccount/ .

The creation of account and addresses (and also alias) can also be scripted

./mox config account add somebody somebody@example.com
./mox config address add somebody@example.org somebody

This basically allows you to test and re-run the whole import.

naddika commented 3 months ago

Thanks