From 84a5ceeb7db03d75425d72e8a23a0bb0f267bc01 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 24 Aug 2009 03:06:32 +0200 Subject: =?UTF-8?q?Hash-Klasse=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 6 +- FindMhash.cmake | 16 ++++ .../Authenticators/PasswordAuthenticator.cpp | 3 +- src/Client/CommandParser.cpp | 2 +- src/Client/InformationManager.cpp | 2 +- src/Client/SystemCommands.cpp | 8 +- src/Client/UserCommands.cpp | 26 +++---- src/Common/AuthBackend.h | 2 +- src/Common/AuthManager.cpp | 4 +- src/Common/AuthManager.h | 4 +- src/Common/CMakeLists.txt | 3 +- src/Common/Hash.cpp | 65 ++++++++++++++++ src/Common/Hash.h | 91 ++++++++++++++++++++++ src/Common/UserManager.cpp | 20 ++--- src/Core/Exception.cpp | 42 +++++----- src/Core/Exception.h | 21 +++-- src/Server/ConnectionManager.cpp | 8 +- src/Server/ConnectionManager.h | 4 +- .../ConnectionRequestHandlerGroup.cpp | 6 +- src/mad.cpp | 2 +- src/madc.cpp | 2 +- src/modules/AuthBackendFile/AuthBackendFile.cpp | 4 +- src/modules/AuthBackendFile/AuthBackendFile.h | 2 +- 23 files changed, 264 insertions(+), 79 deletions(-) create mode 100644 FindMhash.cmake create mode 100644 src/Common/Hash.cpp create mode 100644 src/Common/Hash.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 729ff76..70ef036 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(MAD) set(CMAKE_MODULE_PATH ${MAD_SOURCE_DIR}) find_package(LibXml2 REQUIRED) +find_package(Mhash REQUIRED) find_package(OpenSSL REQUIRED) find_package(Boost REQUIRED date_time filesystem regex signals system thread) find_package(MySQL) @@ -13,6 +14,7 @@ find_package(Editline) if(NOT EDITLINE_FOUND) find_package(Readline REQUIRED) set(EDITLINE_LIBRARY ${READLINE_LIBRARY}) + set(EDITLINE_INCLUDE_DIR ${READLINE_INCLUDE_DIR}) endif(NOT EDITLINE_FOUND) if(WIN32) @@ -36,9 +38,9 @@ set(INCLUDES ${MAD_SOURCE_DIR}/src ${MAD_BINARY_DIR} ${LIBXML2_INCLUDE_DIR} - ${GNUTLS_INCLUDE_DIRS} + ${MHASH_INCLUDE_DIR} ${DL_INCLUDE_DIR} - ${READLINE_INCLUDE_DIR} + ${EDITLINE_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR} ) diff --git a/FindMhash.cmake b/FindMhash.cmake new file mode 100644 index 0000000..5326679 --- /dev/null +++ b/FindMhash.cmake @@ -0,0 +1,16 @@ +FIND_PATH(MHASH_INCLUDE_DIR mhash.h) +FIND_LIBRARY(MHASH_LIBRARY NAMES mhash) + +IF (MHASH_INCLUDE_DIR AND MHASH_LIBRARY) + SET(MHASH_FOUND TRUE) +ENDIF (MHASH_INCLUDE_DIR AND MHASH_LIBRARY) + +IF (MHASH_FOUND) + IF (NOT Mhash_FIND_QUIETLY) + MESSAGE(STATUS "Found Mhash: ${MHASH_LIBRARY}") + ENDIF (NOT Mhash_FIND_QUIETLY) +ELSE (MHASH_FOUND) + IF (Mhash_FIND_REQUIRED) + MESSAGE(FATAL_ERROR "Could not find Mhash") + ENDIF (Mhash_FIND_REQUIRED) +ENDIF (MHASH_FOUND) diff --git a/src/Client/Authenticators/PasswordAuthenticator.cpp b/src/Client/Authenticators/PasswordAuthenticator.cpp index 44608db..3aa9f41 100644 --- a/src/Client/Authenticators/PasswordAuthenticator.cpp +++ b/src/Client/Authenticators/PasswordAuthenticator.cpp @@ -31,8 +31,7 @@ void PasswordAuthenticator::PasswordAuthRequest::sendRequest() { packet.set("method", "Password"); packet.set("user", username); - std::vector challenge(password.begin(), password.end()); - packet.set("challenge", challenge); + packet.set("data", std::vector(password.begin(), password.end())); sendPacket(packet); } diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 56ecbba..ed0f47e 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -205,7 +205,7 @@ void CommandParser::exitCommand(const std::vector &/*args*/) { std::pair, Core::Exception> result = request->getResult(); if(result.second) - std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << result.second.what() << "." << std::endl; else disconnect = true; } diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index 59d8836..108a2a5 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -72,7 +72,7 @@ void InformationManager::daemonListRequestFinished(boost::shared_ptr lock(mutex); if(!packet || error) { - application->logf(Core::LoggerBase::LOG_CRITICAL, "Host list request failed: %s", error.strerror().c_str()); + application->logf(Core::LoggerBase::LOG_CRITICAL, "Host list request failed: %s", error.what()); } else { const Common::XmlPacket::List *list = packet->getList("hosts"); diff --git a/src/Client/SystemCommands.cpp b/src/Client/SystemCommands.cpp index 1cc9bc4..f1bcbbd 100644 --- a/src/Client/SystemCommands.cpp +++ b/src/Client/SystemCommands.cpp @@ -168,7 +168,7 @@ void SystemCommands::fsinfoCommand(CommandParser *commandParser, const std::vect std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << result.second.what() << "." << std::endl; } else { if(args.size() == 1) @@ -205,7 +205,7 @@ void SystemCommands::rebootCommand(CommandParser *commandParser, const std::vect std::pair, Core::Exception> result = (*request)->getResult(); if(result.second) - std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << result.second.what() << "." << std::endl; } } @@ -234,7 +234,7 @@ void SystemCommands::shutdownCommand(CommandParser *commandParser, const std::ve std::pair, Core::Exception> result = (*request)->getResult(); if(result.second) - std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << result.second.what() << "." << std::endl; } } @@ -257,7 +257,7 @@ void SystemCommands::statusCommand(CommandParser *commandParser, const std::vect std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { - std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << result.second.what() << "." << std::endl; } else { if(args.size() == 1) diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp index 166a54d..a1d190a 100644 --- a/src/Client/UserCommands.cpp +++ b/src/Client/UserCommands.cpp @@ -89,7 +89,7 @@ void UserCommands::listUsersCommand(CommandParser *commandParser, const std::vec std::cout << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -132,7 +132,7 @@ void UserCommands::userInfoCommand(CommandParser *commandParser, const std::vect } } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -154,7 +154,7 @@ void UserCommands::listGroupsCommand(CommandParser *commandParser, const std::ve std::cout << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -197,7 +197,7 @@ void UserCommands::groupInfoCommand(CommandParser *commandParser, const std::vec } } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -238,7 +238,7 @@ void UserCommands::addUserCommand(CommandParser *commandParser, const std::vecto std::cout << "User added." << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -286,7 +286,7 @@ void UserCommands::updateUserCommand(CommandParser *commandParser, const std::ve std::cout << "User updated." << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -316,7 +316,7 @@ void UserCommands::deleteUserCommand(CommandParser *commandParser, const std::ve std::cout << "User deleted." << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -346,7 +346,7 @@ void UserCommands::addGroupCommand(CommandParser *commandParser, const std::vect std::cout << "Group added." << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -383,7 +383,7 @@ void UserCommands::updateGroupCommand(CommandParser *commandParser, const std::v std::cout << "Group updated." << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -413,7 +413,7 @@ void UserCommands::deleteGroupCommand(CommandParser *commandParser, const std::v std::cout << "Group deleted." << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -450,7 +450,7 @@ void UserCommands::addUserToGroupCommand(CommandParser *commandParser, const std std::cout << "Added." << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -487,7 +487,7 @@ void UserCommands::deleteUserFromGroupCommand(CommandParser *commandParser, cons std::cout << "Removed." << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } @@ -525,7 +525,7 @@ void UserCommands::setPasswordCommand(CommandParser *commandParser, const std::v std::cout << "Password set." << std::endl; } catch(Core::Exception e) { - std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + std::cerr << "An error occurred during your request: " << e.what() << "." << std::endl; } } diff --git a/src/Common/AuthBackend.h b/src/Common/AuthBackend.h index e933856..53c7769 100644 --- a/src/Common/AuthBackend.h +++ b/src/Common/AuthBackend.h @@ -40,7 +40,7 @@ class AuthBackend { virtual const std::vector& getMethods() const = 0; virtual boost::shared_ptr authenticate(const std::string& /*method*/, const std::string& /*user*/, - const std::vector& /*challenge*/, std::vector& /*response*/, + const std::vector& /*data*/, std::vector& /*response*/, boost::shared_ptr /*context*/) throw(Core::Exception) = 0; public: diff --git a/src/Common/AuthManager.cpp b/src/Common/AuthManager.cpp index b27c29f..ffb8453 100644 --- a/src/Common/AuthManager.cpp +++ b/src/Common/AuthManager.cpp @@ -45,13 +45,13 @@ std::vector AuthManager::getMethods() { return backend->getMethods(); } -boost::shared_ptr AuthManager::authenticate(const std::string &method, const std::string &user, const std::vector &challenge, +boost::shared_ptr AuthManager::authenticate(const std::string &method, const std::string &user, const std::vector &data, std::vector &response, boost::shared_ptr context) throw(Core::Exception) { boost::lock_guard lock(mutex); response.clear(); - return backend->authenticate(method, user, challenge, response, context); + return backend->authenticate(method, user, data, response, context); } } diff --git a/src/Common/AuthManager.h b/src/Common/AuthManager.h index c773214..1516526 100644 --- a/src/Common/AuthManager.h +++ b/src/Common/AuthManager.h @@ -54,7 +54,7 @@ class MAD_COMMON_EXPORT AuthManager : private boost::noncopyable { } virtual boost::shared_ptr authenticate(const std::string& /*method*/, const std::string& /*user*/, - const std::vector& /*challenge*/, std::vector& /*response*/, + const std::vector& /*data*/, std::vector& /*response*/, boost::shared_ptr /*context*/) throw(Core::Exception) { throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); } @@ -74,7 +74,7 @@ class MAD_COMMON_EXPORT AuthManager : private boost::noncopyable { std::vector getMethods(); - boost::shared_ptr authenticate(const std::string &method, const std::string &user, const std::vector &challenge, + boost::shared_ptr authenticate(const std::string &method, const std::string &user, const std::vector &data, std::vector &response, boost::shared_ptr context = boost::shared_ptr()) throw(Core::Exception); }; diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index 13e1599..129c7de 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -25,6 +25,7 @@ mad_library(Common ClientConnection.cpp ClientConnection.h Connection.cpp Connection.h GroupInfo.h + Hash.cpp Hash.h HostInfo.h Module.h ModuleManager.cpp ModuleManager.h @@ -41,4 +42,4 @@ mad_library(Common UserManager.cpp UserManager.h XmlPacket.cpp XmlPacket.h ) -target_link_libraries(Common Net Core modules ${LIBXML2_LIBRARIES} ${DL_LIBRARY}) +target_link_libraries(Common Net Core modules ${LIBXML2_LIBRARIES} ${MHASH_LIBRARY} ${DL_LIBRARY}) diff --git a/src/Common/Hash.cpp b/src/Common/Hash.cpp new file mode 100644 index 0000000..8194ffb --- /dev/null +++ b/src/Common/Hash.cpp @@ -0,0 +1,65 @@ +/* + * Hash.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 "Hash.h" + +#include + +#include + +namespace Mad { +namespace Common { + +const Hash::Hashes Hash::hashes; + +Hash::Hashes::Hashes() { + addHash("SHA256", MHASH_SHA256); + addHash("Tiger192", MHASH_TIGER192); + addHash("HAVAL192", MHASH_HAVAL192); + addHash("SHA1", MHASH_SHA1); + addHash("Tiger160", MHASH_TIGER160); + addHash("HAVAL160", MHASH_HAVAL160); + addHash("RIPEMD-160", MHASH_RIPEMD160); + addHash("MD5", MHASH_MD5); + addHash("Tiger128", MHASH_TIGER128); + addHash("HAVAL128", MHASH_HAVAL128); +} + +std::vector Hash::hash(const std::vector &in, unsigned int method) throw (Core::Exception) { + MHASH mh; + + mh = mhash_init(static_cast(method)); + if(mh == MHASH_FAILED) + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); + + boost::scoped_array inArray(new boost::uint8_t[in.size()]); + std::copy(in.begin(), in.end(), inArray.get()); + + mhash(mh, inArray.get(), in.size()); + + std::size_t hashLength = mhash_get_block_size(static_cast(method)); + + boost::scoped_array outArray(new boost::uint8_t[hashLength]); + mhash_deinit(mh, outArray.get()); + + return std::vector(outArray.get(), outArray.get()+hashLength); +} + +} +} diff --git a/src/Common/Hash.h b/src/Common/Hash.h new file mode 100644 index 0000000..69b3318 --- /dev/null +++ b/src/Common/Hash.h @@ -0,0 +1,91 @@ +/* + * Hash.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_HASH_H_ +#define MAD_COMMON_HASH_H_ + +#include "export.h" + +#include + +#include +#include +#include + +#include + +namespace Mad { +namespace Common { + +class MAD_COMMON_EXPORT Hash { + private: + Hash(); + + class MAD_COMMON_EXPORT Hashes { + private: + std::map map; + std::vector list; + + void addHash(const std::string &name, unsigned id) { + map.insert(std::make_pair(name, id)); + list.push_back(name); + } + + public: + const std::vector& getList() const { + return list; + } + + const std::map& getMap() const { + return map; + } + + Hashes(); + }; + + static const Hashes hashes; + + public: + static const std::vector& getHashList() { + return hashes.getList(); + } + + static std::vector hash(const std::vector &in, unsigned int method) throw (Core::Exception); + + static std::vector hash(const std::vector &in, const std::string &method) throw (Core::Exception) { + std::map::const_iterator methodIt = hashes.getMap().find(method); + if(methodIt == hashes.getMap().end()) + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + + return hash(in, methodIt->second); + } + + static std::vector hash(const std::string &in, unsigned int method) throw (Core::Exception) { + return hash(std::vector(in.begin(), in.end()), method); + } + + static std::vector hash(const std::string &in, const std::string &method) throw (Core::Exception) { + return hash(std::vector(in.begin(), in.end()), method); + } +}; + +} +} + +#endif /* MAD_COMMON_HASH_H_ */ diff --git a/src/Common/UserManager.cpp b/src/Common/UserManager.cpp index d4467ee..d184044 100644 --- a/src/Common/UserManager.cpp +++ b/src/Common/UserManager.cpp @@ -234,7 +234,7 @@ void UserManager::addUser(const UserInfo &userInfo) throw(Core::Exception) { (*configBackend)->addUser(userInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -258,7 +258,7 @@ void UserManager::updateUser(unsigned long uid, const UserInfo &userInfo) throw( (*configBackend)->updateUser(*oldUserInfo, userInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -280,7 +280,7 @@ void UserManager::deleteUser(unsigned long uid) throw(Core::Exception) { (*configBackend)->deleteUser(*userInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -317,7 +317,7 @@ void UserManager::addGroup(const GroupInfo &groupInfo) throw(Core::Exception) { (*configBackend)->addGroup(groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -341,7 +341,7 @@ void UserManager::updateGroup(unsigned long gid, const GroupInfo &groupInfo) thr (*configBackend)->updateGroup(*oldGroupInfo, groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -363,7 +363,7 @@ void UserManager::deleteGroup(unsigned long gid) throw(Core::Exception) { (*configBackend)->deleteGroup(*groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -388,7 +388,7 @@ void UserManager::addUserToGroup(unsigned long uid, unsigned long gid) throw(Cor (*configBackend)->addUserToGroup(*userInfo, *groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -413,7 +413,7 @@ void UserManager::deleteUserFromGroup(unsigned long uid, unsigned long gid) thro (*configBackend)->deleteUserFromGroup(*userInfo, *groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -439,7 +439,7 @@ void UserManager::setPassword(unsigned long uid, const std::string &password) th exception = e; if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } for(std::set >::iterator configBackend = configBackends.begin(); configBackend != configBackends.end(); ++configBackend) { @@ -449,7 +449,7 @@ void UserManager::setPassword(unsigned long uid, const std::string &password) th } catch(Core::Exception e) { if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } diff --git a/src/Core/Exception.cpp b/src/Core/Exception.cpp index 0029613..3e9da4d 100644 --- a/src/Core/Exception.cpp +++ b/src/Core/Exception.cpp @@ -24,45 +24,45 @@ namespace Mad { namespace Core { -std::string Exception::strerror() const { - std::string ret; - - if(!where.empty()) - ret = where + ": "; +void Exception::updateWhatStr() { + if(where.empty()) + whatStr.clear(); + else + whatStr = where + ": "; switch(errorCode) { case SUCCESS: - return ret + "Success"; + whatStr += "Success"; case UNEXPECTED_PACKET: - return ret + "An unexpected packet was received"; + whatStr += "An unexpected packet was received"; case INVALID_ACTION: - return ret + "The action is invalid"; + whatStr += "The action is invalid"; case NOT_AVAILABLE: - return ret + "Not available"; + whatStr += "Not available"; case NOT_FINISHED: - return ret + "Not finished"; + whatStr += "Not finished"; case NOT_IMPLEMENTED: - return ret + "Not implemented"; + whatStr += "Not implemented"; case NOT_FOUND: - return ret + "Not found"; + whatStr += "Not found"; case INVALID_INPUT: - return ret + "Invalid input"; + whatStr += "Invalid input"; case PERMISSION: - return ret + "Permission denied"; + whatStr += "Permission denied"; case INTERNAL_ERRNO: - return ret + std::strerror(subCode); + whatStr += std::strerror(subCode); case INVALID_ADDRESS: - return ret + "Invalid address"; + whatStr += "Invalid address"; case ALREADY_IDENTIFIED: - return ret + "The host is already identified"; + whatStr += "The host is already identified"; case UNKNOWN_DAEMON: - return ret + "The daemon is unknown"; + whatStr += "The daemon is unknown"; case DUPLICATE_ENTRY: - return ret + "Duplicate entry"; + whatStr += "Duplicate entry"; case AUTHENTICATION: - return ret + "Authentication failure"; + whatStr += "Authentication failure"; default: - return ret + "Unknown error"; + whatStr += "Unknown error"; } } diff --git a/src/Core/Exception.h b/src/Core/Exception.h index fb6fae7..f12b3df 100644 --- a/src/Core/Exception.h +++ b/src/Core/Exception.h @@ -22,12 +22,13 @@ #include "export.h" +#include #include namespace Mad { namespace Core { -class MAD_CORE_EXPORT Exception { +class MAD_CORE_EXPORT Exception : public std::exception { public: enum ErrorCode { SUCCESS = 0x0000, UNEXPECTED_PACKET = 0x0001, INVALID_ACTION = 0x0002, NOT_AVAILABLE = 0x0003, NOT_FINISHED = 0x0004, NOT_IMPLEMENTED = 0x0005, @@ -46,18 +47,28 @@ class MAD_CORE_EXPORT Exception { long subCode; long subSubCode; + std::string whatStr; + + void updateWhatStr(); + public: Exception(const std::string &where0, ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0) - : where(where0), errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} - Exception(ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0) : errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} - virtual ~Exception() {} + : where(where0), errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) { + updateWhatStr(); + } + Exception(ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0) : errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) { + updateWhatStr(); + } + virtual ~Exception() throw () {} const std::string& getWhere() const {return where;} ErrorCode getErrorCode() const {return errorCode;} long getSubCode() const {return subCode;} long getSubSubCode() const {return subSubCode;} - std::string strerror() const; + virtual const char* what() const throw () { + return whatStr.c_str(); + } operator bool() const { return (errorCode != SUCCESS); diff --git a/src/Server/ConnectionManager.cpp b/src/Server/ConnectionManager.cpp index e52e8c4..c49e7e8 100644 --- a/src/Server/ConnectionManager.cpp +++ b/src/Server/ConnectionManager.cpp @@ -58,11 +58,11 @@ bool ConnectionManager::ServerConnection::disconnect() { } boost::shared_ptr ConnectionManager::ServerConnection::authenticate(const std::string &method, - const std::string &user, const std::vector &challenge, std::vector &response) { + const std::string &user, const std::vector &data, std::vector &response) { if(!isIdentified()) type = CLIENT; - authContext = application->getAuthManager()->authenticate(method, user, challenge, response, authContext); + authContext = application->getAuthManager()->authenticate(method, user, data, response, authContext); return authContext; } @@ -294,7 +294,7 @@ void ConnectionManager::identifyDaemonConnection(Common::Connection *con, const } boost::shared_ptr ConnectionManager::authenticateConnection(Common::Connection *con, const std::string &method, - const std::string &user, const std::vector &challenge, std::vector &response) { + const std::string &user, const std::vector &data, std::vector &response) { // TODO Logging ServerConnection *connection = dynamic_cast(con); @@ -305,7 +305,7 @@ boost::shared_ptr ConnectionManager::authenticateConn if(!connection->isIdentified()) connection->identify(); - return connection->authenticate(method, user, challenge, response); + return connection->authenticate(method, user, data, response); } std::vector ConnectionManager::getDaemonList() const { diff --git a/src/Server/ConnectionManager.h b/src/Server/ConnectionManager.h index e045a6a..067ce28 100644 --- a/src/Server/ConnectionManager.h +++ b/src/Server/ConnectionManager.h @@ -102,7 +102,7 @@ class MAD_SERVER_EXPORT ConnectionManager : public Core::Configurable, private b } boost::shared_ptr authenticate(const std::string &method, const std::string &user, - const std::vector &challenge, std::vector &response); + const std::vector &data, std::vector &response); }; friend class Application; @@ -143,7 +143,7 @@ class MAD_SERVER_EXPORT ConnectionManager : public Core::Configurable, private b void identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Core::Exception); boost::shared_ptr authenticateConnection(Common::Connection *con, const std::string &method, - const std::string &user, const std::vector &challenge, std::vector &response); + const std::string &user, const std::vector &data, std::vector &response); bool isAuthenticated(Common::Connection *con) const; diff --git a/src/Server/RequestHandlers/ConnectionRequestHandlerGroup.cpp b/src/Server/RequestHandlers/ConnectionRequestHandlerGroup.cpp index b59cc3d..37e2e79 100644 --- a/src/Server/RequestHandlers/ConnectionRequestHandlerGroup.cpp +++ b/src/Server/RequestHandlers/ConnectionRequestHandlerGroup.cpp @@ -51,10 +51,10 @@ void ConnectionRequestHandlerGroup::handleAuthRequest(boost::shared_ptr authContext = application->getConnectionManager()->authenticateConnection(connection, packet->get("method"), packet->get("user"), - packet->get&>("challenge"), response); + packet->get&>("data"), response); if(!response.empty()) - ret->set("response", response); + ret->set("data", response); if(authContext->isAuthenticated()) ret->setType("OK"); @@ -109,7 +109,7 @@ void ConnectionRequestHandlerGroup::handleLogRequest(boost::shared_ptrgetConnectionManager()->getDaemonName(connection)); } catch(Core::Exception &e) { - application->logf(Core::LoggerBase::LOG_ERROR, "Can't determine daemon name: %s", e.strerror().c_str()); + application->logf(Core::LoggerBase::LOG_ERROR, "Can't determine daemon name: %s", e.what()); } ret->setType("OK"); diff --git a/src/mad.cpp b/src/mad.cpp index 8a7b7ba..137328a 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -71,7 +71,7 @@ int main() { application.getRequestManager()->unregisterConnection(connection); } catch(Core::Exception &e) { - application.logf(Core::LoggerBase::LOG_CRITICAL, "Connection error: %s", e.strerror().c_str()); + application.logf(Core::LoggerBase::LOG_CRITICAL, "Connection error: %s", e.what()); } delete connection; diff --git a/src/madc.cpp b/src/madc.cpp index 1fa2112..23a9e45 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -116,7 +116,7 @@ int main(int argc, char *argv[]) { application.getRequestManager()->unregisterConnection(connection); } catch(Core::Exception &e) { - application.logf(Core::LoggerBase::LOG_CRITICAL, "Error: %s", e.strerror().c_str()); + application.logf(Core::LoggerBase::LOG_CRITICAL, "Error: %s", e.what()); } delete connection; diff --git a/src/modules/AuthBackendFile/AuthBackendFile.cpp b/src/modules/AuthBackendFile/AuthBackendFile.cpp index 8374101..ac7a1db 100644 --- a/src/modules/AuthBackendFile/AuthBackendFile.cpp +++ b/src/modules/AuthBackendFile/AuthBackendFile.cpp @@ -75,7 +75,7 @@ bool AuthBackendFile::handleConfigEntry(const Core::ConfigEntry &entry, bool /*h } boost::shared_ptr AuthBackendFile::authenticate(const std::string &method, const std::string &user, - const std::vector &challenge, std::vector& /*response*/, + const std::vector &data, std::vector& /*response*/, boost::shared_ptr context) throw(Core::Exception) { if(method != "Password") throw(Core::Exception(Core::Exception::INVALID_INPUT)); @@ -86,7 +86,7 @@ boost::shared_ptr AuthBackendFile::authenticate(const std:: if(context.get() == 0) context.reset(new AuthContextFile); - std::string password(challenge.begin(), challenge.end()); + std::string password(data.begin(), data.end()); std::map::iterator userIt = userMap.find(user); if(userIt == userMap.end() || password != userIt->second) diff --git a/src/modules/AuthBackendFile/AuthBackendFile.h b/src/modules/AuthBackendFile/AuthBackendFile.h index 81e7d12..90f572c 100644 --- a/src/modules/AuthBackendFile/AuthBackendFile.h +++ b/src/modules/AuthBackendFile/AuthBackendFile.h @@ -58,7 +58,7 @@ class MAD_MODULE_EXPORT AuthBackendFile : public Common::AuthBackend, private Co } virtual boost::shared_ptr authenticate(const std::string &method, const std::string &user, - const std::vector &challenge, std::vector &response, + const std::vector &data, std::vector &response, boost::shared_ptr context) throw(Core::Exception); public: -- cgit v1.2.3