blob: c1ba5ec8b7b66839450edef84500f1ba483cc736 [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 <phosphor-logging/elog-errors.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -06008#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/log.hpp>
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -050010#include <sdbusplus/bus.hpp>
11#include <sdbusplus/server/object.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -060012#include <xyz/openbmc_project/Common/error.hpp>
13#include <xyz/openbmc_project/Object/Enable/server.hpp>
14#include <xyz/openbmc_project/User/Ldap/Config/server.hpp>
15#include <xyz/openbmc_project/User/Ldap/Create/server.hpp>
16#include <xyz/openbmc_project/User/PrivilegeMapper/server.hpp>
Ratan Gupta7b04c352019-04-12 21:46:29 +053017
Ratan Gupta21e88cb2019-04-12 17:15:52 +053018#include <filesystem>
Ratan Gupta7b04c352019-04-12 21:46:29 +053019#include <set>
20#include <string>
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050021
22namespace phosphor
23{
24namespace ldap
25{
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050026
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -050027using namespace phosphor::logging;
28using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060029using ConfigIface = sdbusplus::xyz::openbmc_project::User::Ldap::server::Config;
30using EnableIface = sdbusplus::xyz::openbmc_project::Object::server::Enable;
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050031using CreateIface = sdbusplus::server::object_t<
Ratan Guptaaeaf9412019-02-11 04:41:52 -060032 sdbusplus::xyz::openbmc_project::User::Ldap::server::Create>;
Ratan Gupta21e88cb2019-04-12 17:15:52 +053033namespace fs = std::filesystem;
Ratan Gupta7b04c352019-04-12 21:46:29 +053034using MapperIface =
35 sdbusplus::xyz::openbmc_project::User::server::PrivilegeMapper;
36
37using Ifaces =
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050038 sdbusplus::server::object_t<ConfigIface, EnableIface, MapperIface>;
Ratan Gupta7b04c352019-04-12 21:46:29 +053039using ObjectPath = sdbusplus::message::object_path;
40
Ratan Gupta22f13f12019-04-29 15:36:40 +053041namespace sdbusRule = sdbusplus::bus::match::rules;
42
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050043class ConfigMgr;
Ratan Gupta3a1c2742019-03-20 06:49:42 +053044class MockConfigMgr;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050045
46/** @class Config
47 * @brief Configuration for LDAP.
48 * @details concrete implementation of xyz.openbmc_project.User.Ldap.Config
49 * API, in order to provide LDAP configuration.
50 */
Ratan Guptaaeaf9412019-02-11 04:41:52 -060051class Config : public Ifaces
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050052{
53 public:
54 Config() = delete;
55 ~Config() = default;
56 Config(const Config&) = delete;
57 Config& operator=(const Config&) = delete;
Nan Zhouf3fb77c2022-08-29 17:51:59 +000058 Config(Config&&) = delete;
59 Config& operator=(Config&&) = delete;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050060
61 /** @brief Constructor to put object onto bus at a D-Bus path.
62 * @param[in] bus - Bus to attach to.
63 * @param[in] path - The D-Bus object path to attach at.
64 * @param[in] filePath - LDAP configuration file.
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -060065 * @param[in] caCertFile - LDAP's CA certificate file.
Ratan Gupta22f13f12019-04-29 15:36:40 +053066 * @param[in] certFile - LDAP's client certificate file.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050067 * @param[in] secureLDAP - Specifies whether to use SSL or not.
Patrick Williamse6500a42021-05-01 05:58:23 -050068 * @param[in] ldapServerURI - LDAP URI of the server.
69 * @param[in] ldapBindDN - distinguished name with which to bind.
70 * @param[in] ldapBaseDN - distinguished name to use as search base.
71 * @param[in] ldapBindDNPassword - credentials with which to bind.
72 * @param[in] ldapSearchScope - the search scope.
73 * @param[in] ldapType - Specifies the LDAP server type which can be AD
Ratan Guptaaeaf9412019-02-11 04:41:52 -060074 * or openLDAP.
Patrick Williamse6500a42021-05-01 05:58:23 -050075 * @param[in] ldapServiceEnabled - Specifies whether the service would be
Ratan Guptaaeaf9412019-02-11 04:41:52 -060076 * enabled or not.
77 * @param[in] groupNameAttribute - Specifies attribute name that contains
78 * the name of the Group in the LDAP server.
79 * @param[in] userNameAttribute - Specifies attribute name that contains
80 * the username in the LDAP server.
81 *
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050082 * @param[in] parent - parent of config object.
83 */
84
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050085 Config(sdbusplus::bus_t& bus, const char* path, const char* filePath,
Ratan Gupta22f13f12019-04-29 15:36:40 +053086 const char* caCertFile, const char* certFile, bool secureLDAP,
Patrick Williamse6500a42021-05-01 05:58:23 -050087 std::string ldapServerURI, std::string ldapBindDN,
88 std::string ldapBaseDN, std::string&& ldapBindDNPassword,
89 ConfigIface::SearchScope ldapSearchScope, ConfigIface::Type ldapType,
90 bool ldapServiceEnabled, std::string groupNameAttribute,
Ratan Guptaaeaf9412019-02-11 04:41:52 -060091 std::string userNameAttribute, ConfigMgr& parent);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050092
Ratan Gupta21e88cb2019-04-12 17:15:52 +053093 /** @brief Constructor to put object onto bus at a D-Bus path.
94 * @param[in] bus - Bus to attach to.
95 * @param[in] path - The D-Bus object path to attach at.
96 * @param[in] filePath - LDAP configuration file.
Patrick Williamse6500a42021-05-01 05:58:23 -050097 * @param[in] ldapType - Specifies the LDAP server type which can be AD
Ratan Gupta21e88cb2019-04-12 17:15:52 +053098 * or openLDAP.
99 * @param[in] parent - parent of config object.
100 */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500101 Config(sdbusplus::bus_t& bus, const char* path, const char* filePath,
Ratan Guptaab4fcb42019-04-29 19:39:51 +0530102 const char* caCertFile, const char* certFile,
Patrick Williamse6500a42021-05-01 05:58:23 -0500103 ConfigIface::Type ldapType, ConfigMgr& parent);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530104
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600105 using ConfigIface::groupNameAttribute;
Patrick Williamse6500a42021-05-01 05:58:23 -0500106 using ConfigIface::ldapBaseDN;
107 using ConfigIface::ldapBindDN;
108 using ConfigIface::ldapBindDNPassword;
109 using ConfigIface::ldapSearchScope;
110 using ConfigIface::ldapServerURI;
111 using ConfigIface::ldapType;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500112 using ConfigIface::setPropertyByName;
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600113 using ConfigIface::userNameAttribute;
114 using EnableIface::enabled;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500115
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500116 /** @brief Update the Server URI property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500117 * @param[in] value - ldapServerURI value to be updated.
118 * @returns value of changed ldapServerURI.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500119 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500120 std::string ldapServerURI(std::string value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500121
122 /** @brief Update the BindDN property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500123 * @param[in] value - ldapBindDN value to be updated.
124 * @returns value of changed ldapBindDN.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500125 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500126 std::string ldapBindDN(std::string value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500127
128 /** @brief Update the BaseDN property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500129 * @param[in] value - ldapBaseDN value to be updated.
130 * @returns value of changed ldapBaseDN.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500131 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500132 std::string ldapBaseDN(std::string value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500133
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500134 /** @brief Update the Search scope property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500135 * @param[in] value - ldapSearchScope value to be updated.
136 * @returns value of changed ldapSearchScope.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500137 */
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600138 ConfigIface::SearchScope
Patrick Williamse6500a42021-05-01 05:58:23 -0500139 ldapSearchScope(ConfigIface::SearchScope value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500140
141 /** @brief Update the LDAP Type property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500142 * @param[in] value - ldapType value to be updated.
143 * @returns value of changed ldapType.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500144 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500145 ConfigIface::Type ldapType(ConfigIface::Type value) override;
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600146
147 /** @brief Update the ldapServiceEnabled property.
148 * @param[in] value - ldapServiceEnabled value to be updated.
149 * @returns value of changed ldapServiceEnabled.
150 */
151 bool enabled(bool value) override;
152
153 /** @brief Update the userNameAttribute property.
154 * @param[in] value - userNameAttribute value to be updated.
155 * @returns value of changed userNameAttribute.
156 */
157 std::string userNameAttribute(std::string value) override;
158
159 /** @brief Update the groupNameAttribute property.
160 * @param[in] value - groupNameAttribute value to be updated.
161 * @returns value of changed groupNameAttribute.
162 */
163 std::string groupNameAttribute(std::string value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500164
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530165 /** @brief Update the BindDNPasword property.
Patrick Williamse6500a42021-05-01 05:58:23 -0500166 * @param[in] value - ldapBindDNPassword value to be updated.
167 * @returns value of changed ldapBindDNPassword.
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530168 */
Patrick Williamse6500a42021-05-01 05:58:23 -0500169 std::string ldapBindDNPassword(std::string value) override;
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530170
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530171 /** @brief Function required by Cereal to perform deserialization.
172 * @tparam Archive - Cereal archive type (binary in our case).
173 * @param[in] archive - reference to Cereal archive.
174 * @param[in] version - Class version that enables handling
175 * a serialized data across code levels
176 */
177 template <class Archive>
178 void load(Archive& archive, const std::uint32_t version);
179
180 /** @brief Function required by Cereal to perform serialization.
181 * @tparam Archive - Cereal archive type (binary in our case).
182 * @param[in] archive - reference to Cereal archive.
183 * @param[in] version - Class version that enables handling
184 * a serialized data across code levels
185 */
186 template <class Archive>
187 void save(Archive& archive, const std::uint32_t version) const;
188
189 /** @brief Serialize and persist this object at the persist
190 * location.
191 */
192 void serialize();
193
194 /** @brief Deserialize LDAP config data from the persistent location
195 * into this object
196 * @return bool - true if the deserialization was successful, false
197 * otherwise.
198 */
199 bool deserialize();
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600200
Ratan Guptac5481d12019-04-12 18:31:05 +0530201 /** @brief enable or disable the service with the given value
202 * @param[in] value - enable/disble
203 * @returns value of changed status
204 */
205 bool enableService(bool value);
206
Ratan Gupta7b04c352019-04-12 21:46:29 +0530207 /** @brief Creates a mapping for the group to the privilege
208 *
209 * @param[in] groupName - Group Name to which the privilege needs to be
210 * assigned.
211 * @param[in] privilege - The privilege role associated with the group.
212 *
213 * @return On success return the D-Bus object path of the created privilege
214 * mapper entry.
215 */
216 ObjectPath create(std::string groupName, std::string privilege) override;
217
218 /** @brief Delete privilege mapping for LDAP group
219 *
220 * This method deletes the privilege mapping
221 *
222 * @param[in] id - id of the object which needs to be deleted.
223 */
224 void deletePrivilegeMapper(Id id);
225
226 /** @brief Check if LDAP group privilege mapping requested is valid
227 *
228 * Check if the privilege mapping already exists for the LDAP group name
229 * and group name is empty.
230 *
231 * @param[in] groupName - LDAP group name
232 *
233 * @return throw exception if the conditions are not met.
234 */
235 void checkPrivilegeMapper(const std::string& groupName);
236
237 /** @brief Check if the privilege level is a valid one
238 *
239 * @param[in] privilege - Privilege level
240 *
241 * @return throw exception if the conditions are not met.
242 */
243 void checkPrivilegeLevel(const std::string& privilege);
244
245 /** @brief Construct LDAP mapper entry D-Bus objects from their persisted
246 * representations.
247 */
248 void restoreRoleMapping();
249
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500250 private:
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530251 bool secureLDAP;
Patrick Williamse6500a42021-05-01 05:58:23 -0500252 std::string ldapBindPassword{};
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600253 std::string tlsCacertFile{};
Ratan Gupta22f13f12019-04-29 15:36:40 +0530254 std::string tlsCertFile{};
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530255 std::string configFilePath{};
256 std::string objectPath{};
257 std::filesystem::path configPersistPath{};
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500258
259 /** @brief Persistent sdbusplus D-Bus bus connection. */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500260 sdbusplus::bus_t& bus;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500261
262 /** @brief Create a new LDAP config file.
263 */
264 virtual void writeConfig();
265
266 /** @brief reference to config manager object */
267 ConfigMgr& parent;
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530268
Ratan Gupta7b04c352019-04-12 21:46:29 +0530269 /** @brief Id of the last privilege mapper entry */
270 Id entryId = 0;
271
272 /** @brief container to hold privilege mapper objects */
273 std::map<Id, std::unique_ptr<LDAPMapperEntry>> PrivilegeMapperList;
274
275 /** @brief available privileges container */
Richard Marian Thomaiyar32be2962019-11-08 17:21:53 +0530276 std::set<std::string> privMgr = {
277 "priv-admin",
278 "priv-operator",
279 "priv-user",
280 };
Ratan Gupta7b04c352019-04-12 21:46:29 +0530281
Ratan Gupta22f13f12019-04-29 15:36:40 +0530282 /** @brief React to InterfaceAdded signal
283 * @param[in] msg - sdbusplus message
284 */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500285 void certificateInstalled(sdbusplus::message_t& msg);
Ratan Gupta22f13f12019-04-29 15:36:40 +0530286 sdbusplus::bus::match_t certificateInstalledSignal;
287
manojkiranedaa47fe4e2019-05-23 21:28:33 +0530288 sdbusplus::bus::match_t cacertificateInstalledSignal;
289
Ratan Guptaab4fcb42019-04-29 19:39:51 +0530290 /** @brief React to certificate changed signal
291 * @param[in] msg - sdbusplus message
292 */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500293 void certificateChanged(sdbusplus::message_t& msg);
Ratan Guptaab4fcb42019-04-29 19:39:51 +0530294 sdbusplus::bus::match_t certificateChangedSignal;
295
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530296 friend class MockConfigMgr;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500297};
298
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500299} // namespace ldap
300} // namespace phosphor