Files
BARANTMail/SMTPServer.cpp
T
barantandClaude Opus 5.5 d3d7e5f3e5 SMTP: STARTTLS, smuggling fix, and RFC 5321 conformance
Security:
- SMTP smuggling (CVE-2023-51764 family): only <CRLF>.<CRLF> ends DATA
  (strict CRLF framing); bare CR/LF in the text is stored as CR LF.
  Before, '<LF>.<LF>' and four other variants ended the message and the
  rest ran as fresh commands - a second, forged message was delivered.
- STARTTLS on port 25 (RFC 3207), using the configured tls cert; AUTH
  is only offered/accepted once encrypted (538 5.7.11 otherwise).
- The 250 reply no longer discloses the queue file's full path.
- Queue file names %-encode the addresses ('/' or '|' in an address
  broke the name or pointed it into another directory).

Conformance (RFC 5321, 1870, 2034, 2920, 3207, 3463, 3848, 4616, 4954,
6152):
- 500 for unknown/empty/over-long commands (no reply at all before -
  clients hung); 501 for missing/invalid arguments (bare MAIL/RCPT/HELO
  were out-of-bounds reads).
- HELO/EHLO required before MAIL; nested MAIL refused; EHLO/HELO/RSET/
  end-of-DATA reset the transaction.
- MAIL: SIZE= (552 when over the limit), BODY=7BIT|8BITMIME, AUTH=;
  555 for anything else; case-insensitive keywords; address syntax
  checked.
- RCPT: postmaster (bare and any case) accepted; case-insensitive
  domain and local-part; 550 5.1.1 unknown user vs 550 5.7.1 relay
  denied; aliases accepted (they were rejected at RCPT before); 1000
  recipient cap (452 4.5.3).
- ENHANCEDSTATUSCODES advertised and used on every reply.
- AUTH PLAIN added alongside LOGIN; strict base64; 535 5.7.8 on
  failure; 503 before EHLO, during a transaction, or when already
  authenticated (the last one sent no reply before).
- Received: per RFC 5321 4.4 with client IP, protocol per RFC 3848
  (ESMTP/ESMTPS/ESMTPSA) and queue id; Return-Path: added at local
  delivery.

tests/smtp_conformance.py: 112 checks. origin/master passes 37 of the
72 it can reach before the server stops answering; this passes 112/112.
tests/imap_pop3_regression.py: IMAPS/POP3S unaffected (3/3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0121tAhQVe4qbCwYgVjKQS4B
2026-11-02 11:56:41 -08:00

190 lines
8.9 KiB
C++

#include "SMTPServer.h"
#include "Log.h"
#include "SendMail.h"
#include "SpamAssassinFilter.h"
#include "File.h"
#include <thread>
#include <sys/stat.h>
#include "SMTPSyntax.h"
namespace mail {
SMTPServer::SMTPServer(core::EPoll &ePoll, std::string hostName, MailFileSystem &mailFileSystem,
core::IPAddress ipAddress, core::TLSInfo *tlsInfo, bool watchQueue,
std::map<std::string, DKIMSigner*> *dkimSigners)
: core::TCPServer(ePoll, ipAddress, tlsInfo, " ", 1, "SMTP Server"), core::INotify(ePoll), ePoll(ePoll), hostName(hostName), mailFileSystem(mailFileSystem), dkimSigners(dkimSigners) {
commands.add(_smtp_auth, "AUTH");
commands.add(_smtp_data, "DATA");
commands.add(_smtp_ehlo, "EHLO");
commands.add(_smtp_expn, "EXPN");
commands.add(_smtp_helo, "HELO");
commands.add(_smtp_help, "HELP");
commands.add(_smtp_mail, "MAIL");
commands.add(_smtp_noop, "NOOP");
commands.add(_smtp_quit, "QUIT");
commands.add(_smtp_rcpt, "RCPT");
commands.add(_smtp_rset, "RSET");
commands.add(_smtp_starttls, "STARTTLS");
commands.add(_smtp_vrfy, "VRFY");
if (watchQueue) {
processExisting();
wd = addWatch(mailFileSystem.getQueuePath());
}
}
SMTPSession * SMTPServer::getSocketAccept(core::EPoll &ePoll) {
return new SMTPSession(ePoll, *this);
}
void SMTPServer::sessionErrorHandler(std::string errorString, std::stringstream &out) {
out << "252 " << errorString << CRLF;
}
bool SMTPServer::isLocalDomain(const std::string &domain) {
if (!SMTPSyntax::safePathComponent(domain))
return false;
struct stat st;
std::string path = mailFileSystem.getMailPath().str() + "/" + domain;
return stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
}
std::string SMTPServer::postmasterAddress() {
std::string domain = SMTPSyntax::lower(hostName);
while (true) {
coreutils::MString candidate;
candidate << "postmaster@" << domain.c_str();
if (isLocalDomain(domain) &&
(mailFileSystem.ifMailBoxExists(candidate) || mailFileSystem.ifAliasExists(candidate)))
return "postmaster@" + domain;
size_t dot = domain.find('.');
if (dot == std::string::npos || domain.find('.', dot + 1) == std::string::npos)
return "";
domain = domain.substr(dot + 1);
}
}
void SMTPServer::deliverToMailbox(coreutils::ZString &mailbox, coreutils::ZString &messageId,
SpamAssassinFilter::Result &scan, const std::string &sender) {
// The destination folder has to be picked AFTER the scan runs,
// not before - this used to always build an "/Inbox/" path
// regardless of what spamd found, so a correctly-flagged spam
// message (isSpam true, logged as "(flagged as spam)" right
// below) still landed straight in Inbox every time; the Spam
// folder existed and was reachable, it just never received
// anything. scan.isSpam is only meaningful when scan.ok is
// true (spamd fails open - see SpamAssassinFilter.h's own doc
// comment - an unreachable scanner must not silently reroute
// every message to Spam).
coreutils::MString path;
path << mailFileSystem.getMailBoxPath(mailbox)
<< ((scan.ok && scan.isSpam) ? "/Spam/" : "/Inbox/") << messageId;
coreutils::File mailFile(path, O_CREAT | O_WRONLY, 0660);
std::string returnPath = "Return-Path: <" + sender + ">\r\n";
mailFile.write(returnPath);
mailFile.write(scan.scanned);
coreutils::Log(coreutils::LOG_INFO) << "Message " << messageId << " delivered to recipient " << mailbox
<< (scan.ok && scan.isSpam ? " (flagged as spam)" : "") << ".";
}
void SMTPServer::inCreate(coreutils::ZString &name) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "inCreate; [" << name << "]";
name.split("|");
// Three parts now: messageid|recipient|sender - see
// __SMTP_DATA::queueMail()'s own doc comment for why the sender
// had to be added alongside the recipient.
if(name.getList().size() > 2) {
// Recipient and sender are %-encoded in the file name - see
// SMTPSyntax::encodeQueueField(). Decoding a name written before
// that change is a no-op unless it happened to contain "%".
std::string senderAddress = SMTPSyntax::decodeQueueField(name[2].str());
std::string recipientString = SMTPSyntax::decodeQueueField(name[1].str());
coreutils::MString recipientName(recipientString);
bool exists = mailFileSystem.ifMailBoxExists(recipientName);
// An alias is a plain file (not a directory) at the same
// level a mailbox would be - see MailFileSystem's own doc
// comment on createAlias()/ifAliasExists(). Only checked when
// it isn't a real mailbox, so a mailbox and an alias can never
// both match the same address.
bool isAlias = !exists && mailFileSystem.ifAliasExists(recipientName);
coreutils::MString from;
from << mailFileSystem.getQueuePath() << "/" << name;
if(exists || isAlias) {
// Scanned on the way into the recipient's own Inbox, not the
// outbound relay path below - spamd's added X-Spam-* headers
// are for whatever reads this recipient's mailbox, and a
// message this server relays onward gets scored by ITS
// destination's own filtering, same as any other MTA. Reading
// the whole file in (rather than the plain link() this branch
// used to do) is unavoidable now that spamd may have rewritten
// the content the recipient actually needs to receive.
coreutils::File queuedFile(from, O_RDONLY);
queuedFile.read();
SpamAssassinFilter::Result scan = SpamAssassinFilter::scan(queuedFile.asString());
if (isAlias) {
// One shared scan result delivered to every target - an
// alias should carry one spam verdict for the message, not
// a separately re-scanned copy per target.
std::vector<coreutils::MString> targets = mailFileSystem.getAliasMailboxes(recipientName);
for (auto &target : targets)
deliverToMailbox(target, name[0], scan, senderAddress);
} else {
deliverToMailbox(recipientName, name[0], scan, senderAddress);
}
int rc = unlink(from.c_str());
} else {
// Not a local mailbox - relay it out over SMTP. SendMail's
// own send() is a plain blocking call (see its doc comment
// for why) - queued delivery wants fire-and-forget, so this
// owns the heap-allocated SendMail itself and runs it on its
// own detached thread rather than blocking this EPoll
// worker thread for however long the remote host takes.
coreutils::MString sender(senderAddress);
coreutils::MString recipient(recipientString);
coreutils::File queuedFile(from, O_RDONLY);
queuedFile.read();
coreutils::MString mailData(queuedFile.asString());
SendMail *outbound = new SendMail(hostName, mailFileSystem.getSendLogPath().str(),
name[0].str(), sender, recipient, mailData, dkimSigners);
// The queue file's content is already fully read into
// mailData above, so it's safe to unlink it now rather
// than after the detached thread finishes (which may be
// arbitrarily later, or never, if the process restarts
// first) - previously this was never unlinked at all,
// meaning processExisting() re-relayed every message still
// sitting here on every single restart, sending duplicate
// copies (confirmed live: one message redelivered 35+
// times across repeated crash-restarts).
int rc = unlink(from.c_str());
std::thread([outbound]() {
try {
outbound->send();
} catch (...) {
// An exception escaping a detached thread's entry
// function calls std::terminate() - crashing the
// entire process, not just this one relay attempt
// (this, combined with SIGPIPE previously not being
// ignored anywhere, is what turned an occasional
// failed delivery into repeated whole-server crashes
// - see main.cpp's new signal(SIGPIPE, SIG_IGN)).
coreutils::Log(coreutils::LOG_EXCEPT) << "Outbound relay threw an exception - message not delivered.";
}
delete outbound;
}).detach();
}
}
}
void SMTPServer::processExisting() {
for (const auto & entry : std::filesystem::directory_iterator(mailFileSystem.getQueuePath().str())) {
std::string temp(entry.path().filename().string());
coreutils::ZString temp2(temp);
inCreate(temp2);
}
}
}