Files
BARANTMail/__SMTP_DATA.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

242 lines
11 KiB
C++

#include "__SMTP_DATA.h"
#include "SMTPServer.h"
#include "DKIMVerifier.h"
#include "DMARCChecker.h"
#include "SMTPSyntax.h"
#include "Log.h"
#include <cctype>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <vector>
namespace mail {
namespace {
// Pulls the domain out of the message's own From: header (the
// identity DMARC alignment actually judges) - handles both
// "Name <user@domain>" and a bare "user@domain" value, and
// RFC 5322 3.2.2 folding (a continuation line is joined in before
// looking for the address).
std::string extractFromDomain(const std::string &message) {
size_t headerEnd = message.find("\r\n\r\n");
std::string headerBlock = (headerEnd == std::string::npos) ? message : message.substr(0, headerEnd);
std::vector<std::string> lines;
size_t pos = 0;
while (pos < headerBlock.size()) {
size_t next = headerBlock.find("\r\n", pos);
lines.push_back(next == std::string::npos ? headerBlock.substr(pos) : headerBlock.substr(pos, next - pos));
if (next == std::string::npos)
break;
pos = next + 2;
}
std::string fromValue;
for (size_t i = 0; i < lines.size(); ++i) {
if (lines[i].size() < 5)
continue;
std::string prefix = lines[i].substr(0, 5);
for (char &c : prefix)
c = std::tolower((unsigned char)c);
if (prefix != "from:")
continue;
fromValue = lines[i].substr(5);
for (size_t j = i + 1; j < lines.size() && !lines[j].empty() && (lines[j][0] == ' ' || lines[j][0] == '\t'); ++j)
fromValue += " " + lines[j];
break;
}
if (fromValue.empty())
return "";
size_t lt = fromValue.find('<');
size_t gt = fromValue.find('>');
std::string address = (lt != std::string::npos && gt != std::string::npos && gt > lt)
? fromValue.substr(lt + 1, gt - lt - 1)
: fromValue;
size_t at = address.find_last_of('@');
if (at == std::string::npos)
return "";
std::string domain = address.substr(at + 1);
size_t start = domain.find_first_not_of(" \t");
size_t end = domain.find_last_not_of(" \t");
if (start == std::string::npos)
return "";
return domain.substr(start, end - start + 1);
}
}
// RFC 5321 4.4: every SMTP server MUST prepend a Received: trace
// header to each message it accepts. Grammar (4.4):
// "Received:" "from" Extended-Domain "by" Domain Opt-info ";" date-time
// with the client's address literal as TCP-info, the protocol per
// RFC 3848 (SMTP / ESMTP, +S for TLS, +A for authenticated), and this
// message's queue id. No "for <recipient>" clause: one queued copy is
// linked to every recipient, and 4.4 says to omit it then anyway.
coreutils::MString __SMTP_DATA::receivedHeader(SMTPSession &session, SMTPServer &server) {
time_t now = time(nullptr);
struct tm tmResult;
localtime_r(&now, &tmResult);
char dateBuf[64];
strftime(dateBuf, sizeof(dateBuf), "%a, %d %b %Y %H:%M:%S %z", &tmResult);
std::string protocol = session.esmtp ? "ESMTP" : "SMTP";
if (session.esmtp && session.isTLS()) protocol += "S";
if (session.esmtp && session.authState == USER_KNOWN) protocol += "A";
coreutils::MString header;
header << "Received: from " << session.clientDomainName
<< " ([" << session.ipAddress.getClientAddress().c_str() << "])" << CRLF;
header << "\tby " << server.hostName.c_str() << " (BARANTMail) with " << protocol.c_str()
<< " id " << session.queueId.c_str() << ";" << CRLF;
header << "\t" << dateBuf << CRLF;
return header;
}
int __SMTP_DATA::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
if (session.mode == WAIT_FOR_DATA) {
// RFC 5321 4.1.1.4: "DATA" CRLF - no parameters.
if (!SMTPSyntax::argument(line(request)).empty()) {
session.out << "501 5.5.4 Syntax: DATA (no parameters)" << CRLF;
return 1;
}
if (session.state == MAIL) {
session.out << "503 5.5.1 Need RCPT command" << CRLF;
return 1;
}
if (session.state != RCPT) {
session.out << "503 5.5.1 Need MAIL command" << CRLF;
return 1;
}
session.queueId = generateMailFileName().str();
session.out << "354 End data with <CR><LF>.<CR><LF>" << CRLF;
// The trace header seeds the message; SPF's Received-SPF:
// (computed at MAIL FROM) goes directly after it, where MTAs
// conventionally put it.
session.mailData = receivedHeader(session, server);
session.mailData << session.spfHeader.c_str();
session.dataLineTooLong = false;
session.mode = RECEIVE_DATA;
grabInput(session);
return 1;
}
// ---- RECEIVE_DATA: one line of message text per call. Lines are
// framed on CR LF only (SMTPSession sets strictCRLF), so "." here
// can only be the real <CRLF>.<CRLF> terminator - a bare-LF or
// bare-CR variant inside the text is never mistaken for it (SMTP
// smuggling, CVE-2023-51764 family).
if (request != ".") {
if (session.mailData.getLength() >= SMTPServer::maxMessageSize)
return 1; // over the limit: keep reading, reject at the end
// RFC 5321 4.5.2 transparency: remove one leading '.'.
std::string text = line(request);
if (!text.empty() && text[0] == '.')
text.erase(0, 1);
// A bare CR or LF can't end a line here; store it as a proper
// CR LF (RFC 5321 2.3.8 forbids them in the text), so nothing
// downstream - local mailbox readers, or the next MTA when this
// message is relayed - ever sees a bare line ending from us.
if (text.find_first_of("\r\n") != std::string::npos) {
std::string normalized;
for (char c : text) {
if (c == '\r' || c == '\n') normalized += "\r\n";
else normalized += c;
}
text.swap(normalized);
}
text += "\r\n";
session.mailData << text.c_str();
return 1;
}
// ---- end of data
clearGrab(session);
if (session.dataLineTooLong) {
session.out << "500 5.5.6 Message contains a line that is too long" << CRLF;
} else if (session.mailData.getLength() >= SMTPServer::maxMessageSize) {
session.out << "552 5.3.4 Message size exceeds fixed maximum message size" << CRLF;
} else {
std::string message = session.mailData.str();
// DKIM verification + DMARC alignment, now that the full
// message (including whatever From:/DKIM-Signature headers
// the client sent) is actually available - neither can run
// any earlier than this.
std::vector<DKIMVerifier::SignatureResult> dkimResults = DKIMVerifier::verify(message);
std::string fromDomain = extractFromDomain(message);
DMARCChecker::Policy dmarcPolicy;
DMARCChecker::Result dmarcResult = DMARCChecker::evaluate(
fromDomain, session.spfDomain, session.spfResult, dkimResults, dmarcPolicy);
std::string authResults = DMARCChecker::authenticationResultsHeader(
session.spfResult, session.spfDomain, dkimResults, dmarcResult, fromDomain, server.hostName);
// RFC 7489 6.3: pct= applies the published disposition to only
// that percentage of otherwise-failing messages - the
// remainder still gets the Authentication-Results header
// recording the real (failing) result, just isn't rejected
// for it. p=none/quarantine never reject outright either way
// (quarantine has no separate mailbox to route into here, so
// it degrades to "accept, but the header says fail" - still a
// meaningful signal for whatever reads the header downstream).
bool sampled = (dmarcPolicy.percent >= 100) || ((rand() % 100) < dmarcPolicy.percent);
bool reject = (dmarcResult == DMARCChecker::Result::Fail) && dmarcPolicy.policy == "reject" && sampled;
if (reject) {
coreutils::Log(coreutils::LOG_INFO) << "Rejected message from " << session.from
<< " - DMARC fail, p=reject for " << fromDomain.c_str() << ".";
session.out << "550 5.7.1 Message rejected: failed DMARC policy for " << fromDomain.c_str() << CRLF;
} else {
coreutils::MString withAuthResults(authResults);
withAuthResults << session.mailData;
session.mailData = withAuthResults;
queueMail(server, session.queueId, session.from, session.recipientList, session.mailData);
coreutils::Log(coreutils::LOG_INFO) << "Queued message " << session.queueId.c_str() << ".";
// Just the id - the reply used to include the queue file's
// full path on this server.
session.out << "250 2.0.0 OK: queued as " << session.queueId.c_str() << CRLF;
}
}
// RFC 5321 4.1.1.4: the transaction is over whatever the outcome.
session.resetTransaction();
return 1;
}
coreutils::MString __SMTP_DATA::generateMailFileName() {
struct timespec timex;
clock_gettime(CLOCK_REALTIME, &timex);
std::stringstream temp;
temp << timex.tv_sec << "." << std::setfill('0') << std::setw(9) << timex.tv_nsec;
return coreutils::MString(temp.str());
}
void __SMTP_DATA::queueMail(SMTPServer &server, const std::string &id,
coreutils::MString &sender,
std::vector<coreutils::MString> &recipientList,
coreutils::MString &mailData) {
coreutils::MString fileName;
fileName << server.mailFileSystem.getMailPath() << "/.queue/" << id.c_str();
coreutils::File mailFile(fileName, O_CREAT | O_WRONLY, 0660);
mailFile.write(mailData);
for(coreutils::MString recipient: recipientList) {
// <id>|<recipient>|<sender>: SMTPServer::inCreate() needs both
// addresses and the file name is the only thing that carries
// them there. Both are %-encoded - "/" and "|" are legal in an
// address but would break the name (see SMTPSyntax).
coreutils::MString newName;
newName << fileName << "|" << SMTPSyntax::encodeQueueField(recipient.str()).c_str()
<< "|" << SMTPSyntax::encodeQueueField(sender.str()).c_str();
link(fileName.c_str(), newName.c_str());
}
unlink(fileName.c_str());
}
}