IMAP (RFC 3501): LOGIN through UID, previously all stubs except CAPABILITY/LOGOUT/a partial APPEND. Multi-folder support with per-folder UID/flag indexing (new MailFolder class, backed by .uidvalidity/.uidnext/ .imapindex sidecar files), ENVELOPE support (new MessageHeaders class: header parsing, address-list parsing, IMAP quoting), and shared cursor-based argument parsing (new IMAPArgs class: sequence sets, flag lists, dates, folder-name normalization, and path-traversal protection via isValidFolderName - a bare ".." folder name would otherwise resolve into a sibling user's mailbox directory). POP3 (RFC 1939): USER through QUIT, previously only CAPA. Added UIDL. POP3 handlers use cursor-based parsing throughout rather than indexed access - ZString's copy constructor doesn't copy its `list` field, and POP3's dispatch passes the request by value, so indexed access into a copy was silently reading nothing. SMTP: DMARC evaluation added to the DATA-terminator path (DKIM verification + alignment/policy, Authentication-Results header, conditional rejection matching RFC 7489 6.3's p=reject + sampled pct=). AUTH LOGIN now handles RFC 4954's optional inline initial response (e.g. Python smtplib's "AUTH LOGIN <base64>" in one line) instead of only the exact three-line form. Outbound relay now forces IPv4 (AF_INET) instead of AF_UNSPEC - this server's IPv4 has a correct PTR record and SPF coverage, its IPv6 doesn't, and Gmail was rejecting outbound mail with 550 5.7.25 as a result. Delivery pipeline: local delivery now runs the message through SpamAssassin (new SpamAssassinFilter class, speaking spamd's PROCESS protocol directly over a raw socket - fails open/delivers unscanned if spamd is unreachable) instead of a plain hardlink, since scanning can tag/modify content. BARANTMailConfig: extracted config loading into its own class. compile: (see individual file changes - build script adjustments to match the above). Deliberately excluded from this commit: barantmail.log/ barantmail-service.log (operational logs, not source - barantmail.log in particular was the runaway 55GB/12.9GB file from a busy-loop bug fixed separately in ServerCore) and barantmail.cfg (this server's own live deployment config, matching how APIServer's equivalent config also stays untracked).
113 lines
4.1 KiB
C++
113 lines
4.1 KiB
C++
#include "SpamAssassinFilter.h"
|
|
#include "Log.h"
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
namespace mail {
|
|
|
|
namespace {
|
|
const char *SPAMD_HOST = "127.0.0.1";
|
|
const int SPAMD_PORT = 783;
|
|
}
|
|
|
|
// Speaks the spamc/spamd wire protocol (documented in spamd(1)'s
|
|
// PROTOCOL file) directly over a plain blocking socket, rather than
|
|
// shelling out to the spamc binary - one connect/send/recv is
|
|
// simpler than managing a child process's stdin/stdout pipes
|
|
// without risking a full-pipe deadlock. This blocks whichever
|
|
// thread calls it for however long spamd takes to score the
|
|
// message - same tradeoff SPFChecker::check() already makes for its
|
|
// own synchronous DNS lookups (see __SMTP_MAIL.cpp's doc comment).
|
|
// Called only from SMTPServer::inCreate(), on the queue-watcher's
|
|
// EPoll worker thread, never inline in a client's DATA command - so
|
|
// it's this thread's other queued deliveries that wait behind a
|
|
// slow scan, not a connected client.
|
|
bool SpamAssassinFilter::talkToSpamd(const std::string &message, std::string &response) {
|
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (fd < 0)
|
|
return false;
|
|
|
|
sockaddr_in addr{};
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons(SPAMD_PORT);
|
|
if (inet_pton(AF_INET, SPAMD_HOST, &addr.sin_addr) != 1) {
|
|
close(fd);
|
|
return false;
|
|
}
|
|
|
|
if (connect(fd, (sockaddr *)&addr, sizeof(addr)) != 0) {
|
|
close(fd);
|
|
return false;
|
|
}
|
|
|
|
std::string request = "PROCESS SPAMC/1.5\r\n";
|
|
request += "Content-length: " + std::to_string(message.size()) + "\r\n";
|
|
request += "\r\n";
|
|
request += message;
|
|
|
|
size_t sent = 0;
|
|
while (sent < request.size()) {
|
|
ssize_t n = send(fd, request.data() + sent, request.size() - sent, 0);
|
|
if (n <= 0) {
|
|
close(fd);
|
|
return false;
|
|
}
|
|
sent += n;
|
|
}
|
|
shutdown(fd, SHUT_WR); // tell spamd it has the whole message, without closing our read side
|
|
|
|
response.clear();
|
|
char buf[8192];
|
|
ssize_t n;
|
|
while ((n = recv(fd, buf, sizeof(buf), 0)) > 0)
|
|
response.append(buf, n);
|
|
|
|
close(fd);
|
|
return !response.empty();
|
|
}
|
|
|
|
SpamAssassinFilter::Result SpamAssassinFilter::scan(const std::string &message) {
|
|
Result result;
|
|
result.scanned = message; // fail-open default: deliver the original if anything below bails out
|
|
|
|
std::string response;
|
|
if (!talkToSpamd(message, response)) {
|
|
coreutils::Log(coreutils::LOG_WARN) << "SpamAssassin scan failed (spamd unreachable at "
|
|
<< SPAMD_HOST << ":" << SPAMD_PORT << ") - delivering unscanned.";
|
|
return result;
|
|
}
|
|
|
|
// spamd's own response is itself a small header block
|
|
// ("SPAMD/1.5 0 EX_OK\r\nContent-length: N\r\n\r\n") wrapped
|
|
// around the scanned message - strip that off before treating
|
|
// what follows as the message to actually deliver.
|
|
size_t headerEnd = response.find("\r\n\r\n");
|
|
if (headerEnd == std::string::npos) {
|
|
coreutils::Log(coreutils::LOG_WARN) << "SpamAssassin scan returned an unparseable response - delivering unscanned.";
|
|
return result;
|
|
}
|
|
|
|
std::string statusLine = response.substr(0, response.find("\r\n"));
|
|
if (statusLine.find("EX_OK") == std::string::npos) {
|
|
coreutils::Log(coreutils::LOG_WARN) << "SpamAssassin scan reported an error (" << statusLine << ") - delivering unscanned.";
|
|
return result;
|
|
}
|
|
|
|
result.scanned = response.substr(headerEnd + 4);
|
|
result.ok = true;
|
|
|
|
// spamd's required_score (local.cf) is the actual threshold;
|
|
// this just reads the Yes/No verdict spamd already reached back
|
|
// out of the header it wrote, rather than re-deciding anything.
|
|
static const std::string marker = "\r\nX-Spam-Status: ";
|
|
size_t pos = result.scanned.find(marker);
|
|
if (pos != std::string::npos)
|
|
result.isSpam = result.scanned.compare(pos + marker.size(), 3, "Yes") == 0;
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|