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

167 lines
6.2 KiB
C++

#include "__SMTP_AUTH.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) {
std::string text = line(request);
// ---- 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;
}
// ---- 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) {
// 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);
}
}