From 20ba88890d2a10e9c912e3a88d4db805a275e08b Mon Sep 17 00:00:00 2001 From: Dominik Date: Fri, 27 Jan 2012 15:11:24 +0100 Subject: [PATCH 1/3] - registration with local user accounts stub --- src/config.cpp | 2 ++ src/userregistration.cpp | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/config.cpp b/src/config.cpp index c5ca48b5..d456024f 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -87,6 +87,8 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description ("registration.username_label", value()->default_value("Legacy network username:"), "Label for username field") ("registration.username_mask", value()->default_value(""), "Username mask") ("registration.encoding", value()->default_value("utf8"), "Default encoding in registration form") + ("registration.require_local_account", value()->default_value(false), "True if users have to have a local account to register to this transport from remote servers.") + ("registration.local_username_label", value()->default_value("Local username:"), "Label for local usernme field") ("database.type", value()->default_value("none"), "Database type.") ("database.database", value()->default_value(""), "Database used to store data") ("database.server", value()->default_value("localhost"), "Database server.") diff --git a/src/userregistration.cpp b/src/userregistration.cpp index 9160465f..6d3e9fd5 100644 --- a/src/userregistration.cpp +++ b/src/userregistration.cpp @@ -241,6 +241,20 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID boolean->setLabel((("Remove your registration"))); boolean->setValue(0); form->addField(boolean); + } else { + if (CONFIG_BOOL(m_config,"registration.require_local_account")) { + std::string localUsernameField = CONFIG_STRING(m_config, "registration.local_username_label"); + TextSingleFormField::ref local_username = TextSingleFormField::create(); + local_username->setName("local_username"); + local_username->setLabel((localUsernameField)); + local_username->setRequired(true); + form->addField(local_username); + TextPrivateFormField::ref local_password = TextPrivateFormField::create(); + local_password->setName("local_password"); + local_password->setLabel((("Local Password"))); + local_password->setRequired(true); + form->addField(local_password); + } } reg->setForm(form); @@ -273,6 +287,8 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID std::string encoding; std::string language; + std::string local_username(""); + std::string local_password(""); Form::ref form = payload->getForm(); if (form) { @@ -290,6 +306,13 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID else if (textSingle->getName() == "password") { payload->setPassword(textSingle->getValue()); } + else if (textSingle->getName() == "local_username") { + local_username = textSingle->getValue(); + } + // Pidgin sends it as textSingle, not sure why... + else if (textSingle->getName() == "local_password") { + local_password = textSingle->getValue(); + } continue; } @@ -298,6 +321,9 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID if (textPrivate->getName() == "password") { payload->setPassword(textPrivate->getValue()); } + else if (textPrivate->getName() == "local_password") { + local_password = textPrivate->getValue(); + } continue; } @@ -327,6 +353,22 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID return true; } + if (CONFIG_BOOL(m_config,"registration.require_local_account")) { + /* if (!local_username || !local_password) { + sendResponse(from, id, InBandRegistrationPayload::ref()); + return true + } else */ if (local_username == "" || local_password == "") { + sendResponse(from, id, InBandRegistrationPayload::ref()); + return true; + } else if (local_username != "heinz" || local_password != "heinz") { + // TODO: Check local password and username + sendError(from, id, ErrorPayload::NotAuthorized, ErrorPayload::Modify); + return true; + } + } + + printf("here\n"); + if (!payload->getUsername() || !payload->getPassword()) { sendError(from, id, ErrorPayload::NotAcceptable, ErrorPayload::Modify); return true; From 7619b9e2b2cbd4c6416fcf75f8fed575bb0dc444 Mon Sep 17 00:00:00 2001 From: Dominik Date: Fri, 27 Jan 2012 18:33:18 +0100 Subject: [PATCH 2/3] - check local_username against configured server - TODO: improve really crappy and hacky Swiften password check solution --- spectrum/src/sample2.cfg | 17 +++++++++++++++++ src/config.cpp | 2 ++ src/userregistration.cpp | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/spectrum/src/sample2.cfg b/spectrum/src/sample2.cfg index d2de8992..508395e9 100644 --- a/spectrum/src/sample2.cfg +++ b/spectrum/src/sample2.cfg @@ -95,3 +95,20 @@ type = none # Prefix used for tables #prefix = jabber_ + +[registration] +# Enable public registrations +enable_public_registration=1 + +# Text to display upon user registration form +username_label=Jabber JID (e.g. user@server.tld): +instructions=Enter your remote jabber JID and password as well as your local username and password + +# If True a local jabber account on is needed +# for transport registration, the idea is to enable public registration +# from other servers, but only for users, who have already local accounts +require_local_account=1 +local_username_label=Local username (without @server.tld): +local_account_server=localhost +local_account_server_timeout=10000 + diff --git a/src/config.cpp b/src/config.cpp index d456024f..91e5ed67 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -89,6 +89,8 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description ("registration.encoding", value()->default_value("utf8"), "Default encoding in registration form") ("registration.require_local_account", value()->default_value(false), "True if users have to have a local account to register to this transport from remote servers.") ("registration.local_username_label", value()->default_value("Local username:"), "Label for local usernme field") + ("registration.local_account_server", value()->default_value("localhost"), "The server on which the local accounts will be checked for validity") + ("registration.local_account_server_timeout", value()->default_value(10000), "Timeout when checking local user on local_account_server (msecs)") ("database.type", value()->default_value("none"), "Database type.") ("database.database", value()->default_value(""), "Database used to store data") ("database.server", value()->default_value("localhost"), "Database server.") diff --git a/src/userregistration.cpp b/src/userregistration.cpp index 6d3e9fd5..d5d7d611 100644 --- a/src/userregistration.cpp +++ b/src/userregistration.cpp @@ -26,6 +26,8 @@ #include "transport/user.h" #include "Swiften/Elements/ErrorPayload.h" #include +#include +#include #include "log4cxx/logger.h" using namespace Swift; @@ -360,8 +362,36 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID } else */ if (local_username == "" || local_password == "") { sendResponse(from, id, InBandRegistrationPayload::ref()); return true; - } else if (local_username != "heinz" || local_password != "heinz") { - // TODO: Check local password and username + } + Swift::logging = true; + bool validLocal = false; + std::string localLookupServer = CONFIG_STRING(m_config, "registration.local_account_server"); + std::string localLookupJID = local_username + std::string("@") + localLookupServer; + SimpleEventLoop localLookupEventLoop; + BoostNetworkFactories localLookupNetworkFactories(&localLookupEventLoop); + Client localLookupClient(localLookupJID, local_password, &localLookupNetworkFactories); + + // TODO: this is neccessary on my server ... but should maybe omitted + localLookupClient.setAlwaysTrustCertificates(); + localLookupClient.connect(); + + class SimpleLoopRunner { + public: + SimpleLoopRunner() {}; + + static void run(SimpleEventLoop * loop) { + loop->run(); + }; + }; + + // TODO: Really ugly and hacky solution, any other ideas more than welcome! + boost::thread thread(boost::bind(&(SimpleLoopRunner::run), &localLookupEventLoop)); + thread.timed_join(boost::posix_time::millisec(CONFIG_INT(m_config, "registration.local_account_server_timeout"))); + localLookupEventLoop.stop(); + thread.join(); + validLocal = localLookupClient.isAvailable(); + localLookupClient.disconnect(); + if (!validLocal) { sendError(from, id, ErrorPayload::NotAuthorized, ErrorPayload::Modify); return true; } From daf9143bf26005fe951de79a989d865a564da151 Mon Sep 17 00:00:00 2001 From: Dominik Date: Fri, 27 Jan 2012 18:41:47 +0100 Subject: [PATCH 3/3] - forgot to disable logging --- src/userregistration.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/userregistration.cpp b/src/userregistration.cpp index d5d7d611..f7de616e 100644 --- a/src/userregistration.cpp +++ b/src/userregistration.cpp @@ -363,7 +363,7 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID sendResponse(from, id, InBandRegistrationPayload::ref()); return true; } - Swift::logging = true; +// Swift::logging = true; bool validLocal = false; std::string localLookupServer = CONFIG_STRING(m_config, "registration.local_account_server"); std::string localLookupJID = local_username + std::string("@") + localLookupServer;