m_bufferSize(sizeof(m_header)),
m_stream(NULL),
m_logger(logger),
+#ifdef DUMPCRYPTO
+ m_dataState(false, *this),
+ m_dumper(endpoint())
+#else
m_dataState(false, *this)
+#endif
{
if (m_xmlParser == NULL)
throw Error("Failed to create XML parser.");
if (res < 0)
{
m_state = ERROR;
- Log(__FILE__, "Failed to read data from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Reason: '" + strerror(errno) + "'");
+ Log(__FILE__, "Failed to read data from " + endpoint() + ". 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.");
+ Log(__FILE__, "Failed to read data from " + endpoint() + ". Unexpected EOF.");
return false;
}
+#ifdef DUMPCRYPTO
+ m_dumper.write(m_buffer, res);
+#endif
m_bufferSize -= res;
+ m_buffer = static_cast<char*>(m_buffer) + res;
return HandleBuffer(res);
}
if (res < 0)
{
m_state = ERROR;
- Log(__FILE__, "Failed to write data to " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Reason: '" + strerror(errno) + "'.");
+ Log(__FILE__, "Failed to write data to " + endpoint() + ". Reason: '" + strerror(errno) + "'.");
return false;
}
return true;
{
if (strncmp(m_header, STG_HEADER, sizeof(m_header)) != 0)
{
- Log(__FILE__, "Received invalid header from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ".");
+ Log(__FILE__, "Received invalid header from " + endpoint() + ".");
WriteAnswer(ERR_HEADER, sizeof(ERR_HEADER) - 1); // Without \0
m_state = ERROR;
return false;
if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error.
{
std::string login(m_login, strnlen(m_login, sizeof(m_login)));
- Log(__FILE__, "Received invalid login '" + ToPrintable(login) + "' from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ".");
+ Log(__FILE__, "Received invalid login '" + ToPrintable(login) + "' from " + endpoint() + ".");
WriteAnswer(ERR_LOGIN, sizeof(ERR_LOGIN) - 1); // Without \0
m_state = ERROR;
return false;
if (strncmp(m_login, login, sizeof(login)) != 0)
{
- Log(__FILE__, "Attempt to connect with wrong password from " + m_admin->GetLogin() + "@" + inet_ntostring(IP()) + ":" + x2str(Port()) + ".");
+ Log(__FILE__, "Attempt to connect with wrong password from " + m_admin->GetLogin() + "@" + endpoint() + ".");
WriteAnswer(ERR_LOGINS, sizeof(ERR_LOGINS) - 1); // Without \0
m_state = ERROR;
return false;
bool Conn::HandleData(size_t size)
{
- m_stream->Put(m_buffer, size, size == 0 || memchr(m_buffer, 0, size) != NULL);
+ m_stream->Put(m_data, size, size == 0 || memchr(m_data, 0, size) != NULL);
+ m_buffer = m_data;
return m_stream->IsOk();
}
if (XML_Parse(state.conn.m_xmlParser, xml, length, state.final) == XML_STATUS_ERROR)
{
- state.conn.Log(__FILE__, "Received invalid XML from " + state.conn.m_admin->GetLogin() + "@" + inet_ntostring(state.conn.IP()) + ":" + x2str(state.conn.Port()) + ".");
+ state.conn.Log(__FILE__, "Received invalid XML from " + state.conn.m_admin->GetLogin() + "@" + state.conn.endpoint() + ".");
printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d\n",
static_cast<int>(XML_GetCurrentLineNumber(state.conn.m_xmlParser)),
static_cast<int>(XML_GetCurrentColumnNumber(state.conn.m_xmlParser)),
{
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.Log(__FILE__, "Failed to write response to " + state.conn.m_admin->GetLogin() + "@" + state.conn.endpoint() + ".");
state.conn.m_state = ERROR;
return false;
}
if (conn.m_parser == NULL)
{
- conn.Log(__FILE__, "Received unknown command '" + std::string(el) + "' from " + conn.m_admin->GetLogin() + "@" + inet_ntostring(conn.IP()) + ":" + x2str(conn.Port()) + ".");
+ conn.Log(__FILE__, "Received unknown command '" + std::string(el) + "' from " + conn.m_admin->GetLogin() + "@" + conn.endpoint() + ".");
conn.m_state = ERROR;
return;
}
#include "parser.h"
+#include "dumphelpers.h"
+
#include "stg/os_int.h"
#include "stg/const.h"
uint32_t IP() const { return *(uint32_t *)(&m_addr.sin_addr); }
uint16_t Port() const { return ntohs(m_addr.sin_port); }
+ std::string endpoint() const { return inet_ntostring(IP()) + ":" + x2str(Port()); }
+
bool Read();
bool IsOk() const { return m_state != ERROR; }
Conn & conn;
} m_dataState;
+#ifdef DUMPCRYPTO
+ Dumper m_dumper;
+#endif
+
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);
--- /dev/null
+#ifndef __STG_DUMP_HELPERS_H__
+#define __STG_DUMP_HELPERS_H__
+
+#include "stg/common.h"
+
+#include <string>
+#include <fstream>
+#include <sstream>
+#include <iomanip>
+
+#include <cstddef>
+#include <ctime>
+
+namespace STG
+{
+
+class Dumper
+{
+ public:
+ explicit Dumper(const std::string& tag)
+ : m_stream(getName(tag).c_str())
+ {
+ }
+ ~Dumper() {}
+
+ void write(const void* data, size_t size)
+ {
+ writePrefix();
+ m_stream << " ";
+ writeHEX(data, size);
+ }
+
+ private:
+ std::ofstream m_stream;
+
+ tm getTime() const
+ {
+ time_t now = time(NULL);
+ tm localTime;
+ localtime_r(&now, &localTime);
+ return localTime;
+ }
+
+ std::string getName(const std::string& tag) const
+ {
+ tm localTime = getTime();
+
+ std::ostringstream res;
+ res << tag
+ << "-" << (localTime.tm_year + 1900) << twoDigit(localTime.tm_mon + 1) << twoDigit(localTime.tm_mday)
+ << "-" << twoDigit(localTime.tm_hour) << twoDigit(localTime.tm_min) << twoDigit(localTime.tm_sec)
+ << ".data";
+
+ return res.str();
+ }
+
+ void writePrefix()
+ {
+ tm localTime = getTime();
+ m_stream << "[" << (localTime.tm_year + 1900) << "-" << twoDigit(localTime.tm_mon + 1) << "-" << twoDigit(localTime.tm_mday)
+ << " " << twoDigit(localTime.tm_hour) << ":" << twoDigit(localTime.tm_min) << ":" << twoDigit(localTime.tm_sec)
+ << "]";
+ }
+
+ void writeHEX(const void* data, size_t size)
+ {
+ m_stream << "(" << std::setw(4) << std::setfill(' ') << size << ") ";
+ const unsigned char* pos = static_cast<const unsigned char*>(data);
+ for (size_t i = 0; i < size; ++i)
+ m_stream << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*pos++);
+ m_stream << std::dec << "\n";
+ }
+
+ std::string twoDigit(int value) const
+ {
+ std::string res = x2str(value);
+ if (res.length() < 2)
+ res = "0" + res;
+ return res;
+ }
+};
+
+} // namespace Caster
+
+#endif
utsn.machine + " " +
utsn.nodename;
- m_answer = GetOpenTag() + "<version value=\"" + SERVER_VERSION + "\"/>" +
+ m_answer = std::string("<ServerInfo><version value=\"") + SERVER_VERSION + "\"/>" +
"<tariff_num value=\"" + x2str(m_tariffs.Count()) + "\"/>" +
"<tariff value=\"2\"/>" +
"<user_num value=\"" + x2str(m_users.Count()) + "\"/>" +
for (size_t i = 0; i< DIR_NUM; i++)
m_answer += "<dir_name_" + x2str(i) + " value=\"" + Encode12str(m_settings.GetDirName(i)) + "\"/>";
- m_answer += GetCloseTag();
+ m_answer += "</ServerInfo>";
}
std::string answer;
if (loginInStart)
- answer += "<User result=\"ok\">";
+ answer += "<User login=\"" + user.GetLogin() + "\" result=\"ok\">";
else
- answer += "<User result=\"ok\" login=\"" + user.GetLogin() + "\">";
+ answer += "<User result=\"ok\">";
answer += "<Login value=\"" + user.GetLogin() + "\"/>";
ensure_equals("DecryptString(EncryptString(longTest)) == longTest", source, std::string(longTest));
}
+ template<>
+ template<>
+ void testobject::test<8>()
+ {
+ set_test_name("Check old string encryption");
+
+ BLOWFISH_CTX ctx;
+ InitContext("123456", 7, &ctx);
+ const unsigned char source[] = {0xe9, 0xfe, 0xcb, 0xc5, 0xad, 0x3e, 0x87, 0x39,
+ 0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb,
+ 0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb,
+ 0x3d, 0xd5, 0xf4, 0xed, 0xb0, 0x15, 0xe6, 0xcb};
+ char res[32];
+ DecryptString(res, source, 32, &ctx);
+
+ ensure_equals("DecryptString(...) == 'admin'", std::string(res), "admin");
+ }
+
+ template<>
+ template<>
+ void testobject::test<9>()
+ {
+ set_test_name("Check new string encryption");
+
+ BLOWFISH_CTX ctx;
+ InitContext("123456", 7, &ctx);
+ const unsigned char source[] = {0xe9, 0xfe, 0xcb, 0xc5, 0xad, 0x3e, 0x87, 0x39,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ char res[32];
+ DecryptString(res, source, 32, &ctx);
+
+ ensure_equals("DecryptString(...) == 'admin'", std::string(res), "admin");
+ }
+
}