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
88 lines
3.6 KiB
C++
88 lines
3.6 KiB
C++
#include "__SMTP_MAIL.h"
|
|
#include "SMTPServer.h"
|
|
#include "SPFChecker.h"
|
|
#include "SMTPSyntax.h"
|
|
|
|
namespace mail {
|
|
|
|
int __SMTP_MAIL::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
|
|
|
// RFC 5321 4.1.4: a client MUST say HELO/EHLO before a transaction,
|
|
// and MAIL may not be issued again while one is open.
|
|
if (!session.greeted) {
|
|
session.out << "503 5.5.1 Send HELO/EHLO first" << CRLF;
|
|
return 1;
|
|
}
|
|
if (session.state == MAIL || session.state == RCPT) {
|
|
session.out << "503 5.5.1 Nested MAIL command" << CRLF;
|
|
return 1;
|
|
}
|
|
|
|
SMTPSyntax::PathArgument arg = SMTPSyntax::parsePath(SMTPSyntax::argument(line(request)), "FROM:", true);
|
|
if (!arg.ok) {
|
|
session.out << arg.reply << CRLF;
|
|
return 1;
|
|
}
|
|
|
|
// Service-extension parameters (RFC 5321 4.1.1.11) - only after
|
|
// EHLO, and only the ones this server advertises.
|
|
for (auto ¶m : arg.params) {
|
|
if (!session.esmtp) {
|
|
session.out << "555 5.5.4 MAIL parameters require EHLO" << CRLF;
|
|
return 1;
|
|
}
|
|
if (param.first == "SIZE") {
|
|
// RFC 1870 6.1: refuse up front if the declared size is too big.
|
|
const std::string &v = param.second;
|
|
if (v.empty() || v.size() > 20 || v.find_first_not_of("0123456789") != std::string::npos) {
|
|
session.out << "501 5.5.4 Syntax: SIZE=<number>" << CRLF;
|
|
return 1;
|
|
}
|
|
if (v.size() > 18 || std::stoull(v) > SMTPServer::maxMessageSize) {
|
|
session.out << "552 5.3.4 Message size exceeds fixed maximum message size" << CRLF;
|
|
return 1;
|
|
}
|
|
} else if (param.first == "BODY") {
|
|
// RFC 6152: 7BIT and 8BITMIME. BINARYMIME needs CHUNKING,
|
|
// which isn't offered.
|
|
std::string v = SMTPSyntax::upper(param.second);
|
|
if (v != "7BIT" && v != "8BITMIME") {
|
|
session.out << "555 5.5.4 Unsupported BODY type" << CRLF;
|
|
return 1;
|
|
}
|
|
} else if (param.first == "AUTH") {
|
|
// RFC 4954 5: accepted; the identity it carries is only
|
|
// trusted from an authenticated relay, which this isn't
|
|
// acting as - nothing to record.
|
|
} else {
|
|
session.out << "555 5.5.4 Unsupported parameter " << param.first.c_str() << CRLF;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
session.from = arg.address.c_str();
|
|
session.recipientList.clear();
|
|
session.state = MAIL;
|
|
|
|
// Deliberately not gated on authentication: a receiving MX accepts
|
|
// MAIL from anyone. Authentication only gates relaying to a
|
|
// non-local recipient, which __SMTP_RCPT decides.
|
|
//
|
|
// SPF (RFC 7208 2.4): the identity is the MAIL FROM domain, or for
|
|
// the null reverse-path <> the HELO/EHLO domain. Computed here,
|
|
// where the RFC anchors it, and reused at DATA via spfHeader.
|
|
std::string fromStr = session.from.str();
|
|
size_t at = fromStr.find_last_of('@');
|
|
std::string spfDomain = (at == std::string::npos) ? session.clientDomainName.str() : fromStr.substr(at + 1);
|
|
std::string clientIP = session.ipAddress.getClientAddress();
|
|
session.spfDomain = spfDomain;
|
|
session.spfResult = SPFChecker::check(spfDomain, clientIP);
|
|
session.spfHeader = SPFChecker::receivedSPFHeader(session.spfResult, spfDomain, clientIP,
|
|
session.clientDomainName.str(), server.hostName);
|
|
|
|
session.out << "250 2.1.0 Sender OK" << CRLF;
|
|
return 1;
|
|
}
|
|
|
|
}
|