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

102 lines
3.6 KiB
C++

#ifndef __SMTPSession_h__
# define __SMTPSession_h__
# include "TCPSession.h"
# include "TCPServer.h"
# include "MString.h"
# include "SPFChecker.h"
# define CRLF "\r\n"
namespace mail {
enum State {CONNECT, READY, READYX, MAIL, RCPT, DATA, SENT};
// PLAIN_QUERY: AUTH PLAIN sent with no initial response, waiting for
// the client's single base64 response (RFC 4616, RFC 4954 4).
enum AuthState {USER_UNKNOWN, USER_QUERY, USER_SECRET_QUERY, USER_KNOWN, PLAIN_QUERY};
enum Mode {WAIT_FOR_DATA, RECEIVE_DATA};
class SMTPSession : public core::TCPSession {
public:
SMTPSession(core::EPoll &ePoll, core::TCPServer &server);
void onConnected() override;
///
/// Longest command line accepted. RFC 5321 4.5.3.1.4 sets 512 as the
/// minimum a server must handle; RFC 4954 4 raises it to 12288 for
/// AUTH with an initial response, so that is the cap used for all
/// commands. Message text lines are bounded separately (see the
/// constructor's maxLineLength).
///
static const size_t maxCommandLine = 12288;
///
/// Abort any mail transaction: what RSET, EHLO, HELO and the end of
/// DATA all do (RFC 5321 4.1.1.5, 4.1.4). Keeps the greeting and any
/// authentication.
///
void resetTransaction();
///
/// Forget everything learned from the client - what STARTTLS
/// requires once the handshake starts (RFC 3207 4.2): greeting,
/// authentication and any transaction.
///
void resetAll();
bool greeted = false; // HELO or EHLO accepted (RFC 5321 4.1.4)
bool esmtp = false; // ... and it was EHLO, so extensions apply
bool dataLineTooLong = false;
std::string queueId; // id of the message being received, also used in Received:
coreutils::MString clientDomainName;
coreutils::MString userName;
coreutils::MString password;
coreutils::MString mailData;
State state = CONNECT;
AuthState authState = USER_UNKNOWN;
Mode mode = WAIT_FOR_DATA;
// Defaults to false: relaying to a non-local recipient is only
// permitted after a real, successful AUTH (see __SMTP_AUTH.cpp,
// which sets this true on success) - defaulting to true made every
// brand-new, unauthenticated connection an open relay for anyone
// (__SMTP_RCPT.cpp checks exactly this flag before accepting a
// recipient whose mailbox does not exist locally).
bool relay = false;
coreutils::MString from;
std::vector<coreutils::MString> recipientList;
// Computed once at MAIL FROM time (see __SMTP_MAIL.cpp) and
// reused at DATA time (__SMTP_DATA.cpp), rather than re-running
// the SPF DNS lookups a second time for the same transaction. The
// header is ready to prepend to the message as-is; domain/result
// are also kept separately since DMARC's own alignment check
// (also run at DATA time) needs the raw identity/result, not the
// pre-formatted header text.
std::string spfHeader;
std::string spfDomain;
SPFChecker::Result spfResult = SPFChecker::Result::None;
protected:
///
/// Commands are dispatched through the server's CommandList as
/// before; this adds the replies RFC 5321 4.2.4 requires that the
/// generic TCPSession path never sent - 500 for an unrecognized or
/// empty command line, and 500 for an over-long one. A client that
/// got no reply at all simply hung.
///
void protocol(coreutils::ZString &data) override;
void onLineTooLong() override;
};
}
#endif