blob: 03c6ffc41f91d259a65bbca227d825d9a1680081 [file] [log] [blame]
Ratan Guptae1f4db62019-04-11 18:57:42 +05301#include "ldap_config_mgr.hpp"
Ratan Gupta37fb3fe2019-04-13 12:54:18 +05302#include "ldap_config.hpp"
Nagaraju Goruganti59287f02018-10-12 07:00:20 -05003#include "utils.hpp"
Ratan Gupta21e88cb2019-04-12 17:15:52 +05304
5#include <cereal/types/string.hpp>
6#include <cereal/types/vector.hpp>
7#include <cereal/archives/binary.hpp>
Ratan Gupta95a29312019-02-18 20:34:10 +05308#include <filesystem>
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -05009#include <fstream>
10#include <sstream>
11
Ratan Gupta21e88cb2019-04-12 17:15:52 +053012// Register class version
13// From cereal documentation;
14// "This macro should be placed at global scope"
15CEREAL_CLASS_VERSION(phosphor::ldap::Config, CLASS_VERSION);
16
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050017namespace phosphor
18{
19namespace ldap
20{
Ratan Guptae1f4db62019-04-11 18:57:42 +053021
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050022constexpr auto nslcdService = "nslcd.service";
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -050023constexpr auto nscdService = "nscd.service";
Nagaraju Goruganti59287f02018-10-12 07:00:20 -050024constexpr auto LDAPscheme = "ldap";
25constexpr auto LDAPSscheme = "ldaps";
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 Gupta95a29312019-02-18 20:34:10 +053029namespace fs = std::filesystem;
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -050030using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta27d4c012019-04-12 13:03:35 +053031using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
32using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed;
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -050033
34using Line = std::string;
35using Key = std::string;
36using Val = std::string;
37using ConfigInfo = std::map<Key, Val>;
38
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050039Config::Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -060040 const char* caCertFile, bool secureLDAP,
41 std::string lDAPServerURI, std::string lDAPBindDN,
42 std::string lDAPBaseDN, std::string&& lDAPBindDNPassword,
Ratan Guptaaeaf9412019-02-11 04:41:52 -060043 ConfigIface::SearchScope lDAPSearchScope,
44 ConfigIface::Type lDAPType, bool lDAPServiceEnabled,
45 std::string userNameAttr, std::string groupNameAttr,
46 ConfigMgr& parent) :
47 Ifaces(bus, path, true),
Ratan Gupta3a1c2742019-03-20 06:49:42 +053048 secureLDAP(secureLDAP), lDAPBindPassword(std::move(lDAPBindDNPassword)),
Ratan Gupta21e88cb2019-04-12 17:15:52 +053049 tlsCacertFile(caCertFile), configFilePath(filePath), objectPath(path),
50 bus(bus), parent(parent)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050051{
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050052 ConfigIface::lDAPServerURI(lDAPServerURI);
53 ConfigIface::lDAPBindDN(lDAPBindDN);
54 ConfigIface::lDAPBaseDN(lDAPBaseDN);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050055 ConfigIface::lDAPSearchScope(lDAPSearchScope);
56 ConfigIface::lDAPType(lDAPType);
Ratan Guptaaeaf9412019-02-11 04:41:52 -060057 EnableIface::enabled(lDAPServiceEnabled);
58 ConfigIface::userNameAttribute(userNameAttr);
59 ConfigIface::groupNameAttribute(groupNameAttr);
Ratan Gupta21e88cb2019-04-12 17:15:52 +053060 // NOTE: Don't update the bindDN password under ConfigIface
Ratan Guptaec117542019-04-25 18:38:29 +053061 if (enabled())
62 {
63 writeConfig();
64 }
Ratan Gupta21e88cb2019-04-12 17:15:52 +053065 // save the config.
66 configPersistPath = parent.dbusPersistentPath;
67 configPersistPath += objectPath;
68
69 // create the persistent directory
70 fs::create_directories(configPersistPath);
71
72 configPersistPath += "/config";
73
74 std::ofstream os(configPersistPath, std::ios::binary | std::ios::out);
75 // remove the read permission from others
76 auto permission =
77 fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read;
78 fs::permissions(configPersistPath, permission);
79
80 serialize();
81
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050082 // Emit deferred signal.
83 this->emit_object_added();
Ratan Guptaaeaf9412019-02-11 04:41:52 -060084 parent.startOrStopService(nslcdService, enabled());
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050085}
86
Ratan Gupta21e88cb2019-04-12 17:15:52 +053087Config::Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
88 const char* caCertFile, ConfigIface::Type lDAPType,
89 ConfigMgr& parent) :
90 Ifaces(bus, path, true),
91 tlsCacertFile(caCertFile), configFilePath(filePath), objectPath(path),
92 bus(bus), parent(parent)
93{
94 ConfigIface::lDAPType(lDAPType);
95
96 configPersistPath = parent.dbusPersistentPath;
97 configPersistPath += objectPath;
98
99 // create the persistent directory
100 fs::create_directories(configPersistPath);
101
102 configPersistPath += "/config";
103
104 std::ofstream os(configPersistPath, std::ios::binary | std::ios::out);
105 // remove the read permission from others
106 auto permission =
107 fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read;
108 fs::permissions(configPersistPath, permission);
109}
110
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500111void Config::writeConfig()
112{
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500113 std::stringstream confData;
Ratan Gupta9891f2f2018-10-06 12:07:35 +0530114 auto isPwdTobeWritten = false;
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600115 std::string userNameAttr;
Ratan Gupta9891f2f2018-10-06 12:07:35 +0530116
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500117 confData << "uid root\n";
118 confData << "gid root\n\n";
119 confData << "ldap_version 3\n\n";
120 confData << "timelimit 30\n";
121 confData << "bind_timelimit 30\n";
122 confData << "pagesize 1000\n";
123 confData << "referrals off\n\n";
124 confData << "uri " << lDAPServerURI() << "\n\n";
125 confData << "base " << lDAPBaseDN() << "\n\n";
126 confData << "binddn " << lDAPBindDN() << "\n";
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530127 if (!lDAPBindPassword.empty())
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500128 {
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530129 confData << "bindpw " << lDAPBindPassword << "\n";
Ratan Gupta9891f2f2018-10-06 12:07:35 +0530130 isPwdTobeWritten = true;
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500131 }
132 confData << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500133 switch (lDAPSearchScope())
134 {
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600135 case ConfigIface::SearchScope::sub:
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500136 confData << "scope sub\n\n";
137 break;
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600138 case ConfigIface::SearchScope::one:
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500139 confData << "scope one\n\n";
140 break;
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600141 case ConfigIface::SearchScope::base:
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500142 confData << "scope base\n\n";
143 break;
144 }
145 confData << "base passwd " << lDAPBaseDN() << "\n";
146 confData << "base shadow " << lDAPBaseDN() << "\n\n";
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600147 if (secureLDAP == true)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500148 {
149 confData << "ssl on\n";
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600150 confData << "tls_reqcert hard\n";
151 confData << "tls_cacertFile " << tlsCacertFile.c_str() << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500152 }
153 else
154 {
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500155 confData << "ssl off\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500156 }
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500157 confData << "\n";
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600158 if (lDAPType() == ConfigIface::Type::ActiveDirectory)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500159 {
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600160 if (ConfigIface::userNameAttribute().empty())
161 {
162 ConfigIface::userNameAttribute("sAMAccountName");
163 }
164 if (ConfigIface::groupNameAttribute().empty())
165 {
166 ConfigIface::groupNameAttribute("primaryGroupID");
167 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500168 confData << "filter passwd (&(objectClass=user)(objectClass=person)"
169 "(!(objectClass=computer)))\n";
170 confData
171 << "filter group (|(objectclass=group)(objectclass=groupofnames) "
172 "(objectclass=groupofuniquenames))\n";
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600173 confData << "map passwd uid "
174 << ConfigIface::userNameAttribute() << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500175 confData << "map passwd uidNumber "
176 "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600177 confData << "map passwd gidNumber "
178 << ConfigIface::groupNameAttribute() << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500179 confData << "map passwd homeDirectory \"/home/$sAMAccountName\"\n";
180 confData << "map passwd gecos displayName\n";
181 confData << "map passwd loginShell \"/bin/bash\"\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500182 confData << "map group gidNumber "
183 "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600184 confData << "map group cn "
185 << ConfigIface::userNameAttribute() << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500186 }
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600187 else if (lDAPType() == ConfigIface::Type::OpenLdap)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500188 {
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600189 if (ConfigIface::userNameAttribute().empty())
190 {
raviteja-bc3f56c52019-04-02 11:09:04 -0500191 ConfigIface::userNameAttribute("cn");
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600192 }
193 if (ConfigIface::groupNameAttribute().empty())
194 {
raviteja-bc3f56c52019-04-02 11:09:04 -0500195 ConfigIface::groupNameAttribute("gidNumber");
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600196 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500197 confData << "filter passwd (objectclass=*)\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500198 confData << "map passwd gecos displayName\n";
Nagaraju Goruganti808eda42018-10-10 08:48:12 -0500199 confData << "filter group (objectclass=posixGroup)\n";
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600200 confData << "map passwd uid "
201 << ConfigIface::userNameAttribute() << "\n";
202 confData << "map passwd gidNumber "
203 << ConfigIface::groupNameAttribute() << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500204 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500205 try
206 {
207 std::fstream stream(configFilePath.c_str(), std::fstream::out);
Ratan Gupta9891f2f2018-10-06 12:07:35 +0530208 // remove the read permission from others if password is being written.
209 // nslcd forces this behaviour.
210 auto permission = fs::perms::owner_read | fs::perms::owner_write |
211 fs::perms::group_read;
212 if (isPwdTobeWritten)
213 {
214 fs::permissions(configFilePath, permission);
215 }
216 else
217 {
218 fs::permissions(configFilePath,
219 permission | fs::perms::others_read);
220 }
221
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500222 stream << confData.str();
223 stream.flush();
224 stream.close();
225 }
226 catch (const std::exception& e)
227 {
228 log<level::ERR>(e.what());
229 elog<InternalFailure>();
230 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500231 return;
232}
233
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530234std::string Config::lDAPBindDNPassword(std::string value)
235{
236 // Don't update the D-bus object, this is just to
237 // facilitate if user wants to change the bind dn password
238 // once d-bus object gets created.
239 lDAPBindPassword = value;
240 try
241 {
Ratan Guptaec117542019-04-25 18:38:29 +0530242 if (enabled())
243 {
244 writeConfig();
245 parent.startOrStopService(nslcdService, enabled());
246 }
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530247 serialize();
Ratan Gupta3a1c2742019-03-20 06:49:42 +0530248 }
249 catch (const InternalFailure& e)
250 {
251 throw;
252 }
253 catch (const std::exception& e)
254 {
255 log<level::ERR>(e.what());
256 elog<InternalFailure>();
257 }
258 return value;
259}
260
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500261std::string Config::lDAPServerURI(std::string value)
262{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500263 std::string val;
264 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500265 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500266 if (value == lDAPServerURI())
267 {
268 return value;
269 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500270 if (isValidLDAPURI(value, LDAPSscheme))
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500271 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500272 secureLDAP = true;
273 }
274 else if (isValidLDAPURI(value, LDAPscheme))
275 {
276 secureLDAP = false;
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600277 }
278 else
279 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500280 log<level::ERR>("bad LDAP Server URI",
281 entry("LDAPSERVERURI=%s", value.c_str()));
282 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"),
283 Argument::ARGUMENT_VALUE(value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500284 }
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600285
286 if (secureLDAP && !fs::exists(tlsCacertFile.c_str()))
287 {
288 log<level::ERR>("LDAP server's CA certificate not provided",
289 entry("TLSCACERTFILE=%s", tlsCacertFile.c_str()));
290 elog<NoCACertificate>();
291 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500292 val = ConfigIface::lDAPServerURI(value);
Ratan Guptaec117542019-04-25 18:38:29 +0530293 if (enabled())
294 {
295 writeConfig();
296 parent.startOrStopService(nslcdService, enabled());
297 }
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530298 // save the object.
299 serialize();
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500300 }
301 catch (const InternalFailure& e)
302 {
303 throw;
304 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500305 catch (const InvalidArgument& e)
306 {
307 throw;
308 }
Nagaraju Goruganti3b4d06a2018-11-08 03:13:38 -0600309 catch (const NoCACertificate& e)
310 {
311 throw;
312 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500313 catch (const std::exception& e)
314 {
315 log<level::ERR>(e.what());
316 elog<InternalFailure>();
317 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500318 return val;
319}
320
321std::string Config::lDAPBindDN(std::string value)
322{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500323 std::string val;
324 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500325 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500326 if (value == lDAPBindDN())
327 {
328 return value;
329 }
330
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500331 if (value.empty())
332 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500333 log<level::ERR>("Not a valid LDAP BINDDN",
334 entry("LDAPBINDDN=%s", value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500335 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBindDN"),
336 Argument::ARGUMENT_VALUE(value.c_str()));
337 }
338
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500339 val = ConfigIface::lDAPBindDN(value);
Ratan Guptaec117542019-04-25 18:38:29 +0530340 if (enabled())
341 {
342 writeConfig();
343 parent.startOrStopService(nslcdService, enabled());
344 }
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530345 // save the object.
346 serialize();
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500347 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500348 catch (const InternalFailure& e)
349 {
350 throw;
351 }
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600352 catch (const InvalidArgument& e)
353 {
354 throw;
355 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500356 catch (const std::exception& e)
357 {
358 log<level::ERR>(e.what());
359 elog<InternalFailure>();
360 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500361 return val;
362}
363
364std::string Config::lDAPBaseDN(std::string value)
365{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500366 std::string val;
367 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500368 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500369 if (value == lDAPBaseDN())
370 {
371 return value;
372 }
373
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500374 if (value.empty())
375 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500376 log<level::ERR>("Not a valid LDAP BASEDN",
377 entry("BASEDN=%s", value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500378 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBaseDN"),
379 Argument::ARGUMENT_VALUE(value.c_str()));
380 }
381
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500382 val = ConfigIface::lDAPBaseDN(value);
Ratan Guptaec117542019-04-25 18:38:29 +0530383 if (enabled())
384 {
385 writeConfig();
386 parent.startOrStopService(nslcdService, enabled());
387 }
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530388 // save the object.
389 serialize();
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500390 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500391 catch (const InternalFailure& e)
392 {
393 throw;
394 }
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600395 catch (const InvalidArgument& e)
396 {
397 throw;
398 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500399 catch (const std::exception& e)
400 {
401 log<level::ERR>(e.what());
402 elog<InternalFailure>();
403 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500404 return val;
405}
406
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600407ConfigIface::SearchScope Config::lDAPSearchScope(ConfigIface::SearchScope value)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500408{
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600409 ConfigIface::SearchScope val;
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500410 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500411 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500412 if (value == lDAPSearchScope())
413 {
414 return value;
415 }
416
417 val = ConfigIface::lDAPSearchScope(value);
Ratan Guptaec117542019-04-25 18:38:29 +0530418 if (enabled())
419 {
420 writeConfig();
421
422 parent.startOrStopService(nslcdService, enabled());
423 }
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530424 // save the object.
425 serialize();
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500426 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500427 catch (const InternalFailure& e)
428 {
429 throw;
430 }
431 catch (const std::exception& e)
432 {
433 log<level::ERR>(e.what());
434 elog<InternalFailure>();
435 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500436 return val;
437}
438
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600439ConfigIface::Type Config::lDAPType(ConfigIface::Type value)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500440{
Ratan Gupta27d4c012019-04-12 13:03:35 +0530441 elog<NotAllowed>(NotAllowedArgument::REASON("ReadOnly Property"));
442 return lDAPType();
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500443}
444
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600445bool Config::enabled(bool value)
446{
Ratan Guptac5481d12019-04-12 18:31:05 +0530447 if (value == enabled())
448 {
449 return value;
450 }
451 // Let parent decide that can we enable this config.
452 // It may happen that other config is already enabled,
453 // Current implementation support only one config can
454 // be active at a time.
455 return parent.enableService(*this, value);
456}
457
458bool Config::enableService(bool value)
459{
460 bool isEnable = false;
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600461 try
462 {
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600463 isEnable = EnableIface::enabled(value);
Ratan Guptaec117542019-04-25 18:38:29 +0530464 if (isEnable)
465 {
466 writeConfig();
467 }
Ratan Guptaec117542019-04-25 18:38:29 +0530468 parent.startOrStopService(nslcdService, value);
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530469 serialize();
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600470 }
471 catch (const InternalFailure& e)
472 {
473 throw;
474 }
475 catch (const std::exception& e)
476 {
477 log<level::ERR>(e.what());
478 elog<InternalFailure>();
479 }
480 return isEnable;
481}
482
483std::string Config::userNameAttribute(std::string value)
484{
485 std::string val;
486 try
487 {
488 if (value == userNameAttribute())
489 {
490 return value;
491 }
492
493 val = ConfigIface::userNameAttribute(value);
Ratan Guptaec117542019-04-25 18:38:29 +0530494 if (enabled())
495 {
496 writeConfig();
497
498 parent.startOrStopService(nslcdService, enabled());
499 }
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530500 // save the object.
501 serialize();
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600502 }
503 catch (const InternalFailure& e)
504 {
505 throw;
506 }
507 catch (const std::exception& e)
508 {
509 log<level::ERR>(e.what());
510 elog<InternalFailure>();
511 }
512 return val;
513}
514
515std::string Config::groupNameAttribute(std::string value)
516{
517 std::string val;
518 try
519 {
520 if (value == groupNameAttribute())
521 {
522 return value;
523 }
524
525 val = ConfigIface::groupNameAttribute(value);
Ratan Guptaec117542019-04-25 18:38:29 +0530526 if (enabled())
527 {
528 writeConfig();
529
530 parent.startOrStopService(nslcdService, enabled());
531 }
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530532 // save the object.
533 serialize();
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600534 }
535 catch (const InternalFailure& e)
536 {
537 throw;
538 }
539 catch (const std::exception& e)
540 {
541 log<level::ERR>(e.what());
542 elog<InternalFailure>();
543 }
544 return val;
545}
546
Ratan Gupta21e88cb2019-04-12 17:15:52 +0530547template <class Archive>
548void Config::save(Archive& archive, const std::uint32_t version) const
549{
550 archive(this->enabled());
551 archive(lDAPServerURI());
552 archive(lDAPBindDN());
553 archive(lDAPBaseDN());
554 archive(lDAPSearchScope());
555 archive(lDAPBindPassword);
556 archive(userNameAttribute());
557 archive(groupNameAttribute());
558}
559
560template <class Archive>
561void Config::load(Archive& archive, const std::uint32_t version)
562{
563
564 bool bVal;
565 archive(bVal);
566 EnableIface::enabled(bVal);
567
568 std::string str;
569 archive(str);
570 ConfigIface::lDAPServerURI(str);
571
572 archive(str);
573 ConfigIface::lDAPBindDN(str);
574
575 archive(str);
576 ConfigIface::lDAPBaseDN(str);
577
578 ConfigIface::SearchScope scope;
579 archive(scope);
580 ConfigIface::lDAPSearchScope(scope);
581
582 archive(str);
583 lDAPBindPassword = str;
584
585 archive(str);
586 ConfigIface::userNameAttribute(str);
587
588 archive(str);
589 ConfigIface::groupNameAttribute(str);
590}
591
592void Config::serialize()
593{
594 std::ofstream os(configPersistPath.string(),
595 std::ios::binary | std::ios::out);
596 cereal::BinaryOutputArchive oarchive(os);
597 oarchive(*this);
598 return;
599}
600
601bool Config::deserialize()
602{
603 try
604 {
605 if (fs::exists(configPersistPath))
606 {
607 std::ifstream is(configPersistPath.c_str(),
608 std::ios::in | std::ios::binary);
609 cereal::BinaryInputArchive iarchive(is);
610 iarchive(*this);
611 return true;
612 }
613 return false;
614 }
615 catch (cereal::Exception& e)
616 {
617 log<level::ERR>(e.what());
618 std::error_code ec;
619 fs::remove(configPersistPath, ec);
620 return false;
621 }
622 catch (const fs::filesystem_error& e)
623 {
624 return false;
625 }
626}
627
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500628} // namespace ldap
629} // namespace phosphor