From e00778ef4b372ac8925c9f698551cb14e2858fea Mon Sep 17 00:00:00 2001 From: HanzZ Date: Sun, 20 Jan 2013 21:32:09 +0100 Subject: [PATCH 1/4] Create backend directory as root before setuid/setgid, otherwise we don't have permissions to do that --- src/logging.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/logging.cpp b/src/logging.cpp index e2433e34..b5c080b2 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -91,8 +91,11 @@ static intercept_stream* intercepter_cout; static intercept_stream* intercepter_cerr; -static void initLogging(Config *config, std::string key) { +static void initLogging(Config *config, std::string key, bool only_create_dir = false) { if (CONFIG_STRING(config, key).empty()) { + if (only_create_dir) { + return; + } root = log4cxx::Logger::getRootLogger(); #ifdef _MSC_VER root->addAppender(new ConsoleAppender(new PatternLayout(L"%d %-5p %c: %m%n"))); @@ -157,6 +160,10 @@ static void initLogging(Config *config, std::string key) { } } + if (only_create_dir) { + return; + } + log4cxx::PropertyConfigurator::configure(p); // Change owner of main log file @@ -190,6 +197,7 @@ void initBackendLogging(Config *config) { void initMainLogging(Config *config) { initLogging(config, "logging.config"); + initLogging(config, "logging.backend_config", true); } void redirect_stderr() { From 2f46d9f7a3ab814f76dce516bc5592496481ad53 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Tue, 22 Jan 2013 09:07:10 +0100 Subject: [PATCH 2/4] Working spectrum2_manager restart and hopefully working backend logging directory creation under unprivileged user --- spectrum/src/sample.cfg | 6 ++-- spectrum_manager/src/main.cpp | 3 ++ src/logging.cpp | 58 +++++++++++++++++++---------------- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/spectrum/src/sample.cfg b/spectrum/src/sample.cfg index d795b60e..75735b97 100644 --- a/spectrum/src/sample.cfg +++ b/spectrum/src/sample.cfg @@ -13,15 +13,15 @@ admin_password=test #cert=server.pfx #patch to PKCS#12 certificate #cert_password=test #password to that certificate if any users_per_backend=10 -backend=../..//backends/swiften/spectrum2_swiften_backend +#backend=../..//backends/swiften/spectrum2_swiften_backend #backend=../../backends/twitter/spectrum2_twitter_backend -#backend=/home/hanzz/code/libtransport/backends/libcommuni/spectrum2_libcommuni_backend +backend=/home/hanzz/code/libtransport/backends/libcommuni/spectrum2_libcommuni_backend protocol=prpl-jabber #protocol=prpl-msn #protocol=any #protocol=prpl-icq working_dir=./ -portfile=$jid.port +portfile=./$jid.port irc_server=irc.freenode.org [backend] diff --git a/spectrum_manager/src/main.cpp b/spectrum_manager/src/main.cpp index 9f09d2af..58581be2 100644 --- a/spectrum_manager/src/main.cpp +++ b/spectrum_manager/src/main.cpp @@ -138,6 +138,9 @@ int main(int argc, char **argv) else if (command[0] == "list") { std::vector list = show_list(&config); } + else if (command[0] == "restart") { + return restart_instances(&config); + } else if (command[0] == "server") { Server server(&config); if (server.start() == false) { diff --git a/src/logging.cpp b/src/logging.cpp index b5c080b2..947eb2bb 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -136,30 +136,38 @@ static void initLogging(Config *config, std::string key, bool only_create_dir = p.setProperty("id", id); #endif - std::string dir; + std::vector dirs; BOOST_FOREACH(const log4cxx::LogString &prop, p.propertyNames()) { -// if (boost::ends_with(prop, ".File")) { + if (boost::ends_with(prop, ".File")) { + std::string dir; log4cxx::helpers::Transcoder::encode(p.get(prop), dir); boost::replace_all(dir, "${jid}", jid); boost::replace_all(dir, "${pid}", pid); boost::replace_all(dir, "${id}", id); - break; -// } + dirs.push_back(dir); + } } mode_t old_cmask; - if (!dir.empty()) { - // create directories + // create directories #ifndef WIN32 - old_cmask = umask(0007); + old_cmask = umask(0007); #endif - try { - Transport::Util::createDirectories(config, boost::filesystem::path(dir).parent_path()); - } - catch (const boost::filesystem::filesystem_error &e) { - std::cerr << "Can't create logging directory directory " << boost::filesystem::path(dir).parent_path().string() << ": " << e.what() << ".\n"; + + BOOST_FOREACH(std::string &dir, dirs) { + if (!dir.empty()) { + try { + Transport::Util::createDirectories(config, boost::filesystem::path(dir).parent_path()); + } + catch (const boost::filesystem::filesystem_error &e) { + std::cerr << "Can't create logging directory directory " << boost::filesystem::path(dir).parent_path().string() << ": " << e.what() << ".\n"; + } } } +#ifndef WIN32 + umask(old_cmask); +#endif + if (only_create_dir) { return; } @@ -168,24 +176,20 @@ static void initLogging(Config *config, std::string key, bool only_create_dir = // Change owner of main log file #ifndef WIN32 - if (!CONFIG_STRING(config, "service.group").empty() && !CONFIG_STRING(config, "service.user").empty()) { - struct group *gr; - if ((gr = getgrnam(CONFIG_STRING(config, "service.group").c_str())) == NULL) { - std::cerr << "Invalid service.group name " << CONFIG_STRING(config, "service.group") << "\n"; + BOOST_FOREACH(std::string &dir, dirs) { + if (!CONFIG_STRING(config, "service.group").empty() && !CONFIG_STRING(config, "service.user").empty()) { + struct group *gr; + if ((gr = getgrnam(CONFIG_STRING(config, "service.group").c_str())) == NULL) { + std::cerr << "Invalid service.group name " << CONFIG_STRING(config, "service.group") << "\n"; + } + struct passwd *pw; + if ((pw = getpwnam(CONFIG_STRING(config, "service.user").c_str())) == NULL) { + std::cerr << "Invalid service.user name " << CONFIG_STRING(config, "service.user") << "\n"; + } + chown(dir.c_str(), pw->pw_uid, gr->gr_gid); } - struct passwd *pw; - if ((pw = getpwnam(CONFIG_STRING(config, "service.user").c_str())) == NULL) { - std::cerr << "Invalid service.user name " << CONFIG_STRING(config, "service.user") << "\n"; - } - chown(dir.c_str(), pw->pw_uid, gr->gr_gid); } #endif - -#ifndef WIN32 - if (!dir.empty()) { - umask(old_cmask); - } -#endif } } From 22270d1b616ab3bc7aaa182fcedb6dccecc463bf Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Tue, 22 Jan 2013 09:23:02 +0100 Subject: [PATCH 3/4] Remove bad logging messages --- src/userregistry.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/userregistry.cpp b/src/userregistry.cpp index 8d8a3975..2b7e358c 100644 --- a/src/userregistry.cpp +++ b/src/userregistry.cpp @@ -79,9 +79,6 @@ void UserRegistry::stopLogin(const Swift::JID& user, Swift::ServerFromClientSess LOG4CXX_WARN(logger, key << ": Stopping login process (user probably disconnected while logging in), but this is not active session"); } } - else { - LOG4CXX_WARN(logger, key << ": Stopping login process (user probably disconnected while logging in) for invalid user"); - } // ::removeLater can be called only by libtransport, not by Swift and libtransport // takes care about user disconnecting itself, so don't call our signal. @@ -96,9 +93,6 @@ void UserRegistry::onPasswordValid(const Swift::JID &user) { users[key].session->handlePasswordValid(); users.erase(key); } - else { - LOG4CXX_INFO(logger, key << ": onPasswordValid called for invalid user"); - } } void UserRegistry::onPasswordInvalid(const Swift::JID &user, const std::string &error) { @@ -108,9 +102,6 @@ void UserRegistry::onPasswordInvalid(const Swift::JID &user, const std::string & users[key].session->handlePasswordInvalid(error); users.erase(key); } - else { - LOG4CXX_INFO(logger, key << ": onPasswordInvalid called for invalid user"); - } } void UserRegistry::handleRemoveTimeout(const Swift::JID &user) { From 58f50c754489d9712ce0dd6ba0361fc4b4ac9985 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Tue, 22 Jan 2013 09:28:26 +0100 Subject: [PATCH 4/4] better error message for server mode --- src/userregistry.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/userregistry.cpp b/src/userregistry.cpp index 2b7e358c..c6cbdf32 100644 --- a/src/userregistry.cpp +++ b/src/userregistry.cpp @@ -98,7 +98,7 @@ void UserRegistry::onPasswordValid(const Swift::JID &user) { void UserRegistry::onPasswordInvalid(const Swift::JID &user, const std::string &error) { std::string key = user.toBare().toString(); if (users.find(key) != users.end()) { - LOG4CXX_INFO(logger, key << ": Password is invalid"); + LOG4CXX_INFO(logger, key << ": Password is invalid or there was an error when connecting the legacy network"); users[key].session->handlePasswordInvalid(error); users.erase(key); }