Dummy encryption key implementation + logging glib errors in main log

This commit is contained in:
HanzZ 2011-11-01 22:13:35 +01:00
parent 06ea1ee664
commit 6d64a1858b
5 changed files with 113 additions and 3 deletions

View file

@ -1569,11 +1569,78 @@ static void transport_core_ui_init(void)
// #endif
}
/***** Core Ui Ops *****/
static void
spectrum_glib_log_handler(const gchar *domain,
GLogLevelFlags flags,
const gchar *message,
gpointer user_data)
{
const char *level;
char *new_msg = NULL;
char *new_domain = NULL;
if ((flags & G_LOG_LEVEL_ERROR) == G_LOG_LEVEL_ERROR)
level = "ERROR";
else if ((flags & G_LOG_LEVEL_CRITICAL) == G_LOG_LEVEL_CRITICAL)
level = "CRITICAL";
else if ((flags & G_LOG_LEVEL_WARNING) == G_LOG_LEVEL_WARNING)
level = "WARNING";
else if ((flags & G_LOG_LEVEL_MESSAGE) == G_LOG_LEVEL_MESSAGE)
level = "MESSAGE";
else if ((flags & G_LOG_LEVEL_INFO) == G_LOG_LEVEL_INFO)
level = "INFO";
else if ((flags & G_LOG_LEVEL_DEBUG) == G_LOG_LEVEL_DEBUG)
level = "DEBUG";
else {
LOG4CXX_ERROR(logger, "Unknown glib logging level in " << (guint)flags);
level = "UNKNOWN"; /* This will never happen. */
}
if (message != NULL)
new_msg = purple_utf8_try_convert(message);
if (domain != NULL)
new_domain = purple_utf8_try_convert(domain);
if (new_msg != NULL) {
std::string area("glib");
area.push_back('/');
area.append(level);
std::string message(new_domain ? new_domain : "g_log");
message.push_back(' ');
message.append(new_msg);
LOG4CXX_ERROR(logger, message);
g_free(new_msg);
}
g_free(new_domain);
}
static void
debug_init(void)
{
#define REGISTER_G_LOG_HANDLER(name) \
g_log_set_handler((name), \
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL \
| G_LOG_FLAG_RECURSION), \
spectrum_glib_log_handler, NULL)
REGISTER_G_LOG_HANDLER(NULL);
REGISTER_G_LOG_HANDLER("GLib");
REGISTER_G_LOG_HANDLER("GModule");
REGISTER_G_LOG_HANDLER("GLib-GObject");
REGISTER_G_LOG_HANDLER("GThread");
#undef REGISTER_G_LOD_HANDLER
}
static PurpleCoreUiOps coreUiOps =
{
NULL,
// debug_init,
NULL,
debug_init,
transport_core_ui_init,
NULL,
spectrum_ui_get_info,

View file

@ -24,6 +24,7 @@
#include <stdlib.h>
#include <vector>
#include <string>
#include "Swiften/StringCodecs/Base64.h"
namespace Transport {
@ -31,6 +32,10 @@ namespace Util {
void removeEverythingOlderThan(const std::vector<std::string> &dirs, time_t t);
std::string encryptPassword(const std::string &password, const std::string &key);
std::string decryptPassword(std::string &encrypted, const std::string &key);
}
}

View file

@ -92,6 +92,7 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
("database.password", value<std::string>()->default_value(""), "Database Password.")
("database.port", value<int>()->default_value(0), "Database port.")
("database.prefix", value<std::string>()->default_value(""), "Prefix of tables in database")
("database.encryption_key", value<std::string>()->default_value(""), "Encryption key.")
("logging.config", value<std::string>()->default_value(""), "Path to log4cxx config file which is used for Spectrum 2 instance")
("logging.backend_config", value<std::string>()->default_value(""), "Path to log4cxx config file which is used for backends")
("backend.default_avatar", value<std::string>()->default_value(""), "Full path to default avatar")

View file

@ -21,6 +21,7 @@
#ifdef WITH_MYSQL
#include "transport/mysqlbackend.h"
#include "transport/util.h"
#include <boost/bind.hpp>
#include "log4cxx/logger.h"
@ -409,7 +410,11 @@ bool MySQLBackend::exec(const std::string &query) {
}
void MySQLBackend::setUser(const UserInfo &user) {
*m_setUser << user.jid << user.uin << user.password << user.language << user.encoding << user.vip << user.password;
std::string encrypted = user.password;
if (!CONFIG_STRING(m_config, "database.encryption_key").empty()) {
encrypted = Util::decryptPassword(encrypted, CONFIG_STRING(m_config, "database.encryption_key"));
}
*m_setUser << user.jid << user.uin << encrypted << user.language << user.encoding << user.vip << user.password;
EXEC(m_setUser, setUser(user));
}
@ -423,6 +428,10 @@ bool MySQLBackend::getUser(const std::string &barejid, UserInfo &user) {
while (m_getUser->fetch() == 0) {
ret = true;
*m_getUser >> user.id >> user.jid >> user.uin >> user.password >> user.encoding >> user.language >> user.vip;
if (!CONFIG_STRING(m_config, "database.encryption_key").empty()) {
user.password = Util::decryptPassword(user.password, CONFIG_STRING(m_config, "database.encryption_key"));
}
}
return ret;

View file

@ -75,6 +75,34 @@ void removeEverythingOlderThan(const std::vector<std::string> &dirs, time_t t) {
}
}
std::string encryptPassword(const std::string &password, const std::string &key) {
std::string encrypted;
encrypted.resize(password.size());
for (int i = 0; i < password.size(); i++) {
char c = password[i];
char keychar = key[i % key.size()];
c += keychar;
encrypted[i] = c;
}
encrypted = Swift::Base64::encode(Swift::createByteArray(encrypted));
return encrypted;
}
std::string decryptPassword(std::string &encrypted, const std::string &key) {
encrypted = Swift::byteArrayToString(Swift::Base64::decode(encrypted));
std::string password;
password.resize(encrypted.size());
for (int i = 0; i < encrypted.size(); i++) {
char c = encrypted[i];
char keychar = key[i % key.size()];
c -= keychar;
password[i] = c;
}
return password;
}
}
}