xref: /openbmc/phosphor-user-manager/phosphor-ldap-config/ldap_config.hpp (revision 32be2961cc1f2e48e55b4893ab680fbcfc17f16a)
137fb3feeSRatan Gupta #pragma once
237fb3feeSRatan Gupta 
337fb3feeSRatan Gupta #include "config.h"
437fb3feeSRatan Gupta #include <xyz/openbmc_project/Object/Enable/server.hpp>
537fb3feeSRatan Gupta #include <xyz/openbmc_project/User/Ldap/Create/server.hpp>
637fb3feeSRatan Gupta #include <xyz/openbmc_project/User/Ldap/Config/server.hpp>
77b04c352SRatan Gupta #include <xyz/openbmc_project/User/PrivilegeMapper/server.hpp>
837fb3feeSRatan Gupta #include <xyz/openbmc_project/Common/error.hpp>
97b04c352SRatan Gupta #include "ldap_mapper_entry.hpp"
1037fb3feeSRatan Gupta #include <phosphor-logging/log.hpp>
1137fb3feeSRatan Gupta #include <phosphor-logging/elog.hpp>
1237fb3feeSRatan Gupta #include <phosphor-logging/elog-errors.hpp>
1337fb3feeSRatan Gupta #include <sdbusplus/bus.hpp>
1437fb3feeSRatan Gupta #include <sdbusplus/server/object.hpp>
157b04c352SRatan Gupta 
1621e88cb5SRatan Gupta #include <filesystem>
177b04c352SRatan Gupta #include <set>
187b04c352SRatan Gupta #include <string>
1937fb3feeSRatan Gupta 
2037fb3feeSRatan Gupta namespace phosphor
2137fb3feeSRatan Gupta {
2237fb3feeSRatan Gupta namespace ldap
2337fb3feeSRatan Gupta {
2437fb3feeSRatan Gupta 
2537fb3feeSRatan Gupta using namespace phosphor::logging;
2637fb3feeSRatan Gupta using namespace sdbusplus::xyz::openbmc_project::Common::Error;
2737fb3feeSRatan Gupta using ConfigIface = sdbusplus::xyz::openbmc_project::User::Ldap::server::Config;
2837fb3feeSRatan Gupta using EnableIface = sdbusplus::xyz::openbmc_project::Object::server::Enable;
2937fb3feeSRatan Gupta using CreateIface = sdbusplus::server::object::object<
3037fb3feeSRatan Gupta     sdbusplus::xyz::openbmc_project::User::Ldap::server::Create>;
3121e88cb5SRatan Gupta namespace fs = std::filesystem;
327b04c352SRatan Gupta using MapperIface =
337b04c352SRatan Gupta     sdbusplus::xyz::openbmc_project::User::server::PrivilegeMapper;
347b04c352SRatan Gupta 
357b04c352SRatan Gupta using Ifaces =
367b04c352SRatan Gupta     sdbusplus::server::object::object<ConfigIface, EnableIface, MapperIface>;
377b04c352SRatan Gupta using ObjectPath = sdbusplus::message::object_path;
387b04c352SRatan Gupta 
3922f13f18SRatan Gupta namespace sdbusRule = sdbusplus::bus::match::rules;
4022f13f18SRatan Gupta 
4137fb3feeSRatan Gupta class ConfigMgr;
4237fb3feeSRatan Gupta class MockConfigMgr;
4337fb3feeSRatan Gupta 
4437fb3feeSRatan Gupta /** @class Config
4537fb3feeSRatan Gupta  *  @brief Configuration for LDAP.
4637fb3feeSRatan Gupta  *  @details concrete implementation of xyz.openbmc_project.User.Ldap.Config
4737fb3feeSRatan Gupta  *  API, in order to provide LDAP configuration.
4837fb3feeSRatan Gupta  */
4937fb3feeSRatan Gupta class Config : public Ifaces
5037fb3feeSRatan Gupta {
5137fb3feeSRatan Gupta   public:
5237fb3feeSRatan Gupta     Config() = delete;
5337fb3feeSRatan Gupta     ~Config() = default;
5437fb3feeSRatan Gupta     Config(const Config&) = delete;
5537fb3feeSRatan Gupta     Config& operator=(const Config&) = delete;
5637fb3feeSRatan Gupta     Config(Config&&) = default;
5737fb3feeSRatan Gupta     Config& operator=(Config&&) = default;
5837fb3feeSRatan Gupta 
5937fb3feeSRatan Gupta     /** @brief Constructor to put object onto bus at a D-Bus path.
6037fb3feeSRatan Gupta      *  @param[in] bus - Bus to attach to.
6137fb3feeSRatan Gupta      *  @param[in] path - The D-Bus object path to attach at.
6237fb3feeSRatan Gupta      *  @param[in] filePath - LDAP configuration file.
6337fb3feeSRatan Gupta      *  @param[in] caCertFile - LDAP's CA certificate file.
6422f13f18SRatan Gupta      *  @param[in] certFile - LDAP's client certificate file.
6537fb3feeSRatan Gupta      *  @param[in] secureLDAP - Specifies whether to use SSL or not.
6637fb3feeSRatan Gupta      *  @param[in] lDAPServerURI - LDAP URI of the server.
6737fb3feeSRatan Gupta      *  @param[in] lDAPBindDN - distinguished name with which to bind.
6837fb3feeSRatan Gupta      *  @param[in] lDAPBaseDN -  distinguished name to use as search base.
6937fb3feeSRatan Gupta      *  @param[in] lDAPBindDNPassword - credentials with which to bind.
7037fb3feeSRatan Gupta      *  @param[in] lDAPSearchScope - the search scope.
7137fb3feeSRatan Gupta      *  @param[in] lDAPType - Specifies the LDAP server type which can be AD
7237fb3feeSRatan Gupta      *              or openLDAP.
7337fb3feeSRatan Gupta      *  @param[in] lDAPServiceEnabled - Specifies whether the service would be
7437fb3feeSRatan Gupta      *  enabled or not.
7537fb3feeSRatan Gupta      *  @param[in] groupNameAttribute - Specifies attribute name that contains
7637fb3feeSRatan Gupta      *             the name of the Group in the LDAP server.
7737fb3feeSRatan Gupta      *  @param[in] userNameAttribute - Specifies attribute name that contains
7837fb3feeSRatan Gupta      *             the username in the LDAP server.
7937fb3feeSRatan Gupta      *
8037fb3feeSRatan Gupta      *  @param[in] parent - parent of config object.
8137fb3feeSRatan Gupta      */
8237fb3feeSRatan Gupta 
8337fb3feeSRatan Gupta     Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
8422f13f18SRatan Gupta            const char* caCertFile, const char* certFile, bool secureLDAP,
8522f13f18SRatan Gupta            std::string lDAPServerURI, std::string lDAPBindDN,
8622f13f18SRatan Gupta            std::string lDAPBaseDN, std::string&& lDAPBindDNPassword,
8737fb3feeSRatan Gupta            ConfigIface::SearchScope lDAPSearchScope, ConfigIface::Type lDAPType,
8837fb3feeSRatan Gupta            bool lDAPServiceEnabled, std::string groupNameAttribute,
8937fb3feeSRatan Gupta            std::string userNameAttribute, ConfigMgr& parent);
9037fb3feeSRatan Gupta 
9121e88cb5SRatan Gupta     /** @brief Constructor to put object onto bus at a D-Bus path.
9221e88cb5SRatan Gupta      *  @param[in] bus - Bus to attach to.
9321e88cb5SRatan Gupta      *  @param[in] path - The D-Bus object path to attach at.
9421e88cb5SRatan Gupta      *  @param[in] filePath - LDAP configuration file.
9521e88cb5SRatan Gupta      *  @param[in] lDAPType - Specifies the LDAP server type which can be AD
9621e88cb5SRatan Gupta      *              or openLDAP.
9721e88cb5SRatan Gupta      *  @param[in] parent - parent of config object.
9821e88cb5SRatan Gupta      */
9921e88cb5SRatan Gupta     Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
100ab4fcb4cSRatan Gupta            const char* caCertFile, const char* certFile,
101ab4fcb4cSRatan Gupta            ConfigIface::Type lDAPType, ConfigMgr& parent);
10221e88cb5SRatan Gupta 
10337fb3feeSRatan Gupta     using ConfigIface::groupNameAttribute;
10437fb3feeSRatan Gupta     using ConfigIface::lDAPBaseDN;
10537fb3feeSRatan Gupta     using ConfigIface::lDAPBindDN;
10637fb3feeSRatan Gupta     using ConfigIface::lDAPBindDNPassword;
10737fb3feeSRatan Gupta     using ConfigIface::lDAPSearchScope;
10837fb3feeSRatan Gupta     using ConfigIface::lDAPServerURI;
10937fb3feeSRatan Gupta     using ConfigIface::lDAPType;
11037fb3feeSRatan Gupta     using ConfigIface::setPropertyByName;
11137fb3feeSRatan Gupta     using ConfigIface::userNameAttribute;
11237fb3feeSRatan Gupta     using EnableIface::enabled;
11337fb3feeSRatan Gupta 
11437fb3feeSRatan Gupta     /** @brief Update the Server URI property.
11537fb3feeSRatan Gupta      *  @param[in] value - lDAPServerURI value to be updated.
11637fb3feeSRatan Gupta      *  @returns value of changed lDAPServerURI.
11737fb3feeSRatan Gupta      */
11837fb3feeSRatan Gupta     std::string lDAPServerURI(std::string value) override;
11937fb3feeSRatan Gupta 
12037fb3feeSRatan Gupta     /** @brief Update the BindDN property.
12137fb3feeSRatan Gupta      *  @param[in] value - lDAPBindDN value to be updated.
12237fb3feeSRatan Gupta      *  @returns value of changed lDAPBindDN.
12337fb3feeSRatan Gupta      */
12437fb3feeSRatan Gupta     std::string lDAPBindDN(std::string value) override;
12537fb3feeSRatan Gupta 
12637fb3feeSRatan Gupta     /** @brief Update the BaseDN property.
12737fb3feeSRatan Gupta      *  @param[in] value - lDAPBaseDN value to be updated.
12837fb3feeSRatan Gupta      *  @returns value of changed lDAPBaseDN.
12937fb3feeSRatan Gupta      */
13037fb3feeSRatan Gupta     std::string lDAPBaseDN(std::string value) override;
13137fb3feeSRatan Gupta 
13237fb3feeSRatan Gupta     /** @brief Update the Search scope property.
13337fb3feeSRatan Gupta      *  @param[in] value - lDAPSearchScope value to be updated.
13437fb3feeSRatan Gupta      *  @returns value of changed lDAPSearchScope.
13537fb3feeSRatan Gupta      */
13637fb3feeSRatan Gupta     ConfigIface::SearchScope
13737fb3feeSRatan Gupta         lDAPSearchScope(ConfigIface::SearchScope value) override;
13837fb3feeSRatan Gupta 
13937fb3feeSRatan Gupta     /** @brief Update the LDAP Type property.
14037fb3feeSRatan Gupta      *  @param[in] value - lDAPType value to be updated.
14137fb3feeSRatan Gupta      *  @returns value of changed lDAPType.
14237fb3feeSRatan Gupta      */
14337fb3feeSRatan Gupta     ConfigIface::Type lDAPType(ConfigIface::Type value) override;
14437fb3feeSRatan Gupta 
14537fb3feeSRatan Gupta     /** @brief Update the ldapServiceEnabled property.
14637fb3feeSRatan Gupta      *  @param[in] value - ldapServiceEnabled value to be updated.
14737fb3feeSRatan Gupta      *  @returns value of changed ldapServiceEnabled.
14837fb3feeSRatan Gupta      */
14937fb3feeSRatan Gupta     bool enabled(bool value) override;
15037fb3feeSRatan Gupta 
15137fb3feeSRatan Gupta     /** @brief Update the userNameAttribute property.
15237fb3feeSRatan Gupta      *  @param[in] value - userNameAttribute value to be updated.
15337fb3feeSRatan Gupta      *  @returns value of changed userNameAttribute.
15437fb3feeSRatan Gupta      */
15537fb3feeSRatan Gupta     std::string userNameAttribute(std::string value) override;
15637fb3feeSRatan Gupta 
15737fb3feeSRatan Gupta     /** @brief Update the groupNameAttribute property.
15837fb3feeSRatan Gupta      *  @param[in] value - groupNameAttribute value to be updated.
15937fb3feeSRatan Gupta      *  @returns value of changed groupNameAttribute.
16037fb3feeSRatan Gupta      */
16137fb3feeSRatan Gupta     std::string groupNameAttribute(std::string value) override;
16237fb3feeSRatan Gupta 
16337fb3feeSRatan Gupta     /** @brief Update the BindDNPasword property.
16437fb3feeSRatan Gupta      *  @param[in] value - lDAPBindDNPassword value to be updated.
16537fb3feeSRatan Gupta      *  @returns value of changed lDAPBindDNPassword.
16637fb3feeSRatan Gupta      */
16737fb3feeSRatan Gupta     std::string lDAPBindDNPassword(std::string value) override;
16837fb3feeSRatan Gupta 
16921e88cb5SRatan Gupta     /** @brief Function required by Cereal to perform deserialization.
17021e88cb5SRatan Gupta      *  @tparam Archive - Cereal archive type (binary in our case).
17121e88cb5SRatan Gupta      *  @param[in] archive - reference to Cereal archive.
17221e88cb5SRatan Gupta      *  @param[in] version - Class version that enables handling
17321e88cb5SRatan Gupta      *                       a serialized data across code levels
17421e88cb5SRatan Gupta      */
17521e88cb5SRatan Gupta     template <class Archive>
17621e88cb5SRatan Gupta     void load(Archive& archive, const std::uint32_t version);
17721e88cb5SRatan Gupta 
17821e88cb5SRatan Gupta     /** @brief Function required by Cereal to perform serialization.
17921e88cb5SRatan Gupta      *  @tparam Archive - Cereal archive type (binary in our case).
18021e88cb5SRatan Gupta      *  @param[in] archive - reference to Cereal archive.
18121e88cb5SRatan Gupta      *  @param[in] version - Class version that enables handling
18221e88cb5SRatan Gupta      *                       a serialized data across code levels
18321e88cb5SRatan Gupta      */
18421e88cb5SRatan Gupta     template <class Archive>
18521e88cb5SRatan Gupta     void save(Archive& archive, const std::uint32_t version) const;
18621e88cb5SRatan Gupta 
18721e88cb5SRatan Gupta     /** @brief Serialize and persist this object at the persist
18821e88cb5SRatan Gupta      *         location.
18921e88cb5SRatan Gupta      */
19021e88cb5SRatan Gupta     void serialize();
19121e88cb5SRatan Gupta 
19221e88cb5SRatan Gupta     /** @brief Deserialize LDAP config data from the persistent location
19321e88cb5SRatan Gupta      *         into this object
19421e88cb5SRatan Gupta      *  @return bool - true if the deserialization was successful, false
19521e88cb5SRatan Gupta      *                 otherwise.
19621e88cb5SRatan Gupta      */
19721e88cb5SRatan Gupta     bool deserialize();
19837fb3feeSRatan Gupta 
199c5481d1cSRatan Gupta     /** @brief enable or disable the service with the given value
200c5481d1cSRatan Gupta      *  @param[in] value - enable/disble
201c5481d1cSRatan Gupta      *  @returns value of changed status
202c5481d1cSRatan Gupta      */
203c5481d1cSRatan Gupta     bool enableService(bool value);
204c5481d1cSRatan Gupta 
2057b04c352SRatan Gupta     /** @brief Creates a mapping for the group to the privilege
2067b04c352SRatan Gupta      *
2077b04c352SRatan Gupta      *  @param[in] groupName - Group Name to which the privilege needs to be
2087b04c352SRatan Gupta      *                         assigned.
2097b04c352SRatan Gupta      *  @param[in] privilege - The privilege role associated with the group.
2107b04c352SRatan Gupta      *
2117b04c352SRatan Gupta      *  @return On success return the D-Bus object path of the created privilege
2127b04c352SRatan Gupta      *          mapper entry.
2137b04c352SRatan Gupta      */
2147b04c352SRatan Gupta     ObjectPath create(std::string groupName, std::string privilege) override;
2157b04c352SRatan Gupta 
2167b04c352SRatan Gupta     /** @brief Delete privilege mapping for LDAP group
2177b04c352SRatan Gupta      *
2187b04c352SRatan Gupta      *  This method deletes the privilege mapping
2197b04c352SRatan Gupta      *
2207b04c352SRatan Gupta      *  @param[in] id - id of the object which needs to be deleted.
2217b04c352SRatan Gupta      */
2227b04c352SRatan Gupta     void deletePrivilegeMapper(Id id);
2237b04c352SRatan Gupta 
2247b04c352SRatan Gupta     /** @brief Check if LDAP group privilege mapping requested is valid
2257b04c352SRatan Gupta      *
2267b04c352SRatan Gupta      *  Check if the privilege mapping already exists for the LDAP group name
2277b04c352SRatan Gupta      *  and group name is empty.
2287b04c352SRatan Gupta      *
2297b04c352SRatan Gupta      *  @param[in] groupName - LDAP group name
2307b04c352SRatan Gupta      *
2317b04c352SRatan Gupta      *  @return throw exception if the conditions are not met.
2327b04c352SRatan Gupta      */
2337b04c352SRatan Gupta     void checkPrivilegeMapper(const std::string& groupName);
2347b04c352SRatan Gupta 
2357b04c352SRatan Gupta     /** @brief Check if the privilege level is a valid one
2367b04c352SRatan Gupta      *
2377b04c352SRatan Gupta      *  @param[in] privilege - Privilege level
2387b04c352SRatan Gupta      *
2397b04c352SRatan Gupta      *  @return throw exception if the conditions are not met.
2407b04c352SRatan Gupta      */
2417b04c352SRatan Gupta     void checkPrivilegeLevel(const std::string& privilege);
2427b04c352SRatan Gupta 
2437b04c352SRatan Gupta     /** @brief Construct LDAP mapper entry D-Bus objects from their persisted
2447b04c352SRatan Gupta      *         representations.
2457b04c352SRatan Gupta      */
2467b04c352SRatan Gupta     void restoreRoleMapping();
2477b04c352SRatan Gupta 
24837fb3feeSRatan Gupta   private:
24921e88cb5SRatan Gupta     bool secureLDAP;
25037fb3feeSRatan Gupta     std::string lDAPBindPassword{};
25137fb3feeSRatan Gupta     std::string tlsCacertFile{};
25222f13f18SRatan Gupta     std::string tlsCertFile{};
25321e88cb5SRatan Gupta     std::string configFilePath{};
25421e88cb5SRatan Gupta     std::string objectPath{};
25521e88cb5SRatan Gupta     std::filesystem::path configPersistPath{};
25637fb3feeSRatan Gupta 
25737fb3feeSRatan Gupta     /** @brief Persistent sdbusplus D-Bus bus connection. */
25837fb3feeSRatan Gupta     sdbusplus::bus::bus& bus;
25937fb3feeSRatan Gupta 
26037fb3feeSRatan Gupta     /** @brief Create a new LDAP config file.
26137fb3feeSRatan Gupta      */
26237fb3feeSRatan Gupta     virtual void writeConfig();
26337fb3feeSRatan Gupta 
26437fb3feeSRatan Gupta     /** @brief reference to config manager object */
26537fb3feeSRatan Gupta     ConfigMgr& parent;
26637fb3feeSRatan Gupta 
2677b04c352SRatan Gupta     /** @brief Id of the last privilege mapper entry */
2687b04c352SRatan Gupta     Id entryId = 0;
2697b04c352SRatan Gupta 
2707b04c352SRatan Gupta     /** @brief container to hold privilege mapper objects */
2717b04c352SRatan Gupta     std::map<Id, std::unique_ptr<LDAPMapperEntry>> PrivilegeMapperList;
2727b04c352SRatan Gupta 
2737b04c352SRatan Gupta     /** @brief available privileges container */
274*32be2961SRichard Marian Thomaiyar     std::set<std::string> privMgr = {
275*32be2961SRichard Marian Thomaiyar         "priv-admin",
276*32be2961SRichard Marian Thomaiyar         "priv-operator",
277*32be2961SRichard Marian Thomaiyar         "priv-user",
278*32be2961SRichard Marian Thomaiyar     };
2797b04c352SRatan Gupta 
28022f13f18SRatan Gupta     /** @brief React to InterfaceAdded signal
28122f13f18SRatan Gupta      *  @param[in] msg - sdbusplus message
28222f13f18SRatan Gupta      */
28322f13f18SRatan Gupta     void certificateInstalled(sdbusplus::message::message& msg);
28422f13f18SRatan Gupta     sdbusplus::bus::match_t certificateInstalledSignal;
28522f13f18SRatan Gupta 
286a47fe4eaSmanojkiraneda     sdbusplus::bus::match_t cacertificateInstalledSignal;
287a47fe4eaSmanojkiraneda 
288ab4fcb4cSRatan Gupta     /** @brief React to certificate changed signal
289ab4fcb4cSRatan Gupta      *  @param[in] msg - sdbusplus message
290ab4fcb4cSRatan Gupta      */
291ab4fcb4cSRatan Gupta     void certificateChanged(sdbusplus::message::message& msg);
292ab4fcb4cSRatan Gupta     sdbusplus::bus::match_t certificateChangedSignal;
293ab4fcb4cSRatan Gupta 
29437fb3feeSRatan Gupta     friend class MockConfigMgr;
29537fb3feeSRatan Gupta };
29637fb3feeSRatan Gupta 
29737fb3feeSRatan Gupta } // namespace ldap
29837fb3feeSRatan Gupta } // namespace phosphor
299