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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,20 +5,19 @@
# include "SMTPSession.h"
namespace mail {
class SMTPServer;
class __SMTP_DATA : public SMTPCommand {
class __SMTP_DATA : public SMTPCommand {
public:
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data);
std::string generateMailFileName();
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
std::string generateMailFileName();
std::string queueMail(SMTPServer &server, std::string sender, std::vector<std::string> recipientList, std::stringstream &mailData);
};
}
#endif

View File

@ -1,19 +1,23 @@
#include "__SMTP_EHLO.h"
#include "SMTPServer.h"
#include "SMTPSession.h"
#define CRLF "\r\n"
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) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
s.clientDomainName = request[1].str();
session.clientDomainName = request[1].str();
data << "250-" << server.hostName << CRLF;
s.out << "250-" << server.hostName << CRLF;
// cout << "250-STARTTLS" << CRLF;
// cout << "250-PIPELINING" << CRLF;
// cout << "250-8BITMIME" << CRLF;
data << "250-AUTH LOGIN" << CRLF;
data << "250 HELP" << CRLF;
session.state = READY;
s.out << "250-AUTH LOGIN" << CRLF;
s.out << "250 HELP" << CRLF;
s.state = READY;
return 1;
}

View File

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

View File

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

View File

@ -1,18 +1,17 @@
#ifndef ____SMTP_HELO_h__
# define ____SMTP_HELO_h__
# include "SMTPCommand.h"
# include "Command.h"
# include "SMTPSession.h"
namespace mail {
class SMTPServer;
class __SMTP_HELO : public SMTPCommand {
class __SMTP_HELO : public core::Command {
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 "SMTPServer.h"
#define CRLF "\r\n"
namespace mail {
int __SMTP_HELP::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) {
data << "250 Sure you need help." << CRLF;
session.state = READY;
int __SMTP_HELP::processCommand(coreutils::ZString &request, core::TCPSession &session) {
s.out << "250 Sure you need help." << CRLF;
s.state = READY;
return 1;
}

View File

@ -5,13 +5,9 @@
# include "SMTPSession.h"
namespace mail {
class SMTPServer;
class __SMTP_HELP : public SMTPCommand {
int processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data);
class __SMTP_HELP : public SMTPCommand {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
};
}

View File

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

View File

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

View File

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

View File

@ -8,12 +8,10 @@ namespace mail {
class SMTPServer;
class __SMTP_QUIT : public SMTPCommand {
class __SMTP_QUIT : public Command {
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,19 +1,15 @@
#ifndef ____SMTP_RCPT_h__
# define ____SMTP_RCPT_h__
# include "SMTPCommand.h"
# include "MailFileSystem.h"
# include "SMTPSession.h"
# include "Command.h"
# include "TCPSession.h"
namespace mail {
class SMTPServer;
class __SMTP_RCPT : public SMTPCommand {
class __SMTP_RCPT : public core::Command {
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,20 +1,16 @@
#ifndef ____SMTP_RSET_h__
#define ____SMTP_RSET_h__
#include "SMTPCommand.h"
#include "Command.h"
#include "TCPSession.h"
#include "ZString.h"
namespace mail {
class __SMTP_RSET : public SMTPCommand {
int processCommand(coreutils::PString request, SMTPSession &session, std::stringstream &data) {
session.state = READY;
data << "250 OK" << CRLF;
return 0;
}
class __SMTP_RSET : public core::Command {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
};
}
#endif

View File

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

View File

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