blob: 976aac6bffe32c09ca4b00f5ed6f13457203bb3b [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"
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -05004#include <xyz/openbmc_project/Object/Delete/server.hpp>
Ratan Guptaaeaf9412019-02-11 04:41:52 -06005#include <xyz/openbmc_project/Object/Enable/server.hpp>
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -05006#include <xyz/openbmc_project/User/Ldap/Config/server.hpp>
7#include <xyz/openbmc_project/User/Ldap/Create/server.hpp>
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -05008#include <xyz/openbmc_project/Common/error.hpp>
9#include <phosphor-logging/log.hpp>
10#include <phosphor-logging/elog.hpp>
11#include <phosphor-logging/elog-errors.hpp>
12#include <sdbusplus/bus.hpp>
13#include <sdbusplus/server/object.hpp>
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050014#include <string>
15
16namespace phosphor
17{
18namespace ldap
19{
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060020static constexpr auto defaultNslcdFile = "nslcd.conf.default";
21static constexpr auto nsSwitchFile = "nsswitch.conf";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050022
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -050023using namespace phosphor::logging;
24using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060025using ConfigIface = sdbusplus::xyz::openbmc_project::User::Ldap::server::Config;
26using EnableIface = sdbusplus::xyz::openbmc_project::Object::server::Enable;
27using DeleteIface = sdbusplus::xyz::openbmc_project::Object::server::Delete;
28using Ifaces =
29 sdbusplus::server::object::object<ConfigIface, EnableIface, DeleteIface>;
30using CreateIface = sdbusplus::server::object::object<
31 sdbusplus::xyz::openbmc_project::User::Ldap::server::Create>;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050032
33class ConfigMgr;
34
35/** @class Config
36 * @brief Configuration for LDAP.
37 * @details concrete implementation of xyz.openbmc_project.User.Ldap.Config
38 * API, in order to provide LDAP configuration.
39 */
Ratan Guptaaeaf9412019-02-11 04:41:52 -060040class Config : public Ifaces
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050041{
42 public:
43 Config() = delete;
44 ~Config() = default;
45 Config(const Config&) = delete;
46 Config& operator=(const Config&) = delete;
47 Config(Config&&) = default;
48 Config& operator=(Config&&) = default;
49
50 /** @brief Constructor to put object onto bus at a D-Bus path.
51 * @param[in] bus - Bus to attach to.
52 * @param[in] path - The D-Bus object path to attach at.
53 * @param[in] filePath - LDAP configuration file.
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -060054 * @param[in] caCertFile - LDAP's CA certificate file.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050055 * @param[in] secureLDAP - Specifies whether to use SSL or not.
56 * @param[in] lDAPServerURI - LDAP URI of the server.
57 * @param[in] lDAPBindDN - distinguished name with which to bind.
58 * @param[in] lDAPBaseDN - distinguished name to use as search base.
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060059 * @param[in] lDAPBindDNPassword - credentials with which to bind.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050060 * @param[in] lDAPSearchScope - the search scope.
61 * @param[in] lDAPType - Specifies the LDAP server type which can be AD
Ratan Guptaaeaf9412019-02-11 04:41:52 -060062 * or openLDAP.
63 * @param[in] lDAPServiceEnabled - Specifies whether the service would be
64 * enabled or not.
65 * @param[in] groupNameAttribute - Specifies attribute name that contains
66 * the name of the Group in the LDAP server.
67 * @param[in] userNameAttribute - Specifies attribute name that contains
68 * the username in the LDAP server.
69 *
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050070 * @param[in] parent - parent of config object.
71 */
72
73 Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -060074 const char* caCertFile, bool secureLDAP, std::string lDAPServerURI,
75 std::string lDAPBindDN, std::string lDAPBaseDN,
76 std::string&& lDAPBindDNPassword,
Ratan Guptaaeaf9412019-02-11 04:41:52 -060077 ConfigIface::SearchScope lDAPSearchScope, ConfigIface::Type lDAPType,
78 bool lDAPServiceEnabled, std::string groupNameAttribute,
79 std::string userNameAttribute, ConfigMgr& parent);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050080
Ratan Guptaaeaf9412019-02-11 04:41:52 -060081 using ConfigIface::groupNameAttribute;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050082 using ConfigIface::lDAPBaseDN;
83 using ConfigIface::lDAPBindDN;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050084 using ConfigIface::lDAPSearchScope;
85 using ConfigIface::lDAPServerURI;
86 using ConfigIface::lDAPType;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050087 using ConfigIface::setPropertyByName;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060088 using ConfigIface::userNameAttribute;
89 using EnableIface::enabled;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050090
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050091 /** @brief Update the Server URI property.
92 * @param[in] value - lDAPServerURI value to be updated.
93 * @returns value of changed lDAPServerURI.
94 */
95 std::string lDAPServerURI(std::string value) override;
96
97 /** @brief Update the BindDN property.
98 * @param[in] value - lDAPBindDN value to be updated.
99 * @returns value of changed lDAPBindDN.
100 */
101 std::string lDAPBindDN(std::string value) override;
102
103 /** @brief Update the BaseDN property.
104 * @param[in] value - lDAPBaseDN value to be updated.
105 * @returns value of changed lDAPBaseDN.
106 */
107 std::string lDAPBaseDN(std::string value) override;
108
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500109 /** @brief Update the Search scope property.
110 * @param[in] value - lDAPSearchScope value to be updated.
111 * @returns value of changed lDAPSearchScope.
112 */
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600113 ConfigIface::SearchScope
114 lDAPSearchScope(ConfigIface::SearchScope value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500115
116 /** @brief Update the LDAP Type property.
117 * @param[in] value - lDAPType value to be updated.
118 * @returns value of changed lDAPType.
119 */
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600120 ConfigIface::Type lDAPType(ConfigIface::Type value) override;
121
122 /** @brief Update the ldapServiceEnabled property.
123 * @param[in] value - ldapServiceEnabled value to be updated.
124 * @returns value of changed ldapServiceEnabled.
125 */
126 bool enabled(bool value) override;
127
128 /** @brief Update the userNameAttribute property.
129 * @param[in] value - userNameAttribute value to be updated.
130 * @returns value of changed userNameAttribute.
131 */
132 std::string userNameAttribute(std::string value) override;
133
134 /** @brief Update the groupNameAttribute property.
135 * @param[in] value - groupNameAttribute value to be updated.
136 * @returns value of changed groupNameAttribute.
137 */
138 std::string groupNameAttribute(std::string value) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500139
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500140 /** @brief Delete this D-bus object.
141 */
142 void delete_() override;
143
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600144 bool secureLDAP;
145
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500146 private:
147 std::string configFilePath{};
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600148 std::string tlsCacertFile{};
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600149 std::string lDAPBindDNPassword{};
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500150
151 /** @brief Persistent sdbusplus D-Bus bus connection. */
152 sdbusplus::bus::bus& bus;
153
154 /** @brief Create a new LDAP config file.
155 */
156 virtual void writeConfig();
157
158 /** @brief reference to config manager object */
159 ConfigMgr& parent;
160};
161
162/** @class ConfigMgr
163 * @brief Creates LDAP server configuration.
164 * @details concrete implementation of xyz.openbmc_project.User.Ldap.Create
165 * APIs, in order to create LDAP configuration.
166 */
167class ConfigMgr : public CreateIface
168{
169 public:
170 ConfigMgr() = delete;
171 ~ConfigMgr() = default;
172 ConfigMgr(const ConfigMgr&) = delete;
173 ConfigMgr& operator=(const ConfigMgr&) = delete;
174 ConfigMgr(ConfigMgr&&) = delete;
175 ConfigMgr& operator=(ConfigMgr&&) = delete;
176
177 /** @brief ConfigMgr to put object onto bus at a dbus path.
178 * @param[in] bus - Bus to attach to.
179 * @param[in] path - Path to attach at.
180 * @param[in] filePath - LDAP configuration file.
Ratan Gupta95a29312019-02-18 20:34:10 +0530181 * @param[in] dbusPersistentPath - Persistent path for LDAP D-Bus property.
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600182 * @param[in] caCertFile - LDAP's CA certificate file.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500183 */
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600184 ConfigMgr(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
Ratan Gupta95a29312019-02-18 20:34:10 +0530185 const char* dbusPersistentPath, const char* caCertFile) :
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600186 CreateIface(bus, path, true),
Ratan Gupta95a29312019-02-18 20:34:10 +0530187 dbusPersistentPath(dbusPersistentPath), configFilePath(filePath),
188 bus(bus)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500189 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500190 try
191 {
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600192 restore(configFilePath.c_str());
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500193 emit_object_added();
194 }
195 catch (const std::exception& e)
196 {
197 configPtr.reset(nullptr);
198 log<level::ERR>(e.what());
199 elog<InternalFailure>();
200 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500201 }
202
203 /** @brief concrete implementation of the pure virtual funtion
204 xyz.openbmc_project.User.Ldap.Create.createConfig.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500205 * @param[in] lDAPServerURI - LDAP URI of the server.
206 * @param[in] lDAPBindDN - distinguished name with which bind to bind
207 to the directory server for lookups.
208 * @param[in] lDAPBaseDN - distinguished name to use as search base.
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600209 * @param[in] lDAPBindDNPassword - credentials with which to bind.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500210 * @param[in] lDAPSearchScope - the search scope.
211 * @param[in] lDAPType - Specifies the LDAP server type which can be AD
212 or openLDAP.
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600213 * @param[in] groupNameAttribute - Specifies attribute name that contains
214 * the name of the Group in the LDAP server.
215 * @param[in] usernameAttribute - Specifies attribute name that contains
216 * the username in the LDAP server.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500217 * @returns the object path of the D-Bus object created.
218 */
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600219 std::string createConfig(std::string lDAPServerURI, std::string lDAPBindDN,
220 std::string lDAPBaseDN,
221 std::string lDAPBindDNPassword,
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600222 CreateIface::SearchScope lDAPSearchScope,
223 CreateIface::Type lDAPType,
224 std::string groupNameAttribute,
225 std::string userNameAttribute) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500226
227 /** @brief restarts given service
228 * @param[in] service - Service to be restarted.
229 */
230 virtual void restartService(const std::string& service);
231
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500232 /** @brief stops given service
233 * @param[in] service - Service to be stopped.
234 */
235 virtual void stopService(const std::string& service);
236
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600237 /** @brief start or stop the service depending on the given value
238 * @param[in] service - Service to be start/stop.
239 * @param[in] value - true to start the service otherwise stop.
240 */
241 virtual void startOrStopService(const std::string& service, bool value);
242
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500243 /** @brief delete the config D-Bus object.
244 */
245 void deleteObject();
246
Ratan Gupta95a29312019-02-18 20:34:10 +0530247 /* ldap service enabled property would be saved under
248 * this path.
249 */
250 std::string dbusPersistentPath;
251
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600252 protected:
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600253 std::string configFilePath{};
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600254 std::string tlsCacertFile{};
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600255
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500256 /** @brief Persistent sdbusplus D-Bus bus connection. */
257 sdbusplus::bus::bus& bus;
258
259 /** @brief Pointer to a Config D-Bus object */
260 std::unique_ptr<Config> configPtr = nullptr;
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500261
262 /** @brief Populate existing config into D-Bus properties
263 * @param[in] filePath - LDAP config file path
264 */
265 virtual void restore(const char* filePath);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500266};
267} // namespace ldap
268} // namespace phosphor