#include "SMTPServer.h" #include "Log.h" #include "SendMail.h" #include "SpamAssassinFilter.h" #include "File.h" #include namespace mail { SMTPServer::SMTPServer(core::EPoll &ePoll, std::string hostName, MailFileSystem &mailFileSystem, core::IPAddress ipAddress, core::TLSInfo *tlsInfo, bool watchQueue, DKIMSigner *dkimSigner) : core::TCPServer(ePoll, ipAddress, tlsInfo, " ", 1, "SMTP Server"), core::INotify(ePoll), ePoll(ePoll), hostName(hostName), mailFileSystem(mailFileSystem), dkimSigner(dkimSigner) { commands.add(_smtp_auth, "AUTH"); commands.add(_smtp_data, "DATA"); commands.add(_smtp_ehlo, "EHLO"); commands.add(_smtp_expn, "EXPN"); commands.add(_smtp_helo, "HELO"); commands.add(_smtp_help, "HELP"); commands.add(_smtp_mail, "MAIL"); commands.add(_smtp_noop, "NOOP"); commands.add(_smtp_quit, "QUIT"); commands.add(_smtp_rcpt, "RCPT"); commands.add(_smtp_rset, "RSET"); commands.add(_smtp_vrfy, "VRFY"); if (watchQueue) { processExisting(); wd = addWatch(mailFileSystem.getQueuePath()); } } SMTPSession * SMTPServer::getSocketAccept(core::EPoll &ePoll) { return new SMTPSession(ePoll, *this); } void SMTPServer::sessionErrorHandler(std::string errorString, std::stringstream &out) { out << "252 " << errorString << CRLF; } void SMTPServer::inCreate(coreutils::ZString &name) { coreutils::Log(coreutils::LOG_DEBUG_2) << "inCreate; [" << name << "]"; name.split("|"); // Three parts now: messageid|recipient|sender - see // __SMTP_DATA::queueMail()'s own doc comment for why the sender // had to be added alongside the recipient. if(name.getList().size() > 2) { bool exists = mailFileSystem.ifMailBoxExists(name[1]); coreutils::MString path; path << mailFileSystem.getMailBoxPath(name[1]) << "/Inbox/" << name[0]; coreutils::MString from; from << mailFileSystem.getQueuePath() << "/" << name; if(exists) { // Scanned on the way into the recipient's own Inbox, not the // outbound relay path below - spamd's added X-Spam-* headers // are for whatever reads this recipient's mailbox, and a // message this server relays onward gets scored by ITS // destination's own filtering, same as any other MTA. Reading // the whole file in (rather than the plain link() this branch // used to do) is unavoidable now that spamd may have rewritten // the content the recipient actually needs to receive. coreutils::File queuedFile(from, O_RDONLY); queuedFile.read(); SpamAssassinFilter::Result scan = SpamAssassinFilter::scan(queuedFile.asString()); coreutils::File mailFile(path, O_CREAT | O_WRONLY, 0660); mailFile.write(scan.scanned); int rc = unlink(from.c_str()); coreutils::Log(coreutils::LOG_INFO) << "Message " << name[0] << " delivered to recipient " << name[1] << (scan.ok && scan.isSpam ? " (flagged as spam)" : "") << "."; } else { // Not a local mailbox - relay it out over SMTP. SendMail's // own send() is a plain blocking call (see its doc comment // for why) - queued delivery wants fire-and-forget, so this // owns the heap-allocated SendMail itself and runs it on its // own detached thread rather than blocking this EPoll // worker thread for however long the remote host takes. coreutils::MString sender(name[2]); coreutils::MString recipient(name[1]); coreutils::File queuedFile(from, O_RDONLY); queuedFile.read(); coreutils::MString mailData(queuedFile.asString()); SendMail *outbound = new SendMail(hostName, mailFileSystem.getSendLogPath().str(), name[0].str(), sender, recipient, mailData, dkimSigner); std::thread([outbound]() { outbound->send(); delete outbound; }).detach(); } } } void SMTPServer::processExisting() { for (const auto & entry : std::filesystem::directory_iterator(mailFileSystem.getQueuePath().str())) { std::string temp(entry.path().filename().string()); coreutils::ZString temp2(temp); inCreate(temp2); } } }