X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/f5e4a84330b742c6ad8106f98998ba165671326d..c0b247a2a37ca279bc0b0ad1c31470846b97fcbc:/projects/stargazer/plugins/configuration/sgconfig/conn.cpp?ds=inline diff --git a/projects/stargazer/plugins/configuration/sgconfig/conn.cpp b/projects/stargazer/plugins/configuration/sgconfig/conn.cpp index 718e7c56..8bba49be 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/conn.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/conn.cpp @@ -20,26 +20,16 @@ #include "conn.h" -#include "parser.h" - -#include "parser_server_info.h" -#include "parser_admins.h" -#include "parser_tariffs.h" -#include "parser_users.h" -#include "parser_message.h" -#include "parser_auth_by.h" -#include "parser_user_info.h" - -#include "stg/settings.h" #include "stg/admins.h" -#include "stg/users.h" -#include "stg/tariffs.h" #include "stg/admin.h" +#include "stg/logger.h" #include "stg/blowfish.h" #include "stg/bfstream.h" #include "stg/common.h" #include +#include +#include #include #include @@ -54,15 +44,11 @@ const char Conn::ERR_LOGIN[] = "ERLG"; const char Conn::OK_LOGINS[] = "OKLS"; const char Conn::ERR_LOGINS[] = "ERLS"; -Conn::Conn(const SETTINGS & settings, - ADMINS & admins, - USERS & users, - TARIFFS & tariffs, - int sock, const sockaddr_in& addr) - : m_settings(settings), +Conn::Conn(const BASE_PARSER::REGISTRY & registry, + ADMINS & admins, int sock, const sockaddr_in& addr, + PLUGIN_LOGGER & logger) + : m_registry(registry), m_admins(admins), - m_users(users), - m_tariffs(tariffs), m_admin(NULL), m_sock(sock), m_addr(addr), @@ -73,6 +59,7 @@ Conn::Conn(const SETTINGS & settings, m_buffer(m_header), m_bufferSize(sizeof(m_header)), m_stream(NULL), + m_logger(logger), m_dataState(false, *this) { if (m_xmlParser == NULL) @@ -81,28 +68,6 @@ Conn::Conn(const SETTINGS & settings, XML_ParserReset(m_xmlParser, NULL); XML_SetElementHandler(m_xmlParser, ParseXMLStart, ParseXMLEnd); XML_SetUserData(m_xmlParser, this); - - /*m_parsers.push_back(new STG::PARSER::GET_SERVER_INFO(m_settings, m_users, m_tariffs)); - - m_parsers.push_back(new PARSER_GET_USERS); - m_parsers.push_back(new PARSER_GET_USER); - m_parsers.push_back(new PARSER_CHG_USER); - m_parsers.push_back(new PARSER_ADD_USER); - m_parsers.push_back(new PARSER_DEL_USER); - m_parsers.push_back(new PARSER_CHECK_USER); - m_parsers.push_back(new PARSER_SEND_MESSAGE); - m_parsers.push_back(new PARSER_AUTH_BY); - m_parsers.push_back(new PARSER_USER_INFO); - - m_parsers.push_back(new PARSER_GET_TARIFFS); - m_parsers.push_back(new PARSER_ADD_TARIFF); - m_parsers.push_back(new PARSER_DEL_TARIFF); - m_parsers.push_back(new PARSER_CHG_TARIFF); - - m_parsers.push_back(new PARSER_GET_ADMINS); - m_parsers.push_back(new PARSER_CHG_ADMIN); - m_parsers.push_back(new PARSER_DEL_ADMIN); - m_parsers.push_back(new PARSER_ADD_ADMIN);*/ } Conn::~Conn() @@ -110,9 +75,6 @@ Conn::~Conn() shutdown(m_sock, SHUT_RDWR); close(m_sock); - /*std::map::iterator it(m_parsers.begin()); - for (; it != m_parsers.end(); ++it) - delete it->second;*/ XML_ParserFree(m_xmlParser); } @@ -121,26 +83,38 @@ bool Conn::Read() ssize_t res = read(m_sock, m_buffer, m_bufferSize); if (res < 0) { - // TODO: log it + m_state = ERROR; + Log(__FILE__, "Failed to read data from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Reason: '" + strerror(errno) + "'"); + return false; + } + if (res == 0 && m_state != DATA) // EOF is ok for data. + { + m_state = ERROR; + Log(__FILE__, "Failed to read data from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Unexpected EOF."); return false; } m_bufferSize -= res; return HandleBuffer(res); } -BASE_PARSER * Conn::GetParser(const std::string & tag) +bool Conn::WriteAnswer(const void* buffer, size_t size) +{ + ssize_t res = write(m_sock, buffer, size); + if (res < 0) + { + m_state = ERROR; + Log(__FILE__, "Failed to write data to " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Reason: '" + strerror(errno) + "'."); + return false; + } + return true; +} + +BASE_PARSER * Conn::GetParser(const std::string & tag) const { - if (strcasecmp(tag.c_str(), "getserverinfo") == 0) - return new STG::PARSER::GET_SERVER_INFO(*m_admin, m_settings, m_users, m_tariffs); - if (strcasecmp(tag.c_str(), "getadmins") == 0) - return new STG::PARSER::GET_ADMINS(*m_admin, m_admins); - if (strcasecmp(tag.c_str(), "addadmin") == 0) - return new STG::PARSER::ADD_ADMIN(*m_admin, m_admins); - if (strcasecmp(tag.c_str(), "deladmin") == 0) - return new STG::PARSER::DEL_ADMIN(*m_admin, m_admins); - if (strcasecmp(tag.c_str(), "chgadmin") == 0) - return new STG::PARSER::CHG_ADMIN(*m_admin, m_admins); - return NULL; + BASE_PARSER::REGISTRY::const_iterator it = m_registry.find(ToLower(tag)); + if (it == m_registry.end()) + return NULL; + return it->second->create(*m_admin); } bool Conn::HandleBuffer(size_t size) @@ -166,21 +140,24 @@ bool Conn::HandleHeader() { if (strncmp(m_header, STG_HEADER, sizeof(m_header)) != 0) { - // TODO: log it + Log(__FILE__, "Received invalid header from " + inet_ntostring(IP()) + ":" + x2str(Port()) + "."); + WriteAnswer(ERR_HEADER, sizeof(ERR_HEADER) - 1); // Without \0 m_state = ERROR; return false; } m_state = LOGIN; m_buffer = m_login; m_bufferSize = sizeof(m_login); - return true; + return WriteAnswer(OK_HEADER, sizeof(OK_HEADER) - 1); // Without \0 } bool Conn::HandleLogin() { if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error. { - // TODO: log it + std::string login(m_login, strnlen(m_login, sizeof(m_login))); + Log(__FILE__, "Received invalid login '" + ToPrintable(login) + "' from " + inet_ntostring(IP()) + ":" + x2str(Port()) + "."); + WriteAnswer(ERR_LOGIN, sizeof(ERR_LOGIN) - 1); // Without \0 m_state = ERROR; return false; } @@ -188,7 +165,7 @@ bool Conn::HandleLogin() m_state = CRYPTO_LOGIN; m_buffer = m_cryptoLogin; m_bufferSize = sizeof(m_cryptoLogin); - return true; + return WriteAnswer(OK_LOGIN, sizeof(OK_LOGIN) - 1); // Without \0 } bool Conn::HandleCryptoLogin() @@ -200,7 +177,8 @@ bool Conn::HandleCryptoLogin() if (strncmp(m_login, login, sizeof(login)) != 0) { - // TODO: log it + Log(__FILE__, "Attempt to connect with wrong password from " + m_admin->GetLogin() + "@" + inet_ntostring(IP()) + ":" + x2str(Port()) + "."); + WriteAnswer(ERR_LOGINS, sizeof(ERR_LOGINS) - 1); // Without \0 m_state = ERROR; return false; } @@ -209,7 +187,7 @@ bool Conn::HandleCryptoLogin() m_buffer = m_data; m_bufferSize = sizeof(m_data); m_stream = new STG::DECRYPT_STREAM(m_admin->GetPassword(), DataCallback, &m_dataState); - return true; + return WriteAnswer(OK_LOGINS, sizeof(OK_LOGINS) - 1); // Without \0 } bool Conn::HandleData(size_t size) @@ -223,18 +201,34 @@ bool Conn::DataCallback(const void * block, size_t size, void * data) assert(data != NULL); DataState& state = *static_cast(data); - if (XML_Parse(state.conn.m_xmlParser, - static_cast(block), - size, state.final) == XML_STATUS_ERROR) + const char * xml = static_cast(block); + size_t length = strnlen(xml, size); + + state.final = state.final || length < size || size == 0; + + if (XML_Parse(state.conn.m_xmlParser, xml, length, state.final) == XML_STATUS_ERROR) { - // TODO: log it - printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d", + state.conn.Log(__FILE__, "Received invalid XML from " + state.conn.m_admin->GetLogin() + "@" + inet_ntostring(state.conn.IP()) + ":" + x2str(state.conn.Port()) + "."); + printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d\n", static_cast(XML_GetCurrentLineNumber(state.conn.m_xmlParser)), static_cast(XML_GetCurrentColumnNumber(state.conn.m_xmlParser)), XML_ErrorString(XML_GetErrorCode(state.conn.m_xmlParser)), (int)state.final); + printfd(__FILE__, "Data block: '%s' of size %d\n", xml, length); + state.conn.m_state = ERROR; return false; } + if (state.final) + { + if (!state.conn.WriteResponse()) + { + state.conn.Log(__FILE__, "Failed to write response to " + state.conn.m_admin->GetLogin() + "@" + inet_ntostring(state.conn.IP()) + ":" + x2str(state.conn.Port()) + "."); + state.conn.m_state = ERROR; + return false; + } + state.conn.m_state = DONE; + } + return true; } @@ -248,7 +242,7 @@ void Conn::ParseXMLStart(void * data, const char * el, const char ** attr) if (conn.m_parser == NULL) { - // TODO: log it + conn.Log(__FILE__, "Received unknown command '" + std::string(el) + "' from " + conn.m_admin->GetLogin() + "@" + inet_ntostring(conn.IP()) + ":" + x2str(conn.Port()) + "."); conn.m_state = ERROR; return; } @@ -263,10 +257,36 @@ void Conn::ParseXMLEnd(void * data, const char * el) if (conn.m_parser == NULL) { - // TODO: log it + // No need to log it. conn.m_state = ERROR; return; } conn.m_parser->End(data, el); } + +bool Conn::WriteResponse() +{ + STG::ENCRYPT_STREAM stream(m_admin->GetPassword(), WriteCallback, this); + std::string answer; + if (m_parser != NULL) + answer = m_parser->GetAnswer(); + else + answer = ""; + printfd(__FILE__, "Writing %d bytes of answer.\n", answer.length()); + stream.Put(answer.c_str(), answer.length() + 1 /* including \0 */, true /* final */); + return stream.IsOk(); +} + +bool Conn::WriteCallback(const void * block, size_t size, void * data) +{ + assert(data != NULL); + Conn & conn = *static_cast(data); + return WriteAll(conn.m_sock, block, size);; +} + +void Conn::Log(const char * file, const std::string & message) +{ + printfd(file, "%s\n", message.c_str()); + m_logger(message); +}