blob: 034aab8857208d2bb3a61adada499e9e7eda9a06 [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>
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -05005#include <xyz/openbmc_project/User/Ldap/Config/server.hpp>
6#include <xyz/openbmc_project/User/Ldap/Create/server.hpp>
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -05007#include <xyz/openbmc_project/Common/error.hpp>
8#include <phosphor-logging/log.hpp>
9#include <phosphor-logging/elog.hpp>
10#include <phosphor-logging/elog-errors.hpp>
11#include <sdbusplus/bus.hpp>
12#include <sdbusplus/server/object.hpp>
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050013#include <string>
14
15namespace phosphor
16{
17namespace ldap
18{
19static constexpr auto defaultNslcdFile = "/etc/nslcd.conf.default";
20static constexpr auto nsSwitchFile = "/etc/nsswitch.conf";
21static constexpr auto LDAPNsSwitchFile = "/etc/nsswitch_ldap.conf";
22static constexpr auto linuxNsSwitchFile = "/etc/nsswitch_linux.conf";
23
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -050024using namespace phosphor::logging;
25using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050026namespace ldap_base = sdbusplus::xyz::openbmc_project::User::Ldap::server;
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -050027using ConfigIface = sdbusplus::server::object::object<
28 ldap_base::Config, sdbusplus::xyz::openbmc_project::Object::server::Delete>;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050029using CreateIface = sdbusplus::server::object::object<ldap_base::Create>;
30
31class ConfigMgr;
32
33/** @class Config
34 * @brief Configuration for LDAP.
35 * @details concrete implementation of xyz.openbmc_project.User.Ldap.Config
36 * API, in order to provide LDAP configuration.
37 */
38class Config : public ConfigIface
39{
40 public:
41 Config() = delete;
42 ~Config() = default;
43 Config(const Config&) = delete;
44 Config& operator=(const Config&) = delete;
45 Config(Config&&) = default;
46 Config& operator=(Config&&) = default;
47
48 /** @brief Constructor to put object onto bus at a D-Bus path.
49 * @param[in] bus - Bus to attach to.
50 * @param[in] path - The D-Bus object path to attach at.
51 * @param[in] filePath - LDAP configuration file.
52 * @param[in] secureLDAP - Specifies whether to use SSL or not.
53 * @param[in] lDAPServerURI - LDAP URI of the server.
54 * @param[in] lDAPBindDN - distinguished name with which to bind.
55 * @param[in] lDAPBaseDN - distinguished name to use as search base.
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060056 * @param[in] lDAPBindDNPassword - credentials with which to bind.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050057 * @param[in] lDAPSearchScope - the search scope.
58 * @param[in] lDAPType - Specifies the LDAP server type which can be AD
59 or openLDAP.
60 * @param[in] parent - parent of config object.
61 */
62
63 Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
64 bool secureLDAP, std::string lDAPServerURI, std::string lDAPBindDN,
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060065 std::string lDAPBaseDN, std::string&& lDAPBindDNPassword,
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050066 ldap_base::Config::SearchScope lDAPSearchScope,
67 ldap_base::Config::Type lDAPType, ConfigMgr& parent);
68
69 using ConfigIface::lDAPBaseDN;
70 using ConfigIface::lDAPBindDN;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050071 using ConfigIface::lDAPSearchScope;
72 using ConfigIface::lDAPServerURI;
73 using ConfigIface::lDAPType;
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050074 using ConfigIface::setPropertyByName;
75
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050076 /** @brief Update the Server URI property.
77 * @param[in] value - lDAPServerURI value to be updated.
78 * @returns value of changed lDAPServerURI.
79 */
80 std::string lDAPServerURI(std::string value) override;
81
82 /** @brief Update the BindDN property.
83 * @param[in] value - lDAPBindDN value to be updated.
84 * @returns value of changed lDAPBindDN.
85 */
86 std::string lDAPBindDN(std::string value) override;
87
88 /** @brief Update the BaseDN property.
89 * @param[in] value - lDAPBaseDN value to be updated.
90 * @returns value of changed lDAPBaseDN.
91 */
92 std::string lDAPBaseDN(std::string value) override;
93
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050094 /** @brief Update the Search scope property.
95 * @param[in] value - lDAPSearchScope value to be updated.
96 * @returns value of changed lDAPSearchScope.
97 */
98 ldap_base::Config::SearchScope
99 lDAPSearchScope(ldap_base::Config::SearchScope value) override;
100
101 /** @brief Update the LDAP Type property.
102 * @param[in] value - lDAPType value to be updated.
103 * @returns value of changed lDAPType.
104 */
105 ldap_base::Config::Type lDAPType(ldap_base::Config::Type value) override;
106
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500107 /** @brief Delete this D-bus object.
108 */
109 void delete_() override;
110
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600111 bool secureLDAP;
112
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500113 private:
114 std::string configFilePath{};
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600115 std::string lDAPBindDNPassword{};
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500116
117 /** @brief Persistent sdbusplus D-Bus bus connection. */
118 sdbusplus::bus::bus& bus;
119
120 /** @brief Create a new LDAP config file.
121 */
122 virtual void writeConfig();
123
124 /** @brief reference to config manager object */
125 ConfigMgr& parent;
126};
127
128/** @class ConfigMgr
129 * @brief Creates LDAP server configuration.
130 * @details concrete implementation of xyz.openbmc_project.User.Ldap.Create
131 * APIs, in order to create LDAP configuration.
132 */
133class ConfigMgr : public CreateIface
134{
135 public:
136 ConfigMgr() = delete;
137 ~ConfigMgr() = default;
138 ConfigMgr(const ConfigMgr&) = delete;
139 ConfigMgr& operator=(const ConfigMgr&) = delete;
140 ConfigMgr(ConfigMgr&&) = delete;
141 ConfigMgr& operator=(ConfigMgr&&) = delete;
142
143 /** @brief ConfigMgr to put object onto bus at a dbus path.
144 * @param[in] bus - Bus to attach to.
145 * @param[in] path - Path to attach at.
146 * @param[in] filePath - LDAP configuration file.
147 */
148 ConfigMgr(sdbusplus::bus::bus& bus, const char* path) :
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500149 CreateIface(bus, path, true), bus(bus)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500150 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500151 try
152 {
153 restore(LDAP_CONFIG_FILE);
154 emit_object_added();
155 }
156 catch (const std::exception& e)
157 {
158 configPtr.reset(nullptr);
159 log<level::ERR>(e.what());
160 elog<InternalFailure>();
161 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500162 }
163
164 /** @brief concrete implementation of the pure virtual funtion
165 xyz.openbmc_project.User.Ldap.Create.createConfig.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500166 * @param[in] lDAPServerURI - LDAP URI of the server.
167 * @param[in] lDAPBindDN - distinguished name with which bind to bind
168 to the directory server for lookups.
169 * @param[in] lDAPBaseDN - distinguished name to use as search base.
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600170 * @param[in] lDAPBindDNPassword - credentials with which to bind.
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500171 * @param[in] lDAPSearchScope - the search scope.
172 * @param[in] lDAPType - Specifies the LDAP server type which can be AD
173 or openLDAP.
174 * @returns the object path of the D-Bus object created.
175 */
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600176 std::string createConfig(std::string lDAPServerURI, std::string lDAPBindDN,
177 std::string lDAPBaseDN,
178 std::string lDAPBindDNPassword,
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500179 ldap_base::Create::SearchScope lDAPSearchScope,
180 ldap_base::Create::Type lDAPType) override;
181
182 /** @brief restarts given service
183 * @param[in] service - Service to be restarted.
184 */
185 virtual void restartService(const std::string& service);
186
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500187 /** @brief stops given service
188 * @param[in] service - Service to be stopped.
189 */
190 virtual void stopService(const std::string& service);
191
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500192 /** @brief delete the config D-Bus object.
193 */
194 void deleteObject();
195
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500196 private:
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600197 std::string configFilePath{};
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500198 /** @brief Persistent sdbusplus D-Bus bus connection. */
199 sdbusplus::bus::bus& bus;
200
201 /** @brief Pointer to a Config D-Bus object */
202 std::unique_ptr<Config> configPtr = nullptr;
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500203
204 /** @brief Populate existing config into D-Bus properties
205 * @param[in] filePath - LDAP config file path
206 */
207 virtual void restore(const char* filePath);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500208};
209} // namespace ldap
210} // namespace phosphor