Files
BARANTMail/__SMTP_AUTH.cpp
T
Brad Arant cd5e0a9905 Add real IMAP4rev1/POP3 implementations, DKIM/DMARC/SPF, SpamAssassin delivery hook
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).
2026-09-03 05:24:22 +00:00

110 lines
4.0 KiB
C++

#include "__SMTP_AUTH.h"
#include "Base64.h"
#include "SMTPServer.h"
namespace mail {
int __SMTP_AUTH::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
coreutils::Base64 base64;
switch(session.authState) {
case USER_UNKNOWN:
// SMTPServer's CommandList splits the line only once ("AUTH",
// depth=1 - see its own constructor), so request[1] here is
// "LOGIN" alone for the classic multi-step form, but "LOGIN
// <base64>" as ONE token when a client supplies RFC 4954 4's
// optional initial-response inline to save a round trip -
// confirmed live against Python's smtplib, which does exactly
// that. The old request[1].equals("LOGIN") exact-match rejected
// every such client with a spurious 504, never reaching the
// inline response at all.
if(request[1].ifNextIgnoreCase("LOGIN")) {
request[1].skipWhitespace();
if(!request[1].eod()) {
session.userName = base64.decode(request[1].unparsed());
session.out << "334 UGFzc3dvcmQ6" << CRLF;
session.authState = USER_SECRET_QUERY;
grabInput(session);
} else {
session.out << "334 VXNlcm5hbWU6" << CRLF;
// setTimer(10.0f);
session.authState = USER_QUERY;
grabInput(session);
}
}
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;
}
return 0;
}
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.
return server.mailFileSystem.checkPassword(userName, password);
}
}