blob: 0d69f088db5c6f023e65af640e4ec1bd545696af [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.
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600181 * @param[in] caCertFile - LDAP's CA certificate file.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500182 */
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600183 ConfigMgr(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
184 const char* caCertFile) :
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600185 CreateIface(bus, path, true),
186 configFilePath(filePath), bus(bus)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500187 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500188 try
189 {
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600190 restore(configFilePath.c_str());
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500191 emit_object_added();
192 }
193 catch (const std::exception& e)
194 {
195 configPtr.reset(nullptr);
196 log<level::ERR>(e.what());
197 elog<InternalFailure>();
198 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500199 }
200
201 /** @brief concrete implementation of the pure virtual funtion
202 xyz.openbmc_project.User.Ldap.Create.createConfig.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500203 * @param[in] lDAPServerURI - LDAP URI of the server.
204 * @param[in] lDAPBindDN - distinguished name with which bind to bind
205 to the directory server for lookups.
206 * @param[in] lDAPBaseDN - distinguished name to use as search base.
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600207 * @param[in] lDAPBindDNPassword - credentials with which to bind.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500208 * @param[in] lDAPSearchScope - the search scope.
209 * @param[in] lDAPType - Specifies the LDAP server type which can be AD
210 or openLDAP.
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600211 * @param[in] groupNameAttribute - Specifies attribute name that contains
212 * the name of the Group in the LDAP server.
213 * @param[in] usernameAttribute - Specifies attribute name that contains
214 * the username in the LDAP server.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500215 * @returns the object path of the D-Bus object created.
216 */
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600217 std::string createConfig(std::string lDAPServerURI, std::string lDAPBindDN,
218 std::string lDAPBaseDN,
219 std::string lDAPBindDNPassword,
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600220 CreateIface::SearchScope lDAPSearchScope,
221 CreateIface::Type lDAPType,
222 std::string groupNameAttribute,
223 std::string userNameAttribute) override;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500224
225 /** @brief restarts given service
226 * @param[in] service - Service to be restarted.
227 */
228 virtual void restartService(const std::string& service);
229
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500230 /** @brief stops given service
231 * @param[in] service - Service to be stopped.
232 */
233 virtual void stopService(const std::string& service);
234
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600235 /** @brief start or stop the service depending on the given value
236 * @param[in] service - Service to be start/stop.
237 * @param[in] value - true to start the service otherwise stop.
238 */
239 virtual void startOrStopService(const std::string& service, bool value);
240
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500241 /** @brief delete the config D-Bus object.
242 */
243 void deleteObject();
244
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600245 protected:
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600246 std::string configFilePath{};
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600247 std::string tlsCacertFile{};
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600248
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500249 /** @brief Persistent sdbusplus D-Bus bus connection. */
250 sdbusplus::bus::bus& bus;
251
252 /** @brief Pointer to a Config D-Bus object */
253 std::unique_ptr<Config> configPtr = nullptr;
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500254
255 /** @brief Populate existing config into D-Bus properties
256 * @param[in] filePath - LDAP config file path
257 */
258 virtual void restore(const char* filePath);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500259};
260} // namespace ldap
261} // namespace phosphor