2011-08-21 19:46:55 +02:00
|
|
|
/**
|
|
|
|
* libtransport -- C++ library for easy XMPP Transports development
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include "Swiften/Server/UserRegistry.h"
|
2015-11-18 14:05:57 +01:00
|
|
|
#include "transport/UserRegistry.h"
|
|
|
|
#include "transport/Config.h"
|
|
|
|
#include "transport/Logging.h"
|
2011-08-21 19:46:55 +02:00
|
|
|
|
|
|
|
namespace Transport {
|
|
|
|
|
2012-03-21 16:31:51 +01:00
|
|
|
DEFINE_LOGGER(logger, "UserRegistry");
|
2011-08-21 19:46:55 +02:00
|
|
|
|
2011-08-24 08:31:32 +02:00
|
|
|
UserRegistry::UserRegistry(Config *cfg, Swift::NetworkFactories *factories) {
|
2011-08-21 19:46:55 +02:00
|
|
|
config = cfg;
|
2011-08-24 08:31:32 +02:00
|
|
|
m_removeTimer = factories->getTimerFactory()->createTimer(1);
|
|
|
|
m_inRemoveLater = false;
|
2011-08-21 19:46:55 +02:00
|
|
|
}
|
|
|
|
|
2011-08-24 08:31:32 +02:00
|
|
|
UserRegistry::~UserRegistry() { m_removeTimer->stop(); }
|
2011-08-21 19:46:55 +02:00
|
|
|
|
|
|
|
void UserRegistry::isValidUserPassword(const Swift::JID& user, Swift::ServerFromClientSession *session, const Swift::SafeByteArray& password) {
|
2012-04-09 09:27:26 +02:00
|
|
|
std::vector<std::string> const &x = CONFIG_VECTOR(config,"service.admin_jid");
|
|
|
|
if (std::find(x.begin(), x.end(), user.toBare().toString()) != x.end()) {
|
|
|
|
if (Swift::safeByteArrayToString(password) == CONFIG_STRING(config, "service.admin_password")) {
|
2012-04-11 09:16:56 +02:00
|
|
|
session->handlePasswordValid();
|
2012-04-09 09:27:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-04-11 09:16:56 +02:00
|
|
|
session->handlePasswordInvalid();
|
2012-04-09 09:27:26 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-08-21 19:46:55 +02:00
|
|
|
std::string key = user.toBare().toString();
|
|
|
|
|
|
|
|
// Users try to connect twice
|
|
|
|
if (users.find(key) != users.end()) {
|
2011-08-24 08:31:32 +02:00
|
|
|
// Kill the first session
|
|
|
|
LOG4CXX_INFO(logger, key << ": Removing previous session and making this one active");
|
|
|
|
Swift::ServerFromClientSession *tmp = users[key].session;
|
|
|
|
users[key].session = session;
|
|
|
|
tmp->handlePasswordInvalid();
|
2011-08-21 19:46:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG4CXX_INFO(logger, key << ": Connecting this user to find if password is valid");
|
|
|
|
|
|
|
|
users[key].password = Swift::safeByteArrayToString(password);
|
|
|
|
users[key].session = session;
|
|
|
|
onConnectUser(user);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserRegistry::stopLogin(const Swift::JID& user, Swift::ServerFromClientSession *session) {
|
|
|
|
std::string key = user.toBare().toString();
|
|
|
|
if (users.find(key) != users.end()) {
|
|
|
|
if (users[key].session == session) {
|
|
|
|
LOG4CXX_INFO(logger, key << ": Stopping login process (user probably disconnected while logging in)");
|
|
|
|
users.erase(key);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG4CXX_WARN(logger, key << ": Stopping login process (user probably disconnected while logging in), but this is not active session");
|
|
|
|
}
|
|
|
|
}
|
2011-08-24 08:31:32 +02:00
|
|
|
|
2011-08-24 13:24:48 +02:00
|
|
|
// ::removeLater can be called only by libtransport, not by Swift and libtransport
|
|
|
|
// takes care about user disconnecting itself, so don't call our signal.
|
2011-08-24 08:31:32 +02:00
|
|
|
if (!m_inRemoveLater)
|
|
|
|
onDisconnectUser(user);
|
2011-08-21 19:46:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserRegistry::onPasswordValid(const Swift::JID &user) {
|
|
|
|
std::string key = user.toBare().toString();
|
|
|
|
if (users.find(key) != users.end()) {
|
|
|
|
LOG4CXX_INFO(logger, key << ": Password is valid");
|
|
|
|
users[key].session->handlePasswordValid();
|
|
|
|
users.erase(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 20:49:20 +02:00
|
|
|
void UserRegistry::onPasswordInvalid(const Swift::JID &user, const std::string &error) {
|
2011-08-21 19:46:55 +02:00
|
|
|
std::string key = user.toBare().toString();
|
|
|
|
if (users.find(key) != users.end()) {
|
2013-01-22 09:28:26 +01:00
|
|
|
LOG4CXX_INFO(logger, key << ": Password is invalid or there was an error when connecting the legacy network");
|
2011-10-18 20:49:20 +02:00
|
|
|
users[key].session->handlePasswordInvalid(error);
|
2011-08-21 19:46:55 +02:00
|
|
|
users.erase(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 08:31:32 +02:00
|
|
|
void UserRegistry::handleRemoveTimeout(const Swift::JID &user) {
|
|
|
|
m_inRemoveLater = true;
|
|
|
|
onPasswordInvalid(user);
|
|
|
|
m_inRemoveLater = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserRegistry::removeLater(const Swift::JID &user) {
|
|
|
|
m_removeTimer->onTick.connect(boost::bind(&UserRegistry::handleRemoveTimeout, this, user));
|
|
|
|
m_removeTimer->start();
|
|
|
|
}
|
|
|
|
|
2011-08-24 00:35:34 +02:00
|
|
|
const std::string UserRegistry::getUserPassword(const std::string &barejid) {
|
2011-08-24 00:18:51 +02:00
|
|
|
if (users.find(barejid) != users.end())
|
|
|
|
return users[barejid].password;
|
2011-08-24 00:35:34 +02:00
|
|
|
return "";
|
2011-08-21 19:46:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|