diff options
Diffstat (limited to 'src/modules/AuthProviderFile')
-rw-r--r-- | src/modules/AuthProviderFile/AuthProviderFile.cpp | 78 | ||||
-rw-r--r-- | src/modules/AuthProviderFile/AuthProviderFile.h | 80 | ||||
-rw-r--r-- | src/modules/AuthProviderFile/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/modules/AuthProviderFile/Module.cpp | 30 | ||||
-rw-r--r-- | src/modules/AuthProviderFile/Module.h | 52 |
5 files changed, 248 insertions, 0 deletions
diff --git a/src/modules/AuthProviderFile/AuthProviderFile.cpp b/src/modules/AuthProviderFile/AuthProviderFile.cpp new file mode 100644 index 0000000..ce575cd --- /dev/null +++ b/src/modules/AuthProviderFile/AuthProviderFile.cpp @@ -0,0 +1,78 @@ +/* + * AuthBackendFile.cpp + * + * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "AuthProviderFile.h" +#include <Core/ConfigEntry.h> + +#include <fstream> + +#include <boost/regex.hpp> + +namespace Mad { +namespace Modules { +namespace AuthProviderFile { + +void AuthProviderFile::readFile(const std::string &name) { + std::ifstream stream(name.c_str()); + if(!stream.good()) { + application->logf(Core::LoggerBase::LOG_WARNING, "AuthProviderFile: Can't read file '%s'.", name.c_str()); + return; + } + + while(stream.good() && !stream.eof()) { + std::string line; + std::getline(stream, line); + + if(line.empty()) + continue; + + static const boost::regex regex("([^:]+):(.+)", boost::regex_constants::perl); + boost::smatch match; + + if(!boost::regex_match(line, match, regex)) { + application->logf(Core::LoggerBase::LOG_WARNING, "AuthProviderFile: Malformed line in file '%s'.", name.c_str()); + continue; + } + + + userMap.insert(std::make_pair(match[1].str(), match[2].str())); + } +} + +bool AuthProviderFile::handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/) { + if(!entry[0].getKey().matches("AuthProviderFile")) + return false; + + if(entry[1].empty()) + return true; + + if(entry[1].getKey().matches("File")) { + if(entry[2].empty()) { + readFile(entry[1][0]); + } + } + else if(!entry[2].empty()) + return false; + + return true; +} + +} +} +} diff --git a/src/modules/AuthProviderFile/AuthProviderFile.h b/src/modules/AuthProviderFile/AuthProviderFile.h new file mode 100644 index 0000000..da0c8cd --- /dev/null +++ b/src/modules/AuthProviderFile/AuthProviderFile.h @@ -0,0 +1,80 @@ +/* + * AuthProviderFile.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_MODULES_AUTHPROVIDERFILE_AUTHPROVIDERFILE_H_ +#define MAD_MODULES_AUTHPROVIDERFILE_AUTHPROVIDERFILE_H_ + +#include "../export.h" + +#include <Common/AuthProvider.h> +#include <Common/Application.h> +#include <Common/Hash.h> + +#include <Core/Configurable.h> +#include <Core/ConfigManager.h> + +namespace Mad { +namespace Modules { +namespace AuthProviderFile { + +class MAD_MODULE_EXPORT AuthProviderFile : public Common::AuthProvider, private Core::Configurable, private boost::noncopyable { + private: + void readFile(const std::string &name); + + Common::Application *application; + + std::map<std::string, std::string> userMap; + + std::vector<std::string> hashes; + + protected: + virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/); + + virtual const std::vector<std::string>& getHashes() const { + return hashes; + } + + virtual std::vector<boost::uint8_t> getPassword(const std::string &user, const std::string &hash) throw(Core::Exception) { + std::map<std::string, std::string>::iterator userIt = userMap.find(user); + if(userIt == userMap.end()) + return std::vector<boost::uint8_t>(); + + if(hash == "Clear") + return std::vector<boost::uint8_t>(userIt->second.begin(), userIt->second.end()); + else + return Common::Hash::hash(std::vector<boost::uint8_t>(userIt->second.begin(), userIt->second.end()), hash); + } + + public: + AuthProviderFile(Common::Application *application0) : application(application0), hashes(Common::Hash::getHashList()) { + hashes.push_back("Clear"); + + application->getConfigManager()->registerConfigurable(this); + } + + virtual ~AuthProviderFile() { + application->getConfigManager()->unregisterConfigurable(this); + } +}; + +} +} +} + +#endif /* MAD_MODULES_AUTHPROVIDERFILE_AUTHPROVIDERFILE_H_ */ diff --git a/src/modules/AuthProviderFile/CMakeLists.txt b/src/modules/AuthProviderFile/CMakeLists.txt new file mode 100644 index 0000000..609357a --- /dev/null +++ b/src/modules/AuthProviderFile/CMakeLists.txt @@ -0,0 +1,8 @@ +include_directories(${INCLUDES}) + +mad_module(AuthProviderFile + Module.cpp Module.h + AuthProviderFile.cpp AuthProviderFile.h +) + +mad_module_libraries(AuthProviderFile) diff --git a/src/modules/AuthProviderFile/Module.cpp b/src/modules/AuthProviderFile/Module.cpp new file mode 100644 index 0000000..aa84d22 --- /dev/null +++ b/src/modules/AuthProviderFile/Module.cpp @@ -0,0 +1,30 @@ +/* + * Module.cpp + * + * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "../export.h" + +#include "Module.h" + +extern "C" { + +MAD_MODULE_EXPORT Mad::Common::Module* AuthProviderFile_create(Mad::Common::Application *application) { + return new Mad::Modules::AuthProviderFile::Module(application); +} + +} diff --git a/src/modules/AuthProviderFile/Module.h b/src/modules/AuthProviderFile/Module.h new file mode 100644 index 0000000..772a83b --- /dev/null +++ b/src/modules/AuthProviderFile/Module.h @@ -0,0 +1,52 @@ +/* + * Module.h + * + * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_MODULES_AUTHPROVIDERFILE_MODULE_H_ +#define MAD_MODULES_AUTHPROVIDERFILE_MODULE_H_ + +#include "AuthProviderFile.h" + +#include <Common/Module.h> +#include <Common/AuthManager.h> + +namespace Mad { +namespace Modules { +namespace AuthProviderFile { + +class Module : public Common::Module { + private: + Common::Application *application; + + boost::shared_ptr<AuthProviderFile> provider; + + public: + Module(Common::Application *application0) : application(application0), provider(new AuthProviderFile(application)) { + application->getAuthManager()->setProvider(provider); + } + + virtual ~Module() { + application->getAuthManager()->unsetProvider(provider); + } +}; + +} +} +} + +#endif /* MAD_MODULES_AUTHPROVIDERFILE_MODULE_H_ */ |