blob: 9e918e468072d96e3e72ea9305fabb8dd0eebe9e [file] [log] [blame]
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -05001#include "ldap_configuration.hpp"
Nagaraju Goruganti59287f02018-10-12 07:00:20 -05002#include "utils.hpp"
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -05003#include <experimental/filesystem>
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -05004#include <fstream>
5#include <sstream>
6
7namespace phosphor
8{
9namespace ldap
10{
11constexpr auto nslcdService = "nslcd.service";
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -050012constexpr auto nscdService = "nscd.service";
Nagaraju Goruganti59287f02018-10-12 07:00:20 -050013constexpr auto LDAPscheme = "ldap";
14constexpr auto LDAPSscheme = "ldaps";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050015
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -050016using namespace phosphor::logging;
17using namespace sdbusplus::xyz::openbmc_project::Common::Error;
18namespace fs = std::experimental::filesystem;
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -050019using Argument = xyz::openbmc_project::Common::InvalidArgument;
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -050020
21using Line = std::string;
22using Key = std::string;
23using Val = std::string;
24using ConfigInfo = std::map<Key, Val>;
25
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050026Config::Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
27 bool secureLDAP, std::string lDAPServerURI,
28 std::string lDAPBindDN, std::string lDAPBaseDN,
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060029 std::string&& lDAPBindDNPassword,
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050030 ldap_base::Config::SearchScope lDAPSearchScope,
31 ldap_base::Config::Type lDAPType, ConfigMgr& parent) :
32 ConfigIface(bus, path, true),
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060033 secureLDAP(secureLDAP), configFilePath(filePath),
34 lDAPBindDNPassword(std::move(lDAPBindDNPassword)), bus(bus), parent(parent)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050035{
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050036 ConfigIface::lDAPServerURI(lDAPServerURI);
37 ConfigIface::lDAPBindDN(lDAPBindDN);
38 ConfigIface::lDAPBaseDN(lDAPBaseDN);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050039 ConfigIface::lDAPSearchScope(lDAPSearchScope);
40 ConfigIface::lDAPType(lDAPType);
41 writeConfig();
42 parent.restartService(nslcdService);
43 // Emit deferred signal.
44 this->emit_object_added();
45}
46
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -050047void Config::delete_()
48{
49 parent.deleteObject();
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -050050 try
51 {
52 fs::copy_file(defaultNslcdFile, LDAP_CONFIG_FILE,
53 fs::copy_options::overwrite_existing);
54 fs::copy_file(nsSwitchFile, LDAPNsSwitchFile,
55 fs::copy_options::overwrite_existing);
56 fs::copy_file(linuxNsSwitchFile, nsSwitchFile,
57 fs::copy_options::overwrite_existing);
58 }
59 catch (const std::exception& e)
60 {
61 log<level::ERR>("Failed to rename Config Files while deleting Object",
62 entry("ERR=%s", e.what()));
63 elog<InternalFailure>();
64 }
65
66 parent.restartService(nscdService);
67 parent.stopService(nslcdService);
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -050068}
69
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050070void Config::writeConfig()
71{
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050072 std::stringstream confData;
Ratan Gupta9891f2f2018-10-06 12:07:35 +053073 auto isPwdTobeWritten = false;
74
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050075 confData << "uid root\n";
76 confData << "gid root\n\n";
77 confData << "ldap_version 3\n\n";
78 confData << "timelimit 30\n";
79 confData << "bind_timelimit 30\n";
80 confData << "pagesize 1000\n";
81 confData << "referrals off\n\n";
82 confData << "uri " << lDAPServerURI() << "\n\n";
83 confData << "base " << lDAPBaseDN() << "\n\n";
84 confData << "binddn " << lDAPBindDN() << "\n";
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060085 if (!lDAPBindDNPassword.empty())
Nagaraju Goruganti15675472018-10-05 07:03:05 -050086 {
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060087 confData << "bindpw " << lDAPBindDNPassword << "\n";
Ratan Gupta9891f2f2018-10-06 12:07:35 +053088 isPwdTobeWritten = true;
Nagaraju Goruganti15675472018-10-05 07:03:05 -050089 }
90 confData << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050091 switch (lDAPSearchScope())
92 {
93 case ldap_base::Config::SearchScope::sub:
94 confData << "scope sub\n\n";
95 break;
96 case ldap_base::Config::SearchScope::one:
97 confData << "scope one\n\n";
98 break;
99 case ldap_base::Config::SearchScope::base:
100 confData << "scope base\n\n";
101 break;
102 }
103 confData << "base passwd " << lDAPBaseDN() << "\n";
104 confData << "base shadow " << lDAPBaseDN() << "\n\n";
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600105 if (secureLDAP == true)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500106 {
107 confData << "ssl on\n";
108 confData << "tls_reqcert allow\n";
109 confData << "tls_cert /etc/nslcd/certs/cert.pem\n";
110 }
111 else
112 {
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500113 confData << "ssl off\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500114 }
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500115 confData << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500116 if (lDAPType() == ldap_base::Config::Type::ActiveDirectory)
117 {
118 confData << "filter passwd (&(objectClass=user)(objectClass=person)"
119 "(!(objectClass=computer)))\n";
120 confData
121 << "filter group (|(objectclass=group)(objectclass=groupofnames) "
122 "(objectclass=groupofuniquenames))\n";
123 confData << "map passwd uid sAMAccountName\n";
124 confData << "map passwd uidNumber "
125 "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
126 confData << "map passwd gidNumber primaryGroupID\n";
127 confData << "map passwd homeDirectory \"/home/$sAMAccountName\"\n";
128 confData << "map passwd gecos displayName\n";
129 confData << "map passwd loginShell \"/bin/bash\"\n";
130 confData << "map group gidNumber primaryGroupID\n";
131 confData << "map group gidNumber "
132 "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
133 confData << "map group cn sAMAccountName\n";
134 }
135 else if (lDAPType() == ldap_base::Config::Type::OpenLdap)
136 {
137 confData << "filter passwd (objectclass=*)\n";
138 confData << "map passwd uid cn\n";
139 confData << "map passwd gecos displayName\n";
Nagaraju Goruganti808eda42018-10-10 08:48:12 -0500140 confData << "filter group (objectclass=posixGroup)\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500141 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500142 try
143 {
144 std::fstream stream(configFilePath.c_str(), std::fstream::out);
Ratan Gupta9891f2f2018-10-06 12:07:35 +0530145 // remove the read permission from others if password is being written.
146 // nslcd forces this behaviour.
147 auto permission = fs::perms::owner_read | fs::perms::owner_write |
148 fs::perms::group_read;
149 if (isPwdTobeWritten)
150 {
151 fs::permissions(configFilePath, permission);
152 }
153 else
154 {
155 fs::permissions(configFilePath,
156 permission | fs::perms::others_read);
157 }
158
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500159 stream << confData.str();
160 stream.flush();
161 stream.close();
162 }
163 catch (const std::exception& e)
164 {
165 log<level::ERR>(e.what());
166 elog<InternalFailure>();
167 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500168 return;
169}
170
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500171std::string Config::lDAPServerURI(std::string value)
172{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500173 std::string val;
174 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500175 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500176 if (value == lDAPServerURI())
177 {
178 return value;
179 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500180 if (isValidLDAPURI(value, LDAPSscheme))
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500181 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500182 secureLDAP = true;
183 }
184 else if (isValidLDAPURI(value, LDAPscheme))
185 {
186 secureLDAP = false;
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600187 }
188 else
189 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500190 log<level::ERR>("bad LDAP Server URI",
191 entry("LDAPSERVERURI=%s", value.c_str()));
192 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"),
193 Argument::ARGUMENT_VALUE(value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500194 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500195 val = ConfigIface::lDAPServerURI(value);
196 writeConfig();
197 parent.restartService(nslcdService);
198 }
199 catch (const InternalFailure& e)
200 {
201 throw;
202 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500203 catch (const InvalidArgument& e)
204 {
205 throw;
206 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500207 catch (const std::exception& e)
208 {
209 log<level::ERR>(e.what());
210 elog<InternalFailure>();
211 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500212 return val;
213}
214
215std::string Config::lDAPBindDN(std::string value)
216{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500217 std::string val;
218 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500219 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500220 if (value == lDAPBindDN())
221 {
222 return value;
223 }
224
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500225 if (value.empty())
226 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500227 log<level::ERR>("Not a valid LDAP BINDDN",
228 entry("LDAPBINDDN=%s", value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500229 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBindDN"),
230 Argument::ARGUMENT_VALUE(value.c_str()));
231 }
232
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500233 val = ConfigIface::lDAPBindDN(value);
234 writeConfig();
235 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500236 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500237 catch (const InternalFailure& e)
238 {
239 throw;
240 }
241 catch (const std::exception& e)
242 {
243 log<level::ERR>(e.what());
244 elog<InternalFailure>();
245 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500246 return val;
247}
248
249std::string Config::lDAPBaseDN(std::string value)
250{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500251 std::string val;
252 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500253 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500254 if (value == lDAPBaseDN())
255 {
256 return value;
257 }
258
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500259 if (value.empty())
260 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500261 log<level::ERR>("Not a valid LDAP BASEDN",
262 entry("BASEDN=%s", value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500263 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBaseDN"),
264 Argument::ARGUMENT_VALUE(value.c_str()));
265 }
266
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500267 val = ConfigIface::lDAPBaseDN(value);
268 writeConfig();
269 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500270 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500271 catch (const InternalFailure& e)
272 {
273 throw;
274 }
275 catch (const std::exception& e)
276 {
277 log<level::ERR>(e.what());
278 elog<InternalFailure>();
279 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500280 return val;
281}
282
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500283ldap_base::Config::SearchScope
284 Config::lDAPSearchScope(ldap_base::Config::SearchScope value)
285{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500286 ldap_base::Config::SearchScope val;
287 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500288 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500289 if (value == lDAPSearchScope())
290 {
291 return value;
292 }
293
294 val = ConfigIface::lDAPSearchScope(value);
295 writeConfig();
296 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500297 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500298 catch (const InternalFailure& e)
299 {
300 throw;
301 }
302 catch (const std::exception& e)
303 {
304 log<level::ERR>(e.what());
305 elog<InternalFailure>();
306 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500307 return val;
308}
309
310ldap_base::Config::Type Config::lDAPType(ldap_base::Config::Type value)
311{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500312 ldap_base::Config::Type val;
313 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500314 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500315 if (value == lDAPType())
316 {
317 return value;
318 }
319
320 val = ConfigIface::lDAPType(value);
321 writeConfig();
322 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500323 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500324 catch (const InternalFailure& e)
325 {
326 throw;
327 }
328 catch (const std::exception& e)
329 {
330 log<level::ERR>(e.what());
331 elog<InternalFailure>();
332 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500333 return val;
334}
335
336void ConfigMgr::restartService(const std::string& service)
337{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500338 try
339 {
340 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
341 SYSTEMD_INTERFACE, "RestartUnit");
342 method.append(service.c_str(), "replace");
343 bus.call_noreply(method);
344 }
345 catch (const sdbusplus::exception::SdBusError& ex)
346 {
347 log<level::ERR>("Failed to restart nslcd service",
348 entry("ERR=%s", ex.what()));
349 elog<InternalFailure>();
350 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500351}
352
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500353void ConfigMgr::stopService(const std::string& service)
354{
355 try
356 {
357 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
358 SYSTEMD_INTERFACE, "StopUnit");
359 method.append(service.c_str(), "replace");
360 bus.call_noreply(method);
361 }
362 catch (const sdbusplus::exception::SdBusError& ex)
363 {
364 log<level::ERR>("Failed to stop nslcd service",
365 entry("ERR=%s", ex.what()));
366 elog<InternalFailure>();
367 }
368}
369
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500370void ConfigMgr::deleteObject()
371{
372 configPtr.reset(nullptr);
373}
374
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500375std::string
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600376 ConfigMgr::createConfig(std::string lDAPServerURI, std::string lDAPBindDN,
377 std::string lDAPBaseDN,
378 std::string lDAPBindDNPassword,
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500379 ldap_base::Create::SearchScope lDAPSearchScope,
380 ldap_base::Create::Type lDAPType)
381{
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600382 bool secureLDAP = false;
383
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500384 if (isValidLDAPURI(lDAPServerURI, LDAPSscheme))
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500385 {
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600386 secureLDAP = true;
387 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500388 else if (isValidLDAPURI(lDAPServerURI, LDAPscheme))
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600389 {
390 secureLDAP = false;
391 }
392 else
393 {
394 log<level::ERR>("bad LDAP Server URI",
395 entry("LDAPSERVERURI=%s", lDAPServerURI.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500396 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"),
397 Argument::ARGUMENT_VALUE(lDAPServerURI.c_str()));
398 }
399
400 if (lDAPBindDN.empty())
401 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500402 log<level::ERR>("Not a valid LDAP BINDDN",
403 entry("LDAPBINDDN=%s", lDAPBindDN.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500404 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBindDN"),
405 Argument::ARGUMENT_VALUE(lDAPBindDN.c_str()));
406 }
407
408 if (lDAPBaseDN.empty())
409 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500410 log<level::ERR>("Not a valid LDAP BASEDN",
411 entry("LDAPBASEDN=%s", lDAPBaseDN.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500412 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBaseDN"),
413 Argument::ARGUMENT_VALUE(lDAPBaseDN.c_str()));
414 }
415
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500416 // With current implementation we support only one LDAP server.
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500417 deleteObject();
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500418 try
419 {
420 fs::copy_file(nsSwitchFile, linuxNsSwitchFile,
421 fs::copy_options::overwrite_existing);
422 fs::copy_file(LDAPNsSwitchFile, nsSwitchFile,
423 fs::copy_options::overwrite_existing);
424 }
425 catch (const std::exception& e)
426 {
427 log<level::ERR>("Failed to rename Config Files while creating Object",
428 entry("ERR=%s", e.what()));
429 elog<InternalFailure>();
430 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500431
432 auto objPath = std::string(LDAP_CONFIG_DBUS_OBJ_PATH);
433 configPtr = std::make_unique<Config>(
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600434 bus, objPath.c_str(), configFilePath.c_str(), secureLDAP, lDAPServerURI,
435 lDAPBindDN, lDAPBaseDN, std::move(lDAPBindDNPassword),
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500436 static_cast<ldap_base::Config::SearchScope>(lDAPSearchScope),
437 static_cast<ldap_base::Config::Type>(lDAPType), *this);
438
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500439 restartService(nslcdService);
440 restartService(nscdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500441 return objPath;
442}
443
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500444void ConfigMgr::restore(const char* filePath)
445{
446 if (!fs::exists(filePath))
447 {
448 log<level::ERR>("Config file doesn't exists",
449 entry("LDAP_CONFIG_FILE=%s", LDAP_CONFIG_FILE));
450 return;
451 }
452
453 ConfigInfo configValues;
454
455 try
456 {
457 std::fstream stream(filePath, std::fstream::in);
458 Line line;
459 // read characters from stream and places them into line
460 while (std::getline(stream, line))
461 {
462 // remove leading and trailing extra spaces
463 auto firstScan = line.find_first_not_of(' ');
464 auto first =
465 (firstScan == std::string::npos ? line.length() : firstScan);
466 auto last = line.find_last_not_of(' ');
467 line = line.substr(first, last - first + 1);
468 // reduce multiple spaces between two words to a single space
469 auto pred = [](char a, char b) {
470 return (a == b && a == ' ') ? true : false;
471 };
472
473 auto lastPos = std::unique(line.begin(), line.end(), pred);
474
475 line.erase(lastPos, line.end());
476
477 // Ignore if line is empty or starts with '#'
478 if (line.empty() || line.at(0) == '#')
479 {
480 continue;
481 }
482
483 Key key;
484 std::istringstream isLine(line);
485 // extract characters from isLine and stores them into
486 // key until the delimitation character ' ' is found.
487 // If the delimiter is found, it is extracted and discarded
488 // the next input operation will begin after it.
489 if (std::getline(isLine, key, ' '))
490 {
491 Val value;
492 // extract characters after delimitation character ' '
493 if (std::getline(isLine, value, ' '))
494 {
495 // skip line if it starts with "base shadow" or
496 // "base passwd" because we would have 3 entries
497 // ("base lDAPBaseDN" , "base passwd lDAPBaseDN" and
498 // "base shadow lDAPBaseDN") for the property "lDAPBaseDN",
499 // one is enough to restore it.
500
501 if ((key == "base") &&
502 (value == "passwd" || value == "shadow"))
503 {
504 continue;
505 }
506 // skip the line if it starts with "map passwd".
507 // if config type is AD "map group" entry would be add to
508 // the map configValues. For OpenLdap config file no map
509 // entry would be there.
510 if ((key == "map") && (value == "passwd"))
511 {
512 continue;
513 }
514 configValues[key] = value;
515 }
516 }
517 }
518
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500519 ldap_base::Create::SearchScope lDAPSearchScope;
520 if (configValues["scope"] == "sub")
521 {
522 lDAPSearchScope = ldap_base::Create::SearchScope::sub;
523 }
524 else if (configValues["scope"] == "one")
525 {
526 lDAPSearchScope = ldap_base::Create::SearchScope::one;
527 }
528 else
529 {
530 lDAPSearchScope = ldap_base::Create::SearchScope::base;
531 }
532
533 ldap_base::Create::Type lDAPType;
534 // If the file is having a line which starts with "map group"
535 if (configValues["map"] == "group")
536 {
537 lDAPType = ldap_base::Create::Type::ActiveDirectory;
538 }
539 else
540 {
541 lDAPType = ldap_base::Create::Type::OpenLdap;
542 }
543
544 createConfig(
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600545 std::move(configValues["uri"]), std::move(configValues["binddn"]),
546 std::move(configValues["base"]), std::move(configValues["bindpw"]),
547 lDAPSearchScope, lDAPType);
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500548 }
549 catch (const InvalidArgument& e)
550 {
551 // Don't throw - we don't want to create a D-Bus
552 // object upon finding empty values in config, as
553 // this can be a default config.
554 }
555 catch (const InternalFailure& e)
556 {
557 throw;
558 }
559 catch (const std::exception& e)
560 {
561 log<level::ERR>(e.what());
562 elog<InternalFailure>();
563 }
564}
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500565} // namespace ldap
566} // namespace phosphor