diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-08-27 17:27:58 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-08-27 17:27:58 +0200 |
commit | 82ef58fb3d0bdf6ce7d13f42cca30d03b24973c6 (patch) | |
tree | 4508a31968224772e70fd9df38f11b3a2461c55e | |
parent | 854e90be061166d1619a74cbfdc7e384fc700125 (diff) | |
download | mad-82ef58fb3d0bdf6ce7d13f42cca30d03b24973c6.tar mad-82ef58fb3d0bdf6ce7d13f42cca30d03b24973c6.zip |
AuthProvider hinzugefügt
AuthBackendFile in AuthProviderFile und AuthBackendPassword aufgeteilt
21 files changed, 509 insertions, 271 deletions
diff --git a/src/Common/AuthBackend.h b/src/Common/AuthBackend.h index d916d1d..3fe3840 100644 --- a/src/Common/AuthBackend.h +++ b/src/Common/AuthBackend.h @@ -32,17 +32,18 @@ namespace Common { class AuthContext; class AuthManager; +class AuthProvider; class AuthBackend { protected: friend class AuthManager; - virtual const std::vector<std::string>& getMethods() const = 0; - virtual const std::vector<std::string>& getSubMethods(const std::string &method) const throw(Core::Exception) = 0; + virtual const std::string& getMethodName() const = 0; + virtual std::vector<std::string> getSubMethods(boost::shared_ptr<AuthProvider> provider) const = 0; - virtual boost::shared_ptr<AuthContext> authenticate(const std::string& /*method*/, const std::string& /*subMethod*/, - const std::string& /*user*/, const std::vector<boost::uint8_t>& /*data*/, std::vector<boost::uint8_t>& /*response*/, - boost::shared_ptr<AuthContext> /*context*/) throw(Core::Exception) = 0; + virtual boost::shared_ptr<AuthContext> authenticate(boost::shared_ptr<AuthProvider> provider, const std::string &subMethod, + const std::string &user, const std::vector<boost::uint8_t> &data, std::vector<boost::uint8_t> &response, + boost::shared_ptr<AuthContext> context) throw(Core::Exception) = 0; public: virtual ~AuthBackend() {} diff --git a/src/Common/AuthManager.cpp b/src/Common/AuthManager.cpp index d21909d..c4cace7 100644 --- a/src/Common/AuthManager.cpp +++ b/src/Common/AuthManager.cpp @@ -25,31 +25,54 @@ namespace Mad { namespace Common { -const std::vector<std::string> AuthManager::DenyBackend::methods; +void AuthManager::setProvider(boost::shared_ptr<AuthProvider> newProvider) { + boost::lock_guard<boost::shared_mutex> lock(mutex); + + provider = newProvider; +} + +void AuthManager::unsetProvider(boost::shared_ptr<AuthProvider> oldProvider) { + boost::lock_guard<boost::shared_mutex> lock(mutex); + + if(oldProvider == provider) + provider.reset(); +} -void AuthManager::registerBackend(boost::shared_ptr<AuthBackend> newBackend) { +void AuthManager::registerBackend(boost::shared_ptr<AuthBackend> backend) { boost::lock_guard<boost::shared_mutex> lock(mutex); - backend = newBackend; + methods.insert(backend->getMethodName()); + backends.insert(std::make_pair(backend->getMethodName(), backend)); } -void AuthManager::unregisterBackend(boost::shared_ptr<AuthBackend> oldBackend) { +void AuthManager::unregisterBackend(boost::shared_ptr<AuthBackend> backend) { boost::lock_guard<boost::shared_mutex> lock(mutex); - if(oldBackend == backend) - backend = denyBackend; + std::map<std::string, boost::shared_ptr<AuthBackend> >::iterator backendIt = backends.find(backend->getMethodName()); + if(backendIt == backends.end() || backendIt->second != backend) + return; + + methods.erase(backend->getMethodName()); + backends.erase(backendIt); } -std::vector<std::string> AuthManager::getMethods() { +std::set<std::string> AuthManager::getMethods() { boost::shared_lock<boost::shared_mutex> lock(mutex); - return backend->getMethods(); + return methods; } std::vector<std::string> AuthManager::getSubMethods(const std::string &method) throw(Core::Exception) { boost::shared_lock<boost::shared_mutex> lock(mutex); - return backend->getSubMethods(method); + std::map<std::string, boost::shared_ptr<AuthBackend> >::iterator backend = backends.find(method); + if(backend == backends.end()) + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + + if(!provider) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + return backend->second->getSubMethods(provider); } boost::shared_ptr<AuthContext> AuthManager::authenticate(const std::string &method, const std::string &subMethod, const std::string &user, @@ -58,7 +81,14 @@ boost::shared_ptr<AuthContext> AuthManager::authenticate(const std::string &meth response.clear(); - return backend->authenticate(method, subMethod, user, data, response, context); + std::map<std::string, boost::shared_ptr<AuthBackend> >::iterator backend = backends.find(method); + if(backend == backends.end()) + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + + if(!provider) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + return backend->second->authenticate(provider, subMethod, user, data, response, context); } } diff --git a/src/Common/AuthManager.h b/src/Common/AuthManager.h index 65e1fd3..e45762a 100644 --- a/src/Common/AuthManager.h +++ b/src/Common/AuthManager.h @@ -22,14 +22,13 @@ #include "export.h" -#include "AuthBackend.h" #include "AuthContext.h" #include <Core/Exception.h> -#include <vector> +#include <map> +#include <set> -#include <boost/cstdint.hpp> #include <boost/noncopyable.hpp> #include <boost/shared_ptr.hpp> @@ -40,43 +39,30 @@ namespace Common { class Application; +class AuthBackend; +class AuthProvider; + class MAD_COMMON_EXPORT AuthManager : private boost::noncopyable { private: friend class Application; - class DenyBackend : public AuthBackend { - private: - const static std::vector<std::string> methods; - - protected: - virtual const std::vector<std::string>& getMethods() const { - return methods; - } - - virtual const std::vector<std::string>& getSubMethods(const std::string& /*method*/) const throw(Core::Exception) { - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); - } + boost::shared_ptr<AuthProvider> provider; - virtual boost::shared_ptr<AuthContext> authenticate(const std::string& /*method*/, const std::string& /*subMethod*/, - const std::string& /*user*/, const std::vector<boost::uint8_t>& /*data*/, std::vector<boost::uint8_t>& /*response*/, - boost::shared_ptr<AuthContext> /*context*/) throw(Core::Exception) { - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); - } - }; - - boost::shared_ptr<DenyBackend> denyBackend; - - boost::shared_ptr<AuthBackend> backend; + std::set<std::string> methods; + std::map<std::string, boost::shared_ptr<AuthBackend> > backends; boost::shared_mutex mutex; - AuthManager() : denyBackend(new DenyBackend), backend(denyBackend) {} + AuthManager() {} public: - void registerBackend(boost::shared_ptr<AuthBackend> newBackend); - void unregisterBackend(boost::shared_ptr<AuthBackend> oldBackend); + void setProvider(boost::shared_ptr<AuthProvider> newProvider); + void unsetProvider(boost::shared_ptr<AuthProvider> oldProvider); + + void registerBackend(boost::shared_ptr<AuthBackend> backend); + void unregisterBackend(boost::shared_ptr<AuthBackend> backend); - std::vector<std::string> getMethods(); + std::set<std::string> getMethods(); std::vector<std::string> getSubMethods(const std::string &method) throw(Core::Exception); boost::shared_ptr<AuthContext> authenticate(const std::string &method, const std::string &subMethod, diff --git a/src/Common/AuthProvider.h b/src/Common/AuthProvider.h new file mode 100644 index 0000000..aaf8f10 --- /dev/null +++ b/src/Common/AuthProvider.h @@ -0,0 +1,52 @@ +/* + * AuthProvider.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_COMMON_AUTHPROVIDER_H_ +#define MAD_COMMON_AUTHPROVIDER_H_ + +#include <Core/Exception.h> + +#include <vector> + +#include <boost/cstdint.hpp> + +namespace Mad { +namespace Common { + +class AuthProvider { + public: + virtual const std::vector<std::string>& getHashes() const = 0; + + virtual bool checkPassword(const std::string &user, const std::vector<boost::uint8_t> &data, const std::string &hash) throw(Core::Exception) { + std::vector<boost::uint8_t> password = getPassword(user, hash); + + return (!password.empty() && data.size() == password.size() && std::equal(data.begin(), data.end(), password.begin())); + } + + virtual std::vector<boost::uint8_t> getPassword(const std::string& /*user*/, const std::string& /*hash*/) throw(Core::Exception) { + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + } + + virtual ~AuthProvider() {} +}; + +} +} + +#endif /* MAD_COMMON_AUTHPROVIDER_H_ */ diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index e370c54..a92648b 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -22,6 +22,7 @@ mad_library(Common AuthBackend.h AuthContext.h AuthManager.cpp AuthManager.h + AuthProvider.h Base64Encoder.cpp Base64Encoder.h ClientConnection.cpp ClientConnection.h Connection.cpp Connection.h diff --git a/src/Server/RequestHandlers/ConnectionRequestHandlerGroup.cpp b/src/Server/RequestHandlers/ConnectionRequestHandlerGroup.cpp index 7607171..2a091e6 100644 --- a/src/Server/RequestHandlers/ConnectionRequestHandlerGroup.cpp +++ b/src/Server/RequestHandlers/ConnectionRequestHandlerGroup.cpp @@ -37,17 +37,17 @@ void ConnectionRequestHandlerGroup::handleAuthMethodRequest(boost::shared_ptr<co Common::XmlPacket::List *list = ret->createList("methods"); - const std::vector<std::string> &methods = application->getAuthManager()->getMethods(); + std::set<std::string> methods = application->getAuthManager()->getMethods(); - for(std::vector<std::string>::const_iterator method = methods.begin(); method != methods.end(); ++method) { + for(std::set<std::string>::iterator method = methods.begin(); method != methods.end(); ++method) { Common::XmlPacket::List::iterator entry = list->addEntry(); entry->set("name", *method); Common::XmlPacket::List *subList = entry->createList("subMethods"); - const std::vector<std::string> &subMethods = application->getAuthManager()->getSubMethods(*method); + std::vector<std::string> subMethods = application->getAuthManager()->getSubMethods(*method); - for(std::vector<std::string>::const_iterator subMethod = subMethods.begin(); subMethod != subMethods.end(); ++subMethod) { + for(std::vector<std::string>::iterator subMethod = subMethods.begin(); subMethod != subMethods.end(); ++subMethod) { Common::XmlPacket::List::iterator subEntry = subList->addEntry(); subEntry->set("name", *subMethod); diff --git a/src/mad-server.conf b/src/mad-server.conf index 88213e6..ff8249c 100644 --- a/src/mad-server.conf +++ b/src/mad-server.conf @@ -1,6 +1,7 @@ LoadModule "FileLogger" -LoadModule "AuthBackendFile" +LoadModule "AuthBackendPassword" +LoadModule "AuthProviderFile" LoadModule "SystemBackendPosix" LoadModule "SystemBackendProc" @@ -21,7 +22,7 @@ X509TrustFile ../Cert/ca-cert.pem X509CertFile ../Cert/cert.pem X509KeyFile ../Cert/key.pem -AuthBackendFile { +AuthProviderFile { File "users" } diff --git a/src/modules/AuthBackendFile/AuthBackendFile.cpp b/src/modules/AuthBackendFile/AuthBackendFile.cpp deleted file mode 100644 index b05b2db..0000000 --- a/src/modules/AuthBackendFile/AuthBackendFile.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/* - * 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 "AuthBackendFile.h" -#include <Core/ConfigEntry.h> -#include <Core/ConfigManager.h> - -#include <fstream> - -#include <boost/regex.hpp> - -namespace Mad { -namespace Modules { -namespace AuthBackendFile { - -void AuthBackendFile::readFile(const std::string &name) { - std::ifstream stream(name.c_str()); - if(!stream.good()) { - application->logf(Core::LoggerBase::LOG_WARNING, "AuthBackendFile: 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, "AuthBackendFile: Malformed line in file '%s'.", name.c_str()); - continue; - } - - - userMap.insert(std::make_pair(match[1].str(), match[2].str())); - } -} - -bool AuthBackendFile::handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/) { - if(!entry[0].getKey().matches("AuthBackendFile")) - 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; -} - -boost::shared_ptr<Common::AuthContext> AuthBackendFile::authenticate(const std::string &method, const std::string &subMethod, - const std::string &user, const std::vector<boost::uint8_t> &data, std::vector<boost::uint8_t>& /*response*/, - boost::shared_ptr<Common::AuthContext> context) throw(Core::Exception) { - if(method != "Password") - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - - if(context.get() != 0 && dynamic_cast<AuthContextFile*>(context.get()) == 0) - throw(Core::Exception(Core::Exception::INVALID_INPUT)); - - if(context.get() == 0) - context.reset(new AuthContextFile); - - std::map<std::string, std::string>::iterator userIt = userMap.find(user); - if(userIt == userMap.end()) - throw(Core::Exception(Core::Exception::AUTHENTICATION)); - - if(subMethod == "Clear") { - if(userIt->second != std::string(data.begin(), data.end())) - throw(Core::Exception(Core::Exception::AUTHENTICATION)); - } - else { - if(!std::equal(data.begin(), data.end(), Common::Hash::hash(userIt->second, subMethod).begin())) - throw(Core::Exception(Core::Exception::AUTHENTICATION)); - } - - - return context; -} - -} -} -} diff --git a/src/modules/AuthBackendFile/AuthBackendFile.h b/src/modules/AuthBackendFile/AuthBackendFile.h deleted file mode 100644 index 8098817..0000000 --- a/src/modules/AuthBackendFile/AuthBackendFile.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * AuthBackendFile.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_AUTHBACKENDFILE_AUTHBACKENDFILE_H_ -#define MAD_MODULES_AUTHBACKENDFILE_AUTHBACKENDFILE_H_ - -#include "../export.h" - -#include <Common/AuthBackend.h> -#include <Common/AuthContext.h> -#include <Common/Application.h> -#include <Common/Hash.h> - -#include <Core/Configurable.h> -#include <Core/ConfigManager.h> - -namespace Mad { -namespace Modules { -namespace AuthBackendFile { - -class MAD_MODULE_EXPORT AuthBackendFile : public Common::AuthBackend, private Core::Configurable, private boost::noncopyable { - private: - class AuthContextFile : public Common::AuthContext { - protected: - virtual bool isAuthenticated() const { - return true; - } - }; - - void readFile(const std::string &name); - - Common::Application *application; - - std::vector<std::string> methods; - std::vector<std::string> subMethods; - - std::map<std::string, std::string> userMap; - - protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/); - - virtual const std::vector<std::string>& getMethods() const { - return methods; - } - - virtual const std::vector<std::string>& getSubMethods(const std::string &method) const throw(Core::Exception) { - if(method != "Password") - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - - return subMethods; - } - - virtual boost::shared_ptr<Common::AuthContext> authenticate(const std::string &method, const std::string &subMethod, - const std::string &user, const std::vector<boost::uint8_t> &data, std::vector<boost::uint8_t> &response, - boost::shared_ptr<Common::AuthContext> context) throw(Core::Exception); - - public: - AuthBackendFile(Common::Application *application0) : application(application0) { - methods.push_back("Password"); - - const std::vector<std::string> &hashList = Common::Hash::getHashList(); - if(hashList.empty()) - subMethods.push_back("Clear"); - else - subMethods.push_back(hashList.front()); - - application->getConfigManager()->registerConfigurable(this); - } - - virtual ~AuthBackendFile() { - application->getConfigManager()->unregisterConfigurable(this); - } -}; - -} -} -} - -#endif /* MAD_MODULES_AUTHBACKENDFILE_AUTHBACKENDFILE_H_ */ diff --git a/src/modules/AuthBackendFile/CMakeLists.txt b/src/modules/AuthBackendFile/CMakeLists.txt deleted file mode 100644 index 3afd71f..0000000 --- a/src/modules/AuthBackendFile/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -include_directories(${INCLUDES}) - -mad_module(AuthBackendFile - Module.cpp Module.h - AuthBackendFile.cpp AuthBackendFile.h -) - -mad_module_libraries(AuthBackendFile) diff --git a/src/modules/AuthBackendPassword/AuthBackendPassword.cpp b/src/modules/AuthBackendPassword/AuthBackendPassword.cpp new file mode 100644 index 0000000..7e5aca9 --- /dev/null +++ b/src/modules/AuthBackendPassword/AuthBackendPassword.cpp @@ -0,0 +1,50 @@ +/* + * AuthBackendPassword.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 "AuthBackendPassword.h" + +namespace Mad { +namespace Modules { +namespace AuthBackendPassword { + +const std::string AuthBackendPassword::methodName = "Password"; + +boost::shared_ptr<Common::AuthContext> AuthBackendPassword::authenticate(boost::shared_ptr<Common::AuthProvider> provider, const std::string &subMethod, + const std::string &user, const std::vector<boost::uint8_t> &data, std::vector<boost::uint8_t>& /*response*/, + boost::shared_ptr<Common::AuthContext> context) throw(Core::Exception) { + if(context.get() != 0 && dynamic_cast<AuthContextPassword*>(context.get()) == 0) + throw(Core::Exception(Core::Exception::INVALID_INPUT)); + + std::vector<std::string> allowedMethods = getSubMethods(provider); + if(std::find(allowedMethods.begin(), allowedMethods.end(), subMethod) == allowedMethods.end()) + throw(Core::Exception(Core::Exception::INVALID_INPUT)); + + if(context.get() == 0) + context.reset(new AuthContextPassword); + + if(!provider->checkPassword(user, data, subMethod)) + throw(Core::Exception(Core::Exception::AUTHENTICATION)); + + + return context; +} + +} +} +} diff --git a/src/modules/AuthBackendPassword/AuthBackendPassword.h b/src/modules/AuthBackendPassword/AuthBackendPassword.h new file mode 100644 index 0000000..ac100c1 --- /dev/null +++ b/src/modules/AuthBackendPassword/AuthBackendPassword.h @@ -0,0 +1,71 @@ +/* + * AuthBackendPassword.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_AUTHBACKENDPASSWORD_AUTHBACKENDPASSWORD_H_ +#define MAD_MODULES_AUTHBACKENDPASSWORD_AUTHBACKENDPASSWORD_H_ + +#include "../export.h" + +#include <Common/AuthBackend.h> +#include <Common/AuthContext.h> +#include <Common/AuthProvider.h> +#include <Common/Application.h> + +namespace Mad { +namespace Modules { +namespace AuthBackendPassword { + +class MAD_MODULE_EXPORT AuthBackendPassword : public Common::AuthBackend, private boost::noncopyable { + private: + class AuthContextPassword : public Common::AuthContext { + protected: + virtual bool isAuthenticated() const { + return true; + } + }; + + Common::Application *application; + + static const std::string methodName; + + protected: + virtual const std::string& getMethodName() const { + return methodName; + } + + virtual std::vector<std::string> getSubMethods(boost::shared_ptr<Common::AuthProvider> provider) const { + std::vector<std::string> ret; + ret.push_back(provider->getHashes().front()); + + return ret; + } + + virtual boost::shared_ptr<Common::AuthContext> authenticate(boost::shared_ptr<Common::AuthProvider> provider, const std::string &subMethod, + const std::string &user, const std::vector<boost::uint8_t> &data, std::vector<boost::uint8_t> &response, + boost::shared_ptr<Common::AuthContext> context) throw(Core::Exception); + + public: + AuthBackendPassword(Common::Application *application0) : application(application0) {} +}; + +} +} +} + +#endif /* MAD_MODULES_AUTHBACKENDPASSWORD_AUTHBACKENDPASSWORD_H_ */ diff --git a/src/modules/AuthBackendPassword/CMakeLists.txt b/src/modules/AuthBackendPassword/CMakeLists.txt new file mode 100644 index 0000000..cad37a2 --- /dev/null +++ b/src/modules/AuthBackendPassword/CMakeLists.txt @@ -0,0 +1,8 @@ +include_directories(${INCLUDES}) + +mad_module(AuthBackendPassword + Module.cpp Module.h + AuthBackendPassword.cpp AuthBackendPassword.h +) + +mad_module_libraries(AuthBackendPassword) diff --git a/src/modules/AuthBackendPassword/Module.cpp b/src/modules/AuthBackendPassword/Module.cpp new file mode 100644 index 0000000..a46f6f6 --- /dev/null +++ b/src/modules/AuthBackendPassword/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* AuthBackendPassword_create(Mad::Common::Application *application) { + return new Mad::Modules::AuthBackendPassword::Module(application); +} + +} diff --git a/src/modules/AuthBackendFile/Module.h b/src/modules/AuthBackendPassword/Module.h index b0d14aa..e0a462e 100644 --- a/src/modules/AuthBackendFile/Module.h +++ b/src/modules/AuthBackendPassword/Module.h @@ -17,26 +17,26 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef MAD_MODULES_AUTHBACKENDFILE_MODULE_H_ -#define MAD_MODULES_AUTHBACKENDFILE_MODULE_H_ +#ifndef MAD_MODULES_AUTHBACKENDPASSWORD_MODULE_H_ +#define MAD_MODULES_AUTHBACKENDPASSWORD_MODULE_H_ -#include "AuthBackendFile.h" +#include "AuthBackendPassword.h" #include <Common/Module.h> #include <Common/AuthManager.h> namespace Mad { namespace Modules { -namespace AuthBackendFile { +namespace AuthBackendPassword { class Module : public Common::Module { private: Common::Application *application; - boost::shared_ptr<AuthBackendFile> backend; + boost::shared_ptr<AuthBackendPassword> backend; public: - Module(Common::Application *application0) : application(application0), backend(new AuthBackendFile(application)) { + Module(Common::Application *application0) : application(application0), backend(new AuthBackendPassword(application)) { application->getAuthManager()->registerBackend(backend); } @@ -49,4 +49,4 @@ class Module : public Common::Module { } } -#endif /* MAD_MODULES_AUTHBACKENDFILE_MODULE_H_ */ +#endif /* MAD_MODULES_AUTHBACKENDPASSWORD_MODULE_H_ */ 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/AuthBackendFile/Module.cpp b/src/modules/AuthProviderFile/Module.cpp index e5a9a18..aa84d22 100644 --- a/src/modules/AuthBackendFile/Module.cpp +++ b/src/modules/AuthProviderFile/Module.cpp @@ -23,8 +23,8 @@ extern "C" { -MAD_MODULE_EXPORT Mad::Common::Module* AuthBackendFile_create(Mad::Common::Application *application) { - return new Mad::Modules::AuthBackendFile::Module(application); +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_ */ diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt index e5890f4..0e198f8 100644 --- a/src/modules/CMakeLists.txt +++ b/src/modules/CMakeLists.txt @@ -33,7 +33,8 @@ macro(mad_module_libraries name) endif(WITH_${upper_name}) endmacro(mad_module_libraries) -add_subdirectory(AuthBackendFile) +add_subdirectory(AuthBackendPassword) +add_subdirectory(AuthProviderFile) add_subdirectory(FileLogger) if(UNIX) |