From: Maxim Mamontov Date: Wed, 17 Sep 2014 06:36:51 +0000 (+0300) Subject: Added client connection class into sgconfig. X-Git-Tag: 2.409~270 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/f5e4a84330b742c6ad8106f98998ba165671326d Added client connection class into sgconfig. --- diff --git a/projects/stargazer/plugins/configuration/sgconfig/conn.cpp b/projects/stargazer/plugins/configuration/sgconfig/conn.cpp new file mode 100644 index 00000000..718e7c56 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/conn.cpp @@ -0,0 +1,272 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#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/blowfish.h" +#include "stg/bfstream.h" +#include "stg/common.h" + +#include + +#include +#include + +using STG::Conn; + +const char Conn::STG_HEADER[] = "SG04"; +const char Conn::OK_HEADER[] = "OKHD"; +const char Conn::ERR_HEADER[] = "ERHD"; +const char Conn::OK_LOGIN[] = "OKLG"; +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), + m_admins(admins), + m_users(users), + m_tariffs(tariffs), + m_admin(NULL), + m_sock(sock), + m_addr(addr), + m_keepAlive(false), + m_parser(NULL), + m_xmlParser(XML_ParserCreate(NULL)), + m_state(HEADER), + m_buffer(m_header), + m_bufferSize(sizeof(m_header)), + m_stream(NULL), + m_dataState(false, *this) +{ + if (m_xmlParser == NULL) + throw Error("Failed to create XML parser."); + + 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() +{ + 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); +} + +bool Conn::Read() +{ + ssize_t res = read(m_sock, m_buffer, m_bufferSize); + if (res < 0) + { + // TODO: log it + return false; + } + m_bufferSize -= res; + return HandleBuffer(res); +} + +BASE_PARSER * Conn::GetParser(const std::string & tag) +{ + 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; +} + +bool Conn::HandleBuffer(size_t size) +{ + if (m_state == DATA) + return HandleData(size); + + if (m_bufferSize > 0) + return true; + + switch (m_state) + { + case HEADER: return HandleHeader(); + case LOGIN: return HandleLogin(); + case CRYPTO_LOGIN: return HandleCryptoLogin(); + default: return true; + } + + return true; +} + +bool Conn::HandleHeader() +{ + if (strncmp(m_header, STG_HEADER, sizeof(m_header)) != 0) + { + // TODO: log it + m_state = ERROR; + return false; + } + m_state = LOGIN; + m_buffer = m_login; + m_bufferSize = sizeof(m_login); + return true; +} + +bool Conn::HandleLogin() +{ + if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error. + { + // TODO: log it + m_state = ERROR; + return false; + } + m_admin->SetIP(IP()); + m_state = CRYPTO_LOGIN; + m_buffer = m_cryptoLogin; + m_bufferSize = sizeof(m_cryptoLogin); + return true; +} + +bool Conn::HandleCryptoLogin() +{ + char login[ADM_LOGIN_LEN + 1]; + BLOWFISH_CTX ctx; + InitContext(m_admin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx); + DecryptString(login, m_cryptoLogin, ADM_LOGIN_LEN, &ctx); + + if (strncmp(m_login, login, sizeof(login)) != 0) + { + // TODO: log it + m_state = ERROR; + return false; + } + + m_state = DATA; + m_buffer = m_data; + m_bufferSize = sizeof(m_data); + m_stream = new STG::DECRYPT_STREAM(m_admin->GetPassword(), DataCallback, &m_dataState); + return true; +} + +bool Conn::HandleData(size_t size) +{ + m_stream->Put(m_buffer, size, size == 0 || memchr(m_buffer, 0, size) != NULL); + return m_stream->IsOk(); +} + +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) + { + // TODO: log it + printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d", + 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); + return false; + } + + return true; +} + +void Conn::ParseXMLStart(void * data, const char * el, const char ** attr) +{ + assert(data != NULL); + Conn & conn = *static_cast(data); + + if (conn.m_parser == NULL) + conn.m_parser = conn.GetParser(el); + + if (conn.m_parser == NULL) + { + // TODO: log it + conn.m_state = ERROR; + return; + } + + conn.m_parser->Start(data, el, attr); +} + +void Conn::ParseXMLEnd(void * data, const char * el) +{ + assert(data != NULL); + Conn & conn = *static_cast(data); + + if (conn.m_parser == NULL) + { + // TODO: log it + conn.m_state = ERROR; + return; + } + + conn.m_parser->End(data, el); +} diff --git a/projects/stargazer/plugins/configuration/sgconfig/conn.h b/projects/stargazer/plugins/configuration/sgconfig/conn.h new file mode 100644 index 00000000..39a59754 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/conn.h @@ -0,0 +1,129 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONFIG_CONN_H__ +#define __STG_SGCONFIG_CONN_H__ + +#include "stg/os_int.h" +#include "stg/const.h" + +#include +#include +#include + +#include + +#include + +class SETTINGS; +class ADMINS; +class USERS; +class TARIFFS; +class ADMIN; +class BASE_PARSER; + +namespace STG +{ + +class DECRYPT_STREAM; + +class Conn +{ + public: + struct Error : public std::runtime_error + { + Error(const std::string& message) : runtime_error(message.c_str()) {} + }; + + Conn(const SETTINGS & settings, + ADMINS & admins, + USERS & users, + TARIFFS & tariffs, + int sock, const sockaddr_in& addr); + ~Conn(); + + int Sock() const { return m_sock; } + uint32_t IP() const { return *(uint32_t *)(&m_addr.sin_addr); } + uint16_t Port() const { return m_addr.sin_port; } + + bool Read(); + + bool IsOk() const { return m_state != ERROR; } + bool IsKeepAlive() const { return m_keepAlive; } + + private: + + static const char STG_HEADER[5]; + static const char OK_HEADER[5]; + static const char ERR_HEADER[5]; + static const char OK_LOGIN[5]; + static const char ERR_LOGIN[5]; + static const char OK_LOGINS[5]; + static const char ERR_LOGINS[5]; + + const SETTINGS & m_settings; + ADMINS & m_admins; + USERS & m_users; + TARIFFS & m_tariffs; + + ADMIN * m_admin; + + int m_sock; + sockaddr_in m_addr; + bool m_keepAlive; + + BASE_PARSER * m_parser; + + XML_Parser m_xmlParser; + + enum { HEADER, LOGIN, CRYPTO_LOGIN, DATA, ERROR } m_state; + + void * m_buffer; + size_t m_bufferSize; + char m_header[sizeof(STG_HEADER)]; + char m_login[ADM_LOGIN_LEN + 1]; + char m_cryptoLogin[ADM_LOGIN_LEN + 1]; + char m_data[1024]; + STG::DECRYPT_STREAM * m_stream; + + BASE_PARSER * GetParser(const std::string & tag); + + bool HandleBuffer(size_t size); + + bool HandleHeader(); + bool HandleLogin(); + bool HandleCryptoLogin(); + bool HandleData(size_t size); + + struct DataState + { + DataState(bool f, Conn & c) : final(f), conn(c) {} + bool final; + Conn & conn; + } m_dataState; + + static bool DataCallback(const void * block, size_t size, void * data); + static void ParseXMLStart(void * data, const char * el, const char ** attr); + static void ParseXMLEnd(void * data, const char * el); +}; + +} + +#endif