blob: 7dd8593934c4dcb2be36ab7834412b5f09264444 [file] [log] [blame]
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -05001#pragma once
2
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -05003#include "config.h"
Patrick Williams9638afb2021-02-22 17:16:24 -06004
Ratan Gupta7b04c352019-04-12 21:46:29 +05305#include "ldap_mapper_entry.hpp"
Patrick Williams9638afb2021-02-22 17:16:24 -06006
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -05007#include <sdbusplus/bus.hpp>
8#include <sdbusplus/server/object.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -06009#include <xyz/openbmc_project/Object/Enable/server.hpp>
10#include <xyz/openbmc_project/User/Ldap/Config/server.hpp>
11#include <xyz/openbmc_project/User/Ldap/Create/server.hpp>
12#include <xyz/openbmc_project/User/PrivilegeMapper/server.hpp>
Ratan Gupta7b04c352019-04-12 21:46:29 +053013
Ratan Gupta21e88cb2019-04-12 17:15:52 +053014#include <filesystem>
Ratan Gupta7b04c352019-04-12 21:46:29 +053015#include <set>
16#include <string>
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050017
18namespace phosphor
19{
20namespace ldap
21{
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050022
Ratan Guptaaeaf9412019-02-11 04:41:52 -060023using ConfigIface = sdbusplus::xyz::openbmc_project::User::Ldap::server::Config;
24using EnableIface = sdbusplus::xyz::openbmc_project::Object::server::Enable;
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050025using CreateIface = sdbusplus::server::object_t<
Ratan Guptaaeaf9412019-02-11 04:41:52 -060026 sdbusplus::xyz::openbmc_project::User::Ldap::server::Create>;
Ratan Gupta21e88cb2019-04-12 17:15:52 +053027namespace fs = std::filesystem;
Ratan Gupta7b04c352019-04-12 21:46:29 +053028using MapperIface =
29 sdbusplus::xyz::openbmc_project::User::server::PrivilegeMapper;
30
31using Ifaces =
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050032 sdbusplus::server::object_t<ConfigIface, EnableIface, MapperIface>;
Ratan Gupta7b04c352019-04-12 21:46:29 +053033using ObjectPath = sdbusplus::message::object_path;
34
Ratan Gupta22f13f12019-04-29 15:36:40 +053035namespace sdbusRule = sdbusplus::bus::match::rules;
36
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050037class ConfigMgr;
Ratan Gupta3a1c2742019-03-20 06:49:42 +053038class MockConfigMgr;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050039
40/** @class Config
41 * @brief Configuration for LDAP.
42 * @details concrete implementation of xyz.openbmc_project.User.Ldap.Config
43 * API, in order to provide LDAP configuration.
44 */
Ratan Guptaaeaf9412019-02-11 04:41:52 -060045class Config : public Ifaces
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050046{
47 public:
48 Config() = delete;
49 ~Config() = default;
50 Config(const Config&) = delete;
51 Config& operator=(const Config&) = delete;
Nan Zhouf3fb77c2022-08-29 17:51:59 +000052 Config(Config&&) = delete;
53 Config& operator=(Config&&) = delete;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050054
55 /** @brief Constructor to put object onto bus at a D-Bus path.
56 * @param[in] bus - Bus to attach to.
57 * @param[in] path - The D-Bus object path to attach at.
58 * @param[in] filePath - LDAP configuration file.
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -060059 * @param[in] caCertFile - LDAP's CA certificate file.
Ratan Gupta22f13f12019-04-29 15:36:40 +053060 * @param[in] certFile - LDAP's client certificate file.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050061 * @param[in] secureLDAP - Specifies whether to use SSL or not.
Patrick Williamse6500a42021-05-01 05:58:23 -050062 * @param[in] ldapServerURI - LDAP URI of the server.
63 * @param[in] ldapBindDN - distinguished name with which to bind.
64 * @param[in] ldapBaseDN - distinguished name to use as search base.
65 * @param[in] ldapBindDNPassword - credentials with which to bind.
66 * @param[in] ldapSearchScope - the search scope.
67 * @param[in] ldapType - Specifies the LDAP server type which can be AD
Ratan Guptaaeaf9412019-02-11 04:41:52 -060068 * or openLDAP.
Patrick Williamse6500a42021-05-01 05:58:23 -050069 * @param[in] ldapServiceEnabled - Specifies whether the service would be
Ratan Guptaaeaf9412019-02-11 04:41:52 -060070 * enabled or not.
71 * @param[in] groupNameAttribute - Specifies attribute name that contains
72 * the name of the Group in the LDAP server.
73 * @param[in] userNameAttribute - Specifies attribute name that contains
74 * the username in the LDAP server.
75 *
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050076 * @param[in] parent - parent of config object.
77 */
78
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050079 Config(sdbusplus::bus_t& bus, const char* path, const char* filePath,
Ratan Gupta22f13f12019-04-29 15:36:40 +053080 const char* caCertFile, const char* certFile, bool secureLDAP,
Patrick Williamse6500a42021-05-01 05:58:23 -050081 std::string ldapServerURI, std::string ldapBindDN,
82 std::string ldapBaseDN, std::string&& ldapBindDNPassword,
83 ConfigIface::SearchScope ldapSearchScope, ConfigIface::Type ldapType,
84 bool ldapServiceEnabled, std::string groupNameAttribute,
Ratan Guptaaeaf9412019-02-11 04:41:52 -060085 std::string userNameAttribute, ConfigMgr& parent);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050086
Ratan Gupta21e88cb2019-04-12 17:15:52 +053087 /** @brief Constructor to put object onto bus at a D-Bus path.
88 * @param[in] bus - Bus to attach to.
89 * @param[in] path - The D-Bus object path to attach at.
90 * @param[in] filePath - LDAP configuration file.
Patrick Williamse6500a42021-05-01 05:58:23 -050091 * @param[in] ldapType - Specifies the LDAP server type which can be AD
Ratan Gupta21e88cb2019-04-12 17:15:52 +053092 * or openLDAP.
93 * @param[in] parent - parent of config object.
94 */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050095 Config(sdbusplus::bus_t& bus, const char* path, const char* filePath,
Ratan Guptaab4fcb42019-04-29 19:39:51 +053096 const char* caCertFile, const char* certFile,
Patrick Williamse6500a42021-05-01 05:58:23 -050097 ConfigIface::Type ldapType, ConfigMgr& parent);
Ratan Gupta21e88cb2019-04-12 17:15:52 +053098
Ratan Guptaaeaf9412019-02-11 04:41:52 -060099 using ConfigIface::groupNameAttribute;
Patrick Williamse6500a42021-05-01 05:58:23 -0500100 using ConfigIface::ldapBaseDN;
101 using ConfigIface::ldapBindDN;
102 using ConfigIface::ldapBindDNPassword;
103 using ConfigIface::ldapSearchScope;
104 using ConfigIface::ldapServerURI;
105 using ConfigIface::ldapType;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500106 using ConfigIface::setPropertyByName;
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600107 using ConfigIface::userNameAttribute;
108 using EnableIface::enabled;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500109
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500110 /** @brief Update the Server URI property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500111 * @param[in] value - ldapServerURI value to be updated.
112 * @returns value of changed ldapServerURI.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500113 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500114 std::string ldapServerURI(std::string value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500115
116 /** @brief Update the BindDN property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500117 * @param[in] value - ldapBindDN value to be updated.
118 * @returns value of changed ldapBindDN.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500119 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500120 std::string ldapBindDN(std::string value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500121
122 /** @brief Update the BaseDN property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500123 * @param[in] value - ldapBaseDN value to be updated.
124 * @returns value of changed ldapBaseDN.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500125 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500126 std::string ldapBaseDN(std::string value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500127
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500128 /** @brief Update the Search scope property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500129 * @param[in] value - ldapSearchScope value to be updated.
130 * @returns value of changed ldapSearchScope.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500131 */
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600132 ConfigIface::SearchScope
Patrick Williamse6500a42021-05-01 05:58:23 -0500133 ldapSearchScope(ConfigIface::SearchScope value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500134
135 /** @brief Update the LDAP Type property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500136 * @param[in] value - ldapType value to be updated.
137 * @returns value of changed ldapType.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500138 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500139 ConfigIface::Type ldapType(ConfigIface::Type value) override;
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600140
141 /** @brief Update the ldapServiceEnabled property.
142 * @param[in] value - ldapServiceEnabled value to be updated.
143 * @returns value of changed ldapServiceEnabled.
144 */
145 bool enabled(bool value) override;
146
147 /** @brief Update the userNameAttribute property.
148 * @param[in] value - userNameAttribute value to be updated.
149 * @returns value of changed userNameAttribute.
150 */
151 std::string userNameAttribute(std::string value) override;
152
153 /** @brief Update the groupNameAttribute property.
154 * @param[in] value - groupNameAttribute value to be updated.
155 * @returns value of changed groupNameAttribute.
156 */
157 std::string groupNameAttribute(std::string value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500158
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530159 /** @brief Update the BindDNPasword property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500160 * @param[in] value - ldapBindDNPassword value to be updated.
161 * @returns value of changed ldapBindDNPassword.
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530162 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500163 std::string ldapBindDNPassword(std::string value) override;
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530164
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530165 /** @brief Function required by Cereal to perform deserialization.
166 * @tparam Archive - Cereal archive type (binary in our case).
167 * @param[in] archive - reference to Cereal archive.
168 * @param[in] version - Class version that enables handling
169 * a serialized data across code levels
170 */
171 template <class Archive>
172 void load(Archive& archive, const std::uint32_t version);
173
174 /** @brief Function required by Cereal to perform serialization.
175 * @tparam Archive - Cereal archive type (binary in our case).
176 * @param[in] archive - reference to Cereal archive.
177 * @param[in] version - Class version that enables handling
178 * a serialized data across code levels
179 */
180 template <class Archive>
181 void save(Archive& archive, const std::uint32_t version) const;
182
183 /** @brief Serialize and persist this object at the persist
184 * location.
185 */
186 void serialize();
187
188 /** @brief Deserialize LDAP config data from the persistent location
189 * into this object
190 * @return bool - true if the deserialization was successful, false
191 * otherwise.
192 */
193 bool deserialize();
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600194
Ratan Guptac5481d12019-04-12 18:31:05 +0530195 /** @brief enable or disable the service with the given value
Manojkiran Eda46e773a2024-06-17 14:45:33 +0530196 * @param[in] value - enable/disable
Ratan Guptac5481d12019-04-12 18:31:05 +0530197 * @returns value of changed status
198 */
199 bool enableService(bool value);
200
Ratan Gupta7b04c352019-04-12 21:46:29 +0530201 /** @brief Creates a mapping for the group to the privilege
202 *
203 * @param[in] groupName - Group Name to which the privilege needs to be
204 * assigned.
205 * @param[in] privilege - The privilege role associated with the group.
206 *
207 * @return On success return the D-Bus object path of the created privilege
208 * mapper entry.
209 */
210 ObjectPath create(std::string groupName, std::string privilege) override;
211
212 /** @brief Delete privilege mapping for LDAP group
213 *
214 * This method deletes the privilege mapping
215 *
216 * @param[in] id - id of the object which needs to be deleted.
217 */
218 void deletePrivilegeMapper(Id id);
219
220 /** @brief Check if LDAP group privilege mapping requested is valid
221 *
222 * Check if the privilege mapping already exists for the LDAP group name
223 * and group name is empty.
224 *
225 * @param[in] groupName - LDAP group name
226 *
227 * @return throw exception if the conditions are not met.
228 */
229 void checkPrivilegeMapper(const std::string& groupName);
230
231 /** @brief Check if the privilege level is a valid one
232 *
233 * @param[in] privilege - Privilege level
234 *
235 * @return throw exception if the conditions are not met.
236 */
237 void checkPrivilegeLevel(const std::string& privilege);
238
239 /** @brief Construct LDAP mapper entry D-Bus objects from their persisted
240 * representations.
241 */
242 void restoreRoleMapping();
243
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500244 private:
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530245 bool secureLDAP;
Patrick Williamse6500a42021-05-01 05:58:23 -0500246 std::string ldapBindPassword{};
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600247 std::string tlsCacertFile{};
Ratan Gupta22f13f12019-04-29 15:36:40 +0530248 std::string tlsCertFile{};
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530249 std::string configFilePath{};
250 std::string objectPath{};
251 std::filesystem::path configPersistPath{};
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500252
253 /** @brief Persistent sdbusplus D-Bus bus connection. */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500254 sdbusplus::bus_t& bus;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500255
256 /** @brief Create a new LDAP config file.
257 */
258 virtual void writeConfig();
259
260 /** @brief reference to config manager object */
261 ConfigMgr& parent;
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530262
Ratan Gupta7b04c352019-04-12 21:46:29 +0530263 /** @brief Id of the last privilege mapper entry */
264 Id entryId = 0;
265
266 /** @brief container to hold privilege mapper objects */
267 std::map<Id, std::unique_ptr<LDAPMapperEntry>> PrivilegeMapperList;
268
269 /** @brief available privileges container */
Richard Marian Thomaiyar32be2962019-11-08 17:21:53 +0530270 std::set<std::string> privMgr = {
271 "priv-admin",
272 "priv-operator",
273 "priv-user",
274 };
Ratan Gupta7b04c352019-04-12 21:46:29 +0530275
Ratan Gupta22f13f12019-04-29 15:36:40 +0530276 /** @brief React to InterfaceAdded signal
277 * @param[in] msg - sdbusplus message
278 */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500279 void certificateInstalled(sdbusplus::message_t& msg);
Ratan Gupta22f13f12019-04-29 15:36:40 +0530280 sdbusplus::bus::match_t certificateInstalledSignal;
281
manojkiranedaa47fe4e2019-05-23 21:28:33 +0530282 sdbusplus::bus::match_t cacertificateInstalledSignal;
283
Ratan Guptaab4fcb42019-04-29 19:39:51 +0530284 /** @brief React to certificate changed signal
285 * @param[in] msg - sdbusplus message
286 */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500287 void certificateChanged(sdbusplus::message_t& msg);
Ratan Guptaab4fcb42019-04-29 19:39:51 +0530288 sdbusplus::bus::match_t certificateChangedSignal;
289
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530290 friend class MockConfigMgr;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500291};
292
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500293} // namespace ldap
294} // namespace phosphor