From 82ef58fb3d0bdf6ce7d13f42cca30d03b24973c6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 27 Aug 2009 17:27:58 +0200 Subject: =?UTF-8?q?AuthProvider=20hinzugef=C3=BCgt=20AuthBackendFile=20in?= =?UTF-8?q?=20AuthProviderFile=20und=20AuthBackendPassword=20aufgeteilt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/AuthBackend.h | 11 ++- src/Common/AuthManager.cpp | 50 ++++++++-- src/Common/AuthManager.h | 44 +++------ src/Common/AuthProvider.h | 52 ++++++++++ src/Common/CMakeLists.txt | 1 + .../ConnectionRequestHandlerGroup.cpp | 8 +- src/mad-server.conf | 5 +- src/modules/AuthBackendFile/AuthBackendFile.cpp | 108 --------------------- src/modules/AuthBackendFile/AuthBackendFile.h | 95 ------------------ src/modules/AuthBackendFile/CMakeLists.txt | 8 -- src/modules/AuthBackendFile/Module.cpp | 30 ------ src/modules/AuthBackendFile/Module.h | 52 ---------- .../AuthBackendPassword/AuthBackendPassword.cpp | 50 ++++++++++ .../AuthBackendPassword/AuthBackendPassword.h | 71 ++++++++++++++ src/modules/AuthBackendPassword/CMakeLists.txt | 8 ++ src/modules/AuthBackendPassword/Module.cpp | 30 ++++++ src/modules/AuthBackendPassword/Module.h | 52 ++++++++++ src/modules/AuthProviderFile/AuthProviderFile.cpp | 78 +++++++++++++++ src/modules/AuthProviderFile/AuthProviderFile.h | 80 +++++++++++++++ src/modules/AuthProviderFile/CMakeLists.txt | 8 ++ src/modules/AuthProviderFile/Module.cpp | 30 ++++++ src/modules/AuthProviderFile/Module.h | 52 ++++++++++ src/modules/CMakeLists.txt | 3 +- 23 files changed, 582 insertions(+), 344 deletions(-) create mode 100644 src/Common/AuthProvider.h delete mode 100644 src/modules/AuthBackendFile/AuthBackendFile.cpp delete mode 100644 src/modules/AuthBackendFile/AuthBackendFile.h delete mode 100644 src/modules/AuthBackendFile/CMakeLists.txt delete mode 100644 src/modules/AuthBackendFile/Module.cpp delete mode 100644 src/modules/AuthBackendFile/Module.h create mode 100644 src/modules/AuthBackendPassword/AuthBackendPassword.cpp create mode 100644 src/modules/AuthBackendPassword/AuthBackendPassword.h create mode 100644 src/modules/AuthBackendPassword/CMakeLists.txt create mode 100644 src/modules/AuthBackendPassword/Module.cpp create mode 100644 src/modules/AuthBackendPassword/Module.h create mode 100644 src/modules/AuthProviderFile/AuthProviderFile.cpp create mode 100644 src/modules/AuthProviderFile/AuthProviderFile.h create mode 100644 src/modules/AuthProviderFile/CMakeLists.txt create mode 100644 src/modules/AuthProviderFile/Module.cpp create mode 100644 src/modules/AuthProviderFile/Module.h 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& getMethods() const = 0; - virtual const std::vector& getSubMethods(const std::string &method) const throw(Core::Exception) = 0; + virtual const std::string& getMethodName() const = 0; + virtual std::vector getSubMethods(boost::shared_ptr provider) const = 0; - virtual boost::shared_ptr authenticate(const std::string& /*method*/, const std::string& /*subMethod*/, - const std::string& /*user*/, const std::vector& /*data*/, std::vector& /*response*/, - boost::shared_ptr /*context*/) throw(Core::Exception) = 0; + virtual boost::shared_ptr authenticate(boost::shared_ptr provider, const std::string &subMethod, + const std::string &user, const std::vector &data, std::vector &response, + boost::shared_ptr 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 AuthManager::DenyBackend::methods; +void AuthManager::setProvider(boost::shared_ptr newProvider) { + boost::lock_guard lock(mutex); + + provider = newProvider; +} + +void AuthManager::unsetProvider(boost::shared_ptr oldProvider) { + boost::lock_guard lock(mutex); + + if(oldProvider == provider) + provider.reset(); +} -void AuthManager::registerBackend(boost::shared_ptr newBackend) { +void AuthManager::registerBackend(boost::shared_ptr backend) { boost::lock_guard lock(mutex); - backend = newBackend; + methods.insert(backend->getMethodName()); + backends.insert(std::make_pair(backend->getMethodName(), backend)); } -void AuthManager::unregisterBackend(boost::shared_ptr oldBackend) { +void AuthManager::unregisterBackend(boost::shared_ptr backend) { boost::lock_guard lock(mutex); - if(oldBackend == backend) - backend = denyBackend; + std::map >::iterator backendIt = backends.find(backend->getMethodName()); + if(backendIt == backends.end() || backendIt->second != backend) + return; + + methods.erase(backend->getMethodName()); + backends.erase(backendIt); } -std::vector AuthManager::getMethods() { +std::set AuthManager::getMethods() { boost::shared_lock lock(mutex); - return backend->getMethods(); + return methods; } std::vector AuthManager::getSubMethods(const std::string &method) throw(Core::Exception) { boost::shared_lock lock(mutex); - return backend->getSubMethods(method); + std::map >::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 AuthManager::authenticate(const std::string &method, const std::string &subMethod, const std::string &user, @@ -58,7 +81,14 @@ boost::shared_ptr AuthManager::authenticate(const std::string &meth response.clear(); - return backend->authenticate(method, subMethod, user, data, response, context); + std::map >::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 -#include +#include +#include -#include #include #include @@ -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 methods; - - protected: - virtual const std::vector& getMethods() const { - return methods; - } - - virtual const std::vector& getSubMethods(const std::string& /*method*/) const throw(Core::Exception) { - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); - } + boost::shared_ptr provider; - virtual boost::shared_ptr authenticate(const std::string& /*method*/, const std::string& /*subMethod*/, - const std::string& /*user*/, const std::vector& /*data*/, std::vector& /*response*/, - boost::shared_ptr /*context*/) throw(Core::Exception) { - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); - } - }; - - boost::shared_ptr denyBackend; - - boost::shared_ptr backend; + std::set methods; + std::map > backends; boost::shared_mutex mutex; - AuthManager() : denyBackend(new DenyBackend), backend(denyBackend) {} + AuthManager() {} public: - void registerBackend(boost::shared_ptr newBackend); - void unregisterBackend(boost::shared_ptr oldBackend); + void setProvider(boost::shared_ptr newProvider); + void unsetProvider(boost::shared_ptr oldProvider); + + void registerBackend(boost::shared_ptr backend); + void unregisterBackend(boost::shared_ptr backend); - std::vector getMethods(); + std::set getMethods(); std::vector getSubMethods(const std::string &method) throw(Core::Exception); boost::shared_ptr 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 + * + * 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 . + */ + +#ifndef MAD_COMMON_AUTHPROVIDER_H_ +#define MAD_COMMON_AUTHPROVIDER_H_ + +#include + +#include + +#include + +namespace Mad { +namespace Common { + +class AuthProvider { + public: + virtual const std::vector& getHashes() const = 0; + + virtual bool checkPassword(const std::string &user, const std::vector &data, const std::string &hash) throw(Core::Exception) { + std::vector password = getPassword(user, hash); + + return (!password.empty() && data.size() == password.size() && std::equal(data.begin(), data.end(), password.begin())); + } + + virtual std::vector 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_ptrcreateList("methods"); - const std::vector &methods = application->getAuthManager()->getMethods(); + std::set methods = application->getAuthManager()->getMethods(); - for(std::vector::const_iterator method = methods.begin(); method != methods.end(); ++method) { + for(std::set::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 &subMethods = application->getAuthManager()->getSubMethods(*method); + std::vector subMethods = application->getAuthManager()->getSubMethods(*method); - for(std::vector::const_iterator subMethod = subMethods.begin(); subMethod != subMethods.end(); ++subMethod) { + for(std::vector::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 - * - * 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 . - */ - -#include "AuthBackendFile.h" -#include -#include - -#include - -#include - -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 AuthBackendFile::authenticate(const std::string &method, const std::string &subMethod, - const std::string &user, const std::vector &data, std::vector& /*response*/, - boost::shared_ptr context) throw(Core::Exception) { - if(method != "Password") - throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); - - if(context.get() != 0 && dynamic_cast(context.get()) == 0) - throw(Core::Exception(Core::Exception::INVALID_INPUT)); - - if(context.get() == 0) - context.reset(new AuthContextFile); - - std::map::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 - * - * 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 . - */ - -#ifndef MAD_MODULES_AUTHBACKENDFILE_AUTHBACKENDFILE_H_ -#define MAD_MODULES_AUTHBACKENDFILE_AUTHBACKENDFILE_H_ - -#include "../export.h" - -#include -#include -#include -#include - -#include -#include - -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 methods; - std::vector subMethods; - - std::map userMap; - - protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/); - - virtual const std::vector& getMethods() const { - return methods; - } - - virtual const std::vector& 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 authenticate(const std::string &method, const std::string &subMethod, - const std::string &user, const std::vector &data, std::vector &response, - boost::shared_ptr context) throw(Core::Exception); - - public: - AuthBackendFile(Common::Application *application0) : application(application0) { - methods.push_back("Password"); - - const std::vector &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/AuthBackendFile/Module.cpp b/src/modules/AuthBackendFile/Module.cpp deleted file mode 100644 index e5a9a18..0000000 --- a/src/modules/AuthBackendFile/Module.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Module.cpp - * - * Copyright (C) 2009 Matthias Schiffer - * - * 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 . - */ - -#include "../export.h" - -#include "Module.h" - -extern "C" { - -MAD_MODULE_EXPORT Mad::Common::Module* AuthBackendFile_create(Mad::Common::Application *application) { - return new Mad::Modules::AuthBackendFile::Module(application); -} - -} diff --git a/src/modules/AuthBackendFile/Module.h b/src/modules/AuthBackendFile/Module.h deleted file mode 100644 index b0d14aa..0000000 --- a/src/modules/AuthBackendFile/Module.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Module.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * 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 . - */ - -#ifndef MAD_MODULES_AUTHBACKENDFILE_MODULE_H_ -#define MAD_MODULES_AUTHBACKENDFILE_MODULE_H_ - -#include "AuthBackendFile.h" - -#include -#include - -namespace Mad { -namespace Modules { -namespace AuthBackendFile { - -class Module : public Common::Module { - private: - Common::Application *application; - - boost::shared_ptr backend; - - public: - Module(Common::Application *application0) : application(application0), backend(new AuthBackendFile(application)) { - application->getAuthManager()->registerBackend(backend); - } - - virtual ~Module() { - application->getAuthManager()->unregisterBackend(backend); - } -}; - -} -} -} - -#endif /* MAD_MODULES_AUTHBACKENDFILE_MODULE_H_ */ 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 + * + * 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 . + */ + +#include "AuthBackendPassword.h" + +namespace Mad { +namespace Modules { +namespace AuthBackendPassword { + +const std::string AuthBackendPassword::methodName = "Password"; + +boost::shared_ptr AuthBackendPassword::authenticate(boost::shared_ptr provider, const std::string &subMethod, + const std::string &user, const std::vector &data, std::vector& /*response*/, + boost::shared_ptr context) throw(Core::Exception) { + if(context.get() != 0 && dynamic_cast(context.get()) == 0) + throw(Core::Exception(Core::Exception::INVALID_INPUT)); + + std::vector 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 + * + * 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 . + */ + +#ifndef MAD_MODULES_AUTHBACKENDPASSWORD_AUTHBACKENDPASSWORD_H_ +#define MAD_MODULES_AUTHBACKENDPASSWORD_AUTHBACKENDPASSWORD_H_ + +#include "../export.h" + +#include +#include +#include +#include + +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 getSubMethods(boost::shared_ptr provider) const { + std::vector ret; + ret.push_back(provider->getHashes().front()); + + return ret; + } + + virtual boost::shared_ptr authenticate(boost::shared_ptr provider, const std::string &subMethod, + const std::string &user, const std::vector &data, std::vector &response, + boost::shared_ptr 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 + * + * 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 . + */ + +#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/AuthBackendPassword/Module.h b/src/modules/AuthBackendPassword/Module.h new file mode 100644 index 0000000..e0a462e --- /dev/null +++ b/src/modules/AuthBackendPassword/Module.h @@ -0,0 +1,52 @@ +/* + * Module.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_MODULES_AUTHBACKENDPASSWORD_MODULE_H_ +#define MAD_MODULES_AUTHBACKENDPASSWORD_MODULE_H_ + +#include "AuthBackendPassword.h" + +#include +#include + +namespace Mad { +namespace Modules { +namespace AuthBackendPassword { + +class Module : public Common::Module { + private: + Common::Application *application; + + boost::shared_ptr backend; + + public: + Module(Common::Application *application0) : application(application0), backend(new AuthBackendPassword(application)) { + application->getAuthManager()->registerBackend(backend); + } + + virtual ~Module() { + application->getAuthManager()->unregisterBackend(backend); + } +}; + +} +} +} + +#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 + * + * 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 . + */ + +#include "AuthProviderFile.h" +#include + +#include + +#include + +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 + * + * 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 . + */ + +#ifndef MAD_MODULES_AUTHPROVIDERFILE_AUTHPROVIDERFILE_H_ +#define MAD_MODULES_AUTHPROVIDERFILE_AUTHPROVIDERFILE_H_ + +#include "../export.h" + +#include +#include +#include + +#include +#include + +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 userMap; + + std::vector hashes; + + protected: + virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/); + + virtual const std::vector& getHashes() const { + return hashes; + } + + virtual std::vector getPassword(const std::string &user, const std::string &hash) throw(Core::Exception) { + std::map::iterator userIt = userMap.find(user); + if(userIt == userMap.end()) + return std::vector(); + + if(hash == "Clear") + return std::vector(userIt->second.begin(), userIt->second.end()); + else + return Common::Hash::hash(std::vector(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 + * + * 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 . + */ + +#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 + * + * 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 . + */ + +#ifndef MAD_MODULES_AUTHPROVIDERFILE_MODULE_H_ +#define MAD_MODULES_AUTHPROVIDERFILE_MODULE_H_ + +#include "AuthProviderFile.h" + +#include +#include + +namespace Mad { +namespace Modules { +namespace AuthProviderFile { + +class Module : public Common::Module { + private: + Common::Application *application; + + boost::shared_ptr 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) -- cgit v1.2.3