Compare commits

...
3 Commits
Author SHA1 Message Date
barantandClaude Opus 5.5 d3d7e5f3e5 SMTP: STARTTLS, smuggling fix, and RFC 5321 conformance
Security:
- SMTP smuggling (CVE-2023-51764 family): only <CRLF>.<CRLF> ends DATA
  (strict CRLF framing); bare CR/LF in the text is stored as CR LF.
  Before, '<LF>.<LF>' and four other variants ended the message and the
  rest ran as fresh commands - a second, forged message was delivered.
- STARTTLS on port 25 (RFC 3207), using the configured tls cert; AUTH
  is only offered/accepted once encrypted (538 5.7.11 otherwise).
- The 250 reply no longer discloses the queue file's full path.
- Queue file names %-encode the addresses ('/' or '|' in an address
  broke the name or pointed it into another directory).

Conformance (RFC 5321, 1870, 2034, 2920, 3207, 3463, 3848, 4616, 4954,
6152):
- 500 for unknown/empty/over-long commands (no reply at all before -
  clients hung); 501 for missing/invalid arguments (bare MAIL/RCPT/HELO
  were out-of-bounds reads).
- HELO/EHLO required before MAIL; nested MAIL refused; EHLO/HELO/RSET/
  end-of-DATA reset the transaction.
- MAIL: SIZE= (552 when over the limit), BODY=7BIT|8BITMIME, AUTH=;
  555 for anything else; case-insensitive keywords; address syntax
  checked.
- RCPT: postmaster (bare and any case) accepted; case-insensitive
  domain and local-part; 550 5.1.1 unknown user vs 550 5.7.1 relay
  denied; aliases accepted (they were rejected at RCPT before); 1000
  recipient cap (452 4.5.3).
- ENHANCEDSTATUSCODES advertised and used on every reply.
- AUTH PLAIN added alongside LOGIN; strict base64; 535 5.7.8 on
  failure; 503 before EHLO, during a transaction, or when already
  authenticated (the last one sent no reply before).
- Received: per RFC 5321 4.4 with client IP, protocol per RFC 3848
  (ESMTP/ESMTPS/ESMTPSA) and queue id; Return-Path: added at local
  delivery.

tests/smtp_conformance.py: 112 checks. origin/master passes 37 of the
72 it can reach before the server stops answering; this passes 112/112.
tests/imap_pop3_regression.py: IMAPS/POP3S unaffected (3/3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0121tAhQVe4qbCwYgVjKQS4B
2026-11-02 11:56:41 -08:00
Brad ArantandClaude Sonnet 5 407cc2e915 Add per-domain DKIM signing, admin console TLS, log rotation, and resolver timeout scoping
- DKIM signing was hardcoded to one domain; now keyed per-domain off
  the sending address, fixing mail for every domain but the original.
- Admin console (port 1027) is now TLS-wrapped instead of plaintext.
- barantmail.log now rotates by size (config-driven) instead of
  growing unbounded (previously hit 23GB).
- ResolverThreadScope.h: a <setenv> scoping fix for name resolution
  inside a <try> block that previously did not survive its own
  container.
- IMAPArgs.cpp: fixed "UID STORE 1:*" wrongly requiring UID 1 to
  exist, which silently made empty-folder operations delete nothing.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-06 17:39:06 +00:00
Brad Arant 5f3e6ed2e6 Log MailFolder save failures instead of failing silently
std::ofstream does not throw on a failed open, so a permissions or
disk-full problem writing .imapindex/.uidnext/.uidvalidity previously
just... did not happen, with no trace anywhere. Confirmed live: the
barant accounts inbox and Trash index files were root-owned mode 644
while the mail service runs as a non-root user, so every flag/counter
persist attempt (marking a message \Seen, for instance) silently
failed for weeks - the in-memory session reflected the change for that
one connection, but nothing ever reached disk, so any fresh IMAP
session saw the old state again. Ownership fixed directly on the
affected files; this logs the failure mode so a recurrence is
visible.
2026-09-07 02:29:16 +00:00
35 changed files with 2106 additions and 424 deletions

No files matched your search

+27 -8
View File
@@ -82,20 +82,39 @@ namespace mail {
return hasTls ? &tlsInfo : NULL;
}
bool BARANTMailConfig::hasDkim() {
return file->exists("dkim.domain") && file->exists("dkim.selector") && file->exists("dkim.privateKey");
std::vector<BARANTMailConfig::DkimEntry> BARANTMailConfig::getDkimEntries() {
std::vector<DkimEntry> entries;
// No length-prefixed array accessor on JFile/JString - probe
// ascending indices via exists() (the same technique the JSON
// path syntax's own "a[2]" bracket form is meant for) until one
// is missing, matching a plain JSON array's natural "no trailing
// comma/count field" shape.
for (int i = 0; ; i++) {
std::string indexPrefix = "dkim[" + std::to_string(i) + "]";
std::string domainPath = indexPrefix + ".domain";
if (!file->exists(domainPath))
break;
std::string selectorPath = indexPrefix + ".selector";
std::string keyPath = indexPrefix + ".privateKey";
DkimEntry entry;
entry.domain = file->getString(domainPath);
entry.selector = file->getString(selectorPath);
entry.privateKeyPath = file->getString(keyPath);
entries.push_back(entry);
}
return entries;
}
std::string BARANTMailConfig::getDkimDomain() {
return file->getString("dkim.domain");
bool BARANTMailConfig::hasLog() {
return file->exists("log");
}
std::string BARANTMailConfig::getDkimSelector() {
return file->getString("dkim.selector");
size_t BARANTMailConfig::getLogMaxSizeBytes() {
return file->exists("log.maxSizeBytes") ? (size_t)file->getInt("log.maxSizeBytes") : 104857600;
}
std::string BARANTMailConfig::getDkimPrivateKeyPath() {
return file->getString("dkim.privateKey");
int BARANTMailConfig::getLogMaxBackups() {
return file->exists("log.maxBackups") ? (int)file->getInt("log.maxBackups") : 5;
}
bool BARANTMailConfig::hasAdmin() {
+32 -11
View File
@@ -3,6 +3,7 @@
#include "TLSInfo.h"
#include <string>
#include <vector>
namespace coreutils { class JFile; }
@@ -26,11 +27,13 @@ namespace mail {
/// "key": "/path/to/privkey.pem",
/// "caCertificate": "/path/to/ca.pem"
/// },
/// "dkim": {
/// "domain": "mail.example.com",
/// "selector": "default",
/// "privateKey": "/path/to/dkim_private.pem"
/// }
/// "dkim": [
/// { "domain": "example.com", "selector": "default",
/// "privateKey": "/path/to/example.com.private.pem" },
/// { "domain": "second-domain.com", "selector": "default",
/// "privateKey": "/path/to/second-domain.com.private.pem" }
/// ],
/// "log": { "maxSizeBytes": 104857600, "maxBackups": 5 }
/// }
///
/// "hostName", "mailPath", and "smtp.port" are required - plain port
@@ -43,8 +46,18 @@ namespace mail {
/// "tls.certificate" and "tls.key" become required - there is no
/// path through this class that hands back a submission/IMAPS/POP3S
/// listener request without a real certificate configured for it.
/// "dkim" is entirely optional - outbound mail goes out unsigned
/// unless all three of its fields are present.
/// "dkim" is entirely optional (an empty/missing array - outbound
/// mail goes out unsigned for any domain with no matching entry).
/// Each entry's three fields are all required. A message is signed
/// using whichever entry's "domain" matches the sender address's
/// own domain (case-sensitive exact match) - a domain with no entry
/// here just goes out unsigned, same as if "dkim" were absent
/// entirely.
/// "log" is entirely optional - "maxSizeBytes"/"maxBackups" each
/// independently default (100MB / 5 backups) if omitted, and the
/// whole section can be left out for the same defaults. Applied to
/// coreutils::Log's static rotation fields once this config loads -
/// see Log.h's own doc comment for what rotation actually does.
///
class BARANTMailConfig {
@@ -78,10 +91,18 @@ namespace mail {
core::TLSInfo *getTlsInfo();
bool hasDkim();
std::string getDkimDomain();
std::string getDkimSelector();
std::string getDkimPrivateKeyPath();
struct DkimEntry {
std::string domain;
std::string selector;
std::string privateKeyPath;
};
/// Empty if "dkim" is absent from the config file.
std::vector<DkimEntry> getDkimEntries();
bool hasLog();
size_t getLogMaxSizeBytes();
int getLogMaxBackups();
///
/// The admin console (see ConsoleServer/ConsoleSession) - "port"
BIN
View File
Binary file not shown.
+40 -7
View File
@@ -73,6 +73,35 @@ namespace mail {
uint32_t uid = resolveNumber(part);
if (uid != 0)
result.push_back(uid);
} else if (uidMode) {
// A UID range's boundaries are just numbers, not required
// to name an existing message's UID - RFC 3501: "A:B"
// means every message whose UID falls in [A,B], not "the
// two messages literally named A and B". resolveNumber()
// above requires an exact existing-UID match via
// folder.find(), which is correct for a bare single-UID
// reference but wrong here: a real, long-lived mailbox
// almost never has a message with UID exactly 1 once
// anything has ever been expunged, so the standard
// "select everything" idiom "UID STORE 1:* +FLAGS
// (\\Deleted)" (used by the admin empty-folder feature)
// resolved fromUid to 0 and silently skipped the whole
// range every time - confirmed live: it reported
// {"ok":true} while deleting nothing from a real
// 538-message Trash folder. Scanning directly by UID
// instead of resolving each boundary to a sequence
// position first sidesteps that entirely.
std::string fromStr = part.substr(0, colon);
std::string toStr = part.substr(colon + 1);
uint32_t fromUid = (fromStr == "*") ? (folder.size() == 0 ? 0 : folder.messages().back().uid)
: (uint32_t)std::strtoul(fromStr.c_str(), nullptr, 10);
uint32_t toUid = (toStr == "*") ? (folder.size() == 0 ? 0 : folder.messages().back().uid)
: (uint32_t)std::strtoul(toStr.c_str(), nullptr, 10);
if (fromUid > toUid)
std::swap(fromUid, toUid);
for (const auto &message : folder.messages())
if (message.uid >= fromUid && message.uid <= toUid)
result.push_back(message.uid);
} else {
std::string fromStr = part.substr(0, colon);
std::string toStr = part.substr(colon + 1);
@@ -82,14 +111,18 @@ namespace mail {
continue;
if (fromUid > toUid)
std::swap(fromUid, toUid);
// Sequence-number range (plain, non-UID STORE/FETCH/etc.) -
// resolveNumber() already turned both ends into concrete
// UIDs regardless of uidMode - walking sequence-number-
// by-sequence-number (not uid-by-uid) from here is
// deliberate: a UID range like "5:9" must include every
// message whose UID falls in that range, not just
// messages whose UID happens to equal one of the
// integers 5-9 (UIDs aren't dense/contiguous once
// anything's been expunged).
// UIDs, and unlike the uidMode branch above, a plain
// sequence number always names a real position
// (1..folder.size()), so this round-trip through
// folder.seq() doesn't have that branch's problem. Walking
// sequence-number-by-sequence-number (not uid-by-uid) is
// still deliberate here: a range like "5:9" must include
// every message whose UID falls in that range, not just
// messages whose UID happens to equal one of the integers
// 5-9 (UIDs aren't dense/contiguous once anything's been
// expunged).
uint32_t fromSeq = folder.seq(fromUid);
uint32_t toSeq = folder.seq(toUid);
if (fromSeq == 0 || toSeq == 0)
+58 -14
View File
@@ -1,4 +1,5 @@
#include "MailFolder.h"
#include "Log.h"
#include <algorithm>
#include <ctime>
#include <fstream>
@@ -47,6 +48,15 @@ namespace mail {
void writeCounterFile(const std::string &path, uint32_t value) {
std::ofstream out(path, std::ios::trunc);
out << value;
// ofstream doesn't throw on a failed open (e.g. a permissions
// problem, a full/read-only filesystem) - confirmed live: two
// pre-existing mailbox index files were root-owned mode 644
// while the mail service runs as a non-root user, so every
// counter/flag persist attempt failed here completely silently
// for weeks. Logging it is the only way this becomes visible
// instead of just quietly not happening.
if (!out)
coreutils::Log(coreutils::LOG_EXCEPT) << "MailFolder: failed to write " << path << " - check ownership/permissions.";
}
}
@@ -85,20 +95,48 @@ namespace mail {
while (std::getline(in, line)) {
if (line.empty())
continue;
std::istringstream stream(line);
Message message;
std::string uidStr, dateStr, flagsBlob;
stream >> message.fileName >> uidStr >> dateStr;
std::getline(stream, flagsBlob);
// The flags blob (if any) is whatever's left after the third
// field, minus the single separating space istream>> already
// consumed up to.
if (!flagsBlob.empty() && flagsBlob[0] == ' ')
flagsBlob.erase(0, 1);
message.uid = (uint32_t)std::stoul(uidStr);
message.internalDate = (time_t)std::stoll(dateStr);
message.flags = splitFlags(flagsBlob);
mMessages.push_back(message);
try {
std::istringstream stream(line);
Message message;
std::string uidStr, dateStr, flagsBlob;
stream >> message.fileName >> uidStr >> dateStr;
std::getline(stream, flagsBlob);
// The flags blob (if any) is whatever's left after the third
// field, minus the single separating space istream>> already
// consumed up to.
if (!flagsBlob.empty() && flagsBlob[0] == ' ')
flagsBlob.erase(0, 1);
// A short/truncated line (a write cut off mid-flush by a
// crash or restart racing save() - which already logs that
// this exact failure mode is possible, see save() below)
// leaves uidStr/dateStr empty, and stoul/stoll throw
// std::invalid_argument on that - uncaught, that previously
// propagated all the way up through load() and every IMAP
// handler that calls it (SELECT, EXAMINE) into
// CommandList::processRequest()'s own generic catch-all,
// which silently swallowed it and sent NO response at all -
// indistinguishable from the server hanging, since the
// client just waits forever for a reply that was never
// coming. Confirmed live: a single truncated trailing line
// ("Seen", missing its filename/uid/date - the tail end of
// what should have been a real message line) in a real
// user's Inbox .imapindex made every single SELECT/EXAMINE
// of that folder hang the connection, indefinitely,
// deterministically, until this line was found and
// skipped. Catching here instead means one corrupt line is
// dropped (logged) rather than taking the whole folder
// load - and therefore the whole command - down; the
// in-memory message list this produces is what save() (also
// called at the end of load(), unconditionally) rewrites
// the index file from, so the corrupt line is
// self-healed away on the very next successful load().
message.uid = (uint32_t)std::stoul(uidStr);
message.internalDate = (time_t)std::stoll(dateStr);
message.flags = splitFlags(flagsBlob);
mMessages.push_back(message);
} catch (...) {
coreutils::Log(coreutils::LOG_EXCEPT) << "MailFolder: skipping malformed .imapindex line in " << path << ": [" << line << "]";
}
}
}
@@ -160,9 +198,15 @@ namespace mail {
writeCounterFile(path + "/.uidvalidity", mUidValidity);
writeCounterFile(path + "/.uidnext", mUidNext);
std::ofstream out(path + "/.imapindex", std::ios::trunc);
if (!out) {
coreutils::Log(coreutils::LOG_EXCEPT) << "MailFolder: failed to open " << path << "/.imapindex for writing - flag/message changes will not persist. Check ownership/permissions.";
return;
}
for (const Message &message : mMessages)
out << message.fileName << " " << message.uid << " " << (long long)message.internalDate
<< " " << joinFlags(message.flags) << "\n";
if (!out)
coreutils::Log(coreutils::LOG_EXCEPT) << "MailFolder: write to " << path << "/.imapindex failed partway through - index may be incomplete.";
}
uint32_t MailFolder::seq(uint32_t uid) const {
+51
View File
@@ -0,0 +1,51 @@
#ifndef __ResolverThreadScope_h__
#define __ResolverThreadScope_h__
#include "ThreadScope.h"
#include <resolv.h>
namespace mail {
///
/// ResolverThreadScope
///
/// glibc's classic resolver state (_res, touched by res_query() and
/// getaddrinfo()) is thread-LOCAL, not process-global - confirmed
/// directly (a small standalone test program): a fresh thread's own
/// _res starts zeroed, and its first real resolver call lazily
/// auto-initializes it to glibc's own compiled-in defaults
/// (RES_TIMEOUT=5s, RES_DFLRETRY=2 tries - see /usr/include/resolv.h),
/// completely ignoring anything main() set beforehand. An earlier fix
/// attempt called res_init()+set _res.retrans/_res.retry once in
/// main() before EPoll::start() spawned its worker threads - that
/// only ever affected the MAIN thread's own copy, a pure no-op for
/// every one of the 4 worker threads that actually run
/// SPFChecker/DMARCChecker's res_query()/getaddrinfo() calls for real
/// inbound mail. Same shape as MysqlThreadScope's own reason to
/// exist (see its doc comment) - a client library keying off
/// per-thread state that nothing else in this stack initializes on
/// EPoll's worker threads.
///
/// Bounding these here is what actually keeps a single slow or
/// non-responding DNS server from blocking a worker thread long
/// enough to stall every OTHER connection on every listening port
/// too (SMTP/SMTPTLS/IMAP/POP3/admin all share these same 4 threads) -
/// SPF's own "include:" mechanism can chain up to 10 sequential
/// lookups for a single message, so the per-query bound matters more
/// here than it would for an isolated one-off query.
///
class ResolverThreadScope : public core::ThreadScope {
public:
void onThreadStart() override {
res_init();
_res.retrans = 2;
_res.retry = 1;
}
};
}
#endif
+11
View File
@@ -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());
}
};
}
+45 -11
View File
@@ -4,13 +4,15 @@
#include "SpamAssassinFilter.h"
#include "File.h"
#include <thread>
#include <sys/stat.h>
#include "SMTPSyntax.h"
namespace mail {
SMTPServer::SMTPServer(core::EPoll &ePoll, std::string hostName, MailFileSystem &mailFileSystem,
core::IPAddress ipAddress, core::TLSInfo *tlsInfo, bool watchQueue,
DKIMSigner *dkimSigner)
: core::TCPServer(ePoll, ipAddress, tlsInfo, " ", 1, "SMTP Server"), core::INotify(ePoll), ePoll(ePoll), hostName(hostName), mailFileSystem(mailFileSystem), dkimSigner(dkimSigner) {
std::map<std::string, DKIMSigner*> *dkimSigners)
: core::TCPServer(ePoll, ipAddress, tlsInfo, " ", 1, "SMTP Server"), core::INotify(ePoll), ePoll(ePoll), hostName(hostName), mailFileSystem(mailFileSystem), dkimSigners(dkimSigners) {
commands.add(_smtp_auth, "AUTH");
commands.add(_smtp_data, "DATA");
commands.add(_smtp_ehlo, "EHLO");
@@ -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,13 +142,13 @@ 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());
SendMail *outbound = new SendMail(hostName, mailFileSystem.getSendLogPath().str(),
name[0].str(), sender, recipient, mailData, dkimSigner);
name[0].str(), sender, recipient, mailData, dkimSigners);
// The queue file's content is already fully read into
// mailData above, so it's safe to unlink it now rather
// than after the detached thread finishes (which may be
+51 -7
View File
@@ -3,6 +3,7 @@
# include <string>
# include <vector>
# include <map>
# include <filesystem>
# include "EPoll.h"
# include "TCPServer.h"
@@ -20,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"
@@ -38,27 +40,66 @@ namespace mail {
// each SMTPServer's own INotify watch is independent, so two
// watchers on the same directory would each fire inCreate() for
// every queued message, delivering it twice.
// dkimSigner is optional (nullptr = outbound mail goes out
// unsigned) and not owned - passed straight through to every
// SendMail this instance's inCreate() constructs for outbound
// relay.
// dkimSigners is optional (nullptr = outbound mail goes out
// unsigned for every domain) and not owned - passed straight
// through to every SendMail this instance's inCreate() constructs
// for outbound relay, which picks the entry matching each
// individual message's own sender domain (see SendMail's own doc
// comment).
SMTPServer(core::EPoll &ePoll, std::string hostName, MailFileSystem &mailFileSystem,
core::IPAddress ipAddress, core::TLSInfo *tlsInfo, bool watchQueue = true,
DKIMSigner *dkimSigner = nullptr);
std::map<std::string, DKIMSigner*> *dkimSigners = nullptr);
MailFileSystem &mailFileSystem;
std::string hostName;
core::EPoll &ePoll;
DKIMSigner *dkimSigner;
std::map<std::string, DKIMSigner*> *dkimSigners;
// Advertised via EHLO's "250-SIZE <n>" and enforced by __SMTP_DATA
// while accumulating a message body - RFC 1870. 25MB, matching
// 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);
@@ -74,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;
@@ -84,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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
+21 -8
View File
@@ -1,6 +1,7 @@
#include "SendMail.h"
#include "Log.h"
#include <algorithm>
#include <map>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <cstdlib>
@@ -16,9 +17,9 @@ namespace mail {
SendMail::SendMail(std::string hostName, std::string sendLogPath, std::string messageId,
coreutils::MString from, coreutils::MString recipient, coreutils::MString mailData,
DKIMSigner *dkimSigner)
std::map<std::string, DKIMSigner*> *dkimSigners)
: hostName(hostName), sendLogPath(sendLogPath), messageId(messageId),
from(from), recipient(recipient), mailData(mailData), dkimSigner(dkimSigner) {}
from(from), recipient(recipient), mailData(mailData), dkimSigners(dkimSigners) {}
std::string SendMail::domainOf(coreutils::MString &address) {
std::string addr = address.str();
@@ -216,17 +217,29 @@ namespace mail {
coreutils::File log(logFileName, O_CREAT | O_WRONLY | O_APPEND, 0660);
log.write("=== delivery attempt: message " + messageId + " -> " + recipient.str() + " ===\n");
if (dkimSigner) {
if (dkimSigners) {
// Signed once here, not inside deliverToHost() - deliverToHost()
// can run again against a second MX host if the first fails, and
// the signature (over this exact message content) is the same
// either way, so signing once upfront avoids redundant RSA ops
// and keeps every delivery attempt for this message consistent.
std::string signature = dkimSigner->sign(mailData.str());
coreutils::MString signedData;
signedData << signature.c_str() << mailData;
mailData = signedData;
log.write("DKIM-signed.\n");
// Which signer (if any) is used depends on the sender's own
// domain, not a single server-wide identity - a real multi-
// domain deployment must never sign one domain's mail with a
// different domain's key (DMARC alignment would fail it
// anyway, and it would leak which private key production
// uses for an unrelated domain).
std::string fromDomain = domainOf(from);
auto it = dkimSigners->find(fromDomain);
if (it != dkimSigners->end()) {
std::string signature = it->second->sign(mailData.str());
coreutils::MString signedData;
signedData << signature.c_str() << mailData;
mailData = signedData;
log.write("DKIM-signed (domain: " + fromDomain + ").\n");
} else {
log.write("No DKIM key configured for domain: " + fromDomain + " - sending unsigned.\n");
}
}
std::string domain = domainOf(recipient);
+11 -6
View File
@@ -4,6 +4,7 @@
#include "DKIMSigner.h"
#include "File.h"
#include "MString.h"
#include <map>
#include <string>
#include <vector>
@@ -41,13 +42,17 @@ namespace mail {
class SendMail {
public:
// dkimSigner is optional (nullptr = don't sign) and not owned -
// the caller keeps it alive for as long as any SendMail built
// from it might still be sending (a shared signer is fine to
// reuse across many messages; it holds no per-message state).
// dkimSigners is optional (nullptr = don't sign anything) and not
// owned - the caller keeps both it and every DKIMSigner it points
// to alive for as long as any SendMail built from it might still
// be sending (shared, reusable across many messages; a signer
// holds no per-message state). Keyed by domain (matching a
// sender address's own domain, case-sensitive exact match, via
// domainOf() on `from`) - a domain with no entry just goes out
// unsigned, same as dkimSigners being nullptr entirely.
SendMail(std::string hostName, std::string sendLogPath, std::string messageId,
coreutils::MString from, coreutils::MString recipient, coreutils::MString mailData,
DKIMSigner *dkimSigner = nullptr);
std::map<std::string, DKIMSigner*> *dkimSigners = nullptr);
/// Blocking. Returns true if some MX host for the recipient's
/// domain accepted the message, false if DNS resolution or every
@@ -68,7 +73,7 @@ namespace mail {
coreutils::MString from;
coreutils::MString recipient;
coreutils::MString mailData;
DKIMSigner *dkimSigner;
std::map<std::string, DKIMSigner*> *dkimSigners;
};
+150 -93
View File
@@ -1,108 +1,165 @@
#include "__SMTP_AUTH.h"
#include "Base64.h"
#include "SMTPServer.h"
#include "SMTPSyntax.h"
namespace mail {
// SMTP AUTH (RFC 4954) with the PLAIN (RFC 4616) and LOGIN mechanisms.
//
// The first line ("AUTH mech [initial-response]") arrives through the
// normal command dispatch; while an exchange is in progress grabInput()
// routes every following line here unsplit, and authState says which
// response it is.
namespace {
const char *CANCELLED = "501 5.0.0 Authentication cancelled";
const char *BAD_BASE64 = "501 5.5.2 Cannot decode response";
const char *FAILED = "535 5.7.8 Authentication credentials invalid";
}
void __SMTP_AUTH::finish(SMTPSession &session, const char *reply) {
session.out << reply << CRLF;
session.authState = USER_UNKNOWN;
session.userName = "";
session.password = "";
clearGrab(session);
}
void __SMTP_AUTH::verify(SMTPSession &session, SMTPServer &server) {
if (authLogin(session.userName, session.password, server)) {
session.out << "235 2.7.0 Authentication successful" << CRLF;
session.authState = USER_KNOWN;
session.relay = true;
session.password = "";
clearGrab(session);
} else {
// Back to a clean state so the client can try again on the
// same connection (RFC 4954 4).
finish(session, FAILED);
}
}
// PLAIN message: [authzid] NUL authcid NUL passwd (RFC 4616 2).
void __SMTP_AUTH::plain(const std::string &response, SMTPSession &session, SMTPServer &server) {
size_t a = response.find('\0');
size_t b = a == std::string::npos ? a : response.find('\0', a + 1);
if (b == std::string::npos || response.find('\0', b + 1) != std::string::npos) {
finish(session, "501 5.5.2 Malformed PLAIN response");
return;
}
std::string authzid = response.substr(0, a);
std::string authcid = response.substr(a + 1, b - a - 1);
std::string passwd = response.substr(b + 1);
// This server has no proxy-authorization: acting as anyone other
// than the authenticated identity is refused.
if (authcid.empty() || (!authzid.empty() && authzid != authcid)) {
finish(session, FAILED);
return;
}
session.userName = authcid.c_str();
session.password = passwd.c_str();
verify(session, server);
}
int __SMTP_AUTH::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
coreutils::Base64 base64;
std::string text = line(request);
switch(session.authState) {
case USER_UNKNOWN:
// SMTPServer's CommandList splits the line only once ("AUTH",
// depth=1 - see its own constructor), so request[1] here is
// "LOGIN" alone for the classic multi-step form, but "LOGIN
// <base64>" as ONE token when a client supplies RFC 4954 4's
// optional initial-response inline to save a round trip -
// confirmed live against Python's smtplib, which does exactly
// that. The old request[1].equals("LOGIN") exact-match rejected
// every such client with a spurious 504, never reaching the
// inline response at all.
if(request[1].ifNextIgnoreCase("LOGIN")) {
request[1].skipWhitespace();
if(!request[1].eod()) {
session.userName = base64.decode(request[1].unparsed());
session.out << "334 UGFzc3dvcmQ6" << CRLF;
session.authState = USER_SECRET_QUERY;
grabInput(session);
} else {
session.out << "334 VXNlcm5hbWU6" << CRLF;
// setTimer(10.0f);
session.authState = USER_QUERY;
grabInput(session);
}
}
else {
session.out << "504 AUTH method not supported." << CRLF;
}
return 1;
case USER_QUERY:
// RFC 4954 4: a bare "*" cancels the exchange rather than being
// treated as (still-encoded) credential data.
if (request.equals("*")) {
session.out << "501 5.7.0 Authentication cancelled" << CRLF;
session.authState = USER_UNKNOWN;
clearGrab(session);
return 1;
}
// grabInput() above means this line never goes through
// CommandList's own .split() (see CommandList::processRequest's
// grab branch) - request[0] here was actually an out-of-bounds
// read on an always-empty list, not "the first word". The
// whole line IS the client's response: a single base64-encoded
// token with no embedded whitespace, exactly what the unsplit
// request already holds. It was never decoded either - AUTH
// LOGIN's wire format sends the username/password base64
// encoded (that's what the "334 VXNlcm5hbWU6"/"334
// UGFzc3dvcmQ6" challenges - "Username:"/"Password:" - signal)
// - comparing the raw, still-encoded text against a plaintext
// secret file could never match for any real client.
session.userName = base64.decode(request);
// setTimer(0.0f);
session.out << "334 UGFzc3dvcmQ6" << CRLF;
// setTimer(10.0f);
session.authState = USER_SECRET_QUERY;
return 1;
case USER_SECRET_QUERY:
if (request.equals("*")) {
session.out << "501 5.7.0 Authentication cancelled" << CRLF;
session.authState = USER_UNKNOWN;
clearGrab(session);
return 1;
}
session.password = base64.decode(request);
// setTimer(0.0f);
if(authLogin(session.userName, session.password, server)) {
session.out << "235 2.7.0 Authentication successful" << CRLF;
session.relay = true;
session.authState = USER_KNOWN;
clearGrab(session);
}
else {
// Reset back to a clean, addressable state - without this,
// the session stayed grabbed and stuck expecting yet another
// "password" line forever after one wrong guess, since
// nothing else ever set authState back to USER_UNKNOWN or
// released the grab. A client that mistypes a password needs
// to be able to retry AUTH on the same connection, not
// reconnect entirely.
session.out << "530 Login was unsuccessful." << CRLF;
session.authState = USER_UNKNOWN;
clearGrab(session);
}
return 1;
// ---- continuation lines of an exchange in progress
if (session.authState == USER_QUERY || session.authState == USER_SECRET_QUERY ||
session.authState == PLAIN_QUERY) {
if (text == "*") { // RFC 4954 4: client cancels
finish(session, CANCELLED);
return 1;
}
std::string decoded;
if (!SMTPSyntax::decodeBase64(text, decoded)) {
finish(session, BAD_BASE64);
return 1;
}
if (session.authState == PLAIN_QUERY) {
plain(decoded, session, server);
} else if (session.authState == USER_QUERY) {
session.userName = decoded.c_str();
session.authState = USER_SECRET_QUERY;
session.out << "334 UGFzc3dvcmQ6" << CRLF; // "Password:"
} else {
session.password = decoded.c_str();
verify(session, server);
}
return 1;
}
return 0;
// ---- a new AUTH command (RFC 4954 4)
if (!session.esmtp) {
session.out << "503 5.5.1 Send EHLO first" << CRLF;
return 1;
}
if (session.authState == USER_KNOWN) {
session.out << "503 5.5.1 Already authenticated" << CRLF;
return 1;
}
if (session.state == MAIL || session.state == RCPT) {
session.out << "503 5.5.1 AUTH not permitted during a mail transaction" << CRLF;
return 1;
}
if (!server.authAllowed(session)) {
session.out << "538 5.7.11 Encryption required for requested authentication mechanism" << CRLF;
return 1;
}
std::string arg = SMTPSyntax::argument(text);
if (arg.empty()) {
session.out << "501 5.5.4 Syntax: AUTH mechanism [initial-response]" << CRLF;
return 1;
}
size_t sp = arg.find(' ');
std::string mechanism = SMTPSyntax::upper(arg.substr(0, sp));
std::string initial = sp == std::string::npos ? "" : SMTPSyntax::argument(arg);
bool hasInitial = sp != std::string::npos && !initial.empty();
if (mechanism != "PLAIN" && mechanism != "LOGIN") {
session.out << "504 5.5.4 Unrecognized authentication type" << CRLF;
return 1;
}
if (hasInitial) {
// "=" is an initial response of zero length (RFC 4954 4).
std::string decoded;
if (initial != "=" && !SMTPSyntax::decodeBase64(initial, decoded)) {
session.out << BAD_BASE64 << CRLF;
return 1;
}
if (mechanism == "PLAIN") {
plain(decoded, session, server);
} else {
session.userName = decoded.c_str();
session.authState = USER_SECRET_QUERY;
grabInput(session);
session.out << "334 UGFzc3dvcmQ6" << CRLF;
}
return 1;
}
grabInput(session);
if (mechanism == "PLAIN") {
session.authState = PLAIN_QUERY;
session.out << "334 " << CRLF; // empty challenge
} else {
session.authState = USER_QUERY;
session.out << "334 VXNlcm5hbWU6" << CRLF; // "Username:"
}
return 1;
}
bool __SMTP_AUTH::authLogin(coreutils::ZString &userName, coreutils::ZString &password, SMTPServer &server) {
// The actual check now lives in MailFileSystem::checkPassword(),
// shared with IMAP LOGIN and POP3 USER/PASS - see its own doc
// comment.
// Shared with IMAP LOGIN and POP3 USER/PASS - see
// MailFileSystem::checkPassword(). A user name that can't be a
// mailbox path (no "@", "/", "..") can't have a password file.
std::string local, domain;
SMTPSyntax::splitAddress(userName.str(), local, domain);
if (!SMTPSyntax::safePathComponent(local) || !SMTPSyntax::safePathComponent(domain))
return false;
return server.mailFileSystem.checkPassword(userName, password);
}
+5
View File
@@ -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);
};
}
+130 -136
View File
@@ -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,131 +84,126 @@ 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) {
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);
return 1;
}
case WAIT_FOR_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 != ".") {
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;
}
text.swap(normalized);
}
text += "\r\n";
session.mailData << text.c_str();
return 1;
}
switch(session.state) {
// ---- end of data
clearGrab(session);
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 {
std::string message = session.mailData.str();
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.
session.mailData = receivedHeader(session, server);
session.mailData << session.spfHeader.c_str();
session.mode = RECEIVE_DATA;
grabInput(session);
// session.setTimer(120.0f);
break;
// DKIM verification + DMARC alignment, now that the full
// message (including whatever From:/DKIM-Signature headers
// the client sent) is actually available - neither can run
// any earlier than this.
std::vector<DKIMVerifier::SignatureResult> dkimResults = DKIMVerifier::verify(message);
std::string fromDomain = extractFromDomain(message);
DMARCChecker::Policy dmarcPolicy;
DMARCChecker::Result dmarcResult = DMARCChecker::evaluate(
fromDomain, session.spfDomain, session.spfResult, dkimResults, dmarcPolicy);
std::string authResults = DMARCChecker::authenticationResultsHeader(
session.spfResult, session.spfDomain, dkimResults, dmarcResult, fromDomain, server.hostName);
case MAIL:
session.out << "503 Please use RCPT first" << CRLF;
break;
// RFC 7489 6.3: pct= applies the published disposition to only
// that percentage of otherwise-failing messages - the
// remainder still gets the Authentication-Results header
// recording the real (failing) result, just isn't rejected
// for it. p=none/quarantine never reject outright either way
// (quarantine has no separate mailbox to route into here, so
// it degrades to "accept, but the header says fail" - still a
// meaningful signal for whatever reads the header downstream).
bool sampled = (dmarcPolicy.percent >= 100) || ((rand() % 100) < dmarcPolicy.percent);
bool reject = (dmarcResult == DMARCChecker::Result::Fail) && dmarcPolicy.policy == "reject" && sampled;
default:
session.out << "503 Please use MAIL first" << CRLF;
break;
}
break;
case RECEIVE_DATA:
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;
}
else {
session.mode = WAIT_FOR_DATA;
session.state = READY;
clearGrab(session);
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) {
std::string message = session.mailData.str();
// DKIM verification + DMARC alignment, now that the full
// message (including whatever From:/DKIM-Signature headers
// the client sent) is actually available - neither can run
// any earlier than this.
std::vector<DKIMVerifier::SignatureResult> dkimResults = DKIMVerifier::verify(message);
std::string fromDomain = extractFromDomain(message);
DMARCChecker::Policy dmarcPolicy;
DMARCChecker::Result dmarcResult = DMARCChecker::evaluate(
fromDomain, session.spfDomain, session.spfResult, dkimResults, dmarcPolicy);
std::string authResults = DMARCChecker::authenticationResultsHeader(
session.spfResult, session.spfDomain, dkimResults, dmarcResult, fromDomain, server.hostName);
// RFC 7489 6.3: pct= applies the published disposition to only
// that percentage of otherwise-failing messages - the
// remainder still gets the Authentication-Results header
// recording the real (failing) result, just isn't rejected
// for it. p=none/quarantine never reject outright either way
// (quarantine has no separate mailbox to route into here, so
// it degrades to "accept, but the header says fail" - still a
// meaningful signal for whatever reads the header downstream).
bool sampled = (dmarcPolicy.percent >= 100) || ((rand() % 100) < dmarcPolicy.percent);
bool reject = (dmarcResult == DMARCChecker::Result::Fail) && dmarcPolicy.policy == "reject" && sampled;
if (reject) {
coreutils::Log(coreutils::LOG_INFO) << "Rejected message from " << session.from
<< " - DMARC fail, p=reject for " << fromDomain.c_str() << ".";
session.out << "550 5.7.1 Message rejected: failed DMARC policy for " << fromDomain.c_str() << CRLF;
} else {
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;
}
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;
}
if (reject) {
coreutils::Log(coreutils::LOG_INFO) << "Rejected message from " << session.from
<< " - DMARC fail, p=reject for " << fromDomain.c_str() << ".";
session.out << "550 5.7.1 Message rejected: failed DMARC policy for " << fromDomain.c_str() << CRLF;
} else {
coreutils::MString withAuthResults(authResults);
withAuthResults << session.mailData;
session.mailData = withAuthResults;
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;
}
}
// RFC 5321 4.1.1.4: the transaction is over whatever the outcome.
session.resetTransaction();
return 1;
}
@@ -214,34 +211,31 @@ namespace mail {
struct timespec timex;
clock_gettime(CLOCK_REALTIME, &timex);
std::stringstream temp;
temp << timex.tv_sec << "." << std::setfill('0') << std::setw(9) << timex.tv_nsec;
temp << timex.tv_sec << "." << std::setfill('0') << std::setw(9) << timex.tv_nsec;
return coreutils::MString(temp.str());
}
coreutils::MString __SMTP_DATA::queueMail(SMTPServer &server,
coreutils::MString &sender,
std::vector<coreutils::MString> &recipientList,
coreutils::MString &mailData) {
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) {
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;
link(fileName.c_str(), newName.c_str());
// <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;
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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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;
}
+74 -45
View File
@@ -1,58 +1,87 @@
#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;
session.recipientList.clear();
session.state = MAIL;
// 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;
}
// 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.
std::string fromStr = session.from.str();
size_t at = fromStr.find_last_of('@');
std::string spfDomain = (at == std::string::npos) ? session.clientDomainName.str() : fromStr.substr(at + 1);
std::string clientIP = session.ipAddress.getClientAddress();
session.spfDomain = spfDomain;
session.spfResult = SPFChecker::check(spfDomain, clientIP);
session.spfHeader = SPFChecker::receivedSPFHeader(session.spfResult, spfDomain, clientIP,
session.clientDomainName.str(), server.hostName);
} else
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
} else
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
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 &param : arg.params) {
if (!session.esmtp) {
session.out << "555 5.5.4 MAIL parameters require EHLO" << CRLF;
return 1;
}
if (param.first == "SIZE") {
// RFC 1870 6.1: refuse up front if the declared size is too big.
const std::string &v = param.second;
if (v.empty() || v.size() > 20 || v.find_first_not_of("0123456789") != std::string::npos) {
session.out << "501 5.5.4 Syntax: SIZE=<number>" << CRLF;
return 1;
}
if (v.size() > 18 || std::stoull(v) > SMTPServer::maxMessageSize) {
session.out << "552 5.3.4 Message size exceeds fixed maximum message size" << CRLF;
return 1;
}
} else if (param.first == "BODY") {
// RFC 6152: 7BIT and 8BITMIME. BINARYMIME needs CHUNKING,
// which isn't offered.
std::string v = SMTPSyntax::upper(param.second);
if (v != "7BIT" && v != "8BITMIME") {
session.out << "555 5.5.4 Unsupported BODY type" << CRLF;
return 1;
}
} else if (param.first == "AUTH") {
// RFC 4954 5: accepted; the identity it carries is only
// trusted from an authenticated relay, which this isn't
// acting as - nothing to record.
} else {
session.out << "555 5.5.4 Unsupported parameter " << param.first.c_str() << CRLF;
return 1;
}
}
session.from = arg.address.c_str();
session.recipientList.clear();
session.state = MAIL;
// Deliberately not gated on authentication: a receiving MX accepts
// MAIL from anyone. Authentication only gates relaying to a
// non-local recipient, which __SMTP_RCPT decides.
//
// SPF (RFC 7208 2.4): the identity is the MAIL FROM domain, or for
// the null reverse-path <> the HELO/EHLO domain. Computed here,
// where the RFC anchors it, and reused at DATA via spfHeader.
std::string fromStr = session.from.str();
size_t at = fromStr.find_last_of('@');
std::string spfDomain = (at == std::string::npos) ? session.clientDomainName.str() : fromStr.substr(at + 1);
std::string clientIP = session.ipAddress.getClientAddress();
session.spfDomain = spfDomain;
session.spfResult = SPFChecker::check(spfDomain, clientIP);
session.spfHeader = SPFChecker::receivedSPFHeader(session.spfResult, spfDomain, clientIP,
session.clientDomainName.str(), server.hostName);
session.out << "250 2.1.0 Sender OK" << CRLF;
return 1;
}
}
+2 -1
View File
@@ -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
View File
@@ -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;
}
}
+70 -21
View File
@@ -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;
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;
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;
session.out << "250 2.1.5 Recipient OK" << CRLF;
return 1;
}
+11 -2
View File
@@ -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;
}
+39
View File
@@ -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;
}
}
+25
View File
@@ -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
View File
@@ -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;
}
}
+65 -18
View File
@@ -12,6 +12,7 @@
#include "SendMail.h"
#include "BARANTMailConfig.h"
#include "DKIMSigner.h"
#include <map>
#include "__ADMIN_DOMAIN.h"
#include "__ADMIN_MAILBOX.h"
#include "__ADMIN_ALIAS.h"
@@ -21,6 +22,7 @@
#include <iostream>
#include <signal.h>
#include <sstream>
#include "ResolverThreadScope.h"
int main(int argc, char **argv) {
// Every socket write in this process (outbound relay in
@@ -61,14 +63,19 @@ int main(int argc, char **argv) {
// as much "outbound mail from this server" as anything queued
// through SMTP, and should be signed the same way. A missing
// config file here (BARANTMailConfig's own JFile falls back to
// an empty "{}") just means hasDkim() is false and this
// an empty "{}") just means getDkimEntries() is empty and this
// continues unsigned, not a hard failure.
mail::DKIMSigner *dkimSigner = nullptr;
std::map<std::string, mail::DKIMSigner*> *dkimSigners = nullptr;
try {
mail::BARANTMailConfig config("barantmail.cfg");
if (config.hasDkim())
dkimSigner = new mail::DKIMSigner(config.getDkimDomain(), config.getDkimSelector(),
config.getDkimPrivateKeyPath());
coreutils::Log::maxLogSize = config.getLogMaxSizeBytes();
coreutils::Log::maxLogBackups = config.getLogMaxBackups();
auto dkimEntries = config.getDkimEntries();
if (!dkimEntries.empty()) {
dkimSigners = new std::map<std::string, mail::DKIMSigner*>();
for (auto &entry : dkimEntries)
(*dkimSigners)[entry.domain] = new mail::DKIMSigner(entry.domain, entry.selector, entry.privateKeyPath);
}
} catch (...) {
// Config missing hostName/mailPath/smtp.port (all required -
// see BARANTMailConfig's own doc comment) - fine, this
@@ -85,7 +92,7 @@ int main(int argc, char **argv) {
idStream << timex.tv_sec << "." << std::setfill('0') << std::setw(9) << timex.tv_nsec;
mail::SendMail mail("localhost", mailFileSystem.getSendLogPath().str(), idStream.str(),
coreutils::MString(argv[1]), coreutils::MString(argv[2]), message, dkimSigner);
coreutils::MString(argv[1]), coreutils::MString(argv[2]), message, dkimSigners);
bool ok = mail.send();
coreutils::Log(coreutils::LOG_DEBUG_1) << "send result: " << (ok ? "delivered" : "failed");
std::cout << (ok ? "250 OK\n" : "550 delivery failed\n");
@@ -101,6 +108,8 @@ int main(int argc, char **argv) {
std::string configPath = (argc > 1) ? argv[1] : "barantmail.cfg";
mail::BARANTMailConfig config(configPath);
coreutils::Log::maxLogSize = config.getLogMaxSizeBytes();
coreutils::Log::maxLogBackups = config.getLogMaxBackups();
std::string hostName = config.getHostName();
std::string ipAddress = "0.0.0.0";
@@ -112,16 +121,37 @@ int main(int argc, char **argv) {
mail::MailFileSystem mailFileSystem(mailPath);
core::EPoll ePoll;
ePoll.start(4, 1000);
// glibc's resolver state (_res, touched by res_query()/
// getaddrinfo() inside SPFChecker/DMARCChecker) is thread-local -
// confirmed directly (a standalone test program) that a fresh
// thread's _res starts zeroed and its own first resolver call
// lazily auto-initializes it to glibc's compiled-in defaults
// (RES_TIMEOUT=5s, RES_DFLRETRY=2 tries), regardless of anything
// set on a different thread beforehand. An earlier fix attempt
// set _res.retrans/_res.retry once here in main() before
// start() - a pure no-op for every one of these 4 worker
// threads, none of which ever saw it. See ResolverThreadScope's
// own doc comment for the full reasoning; same shape as
// MysqlThreadScope's per-thread mysql_thread_init() need just
// below.
static mail::ResolverThreadScope resolverThreadScope;
ePoll.start(4, 1000, &resolverThreadScope);
// One signer, shared by every outbound-relaying listener - it
// holds no per-message state (see DKIMSigner's own doc comment),
// so there's no reason for the plaintext and TLS SMTP listeners
// to each load their own copy of the same private key.
mail::DKIMSigner *dkimSigner = nullptr;
if (config.hasDkim())
dkimSigner = new mail::DKIMSigner(config.getDkimDomain(), config.getDkimSelector(),
config.getDkimPrivateKeyPath());
// One map of signers, shared by every outbound-relaying listener -
// each DKIMSigner holds no per-message state (see its own doc
// comment), so there's no reason for the plaintext and TLS SMTP
// listeners to each load their own copies of the same keys. Built
// once here regardless of how many domains are configured -
// SendMail picks the entry matching each individual outbound
// message's own sender domain at send time (see its own doc
// comment) rather than this server having one fixed identity.
std::map<std::string, mail::DKIMSigner*> *dkimSigners = nullptr;
auto dkimEntries = config.getDkimEntries();
if (!dkimEntries.empty()) {
dkimSigners = new std::map<std::string, mail::DKIMSigner*>();
for (auto &entry : dkimEntries)
(*dkimSigners)[entry.domain] = new mail::DKIMSigner(entry.domain, entry.selector, entry.privateKeyPath);
}
// Port 25 always runs unencrypted and always owns the mail queue
// watch (see SMTPServer's own doc comment on watchQueue) - an
@@ -130,13 +160,30 @@ int main(int argc, char **argv) {
// needs regardless of what else is configured.
mail::SMTPServer smtpServer(ePoll, hostName, mailFileSystem,
core::IPAddress(ipAddress, config.getSmtpPort()), nullptr, true,
dkimSigner);
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,
core::IPAddress(ipAddress, config.getSmtpTLSPort()),
config.getTlsInfo(), false, dkimSigner);
config.getTlsInfo(), false, dkimSigners);
mail::POP3Server *pop3Server = nullptr;
if (config.hasPop3())
@@ -167,7 +214,7 @@ int main(int argc, char **argv) {
if (config.hasAdmin()) {
std::string adminUser = config.getAdminUser();
std::string adminPasswordValue = config.getAdminPassword();
consoleServer = new core::ConsoleServer(ePoll, core::IPAddress(ipAddress, config.getAdminPort()), nullptr,
consoleServer = new core::ConsoleServer(ePoll, core::IPAddress(ipAddress, config.getAdminPort()), config.getTlsInfo(),
[adminUser, adminPasswordValue](std::string user, std::string password) {
return user == adminUser && password == adminPasswordValue;
});
+53
View File
@@ -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)
+46
View File
@@ -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()
+538
View File
@@ -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)