This commit is contained in:
Brad Arant 2022-06-29 13:34:49 -07:00
parent 37b5a5bf4e
commit 6c844983d9
27 changed files with 230 additions and 272 deletions

18
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/barant/Development/ServerCore",
"/home/barant/Development/CoreUtils"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"cmake.configureOnOpen": false
}

View File

@ -1,15 +0,0 @@
# include "Command.h"
# include "SMTPSession.h"
# include "SMTPServer.h"
# include "Log.h"
namespace mail {
int SMTPCommand::processCommand(std::string request, core::TCPSession *session, std::stringstream &data) {
coreutils::PString parser(request);
parser.split(" ");
return processCommand(parser, (SMTPSession &)*session, (SMTPServer &)session->server, data);
}
}

View File

@ -1,23 +0,0 @@
#ifndef __SMTPCommand_h__
# define __SMTPCommand_h__
# include "Command.h"
# include "SMTPSession.h"
namespace mail {
class SMTPServer;
class SMTPCommand : public core::Command {
public:
virtual int processCommand(std::string request, core::TCPSession *session, std::stringstream &data) override;
virtual int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) {
return 0;
}
};
}
#endif

View File

@ -45,25 +45,25 @@ namespace mail {
std::string mailPath; std::string mailPath;
SMTPSession * getSocketAccept(core::EPoll &ePoll) override { SMTPSession * getSocketAccept(core::EPoll &ePoll) override {
return new SMTPSession(ePoll, *this); return new SMTPSession(ePoll, *this);
} }
void sessionErrorHandler(std::string errorString, std::stringstream &out) { void sessionErrorHandler(std::string errorString, std::stringstream &out) {
out << "252 " << errorString << CRLF; out << "252 " << errorString << CRLF;
} }
protected: protected:
void inCreate(std::string name) { void inCreate(std::string name) {
int pos = name.find(".") + 10; int pos = name.find(".") + 10;
std::string mail = name.substr(0, pos); std::string mail = name.substr(0, pos);
std::string recipient = name.substr(pos); std::string recipient = name.substr(pos);
if(recipient != "") { if(recipient != "") {
std::string fileName = mailPath + "/.queue/" + name; std::string fileName = mailPath + "/.queue/" + name;
std::string path = mailFileSystem.getMailBoxPath(recipient) + "/Inbox/" + mail; std::string path = mailFileSystem.getMailBoxPath(recipient) + "/Inbox/" + mail;
int rc = link(fileName.c_str(), path.c_str()); int rc = link(fileName.c_str(), path.c_str());
rc = unlink(fileName.c_str()); rc = unlink(fileName.c_str());
coreutils::Log(coreutils::LOG_INFO) << "Message " << mail << " delivered to " << recipient << "."; coreutils::Log(coreutils::LOG_INFO) << "Message " << mail << " delivered to " << recipient << ".";
} }
} }
private: private:

View File

@ -16,10 +16,10 @@ namespace mail {
public: public:
SMTPSession(core::EPoll &ePoll, core::TCPServer &server) SMTPSession(core::EPoll &ePoll, core::TCPServer &server)
: TCPSession(ePoll, server, "SMTP Client Session") {} : TCPSession(ePoll, server, "SMTP Client Session") {}
void onConnected() override { void onConnected() override {
out << "220 localhost BMAMail" << CRLF; out << "220 localhost BMAMail" << CRLF;
} }
std::string clientDomainName; std::string clientDomainName;

View File

@ -4,63 +4,52 @@
namespace mail { namespace mail {
int __SMTP_AUTH::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) { int __SMTP_AUTH::processCommand(coreutils::ZString &request, core::TCPSession &session) {
switch(session.authState) { SMTPSession &s = dynamic_cast<SMTPSession &>(session);
SMTPServer &server = session.server;
switch((SMTPSession &)session.authState) {
case USER_UNKNOWN: case USER_UNKNOWN:
if(request[1].str() == "LOGIN") { if(request[1].equals("LOGIN") {
data << "334 VXNlcm5hbWU6" << CRLF;
data << "334 VXNlcm5hbWU6" << CRLF; // setTimer(10.0f);
s.authState = USER_QUERY;
// setTimer(10.0f); server.commands.grabInput(&session, *this);
}
session.authState = USER_QUERY; else {
session.server.commands.grabInput(&session, *this); s.out << "504 AUTH method not supported." << CRLF;
} }
else { return 1;
data << "504 AUTH method not supported." << CRLF;
}
return 1;
case USER_QUERY: case USER_QUERY:
s.userName = request[0].str();
session.userName = request[0].str(); // setTimer(0.0f);
s.out << "334 UGFzc3dvcmQ6" << CRLF;
// setTimer(0.0f); // setTimer(10.0f);
s.authState = USER_SECRET_QUERY;
data << "334 UGFzc3dvcmQ6" << CRLF; return 1;
// setTimer(10.0f);
session.authState = USER_SECRET_QUERY;
return 1;
case USER_SECRET_QUERY: case USER_SECRET_QUERY:
s.password = request[0].str();
session.password = request[0].str(); // setTimer(0.0f);
coreutils::Base64 base64;
// setTimer(0.0f); if(authLogin(s.userName, s.password, server)) {
s.out << "235 2.7.0 Authentication successful" << CRLF;
coreutils::Base64 base64; s.relay = true;
s.authState = USER_KNOWN;
if(authLogin(session.userName, session.password, server)) { server.commands.clearGrab(&session);
data << "235 2.7.0 Authentication successful" << CRLF; }
session.relay = true; else {
session.authState = USER_KNOWN; s.out << "530 Login was unsuccessful." << CRLF;
session.server.commands.clearGrab(&session); }
} return 1;
else {
data << "530 Login was unsuccessful." << CRLF;
}
return 1;
} }
return 0; return 0;
} }
bool __SMTP_AUTH::authLogin(std::string userName, std::string password, SMTPServer &server) { bool __SMTP_AUTH::authLogin(coreutils::ZString userName, coreutils::ZString password, SMTPServer &server) {
std::string secretPath = server.mailFileSystem.getMailBoxPath(userName) + "/.password"; std::string secretPath = server.mailFileSystem.getMailBoxPath(userName) + "/.password";
coreutils::File secret(secretPath); coreutils::File secret(secretPath);
secret.read(); secret.read();

View File

@ -1,19 +1,17 @@
#ifndef ____SMTP_AUTH_h__ #ifndef ____SMTP_AUTH_h__
# define ____SMTP_AUTH_h__ # define ____SMTP_AUTH_h__
# include "SMTPCommand.h" # include "Command.h"
# include "SMTPSession.h"
namespace mail { namespace mail {
class SMTPServer; class SMTPServer;
class __SMTP_AUTH : public SMTPCommand { class __SMTP_AUTH : public core::Command {
public: public:
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data); int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
bool authLogin(coreutils::ZString userName, coreutils::ZString password, SMTPServer &server);
bool authLogin(std::string userName, std::string password, SMTPServer &server);
}; };

View File

@ -1,65 +1,71 @@
#include "__SMTP_DATA.h" #include "__SMTP_DATA.h"
#include "SMTPSession.h"
#include "SMTPServer.h" #include "SMTPServer.h"
#include "Log.h" #include "Log.h"
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#define CRLF "\r\n"
namespace mail { namespace mail {
int __SMTP_DATA::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) { int __SMTP_DATA::processCommand(coreutils::ZString &request, core::TCPSession &session) {
switch(session.mode) { SMTPSession &s = dynamic_cast<SMTPSession &>(session);
SMTPServer &server = session.server;
case WAIT_FOR_DATA: switch(session.mode) {
switch(session.state) { case WAIT_FOR_DATA:
case RCPT: switch(session.state) {
data << "354 Enter the mail message terminated by <CRLF>.<CRLF>" << CRLF;
session.mailData.str(""); case RCPT:
session.mode = RECEIVE_DATA; s.out << "354 Enter the mail message terminated by <CRLF>.<CRLF>" << CRLF;
session.server.commands.grabInput(&session, *this); s.mailData.str("");
s.mode = RECEIVE_DATA;
server.commands.grabInput(&session, *this);
// setTimer(120.0f); // setTimer(120.0f);
break; break;
case MAIL: case MAIL:
data << "503 Please use RCPT first" << CRLF; s.out << "503 Please use RCPT first" << CRLF;
break; break;
default: default:
data << "503 Please use MAIL first" << CRLF; s.out << "503 Please use MAIL first" << CRLF;
break; break;
} }
break; break;
case RECEIVE_DATA: case RECEIVE_DATA:
if(request.str() != ".") if(request.str() != ".")
session.mailData << request.str() << CRLF; s.mailData << request.str() << CRLF;
else { else {
session.mode = WAIT_FOR_DATA; s.mode = WAIT_FOR_DATA;
session.state = READY; s.state = READY;
session.server.commands.clearGrab(&session); server.commands.clearGrab(&session);
// if(filterMessage(session.mailData)) { // if(filterMessage(session.mailData)) {
if(session.recipientList.size() > 0) { if(s.recipientList.size() > 0) {
if(session.mailData.str().size() > 0) { if(s.mailData.str().size() > 0) {
std::string ID = queueMail(server, session.sender, session.recipientList, session.mailData); std::string ID = queueMail(server, session.sender, session.recipientList, session.mailData);
if(ID != "") { if(ID != "") {
coreutils::Log(coreutils::LOG_INFO) << "Queued message " << ID << "."; coreutils::Log(coreutils::LOG_INFO) << "Queued message " << ID << ".";
data << "250 OK Queued message " << ID << CRLF; s.out << "250 OK Queued message " << ID << CRLF;
} }
else else
data << "550 Mail message too big" << CRLF; s.out << "550 Mail message too big" << CRLF;
} else } else
data << "550 Mail message was empty" << CRLF; s.out << "550 Mail message was empty" << CRLF;
} else } else
data << "250 OK Queued message " << CRLF; s.out << "250 OK Queued message " << CRLF;
// } else // } else
// data << "550 Message is probably spam" << CRLF; // data << "550 Message is probably spam" << CRLF;
} }
break; break;
} }
return 1; return 1;
} }
std::string __SMTP_DATA::generateMailFileName() { std::string __SMTP_DATA::generateMailFileName() {
@ -76,8 +82,8 @@ namespace mail {
mailFile.write(mailData.str()); mailFile.write(mailData.str());
for(std::string recipient: recipientList) { for(std::string recipient: recipientList) {
std::string newName = fileName + recipient; std::string newName = fileName + recipient;
link(fileName.c_str(), newName.c_str()); link(fileName.c_str(), newName.c_str());
} }
unlink(fileName.c_str()); unlink(fileName.c_str());

View File

@ -11,7 +11,7 @@ namespace mail {
class __SMTP_DATA : public SMTPCommand { class __SMTP_DATA : public SMTPCommand {
public: public:
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data); int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
std::string generateMailFileName(); std::string generateMailFileName();
std::string queueMail(SMTPServer &server, std::string sender, std::vector<std::string> recipientList, std::stringstream &mailData); std::string queueMail(SMTPServer &server, std::string sender, std::vector<std::string> recipientList, std::stringstream &mailData);
@ -21,4 +21,3 @@ namespace mail {
} }
#endif #endif

View File

@ -1,19 +1,23 @@
#include "__SMTP_EHLO.h" #include "__SMTP_EHLO.h"
#include "SMTPServer.h" #include "SMTPSession.h"
#define CRLF "\r\n"
namespace mail { namespace mail {
int __SMTP_EHLO::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) { int __SMTP_EHLO::processCommand(coreutils::ZString &request, core::TCPSession &session) {
session.clientDomainName = request[1].str(); SMTPSession &s = dynamic_cast<SMTPSession &>(session);
data << "250-" << server.hostName << CRLF; s.clientDomainName = request[1].str();
s.out << "250-" << server.hostName << CRLF;
// cout << "250-STARTTLS" << CRLF; // cout << "250-STARTTLS" << CRLF;
// cout << "250-PIPELINING" << CRLF; // cout << "250-PIPELINING" << CRLF;
// cout << "250-8BITMIME" << CRLF; // cout << "250-8BITMIME" << CRLF;
data << "250-AUTH LOGIN" << CRLF; s.out << "250-AUTH LOGIN" << CRLF;
data << "250 HELP" << CRLF; s.out << "250 HELP" << CRLF;
session.state = READY; s.state = READY;
return 1; return 1;
} }

View File

@ -11,7 +11,7 @@ namespace mail {
class __SMTP_EHLO : public SMTPCommand { class __SMTP_EHLO : public SMTPCommand {
public: public:
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data); int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
}; };

View File

@ -1,11 +1,14 @@
#include "__SMTP_EXPN.h" #include "__SMTP_EXPN.h"
#include "SMTPServer.h" #include "SMTPServer.h"
#define CRLF "\r\n"
namespace mail { namespace mail {
int __SMTP_EXPN::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) { int __SMTP_EXPN::processCommand(coreutils::ZString &request, core::TCPSession &session) {
data << "252 You must know recipient." << CRLF; SMTPSession &s = dynamic_cast<SMTPSession &>(session);
session.state = READY; s.out << "252 You must know recipient." << CRLF;
s.state = READY;
return 1; return 1;
} }

View File

@ -11,8 +11,7 @@ namespace mail {
class __SMTP_EXPN : public SMTPCommand { class __SMTP_EXPN : public SMTPCommand {
public: public:
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data); int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
}; };
} }

View File

@ -3,12 +3,11 @@
namespace mail { namespace mail {
int __SMTP_HELO::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) { int __SMTP_HELO::processCommand(coreutils::ZString &request, core::TCPSession &session) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
std::string clientName = request[1].str(); s.clientName = request[1].str();
s.out << "250 " << server.hostName << CRLF;
data << "250 " << server.hostName << CRLF; s.state = READY;
session.state = READY;
return 1; return 1;
} }

View File

@ -1,18 +1,17 @@
#ifndef ____SMTP_HELO_h__ #ifndef ____SMTP_HELO_h__
# define ____SMTP_HELO_h__ # define ____SMTP_HELO_h__
# include "SMTPCommand.h" # include "Command.h"
# include "SMTPSession.h" # include "SMTPSession.h"
namespace mail { namespace mail {
class SMTPServer; class SMTPServer;
class __SMTP_HELO : public SMTPCommand { class __SMTP_HELO : public core::Command {
public: public:
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data); int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
}; };
} }

View File

@ -1,11 +1,13 @@
#include "__SMTP_HELP.h" #include "__SMTP_HELP.h"
#include "SMTPServer.h" #include "SMTPServer.h"
#define CRLF "\r\n"
namespace mail { namespace mail {
int __SMTP_HELP::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) { int __SMTP_HELP::processCommand(coreutils::ZString &request, core::TCPSession &session) {
data << "250 Sure you need help." << CRLF; s.out << "250 Sure you need help." << CRLF;
session.state = READY; s.state = READY;
return 1; return 1;
} }

View File

@ -6,12 +6,8 @@
namespace mail { namespace mail {
class SMTPServer;
class __SMTP_HELP : public SMTPCommand { class __SMTP_HELP : public SMTPCommand {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data);
}; };
} }

View File

@ -1,29 +1,32 @@
#include "__SMTP_MAIL.h" #include "__SMTP_MAIL.h"
#include "SMTPServer.h" #include "SMTPSession.h"
#define CRLF "\r\n"
namespace mail { namespace mail {
int __SMTP_MAIL::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) { int __SMTP_MAIL::processCommand(coreutils::ZString &request, core::TCPSession &session) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
if(request.ifNext("MAIL FROM:")) { if(request.ifNext("MAIL FROM:")) {
request.skipWhitespace(); request.skipWhitespace();
if(request.ifNext("<")) { if(request.ifNext("<")) {
session.sender = request.getTokenExclude(">"); s.sender = request.getTokenExclude(">");
if(session.authState = USER_KNOWN) { if(s.authState = USER_KNOWN) {
data << "250 OK" << CRLF; s.out << "250 OK" << CRLF;
session.recipientList.clear(); s.recipientList.clear();
session.state = MAIL; s.state = MAIL;
} }
} else } else
data << "550 Usage: MAIL FROM:<email-address>" << CRLF; s.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
} else } else
data << "550 Usage: MAIL FROM:<email-address>" << CRLF; s.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
return 1; return 1;
} }
std::string domainOnly(std::string email) { std::string domainOnly(std::string email) {
coreutils::PString split(email); coreutils::ZString split(email);
split.split("@"); split.split("@");
return split[1].str(); return split[1].str();
} }

View File

@ -8,12 +8,10 @@ namespace mail {
class SMTPServer; class SMTPServer;
class __SMTP_MAIL : public SMTPCommand { class __SMTP_MAIL : public Command {
public: public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data);
}; };
} }

View File

@ -1,21 +1,17 @@
#ifndef ____SMTP_NOOP_h__ #ifndef ____SMTP_NOOP_h__
# define ____SMTP_NOOP_h__ # define ____SMTP_NOOP_h__
# include "SMTPCommand.h"
# include "SMTPSession.h" # include "SMTPSession.h"
# include "PString.h" # include "__SMTP_NOOP.h"
# include "ZString.h"
namespace mail { namespace mail {
class __SMTP_NOOP : public SMTPCommand { int __SMTP_NOOP::processCommand(coreutils::ZString &request, core::TCPSession &session) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
int processCommand(coreutils::PString request, SMTPSession &session, std::stringstream &data) { s.out << "250 OK" << CRLF;
data << "250 OK" << CRLF; return 1;
return 1;
} }
};
} }
#endif #endif

View File

@ -1,19 +1,13 @@
#ifndef ____SMTP_NOOP_h__ #ifndef ____SMTP_NOOP_h__
# define ____SMTP_NOOP_h__ # define ____SMTP_NOOP_h__
# include "SMTPCommand.h"
# include "SMTPSession.h" # include "SMTPSession.h"
# include "PString.h" # include "ZString.h"
namespace mail { namespace mail {
class __SMTP_NOOP : public SMTPCommand { class __SMTP_NOOP : public Command {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::PString request, SMTPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
return 1;
}
}; };
} }

View File

@ -8,12 +8,10 @@ namespace mail {
class SMTPServer; class SMTPServer;
class __SMTP_QUIT : public SMTPCommand { class __SMTP_QUIT : public Command {
public: public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data);
}; };
} }

View File

@ -1,19 +1,15 @@
#ifndef ____SMTP_RCPT_h__ #ifndef ____SMTP_RCPT_h__
# define ____SMTP_RCPT_h__ # define ____SMTP_RCPT_h__
# include "SMTPCommand.h" # include "Command.h"
# include "MailFileSystem.h" # include "TCPSession.h"
# include "SMTPSession.h"
namespace mail { namespace mail {
class SMTPServer; class __SMTP_RCPT : public core::Command {
class __SMTP_RCPT : public SMTPCommand {
public: public:
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data); int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
}; };
} }

View File

@ -1,18 +1,14 @@
#ifndef ____SMTP_RSET_h__ #ifndef ____SMTP_RSET_h__
#define ____SMTP_RSET_h__ #define ____SMTP_RSET_h__
#include "SMTPCommand.h" #include "Command.h"
#include "TCPSession.h"
#include "ZString.h"
namespace mail { namespace mail {
class __SMTP_RSET : public SMTPCommand { class __SMTP_RSET : public core::Command {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::PString request, SMTPSession &session, std::stringstream &data) {
session.state = READY;
data << "250 OK" << CRLF;
return 0;
}
}; };
} }

View File

@ -1,10 +1,11 @@
#include "__SMTP_VRFY.h" #include "__SMTP_VRFY.h"
#define CRLF "\r\n"
namespace mail { namespace mail {
int __SMTP_VRFY::processCommand(coreutils::PString request, SMTPSession &session, std::stringstream &data) { int __SMTP_VRFY::processCommand(coreutils::ZString &request, core::TCPSession &session) {
data << "252 You must know who the mail is for" << CRLF; session.out << "252 You must know who the mail is for" << CRLF;
return 0; return 1;
} }
} }

View File

@ -1,15 +1,14 @@
#ifndef ____SMTP_VRFY_h__ #ifndef ____SMTP_VRFY_h__
# define ____SMTP_VRFY_h__ # define ____SMTP_VRFY_h__
# include "SMTPCommand.h" # include "Command.h"
# include "PString.h" # include "TCPSession.h"
# include "ZString.h"
namespace mail { namespace mail {
class __SMTP_VRFY : public SMTPCommand { class __SMTP_VRFY : public core::Command {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::PString request, SMTPSession &session, std::stringstream &data);
}; };
} }