Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame^] | 3 | #include "config.h" |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 4 | #include <xyz/openbmc_project/User/Ldap/Config/server.hpp> |
| 5 | #include <xyz/openbmc_project/User/Ldap/Create/server.hpp> |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame^] | 6 | #include <xyz/openbmc_project/Common/error.hpp> |
| 7 | #include <phosphor-logging/log.hpp> |
| 8 | #include <phosphor-logging/elog.hpp> |
| 9 | #include <phosphor-logging/elog-errors.hpp> |
| 10 | #include <sdbusplus/bus.hpp> |
| 11 | #include <sdbusplus/server/object.hpp> |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 12 | #include <string> |
| 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace ldap |
| 17 | { |
| 18 | static constexpr auto defaultNslcdFile = "/etc/nslcd.conf.default"; |
| 19 | static constexpr auto nsSwitchFile = "/etc/nsswitch.conf"; |
| 20 | static constexpr auto LDAPNsSwitchFile = "/etc/nsswitch_ldap.conf"; |
| 21 | static constexpr auto linuxNsSwitchFile = "/etc/nsswitch_linux.conf"; |
| 22 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame^] | 23 | using namespace phosphor::logging; |
| 24 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 25 | namespace ldap_base = sdbusplus::xyz::openbmc_project::User::Ldap::server; |
| 26 | using ConfigIface = sdbusplus::server::object::object<ldap_base::Config>; |
| 27 | using CreateIface = sdbusplus::server::object::object<ldap_base::Create>; |
| 28 | |
| 29 | class ConfigMgr; |
| 30 | |
| 31 | /** @class Config |
| 32 | * @brief Configuration for LDAP. |
| 33 | * @details concrete implementation of xyz.openbmc_project.User.Ldap.Config |
| 34 | * API, in order to provide LDAP configuration. |
| 35 | */ |
| 36 | class Config : public ConfigIface |
| 37 | { |
| 38 | public: |
| 39 | Config() = delete; |
| 40 | ~Config() = default; |
| 41 | Config(const Config&) = delete; |
| 42 | Config& operator=(const Config&) = delete; |
| 43 | Config(Config&&) = default; |
| 44 | Config& operator=(Config&&) = default; |
| 45 | |
| 46 | /** @brief Constructor to put object onto bus at a D-Bus path. |
| 47 | * @param[in] bus - Bus to attach to. |
| 48 | * @param[in] path - The D-Bus object path to attach at. |
| 49 | * @param[in] filePath - LDAP configuration file. |
| 50 | * @param[in] secureLDAP - Specifies whether to use SSL or not. |
| 51 | * @param[in] lDAPServerURI - LDAP URI of the server. |
| 52 | * @param[in] lDAPBindDN - distinguished name with which to bind. |
| 53 | * @param[in] lDAPBaseDN - distinguished name to use as search base. |
| 54 | * @param[in] lDAPBindDNpassword - credentials with which to bind. |
| 55 | * @param[in] lDAPSearchScope - the search scope. |
| 56 | * @param[in] lDAPType - Specifies the LDAP server type which can be AD |
| 57 | or openLDAP. |
| 58 | * @param[in] parent - parent of config object. |
| 59 | */ |
| 60 | |
| 61 | Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath, |
| 62 | bool secureLDAP, std::string lDAPServerURI, std::string lDAPBindDN, |
| 63 | std::string lDAPBaseDN, std::string lDAPBindDNpassword, |
| 64 | ldap_base::Config::SearchScope lDAPSearchScope, |
| 65 | ldap_base::Config::Type lDAPType, ConfigMgr& parent); |
| 66 | |
| 67 | using ConfigIface::lDAPBaseDN; |
| 68 | using ConfigIface::lDAPBindDN; |
| 69 | using ConfigIface::lDAPBINDDNpassword; |
| 70 | using ConfigIface::lDAPSearchScope; |
| 71 | using ConfigIface::lDAPServerURI; |
| 72 | using ConfigIface::lDAPType; |
| 73 | using ConfigIface::secureLDAP; |
| 74 | using ConfigIface::setPropertyByName; |
| 75 | |
| 76 | /** @brief Update the secure LDAP property. |
| 77 | * @param[in] value - secureLDAP value to be updated. |
| 78 | * @returns value of changed secureLDAP. |
| 79 | */ |
| 80 | bool secureLDAP(bool value) override; |
| 81 | |
| 82 | /** @brief Update the Server URI property. |
| 83 | * @param[in] value - lDAPServerURI value to be updated. |
| 84 | * @returns value of changed lDAPServerURI. |
| 85 | */ |
| 86 | std::string lDAPServerURI(std::string value) override; |
| 87 | |
| 88 | /** @brief Update the BindDN property. |
| 89 | * @param[in] value - lDAPBindDN value to be updated. |
| 90 | * @returns value of changed lDAPBindDN. |
| 91 | */ |
| 92 | std::string lDAPBindDN(std::string value) override; |
| 93 | |
| 94 | /** @brief Update the BaseDN property. |
| 95 | * @param[in] value - lDAPBaseDN value to be updated. |
| 96 | * @returns value of changed lDAPBaseDN. |
| 97 | */ |
| 98 | std::string lDAPBaseDN(std::string value) override; |
| 99 | |
| 100 | /** @brief Update the BindDN password property. |
| 101 | * @param[in] value - lDAPBINDDNpassword value to be updated. |
| 102 | * @returns value of changed lDAPBINDDNpassword. |
| 103 | */ |
| 104 | std::string lDAPBINDDNpassword(std::string value) override; |
| 105 | |
| 106 | /** @brief Update the Search scope property. |
| 107 | * @param[in] value - lDAPSearchScope value to be updated. |
| 108 | * @returns value of changed lDAPSearchScope. |
| 109 | */ |
| 110 | ldap_base::Config::SearchScope |
| 111 | lDAPSearchScope(ldap_base::Config::SearchScope value) override; |
| 112 | |
| 113 | /** @brief Update the LDAP Type property. |
| 114 | * @param[in] value - lDAPType value to be updated. |
| 115 | * @returns value of changed lDAPType. |
| 116 | */ |
| 117 | ldap_base::Config::Type lDAPType(ldap_base::Config::Type value) override; |
| 118 | |
| 119 | private: |
| 120 | std::string configFilePath{}; |
| 121 | |
| 122 | /** @brief Persistent sdbusplus D-Bus bus connection. */ |
| 123 | sdbusplus::bus::bus& bus; |
| 124 | |
| 125 | /** @brief Create a new LDAP config file. |
| 126 | */ |
| 127 | virtual void writeConfig(); |
| 128 | |
| 129 | /** @brief reference to config manager object */ |
| 130 | ConfigMgr& parent; |
| 131 | }; |
| 132 | |
| 133 | /** @class ConfigMgr |
| 134 | * @brief Creates LDAP server configuration. |
| 135 | * @details concrete implementation of xyz.openbmc_project.User.Ldap.Create |
| 136 | * APIs, in order to create LDAP configuration. |
| 137 | */ |
| 138 | class ConfigMgr : public CreateIface |
| 139 | { |
| 140 | public: |
| 141 | ConfigMgr() = delete; |
| 142 | ~ConfigMgr() = default; |
| 143 | ConfigMgr(const ConfigMgr&) = delete; |
| 144 | ConfigMgr& operator=(const ConfigMgr&) = delete; |
| 145 | ConfigMgr(ConfigMgr&&) = delete; |
| 146 | ConfigMgr& operator=(ConfigMgr&&) = delete; |
| 147 | |
| 148 | /** @brief ConfigMgr to put object onto bus at a dbus path. |
| 149 | * @param[in] bus - Bus to attach to. |
| 150 | * @param[in] path - Path to attach at. |
| 151 | * @param[in] filePath - LDAP configuration file. |
| 152 | */ |
| 153 | ConfigMgr(sdbusplus::bus::bus& bus, const char* path) : |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame^] | 154 | CreateIface(bus, path, true), bus(bus) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 155 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame^] | 156 | try |
| 157 | { |
| 158 | restore(LDAP_CONFIG_FILE); |
| 159 | emit_object_added(); |
| 160 | } |
| 161 | catch (const std::exception& e) |
| 162 | { |
| 163 | configPtr.reset(nullptr); |
| 164 | log<level::ERR>(e.what()); |
| 165 | elog<InternalFailure>(); |
| 166 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** @brief concrete implementation of the pure virtual funtion |
| 170 | xyz.openbmc_project.User.Ldap.Create.createConfig. |
| 171 | * @param[in] secureLDAP - Specifies whether to use SSL or not. |
| 172 | * @param[in] lDAPServerURI - LDAP URI of the server. |
| 173 | * @param[in] lDAPBindDN - distinguished name with which bind to bind |
| 174 | to the directory server for lookups. |
| 175 | * @param[in] lDAPBaseDN - distinguished name to use as search base. |
| 176 | * @param[in] lDAPBindDNpassword - credentials with which to bind. |
| 177 | * @param[in] lDAPSearchScope - the search scope. |
| 178 | * @param[in] lDAPType - Specifies the LDAP server type which can be AD |
| 179 | or openLDAP. |
| 180 | * @returns the object path of the D-Bus object created. |
| 181 | */ |
| 182 | std::string createConfig(bool secureLDAP, std::string lDAPServerURI, |
| 183 | std::string lDAPBindDN, std::string lDAPBaseDN, |
| 184 | std::string lDAPBindDNpassword, |
| 185 | ldap_base::Create::SearchScope lDAPSearchScope, |
| 186 | ldap_base::Create::Type lDAPType) override; |
| 187 | |
| 188 | /** @brief restarts given service |
| 189 | * @param[in] service - Service to be restarted. |
| 190 | */ |
| 191 | virtual void restartService(const std::string& service); |
| 192 | |
| 193 | private: |
| 194 | /** @brief Persistent sdbusplus D-Bus bus connection. */ |
| 195 | sdbusplus::bus::bus& bus; |
| 196 | |
| 197 | /** @brief Pointer to a Config D-Bus object */ |
| 198 | std::unique_ptr<Config> configPtr = nullptr; |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame^] | 199 | |
| 200 | /** @brief Populate existing config into D-Bus properties |
| 201 | * @param[in] filePath - LDAP config file path |
| 202 | */ |
| 203 | virtual void restore(const char* filePath); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 204 | }; |
| 205 | } // namespace ldap |
| 206 | } // namespace phosphor |