# include "SMTPSession.h" # include "SMTPServer.h" # include "Log.h" namespace mail { SMTPSession::SMTPSession(core::EPoll &ePoll, core::TCPServer &server) : TCPSession(ePoll, &server, "SMTP Client Session") { // Only CR LF ends a line - see core::TCPSocket::strictCRLF. This is // what closes SMTP smuggling: "." (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 << " 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(); } } }