blob: 5582bf131352155708b71cf07a1358b25227bb81 [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";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500138 confData << "map passwd gecos displayName\n";
Nagaraju Goruganti808eda42018-10-10 08:48:12 -0500139 confData << "filter group (objectclass=posixGroup)\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500140 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500141 try
142 {
143 std::fstream stream(configFilePath.c_str(), std::fstream::out);
Ratan Gupta9891f2f2018-10-06 12:07:35 +0530144 // remove the read permission from others if password is being written.
145 // nslcd forces this behaviour.
146 auto permission = fs::perms::owner_read | fs::perms::owner_write |
147 fs::perms::group_read;
148 if (isPwdTobeWritten)
149 {
150 fs::permissions(configFilePath, permission);
151 }
152 else
153 {
154 fs::permissions(configFilePath,
155 permission | fs::perms::others_read);
156 }
157
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500158 stream << confData.str();
159 stream.flush();
160 stream.close();
161 }
162 catch (const std::exception& e)
163 {
164 log<level::ERR>(e.what());
165 elog<InternalFailure>();
166 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500167 return;
168}
169
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500170std::string Config::lDAPServerURI(std::string value)
171{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500172 std::string val;
173 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500174 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500175 if (value == lDAPServerURI())
176 {
177 return value;
178 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500179 if (isValidLDAPURI(value, LDAPSscheme))
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500180 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500181 secureLDAP = true;
182 }
183 else if (isValidLDAPURI(value, LDAPscheme))
184 {
185 secureLDAP = false;
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600186 }
187 else
188 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500189 log<level::ERR>("bad LDAP Server URI",
190 entry("LDAPSERVERURI=%s", value.c_str()));
191 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"),
192 Argument::ARGUMENT_VALUE(value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500193 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500194 val = ConfigIface::lDAPServerURI(value);
195 writeConfig();
196 parent.restartService(nslcdService);
197 }
198 catch (const InternalFailure& e)
199 {
200 throw;
201 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500202 catch (const InvalidArgument& e)
203 {
204 throw;
205 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500206 catch (const std::exception& e)
207 {
208 log<level::ERR>(e.what());
209 elog<InternalFailure>();
210 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500211 return val;
212}
213
214std::string Config::lDAPBindDN(std::string value)
215{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500216 std::string val;
217 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500218 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500219 if (value == lDAPBindDN())
220 {
221 return value;
222 }
223
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500224 if (value.empty())
225 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500226 log<level::ERR>("Not a valid LDAP BINDDN",
227 entry("LDAPBINDDN=%s", value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500228 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBindDN"),
229 Argument::ARGUMENT_VALUE(value.c_str()));
230 }
231
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500232 val = ConfigIface::lDAPBindDN(value);
233 writeConfig();
234 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500235 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500236 catch (const InternalFailure& e)
237 {
238 throw;
239 }
240 catch (const std::exception& e)
241 {
242 log<level::ERR>(e.what());
243 elog<InternalFailure>();
244 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500245 return val;
246}
247
248std::string Config::lDAPBaseDN(std::string value)
249{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500250 std::string val;
251 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500252 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500253 if (value == lDAPBaseDN())
254 {
255 return value;
256 }
257
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500258 if (value.empty())
259 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500260 log<level::ERR>("Not a valid LDAP BASEDN",
261 entry("BASEDN=%s", value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500262 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBaseDN"),
263 Argument::ARGUMENT_VALUE(value.c_str()));
264 }
265
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500266 val = ConfigIface::lDAPBaseDN(value);
267 writeConfig();
268 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500269 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500270 catch (const InternalFailure& e)
271 {
272 throw;
273 }
274 catch (const std::exception& e)
275 {
276 log<level::ERR>(e.what());
277 elog<InternalFailure>();
278 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500279 return val;
280}
281
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500282ldap_base::Config::SearchScope
283 Config::lDAPSearchScope(ldap_base::Config::SearchScope value)
284{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500285 ldap_base::Config::SearchScope val;
286 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500287 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500288 if (value == lDAPSearchScope())
289 {
290 return value;
291 }
292
293 val = ConfigIface::lDAPSearchScope(value);
294 writeConfig();
295 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500296 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500297 catch (const InternalFailure& e)
298 {
299 throw;
300 }
301 catch (const std::exception& e)
302 {
303 log<level::ERR>(e.what());
304 elog<InternalFailure>();
305 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500306 return val;
307}
308
309ldap_base::Config::Type Config::lDAPType(ldap_base::Config::Type value)
310{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500311 ldap_base::Config::Type val;
312 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500313 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500314 if (value == lDAPType())
315 {
316 return value;
317 }
318
319 val = ConfigIface::lDAPType(value);
320 writeConfig();
321 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500322 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500323 catch (const InternalFailure& e)
324 {
325 throw;
326 }
327 catch (const std::exception& e)
328 {
329 log<level::ERR>(e.what());
330 elog<InternalFailure>();
331 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500332 return val;
333}
334
335void ConfigMgr::restartService(const std::string& service)
336{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500337 try
338 {
339 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
340 SYSTEMD_INTERFACE, "RestartUnit");
341 method.append(service.c_str(), "replace");
342 bus.call_noreply(method);
343 }
344 catch (const sdbusplus::exception::SdBusError& ex)
345 {
346 log<level::ERR>("Failed to restart nslcd service",
347 entry("ERR=%s", ex.what()));
348 elog<InternalFailure>();
349 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500350}
351
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500352void ConfigMgr::stopService(const std::string& service)
353{
354 try
355 {
356 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
357 SYSTEMD_INTERFACE, "StopUnit");
358 method.append(service.c_str(), "replace");
359 bus.call_noreply(method);
360 }
361 catch (const sdbusplus::exception::SdBusError& ex)
362 {
363 log<level::ERR>("Failed to stop nslcd service",
364 entry("ERR=%s", ex.what()));
365 elog<InternalFailure>();
366 }
367}
368
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500369void ConfigMgr::deleteObject()
370{
371 configPtr.reset(nullptr);
372}
373
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500374std::string
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600375 ConfigMgr::createConfig(std::string lDAPServerURI, std::string lDAPBindDN,
376 std::string lDAPBaseDN,
377 std::string lDAPBindDNPassword,
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500378 ldap_base::Create::SearchScope lDAPSearchScope,
379 ldap_base::Create::Type lDAPType)
380{
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600381 bool secureLDAP = false;
382
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500383 if (isValidLDAPURI(lDAPServerURI, LDAPSscheme))
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500384 {
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600385 secureLDAP = true;
386 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500387 else if (isValidLDAPURI(lDAPServerURI, LDAPscheme))
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600388 {
389 secureLDAP = false;
390 }
391 else
392 {
393 log<level::ERR>("bad LDAP Server URI",
394 entry("LDAPSERVERURI=%s", lDAPServerURI.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500395 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"),
396 Argument::ARGUMENT_VALUE(lDAPServerURI.c_str()));
397 }
398
399 if (lDAPBindDN.empty())
400 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500401 log<level::ERR>("Not a valid LDAP BINDDN",
402 entry("LDAPBINDDN=%s", lDAPBindDN.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500403 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBindDN"),
404 Argument::ARGUMENT_VALUE(lDAPBindDN.c_str()));
405 }
406
407 if (lDAPBaseDN.empty())
408 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500409 log<level::ERR>("Not a valid LDAP BASEDN",
410 entry("LDAPBASEDN=%s", lDAPBaseDN.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500411 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBaseDN"),
412 Argument::ARGUMENT_VALUE(lDAPBaseDN.c_str()));
413 }
414
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500415 // With current implementation we support only one LDAP server.
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500416 deleteObject();
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500417 try
418 {
419 fs::copy_file(nsSwitchFile, linuxNsSwitchFile,
420 fs::copy_options::overwrite_existing);
421 fs::copy_file(LDAPNsSwitchFile, nsSwitchFile,
422 fs::copy_options::overwrite_existing);
423 }
424 catch (const std::exception& e)
425 {
426 log<level::ERR>("Failed to rename Config Files while creating Object",
427 entry("ERR=%s", e.what()));
428 elog<InternalFailure>();
429 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500430
431 auto objPath = std::string(LDAP_CONFIG_DBUS_OBJ_PATH);
432 configPtr = std::make_unique<Config>(
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600433 bus, objPath.c_str(), configFilePath.c_str(), secureLDAP, lDAPServerURI,
434 lDAPBindDN, lDAPBaseDN, std::move(lDAPBindDNPassword),
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500435 static_cast<ldap_base::Config::SearchScope>(lDAPSearchScope),
436 static_cast<ldap_base::Config::Type>(lDAPType), *this);
437
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500438 restartService(nslcdService);
439 restartService(nscdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500440 return objPath;
441}
442
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500443void ConfigMgr::restore(const char* filePath)
444{
445 if (!fs::exists(filePath))
446 {
447 log<level::ERR>("Config file doesn't exists",
448 entry("LDAP_CONFIG_FILE=%s", LDAP_CONFIG_FILE));
449 return;
450 }
451
452 ConfigInfo configValues;
453
454 try
455 {
456 std::fstream stream(filePath, std::fstream::in);
457 Line line;
458 // read characters from stream and places them into line
459 while (std::getline(stream, line))
460 {
461 // remove leading and trailing extra spaces
462 auto firstScan = line.find_first_not_of(' ');
463 auto first =
464 (firstScan == std::string::npos ? line.length() : firstScan);
465 auto last = line.find_last_not_of(' ');
466 line = line.substr(first, last - first + 1);
467 // reduce multiple spaces between two words to a single space
468 auto pred = [](char a, char b) {
469 return (a == b && a == ' ') ? true : false;
470 };
471
472 auto lastPos = std::unique(line.begin(), line.end(), pred);
473
474 line.erase(lastPos, line.end());
475
476 // Ignore if line is empty or starts with '#'
477 if (line.empty() || line.at(0) == '#')
478 {
479 continue;
480 }
481
482 Key key;
483 std::istringstream isLine(line);
484 // extract characters from isLine and stores them into
485 // key until the delimitation character ' ' is found.
486 // If the delimiter is found, it is extracted and discarded
487 // the next input operation will begin after it.
488 if (std::getline(isLine, key, ' '))
489 {
490 Val value;
491 // extract characters after delimitation character ' '
492 if (std::getline(isLine, value, ' '))
493 {
494 // skip line if it starts with "base shadow" or
495 // "base passwd" because we would have 3 entries
496 // ("base lDAPBaseDN" , "base passwd lDAPBaseDN" and
497 // "base shadow lDAPBaseDN") for the property "lDAPBaseDN",
498 // one is enough to restore it.
499
500 if ((key == "base") &&
501 (value == "passwd" || value == "shadow"))
502 {
503 continue;
504 }
505 // skip the line if it starts with "map passwd".
506 // if config type is AD "map group" entry would be add to
507 // the map configValues. For OpenLdap config file no map
508 // entry would be there.
509 if ((key == "map") && (value == "passwd"))
510 {
511 continue;
512 }
513 configValues[key] = value;
514 }
515 }
516 }
517
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500518 ldap_base::Create::SearchScope lDAPSearchScope;
519 if (configValues["scope"] == "sub")
520 {
521 lDAPSearchScope = ldap_base::Create::SearchScope::sub;
522 }
523 else if (configValues["scope"] == "one")
524 {
525 lDAPSearchScope = ldap_base::Create::SearchScope::one;
526 }
527 else
528 {
529 lDAPSearchScope = ldap_base::Create::SearchScope::base;
530 }
531
532 ldap_base::Create::Type lDAPType;
533 // If the file is having a line which starts with "map group"
534 if (configValues["map"] == "group")
535 {
536 lDAPType = ldap_base::Create::Type::ActiveDirectory;
537 }
538 else
539 {
540 lDAPType = ldap_base::Create::Type::OpenLdap;
541 }
542
543 createConfig(
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600544 std::move(configValues["uri"]), std::move(configValues["binddn"]),
545 std::move(configValues["base"]), std::move(configValues["bindpw"]),
546 lDAPSearchScope, lDAPType);
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500547 }
548 catch (const InvalidArgument& e)
549 {
550 // Don't throw - we don't want to create a D-Bus
551 // object upon finding empty values in config, as
552 // this can be a default config.
553 }
554 catch (const InternalFailure& e)
555 {
556 throw;
557 }
558 catch (const std::exception& e)
559 {
560 log<level::ERR>(e.what());
561 elog<InternalFailure>();
562 }
563}
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500564} // namespace ldap
565} // namespace phosphor