Barış Akın~/blog

·9 min read·54 views

OxiMail: my own mail server in Go, with all its mail in OxiDB

OxiMail puts SMTP, IMAP, webmail, spam checks and antivirus in one program and keeps all its data in OxiDB. It also sends this blog's newsletter. This Go version is a prototype that will be rewritten in Rill. How it works, measurements, and the bugs I found and fixed while reviewing it.

OxiMail: my own mail server in Go, with all its mail in OxiDB

OxiMail is a mail server I wrote in Go. One program does SMTP, IMAP, webmail, spam checks and antivirus, and everything it knows is in OxiDB: accounts, folders, flags, the delivery queue and the message bodies. There is no Maildir, no Postfix and no Dovecot behind it. If you subscribed to this blog by email, the letter you received came from OxiMail.

This Go version is a prototype. OxiMail will be rewritten in Rill, and what is here is where the design gets tested against real mail first.

The code is on GitHub under AGPL-3.0: github.com/parisxmas/OxiMail. It is 112 commits, about 15,000 lines of Go plus 8,200 lines of tests, and an Angular webmail of 4,700 lines.

This post covers how a message travels through it, what real mail taught it, measurements on a clean copy, and the bugs I found and fixed while reviewing it for this post. One of them could lose mail.

What runs

portwhat it is
25inbound mail from other servers
587, 465sending, with a password, over TLS
143, 993IMAP for mail clients
8080webmail and its API
9090Prometheus metrics and health checks

A second program, oximailctl, does the administration: domains, accounts, aliases, DKIM keys, vacation replies, Sieve filters, and backup and restore of a single account.

$ oximailctl domain add example.com
$ oximailctl domain dkim example.com
$ echo 'hunter2' | oximailctl account add alice@example.com

How a message comes in

A message arriving on port 25 goes through checks in order, and the first one that decides ends it.

  1. Rate limit. 100 messages a minute per IP.
  2. DNS blocklist. zen.spamhaus.org by default.
  3. Is the recipient here? An unknown address gets a 550 before any body is read.
  4. Greylisting. A first-time sender is told to come back in a minute. Real servers retry, most spam tools do not.
  5. DMARC, with SPF and DKIM. p=reject is refused, p=quarantine goes to the Junk folder.
  6. Antivirus. Every attachment is hashed and looked up.
  7. Sieve. The user's own filter picks the folder.
  8. Delivery. Then a vacation reply if one is set, and forwarding for aliases.

A DNS failure does not stop mail. A blocklist that cannot answer counts as not listed, and a sender with no DMARC record passes. A broken database does stop it: the sender gets a 451 and tries again later. Temporary trouble on the checking side should not lose a letter, and trouble on the storing side should not pretend to have kept one.

Everything in OxiDB

Each account has its own collections for folders, messages and the expunge log. A message is a document, and its IMAP flags are a field on that document. The body is a blob, stored once and reference-counted.

That reference count is what makes IMAP COPY cheap. In my measurement, copying 1,000 messages to another folder did not grow the data on disk at all: the new documents point at the same bodies.

The order of writes is chosen so that a crash leaves something harmless behind. A UID is taken before the body is written, so a crash wastes a number and never reuses one. A delete removes the document before the blob, so a crash leaves unused bytes on disk rather than a message with no body.

IMAP, with the hard parts

Most small mail servers stop at basic IMAP. OxiMail does CONDSTORE and QRESYNC, which let a phone ask "what changed since I last looked" and get an answer in one round trip instead of downloading every flag again. The Go IMAP library has no server side for these, so the repository carries a 941-line patch for it. IDLE is real too: when a message arrives on another connection, the waiting client is told at once.

The webmail has a threaded list, search operators, keyboard shortcuts, a rich text editor, and settings for vacation replies and Sieve rules.

How a message goes out

The queue lives in OxiDB, so it survives a restart. It wakes every 30 seconds, looks up the MX, tries STARTTLS, and respects MTA-STS when the receiving domain enforces it. Each letter is signed with DKIM on the way out. Recipients are tracked one by one, so one bad address does not hold back the others. A temporary failure is retried for about five days, and a permanent one produces a proper bounce back to the sender.

This blog's newsletter uses exactly this path. It hands a letter to port 465, and OxiMail does the rest. Gmail's verdict on the first letter was:

SPF:    PASS with IP 188.68.37.53
DKIM:   PASS with domain baltavista.com
DMARC:  PASS

It still went to the spam folder. All three checks passing only proves who sent it, not that anyone should want it, and this server had a quiet address and a brand new sender. After I marked that one as not spam, the next newsletter landed in the inbox, and a bare one-line test after that went to spam again, with all three checks still passing. A mail server can do everything correctly and still have to earn its reputation.

An antivirus of its own

internal/av is the part I find most unusual. It is not a scanner in the usual sense. Every six hours it downloads known malware hashes from MalwareBazaar and ThreatFox, and inbound attachments are hashed with SHA-256 and looked up. A match is refused with a 550. The new list is swapped in atomically, so nothing has to stop.

Its limit is plain: change one byte and the hash no longer matches, and it does not look inside archives. It catches known files, cheaply. Rspamd used to do content filtering here, and it was removed in May.

What real mail taught it

Tests pass on the mail you imagine. Three fixes came from the mail that actually arrived.

Every message was refused. The blocklist check treated any DNS answer as "listed". Spamhaus answers 127.255.255.254 to mean "I refuse to answer you", which is what a public resolver gets. So all inbound mail was rejected. I met the same answer while checking this server for this post: it says nothing about you, and it is easy to read it as bad news.

Gmail never got through greylisting. Gmail retries from a neighbouring address, so an exact IP match never saw the second attempt. The key is now the /24 network.

700 MB of memory for short strings. Antivirus labels were slices of an 80 MB parse buffer, and in Go one small substring keeps the whole buffer alive. Copying the strings fixed it, by the figures in the commit message.

Measurements

These ran on a clean copy on an Apple M4, with OxiDB 0.45.1 on the same machine, TLS on, and every message delivered to a local mailbox, so nothing touched the network. I sent 41,400 messages, and all 41,400 were in the mailbox at the end. All 591 tests passed against OxiDB 0.45.1, including the integration tests and the race detector. These numbers are from the fixed code, and other work was running on the machine at the time, so treat the IMAP times as rough.

More connections add waiting, not speed: with 1, 4 and 16 connections OxiMail delivers about 40, 45 and 41 messages a second while the median wait rises from 23 to 84 to 370 milliseconds

The rate is about 40 messages a second however many connections there are, and the wait grows with the queue. A 100 KB message costs about the same as a 4 KB one, so the cost is per message, not per byte. My first guess was the single OxiDB connection, and that was wrong: with a pool of eight the rate stayed the same, though reads got much faster. The limit is OxiDB syncing to disk on every commit, the price of a confirmed message being really on disk. For a personal or small company server, 40 a second is about 3.4 million a day and is not a problem. For a large newsletter it would be.

With 41,400 messages in one folder:

IMAP operationtime
SELECT0.3 s
flags of every message0.15 s
envelopes of every message7.1 s
headers of 1,000 messages0.15 s
full bodies of 1,000 messages0.08 s
search by subject6.5 s
search by body5.9 s
change flags on 1,000 messages12.1 s
copy 1,000 messages24.7 s

Reading is fast. Searching walks the whole folder, and writes go one message at a time, about 12 ms each.

I also killed the server with kill -9 while four clients were sending. It had confirmed 280 messages, and after the restart all 280 were there, plus one it had stored without getting to confirm. It was listening again in 0.1 s.

The program is one 17 MB file. Idle on a laptop it used 20 MB, and OxiDB 43 MB. In production, with the malware lists loaded, it holds about 250 MB, and OxiDB about 90 MB.

What I found and fixed

Reviewing it line by line found more than I expected. I reproduced the first three on a running server, and all of this is now fixed and live on this server.

  • Deleting a folder could destroy mail in another folder. This was the serious one. Copy a message into folder A, delete folder A, and the original in the inbox lost its body too. Fetching it then never returned, and the whole session hung. DeleteMailbox deleted the body directly instead of going through the reference count. Bodies already lost that way do not come back.
  • Quotas were not enforced. An account with a 5,000 byte quota accepted 36,096 bytes. It now gets 452 Mailbox full.
  • Sending did not check who you are. An authenticated user could send as someone else. Both the envelope and the From line must now be the account's own address or its alias.
  • The queue gave up too soon. It tried for about 75 minutes, so a receiving server down for two hours meant a bounce. It now tries for about five days.
  • Smaller things. Concurrent flag changes could lose one. Moves did not reach the log offline clients sync from. HTML mail relied on one sanitiser in the browser; it is now cleaned on the server and shown in a sandboxed frame with remote images blocked. No Received header was added. Sieve read else and ignored it. The README described Rspamd, which was gone, and OxiMem, which was never used.

Some things are still open. Two server processes do not see each other's IDLE events, a retried submission can still duplicate a local delivery, and Sieve's fileinto combined with keep loses the keep.

Trying it

It needs Go 1.25 and OxiDB. The repository has a Docker Compose file that brings up both, and four settings are required in deploy/.env:

OXIMAIL_HOSTNAME=mail.example.com
OXIMAIL_ACME_HOSTS=mail.example.com
OXIMAIL_ACME_EMAIL=ops@example.com
OXIMAIL_SRS_SECRET=<openssl rand -hex 32>
$ git clone https://github.com/parisxmas/OxiMail
$ cd OxiMail
$ docker compose --env-file deploy/.env -f deploy/docker-compose.yml up -d

The next OxiMail will be written in Rill. This Go version did its job as a prototype: it showed which design holds up under real mail, and the list above is what the Rill version has to get right from the start.

The hard part of mail was never the software. It is the DNS records, the reverse DNS of your address, and the time it takes for the big providers to believe you. The README lists every record you need.

Barış Akın

Software solutions architect with 25+ years of experience. FinTech and distributed systems; creator of OxiDB, Rill and Cobra.

Get new posts by email

I will let you know when a new post is out. You can leave at any time.

Comments

    Be the first to comment.

    Sign in with your Google account to comment. Your name and comment are public. Your email and IP address are kept to deal with abuse, but never shown.

    Before you subscribe

    Privacy