summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorPhilipp Sehmisch <mana@crushnet.org>2010-01-17 01:28:42 +0100
committerPhilipp Sehmisch <mana@crushnet.org>2010-01-17 01:28:59 +0100
commitc9c199007fb6b77f8cd13ffdb560cd18ff3c5af4 (patch)
treea6e4b3d0ab1ae7f26e3e0847ebc53b44c59a7efd /src/common
parent8bf0791932c8809fe5bd85d53859b8ce6197e4ed (diff)
downloadmanaserv-c9c199007fb6b77f8cd13ffdb560cd18ff3c5af4.tar.gz
manaserv-c9c199007fb6b77f8cd13ffdb560cd18ff3c5af4.tar.xz
manaserv-c9c199007fb6b77f8cd13ffdb560cd18ff3c5af4.zip
Added permission manager. Currently only used for @commands and doesn't support <deny> and <alias> yet.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/permissionmanager.cpp147
-rw-r--r--src/common/permissionmanager.hpp54
2 files changed, 201 insertions, 0 deletions
diff --git a/src/common/permissionmanager.cpp b/src/common/permissionmanager.cpp
new file mode 100644
index 0000000..1e97e02
--- /dev/null
+++ b/src/common/permissionmanager.cpp
@@ -0,0 +1,147 @@
+/*
+ * The Mana Server
+ * Copyright (C) 2010 The Mana World Development Team
+ *
+ * This file is part of The Mana Server.
+ *
+ * The Mana Server 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
+ * any later version.
+ *
+ * The Mana Server 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 The Mana Server. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "common/permissionmanager.hpp"
+
+#include "game-server/character.hpp"
+#include "game-server/resourcemanager.hpp"
+#include "utils/logger.h"
+#include "utils/xml.hpp"
+
+
+static std::map<std::string, unsigned char> permissions;
+static std::string permissionFile;
+
+void addPermission(std::string permission, char mask)
+{
+
+ std::map<std::string, unsigned char>::iterator i = permissions.find(permission);
+ if (i == permissions.end())
+ {
+ permissions.insert(std::make_pair<std::string, unsigned char>(permission, mask));
+ } else {
+ i->second |= mask;
+ }
+}
+
+void PermissionManager::initialize(const std::string & file)
+{
+ permissionFile = file;
+ reload();
+}
+
+void PermissionManager::reload()
+{
+ int size;
+ char *data = ResourceManager::loadFile(permissionFile, size);
+
+ if (!data) {
+ LOG_ERROR("Permission Manager: Could not find "
+ << permissionFile << "!");
+ free(data);
+ return;
+ }
+
+ xmlDocPtr doc = xmlParseMemory(data, size);
+ free(data);
+
+ if (!doc)
+ {
+ LOG_ERROR("Permission Manager: Error while parsing permission database ("
+ << permissionFile << ")!");
+ return;
+ }
+
+ xmlNodePtr node = xmlDocGetRootElement(doc);
+ if (!node || !xmlStrEqual(node->name, BAD_CAST "permissions"))
+ {
+ LOG_ERROR("Permission Manager: " << permissionFile
+ << " is not a valid database file!");
+ xmlFreeDoc(doc);
+ return;
+ }
+
+ LOG_INFO("Loading permission reference...");
+ for (node = node->xmlChildrenNode; node != NULL; node = node->next)
+ {
+ unsigned char classmask = 0x01;
+ if (!xmlStrEqual(node->name, BAD_CAST "class"))
+ {
+ continue;
+ }
+ int level = XML::getProperty(node, "level", 0);
+ if (level < 1 || level > 8)
+ {
+ LOG_WARN("PermissionManager: Illegal class level "
+ <<level
+ <<" in "
+ <<permissionFile
+ <<" (allowed range: 1..8)");
+ continue;
+ }
+ classmask = classmask << (level-1);
+
+
+ xmlNodePtr perNode;
+ for (perNode = node->xmlChildrenNode; perNode != NULL; perNode = perNode->next)
+ {
+ if (xmlStrEqual(perNode->name, BAD_CAST "allow"))
+ {
+ const char* permission = (const char*)perNode->xmlChildrenNode->content;
+ if (permission && strlen(permission) > 0)
+ {
+ addPermission(permission, classmask);
+ }
+ } else if (xmlStrEqual(perNode->name, BAD_CAST "deny")){
+ const char* permission = (const char*)perNode->xmlChildrenNode->content;
+ // To be implemented
+ } else if (xmlStrEqual(perNode->name, BAD_CAST "alias")){
+ const char* alias = (const char*)perNode->xmlChildrenNode->content;
+ // To be implemented
+ }
+ }
+ }
+
+ LOG_INFO("Permission List:");
+ for (std::map<std::string, unsigned char>::iterator i = permissions.begin();
+ i != permissions.end();
+ i++)
+ {
+ LOG_INFO(i->first<<" "<<(int)i->second);
+ }
+}
+
+
+PermissionManager::Result PermissionManager::checkPermission(const Character* character, std::string permission)
+{
+ std::map<std::string, unsigned char>::iterator iP = permissions.find(permission);
+
+ if (iP == permissions.end())
+ {
+ LOG_WARN("PermissionManager: Check for unknown permission \""<<permission<<"\" requested.");
+ return PMR_UNKNOWN;
+ }
+ if (character->getAccountLevel() & iP->second)
+ {
+ return PMR_ALLOWED;
+ } else {
+ return PMR_DENIED;
+ }
+}
diff --git a/src/common/permissionmanager.hpp b/src/common/permissionmanager.hpp
new file mode 100644
index 0000000..4989d0a
--- /dev/null
+++ b/src/common/permissionmanager.hpp
@@ -0,0 +1,54 @@
+/*
+ * The Mana Server
+ * Copyright (C) 2010 The Mana World Development Team
+ *
+ * This file is part of The Mana Server.
+ *
+ * The Mana Server 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
+ * any later version.
+ *
+ * The Mana Server 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 The Mana Server. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PERMISSIONMANAGER_HPP
+#define PERMISSIONMANAGER_HPP
+
+#include <map>
+#include <string>
+
+class Character;
+
+namespace PermissionManager
+{
+ enum Result
+ {
+ PMR_UNKNOWN,
+ PMR_DENIED,
+ PMR_ALLOWED
+ };
+ /**
+ * Loads permission file.
+ */
+ void initialize(const std::string &);
+
+ /**
+ * Reloads permission file.
+ */
+ void reload();
+
+ /**
+ * Returns if the characters account has the given permission
+ */
+ Result checkPermission(const Character* character, std::string permission);
+
+}
+
+#endif