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
This commit is contained in:
barantandClaude Opus 5.5 authored and brad Arant committed 2026-11-02 11:56:41 -08:00
1 parent 407cc2e915
commit d3d7e5f3e5
27 files changed
+1821 -358

No files matched your search

+150 -93
View File
@@ -1,108 +1,165 @@
#include "__SMTP_AUTH.h"
#include "Base64.h"
#include "SMTPServer.h"
#include "SMTPSyntax.h"
namespace mail {
// SMTP AUTH (RFC 4954) with the PLAIN (RFC 4616) and LOGIN mechanisms.
//
// The first line ("AUTH mech [initial-response]") arrives through the
// normal command dispatch; while an exchange is in progress grabInput()
// routes every following line here unsplit, and authState says which
// response it is.
namespace {
const char *CANCELLED = "501 5.0.0 Authentication cancelled";
const char *BAD_BASE64 = "501 5.5.2 Cannot decode response";
const char *FAILED = "535 5.7.8 Authentication credentials invalid";
}
void __SMTP_AUTH::finish(SMTPSession &session, const char *reply) {
session.out << reply << CRLF;
session.authState = USER_UNKNOWN;
session.userName = "";
session.password = "";
clearGrab(session);
}
void __SMTP_AUTH::verify(SMTPSession &session, SMTPServer &server) {
if (authLogin(session.userName, session.password, server)) {
session.out << "235 2.7.0 Authentication successful" << CRLF;
session.authState = USER_KNOWN;
session.relay = true;
session.password = "";
clearGrab(session);
} else {
// Back to a clean state so the client can try again on the
// same connection (RFC 4954 4).
finish(session, FAILED);
}
}
// PLAIN message: [authzid] NUL authcid NUL passwd (RFC 4616 2).
void __SMTP_AUTH::plain(const std::string &response, SMTPSession &session, SMTPServer &server) {
size_t a = response.find('\0');
size_t b = a == std::string::npos ? a : response.find('\0', a + 1);
if (b == std::string::npos || response.find('\0', b + 1) != std::string::npos) {
finish(session, "501 5.5.2 Malformed PLAIN response");
return;
}
std::string authzid = response.substr(0, a);
std::string authcid = response.substr(a + 1, b - a - 1);
std::string passwd = response.substr(b + 1);
// This server has no proxy-authorization: acting as anyone other
// than the authenticated identity is refused.
if (authcid.empty() || (!authzid.empty() && authzid != authcid)) {
finish(session, FAILED);
return;
}
session.userName = authcid.c_str();
session.password = passwd.c_str();
verify(session, server);
}
int __SMTP_AUTH::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
coreutils::Base64 base64;
std::string text = line(request);
switch(session.authState) {
case USER_UNKNOWN:
// SMTPServer's CommandList splits the line only once ("AUTH",
// depth=1 - see its own constructor), so request[1] here is
// "LOGIN" alone for the classic multi-step form, but "LOGIN
// <base64>" as ONE token when a client supplies RFC 4954 4's
// optional initial-response inline to save a round trip -
// confirmed live against Python's smtplib, which does exactly
// that. The old request[1].equals("LOGIN") exact-match rejected
// every such client with a spurious 504, never reaching the
// inline response at all.
if(request[1].ifNextIgnoreCase("LOGIN")) {
request[1].skipWhitespace();
if(!request[1].eod()) {
session.userName = base64.decode(request[1].unparsed());
session.out << "334 UGFzc3dvcmQ6" << CRLF;
session.authState = USER_SECRET_QUERY;
grabInput(session);
} else {
session.out << "334 VXNlcm5hbWU6" << CRLF;
// setTimer(10.0f);
session.authState = USER_QUERY;
grabInput(session);
}
}
else {
session.out << "504 AUTH method not supported." << CRLF;
}
return 1;
case USER_QUERY:
// RFC 4954 4: a bare "*" cancels the exchange rather than being
// treated as (still-encoded) credential data.
if (request.equals("*")) {
session.out << "501 5.7.0 Authentication cancelled" << CRLF;
session.authState = USER_UNKNOWN;
clearGrab(session);
return 1;
}
// grabInput() above means this line never goes through
// CommandList's own .split() (see CommandList::processRequest's
// grab branch) - request[0] here was actually an out-of-bounds
// read on an always-empty list, not "the first word". The
// whole line IS the client's response: a single base64-encoded
// token with no embedded whitespace, exactly what the unsplit
// request already holds. It was never decoded either - AUTH
// LOGIN's wire format sends the username/password base64
// encoded (that's what the "334 VXNlcm5hbWU6"/"334
// UGFzc3dvcmQ6" challenges - "Username:"/"Password:" - signal)
// - comparing the raw, still-encoded text against a plaintext
// secret file could never match for any real client.
session.userName = base64.decode(request);
// setTimer(0.0f);
session.out << "334 UGFzc3dvcmQ6" << CRLF;
// setTimer(10.0f);
session.authState = USER_SECRET_QUERY;
return 1;
case USER_SECRET_QUERY:
if (request.equals("*")) {
session.out << "501 5.7.0 Authentication cancelled" << CRLF;
session.authState = USER_UNKNOWN;
clearGrab(session);
return 1;
}
session.password = base64.decode(request);
// setTimer(0.0f);
if(authLogin(session.userName, session.password, server)) {
session.out << "235 2.7.0 Authentication successful" << CRLF;
session.relay = true;
session.authState = USER_KNOWN;
clearGrab(session);
}
else {
// Reset back to a clean, addressable state - without this,
// the session stayed grabbed and stuck expecting yet another
// "password" line forever after one wrong guess, since
// nothing else ever set authState back to USER_UNKNOWN or
// released the grab. A client that mistypes a password needs
// to be able to retry AUTH on the same connection, not
// reconnect entirely.
session.out << "530 Login was unsuccessful." << CRLF;
session.authState = USER_UNKNOWN;
clearGrab(session);
}
return 1;
// ---- continuation lines of an exchange in progress
if (session.authState == USER_QUERY || session.authState == USER_SECRET_QUERY ||
session.authState == PLAIN_QUERY) {
if (text == "*") { // RFC 4954 4: client cancels
finish(session, CANCELLED);
return 1;
}
std::string decoded;
if (!SMTPSyntax::decodeBase64(text, decoded)) {
finish(session, BAD_BASE64);
return 1;
}
if (session.authState == PLAIN_QUERY) {
plain(decoded, session, server);
} else if (session.authState == USER_QUERY) {
session.userName = decoded.c_str();
session.authState = USER_SECRET_QUERY;
session.out << "334 UGFzc3dvcmQ6" << CRLF; // "Password:"
} else {
session.password = decoded.c_str();
verify(session, server);
}
return 1;
}
return 0;
// ---- a new AUTH command (RFC 4954 4)
if (!session.esmtp) {
session.out << "503 5.5.1 Send EHLO first" << CRLF;
return 1;
}
if (session.authState == USER_KNOWN) {
session.out << "503 5.5.1 Already authenticated" << CRLF;
return 1;
}
if (session.state == MAIL || session.state == RCPT) {
session.out << "503 5.5.1 AUTH not permitted during a mail transaction" << CRLF;
return 1;
}
if (!server.authAllowed(session)) {
session.out << "538 5.7.11 Encryption required for requested authentication mechanism" << CRLF;
return 1;
}
std::string arg = SMTPSyntax::argument(text);
if (arg.empty()) {
session.out << "501 5.5.4 Syntax: AUTH mechanism [initial-response]" << CRLF;
return 1;
}
size_t sp = arg.find(' ');
std::string mechanism = SMTPSyntax::upper(arg.substr(0, sp));
std::string initial = sp == std::string::npos ? "" : SMTPSyntax::argument(arg);
bool hasInitial = sp != std::string::npos && !initial.empty();
if (mechanism != "PLAIN" && mechanism != "LOGIN") {
session.out << "504 5.5.4 Unrecognized authentication type" << CRLF;
return 1;
}
if (hasInitial) {
// "=" is an initial response of zero length (RFC 4954 4).
std::string decoded;
if (initial != "=" && !SMTPSyntax::decodeBase64(initial, decoded)) {
session.out << BAD_BASE64 << CRLF;
return 1;
}
if (mechanism == "PLAIN") {
plain(decoded, session, server);
} else {
session.userName = decoded.c_str();
session.authState = USER_SECRET_QUERY;
grabInput(session);
session.out << "334 UGFzc3dvcmQ6" << CRLF;
}
return 1;
}
grabInput(session);
if (mechanism == "PLAIN") {
session.authState = PLAIN_QUERY;
session.out << "334 " << CRLF; // empty challenge
} else {
session.authState = USER_QUERY;
session.out << "334 VXNlcm5hbWU6" << CRLF; // "Username:"
}
return 1;
}
bool __SMTP_AUTH::authLogin(coreutils::ZString &userName, coreutils::ZString &password, SMTPServer &server) {
// The actual check now lives in MailFileSystem::checkPassword(),
// shared with IMAP LOGIN and POP3 USER/PASS - see its own doc
// comment.
// Shared with IMAP LOGIN and POP3 USER/PASS - see
// MailFileSystem::checkPassword(). A user name that can't be a
// mailbox path (no "@", "/", "..") can't have a password file.
std::string local, domain;
SMTPSyntax::splitAddress(userName.str(), local, domain);
if (!SMTPSyntax::safePathComponent(local) || !SMTPSyntax::safePathComponent(domain))
return false;
return server.mailFileSystem.checkPassword(userName, password);
}