# include "__SMTP_RCPT.h" # include "SMTPServer.h" # include "SMTPSyntax.h" namespace mail { int __SMTP_RCPT::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) { if (session.state != MAIL && session.state != RCPT) { session.out << "503 5.5.1 Need MAIL command first" << CRLF; return 1; } SMTPSyntax::PathArgument arg = SMTPSyntax::parsePath(SMTPSyntax::argument(line(request)), "TO:", false); if (!arg.ok) { session.out << arg.reply << CRLF; return 1; } // No RCPT parameters are offered (no DSN) - RFC 5321 4.1.1.11. if (!arg.params.empty()) { session.out << "555 5.5.4 Unsupported parameter " << arg.params[0].first.c_str() << CRLF; return 1; } if (session.recipientList.size() >= SMTPServer::maxRecipients) { session.out << "452 4.5.3 Too many recipients" << CRLF; return 1; } std::string local, domain; SMTPSyntax::splitAddress(arg.address, local, domain); // RFC 5321 4.5.1: "postmaster" with no domain MUST be accepted, and // the postmaster local-part is case-insensitive at any local domain. bool isPostmaster = SMTPSyntax::iequals(local, "postmaster") && (domain.empty() || server.isLocalDomain(domain)); std::string recipient = local + "@" + domain; if (isPostmaster) { recipient = "postmaster@" + domain; coreutils::MString r(recipient); if (domain.empty() || !(server.mailFileSystem.ifMailBoxExists(r) || server.mailFileSystem.ifAliasExists(r))) recipient = server.postmasterAddress(); if (recipient.empty()) { // Nothing to deliver it to - a configuration gap, not the // client's fault (create a postmaster mailbox or alias). session.out << "451 4.3.0 Postmaster mailbox not configured" << CRLF; return 1; } } else if (server.isLocalDomain(domain)) { // Our own domain: the mailbox or alias must exist. Local-parts // that can't be a directory name ("/", ".", "..") can't be one. // Mailbox directories are lower case; a sender writing // "Brad@..." should still reach "brad" (RFC 5321 2.4 lets a // server treat local-parts case-insensitively, and nearly all do). auto known = [&](const std::string &address) { coreutils::MString r(address); return server.mailFileSystem.ifMailBoxExists(r) || server.mailFileSystem.ifAliasExists(r); }; std::string lowered = SMTPSyntax::lower(local) + "@" + domain; if (!SMTPSyntax::safePathComponent(local)) recipient.clear(); else if (!known(recipient)) recipient = known(lowered) ? lowered : ""; if (recipient.empty()) { session.out << "550 5.1.1 No such user here" << CRLF; return 1; } } else if (!session.relay) { // Someone else's domain and the client hasn't authenticated: // relaying it would make this an open relay (RFC 5321 3.6.2, 7.1). session.out << "550 5.7.1 Relaying denied" << CRLF; return 1; } session.recipientList.push_back(coreutils::MString(recipient)); session.state = RCPT; session.out << "250 2.1.5 Recipient OK" << CRLF; return 1; } }