2010-07-29 22:01:38 +05:30
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2010 Alistair Riddoch
|
|
|
|
|
//
|
|
|
|
|
// 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 02110-1301 USA
|
|
|
|
|
|
|
|
|
|
#include "TeleportAuthenticator.h"
|
|
|
|
|
#include "PendingTeleport.h"
|
|
|
|
|
|
2010-11-26 11:18:06 +00:00
|
|
|
#include "common/BaseWorld.h"
|
2010-08-05 03:02:57 +05:30
|
|
|
#include "common/log.h"
|
|
|
|
|
#include "common/compose.hpp"
|
|
|
|
|
|
2013-02-08 23:38:32 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
2010-07-29 22:53:44 +05:30
|
|
|
TeleportAuthenticator * TeleportAuthenticator::m_instance = NULL;
|
|
|
|
|
|
2010-08-05 17:03:23 +05:30
|
|
|
/// \brief Checks if there is a pending teleport on an account
|
|
|
|
|
///
|
|
|
|
|
/// \param entity_id The entity ID to check for pending teleport
|
2013-02-09 00:33:47 +00:00
|
|
|
bool TeleportAuthenticator::isPending(const std::string & entity_id) const
|
2010-07-29 22:01:38 +05:30
|
|
|
{
|
2013-02-08 23:13:46 +00:00
|
|
|
PendingTeleportMap::const_iterator I = m_teleports.find(entity_id);
|
2010-08-05 03:02:57 +05:30
|
|
|
return (I != m_teleports.end());
|
2010-07-29 22:01:38 +05:30
|
|
|
}
|
|
|
|
|
|
2010-08-05 17:03:23 +05:30
|
|
|
/// \brief Add a teleport authentication entry
|
|
|
|
|
///
|
|
|
|
|
/// \param entity_id The ID of the entity whose data is to be removed
|
|
|
|
|
/// \param possess_key The possess key to authenticate the entity with
|
2013-02-09 00:33:47 +00:00
|
|
|
int TeleportAuthenticator::addTeleport(const std::string & entity_id,
|
|
|
|
|
const std::string & possess_key)
|
2010-07-29 22:01:38 +05:30
|
|
|
{
|
2010-08-03 02:02:24 +05:30
|
|
|
if (isPending(entity_id)) {
|
2010-07-29 22:01:38 +05:30
|
|
|
return -1;
|
|
|
|
|
}
|
2013-02-08 23:18:59 +00:00
|
|
|
PendingTeleport * pt = new PendingTeleport(entity_id, possess_key);
|
|
|
|
|
if (pt == 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
m_teleports.insert(std::make_pair(entity_id, pt));
|
2010-08-05 03:02:57 +05:30
|
|
|
log(INFO, String::compose("Added teleport auth entry for %1,%2",
|
2013-02-08 23:18:59 +00:00
|
|
|
entity_id, possess_key));
|
2010-08-05 03:02:57 +05:30
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-09 00:29:03 +00:00
|
|
|
/// \brief Remove a teleport authentications entry.
|
2010-08-05 17:03:23 +05:30
|
|
|
///
|
|
|
|
|
/// \param entity_id The ID of the entity whose data is to be removed
|
2010-08-05 03:02:57 +05:30
|
|
|
int TeleportAuthenticator::removeTeleport(const std::string &entity_id)
|
|
|
|
|
{
|
|
|
|
|
PendingTeleportMap::iterator I = m_teleports.find(entity_id);
|
|
|
|
|
if (I == m_teleports.end()) {
|
|
|
|
|
log(ERROR, String::compose("No teleport auth entry for entity ID %1",
|
|
|
|
|
entity_id));
|
2010-08-05 17:11:02 +05:30
|
|
|
return -1;
|
2010-08-05 03:02:57 +05:30
|
|
|
}
|
2013-02-09 00:25:33 +00:00
|
|
|
removeTeleport(I);
|
2010-08-05 03:02:57 +05:30
|
|
|
log(ERROR, String::compose("Removed teleport auth entry for entity ID %1",
|
|
|
|
|
entity_id));
|
|
|
|
|
return 0;
|
2010-07-29 22:01:38 +05:30
|
|
|
}
|
|
|
|
|
|
2013-02-09 00:29:03 +00:00
|
|
|
/// \brief Remove a teleport authentications entry internals.
|
2010-08-06 00:33:17 +05:30
|
|
|
///
|
2013-02-09 00:29:03 +00:00
|
|
|
/// Typically after a successful authentication
|
2010-08-06 00:33:17 +05:30
|
|
|
/// \param I The iterator in m_teleports to be removed
|
2013-02-09 00:23:20 +00:00
|
|
|
void TeleportAuthenticator::removeTeleport(PendingTeleportMap::iterator I)
|
2010-08-06 00:33:17 +05:30
|
|
|
{
|
2013-02-08 23:22:30 +00:00
|
|
|
assert(I->second != 0);
|
|
|
|
|
delete I->second;
|
2010-08-06 00:33:17 +05:30
|
|
|
m_teleports.erase(I);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-05 17:03:23 +05:30
|
|
|
/// \brief Authenticate a teleport request
|
|
|
|
|
///
|
|
|
|
|
/// \param entity_id The ID of the entity that was created
|
2010-08-05 22:09:25 +05:30
|
|
|
/// \param possess_key The possess key sent by the client
|
2013-01-17 10:17:07 +00:00
|
|
|
LocatedEntity * TeleportAuthenticator::authenticateTeleport(const std::string &entity_id,
|
2010-07-29 22:01:38 +05:30
|
|
|
const std::string &possess_key)
|
|
|
|
|
{
|
2010-08-06 00:33:17 +05:30
|
|
|
PendingTeleportMap::iterator I = m_teleports.find(entity_id);
|
|
|
|
|
if (I == m_teleports.end()) {
|
2011-04-24 23:18:14 +02:00
|
|
|
log(ERROR, String::compose("Unable to find teleported entity with ID %1",
|
2010-08-06 00:33:17 +05:30
|
|
|
entity_id));
|
2010-08-05 03:02:57 +05:30
|
|
|
return NULL;
|
2010-07-29 22:01:38 +05:30
|
|
|
}
|
2010-08-06 00:33:17 +05:30
|
|
|
PendingTeleport *entry = I->second;
|
2013-02-08 23:22:30 +00:00
|
|
|
assert(entry != 0);
|
2010-07-29 22:01:38 +05:30
|
|
|
if (entry->validate(entity_id, possess_key)) {
|
|
|
|
|
// We are authenticated!
|
2013-01-17 10:17:07 +00:00
|
|
|
LocatedEntity * entity = BaseWorld::instance().getEntity(entity_id);
|
2010-08-05 17:11:02 +05:30
|
|
|
if (entity == 0) {
|
2010-08-05 17:13:02 +05:30
|
|
|
// This means the authentication entry itself is invalid. Remove it.
|
2011-04-24 23:18:14 +02:00
|
|
|
log(ERROR, String::compose("Unable to find teleported entity with ID %1",
|
2010-08-05 03:02:57 +05:30
|
|
|
entity_id));
|
2010-08-06 00:33:17 +05:30
|
|
|
removeTeleport(I);
|
2010-08-05 03:02:57 +05:30
|
|
|
return NULL;
|
|
|
|
|
}
|
2010-08-05 17:11:02 +05:30
|
|
|
// Don't remove the entry yet. It will be removed after connecting
|
|
|
|
|
// the entity to the account in Account::LookOperation() successfully
|
2010-08-05 17:06:11 +05:30
|
|
|
// removeTeleport(entity_id);
|
2010-08-05 03:02:57 +05:30
|
|
|
return entity;
|
2010-07-29 22:01:38 +05:30
|
|
|
}
|
2010-08-05 17:13:02 +05:30
|
|
|
// We failed the authentication. Keep authentication entry for retries.
|
2010-08-05 03:02:57 +05:30
|
|
|
return NULL;
|
2010-07-29 22:01:38 +05:30
|
|
|
}
|