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:
1 parent
407cc2e915
commit
d3d7e5f3e5
27 files changed
+1738
-275
No files matched your search
@@ -18,6 +18,17 @@ namespace mail {
|
||||
void grabInput(SMTPSession &session);
|
||||
void clearGrab(SMTPSession &session);
|
||||
|
||||
///
|
||||
/// The whole command line as received. CommandList splits it only
|
||||
/// once ("VERB", "rest"), so request[1] doesn't exist at all on a
|
||||
/// bare "MAIL" - indexing it was an out-of-bounds read. Handlers
|
||||
/// parse from this instead.
|
||||
///
|
||||
|
||||
static std::string line(coreutils::ZString &request) {
|
||||
return std::string(request.getData(), request.getLength());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+42
-8
@@ -4,6 +4,8 @@
|
||||
#include "SpamAssassinFilter.h"
|
||||
#include "File.h"
|
||||
#include <thread>
|
||||
#include <sys/stat.h>
|
||||
#include "SMTPSyntax.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
@@ -22,6 +24,7 @@ namespace mail {
|
||||
commands.add(_smtp_quit, "QUIT");
|
||||
commands.add(_smtp_rcpt, "RCPT");
|
||||
commands.add(_smtp_rset, "RSET");
|
||||
commands.add(_smtp_starttls, "STARTTLS");
|
||||
commands.add(_smtp_vrfy, "VRFY");
|
||||
if (watchQueue) {
|
||||
processExisting();
|
||||
@@ -37,8 +40,31 @@ namespace mail {
|
||||
out << "252 " << errorString << CRLF;
|
||||
}
|
||||
|
||||
bool SMTPServer::isLocalDomain(const std::string &domain) {
|
||||
if (!SMTPSyntax::safePathComponent(domain))
|
||||
return false;
|
||||
struct stat st;
|
||||
std::string path = mailFileSystem.getMailPath().str() + "/" + domain;
|
||||
return stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
|
||||
}
|
||||
|
||||
std::string SMTPServer::postmasterAddress() {
|
||||
std::string domain = SMTPSyntax::lower(hostName);
|
||||
while (true) {
|
||||
coreutils::MString candidate;
|
||||
candidate << "postmaster@" << domain.c_str();
|
||||
if (isLocalDomain(domain) &&
|
||||
(mailFileSystem.ifMailBoxExists(candidate) || mailFileSystem.ifAliasExists(candidate)))
|
||||
return "postmaster@" + domain;
|
||||
size_t dot = domain.find('.');
|
||||
if (dot == std::string::npos || domain.find('.', dot + 1) == std::string::npos)
|
||||
return "";
|
||||
domain = domain.substr(dot + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void SMTPServer::deliverToMailbox(coreutils::ZString &mailbox, coreutils::ZString &messageId,
|
||||
SpamAssassinFilter::Result &scan) {
|
||||
SpamAssassinFilter::Result &scan, const std::string &sender) {
|
||||
// The destination folder has to be picked AFTER the scan runs,
|
||||
// not before - this used to always build an "/Inbox/" path
|
||||
// regardless of what spamd found, so a correctly-flagged spam
|
||||
@@ -54,6 +80,8 @@ namespace mail {
|
||||
<< ((scan.ok && scan.isSpam) ? "/Spam/" : "/Inbox/") << messageId;
|
||||
|
||||
coreutils::File mailFile(path, O_CREAT | O_WRONLY, 0660);
|
||||
std::string returnPath = "Return-Path: <" + sender + ">\r\n";
|
||||
mailFile.write(returnPath);
|
||||
mailFile.write(scan.scanned);
|
||||
|
||||
coreutils::Log(coreutils::LOG_INFO) << "Message " << messageId << " delivered to recipient " << mailbox
|
||||
@@ -67,13 +95,19 @@ namespace mail {
|
||||
// __SMTP_DATA::queueMail()'s own doc comment for why the sender
|
||||
// had to be added alongside the recipient.
|
||||
if(name.getList().size() > 2) {
|
||||
bool exists = mailFileSystem.ifMailBoxExists(name[1]);
|
||||
// Recipient and sender are %-encoded in the file name - see
|
||||
// SMTPSyntax::encodeQueueField(). Decoding a name written before
|
||||
// that change is a no-op unless it happened to contain "%".
|
||||
std::string senderAddress = SMTPSyntax::decodeQueueField(name[2].str());
|
||||
std::string recipientString = SMTPSyntax::decodeQueueField(name[1].str());
|
||||
coreutils::MString recipientName(recipientString);
|
||||
bool exists = mailFileSystem.ifMailBoxExists(recipientName);
|
||||
// An alias is a plain file (not a directory) at the same
|
||||
// level a mailbox would be - see MailFileSystem's own doc
|
||||
// comment on createAlias()/ifAliasExists(). Only checked when
|
||||
// it isn't a real mailbox, so a mailbox and an alias can never
|
||||
// both match the same address.
|
||||
bool isAlias = !exists && mailFileSystem.ifAliasExists(name[1]);
|
||||
bool isAlias = !exists && mailFileSystem.ifAliasExists(recipientName);
|
||||
coreutils::MString from;
|
||||
from << mailFileSystem.getQueuePath() << "/" << name;
|
||||
if(exists || isAlias) {
|
||||
@@ -93,11 +127,11 @@ namespace mail {
|
||||
// One shared scan result delivered to every target - an
|
||||
// alias should carry one spam verdict for the message, not
|
||||
// a separately re-scanned copy per target.
|
||||
std::vector<coreutils::MString> targets = mailFileSystem.getAliasMailboxes(name[1]);
|
||||
std::vector<coreutils::MString> targets = mailFileSystem.getAliasMailboxes(recipientName);
|
||||
for (auto &target : targets)
|
||||
deliverToMailbox(target, name[0], scan);
|
||||
deliverToMailbox(target, name[0], scan, senderAddress);
|
||||
} else {
|
||||
deliverToMailbox(name[1], name[0], scan);
|
||||
deliverToMailbox(recipientName, name[0], scan, senderAddress);
|
||||
}
|
||||
|
||||
int rc = unlink(from.c_str());
|
||||
@@ -108,8 +142,8 @@ namespace mail {
|
||||
// owns the heap-allocated SendMail itself and runs it on its
|
||||
// own detached thread rather than blocking this EPoll
|
||||
// worker thread for however long the remote host takes.
|
||||
coreutils::MString sender(name[2]);
|
||||
coreutils::MString recipient(name[1]);
|
||||
coreutils::MString sender(senderAddress);
|
||||
coreutils::MString recipient(recipientString);
|
||||
coreutils::File queuedFile(from, O_RDONLY);
|
||||
queuedFile.read();
|
||||
coreutils::MString mailData(queuedFile.asString());
|
||||
|
||||
+42
-1
@@ -21,6 +21,7 @@
|
||||
# include "__SMTP_QUIT.h"
|
||||
# include "__SMTP_RCPT.h"
|
||||
# include "__SMTP_RSET.h"
|
||||
# include "__SMTP_STARTTLS.h"
|
||||
# include "__SMTP_VRFY.h"
|
||||
# include "INotify.h"
|
||||
# include "SendMail.h"
|
||||
@@ -59,9 +60,46 @@ namespace mail {
|
||||
// the limit most real providers (e.g. Gmail) use.
|
||||
static const size_t maxMessageSize = 25 * 1024 * 1024;
|
||||
|
||||
// RFC 5321 4.5.3.1.8 requires accepting at least 100 recipients per
|
||||
// transaction; past this many, RCPT gets 452 4.5.3 and the client
|
||||
// sends the rest in another transaction.
|
||||
static const size_t maxRecipients = 1000;
|
||||
|
||||
SMTPSession * getSocketAccept(core::EPoll &ePoll) override;
|
||||
void sessionErrorHandler(std::string errorString, std::stringstream &out);
|
||||
|
||||
///
|
||||
/// When true (the default) and the session can be encrypted (it is
|
||||
/// already, or STARTTLS is available), AUTH is offered and accepted
|
||||
/// only once the session is encrypted, so mailbox passwords never
|
||||
/// cross the network in clear (RFC 4954 4 and 14). With no TLS
|
||||
/// certificate loaded at all, AUTH stays available in clear rather
|
||||
/// than locking mail clients out.
|
||||
///
|
||||
|
||||
bool requireTLSForAuth = true;
|
||||
|
||||
bool authAllowed(SMTPSession &session) {
|
||||
return session.isTLS() || (!startTLSContext && !ctx) || !requireTLSForAuth;
|
||||
}
|
||||
|
||||
///
|
||||
/// True if `domain` (lower case) is one this server holds mailboxes
|
||||
/// for - i.e. a domain directory exists under the mail path.
|
||||
///
|
||||
|
||||
bool isLocalDomain(const std::string &domain);
|
||||
|
||||
///
|
||||
/// Where mail for the bare "postmaster" address goes (RFC 5321 4.5.1:
|
||||
/// it MUST be accepted, with or without a domain). postmaster@ the
|
||||
/// server's own host name if that mailbox or alias exists, else the
|
||||
/// same for each parent domain (mail.barant.com, then barant.com).
|
||||
/// Empty if none exists anywhere.
|
||||
///
|
||||
|
||||
std::string postmasterAddress();
|
||||
|
||||
protected:
|
||||
void inCreate(coreutils::ZString &name);
|
||||
|
||||
@@ -77,6 +115,7 @@ namespace mail {
|
||||
__SMTP_QUIT _smtp_quit;
|
||||
__SMTP_RCPT _smtp_rcpt;
|
||||
__SMTP_RSET _smtp_rset;
|
||||
__SMTP_STARTTLS _smtp_starttls;
|
||||
__SMTP_VRFY _smtp_vrfy;
|
||||
int wd;
|
||||
|
||||
@@ -87,8 +126,10 @@ namespace mail {
|
||||
// (MailFileSystem::ifAliasExists()/getAliasMailboxes()) can
|
||||
// deliver the same scanned copy to each of its target mailboxes,
|
||||
// not just the single recipient a plain local delivery has.
|
||||
// `sender` is the envelope reverse-path, recorded as Return-Path:
|
||||
// (RFC 5321 4.4 - the final delivery server MUST add it).
|
||||
void deliverToMailbox(coreutils::ZString &mailbox, coreutils::ZString &messageId,
|
||||
SpamAssassinFilter::Result &scan);
|
||||
SpamAssassinFilter::Result &scan, const std::string &sender);
|
||||
|
||||
};
|
||||
|
||||
|
||||
+57
-2
@@ -5,14 +5,69 @@
|
||||
namespace mail {
|
||||
|
||||
SMTPSession::SMTPSession(core::EPoll &ePoll, core::TCPServer &server)
|
||||
: TCPSession(ePoll, &server, "SMTP Client Session") {}
|
||||
: TCPSession(ePoll, &server, "SMTP Client Session") {
|
||||
// Only CR LF ends a line - see core::TCPSocket::strictCRLF. This is
|
||||
// what closes SMTP smuggling: "<LF>.<LF>" (or any other bare CR/LF
|
||||
// variant) inside a message is message text, never end-of-data.
|
||||
strictCRLF = true;
|
||||
// Upper bound for one line of either a command or message text.
|
||||
// RFC 5322 2.1.1 caps a message line at 998 characters; this is far
|
||||
// above that and exists only so a peer that never sends CR LF can't
|
||||
// grow the line buffer without limit.
|
||||
maxLineLength = 1024 * 1024;
|
||||
}
|
||||
|
||||
void SMTPSession::onConnected() {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_2) << "onConnected -> sending 220";
|
||||
// RFC 5321 4.2 requires the greeting to name the server's own
|
||||
// domain - the literal "localhost" was hardcoded here regardless
|
||||
// of the hostName this server was actually configured with.
|
||||
out << "220 " << ((SMTPServer *)server)->hostName << " BMAMail" << CRLF;
|
||||
out << "220 " << ((SMTPServer *)server)->hostName << " ESMTP BARANTMail" << CRLF;
|
||||
}
|
||||
|
||||
void SMTPSession::resetTransaction() {
|
||||
from = "";
|
||||
recipientList.clear();
|
||||
mailData = "";
|
||||
mode = WAIT_FOR_DATA;
|
||||
state = greeted ? READY : CONNECT;
|
||||
spfHeader.clear();
|
||||
spfDomain.clear();
|
||||
spfResult = SPFChecker::Result::None;
|
||||
dataLineTooLong = false;
|
||||
queueId.clear();
|
||||
}
|
||||
|
||||
void SMTPSession::resetAll() {
|
||||
greeted = false;
|
||||
esmtp = false;
|
||||
resetTransaction();
|
||||
clientDomainName = "";
|
||||
userName = "";
|
||||
password = "";
|
||||
authState = USER_UNKNOWN;
|
||||
relay = false;
|
||||
}
|
||||
|
||||
void SMTPSession::protocol(coreutils::ZString &data) {
|
||||
if (grab == NULL && data.getLength() > maxCommandLine) {
|
||||
out << "500 5.5.6 Line too long" << CRLF;
|
||||
return;
|
||||
}
|
||||
if (!server->commands.processRequest(data, *this))
|
||||
out << "500 5.5.2 Syntax error, command unrecognized" << CRLF;
|
||||
}
|
||||
|
||||
void SMTPSession::onLineTooLong() {
|
||||
if (mode == RECEIVE_DATA) {
|
||||
// Can't reply in the middle of DATA (the client isn't reading
|
||||
// until it has sent the terminator) - remember it and reject the
|
||||
// message when the terminator arrives.
|
||||
dataLineTooLong = true;
|
||||
} else {
|
||||
out << "500 5.5.6 Line too long" << CRLF;
|
||||
send();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+46
-1
@@ -11,7 +11,9 @@
|
||||
namespace mail {
|
||||
|
||||
enum State {CONNECT, READY, READYX, MAIL, RCPT, DATA, SENT};
|
||||
enum AuthState {USER_UNKNOWN, USER_QUERY, USER_SECRET_QUERY, USER_KNOWN};
|
||||
// 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 {
|
||||
@@ -21,6 +23,37 @@ namespace mail {
|
||||
|
||||
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;
|
||||
@@ -49,6 +82,18 @@ namespace mail {
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+287
@@ -0,0 +1,287 @@
|
||||
#include "SMTPSyntax.h"
|
||||
#include <cctype>
|
||||
|
||||
namespace mail {
|
||||
|
||||
namespace SMTPSyntax {
|
||||
|
||||
bool iequals(const std::string &a, const std::string &b) {
|
||||
if (a.size() != b.size())
|
||||
return false;
|
||||
for (size_t i = 0; i < a.size(); ++i)
|
||||
if (std::tolower((unsigned char)a[i]) != std::tolower((unsigned char)b[i]))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string upper(std::string s) {
|
||||
for (char &c : s) c = std::toupper((unsigned char)c);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string lower(std::string s) {
|
||||
for (char &c : s) c = std::tolower((unsigned char)c);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string argument(const std::string &line) {
|
||||
size_t sp = line.find(' ');
|
||||
if (sp == std::string::npos)
|
||||
return "";
|
||||
size_t start = line.find_first_not_of(' ', sp);
|
||||
if (start == std::string::npos)
|
||||
return "";
|
||||
size_t end = line.find_last_not_of(' ');
|
||||
return line.substr(start, end - start + 1);
|
||||
}
|
||||
|
||||
// RFC 5322 3.2.3 atext
|
||||
static bool atext(unsigned char c) {
|
||||
return std::isalnum(c) || (c && std::string("!#$%&'*+-/=?^_`{|}~").find(c) != std::string::npos);
|
||||
}
|
||||
|
||||
static bool validDotString(const std::string &s) {
|
||||
if (s.empty() || s.front() == '.' || s.back() == '.')
|
||||
return false;
|
||||
for (size_t i = 0; i < s.size(); ++i) {
|
||||
unsigned char c = s[i];
|
||||
if (c == '.') {
|
||||
if (s[i + 1] == '.')
|
||||
return false;
|
||||
} else if (!atext(c) && c < 0x80) {
|
||||
return false;
|
||||
}
|
||||
// c >= 0x80: tolerated (UTF-8 local-parts, RFC 6531 style);
|
||||
// the server doesn't advertise SMTPUTF8 but rejecting them
|
||||
// outright helps nobody.
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool validQuotedString(const std::string &s) {
|
||||
if (s.size() < 2 || s.front() != '"' || s.back() != '"')
|
||||
return false;
|
||||
for (size_t i = 1; i + 1 < s.size(); ++i) {
|
||||
unsigned char c = s[i];
|
||||
if (c == '\\') {
|
||||
if (i + 2 >= s.size())
|
||||
return false;
|
||||
unsigned char n = s[++i];
|
||||
if (n < 32 || n > 126)
|
||||
return false;
|
||||
} else if (c == '"' || c < 32 || c == 127) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool validDomain(const std::string &d) {
|
||||
if (d.empty() || d.size() > 255)
|
||||
return false;
|
||||
if (d.front() == '[')
|
||||
return d.back() == ']' && d.size() > 2 &&
|
||||
d.find_first_of("[]\\", 1) == d.size() - 1; // address-literal; content not interpreted
|
||||
size_t start = 0;
|
||||
while (true) {
|
||||
size_t dot = d.find('.', start);
|
||||
std::string label = d.substr(start, dot == std::string::npos ? std::string::npos : dot - start);
|
||||
if (label.empty() || label.size() > 63 || label.front() == '-' || label.back() == '-')
|
||||
return false;
|
||||
for (unsigned char c : label)
|
||||
if (!std::isalnum(c) && c != '-' && c < 0x80)
|
||||
return false;
|
||||
if (dot == std::string::npos)
|
||||
return true;
|
||||
start = dot + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void splitAddress(const std::string &address, std::string &local, std::string &domain) {
|
||||
size_t at = address.rfind('@');
|
||||
if (at == std::string::npos) {
|
||||
local = address;
|
||||
domain = "";
|
||||
} else {
|
||||
local = address.substr(0, at);
|
||||
domain = lower(address.substr(at + 1));
|
||||
}
|
||||
}
|
||||
|
||||
bool validMailbox(const std::string &address) {
|
||||
std::string local, domain;
|
||||
splitAddress(address, local, domain);
|
||||
if (address.find('@') == std::string::npos || local.empty() || local.size() > 64)
|
||||
return false;
|
||||
if (!(local.front() == '"' ? validQuotedString(local) : validDotString(local)))
|
||||
return false;
|
||||
return validDomain(domain);
|
||||
}
|
||||
|
||||
bool safePathComponent(const std::string &s) {
|
||||
return !s.empty() && s != "." && s != ".." && s.front() != '.' &&
|
||||
s.find('/') == std::string::npos && s.find('\0') == std::string::npos;
|
||||
}
|
||||
|
||||
PathArgument parsePath(const std::string &arg, const std::string &keyword, bool allowEmpty) {
|
||||
PathArgument result;
|
||||
std::string usage = keyword == "FROM:" ? "501 5.5.4 Syntax: MAIL FROM:<address> [parameters]"
|
||||
: "501 5.5.4 Syntax: RCPT TO:<address> [parameters]";
|
||||
if (arg.size() < keyword.size() || !iequals(arg.substr(0, keyword.size()), keyword)) {
|
||||
result.reply = usage;
|
||||
return result;
|
||||
}
|
||||
size_t pos = keyword.size();
|
||||
if (pos < arg.size() && arg[pos] == ' ')
|
||||
++pos;
|
||||
if (pos >= arg.size() || arg[pos] != '<') {
|
||||
result.reply = usage;
|
||||
return result;
|
||||
}
|
||||
// Find the closing '>' - not inside a quoted local-part.
|
||||
size_t i = pos + 1;
|
||||
bool quoted = false;
|
||||
for (; i < arg.size(); ++i) {
|
||||
char c = arg[i];
|
||||
if (quoted && c == '\\') { ++i; continue; }
|
||||
if (c == '"') quoted = !quoted;
|
||||
else if (c == '>' && !quoted) break;
|
||||
}
|
||||
if (i >= arg.size()) {
|
||||
result.reply = usage;
|
||||
return result;
|
||||
}
|
||||
std::string path = arg.substr(pos + 1, i - pos - 1);
|
||||
// RFC 5321 4.1.2 / 3.3: a source route ("@a,@b:user@c") MUST be
|
||||
// accepted and SHOULD be ignored.
|
||||
if (!path.empty() && path.front() == '@') {
|
||||
size_t colon = path.find(':');
|
||||
if (colon == std::string::npos) {
|
||||
result.reply = usage;
|
||||
return result;
|
||||
}
|
||||
path = path.substr(colon + 1);
|
||||
}
|
||||
if (path.size() > 254) {
|
||||
result.reply = "501 5.1.3 Path too long";
|
||||
return result;
|
||||
}
|
||||
if (path.empty()) {
|
||||
if (!allowEmpty) {
|
||||
result.reply = "501 5.1.3 Empty forward-path";
|
||||
return result;
|
||||
}
|
||||
} else if (!(keyword == "TO:" && iequals(path, "postmaster")) && !validMailbox(path)) {
|
||||
result.reply = keyword == "FROM:" ? "501 5.1.7 Bad sender address syntax"
|
||||
: "501 5.1.3 Bad recipient address syntax";
|
||||
return result;
|
||||
}
|
||||
result.address = path;
|
||||
|
||||
// esmtp-param *(SP esmtp-param); esmtp-param = esmtp-keyword ["=" esmtp-value]
|
||||
size_t p = i + 1;
|
||||
while (p < arg.size()) {
|
||||
if (arg[p] != ' ') {
|
||||
result.reply = usage;
|
||||
return result;
|
||||
}
|
||||
while (p < arg.size() && arg[p] == ' ')
|
||||
++p;
|
||||
if (p >= arg.size())
|
||||
break;
|
||||
size_t end = arg.find(' ', p);
|
||||
std::string param = arg.substr(p, end == std::string::npos ? std::string::npos : end - p);
|
||||
p = end == std::string::npos ? arg.size() : end;
|
||||
size_t eq = param.find('=');
|
||||
std::string key = param.substr(0, eq), value = eq == std::string::npos ? "" : param.substr(eq + 1);
|
||||
bool keyOk = !key.empty() && std::isalnum((unsigned char)key[0]);
|
||||
for (unsigned char c : key)
|
||||
if (!std::isalnum(c) && c != '-')
|
||||
keyOk = false;
|
||||
bool valueOk = eq == std::string::npos || !value.empty();
|
||||
for (unsigned char c : value)
|
||||
if (c < 33 || c > 126 || c == '=')
|
||||
valueOk = false;
|
||||
if (!keyOk || !valueOk) {
|
||||
result.reply = "501 5.5.4 Malformed parameter";
|
||||
return result;
|
||||
}
|
||||
std::string K = upper(key);
|
||||
for (auto &existing : result.params)
|
||||
if (existing.first == K) {
|
||||
result.reply = "501 5.5.4 Duplicate parameter " + K;
|
||||
return result;
|
||||
}
|
||||
result.params.push_back({K, value});
|
||||
}
|
||||
result.ok = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool decodeBase64(const std::string &in, std::string &out) {
|
||||
out.clear();
|
||||
if (in.size() % 4 != 0)
|
||||
return false;
|
||||
auto val = [](char c) -> int {
|
||||
if (c >= 'A' && c <= 'Z') return c - 'A';
|
||||
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
||||
if (c >= '0' && c <= '9') return c - '0' + 52;
|
||||
if (c == '+') return 62;
|
||||
if (c == '/') return 63;
|
||||
return -1;
|
||||
};
|
||||
for (size_t i = 0; i < in.size(); i += 4) {
|
||||
int v[4];
|
||||
int pad = 0;
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
char c = in[i + k];
|
||||
if (c == '=') {
|
||||
// only in the last group, only in the last two places, only trailing
|
||||
if (i + 4 != in.size() || k < 2)
|
||||
return false;
|
||||
v[k] = 0;
|
||||
++pad;
|
||||
} else {
|
||||
if (pad)
|
||||
return false;
|
||||
v[k] = val(c);
|
||||
if (v[k] < 0)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
unsigned int n = (v[0] << 18) | (v[1] << 12) | (v[2] << 6) | v[3];
|
||||
out += (char)((n >> 16) & 0xff);
|
||||
if (pad < 2) out += (char)((n >> 8) & 0xff);
|
||||
if (pad < 1) out += (char)(n & 0xff);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string encodeQueueField(const std::string &s) {
|
||||
std::string out;
|
||||
for (char c : s) {
|
||||
if (c == '%') out += "%25";
|
||||
else if (c == '/') out += "%2F";
|
||||
else if (c == '|') out += "%7C";
|
||||
else out += c;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string decodeQueueField(const std::string &s) {
|
||||
std::string out;
|
||||
for (size_t i = 0; i < s.size(); ++i) {
|
||||
if (s[i] == '%' && i + 2 < s.size()) {
|
||||
std::string h = upper(s.substr(i + 1, 2));
|
||||
if (h == "25") { out += '%'; i += 2; continue; }
|
||||
if (h == "2F") { out += '/'; i += 2; continue; }
|
||||
if (h == "7C") { out += '|'; i += 2; continue; }
|
||||
}
|
||||
out += s[i];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#ifndef __SMTPSyntax_h__
|
||||
# define __SMTPSyntax_h__
|
||||
|
||||
# include <string>
|
||||
# include <utility>
|
||||
# include <vector>
|
||||
|
||||
namespace mail {
|
||||
|
||||
///
|
||||
/// SMTPSyntax
|
||||
///
|
||||
/// Parsing and validation shared by the SMTP command handlers, kept in
|
||||
/// one place so every command applies the same grammar (RFC 5321
|
||||
/// 4.1.2) rather than each handler doing its own partial version.
|
||||
/// Pure functions - no server or session state.
|
||||
///
|
||||
|
||||
namespace SMTPSyntax {
|
||||
|
||||
///
|
||||
/// The parsed argument of "MAIL FROM:<path> params" or
|
||||
/// "RCPT TO:<path> params". On failure `reply` holds the complete
|
||||
/// reply line to send (code, enhanced code and text, no CRLF).
|
||||
///
|
||||
|
||||
struct PathArgument {
|
||||
bool ok = false;
|
||||
std::string reply;
|
||||
std::string address; // contents of <...>, may be empty for MAIL FROM:<>
|
||||
std::vector<std::pair<std::string, std::string>> params; // keyword upper-cased
|
||||
};
|
||||
|
||||
///
|
||||
/// The argument text after a command verb: everything past the
|
||||
/// first run of spaces. "" when there is none.
|
||||
///
|
||||
|
||||
std::string argument(const std::string &line);
|
||||
|
||||
///
|
||||
/// Parse the argument of MAIL ("FROM:") or RCPT ("TO:"). The
|
||||
/// keyword is matched case-insensitively (RFC 5321 2.4); a single
|
||||
/// space after the colon is tolerated, as most servers do, though
|
||||
/// the grammar has none. Parameters are checked for esmtp-param
|
||||
/// syntax only; deciding which ones are supported is the caller's.
|
||||
///
|
||||
|
||||
PathArgument parsePath(const std::string &argument, const std::string &keyword, bool allowEmpty);
|
||||
|
||||
///
|
||||
/// RFC 5321 4.1.2 Mailbox (Local-part "@" ( Domain / address-literal ))
|
||||
/// with the 4.5.3.1 size limits (local-part 64, domain 255).
|
||||
///
|
||||
|
||||
bool validMailbox(const std::string &address);
|
||||
|
||||
///
|
||||
/// Split "local@domain" at the LAST "@" (a quoted local-part may
|
||||
/// contain one). Domain is lower-cased - domains are case-insensitive
|
||||
/// (RFC 5321 2.4) and the mail store keys directories on them.
|
||||
///
|
||||
|
||||
void splitAddress(const std::string &address, std::string &local, std::string &domain);
|
||||
|
||||
///
|
||||
/// True when a local-part/domain can be used as a path component
|
||||
/// under the mail store without escaping it ("/" and NUL are legal
|
||||
/// in an RFC 5321 local-part, but not as a directory name, and "."/
|
||||
/// ".." would walk the tree).
|
||||
///
|
||||
|
||||
bool safePathComponent(const std::string &s);
|
||||
|
||||
bool iequals(const std::string &a, const std::string &b);
|
||||
std::string upper(std::string s);
|
||||
std::string lower(std::string s);
|
||||
|
||||
///
|
||||
/// Strict RFC 4648 base64 decode, as RFC 4954 4 requires for SASL
|
||||
/// responses: returns false for any character outside the alphabet,
|
||||
/// bad padding, or a length that isn't a multiple of 4.
|
||||
///
|
||||
|
||||
bool decodeBase64(const std::string &in, std::string &out);
|
||||
|
||||
///
|
||||
/// The queue file name is "<id>|<recipient>|<sender>" (see
|
||||
/// __SMTP_DATA::queueMail()). "/" and "|" are both legal in an
|
||||
/// address, and either one breaks that name - a "/" turns it into
|
||||
/// a path into some other directory, a "|" shifts the fields - so
|
||||
/// the address parts are %-encoded ("%", "/", "|") on the way in
|
||||
/// and decoded by SMTPServer::inCreate() on the way out.
|
||||
///
|
||||
|
||||
std::string encodeQueueField(const std::string &s);
|
||||
std::string decodeQueueField(const std::string &s);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
+139
-82
@@ -1,108 +1,165 @@
|
||||
#include "__SMTP_AUTH.h"
|
||||
#include "Base64.h"
|
||||
#include "SMTPServer.h"
|
||||
#include "SMTPSyntax.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_AUTH::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
// 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.
|
||||
|
||||
coreutils::Base64 base64;
|
||||
|
||||
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);
|
||||
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";
|
||||
}
|
||||
}
|
||||
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;
|
||||
void __SMTP_AUTH::finish(SMTPSession &session, const char *reply) {
|
||||
session.out << reply << CRLF;
|
||||
session.authState = USER_UNKNOWN;
|
||||
session.userName = "";
|
||||
session.password = "";
|
||||
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);
|
||||
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.relay = true;
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,11 @@ namespace mail {
|
||||
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
|
||||
bool authLogin(coreutils::ZString &userName, coreutils::ZString &password, SMTPServer &server);
|
||||
|
||||
private:
|
||||
void finish(SMTPSession &session, const char *reply);
|
||||
void verify(SMTPSession &session, SMTPServer &server);
|
||||
void plain(const std::string &response, SMTPSession &session, SMTPServer &server);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+84
-90
@@ -2,6 +2,7 @@
|
||||
#include "SMTPServer.h"
|
||||
#include "DKIMVerifier.h"
|
||||
#include "DMARCChecker.h"
|
||||
#include "SMTPSyntax.h"
|
||||
#include "Log.h"
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
@@ -69,12 +70,13 @@ namespace mail {
|
||||
|
||||
}
|
||||
|
||||
// RFC 5321 4.4: every SMTP server MUST prepend a Received trace
|
||||
// header to a message it processes - this codebase never wrote one
|
||||
// at all before. Deliberately no "for <recipient>" clause: one
|
||||
// already-written queue file gets hardlinked to every recipient (see
|
||||
// queueMail() below), and RFC 5321 4.4 itself says that clause
|
||||
// SHOULD be omitted once there's more than one anyway.
|
||||
// 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;
|
||||
@@ -82,82 +84,84 @@ namespace mail {
|
||||
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 << CRLF;
|
||||
header << "\tby " << server.hostName.c_str() << " with SMTP;" << CRLF;
|
||||
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) {
|
||||
|
||||
switch(session.mode) {
|
||||
|
||||
case WAIT_FOR_DATA:
|
||||
|
||||
switch(session.state) {
|
||||
|
||||
case RCPT:
|
||||
session.out << "354 Enter the mail message terminated by <CRLF>.<CRLF>" << CRLF;
|
||||
// RFC 5321 4.4 requires a Received trace header on every message
|
||||
// this server processes - previously absent entirely. Written
|
||||
// once here as the seed of mailData so the body lines that
|
||||
// follow just accumulate after it; omits a "for <recipient>"
|
||||
// clause since one already-written file gets hardlinked to every
|
||||
// recipient (see queueMail() below) and RFC 5321 4.4 itself says
|
||||
// that clause SHOULD be left out once there's more than one.
|
||||
// session.spfHeader was computed once already, back at MAIL
|
||||
// FROM time (see __SMTP_MAIL.cpp) - reused here rather than
|
||||
// re-running the SPF DNS lookups a second time for the same
|
||||
// transaction. Placed directly after Received:, matching
|
||||
// where real-world MTAs conventionally put it.
|
||||
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);
|
||||
// session.setTimer(120.0f);
|
||||
break;
|
||||
|
||||
case MAIL:
|
||||
session.out << "503 Please use RCPT first" << CRLF;
|
||||
break;
|
||||
|
||||
default:
|
||||
session.out << "503 Please use MAIL first" << CRLF;
|
||||
break;
|
||||
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case RECEIVE_DATA:
|
||||
// ---- 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 != ".") {
|
||||
// RFC 5321 4.5.2 transparency: a line starting with '.' that
|
||||
// isn't the bare terminator has been stuffed with an extra
|
||||
// leading '.' by a compliant sender - strip exactly one before
|
||||
// storing, or every such line comes out with a spurious
|
||||
// leading dot baked permanently into the stored message body.
|
||||
coreutils::ZString line = request.startsWith(".")
|
||||
? coreutils::ZString(request.getData() + 1, request.getLength() - 1)
|
||||
: request;
|
||||
// Cap accumulation at the advertised SIZE limit rather than
|
||||
// growing mailData without bound - still consuming every line
|
||||
// the client sends either way (RFC 5321 doesn't support a
|
||||
// synchronous mid-DATA reply; the client isn't reading
|
||||
// anything back until its own terminating "." goes out), so
|
||||
// this only bounds our own memory use. The client actually
|
||||
// finds out via the 552 reply below, once that terminator
|
||||
// arrives.
|
||||
if (session.mailData.getLength() < SMTPServer::maxMessageSize)
|
||||
session.mailData << line << CRLF;
|
||||
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;
|
||||
}
|
||||
else {
|
||||
session.mode = WAIT_FOR_DATA;
|
||||
session.state = READY;
|
||||
text.swap(normalized);
|
||||
}
|
||||
text += "\r\n";
|
||||
session.mailData << text.c_str();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ---- end of data
|
||||
clearGrab(session);
|
||||
if (session.mailData.getLength() >= SMTPServer::maxMessageSize) {
|
||||
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 if(session.recipientList.size() > 0) {
|
||||
if(session.mailData.getLength() > 0) {
|
||||
} else {
|
||||
std::string message = session.mailData.str();
|
||||
|
||||
// DKIM verification + DMARC alignment, now that the full
|
||||
@@ -191,22 +195,15 @@ namespace mail {
|
||||
coreutils::MString withAuthResults(authResults);
|
||||
withAuthResults << session.mailData;
|
||||
session.mailData = withAuthResults;
|
||||
|
||||
coreutils::MString ID = queueMail(server, session.from, session.recipientList, session.mailData);
|
||||
if(ID != "") {
|
||||
coreutils::Log(coreutils::LOG_INFO) << "Queued message " << ID << ".";
|
||||
session.out << "250 OK Queued message " << ID << CRLF;
|
||||
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;
|
||||
}
|
||||
else
|
||||
session.out << "550 Mail message too big" << CRLF;
|
||||
}
|
||||
} else
|
||||
session.out << "550 Mail message was empty" << CRLF;
|
||||
} else
|
||||
session.out << "250 OK Queued message " << CRLF;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// RFC 5321 4.1.1.4: the transaction is over whatever the outcome.
|
||||
session.resetTransaction();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -218,30 +215,27 @@ namespace mail {
|
||||
return coreutils::MString(temp.str());
|
||||
}
|
||||
|
||||
coreutils::MString __SMTP_DATA::queueMail(SMTPServer &server,
|
||||
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/" << generateMailFileName();
|
||||
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;
|
||||
// Also encode the envelope sender, not just the recipient -
|
||||
// SMTPServer::inCreate() needs the real MAIL FROM address to
|
||||
// hand outbound (non-local) mail to SendMail, and the filename
|
||||
// is the only thing that survives from this queuing step to
|
||||
// that point.
|
||||
newName << fileName << "|" << recipient << "|" << sender;
|
||||
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());
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ namespace mail {
|
||||
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
|
||||
|
||||
coreutils::MString generateMailFileName();
|
||||
coreutils::MString queueMail(SMTPServer &server, coreutils::MString &sender, std::vector<coreutils::MString> &recipientList, coreutils::MString &mailData);
|
||||
void queueMail(SMTPServer &server, const std::string &id, coreutils::MString &sender, std::vector<coreutils::MString> &recipientList, coreutils::MString &mailData);
|
||||
coreutils::MString receivedHeader(SMTPSession &session, SMTPServer &server);
|
||||
|
||||
};
|
||||
|
||||
+25
-16
@@ -1,34 +1,43 @@
|
||||
#include "__SMTP_EHLO.h"
|
||||
#include "SMTPServer.h"
|
||||
#include "SMTPSyntax.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_EHLO::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
|
||||
// RFC 5321 4.1.1.1 requires a domain/address-literal argument -
|
||||
// request[1] on a bare "EHLO" with none would have been an
|
||||
// out-of-bounds read on the split list (ZString::operator[] does
|
||||
// no bounds checking), not a graceful rejection.
|
||||
if (request.getList().size() < 2) {
|
||||
session.out << "501 Syntax error: EHLO requires a domain/address-literal argument" << CRLF;
|
||||
// RFC 5321 4.1.1.1 requires a domain/address-literal argument.
|
||||
std::string arg = SMTPSyntax::argument(line(request));
|
||||
if (arg.empty()) {
|
||||
session.out << "501 5.5.4 Syntax: EHLO hostname" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
session.clientDomainName = request[1];
|
||||
// EHLO also aborts any transaction in progress (RFC 5321 4.1.4).
|
||||
session.greeted = true;
|
||||
session.esmtp = true;
|
||||
session.resetTransaction();
|
||||
session.clientDomainName = arg.substr(0, arg.find(' ')).c_str();
|
||||
|
||||
session.out << "250-" << server.hostName << CRLF;
|
||||
// Both already true of this implementation, not just advertised
|
||||
// hopefully: multiple pipelined command lines arriving in one TCP
|
||||
// segment are already handled correctly (TCPSocket::onDataReceived
|
||||
// loops over every complete line it has buffered before returning,
|
||||
// not just the first), and message bodies are stored byte-for-byte
|
||||
// with no 7-bit stripping anywhere in this codebase.
|
||||
// Multiple pipelined command lines arriving in one read are
|
||||
// processed in order (TCPSocket::onDataReceived loops over every
|
||||
// complete line it has), and replies go out in the same order.
|
||||
session.out << "250-PIPELINING" << CRLF;
|
||||
// Message bodies are stored byte-for-byte; no 7-bit stripping.
|
||||
session.out << "250-8BITMIME" << CRLF;
|
||||
// Enforced by __SMTP_DATA.cpp while accumulating the body.
|
||||
// Declared in MAIL FROM (SIZE=) and enforced while receiving DATA.
|
||||
session.out << "250-SIZE " << (long)SMTPServer::maxMessageSize << CRLF;
|
||||
session.out << "250-AUTH LOGIN" << CRLF;
|
||||
// Every 2xx/4xx/5xx reply after this one carries a RFC 3463 code.
|
||||
session.out << "250-ENHANCEDSTATUSCODES" << CRLF;
|
||||
// RFC 3207 4.2: only while the session is still in clear text.
|
||||
if (server.startTLSContext && !session.isTLS())
|
||||
session.out << "250-STARTTLS" << CRLF;
|
||||
// RFC 4954 4/14: no plain-text password mechanisms over an
|
||||
// unencrypted connection when encryption is available.
|
||||
if (server.authAllowed(session))
|
||||
session.out << "250-AUTH PLAIN LOGIN" << CRLF;
|
||||
session.out << "250 HELP" << CRLF;
|
||||
session.state = READY;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
+4
-5
@@ -4,11 +4,10 @@
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_EXPN::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
session.out << "252 You must know recipient." << CRLF;
|
||||
// EXPN is usable at any point in a session (RFC 5321 4.1.1) and
|
||||
// must not disturb an in-progress MAIL/RCPT transaction - this
|
||||
// used to reset state to READY unconditionally, silently
|
||||
// aborting whatever transaction was already underway.
|
||||
// EXPN is optional (RFC 5321 4.5.1, 3.5.2) and commonly disabled so
|
||||
// alias membership can't be enumerated. It must not disturb a
|
||||
// transaction in progress (4.1.1).
|
||||
session.out << "502 5.5.1 EXPN not available" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
+12
-3
@@ -1,14 +1,23 @@
|
||||
#include "__SMTP_HELO.h"
|
||||
#include "SMTPServer.h"
|
||||
#include "SMTPSyntax.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_HELO::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
session.clientDomainName = request[1];
|
||||
// RFC 5321 4.1.1.1: HELO requires a domain argument. request[1] on
|
||||
// a bare "HELO" was an out-of-bounds read.
|
||||
std::string arg = SMTPSyntax::argument(line(request));
|
||||
if (arg.empty()) {
|
||||
session.out << "501 Syntax: HELO hostname" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
session.greeted = true;
|
||||
session.esmtp = false;
|
||||
session.resetTransaction();
|
||||
session.clientDomainName = arg.substr(0, arg.find(' ')).c_str();
|
||||
session.out << "250 " << server.hostName << CRLF;
|
||||
session.state = READY;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-4
@@ -4,10 +4,11 @@
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_HELP::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
session.out << "214 " << server.hostName << " - see RFC 5321 for a list of commands." << CRLF;
|
||||
// HELP is usable at any point in a session and must not disturb
|
||||
// an in-progress MAIL/RCPT transaction - same reasoning as
|
||||
// __SMTP_EXPN.cpp's own fix.
|
||||
// Usable at any point; must not disturb a transaction (RFC 5321 4.1.1).
|
||||
session.out << "214-2.0.0 " << server.hostName << " BARANTMail. Commands:" << CRLF;
|
||||
session.out << "214-2.0.0 EHLO HELO MAIL RCPT DATA RSET NOOP QUIT VRFY HELP" << CRLF;
|
||||
session.out << "214-2.0.0 STARTTLS AUTH" << CRLF;
|
||||
session.out << "214 2.0.0 End of HELP" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
+64
-35
@@ -1,44 +1,76 @@
|
||||
#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) {
|
||||
|
||||
if(request[1].ifNext("FROM:")) {
|
||||
request[1].skipWhitespace();
|
||||
if(request[1].ifNext("<")) {
|
||||
session.from = request[1].getTokenExclude(">");
|
||||
// Deliberately unconditional - not gated on authState. A
|
||||
// receiving MX server accepts MAIL FROM addressed to its own
|
||||
// local mailboxes from anyone; sending MTAs never AUTH to a
|
||||
// destination domain, only to their own outbound smarthost.
|
||||
// Authentication only needs to gate relaying to a NON-local
|
||||
// recipient, which __SMTP_RCPT.cpp already does via
|
||||
// session.relay. This used to be
|
||||
// `if(session.authState = USER_KNOWN)` - an assignment, not
|
||||
// a comparison - which accidentally produced this same
|
||||
// "always accept" behavior; a literal `==` fix would instead
|
||||
// have started rejecting all normal inbound mail.
|
||||
session.out << "250 OK" << CRLF;
|
||||
// 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;
|
||||
|
||||
// RFC 7208 2.4: the identity to check is the MAIL FROM
|
||||
// address's own domain, or - for the null reverse-path <>
|
||||
// bounce messages use, which has no domain of its own - the
|
||||
// HELO/EHLO domain instead. Computed here (not deferred to
|
||||
// DATA) since MAIL FROM is where the RFC actually anchors the
|
||||
// check; DATA just reuses the result via session.spfHeader
|
||||
// rather than re-running the DNS lookups a second time. This
|
||||
// does block this connection's EPoll worker thread for
|
||||
// however long the SPF DNS lookups take (matching
|
||||
// SendMail::lookupMX()'s own res_query() calls, which are
|
||||
// synchronous the same way) - acceptable for a well-behaved
|
||||
// sender domain, not for a slow/hostile one, same tradeoff
|
||||
// already accepted elsewhere in this codebase rather than a
|
||||
// new one introduced here.
|
||||
// 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);
|
||||
@@ -47,12 +79,9 @@ namespace mail {
|
||||
session.spfResult = SPFChecker::check(spfDomain, clientIP);
|
||||
session.spfHeader = SPFChecker::receivedSPFHeader(session.spfResult, spfDomain, clientIP,
|
||||
session.clientDomainName.str(), server.hostName);
|
||||
} else
|
||||
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
|
||||
} else
|
||||
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
|
||||
|
||||
session.out << "250 2.1.0 Sender OK" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-1
@@ -3,7 +3,8 @@
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_NOOP::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
session.out << "250 OK" << CRLF;
|
||||
// RFC 5321 4.1.1.9: any parameter string is ignored.
|
||||
session.out << "250 2.0.0 OK" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -5,10 +5,9 @@
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_QUIT::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
session.out << "221 " << server.hostName << CRLF;
|
||||
session.out << "221 2.0.0 " << server.hostName << " closing connection" << CRLF;
|
||||
session.terminate();
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+69
-20
@@ -1,30 +1,79 @@
|
||||
# include "__SMTP_RCPT.h"
|
||||
# include "SMTPServer.h"
|
||||
# include "SMTPSyntax.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_RCPT::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
if((session.state == MAIL) || (session.state == RCPT)) {
|
||||
if(request[1].ifNext("TO:")) {
|
||||
request[1].skipWhitespace();
|
||||
if(request[1].ifNext("<")) {
|
||||
coreutils::MString recipient(request[1].getTokenExclude(">"));
|
||||
if(server.mailFileSystem.ifMailBoxExists(recipient)) {
|
||||
session.recipientList.push_back(recipient);
|
||||
session.out << "250 OK" << CRLF;
|
||||
|
||||
if (session.state != MAIL && session.state != RCPT) {
|
||||
session.out << "503 5.5.1 Need MAIL command first" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
SMTPSyntax::PathArgument arg = SMTPSyntax::parsePath(SMTPSyntax::argument(line(request)), "TO:", false);
|
||||
if (!arg.ok) {
|
||||
session.out << arg.reply << CRLF;
|
||||
return 1;
|
||||
}
|
||||
// No RCPT parameters are offered (no DSN) - RFC 5321 4.1.1.11.
|
||||
if (!arg.params.empty()) {
|
||||
session.out << "555 5.5.4 Unsupported parameter " << arg.params[0].first.c_str() << CRLF;
|
||||
return 1;
|
||||
}
|
||||
if (session.recipientList.size() >= SMTPServer::maxRecipients) {
|
||||
session.out << "452 4.5.3 Too many recipients" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string local, domain;
|
||||
SMTPSyntax::splitAddress(arg.address, local, domain);
|
||||
|
||||
// RFC 5321 4.5.1: "postmaster" with no domain MUST be accepted, and
|
||||
// the postmaster local-part is case-insensitive at any local domain.
|
||||
bool isPostmaster = SMTPSyntax::iequals(local, "postmaster") && (domain.empty() || server.isLocalDomain(domain));
|
||||
std::string recipient = local + "@" + domain;
|
||||
|
||||
if (isPostmaster) {
|
||||
recipient = "postmaster@" + domain;
|
||||
coreutils::MString r(recipient);
|
||||
if (domain.empty() || !(server.mailFileSystem.ifMailBoxExists(r) || server.mailFileSystem.ifAliasExists(r)))
|
||||
recipient = server.postmasterAddress();
|
||||
if (recipient.empty()) {
|
||||
// Nothing to deliver it to - a configuration gap, not the
|
||||
// client's fault (create a postmaster mailbox or alias).
|
||||
session.out << "451 4.3.0 Postmaster mailbox not configured" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
} else if (server.isLocalDomain(domain)) {
|
||||
// Our own domain: the mailbox or alias must exist. Local-parts
|
||||
// that can't be a directory name ("/", ".", "..") can't be one.
|
||||
// Mailbox directories are lower case; a sender writing
|
||||
// "Brad@..." should still reach "brad" (RFC 5321 2.4 lets a
|
||||
// server treat local-parts case-insensitively, and nearly all do).
|
||||
auto known = [&](const std::string &address) {
|
||||
coreutils::MString r(address);
|
||||
return server.mailFileSystem.ifMailBoxExists(r) || server.mailFileSystem.ifAliasExists(r);
|
||||
};
|
||||
std::string lowered = SMTPSyntax::lower(local) + "@" + domain;
|
||||
if (!SMTPSyntax::safePathComponent(local))
|
||||
recipient.clear();
|
||||
else if (!known(recipient))
|
||||
recipient = known(lowered) ? lowered : "";
|
||||
if (recipient.empty()) {
|
||||
session.out << "550 5.1.1 No such user here" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
} else if (!session.relay) {
|
||||
// Someone else's domain and the client hasn't authenticated:
|
||||
// relaying it would make this an open relay (RFC 5321 3.6.2, 7.1).
|
||||
session.out << "550 5.7.1 Relaying denied" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
session.recipientList.push_back(coreutils::MString(recipient));
|
||||
session.state = RCPT;
|
||||
} else if(session.relay) {
|
||||
session.recipientList.push_back(recipient);
|
||||
session.out << "250 OK" << CRLF;
|
||||
session.state = RCPT;
|
||||
} else
|
||||
session.out << "550 Mailbox does not exist" << CRLF;
|
||||
} else
|
||||
session.out << "550 Usage: RCPT TO:<email-address>" << CRLF;
|
||||
} else
|
||||
session.out << "550 Usage: RCPT TO:<email-address>" << CRLF;
|
||||
} else
|
||||
session.out << "503 Please use MAIL first" << CRLF;
|
||||
session.out << "250 2.1.5 Recipient OK" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
+11
-2
@@ -1,10 +1,19 @@
|
||||
#include "__SMTP_RSET.h"
|
||||
#include "SMTPCommand.h"
|
||||
#include "SMTPSyntax.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_RSET::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
session.state = READY;
|
||||
session.out << "250 OK" << CRLF;
|
||||
// RFC 5321 4.1.1.5: "RSET" CRLF - no parameters.
|
||||
if (!SMTPSyntax::argument(line(request)).empty()) {
|
||||
session.out << "501 5.5.4 Syntax: RSET (no parameters)" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
// Clears the transaction (sender, recipients, data) but keeps the
|
||||
// EHLO/HELO and any authentication.
|
||||
session.resetTransaction();
|
||||
session.out << "250 2.0.0 OK" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "__SMTP_STARTTLS.h"
|
||||
#include "SMTPServer.h"
|
||||
#include "SMTPSyntax.h"
|
||||
#include "Log.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_STARTTLS::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
|
||||
if (session.isTLS()) {
|
||||
session.out << "503 5.5.1 TLS already active" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
if (!server.startTLSContext) {
|
||||
session.out << "454 4.7.0 TLS not available due to temporary reason" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
// RFC 3207 4: "STARTTLS" CRLF - no parameters.
|
||||
if (!SMTPSyntax::argument(line(request)).empty()) {
|
||||
session.out << "501 5.5.4 Syntax error (no parameters allowed)" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// RFC 3207 4.2: once the handshake starts the server MUST discard
|
||||
// everything it learned from the client before it - the EHLO name,
|
||||
// any AUTH, any transaction - none of it was protected. The client
|
||||
// sends EHLO again over the encrypted channel.
|
||||
session.resetAll();
|
||||
|
||||
session.out << "220 2.0.0 Ready to start TLS" << CRLF;
|
||||
// Sends the 220 in clear, drops anything the client pipelined
|
||||
// after STARTTLS (RFC 3207 4 forbids it; acting on it is the
|
||||
// CVE-2011-0411 injection hole), then starts the handshake.
|
||||
if (!session.startTLS(server.startTLSContext))
|
||||
coreutils::Log(coreutils::LOG_INFO) << "STARTTLS upgrade failed; connection closed.";
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef ____SMTP_STARTTLS_h__
|
||||
# define ____SMTP_STARTTLS_h__
|
||||
|
||||
# include "SMTPCommand.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
class SMTPServer;
|
||||
|
||||
///
|
||||
/// STARTTLS (RFC 3207). Upgrades the current plain-text session to TLS
|
||||
/// using the server's startTLSContext (see main() and
|
||||
/// core::TCPServer::enableStartTLS()).
|
||||
///
|
||||
|
||||
class __SMTP_STARTTLS : public SMTPCommand {
|
||||
|
||||
public:
|
||||
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
+10
-2
@@ -1,11 +1,19 @@
|
||||
#include "__SMTP_VRFY.h"
|
||||
#include "SMTPCommand.h"
|
||||
#include "SMTPSyntax.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_VRFY::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
session.out << "252 You must know who the mail is for" << CRLF;
|
||||
// RFC 5321 3.5.3: 252 means "won't verify, but will accept mail for
|
||||
// it and try" - VRFY stays implemented (4.5.1 requires it) without
|
||||
// turning it into a mailbox-enumeration oracle.
|
||||
if (SMTPSyntax::argument(line(request)).empty()) {
|
||||
session.out << "501 5.5.4 Syntax: VRFY address" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
session.out << "252 2.5.2 Cannot VRFY user, but will accept message and attempt delivery" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -162,6 +162,23 @@ int main(int argc, char **argv) {
|
||||
core::IPAddress(ipAddress, config.getSmtpPort()), nullptr, true,
|
||||
dkimSigners);
|
||||
|
||||
// STARTTLS on port 25 (RFC 3207), using the same certificate as the
|
||||
// TLS listeners. Peer MTAs connect in clear and upgrade; a
|
||||
// certificate that can't be loaded here is logged and port 25
|
||||
// simply carries on without offering STARTTLS - inbound mail must
|
||||
// keep flowing regardless. The certificate has to name the host
|
||||
// other servers connect to (the MX). The running server keeps the
|
||||
// certificate it loaded at startup, so restart after a renewal.
|
||||
if (config.getTlsInfo()) {
|
||||
try {
|
||||
smtpServer.enableStartTLS(*config.getTlsInfo());
|
||||
coreutils::Log(coreutils::LOG_INFO) << "SMTP STARTTLS enabled on port " << config.getSmtpPort() << ".";
|
||||
}
|
||||
catch (coreutils::Exception &exception) {
|
||||
coreutils::Log(coreutils::LOG_EXCEPT) << "SMTP STARTTLS disabled: " << exception.text;
|
||||
}
|
||||
}
|
||||
|
||||
mail::SMTPServer *smtpTLSServer = nullptr;
|
||||
if (config.hasSmtpTLS())
|
||||
smtpTLSServer = new mail::SMTPServer(ePoll, hostName, mailFileSystem,
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""IMAPS/POP3S regression check. Usage: imap_pop3_regression.py <imaps_port> <pop3s_port>"""
|
||||
import imaplib, poplib, ssl, sys, threading
|
||||
|
||||
IMAPS, POP3S = int(sys.argv[1]), int(sys.argv[2])
|
||||
USER, PASSWORD = "brad@mail.test", "secret"
|
||||
ctx = ssl.create_default_context(); ctx.check_hostname = False; ctx.verify_mode = ssl.CERT_NONE
|
||||
results = []
|
||||
|
||||
def check(name, ok, detail=""):
|
||||
results.append(ok)
|
||||
print(("PASS " if ok else "FAIL ") + name + ("" if ok else " -> " + repr(detail)[:200]))
|
||||
|
||||
def imap_session():
|
||||
m = imaplib.IMAP4_SSL("127.0.0.1", IMAPS, ssl_context=ctx, timeout=10)
|
||||
m.login(USER, PASSWORD)
|
||||
typ, data = m.select("INBOX")
|
||||
n = int(data[0])
|
||||
body = None
|
||||
if n:
|
||||
typ, msg = m.fetch(b"1", "(BODY.PEEK[])")
|
||||
body = msg[0][1]
|
||||
m.logout()
|
||||
return n, body
|
||||
|
||||
try:
|
||||
n, body = imap_session()
|
||||
check("IMAPS login/select/fetch (INBOX has %d messages)" % n, n > 0 and body and body.startswith(b"Return-Path:") or n > 0, (n, (body or b"")[:80]))
|
||||
except Exception as e:
|
||||
check("IMAPS login/select/fetch", False, e)
|
||||
|
||||
try:
|
||||
p = poplib.POP3_SSL("127.0.0.1", POP3S, context=ctx, timeout=10)
|
||||
p.user(USER); p.pass_(PASSWORD)
|
||||
count, size = p.stat()
|
||||
resp, lines, octets = p.retr(1)
|
||||
p.quit()
|
||||
check("POP3S login/stat/retr (%d messages)" % count, count > 0 and len(lines) > 0, (count, lines[:2]))
|
||||
except Exception as e:
|
||||
check("POP3S login/stat/retr", False, e)
|
||||
|
||||
errors = []
|
||||
def worker():
|
||||
try:
|
||||
imap_session()
|
||||
except Exception as e:
|
||||
errors.append(repr(e))
|
||||
ts = [threading.Thread(target=worker) for _ in range(30)]
|
||||
[t.start() for t in ts]; [t.join(30) for t in ts]
|
||||
check("30 concurrent IMAPS sessions", not errors, errors[:3])
|
||||
|
||||
print("%d/%d passed" % (sum(results), len(results)))
|
||||
sys.exit(0 if all(results) else 1)
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Minimal DNS responder for tests: answers every query NXDOMAIN (UDP and TCP on 127.0.0.1:53)."""
|
||||
import socket, socketserver, struct, threading
|
||||
|
||||
def nx(query):
|
||||
if len(query) < 12:
|
||||
return None
|
||||
qid, flags = struct.unpack(">HH", query[:4])
|
||||
rd = flags & 0x0100
|
||||
# QR=1, opcode copied, AA=1, RD copied, RA=1, RCODE=3 (NXDOMAIN)
|
||||
rflags = 0x8000 | (flags & 0x7800) | 0x0400 | rd | 0x0080 | 3
|
||||
qd = struct.unpack(">H", query[4:6])[0]
|
||||
# find end of question section
|
||||
i, n = 12, 0
|
||||
while n < qd and i < len(query):
|
||||
while i < len(query) and query[i] != 0:
|
||||
i += query[i] + 1
|
||||
i += 5
|
||||
n += 1
|
||||
return struct.pack(">HHHHHH", qid, rflags, qd, 0, 0, 0) + query[12:i]
|
||||
|
||||
class UDP(socketserver.BaseRequestHandler):
|
||||
def handle(self):
|
||||
data, sock = self.request
|
||||
r = nx(data)
|
||||
if r: sock.sendto(r, self.client_address)
|
||||
|
||||
class TCP(socketserver.BaseRequestHandler):
|
||||
def handle(self):
|
||||
hdr = self.request.recv(2)
|
||||
if len(hdr) < 2: return
|
||||
ln = struct.unpack(">H", hdr)[0]
|
||||
data = b""
|
||||
while len(data) < ln:
|
||||
c = self.request.recv(ln - len(data))
|
||||
if not c: return
|
||||
data += c
|
||||
r = nx(data)
|
||||
if r: self.request.sendall(struct.pack(">H", len(r)) + r)
|
||||
|
||||
socketserver.ThreadingUDPServer.allow_reuse_address = True
|
||||
socketserver.ThreadingTCPServer.allow_reuse_address = True
|
||||
u = socketserver.ThreadingUDPServer(("127.0.0.1", 53), UDP)
|
||||
t = socketserver.ThreadingTCPServer(("127.0.0.1", 53), TCP)
|
||||
threading.Thread(target=t.serve_forever, daemon=True).start()
|
||||
u.serve_forever()
|
||||
@@ -0,0 +1,538 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
SMTP server conformance + robustness suite for BARANTMail.
|
||||
|
||||
Usage: smtp_conformance.py <plain_port> <implicit_tls_port> <mail_dir> [--json out.json]
|
||||
|
||||
Run it against a test instance, never production: it delivers real
|
||||
messages to the test mailboxes. Setup used for the results in the commit
|
||||
message: a throwaway barantmail.cfg with smtp/smtpTLS/imap/pop3 ports, a
|
||||
self-signed "tls" cert, and tests/nxdns.py serving NXDOMAIN on
|
||||
127.0.0.1:53 (resolv.conf pointed at it) so SPF/DMARC lookups answer
|
||||
immediately and deterministically.
|
||||
|
||||
Every check names the spec section it enforces. "MUST"-level requirements
|
||||
and security properties are tagged; a few SHOULD-level behaviours are
|
||||
tagged SHOULD. Test mailboxes expected under <mail_dir>:
|
||||
mail.test/brad (.password = "secret")
|
||||
mail.test/postmaster
|
||||
"""
|
||||
import base64, glob, json, os, re, socket, ssl, sys, threading, time
|
||||
|
||||
PLAIN, IMPLICIT, MAILDIR = int(sys.argv[1]), int(sys.argv[2]), sys.argv[3]
|
||||
JSON_OUT = sys.argv[sys.argv.index("--json") + 1] if "--json" in sys.argv else None
|
||||
HOST = "127.0.0.1"
|
||||
LOCAL = "mail.test"
|
||||
USER, PASSWORD = "brad@mail.test", "secret"
|
||||
INBOX = os.path.join(MAILDIR, "mail.test", "brad", "Inbox")
|
||||
PM_INBOX = os.path.join(MAILDIR, "mail.test", "postmaster", "Inbox")
|
||||
ENH = re.compile(r"^[245]\d\d[ -][245]\.\d{1,3}\.\d{1,3}(\s|$)")
|
||||
|
||||
results = []
|
||||
|
||||
def check(group, name, ok, detail="", level="MUST"):
|
||||
results.append({"group": group, "name": name, "ok": bool(ok), "level": level,
|
||||
"detail": "" if ok else repr(detail)[:240]})
|
||||
print(("PASS " if ok else "FAIL ") + "[%s] %s" % (group, name) + ("" if ok else "\n -> " + repr(detail)[:240]))
|
||||
|
||||
def tls_ctx():
|
||||
c = ssl.create_default_context(); c.check_hostname = False; c.verify_mode = ssl.CERT_NONE
|
||||
return c
|
||||
|
||||
class S:
|
||||
"""One SMTP client connection."""
|
||||
def __init__(self, port=PLAIN, implicit=False, timeout=5):
|
||||
self.sock = socket.create_connection((HOST, port), timeout=timeout)
|
||||
if implicit:
|
||||
self.sock = tls_ctx().wrap_socket(self.sock, server_hostname=LOCAL)
|
||||
self.buf = b""
|
||||
self.greeting = self.reply()
|
||||
def send(self, data):
|
||||
if isinstance(data, str): data = data.encode()
|
||||
self.sock.sendall(data)
|
||||
def reply(self, timeout=None):
|
||||
if timeout is not None: self.sock.settimeout(timeout)
|
||||
lines = []
|
||||
try:
|
||||
while True:
|
||||
while b"\r\n" not in self.buf:
|
||||
chunk = self.sock.recv(65536)
|
||||
if not chunk:
|
||||
raise EOFError("connection closed after %r" % lines)
|
||||
self.buf += chunk
|
||||
line, self.buf = self.buf.split(b"\r\n", 1)
|
||||
lines.append(line.decode("utf-8", "replace"))
|
||||
if len(line) < 4 or line[3:4] != b"-":
|
||||
return lines
|
||||
finally:
|
||||
if timeout is not None: self.sock.settimeout(5)
|
||||
def cmd(self, line):
|
||||
self.send(line + "\r\n")
|
||||
return self.reply()
|
||||
def code(self, line):
|
||||
return self.cmd(line)[-1][:3]
|
||||
def ehlo(self, name="client.test"):
|
||||
return self.cmd("EHLO " + name)
|
||||
def starttls(self):
|
||||
r = self.cmd("STARTTLS")
|
||||
if not r[-1].startswith("220"): raise RuntimeError("STARTTLS refused: %r" % r)
|
||||
self.sock = tls_ctx().wrap_socket(self.sock, server_hostname=LOCAL)
|
||||
self.buf = b""
|
||||
return r
|
||||
def silent(self, wait=1.0):
|
||||
"""True if the server sends nothing more within `wait` seconds."""
|
||||
if self.buf: return False
|
||||
self.sock.settimeout(wait)
|
||||
try:
|
||||
d = self.sock.recv(4096)
|
||||
self.buf += d
|
||||
return False if d else True
|
||||
except (socket.timeout, TimeoutError, ssl.SSLError):
|
||||
return True
|
||||
finally:
|
||||
self.sock.settimeout(5)
|
||||
def close(self):
|
||||
try: self.sock.close()
|
||||
except Exception: pass
|
||||
|
||||
def code(r): return r[-1][:3]
|
||||
def has_enh(r): return bool(ENH.match(r[-1]))
|
||||
def b64(s): return base64.b64encode(s.encode() if isinstance(s, str) else s).decode()
|
||||
|
||||
def run(group, fn):
|
||||
try:
|
||||
fn()
|
||||
except Exception as e:
|
||||
check(group, "(suite step raised) " + fn.__name__, False, e)
|
||||
|
||||
def inbox_files(path=INBOX):
|
||||
return set(glob.glob(os.path.join(path, "*")))
|
||||
|
||||
def wait_new(before, path=INBOX, n=1, timeout=8):
|
||||
end = time.time() + timeout
|
||||
while time.time() < end:
|
||||
new = inbox_files(path) - before
|
||||
if len(new) >= n:
|
||||
time.sleep(0.2)
|
||||
return sorted(inbox_files(path) - before, key=os.path.getmtime)
|
||||
time.sleep(0.1)
|
||||
return sorted(inbox_files(path) - before, key=os.path.getmtime)
|
||||
|
||||
def deliver(s, body, frm="sender@client.test", rcpt=USER):
|
||||
"""Run one transaction on an EHLO'd session; return (final reply, delivered file contents)."""
|
||||
before = inbox_files()
|
||||
s.cmd("MAIL FROM:<%s>" % frm)
|
||||
s.cmd("RCPT TO:<%s>" % rcpt)
|
||||
r = s.cmd("DATA")
|
||||
if code(r) != "354": return r, None
|
||||
s.send(body)
|
||||
r = s.reply(timeout=15)
|
||||
files = wait_new(before) if code(r) == "250" else []
|
||||
return r, (open(files[-1], "rb").read() if files else None)
|
||||
|
||||
# ---------------------------------------------------------------- greeting
|
||||
def t_greeting():
|
||||
s = S()
|
||||
check("greeting", "220 greeting naming the server (RFC 5321 4.2, 4.3.1)",
|
||||
s.greeting[-1].startswith("220 " + LOCAL), s.greeting)
|
||||
s.close()
|
||||
|
||||
# ---------------------------------------------------------------- command parsing
|
||||
def t_parsing():
|
||||
s = S(); s.ehlo()
|
||||
r = s.cmd("FROB the knob")
|
||||
check("parsing", "unknown command -> 500 (RFC 5321 4.2.4)", code(r) == "500", r)
|
||||
r = s.cmd("")
|
||||
check("parsing", "empty command line -> 500 (RFC 5321 4.2.4)", code(r) == "500", r)
|
||||
r = s.cmd("noop")
|
||||
check("parsing", "verbs are case-insensitive (RFC 5321 2.4)", code(r) == "250", r)
|
||||
r = s.cmd("mail from:<a@client.test>")
|
||||
check("parsing", "'mail from:' accepted in lower case (RFC 5321 2.4)", code(r) == "250", r)
|
||||
r = s.cmd("rcpt to:<%s>" % USER)
|
||||
check("parsing", "'rcpt to:' accepted in lower case (RFC 5321 2.4)", code(r) == "250", r)
|
||||
s.cmd("RSET")
|
||||
for verb in ("MAIL", "RCPT", "HELO", "VRFY"):
|
||||
r = s.cmd(verb)
|
||||
check("parsing", "%s with no argument -> 501, no crash (RFC 5321 4.1.1)" % verb, code(r) in ("501", "503"), r)
|
||||
r = s.cmd("NOOP " + "x" * 20000)
|
||||
check("parsing", "over-long command line -> 500 'line too long' (RFC 5321 4.5.3.1.4)", code(r) == "500", r)
|
||||
r = s.cmd("NOOP")
|
||||
check("parsing", "session still usable after over-long line", code(r) == "250", r)
|
||||
s.close()
|
||||
|
||||
# ---------------------------------------------------------------- EHLO / HELO
|
||||
def t_ehlo():
|
||||
s = S()
|
||||
r = s.cmd("EHLO")
|
||||
check("ehlo", "EHLO without domain -> 501 (RFC 5321 4.1.1.1)", code(r) == "501", r)
|
||||
r = s.ehlo()
|
||||
kw = [l[4:].split(" ")[0].upper() for l in r[1:]]
|
||||
check("ehlo", "first line 250-<domain> (RFC 5321 4.1.1.1)", r[0].startswith("250-" + LOCAL) or r[0].startswith("250 " + LOCAL), r)
|
||||
check("ehlo", "advertises PIPELINING (RFC 2920)", "PIPELINING" in kw, r, "SHOULD")
|
||||
check("ehlo", "advertises 8BITMIME (RFC 6152)", "8BITMIME" in kw, r, "SHOULD")
|
||||
check("ehlo", "advertises SIZE with a limit (RFC 1870)", any(re.match(r"^250[ -]SIZE \d+$", l) for l in r), r, "SHOULD")
|
||||
check("ehlo", "advertises ENHANCEDSTATUSCODES (RFC 2034)", "ENHANCEDSTATUSCODES" in kw, r, "SHOULD")
|
||||
check("ehlo", "advertises STARTTLS on port 25 (RFC 3207)", "STARTTLS" in kw, r)
|
||||
check("ehlo", "no plain-text AUTH offered before TLS (RFC 4954 4, 14)", "AUTH" not in kw, r, "SHOULD")
|
||||
s.close()
|
||||
s = S()
|
||||
r = s.cmd("HELO")
|
||||
check("ehlo", "HELO without domain -> 501 (RFC 5321 4.1.1.1)", code(r) == "501", r)
|
||||
r = s.cmd("HELO client.test")
|
||||
check("ehlo", "HELO client.test -> 250", code(r) == "250", r)
|
||||
s.close()
|
||||
|
||||
def t_enhanced():
|
||||
s = S(); s.ehlo()
|
||||
probes = ["NOOP", "MAIL FROM:<a@client.test>", "RCPT TO:<%s>" % USER, "RSET",
|
||||
"RCPT TO:<%s>" % USER, "FROB", "VRFY brad", "HELP", "MAIL FROM:<a@client.test> FROB=1"]
|
||||
bad = []
|
||||
for p in probes:
|
||||
r = s.cmd(p)
|
||||
if not has_enh(r): bad.append((p, r[-1]))
|
||||
check("enhanced", "every 2xx/4xx/5xx reply after EHLO carries an enhanced code (RFC 2034 3, RFC 3463)", not bad, bad)
|
||||
s.close()
|
||||
|
||||
# ---------------------------------------------------------------- sequencing
|
||||
def t_sequence():
|
||||
s = S()
|
||||
r = s.cmd("MAIL FROM:<a@client.test>")
|
||||
check("sequence", "MAIL before HELO/EHLO -> 503 (RFC 5321 4.1.4)", code(r) == "503", r)
|
||||
s.ehlo()
|
||||
r = s.cmd("RCPT TO:<%s>" % USER)
|
||||
check("sequence", "RCPT before MAIL -> 503 (RFC 5321 4.1.4)", code(r) == "503", r)
|
||||
r = s.cmd("DATA")
|
||||
check("sequence", "DATA before MAIL/RCPT -> 503 (RFC 5321 4.1.4)", code(r) in ("503", "554"), r)
|
||||
s.cmd("MAIL FROM:<a@client.test>")
|
||||
r = s.cmd("MAIL FROM:<b@client.test>")
|
||||
check("sequence", "nested MAIL inside a transaction -> 503 (RFC 5321 4.1.4)", code(r) == "503", r)
|
||||
r = s.cmd("DATA")
|
||||
check("sequence", "DATA with no accepted RCPT -> 503/554 (RFC 5321 3.3)", code(r) in ("503", "554"), r)
|
||||
s.cmd("RCPT TO:<%s>" % USER)
|
||||
s.ehlo()
|
||||
r = s.cmd("RCPT TO:<%s>" % USER)
|
||||
check("sequence", "EHLO resets the transaction like RSET (RFC 5321 4.1.4)", code(r) == "503", r)
|
||||
s.cmd("MAIL FROM:<a@client.test>"); s.cmd("RCPT TO:<%s>" % USER)
|
||||
r = s.cmd("RSET")
|
||||
check("sequence", "RSET -> 250 (RFC 5321 4.1.1.5)", code(r) == "250", r)
|
||||
r = s.cmd("RCPT TO:<%s>" % USER)
|
||||
check("sequence", "RSET clears MAIL/RCPT state", code(r) == "503", r)
|
||||
r = s.cmd("MAIL FROM:<a@client.test>")
|
||||
check("sequence", "RSET keeps the EHLO (new MAIL allowed without re-EHLO)", code(r) == "250", r)
|
||||
s.cmd("RSET")
|
||||
r = s.cmd("RSET now")
|
||||
check("sequence", "RSET with an argument -> 501 (RFC 5321 4.1.1.5)", code(r) == "501", r, "SHOULD")
|
||||
s.close()
|
||||
|
||||
# ---------------------------------------------------------------- MAIL
|
||||
def t_mail():
|
||||
s = S(); s.ehlo()
|
||||
r = s.cmd("MAIL FROM:<a@client.test>")
|
||||
check("mail", "MAIL FROM:<addr> -> 250 (RFC 5321 4.1.1.2)", code(r) == "250", r); s.cmd("RSET")
|
||||
r = s.cmd("MAIL FROM:<>")
|
||||
check("mail", "null reverse-path <> accepted (RFC 5321 4.5.5)", code(r) == "250", r); s.cmd("RSET")
|
||||
r = s.cmd("MAIL FROM:<a@client.test> SIZE=99999999999")
|
||||
check("mail", "declared SIZE over the limit -> 552 (RFC 1870 6.1)", code(r) == "552", r); s.cmd("RSET")
|
||||
r = s.cmd("MAIL FROM:<a@client.test> SIZE=1000 BODY=8BITMIME")
|
||||
check("mail", "SIZE= and BODY=8BITMIME parameters accepted (RFC 1870, RFC 6152)", code(r) == "250", r); s.cmd("RSET")
|
||||
r = s.cmd("MAIL FROM:<a@client.test> BODY=7BIT")
|
||||
check("mail", "BODY=7BIT accepted (RFC 6152 3)", code(r) == "250", r); s.cmd("RSET")
|
||||
r = s.cmd("MAIL FROM:<a@client.test> FROB=1")
|
||||
check("mail", "unknown MAIL parameter -> 555 (RFC 5321 4.1.1.11)", code(r) == "555", r); s.cmd("RSET")
|
||||
r = s.cmd("MAIL FROM:a@client.test")
|
||||
check("mail", "reverse-path without <> -> 501 (RFC 5321 4.1.2)", code(r) == "501", r); s.cmd("RSET")
|
||||
r = s.cmd("MAIL TO:<a@client.test>")
|
||||
check("mail", "malformed MAIL -> 501 (RFC 5321 4.2.2)", code(r) == "501", r)
|
||||
s.close()
|
||||
|
||||
# ---------------------------------------------------------------- RCPT
|
||||
def t_rcpt():
|
||||
s = S(); s.ehlo(); s.cmd("MAIL FROM:<a@client.test>")
|
||||
r = s.cmd("RCPT TO:<%s>" % USER)
|
||||
check("rcpt", "local mailbox -> 250 (RFC 5321 4.1.1.3)", code(r) == "250", r)
|
||||
r = s.cmd("RCPT TO:<brad@MAIL.TEST>")
|
||||
check("rcpt", "domain matched case-insensitively (RFC 5321 2.4)", code(r) == "250", r)
|
||||
r = s.cmd("RCPT TO:<Brad@mail.test>")
|
||||
check("rcpt", "local-part matched case-insensitively for existing mailbox (RFC 5321 2.4)", code(r) == "250", r, "SHOULD")
|
||||
r = s.cmd("RCPT TO:<nobody@mail.test>")
|
||||
check("rcpt", "unknown local user -> 550 5.1.1 (RFC 5321 4.2.2, RFC 3463)", code(r) == "550" and " 5.1.1" in r[-1], r)
|
||||
r = s.cmd("RCPT TO:<victim@elsewhere.example>")
|
||||
check("rcpt", "unauthenticated relay refused -> 550/554 5.7.1 (RFC 5321 3.6.2, 7.1)", code(r) in ("550", "554") and " 5.7.1" in r[-1], r)
|
||||
r = s.cmd("RCPT TO:<postmaster>")
|
||||
check("rcpt", "<postmaster> with no domain accepted (RFC 5321 4.5.1 MUST)", code(r) == "250", r)
|
||||
r = s.cmd("RCPT TO:<PostMaster@mail.test>")
|
||||
check("rcpt", "postmaster local-part case-insensitive (RFC 5321 4.5.1)", code(r) == "250", r)
|
||||
r = s.cmd("RCPT TO:<%s> NOTIFY=NEVER" % USER)
|
||||
check("rcpt", "unsupported RCPT parameter -> 555 (RFC 5321 4.1.1.11)", code(r) == "555", r)
|
||||
r = s.cmd("RCPT TO:brad@mail.test")
|
||||
check("rcpt", "forward-path without <> -> 501 (RFC 5321 4.1.2)", code(r) == "501", r)
|
||||
r = s.cmd("RCPT TO:<justaname>")
|
||||
check("rcpt", "address without @ (not postmaster) rejected, no crash", code(r) in ("501", "550", "553"), r)
|
||||
r = s.cmd("NOOP")
|
||||
check("rcpt", "server still answering after malformed recipients", code(r) == "250", r)
|
||||
s.cmd("RSET"); s.cmd("MAIL FROM:<a@client.test>")
|
||||
codes = [code(s.cmd("RCPT TO:<%s>" % USER)) for _ in range(100)]
|
||||
check("rcpt", "100 recipients accepted in one transaction (RFC 5321 4.5.3.1.8)", codes.count("250") == 100, codes[-3:])
|
||||
s.close()
|
||||
|
||||
# ---------------------------------------------------------------- DATA
|
||||
def t_data():
|
||||
s = S(); s.ehlo()
|
||||
s.cmd("MAIL FROM:<a@client.test>"); s.cmd("RCPT TO:<%s>" % USER)
|
||||
r = s.cmd("DATA now")
|
||||
check("data", "DATA with an argument -> 501 (RFC 5321 4.1.1.4)", code(r) == "501", r, "SHOULD")
|
||||
s.cmd("RSET")
|
||||
body = ("Subject: conformance dot test\r\nFrom: <sender@client.test>\r\n\r\n"
|
||||
"first line\r\n..leading dot stuffed\r\n.\r\n").encode()
|
||||
# the data above is already terminated; dot line ".." must come back as "."
|
||||
r, msg = deliver(s, body)
|
||||
check("data", "message accepted -> 250 (RFC 5321 4.1.1.4)", code(r) == "250", r)
|
||||
check("data", "queue ID in reply does not leak server filesystem paths", "/" not in r[-1], r, "SHOULD")
|
||||
if msg is None:
|
||||
check("data", "message delivered to local mailbox", False, "nothing appeared in Inbox")
|
||||
else:
|
||||
text = msg.decode("utf-8", "replace")
|
||||
check("data", "dot-unstuffing: '..x' stored as '.x' (RFC 5321 4.5.2)", "\r\n.leading dot stuffed\r\n" in text and "..leading" not in text, text[-200:])
|
||||
check("data", "Received: trace header prepended (RFC 5321 4.4 MUST)", re.search(r"(?m)^Received: from ", text) is not None, text[:300])
|
||||
check("data", "Received header has 'by <host>' and 'with ESMTP' (RFC 5321 4.4, RFC 3848)",
|
||||
re.search(r"by %s.*with ESMTP" % re.escape(LOCAL), text, re.S) is not None, text[:300])
|
||||
check("data", "Received header records client IP (RFC 5321 4.4 SHOULD)", "[127.0.0.1]" in text[:600], text[:300], "SHOULD")
|
||||
check("data", "Return-Path added at final delivery (RFC 5321 4.4 MUST)",
|
||||
text.startswith("Return-Path: <sender@client.test>"), text[:120])
|
||||
check("data", "stored lines are CRLF-terminated", "\n" not in text.replace("\r\n", ""), "bare LF in stored message", "SHOULD")
|
||||
r = s.cmd("RCPT TO:<%s>" % USER)
|
||||
check("data", "transaction cleared after DATA completes (RFC 5321 4.1.1.4)", code(r) == "503", r)
|
||||
s.close()
|
||||
|
||||
def t_split_crlf():
|
||||
# A CR LF split across two TCP segments must still be one line ending.
|
||||
s = S(); s.ehlo()
|
||||
before = inbox_files()
|
||||
s.cmd("MAIL FROM:<sender@client.test>"); s.cmd("RCPT TO:<%s>" % USER); s.cmd("DATA")
|
||||
s.send(b"Subject: split crlf\r\n\r\nalpha\r"); time.sleep(0.4)
|
||||
s.send(b"\nbeta\r\n.\r\n")
|
||||
r = s.reply(timeout=10)
|
||||
new = wait_new(before)
|
||||
msg = open(new[-1], "rb").read() if new else b""
|
||||
check("data", "CR LF split across reads is one line ending - no blank line inserted (RFC 5321 2.3.8)",
|
||||
code(r) == "250" and b"alpha\r\nbeta\r\n" in msg, (r, msg[-60:]))
|
||||
s.close()
|
||||
|
||||
def t_size():
|
||||
s = S(); s.ehlo()
|
||||
s.cmd("MAIL FROM:<a@client.test>"); s.cmd("RCPT TO:<%s>" % USER); s.cmd("DATA")
|
||||
chunk = ("X" * 998 + "\r\n").encode()
|
||||
blob = chunk * (27 * 1024) # ~27 MB > 25 MB limit
|
||||
s.send(blob + b".\r\n")
|
||||
r = s.reply(timeout=60)
|
||||
check("size", "message over SIZE limit -> 552 after DATA (RFC 1870 6.3, RFC 5321 4.5.3.1.10)", code(r) == "552", r)
|
||||
r = s.cmd("NOOP")
|
||||
check("size", "session still usable after oversized message", code(r) == "250", r)
|
||||
s.close()
|
||||
|
||||
def t_smuggling():
|
||||
# SMTP smuggling (CVE-2023-51764/51765/51766 family): only <CRLF>.<CRLF>
|
||||
# ends DATA (RFC 5321 4.1.1.4 / 2.3.8); bare-LF / bare-CR variants must not.
|
||||
variants = {"<LF>.<LF>": b"\n.\n", "<LF>.<CR><LF>": b"\n.\r\n", "<CR><LF>.<LF>": b"\r\n.\n",
|
||||
"<CR>.<CR>": b"\r.\r", "<CR><LF>.<CR>": b"\r\n.\r"}
|
||||
for name, term in variants.items():
|
||||
s = S(); s.ehlo()
|
||||
before = inbox_files()
|
||||
s.cmd("MAIL FROM:<sender@client.test>"); s.cmd("RCPT TO:<%s>" % USER); s.cmd("DATA")
|
||||
body = (b"Subject: carrier\r\n\r\nhello" + term +
|
||||
b"MAIL FROM:<forged@paypal.example>\r\nRCPT TO:<" + USER.encode() + b">\r\nDATA\r\n"
|
||||
b"Subject: SMUGGLED\r\n\r\nsmuggled\r\n.\r\n")
|
||||
s.send(body)
|
||||
try:
|
||||
r = s.reply(timeout=10)
|
||||
except Exception as e:
|
||||
r = [repr(e)]
|
||||
extra = not s.silent(1.5)
|
||||
new = wait_new(before, timeout=4)
|
||||
contents = b"".join(open(f, "rb").read() for f in new)
|
||||
# Smuggled = a SEPARATE message (its own header section) with the
|
||||
# forged subject. The text staying inside the one carrier message's
|
||||
# body is the correct outcome.
|
||||
def header_subject(data):
|
||||
head = data.split(b"\r\n\r\n", 1)[0]
|
||||
m = re.search(rb"(?mi)^Subject: (.*)$", head)
|
||||
return m.group(1).strip() if m else b""
|
||||
smuggled = any(header_subject(open(f, "rb").read()) == b"SMUGGLED" for f in new)
|
||||
ok = code(r) == "250" and not extra and len(new) == 1 and not smuggled
|
||||
# it's also acceptable to reject the whole message outright for bare line endings
|
||||
ok = ok or (code(r) in ("550", "554", "500") and not extra and not smuggled)
|
||||
check("smuggling", "%s inside DATA does not end the message (RFC 5321 2.3.8, 4.1.1.4)" % name, ok,
|
||||
(r, "extra replies" if extra else "", len(new), "smuggled" if smuggled else ""))
|
||||
s.close()
|
||||
|
||||
def t_pipelining():
|
||||
s = S(); s.ehlo()
|
||||
s.send("MAIL FROM:<a@client.test>\r\nRCPT TO:<%s>\r\nRCPT TO:<nobody@mail.test>\r\nDATA\r\n" % USER)
|
||||
codes = [code(s.reply()) for _ in range(4)]
|
||||
check("pipelining", "pipelined MAIL/RCPT/RCPT/DATA answered in order (RFC 2920 3.1)", codes == ["250", "250", "550", "354"], codes)
|
||||
s.send(".\r\nQUIT\r\n")
|
||||
codes = [code(s.reply()) for _ in range(2)]
|
||||
check("pipelining", "end-of-data then QUIT answered in order", codes == ["250", "221"], codes)
|
||||
s.close()
|
||||
|
||||
# ---------------------------------------------------------------- misc commands
|
||||
def t_misc():
|
||||
s = S(); s.ehlo()
|
||||
r = s.cmd("NOOP"); check("misc", "NOOP -> 250 (RFC 5321 4.1.1.9)", code(r) == "250", r)
|
||||
r = s.cmd("NOOP hello"); check("misc", "NOOP with a string -> 250 (RFC 5321 4.1.1.9)", code(r) == "250", r)
|
||||
r = s.cmd("VRFY brad"); check("misc", "VRFY -> 250/251/252/550/553/502 (RFC 5321 3.5.3)", code(r) in ("250", "251", "252", "502", "550", "553"), r)
|
||||
r = s.cmd("EXPN staff"); check("misc", "EXPN -> 250/252/502/550 (RFC 5321 3.5.3)", code(r) in ("250", "252", "502", "550"), r)
|
||||
r = s.cmd("HELP"); check("misc", "HELP -> 211/214 (RFC 5321 4.1.1.8)", code(r) in ("211", "214"), r)
|
||||
s.cmd("MAIL FROM:<a@client.test>"); s.cmd("RCPT TO:<%s>" % USER)
|
||||
s.cmd("VRFY x"); s.cmd("HELP"); s.cmd("NOOP")
|
||||
r = s.cmd("DATA"); check("misc", "VRFY/HELP/NOOP don't disturb the transaction (RFC 5321 4.1.4)", code(r) == "354", r)
|
||||
s.send(".\r\n"); s.reply()
|
||||
r = s.cmd("QUIT"); check("misc", "QUIT -> 221 (RFC 5321 4.1.1.10)", code(r) == "221", r)
|
||||
check("misc", "connection closed after QUIT (RFC 5321 4.1.1.10)", s.silent(2) is True and _closed(s), "")
|
||||
s.close()
|
||||
|
||||
def _closed(s):
|
||||
s.sock.settimeout(2)
|
||||
try: return s.sock.recv(1) == b""
|
||||
except Exception: return True
|
||||
|
||||
# ---------------------------------------------------------------- STARTTLS
|
||||
def t_starttls():
|
||||
s = S(); s.ehlo()
|
||||
r = s.cmd("STARTTLS now")
|
||||
check("starttls", "STARTTLS with a parameter -> 501 (RFC 3207 4)", code(r) == "501", r)
|
||||
s.cmd("MAIL FROM:<before@client.test>")
|
||||
s.starttls()
|
||||
check("starttls", "TLS negotiated after 220 (RFC 3207 4)", s.sock.version() in ("TLSv1.2", "TLSv1.3"), s.sock.version())
|
||||
r = s.cmd("RCPT TO:<%s>" % USER)
|
||||
check("starttls", "pre-TLS state discarded after handshake (RFC 3207 4.2)", code(r) == "503", r)
|
||||
r = s.ehlo(); kw = [l[4:].split(" ")[0].upper() for l in r[1:]]
|
||||
check("starttls", "STARTTLS not offered again once encrypted (RFC 3207 4.2)", "STARTTLS" not in kw, r)
|
||||
check("starttls", "AUTH offered once encrypted (RFC 4954)", "AUTH" in kw, r)
|
||||
r = s.cmd("STARTTLS")
|
||||
check("starttls", "second STARTTLS -> 503 (RFC 3207 4)", code(r) == "503", r, "SHOULD")
|
||||
before = inbox_files()
|
||||
r, msg = deliver(s, b"Subject: over tls\r\n\r\nbody\r\n.\r\n")
|
||||
check("starttls", "message accepted over STARTTLS", code(r) == "250", r)
|
||||
check("starttls", "Received header says 'with ESMTPS' (RFC 3848)", msg is not None and b"with ESMTPS" in msg[:800], (msg or b"")[:300])
|
||||
s.close()
|
||||
# command injection (CVE-2011-0411 class)
|
||||
s = S(); s.ehlo()
|
||||
s.send("STARTTLS\r\nMAIL FROM:<injected@evil.example>\r\nRCPT TO:<%s>\r\n" % USER)
|
||||
r = s.reply()
|
||||
left = s.buf
|
||||
s.sock = tls_ctx().wrap_socket(s.sock, server_hostname=LOCAL); s.buf = b""
|
||||
quiet = s.silent(1.0)
|
||||
s.ehlo()
|
||||
r2 = s.cmd("RCPT TO:<%s>" % USER)
|
||||
check("starttls", "plain-text commands pipelined after STARTTLS are discarded (RFC 3207 4, CVE-2011-0411)",
|
||||
code(r) == "220" and not left and quiet and code(r2) == "503", (r, left, quiet, r2))
|
||||
s.close()
|
||||
# large body over TLS (many full 16KB TLS records)
|
||||
s = S(); s.ehlo(); s.starttls(); s.ehlo()
|
||||
lines = ["Line %06d %s" % (i, "y" * 70) for i in range(6000)]
|
||||
body = ("Subject: big tls\r\n\r\n" + "\r\n".join(lines) + "\r\n.\r\n").encode()
|
||||
t0 = time.time(); r, msg = deliver(s, body)
|
||||
ok = code(r) == "250" and msg is not None and all(l.encode() in msg for l in (lines[0], lines[2999], lines[-1]))
|
||||
check("starttls", "~500KB message over TLS arrives complete (%.1fs)" % (time.time() - t0), ok, r)
|
||||
s.close()
|
||||
|
||||
def t_implicit():
|
||||
s = S(IMPLICIT, implicit=True)
|
||||
check("implicit-tls", "greeting over implicit TLS (RFC 8314 3.3)", s.greeting[-1].startswith("220"), s.greeting)
|
||||
r = s.ehlo(); kw = [l[4:].split(" ")[0].upper() for l in r[1:]]
|
||||
check("implicit-tls", "no STARTTLS offered on an already-TLS port (RFC 3207 4.2)", "STARTTLS" not in kw, r)
|
||||
check("implicit-tls", "AUTH offered on the TLS port", "AUTH" in kw, r)
|
||||
r = s.cmd("AUTH PLAIN " + b64("\0%s\0%s" % (USER, PASSWORD)))
|
||||
check("implicit-tls", "AUTH PLAIN succeeds on the TLS port", code(r) == "235", r)
|
||||
s.close()
|
||||
|
||||
# ---------------------------------------------------------------- AUTH
|
||||
def t_auth():
|
||||
s = S()
|
||||
r = s.cmd("AUTH PLAIN " + b64("\0%s\0%s" % (USER, PASSWORD)))
|
||||
check("auth", "AUTH before EHLO -> 503 (RFC 4954 4)", code(r) == "503", r)
|
||||
s.ehlo()
|
||||
r = s.cmd("AUTH LOGIN")
|
||||
check("auth", "AUTH before TLS refused -> 538 5.7.11 (RFC 4954 6)", code(r) == "538", r, "SHOULD")
|
||||
s.close()
|
||||
s = S(); s.ehlo(); s.starttls(); s.ehlo()
|
||||
r = s.cmd("AUTH CRAM-MD5")
|
||||
check("auth", "unsupported mechanism -> 504 (RFC 4954 6)", code(r) == "504", r)
|
||||
r = s.cmd("AUTH")
|
||||
check("auth", "AUTH without mechanism -> 501", code(r) == "501", r)
|
||||
r = s.cmd("AUTH PLAIN " + b64("\0%s\0wrong" % USER))
|
||||
check("auth", "bad password -> 535 5.7.8 (RFC 4954 6)", code(r) == "535" and " 5.7.8" in r[-1], r)
|
||||
r = s.cmd("NOOP")
|
||||
check("auth", "session usable after failed AUTH", code(r) == "250", r)
|
||||
r = s.cmd("AUTH LOGIN"); r2 = s.cmd("*")
|
||||
check("auth", "'*' cancels the exchange -> 501 (RFC 4954 4)", code(r) == "334" and code(r2) == "501", (r, r2))
|
||||
r = s.cmd("AUTH LOGIN"); r2 = s.cmd("!!!not base64!!!")
|
||||
check("auth", "invalid base64 -> 501 5.5.2 (RFC 4954 4)", code(r2) == "501", (r, r2))
|
||||
r = s.cmd("AUTH PLAIN"); r2 = s.cmd(b64("\0%s\0%s" % (USER, PASSWORD)))
|
||||
check("auth", "AUTH PLAIN without initial response: 334 then credentials -> 235 (RFC 4616, RFC 4954 4)",
|
||||
r[-1].startswith("334") and code(r2) == "235", (r, r2))
|
||||
r = s.cmd("AUTH PLAIN " + b64("\0%s\0%s" % (USER, PASSWORD)))
|
||||
check("auth", "AUTH after successful AUTH -> 503 (RFC 4954 4)", code(r) == "503", r)
|
||||
s.cmd("MAIL FROM:<%s>" % USER)
|
||||
r = s.cmd("RCPT TO:<someone@elsewhere.example>")
|
||||
check("auth", "authenticated client may relay (RFC 4954, RFC 6409)", code(r) == "250", r)
|
||||
s.cmd("RSET")
|
||||
s.close()
|
||||
s = S(); s.ehlo(); s.starttls(); s.ehlo()
|
||||
r = s.cmd("AUTH LOGIN"); r2 = s.cmd(b64(USER)); r3 = s.cmd(b64(PASSWORD))
|
||||
check("auth", "AUTH LOGIN two-step -> 334, 334, 235", r[-1].startswith("334") and r2[-1].startswith("334") and code(r3) == "235", (r, r2, r3))
|
||||
s.close()
|
||||
s = S(); s.ehlo(); s.starttls(); s.ehlo()
|
||||
r = s.cmd("AUTH PLAIN " + b64("someoneelse@mail.test\0%s\0%s" % (USER, PASSWORD)))
|
||||
check("auth", "PLAIN authzid differing from authcid refused -> 535 (RFC 4616 2)", code(r) in ("535", "501"), r)
|
||||
s.cmd("MAIL FROM:<a@client.test>")
|
||||
r = s.cmd("AUTH PLAIN " + b64("\0%s\0%s" % (USER, PASSWORD)))
|
||||
check("auth", "AUTH during a mail transaction -> 503 (RFC 4954 4)", code(r) == "503", r)
|
||||
s.close()
|
||||
s = S(); s.ehlo(); s.starttls(); s.ehlo()
|
||||
s.cmd("AUTH PLAIN " + b64("\0%s\0%s" % (USER, PASSWORD)))
|
||||
r, msg = deliver(s, b"Subject: authenticated\r\n\r\nbody\r\n.\r\n", frm=USER)
|
||||
check("auth", "Received header says 'with ESMTPSA' when authenticated over TLS (RFC 3848)", msg is not None and b"with ESMTPSA" in msg[:800], (msg or b"")[:300])
|
||||
s.close()
|
||||
|
||||
# ---------------------------------------------------------------- robustness
|
||||
def t_robust():
|
||||
payloads = ["MAIL FROM:<", "RCPT TO:<", "MAIL FROM:<@>", "RCPT TO:<@@@>", "HELO " + "a" * 3000,
|
||||
"AUTH LOGIN " + "=" * 40, "MAIL FROM:<" + "a" * 5000 + "@x>", "\x00\x01\x02", "VRFY"]
|
||||
for p in payloads:
|
||||
try:
|
||||
s = S(); s.ehlo()
|
||||
if p.startswith("RCPT"): s.cmd("MAIL FROM:<a@client.test>")
|
||||
s.cmd(p); s.close()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
s = S(); r = s.cmd("NOOP"); alive = code(r) == "250"; s.close()
|
||||
except Exception as e:
|
||||
alive = False
|
||||
check("robustness", "server survives malformed input (%d payloads)" % len(payloads), alive, "server stopped answering")
|
||||
errors = []
|
||||
def worker(n):
|
||||
try:
|
||||
k = S(); k.ehlo("c%d.test" % n)
|
||||
k.cmd("MAIL FROM:<c%d@client.test>" % n); k.cmd("RCPT TO:<%s>" % USER); k.cmd("DATA")
|
||||
k.send(("Subject: burst %d\r\n\r\n" % n + "line\r\n" * 500 + ".\r\n").encode())
|
||||
r = k.reply(timeout=20)
|
||||
if code(r) != "250": errors.append((n, r))
|
||||
k.cmd("QUIT"); k.close()
|
||||
except Exception as e:
|
||||
errors.append((n, repr(e)))
|
||||
ts = [threading.Thread(target=worker, args=(n,)) for n in range(40)]
|
||||
[t.start() for t in ts]; [t.join(40) for t in ts]
|
||||
check("robustness", "40 concurrent deliveries all succeed", not errors, errors[:3])
|
||||
|
||||
for fn in (t_greeting, t_parsing, t_ehlo, t_enhanced, t_sequence, t_mail, t_rcpt, t_data, t_split_crlf, t_size,
|
||||
t_smuggling, t_pipelining, t_misc, t_starttls, t_implicit, t_auth, t_robust):
|
||||
run(fn.__name__[2:], fn)
|
||||
|
||||
total = len(results); passed = sum(r["ok"] for r in results)
|
||||
print("\n%d/%d checks passed" % (passed, total))
|
||||
if JSON_OUT:
|
||||
json.dump(results, open(JSON_OUT, "w"), indent=1)
|
||||
sys.exit(0 if passed == total else 1)
|
||||
Reference in new issue
Block a user