From 86e5f80837ad55932f2469d79d9e6b6bb07cf5ed Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 28 Sep 2009 19:09:22 +0200 Subject: Implemented new ConfigManager --- src/modules/AuthProviderFile/AuthProviderFile.cpp | 34 +---- src/modules/AuthProviderFile/AuthProviderFile.h | 3 +- src/modules/FileLogger/Module.cpp | 57 ++++---- src/modules/FileLogger/Module.h | 4 +- .../StorageBackendFile/StorageBackendFile.cpp | 21 +-- .../StorageBackendFile/StorageBackendFile.h | 3 +- .../UserConfigBackendHome.cpp | 42 ++---- .../UserConfigBackendHome/UserConfigBackendHome.h | 4 +- .../UserConfigBackendKrb5.cpp | 43 +----- .../UserConfigBackendKrb5/UserConfigBackendKrb5.h | 3 +- .../UserDBBackendMysql/UserDBBackendMysql.cpp | 146 ++++----------------- .../UserDBBackendMysql/UserDBBackendMysql.h | 5 +- 12 files changed, 81 insertions(+), 284 deletions(-) (limited to 'src/modules') diff --git a/src/modules/AuthProviderFile/AuthProviderFile.cpp b/src/modules/AuthProviderFile/AuthProviderFile.cpp index 177d633..4522092 100644 --- a/src/modules/AuthProviderFile/AuthProviderFile.cpp +++ b/src/modules/AuthProviderFile/AuthProviderFile.cpp @@ -88,33 +88,9 @@ void AuthProviderFile::readFile(const std::string &name) { } } -bool AuthProviderFile::handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/) { - if(!entry[0].getKey().matches("AuthProviderFile")) - return false; +void AuthProviderFile::configure() { + filehash = application->getConfigManager()->get("AuthProviderFile.Hash"); - if(entry[1].isEmpty()) - return true; - - if(entry[1].getKey().matches("Hash")) { - if(entry[2].isEmpty()) { - filehash = entry[1][0]; - - if(!Common::Hash::isHashSupported(filehash)) - application->logf(Core::Logger::LOG_WARNING, "AuthProviderFile: Unsupported hash '%s'", filehash.toLocale().c_str()); - } - } - else if(entry[1].getKey().matches("File")) { - if(entry[2].isEmpty()) { - files.push_back(entry[1][0]); - } - } - else if(!entry[2].isEmpty()) - return false; - - return true; -} - -void AuthProviderFile::configFinished() { if(filehash.isEmpty() || filehash.matches("clear")) { hashes = Common::Hash::getHashList(); filehash.remove(); @@ -126,8 +102,10 @@ void AuthProviderFile::configFinished() { hashes.push_back("Clear"); - for(std::vector::iterator file = files.begin(); file != files.end(); ++file) - readFile(file->toLocale()); + std::vector fileEntries = application->getConfigManager()->getEntries("AuthProviderFile.File"); + for(std::vector::iterator fileEntry = fileEntries.begin(); fileEntry != fileEntries.end(); ++fileEntry) { + readFile((*fileEntry)->getValue().toLocale()); + } } bool AuthProviderFile::checkPassword(const Core::String &user, const std::vector &data, const Core::String &hash) throw(Core::Exception) { diff --git a/src/modules/AuthProviderFile/AuthProviderFile.h b/src/modules/AuthProviderFile/AuthProviderFile.h index 6ca907f..25a30b5 100644 --- a/src/modules/AuthProviderFile/AuthProviderFile.h +++ b/src/modules/AuthProviderFile/AuthProviderFile.h @@ -50,8 +50,7 @@ class MAD_MODULE_EXPORT AuthProviderFile : public Common::AuthProvider, private std::vector hashes; protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/); - virtual void configFinished(); + virtual void configure(); virtual const std::vector& getHashes() const { return hashes; diff --git a/src/modules/FileLogger/Module.cpp b/src/modules/FileLogger/Module.cpp index d8a302c..25a54b4 100644 --- a/src/modules/FileLogger/Module.cpp +++ b/src/modules/FileLogger/Module.cpp @@ -26,45 +26,38 @@ namespace Mad { namespace Modules { namespace FileLogger { -bool Module::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { - if(handled) - return false; +void Module::configure() { + std::vector entries = application->getConfigManager()->getEntries("Log"); - if(entry[0].getKey().matches("Log")) { - if(entry[0][0].matches("File")) { - if(entry[1].isEmpty()) { - if(!entry[0][1].isEmpty()) { - lastLogger.reset(new FileLogger(entry[0][1].toLocale())); + for(std::vector::iterator entry = entries.begin(); entry != entries.end(); ++entry) { + const std::vector &values = (*entry)->getValues(); - loggers.insert(lastLogger); - application->getLogManager()->registerLogger(lastLogger); - } - else { - lastLogger.reset(); - application->logf(Core::Logger::LOG_WARNING, "FileLogger: no filename given."); - } + if(values.size() < 2 || !values.front().matches("File")) + continue; - return true; - } - else if(entry[1].getKey().matches("Level")) { - if(entry[2].isEmpty()) { - try { - if(entry[1][0].matches("remote")) - lastLogger->setRemoteLevel(Core::LogManager::parseLevel(entry[1][1])); - else - lastLogger->setLevel(Core::LogManager::parseLevel(entry[1][0])); - } - catch(Core::Exception e) { - application->logf(Core::Logger::LOG_WARNING, "Unknown log level '%s'.", entry[1][0].toLocale().c_str()); - } + boost::shared_ptr logger(new FileLogger(values[1].toLocale())); + loggers.insert(logger); + application->getLogManager()->registerLogger(logger); + + bool remote = false; + for(std::vector::const_iterator value = values.begin()+2; value != values.end(); ++value) { + if(value->matches("remote")) + remote = true; + } - return true; - } + Core::String level = (*entry)->get("Level"); + if(!level.isEmpty()) { + try { + if(remote) + logger->setRemoteLevel(Core::LogManager::parseLevel(level)); + else + logger->setLevel(Core::LogManager::parseLevel(level)); + } + catch(Core::Exception e) { + application->logf(Core::Logger::LOG_WARNING, "Unknown log level '%s'.", level.toLocale().c_str()); } } } - - return false; } } diff --git a/src/modules/FileLogger/Module.h b/src/modules/FileLogger/Module.h index 8cf54a1..5cbd1df 100644 --- a/src/modules/FileLogger/Module.h +++ b/src/modules/FileLogger/Module.h @@ -39,10 +39,8 @@ class Module : public Common::Module, private Core::Configurable { std::set > loggers; - boost::shared_ptr lastLogger; - protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); + virtual void configure(); public: Module(Common::Application *application0) : application(application0) { diff --git a/src/modules/StorageBackendFile/StorageBackendFile.cpp b/src/modules/StorageBackendFile/StorageBackendFile.cpp index e5da4e0..e801a64 100644 --- a/src/modules/StorageBackendFile/StorageBackendFile.cpp +++ b/src/modules/StorageBackendFile/StorageBackendFile.cpp @@ -28,27 +28,10 @@ namespace Mad { namespace Modules { namespace StorageBackendFile { -bool StorageBackendFile::handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/) { - if(!entry[0].getKey().matches("Storage")) - return false; - +void StorageBackendFile::configure() { boost::lock_guard lock(mutex); - if(entry[1].getKey().matches("Root")) { - if(!entry[2].isEmpty()) - return false; - - storageRoot = entry[1][0].toLocale(); - } - else if(!entry[1].isEmpty()) { - return false; - } - - return true; -} - -void StorageBackendFile::configFinished() { - boost::lock_guard lock(mutex); + storageRoot = application->getConfigManager()->get("Storage.Root").toLocale(); if(!boost::filesystem::exists(storageRoot)) { boost::filesystem::create_directories(storageRoot); diff --git a/src/modules/StorageBackendFile/StorageBackendFile.h b/src/modules/StorageBackendFile/StorageBackendFile.h index f38e674..b1c685d 100644 --- a/src/modules/StorageBackendFile/StorageBackendFile.h +++ b/src/modules/StorageBackendFile/StorageBackendFile.h @@ -45,8 +45,7 @@ class StorageBackendFile : public Common::StorageBackend, private Core::Configur boost::filesystem::path getFileName(const Core::String &type, const Core::String &name); protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); - virtual void configFinished(); + virtual void configure(); virtual std::set listTypes() throw (Core::Exception); virtual std::set list(const Core::String &type) throw (Core::Exception); diff --git a/src/modules/UserConfigBackendHome/UserConfigBackendHome.cpp b/src/modules/UserConfigBackendHome/UserConfigBackendHome.cpp index 4dca729..a19769c 100644 --- a/src/modules/UserConfigBackendHome/UserConfigBackendHome.cpp +++ b/src/modules/UserConfigBackendHome/UserConfigBackendHome.cpp @@ -31,40 +31,16 @@ namespace Mad { namespace Modules { namespace UserConfigBackendHome { -bool UserConfigBackendHome::handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/) { - if(entry[0].getKey().matches("UserManager")) { - if(entry[1].getKey().matches("Skeleton")) { - if(entry[2].isEmpty()) - skeleton = entry[1][0]; - } - else if(entry[1].getKey().matches("HomeDir")) { - if(entry[2].isEmpty()) - homeDir = entry[1][0]; - } - else if(entry[1].getKey().matches("UserDirMode")) { - if(entry[2].isEmpty()) { - if(entry[1][0].isEmpty()) { - dirMode = 0755; - } - else { - char *endptr; - unsigned long val = std::strtoul(entry[1][0].toString().c_str(), &endptr, 8); - if(*endptr || val > 07777) { - application->logf(Core::Logger::LOG_WARNING, "UserBackendHome: Invalid configuration: DirMode '%s'", entry[1][0].toLocale().c_str()); - } - else { - dirMode = val; - } - } - } - } - else if(!entry[1].isEmpty()) - return false; - - return true; +void UserConfigBackendHome::configure() { + skeleton = application->getConfigManager()->get("UserManager.Skeleton"); + homeDir = application->getConfigManager()->get("UserManager.HomeDir", "/home"); + + dirMode = std::strtoul(application->getConfigManager()->get("UserManager.UserDirMode", "775").toString().c_str(), 0, 8); + if(dirMode > 07777) { + application->logf(Core::Logger::LOG_WARNING, "UserBackendHome: Invalid configuration: UserDirMode '%s'", + application->getConfigManager()->get("UserManager.UserDirMode").toLocale().c_str()); + dirMode = 0775; } - - return false; } void UserConfigBackendHome::setOwnerAndCopyMode(const std::string &source, const std::string &dest, const Common::UserInfo &userInfo, bool isSymlink) { diff --git a/src/modules/UserConfigBackendHome/UserConfigBackendHome.h b/src/modules/UserConfigBackendHome/UserConfigBackendHome.h index e82795c..41370ff 100644 --- a/src/modules/UserConfigBackendHome/UserConfigBackendHome.h +++ b/src/modules/UserConfigBackendHome/UserConfigBackendHome.h @@ -47,14 +47,14 @@ class UserConfigBackendHome : public Common::UserConfigBackend, private Core::Co void migrateOwner(const std::string &path, const Common::UserInfo &oldUserInfo, const Common::UserInfo &userInfo, bool isSymlink); protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); + virtual void configure(); virtual void addUser(const Common::UserInfo &userInfo) throw(Core::Exception); virtual void updateUser(const Common::UserInfo &oldUserInfo, const Common::UserInfo &userInfo) throw(Core::Exception); virtual void deleteUser(const Common::UserInfo &userInfo) throw(Core::Exception); public: - UserConfigBackendHome(Common::Application *application0) : application(application0), homeDir("/home"), dirMode(0755) { + UserConfigBackendHome(Common::Application *application0) : application(application0) { application->getConfigManager()->registerConfigurable(this); } diff --git a/src/modules/UserConfigBackendKrb5/UserConfigBackendKrb5.cpp b/src/modules/UserConfigBackendKrb5/UserConfigBackendKrb5.cpp index f0c65b1..61ad996 100644 --- a/src/modules/UserConfigBackendKrb5/UserConfigBackendKrb5.cpp +++ b/src/modules/UserConfigBackendKrb5/UserConfigBackendKrb5.cpp @@ -93,46 +93,15 @@ void UserConfigBackendKrb5::_connect() { return; } -bool UserConfigBackendKrb5::handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/) { - if(!entry[0].getKey().matches("UserManager")) - return false; - - if(entry[1].isEmpty()) - return true; - - if(!entry[1].getKey().matches("Krb5")) - return false; - +void UserConfigBackendKrb5::configure() { boost::lock_guard lock(mutex); - if(entry[2].getKey().matches("Realm")) { - if(entry[3].isEmpty()) - realm = entry[2][0]; - } - else if(entry[2].getKey().matches("Principal")) { - if(entry[3].isEmpty()) - principal = entry[2][0]; - } - else if(entry[2].getKey().matches("Server")) { - if(entry[3].isEmpty()) - server = entry[2][0]; - } - else if(entry[2].getKey().matches("Password")) { - if(entry[3].isEmpty()) - password = entry[2][0]; - } - else if(entry[2].getKey().matches("Keytab")) { - if(entry[3].isEmpty()) - keytab = entry[2][0]; - } - else if(!entry[2].isEmpty()) - return false; + realm = application->getConfigManager()->get("UserManager.Krb5.Realm", realm); + principal = application->getConfigManager()->get("UserManager.Krb5.Principal"); + server = application->getConfigManager()->get("UserManager.Krb5.Server"); + password = application->getConfigManager()->get("UserManager.Krb5.Password"); + keytab = application->getConfigManager()->get("UserManager.Krb5.Keytab"); - return true; -} - -void UserConfigBackendKrb5::configFinished() { - boost::lock_guard lock(mutex); _connect(); } diff --git a/src/modules/UserConfigBackendKrb5/UserConfigBackendKrb5.h b/src/modules/UserConfigBackendKrb5/UserConfigBackendKrb5.h index eab8be4..64f8501 100644 --- a/src/modules/UserConfigBackendKrb5/UserConfigBackendKrb5.h +++ b/src/modules/UserConfigBackendKrb5/UserConfigBackendKrb5.h @@ -50,8 +50,7 @@ class UserConfigBackendKrb5 : public Common::UserConfigBackend, private Core::Co void _connect(); protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); - virtual void configFinished(); + virtual void configure(); virtual void checkUserInfo(const Common::UserInfo &userInfo) throw(Core::Exception); diff --git a/src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp b/src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp index 1d213a8..be5426d 100644 --- a/src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp +++ b/src/modules/UserDBBackendMysql/UserDBBackendMysql.cpp @@ -37,128 +37,32 @@ namespace UserDBBackendMysql { const Core::String UserDBBackendMysql::name("UserDBBackendMysql"); -bool UserDBBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/) { - if(!entry[0].getKey().matches("UserManager")) - return false; +void UserDBBackendMysql::configure() { + host = application->getConfigManager()->get("UserManager.Mysql.Host"); + username = application->getConfigManager()->get("UserManager.Mysql.Username"); + passwd = application->getConfigManager()->get("UserManager.Mysql.Password"); + db = application->getConfigManager()->get("UserManager.Mysql.Database"); + port = std::strtol(application->getConfigManager()->get("UserManager.Mysql.Port").toString().c_str(), 0, 10); + unixSocket = application->getConfigManager()->get("UserManager.Mysql.UnixSocket"); + + queryListUsers = application->getConfigManager()->get("UserManager.Mysql.Queries.ListUsers"); + queryListGroups = application->getConfigManager()->get("UserManager.Mysql.Queries.ListGroups"); + queryListUserGroups = application->getConfigManager()->get("UserManager.Mysql.Queries.ListUserGroups"); + queryListGroupUsers = application->getConfigManager()->get("UserManager.Mysql.Queries.ListGroupUsers"); + queryUserById = application->getConfigManager()->get("UserManager.Mysql.Queries.UserById"); + queryUserByName = application->getConfigManager()->get("UserManager.Mysql.Queries.UserByName"); + queryGroupById = application->getConfigManager()->get("UserManager.Mysql.Queries.GroupById"); + queryGroupByName = application->getConfigManager()->get("UserManager.Mysql.Queries.GroupByName"); + queryUserGroupTable = application->getConfigManager()->get("UserManager.Mysql.Queries.UserGroupTable"); + queryAddUser = application->getConfigManager()->get("UserManager.Mysql.Queries.AddUser"); + queryUpdateUser = application->getConfigManager()->get("UserManager.Mysql.Queries.UpdateUser"); + queryDeleteUser = application->getConfigManager()->get("UserManager.Mysql.Queries.DeleteUser"); + queryAddGroup = application->getConfigManager()->get("UserManager.Mysql.Queries.AddGroup"); + queryUpdateGroup = application->getConfigManager()->get("UserManager.Mysql.Queries.UpdateGroup"); + queryDeleteGroup = application->getConfigManager()->get("UserManager.Mysql.Queries.DeleteGroup"); + queryAddUserToGroup = application->getConfigManager()->get("UserManager.Mysql.Queries.AddUserToGroup"); + queryDeleteUserFromGroup = application->getConfigManager()->get("UserManager.Mysql.Queries.DeleteUserFromGroup"); - if(entry[1].isEmpty()) - return true; - - if(!entry[1].getKey().matches("Mysql")) - return false; - - if(entry[2].getKey().matches("Host")) { - if(entry[3].isEmpty()) - host = entry[2][0]; - } - else if(entry[2].getKey().matches("Username")) { - if(entry[3].isEmpty()) - username = entry[2][0]; - } - else if(entry[2].getKey().matches("Password")) { - if(entry[3].isEmpty()) - passwd = entry[2][0]; - } - else if(entry[2].getKey().matches("Database")) { - if(entry[3].isEmpty()) - db = entry[2][0]; - } - else if(entry[2].getKey().matches("Port")) { - if(entry[3].isEmpty()) { - char *endptr; - long val; - - val = strtol(entry[2][0].toString().c_str(), &endptr, 10); - - if(endptr != 0 || val < 0 || val > 65535) - application->log(Core::Logger::LOG_WARNING, "UserDBBackendMysql: Invalid port"); - else - port = val; - } - } - else if(entry[2].getKey().matches("UnixSocket")) { - if(entry[3].isEmpty()) - unixSocket = entry[2][0]; - } - else if(entry[2].getKey().matches("Queries")) { - if(entry[3].getKey().matches("ListUsers")) { - if(entry[4].isEmpty()) - queryListUsers = entry[3][0]; - } - else if(entry[3].getKey().matches("ListGroups")) { - if(entry[4].isEmpty()) - queryListGroups = entry[3][0]; - } - else if(entry[3].getKey().matches("ListUserGroups")) { - if(entry[4].isEmpty()) - queryListUserGroups = entry[3][0]; - } - else if(entry[3].getKey().matches("ListGroupUsers")) { - if(entry[4].isEmpty()) - queryListGroupUsers = entry[3][0]; - } - else if(entry[3].getKey().matches("UserById")) { - if(entry[4].isEmpty()) - queryUserById = entry[3][0]; - } - else if(entry[3].getKey().matches("UserByName")) { - if(entry[4].isEmpty()) - queryUserByName = entry[3][0]; - } - else if(entry[3].getKey().matches("GroupById")) { - if(entry[4].isEmpty()) - queryGroupById = entry[3][0]; - } - else if(entry[3].getKey().matches("GroupByName")) { - if(entry[4].isEmpty()) - queryGroupByName = entry[3][0]; - } - else if(entry[3].getKey().matches("UserGroupTable")) { - if(entry[4].isEmpty()) - queryUserGroupTable = entry[3][0]; - } - else if(entry[3].getKey().matches("AddUser")) { - if(entry[4].isEmpty()) - queryAddUser = entry[3][0]; - } - else if(entry[3].getKey().matches("UpdateUser")) { - if(entry[4].isEmpty()) - queryUpdateUser = entry[3][0]; - } - else if(entry[3].getKey().matches("DeleteUser")) { - if(entry[4].isEmpty()) - queryDeleteUser = entry[3][0]; - } - else if(entry[3].getKey().matches("AddGroup")) { - if(entry[4].isEmpty()) - queryAddGroup = entry[3][0]; - } - else if(entry[3].getKey().matches("UpdateGroup")) { - if(entry[4].isEmpty()) - queryUpdateGroup = entry[3][0]; - } - else if(entry[3].getKey().matches("DeleteGroup")) { - if(entry[4].isEmpty()) - queryDeleteGroup = entry[3][0]; - } - else if(entry[3].getKey().matches("AddUserToGroup")) { - if(entry[4].isEmpty()) - queryAddUserToGroup = entry[3][0]; - } - else if(entry[3].getKey().matches("DeleteUserFromGroup")) { - if(entry[4].isEmpty()) - queryDeleteUserFromGroup = entry[3][0]; - } - else if(!entry[3].isEmpty()) - return false; - } - else if(!entry[2].isEmpty()) - return false; - - return true; -} - -void UserDBBackendMysql::configFinished() { if(db.isEmpty()) { application->log(Core::Logger::LOG_ERROR, "UserDBBackendMysql: No database name given"); return; diff --git a/src/modules/UserDBBackendMysql/UserDBBackendMysql.h b/src/modules/UserDBBackendMysql/UserDBBackendMysql.h index 9351ca1..10c069c 100644 --- a/src/modules/UserDBBackendMysql/UserDBBackendMysql.h +++ b/src/modules/UserDBBackendMysql/UserDBBackendMysql.h @@ -107,8 +107,7 @@ class UserDBBackendMysql : public Common::UserDBBackend, private Core::Configura boost::mutex mutex; protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); - virtual void configFinished(); + virtual void configure(); virtual boost::shared_ptr > getUserList(boost::posix_time::ptime *timestamp) throw(Core::Exception); virtual boost::shared_ptr getUserInfo(unsigned long uid, boost::posix_time::ptime *timestamp) throw(Core::Exception); @@ -134,7 +133,7 @@ class UserDBBackendMysql : public Common::UserDBBackend, private Core::Configura virtual void deleteUserFromGroup(unsigned long uid, unsigned long gid) throw(Core::Exception); public: - UserDBBackendMysql(Common::Application *application0) : application(application0), port(0), mysql(0), lastUpdate(boost::posix_time::microsec_clock::universal_time()) { + UserDBBackendMysql(Common::Application *application0) : application(application0), mysql(0), lastUpdate(boost::posix_time::microsec_clock::universal_time()) { application->getConfigManager()->registerConfigurable(this); } -- cgit v1.2.3