From 50eb707f1016eb5e686add17d03477075b163d99 Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Fri, 13 Sep 2019 10:34:10 -0700 Subject: [PATCH] More development. --- BMAIMAPServer.h | 25 ------------------------ BMAIMAPSession.h | 34 -------------------------------- BMAMail.project | 4 ++++ BMAPOP3Server.h | 25 ------------------------ BMAPOP3Session.h | 34 -------------------------------- BMASMTPServer.h | 31 ----------------------------- BMASMTPSession.h | 33 ------------------------------- IMAPService.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ POP3Service.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ SMTPService.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ SMTPSession.h | 17 ++++++++++++++++ __SMTP_AUTH.h | 2 +- __SMTP_DATA.h | 13 ++++++++++++ __SMTP_EHLO.h | 21 ++++++++++++-------- __SMTP_HELO.h | 13 ++++++++++++ __SMTP_MAIL.h | 13 ++++++++++++ __SMTP_NOOP.h | 19 +++++------------- __SMTP_QUIT.h | 24 +++++++++-------------- __SMTP_RCPT.h | 31 +++++++++++++---------------- __SMTP_RSET.h | 2 +- __SMTP_VRFY.h | 2 +- main.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++----- 22 files changed, 302 insertions(+), 244 deletions(-) delete mode 100644 BMAIMAPServer.h delete mode 100644 BMAIMAPSession.h delete mode 100644 BMAPOP3Server.h delete mode 100644 BMAPOP3Session.h delete mode 100644 BMASMTPServer.h delete mode 100644 BMASMTPSession.h create mode 100644 IMAPService.h create mode 100644 POP3Service.h create mode 100644 SMTPService.h create mode 100644 SMTPSession.h diff --git a/BMAIMAPServer.h b/BMAIMAPServer.h deleted file mode 100644 index 8248a59..0000000 --- a/BMAIMAPServer.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef BMAIMAPServer_h__ -#define BMAIMAPServer_h__ - -#include "includes" -#include "BMATCPServerSocket.h" -#include "BMACommand.h" -class BMATCPSocket; - -class BMAIMAPServer : public BMATCPServerSocket { - - public: - BMAIMAPServer(BMAEPoll &ePoll, std::string url, short int port); - ~BMAIMAPServer(); - - BMASession * getSocketAccept(); - - void registerCommand(BMACommand &command); - - int processCommand(BMASession *session) override; /// commands; - -}; - -#endif diff --git a/BMAIMAPSession.h b/BMAIMAPSession.h deleted file mode 100644 index 2b50952..0000000 --- a/BMAIMAPSession.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef __BMAIMAPSession_h__ -#define __BMAIMAPSession_h__ - -#include "BMASession.h" -#include "BMAIMAPServer.h" - -/// -/// BMAIMAPSession -/// -/// Extends the session parameters for this BMATCPSocket derived object. -/// Extend the protocol() method in order to define the behavior and -/// protocol interaction for this socket which is an IMAP session. -/// - -class BMAIMAPSession : public BMASession { - - public: - BMAIMAPSession(BMAEPoll &ePoll, BMAConsoleServer &server); - ~BMAIMAPSession(); - - virtual void output(std::stringstream &out); - - protected: - void protocol(char *data, int length) override; - -private: - BMAConsoleServer &server; - enum Status {WELCOME, PROMPT, INPUT, PROCESS, DONE}; - Status status = WELCOME; - void doCommand(std::string request); - -}; - -#endif diff --git a/BMAMail.project b/BMAMail.project index 4b8795b..0f001c4 100644 --- a/BMAMail.project +++ b/BMAMail.project @@ -20,6 +20,10 @@ + + + + diff --git a/BMAPOP3Server.h b/BMAPOP3Server.h deleted file mode 100644 index 287393d..0000000 --- a/BMAPOP3Server.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef BMAPOP3Server_h__ -#define BMAPOP3Server_h__ - -#include "includes" -#include "BMATCPServerSocket.h" -#include "BMACommand.h" -class BMATCPSocket; - -class BMAPOP3Server : public BMATCPServerSocket { - - public: - BMAPOP3Server(BMAEPoll &ePoll, std::string url, short int port); - ~BMAPOP3Server(); - - BMASession * getSocketAccept(); - - void registerCommand(BMACommand &command); - - int processCommand(BMASession *session) override; /// commands; - -}; - -#endif diff --git a/BMAPOP3Session.h b/BMAPOP3Session.h deleted file mode 100644 index dde6a7c..0000000 --- a/BMAPOP3Session.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef __BMAPOP3Session_h__ -#define __BMAPOP3Session_h__ - -#include "BMASession.h" -#include "BMAPOP3Server.h" - -/// -/// BMAPOP3Session -/// -/// Extends the session parameters for this BMATCPSocket derived object. -/// Extend the protocol() method in order to define the behavior and -/// protocol interaction for this socket which is a console session. -/// - -class BMAPOP3Session : public BMASession { - - public: - BMAPOP3Session(BMAEPoll &ePoll, BMAPOP3Server &server); - ~BMAPop3Session(); - - virtual void output(std::stringstream &out); - - protected: - void protocol(char *data, int length) override; - -private: - BMAConsoleServer &server; - enum Status {WELCOME, PROMPT, INPUT, PROCESS, DONE}; - Status status = WELCOME; - void doCommand(std::string request); - -}; - -#endif diff --git a/BMASMTPServer.h b/BMASMTPServer.h deleted file mode 100644 index e65cc4e..0000000 --- a/BMASMTPServer.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef BMAConsoleServer_h__ -#define BMAConsoleServer_h__ - -#include "includes" -#include "BMATCPServerSocket.h" -#include "BMACommand.h" -class BMATCPSocket; - -/// -/// BMASMTPServer -/// -/// Use this object to create a fully supported SMTP server. -/// - -class BMASMTPServer : public BMATCPServerSocket { - - public: - BMASMTPServer(BMAEPoll &ePoll, std::string url, short int port); - ~BMASMTPServer(); - - BMASession * getSocketAccept() override; - - void registerCommand(BMACommand &command); - - int processCommand(BMASession *session) override; /// commands; - -}; - -#endif diff --git a/BMASMTPSession.h b/BMASMTPSession.h deleted file mode 100644 index 9095e9c..0000000 --- a/BMASMTPSession.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef __BMASMTPSession_h__ -#define __BMASMTPSession_h__ - -#include "BMASession.h" - -/// -/// BMAConsoleSession -/// -/// Extends the session parameters for this BMATCPSocket derived object. -/// Extend the protocol() method in order to define the behavior and -/// protocol interaction for this socket which is a console session. -/// - -class BMASMTPSession : public BMASession { - - public: - BMAConsoleSession(BMAEPoll &ePoll, BMAConsoleServer &server); - ~BMAConsoleSession(); - - virtual void output(std::stringstream &out); - - protected: - void protocol(char *data, int length) override; - -private: - BMAConsoleServer &server; - enum Status {WELCOME, PROMPT, INPUT, PROCESS, DONE}; - Status status = WELCOME; - void doCommand(std::string request); - -}; - -#endif diff --git a/IMAPService.h b/IMAPService.h new file mode 100644 index 0000000..d9890cf --- /dev/null +++ b/IMAPService.h @@ -0,0 +1,51 @@ +#ifndef __IMAPService_h__ +#define __IMAPService_h__ + +#include "Service.h" +#include "Exception.h" +#include "__SMTP_AUTH.h" +#include "__SMTP_DATA.h" +#include "__SMTP_EHLO.h" +#include "__SMTP_HELO.h" +#include "__SMTP_MAIL.h" +#include "__SMTP_NOOP.h" +#include "__SMTP_QUIT.h" +#include "__SMTP_RCPT.h" +#include "__SMTP_RSET.h" +#include "__SMTP_VRFY.h" + +namespace mail { + + class IMAPService : public core::Service { + + public: + IMAPService() { + commands.add(_smtp_auth, "AUTH"); + commands.add(_smtp_data, "DATA"); + commands.add(_smtp_ehlo, "EHLO"); + commands.add(_smtp_helo, "HELO"); + 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"); + } + + private: + __SMTP_AUTH _smtp_auth; + __SMTP_DATA _smtp_data; + __SMTP_EHLO _smtp_ehlo; + __SMTP_HELO _smtp_helo; + __SMTP_MAIL _smtp_mail; + __SMTP_NOOP _smtp_noop; + __SMTP_QUIT _smtp_quit; + __SMTP_RCPT _smtp_rcpt; + __SMTP_RSET _smtp_rset; + __SMTP_VRFY _smtp_vrfy; + + }; + +} + +#endif diff --git a/POP3Service.h b/POP3Service.h new file mode 100644 index 0000000..61d0593 --- /dev/null +++ b/POP3Service.h @@ -0,0 +1,51 @@ +#ifndef __POP3Service_h__ +#define __POP3Service_h__ + +#include "Service.h" +#include "Exception.h" +#include "__SMTP_AUTH.h" +#include "__SMTP_DATA.h" +#include "__SMTP_EHLO.h" +#include "__SMTP_HELO.h" +#include "__SMTP_MAIL.h" +#include "__SMTP_NOOP.h" +#include "__SMTP_QUIT.h" +#include "__SMTP_RCPT.h" +#include "__SMTP_RSET.h" +#include "__SMTP_VRFY.h" + +namespace mail { + + class POP3Service : public core::Service { + + public: + POP3Service() { + commands.add(_smtp_auth, "AUTH"); + commands.add(_smtp_data, "DATA"); + commands.add(_smtp_ehlo, "EHLO"); + commands.add(_smtp_helo, "HELO"); + 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"); + } + + private: + __SMTP_AUTH _smtp_auth; + __SMTP_DATA _smtp_data; + __SMTP_EHLO _smtp_ehlo; + __SMTP_HELO _smtp_helo; + __SMTP_MAIL _smtp_mail; + __SMTP_NOOP _smtp_noop; + __SMTP_QUIT _smtp_quit; + __SMTP_RCPT _smtp_rcpt; + __SMTP_RSET _smtp_rset; + __SMTP_VRFY _smtp_vrfy; + + }; + +} + +#endif diff --git a/SMTPService.h b/SMTPService.h new file mode 100644 index 0000000..ebfd60c --- /dev/null +++ b/SMTPService.h @@ -0,0 +1,51 @@ +#ifndef __SMTPService_h__ +#define __SMTPService_h__ + +#include "Service.h" +#include "Exception.h" +#include "__SMTP_AUTH.h" +#include "__SMTP_DATA.h" +#include "__SMTP_EHLO.h" +#include "__SMTP_HELO.h" +#include "__SMTP_MAIL.h" +#include "__SMTP_NOOP.h" +#include "__SMTP_QUIT.h" +#include "__SMTP_RCPT.h" +#include "__SMTP_RSET.h" +#include "__SMTP_VRFY.h" + +namespace mail { + + class SMTPService : public core::Service { + + public: + SMTPService() { + commands.add(_smtp_auth, "AUTH"); + commands.add(_smtp_data, "DATA"); + commands.add(_smtp_ehlo, "EHLO"); + commands.add(_smtp_helo, "HELO"); + 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"); + } + + private: + __SMTP_AUTH _smtp_auth; + __SMTP_DATA _smtp_data; + __SMTP_EHLO _smtp_ehlo; + __SMTP_HELO _smtp_helo; + __SMTP_MAIL _smtp_mail; + __SMTP_NOOP _smtp_noop; + __SMTP_QUIT _smtp_quit; + __SMTP_RCPT _smtp_rcpt; + __SMTP_RSET _smtp_rset; + __SMTP_VRFY _smtp_vrfy; + + }; + +} + +#endif diff --git a/SMTPSession.h b/SMTPSession.h new file mode 100644 index 0000000..045f8f7 --- /dev/null +++ b/SMTPSession.h @@ -0,0 +1,17 @@ +#ifndef __SMTPSession_h__ +#define __SMTPSession_h__ + +#include "includes" + +namespace mail { + + class SMTPSession : public core::Session { + + public: + std::string state; + + }; + +} + +#endif diff --git a/__SMTP_AUTH.h b/__SMTP_AUTH.h index a606280..512dd68 100644 --- a/__SMTP_AUTH.h +++ b/__SMTP_AUTH.h @@ -3,7 +3,7 @@ #include "Command.h" -namespace http { +namespace mail { class __SMTP_AUTH : public core::Command { diff --git a/__SMTP_DATA.h b/__SMTP_DATA.h index a5e9c11..ceae133 100644 --- a/__SMTP_DATA.h +++ b/__SMTP_DATA.h @@ -1,3 +1,16 @@ +#ifndef ____SMTP_DATA_h__ +#define ____SMTP_DATA_h__ + +#include "Command.h" + +namespace mail { + + class __SMTP_DATA : public core::Command { + + int processCommand(std::string request, Session *session, std::stringstream &data); + + data << "" << std::endl; + //--------------------------------------------------------------------------- // DATA command request handling. //--------------------------------------------------------------------------- diff --git a/__SMTP_EHLO.h b/__SMTP_EHLO.h index 3d51ef0..8691769 100644 --- a/__SMTP_EHLO.h +++ b/__SMTP_EHLO.h @@ -1,8 +1,13 @@ - //--------------------------------------------------------------------------- - // EHLO command request handling. - //--------------------------------------------------------------------------- +#ifndef ____SMTP_EHLO_h__ +#define ____SMTP_EHLO_h__ - else if(command(input) == "EHLO") { +#include "Command.h" + +namespace mail { + + class __SMTP_EHLO : public core::Command { + + int processCommand(std::string request, Session *session, std::stringstream &data); if(input.length() > 5) { string hostName = input.substr(5); @@ -14,12 +19,12 @@ // } - cout << "250-" << getHostName() << CRLF; + data << "250-" << getHostName() << CRLF; // cout << "250-STARTTLS" << CRLF; // cout << "250-PIPELINING" << CRLF; // cout << "250-8BITMIME" << CRLF; - cout << "250-AUTH LOGIN" << CRLF; - cout << "250 HELP" << CRLF; - state = "READY"; + data << "250-AUTH LOGIN" << CRLF; + data << "250 HELP" << CRLF; + session->state = "READY"; } \ No newline at end of file diff --git a/__SMTP_HELO.h b/__SMTP_HELO.h index 6f8dd8f..85065f2 100644 --- a/__SMTP_HELO.h +++ b/__SMTP_HELO.h @@ -1,3 +1,16 @@ +#ifndef ____SMTP_HELO_h__ +#define ____SMTP_HELO_h__ + +#include "Command.h" + +namespace mail { + + class __SMTP_HELO : public core::Command { + + int processCommand(std::string request, Session *session, std::stringstream &data); + + data << "" << std::endl; + //--------------------------------------------------------------------------- // HELO command request handling. //--------------------------------------------------------------------------- diff --git a/__SMTP_MAIL.h b/__SMTP_MAIL.h index 6cc816d..6274ac8 100644 --- a/__SMTP_MAIL.h +++ b/__SMTP_MAIL.h @@ -1,3 +1,16 @@ +#ifndef ____SMTP_MAIL_h__ +#define ____SMTP_MAIL_h__ + +#include "Command.h" + +namespace mail { + + class __SMTP_MAIL : public core::Command { + + int processCommand(std::string request, Session *session, std::stringstream &data); + + data << "" << std::endl; + //--------------------------------------------------------------------------- // MAIL command request handling. //--------------------------------------------------------------------------- diff --git a/__SMTP_NOOP.h b/__SMTP_NOOP.h index df17538..57e35bb 100644 --- a/__SMTP_NOOP.h +++ b/__SMTP_NOOP.h @@ -1,21 +1,12 @@ -#ifndef ____SMTP_AUTH_h__ -#define ____SMTP_AUTH_h__ +#ifndef ____SMTP_NOOP_h__ +#define ____SMTP_NOOP_h__ #include "Command.h" -namespace http { +namespace mail { - class __SMTP_AUTH : public core::Command { + class __SMTP_NOOP : public core::Command { int processCommand(std::string request, Session *session, std::stringstream &data); - - data << "" << std::endl; - - //--------------------------------------------------------------------------- - // NOOP command request handling. - //--------------------------------------------------------------------------- - - else if(command(input) == "NOOP") { - cout << "250 OK" << CRLF; - log.message("Response: 250 OK."); + data << "250 OK" << CRLF; } diff --git a/__SMTP_QUIT.h b/__SMTP_QUIT.h index 24c3619..b1d06de 100644 --- a/__SMTP_QUIT.h +++ b/__SMTP_QUIT.h @@ -1,23 +1,17 @@ -#ifndef ____SMTP_AUTH_h__ -#define ____SMTP_AUTH_h__ +#ifndef ____SMTP_QUIT_h__ +#define ____SMTP_QUIT_h__ #include "Command.h" -namespace http { +namespace mail { - class __SMTP_AUTH : public core::Command { + class __SMTP_QUIT : public core::Command { int processCommand(std::string request, Session *session, std::stringstream &data); + data << "221 " << getHostName() << CRLF; + session->shutdown(); + } - data << "" << std::endl; + }; - //--------------------------------------------------------------------------- - // QUIT command request handling. - //--------------------------------------------------------------------------- - - else if(command(input) == "QUIT") { - cout << "221 " << getHostName() << CRLF; - log.message("Response: 221 " + getHostName() + "."); - state = "QUIT"; - break; - } \ No newline at end of file +} diff --git a/__SMTP_RCPT.h b/__SMTP_RCPT.h index de6ae35..844e789 100644 --- a/__SMTP_RCPT.h +++ b/__SMTP_RCPT.h @@ -1,22 +1,14 @@ -#ifndef ____SMTP_AUTH_h__ -#define ____SMTP_AUTH_h__ +#ifndef ____SMTP_RCPT_h__ +#define ____SMTP_RCPT_h__ #include "Command.h" -namespace http { +namespace mail { - class __SMTP_AUTH : public core::Command { + class __SMTP_RCPT : public core::Command { int processCommand(std::string request, Session *session, std::stringstream &data); - data << "" << std::endl; - - //--------------------------------------------------------------------------- - // RCPT command request handling. - //--------------------------------------------------------------------------- - - else if(command(input) == "RCPT") { - if((state == "MAIL") || (state == "RCPT")) { bool done = false; @@ -41,8 +33,7 @@ namespace http { //----------------------------------------------- recipientList += aliasRecipients; - cout << "250 OK" << CRLF; - log.message("Response: 250 OK."); + data << "250 OK" << CRLF; state = "RCPT"; done = true; } @@ -65,8 +56,7 @@ namespace http { //------------------------------------------ recipientList += cleanEMail(recipient) + " "; - cout << "250 OK" << CRLF; - log.message("Response: 250 OK."); + data << "250 OK" << CRLF; state = "RCPT"; done = true; } @@ -162,4 +152,11 @@ namespace http { cout << "503 Please use MAIL first" << CRLF; log.message("Response: 503 Please use MAIL first."); } - } \ No newline at end of file + return 0; + } + + }; + +} + +#endif diff --git a/__SMTP_RSET.h b/__SMTP_RSET.h index 5f2a014..ae40248 100644 --- a/__SMTP_RSET.h +++ b/__SMTP_RSET.h @@ -3,7 +3,7 @@ #include "Command.h" -namespace http { +namespace mail { class __SMTP_RSET : public core::Command { diff --git a/__SMTP_VRFY.h b/__SMTP_VRFY.h index 453b670..a9d9fae 100644 --- a/__SMTP_VRFY.h +++ b/__SMTP_VRFY.h @@ -3,7 +3,7 @@ #include "Command.h" -namespace http { +namespace mail { class __SMTP_VRFY : public core::Command { diff --git a/main.cpp b/main.cpp index 82bd805..4439de9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,47 @@ -#include +#include "includes" +#include "EPoll.h" +#include "ConsoleServer.h" +#include "Exception.h" +#include "File.h" +#include "Log.h" +#include "IPAddress.h" +#include "HTTPService.h" -int main(int argc, char **argv) -{ - printf("hello world\n"); - return 0; +int main(int argc, char **argv) { + + try { + + coreutils::Log(new coreutils::File("/var/log/mail.log", O_WRONLY | O_APPEND | O_CREAT, 0644)); + coreutils::Log(coreutils::LOG_INFO) << "BMAMail Server starting. Build " << __DATE__ << " " << __TIME__; + + std::string ipAddress = "0.0.0.0"; + + core::EPoll ePoll; + + mail::SMTPService smtpService; + core::TCPServerSocket smtp(ePoll, smtpService, core::IPAddress(ipAddress, 25)); + + mail::POP3Service pop3Service; + core::TCPServerSocket pop3(ePoll, pop3Service, core::IPAddress(ipAddress, 110)); + + mail::IMAPService imapService; + core::TCPServerSocket imap(ePoll, imapService, core::IPAddress(ipAddress, 143)); + + core::ConsoleService consoleService; + core::TCPServerSocket console(ePoll, consoleService, core::IPAddress(ipAddress, 1027)); + consoleService.commands.add(consoleService.commands, "help"); + ePoll.start(16, 1000); + + while(true) + sleep(300); + + ePoll.stop(); + + } + + catch(coreutils::Exception exception) { + std::cout << exception.text << " Error reason is '" << strerror(exception.errorNumber) << "' in file " << exception.file << " at line " << exception.line << std::endl; + sleep(10); + } + }