From 95a2931473dfa61a30e7a65606dab15ab24cd5b4 Mon Sep 17 00:00:00 2001 From: Ratan Gupta Date: Mon, 18 Feb 2019 20:34:10 +0530 Subject: LDAP: Add the persistency for the "Enabled" property This property will control that whether the LDAP service would be started or not. We are persisting this property using cereal, other properties is being persisted through nslcd.conf, nslcd doesn't give us a way to put this property under nslcd.conf. Tested By: Test the persistency of enabled property. Verified that it was getting persisted across restart/reboot. Change-Id: Id64b23b71865bac15d3be2d79abad615aa576bea Signed-off-by: Ratan Gupta --- phosphor-ldap-config/Makefile.am | 3 +- phosphor-ldap-config/ldap_configuration.cpp | 14 ++++- phosphor-ldap-config/ldap_configuration.hpp | 11 +++- phosphor-ldap-config/ldap_serialize.cpp | 88 +++++++++++++++++++++++++++++ phosphor-ldap-config/ldap_serialize.hpp | 29 ++++++++++ phosphor-ldap-config/main.cpp | 2 +- 6 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 phosphor-ldap-config/ldap_serialize.cpp create mode 100644 phosphor-ldap-config/ldap_serialize.hpp (limited to 'phosphor-ldap-config') diff --git a/phosphor-ldap-config/Makefile.am b/phosphor-ldap-config/Makefile.am index 907c365..501e577 100644 --- a/phosphor-ldap-config/Makefile.am +++ b/phosphor-ldap-config/Makefile.am @@ -5,7 +5,8 @@ noinst_HEADERS = ldap_configuration.hpp utils.hpp phosphor_ldap_conf_SOURCES = \ main.cpp \ utils.cpp \ - ldap_configuration.cpp + ldap_configuration.cpp \ + ldap_serialize.cpp phosphor_ldap_conf_LDFLAGS = $(SDBUSPLUS_LIBS) \ $(PHOSPHOR_DBUS_INTERFACES_LIBS) \ diff --git a/phosphor-ldap-config/ldap_configuration.cpp b/phosphor-ldap-config/ldap_configuration.cpp index 413998a..4f2f85c 100644 --- a/phosphor-ldap-config/ldap_configuration.cpp +++ b/phosphor-ldap-config/ldap_configuration.cpp @@ -1,6 +1,7 @@ #include "ldap_configuration.hpp" +#include "ldap_serialize.hpp" #include "utils.hpp" -#include +#include #include #include @@ -15,7 +16,7 @@ constexpr auto LDAPSscheme = "ldaps"; using namespace phosphor::logging; using namespace sdbusplus::xyz::openbmc_project::Common::Error; -namespace fs = std::experimental::filesystem; +namespace fs = std::filesystem; using Argument = xyz::openbmc_project::Common::InvalidArgument; using Line = std::string; @@ -386,6 +387,8 @@ bool Config::enabled(bool value) return value; } isEnable = EnableIface::enabled(value); + // save the enabled property. + serialize(*this, parent.dbusPersistentPath); parent.startOrStopService(nslcdService, value); } catch (const InternalFailure& e) @@ -691,6 +694,13 @@ void ConfigMgr::restore(const char* filePath) std::move(configValues["bindpw"]), lDAPSearchScope, lDAPType, std::move(configValues["map_passwd_uid"]), std::move(configValues["map_passwd_gidNumber"])); + + // Get the enabled property value from the persistent location + if (!deserialize(dbusPersistentPath, *configPtr)) + { + log( + "Deserialization Failed, continue with service disable"); + } } catch (const InvalidArgument& e) { diff --git a/phosphor-ldap-config/ldap_configuration.hpp b/phosphor-ldap-config/ldap_configuration.hpp index 0d69f08..976aac6 100644 --- a/phosphor-ldap-config/ldap_configuration.hpp +++ b/phosphor-ldap-config/ldap_configuration.hpp @@ -178,12 +178,14 @@ class ConfigMgr : public CreateIface * @param[in] bus - Bus to attach to. * @param[in] path - Path to attach at. * @param[in] filePath - LDAP configuration file. + * @param[in] dbusPersistentPath - Persistent path for LDAP D-Bus property. * @param[in] caCertFile - LDAP's CA certificate file. */ ConfigMgr(sdbusplus::bus::bus& bus, const char* path, const char* filePath, - const char* caCertFile) : + const char* dbusPersistentPath, const char* caCertFile) : CreateIface(bus, path, true), - configFilePath(filePath), bus(bus) + dbusPersistentPath(dbusPersistentPath), configFilePath(filePath), + bus(bus) { try { @@ -242,6 +244,11 @@ class ConfigMgr : public CreateIface */ void deleteObject(); + /* ldap service enabled property would be saved under + * this path. + */ + std::string dbusPersistentPath; + protected: std::string configFilePath{}; std::string tlsCacertFile{}; diff --git a/phosphor-ldap-config/ldap_serialize.cpp b/phosphor-ldap-config/ldap_serialize.cpp new file mode 100644 index 0000000..510686c --- /dev/null +++ b/phosphor-ldap-config/ldap_serialize.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include + +#include "ldap_serialize.hpp" +#include "ldap_configuration.hpp" +#include +#include "config.h" + +// Register class version +// From cereal documentation; +// "This macro should be placed at global scope" +CEREAL_CLASS_VERSION(phosphor::ldap::Config, CLASS_VERSION); + +namespace phosphor +{ +namespace ldap +{ + +using namespace phosphor::logging; + +/** @brief Function required by Cereal to perform serialization. + * @tparam Archive - Cereal archive type (binary in our case). + * @param[in] archive - reference to Cereal archive. + * @param[in] config - const reference to ldap config. + * @param[in] version - Class version that enables handling + * a serialized data across code levels + */ +template +void save(Archive& archive, const Config& config, const std::uint32_t version) +{ + archive(config.enabled()); +} + +/** @brief Function required by Cereal to perform deserialization. + * @tparam Archive - Cereal archive type (binary in our case). + * @param[in] archive - reference to Cereal archive. + * @param[in] config - reference of ldap config object. + * @param[in] version - Class version that enables handling + * a serialized data across code levels + */ +template +void load(Archive& archive, Config& config, const std::uint32_t version) +{ + bool enabled = false; + archive(enabled); + config.enabled(enabled); +} + +fs::path serialize(const Config& config, const fs::path& path) +{ + fs::create_directories(path.parent_path()); + + std::ofstream os(path.string(), std::ios::binary); + cereal::BinaryOutputArchive oarchive(os); + oarchive(config); + return path; +} + +bool deserialize(const fs::path& path, Config& config) +{ + try + { + if (fs::exists(path)) + { + std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); + cereal::BinaryInputArchive iarchive(is); + iarchive(config); + return true; + } + return false; + } + catch (cereal::Exception& e) + { + log(e.what()); + std::error_code ec; + fs::remove(path, ec); + return false; + } + catch (const fs::filesystem_error& e) + { + return false; + } +} + +} // namespace ldap +} // namespace phosphor diff --git a/phosphor-ldap-config/ldap_serialize.hpp b/phosphor-ldap-config/ldap_serialize.hpp new file mode 100644 index 0000000..b784baf --- /dev/null +++ b/phosphor-ldap-config/ldap_serialize.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include "ldap_configuration.hpp" + +namespace phosphor +{ +namespace ldap +{ + +namespace fs = std::filesystem; + +/** @brief Serialize and persist LDAP service status property. + * @param[in] config - const reference to LDAP config object. + * @param[in] path - path of persistent location where D-Bus property would be + * saved. + * @return fs::path - pathname of persisted LDAP Config file. + */ +fs::path serialize(const Config& config, const fs::path& path); + +/** @brief Deserialize LDAP service status into a D-Bus object + * @param[in] path - pathname of persisted LDAP Config file. + * @param[in] config - reference of the object which needs to be deserialized. + * @return bool - true if the deserialization was successful, false otherwise. + */ +bool deserialize(const fs::path& path, Config& config); + +} // namespace ldap +} // namespace phosphor diff --git a/phosphor-ldap-config/main.cpp b/phosphor-ldap-config/main.cpp index 3ecc0ca..2e3bf66 100644 --- a/phosphor-ldap-config/main.cpp +++ b/phosphor-ldap-config/main.cpp @@ -26,7 +26,7 @@ int main(int argc, char* argv[]) sdbusplus::server::manager::manager objManager(bus, LDAP_CONFIG_ROOT); phosphor::ldap::ConfigMgr mgr(bus, LDAP_CONFIG_ROOT, LDAP_CONFIG_FILE, - TLS_CACERT_FILE); + LDAP_CONF_PERSIST_PATH, TLS_CACERT_FILE); bus.request_name(LDAP_CONFIG_BUSNAME); -- cgit v1.2.1