#include "__SMTP_AUTH.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) { std::string text = line(request); // ---- 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; } // ---- 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) { // 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); } }