#include "__SMTP_MAIL.h" #include "SMTPServer.h" #include "SPFChecker.h" #include "SMTPSyntax.h" namespace mail { int __SMTP_MAIL::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) { // RFC 5321 4.1.4: a client MUST say HELO/EHLO before a transaction, // and MAIL may not be issued again while one is open. if (!session.greeted) { session.out << "503 5.5.1 Send HELO/EHLO first" << CRLF; return 1; } if (session.state == MAIL || session.state == RCPT) { session.out << "503 5.5.1 Nested MAIL command" << CRLF; return 1; } SMTPSyntax::PathArgument arg = SMTPSyntax::parsePath(SMTPSyntax::argument(line(request)), "FROM:", true); if (!arg.ok) { session.out << arg.reply << CRLF; return 1; } // Service-extension parameters (RFC 5321 4.1.1.11) - only after // EHLO, and only the ones this server advertises. for (auto ¶m : arg.params) { if (!session.esmtp) { session.out << "555 5.5.4 MAIL parameters require EHLO" << CRLF; return 1; } if (param.first == "SIZE") { // RFC 1870 6.1: refuse up front if the declared size is too big. const std::string &v = param.second; if (v.empty() || v.size() > 20 || v.find_first_not_of("0123456789") != std::string::npos) { session.out << "501 5.5.4 Syntax: SIZE=" << 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; } }