blob: 13693627943eb8516561ad0f7babca7f502c3d84 [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);
Ratan Gupta3e7a72e2018-10-18 09:57:14 +053054
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -050055 fs::copy_file(linuxNsSwitchFile, nsSwitchFile,
56 fs::copy_options::overwrite_existing);
57 }
58 catch (const std::exception& e)
59 {
60 log<level::ERR>("Failed to rename Config Files while deleting Object",
61 entry("ERR=%s", e.what()));
62 elog<InternalFailure>();
63 }
64
65 parent.restartService(nscdService);
66 parent.stopService(nslcdService);
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -050067}
68
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050069void Config::writeConfig()
70{
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050071 std::stringstream confData;
Ratan Gupta9891f2f2018-10-06 12:07:35 +053072 auto isPwdTobeWritten = false;
73
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050074 confData << "uid root\n";
75 confData << "gid root\n\n";
76 confData << "ldap_version 3\n\n";
77 confData << "timelimit 30\n";
78 confData << "bind_timelimit 30\n";
79 confData << "pagesize 1000\n";
80 confData << "referrals off\n\n";
81 confData << "uri " << lDAPServerURI() << "\n\n";
82 confData << "base " << lDAPBaseDN() << "\n\n";
83 confData << "binddn " << lDAPBindDN() << "\n";
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060084 if (!lDAPBindDNPassword.empty())
Nagaraju Goruganti15675472018-10-05 07:03:05 -050085 {
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060086 confData << "bindpw " << lDAPBindDNPassword << "\n";
Ratan Gupta9891f2f2018-10-06 12:07:35 +053087 isPwdTobeWritten = true;
Nagaraju Goruganti15675472018-10-05 07:03:05 -050088 }
89 confData << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050090 switch (lDAPSearchScope())
91 {
92 case ldap_base::Config::SearchScope::sub:
93 confData << "scope sub\n\n";
94 break;
95 case ldap_base::Config::SearchScope::one:
96 confData << "scope one\n\n";
97 break;
98 case ldap_base::Config::SearchScope::base:
99 confData << "scope base\n\n";
100 break;
101 }
102 confData << "base passwd " << lDAPBaseDN() << "\n";
103 confData << "base shadow " << lDAPBaseDN() << "\n\n";
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600104 if (secureLDAP == true)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500105 {
106 confData << "ssl on\n";
107 confData << "tls_reqcert allow\n";
108 confData << "tls_cert /etc/nslcd/certs/cert.pem\n";
109 }
110 else
111 {
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500112 confData << "ssl off\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500113 }
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500114 confData << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500115 if (lDAPType() == ldap_base::Config::Type::ActiveDirectory)
116 {
117 confData << "filter passwd (&(objectClass=user)(objectClass=person)"
118 "(!(objectClass=computer)))\n";
119 confData
120 << "filter group (|(objectclass=group)(objectclass=groupofnames) "
121 "(objectclass=groupofuniquenames))\n";
122 confData << "map passwd uid sAMAccountName\n";
123 confData << "map passwd uidNumber "
124 "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
125 confData << "map passwd gidNumber primaryGroupID\n";
126 confData << "map passwd homeDirectory \"/home/$sAMAccountName\"\n";
127 confData << "map passwd gecos displayName\n";
128 confData << "map passwd loginShell \"/bin/bash\"\n";
129 confData << "map group gidNumber primaryGroupID\n";
130 confData << "map group gidNumber "
131 "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
132 confData << "map group cn sAMAccountName\n";
133 }
134 else if (lDAPType() == ldap_base::Config::Type::OpenLdap)
135 {
136 confData << "filter passwd (objectclass=*)\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500137 confData << "map passwd gecos displayName\n";
Nagaraju Goruganti808eda42018-10-10 08:48:12 -0500138 confData << "filter group (objectclass=posixGroup)\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500139 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500140 try
141 {
142 std::fstream stream(configFilePath.c_str(), std::fstream::out);
Ratan Gupta9891f2f2018-10-06 12:07:35 +0530143 // remove the read permission from others if password is being written.
144 // nslcd forces this behaviour.
145 auto permission = fs::perms::owner_read | fs::perms::owner_write |
146 fs::perms::group_read;
147 if (isPwdTobeWritten)
148 {
149 fs::permissions(configFilePath, permission);
150 }
151 else
152 {
153 fs::permissions(configFilePath,
154 permission | fs::perms::others_read);
155 }
156
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500157 stream << confData.str();
158 stream.flush();
159 stream.close();
160 }
161 catch (const std::exception& e)
162 {
163 log<level::ERR>(e.what());
164 elog<InternalFailure>();
165 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500166 return;
167}
168
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500169std::string Config::lDAPServerURI(std::string value)
170{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500171 std::string val;
172 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500173 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500174 if (value == lDAPServerURI())
175 {
176 return value;
177 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500178 if (isValidLDAPURI(value, LDAPSscheme))
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500179 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500180 secureLDAP = true;
181 }
182 else if (isValidLDAPURI(value, LDAPscheme))
183 {
184 secureLDAP = false;
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600185 }
186 else
187 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500188 log<level::ERR>("bad LDAP Server URI",
189 entry("LDAPSERVERURI=%s", value.c_str()));
190 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"),
191 Argument::ARGUMENT_VALUE(value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500192 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500193 val = ConfigIface::lDAPServerURI(value);
194 writeConfig();
195 parent.restartService(nslcdService);
196 }
197 catch (const InternalFailure& e)
198 {
199 throw;
200 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500201 catch (const InvalidArgument& e)
202 {
203 throw;
204 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500205 catch (const std::exception& e)
206 {
207 log<level::ERR>(e.what());
208 elog<InternalFailure>();
209 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500210 return val;
211}
212
213std::string Config::lDAPBindDN(std::string value)
214{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500215 std::string val;
216 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500217 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500218 if (value == lDAPBindDN())
219 {
220 return value;
221 }
222
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500223 if (value.empty())
224 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500225 log<level::ERR>("Not a valid LDAP BINDDN",
226 entry("LDAPBINDDN=%s", value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500227 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBindDN"),
228 Argument::ARGUMENT_VALUE(value.c_str()));
229 }
230
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500231 val = ConfigIface::lDAPBindDN(value);
232 writeConfig();
233 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500234 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500235 catch (const InternalFailure& e)
236 {
237 throw;
238 }
239 catch (const std::exception& e)
240 {
241 log<level::ERR>(e.what());
242 elog<InternalFailure>();
243 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500244 return val;
245}
246
247std::string Config::lDAPBaseDN(std::string value)
248{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500249 std::string val;
250 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500251 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500252 if (value == lDAPBaseDN())
253 {
254 return value;
255 }
256
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500257 if (value.empty())
258 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500259 log<level::ERR>("Not a valid LDAP BASEDN",
260 entry("BASEDN=%s", value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500261 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBaseDN"),
262 Argument::ARGUMENT_VALUE(value.c_str()));
263 }
264
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500265 val = ConfigIface::lDAPBaseDN(value);
266 writeConfig();
267 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500268 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500269 catch (const InternalFailure& e)
270 {
271 throw;
272 }
273 catch (const std::exception& e)
274 {
275 log<level::ERR>(e.what());
276 elog<InternalFailure>();
277 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500278 return val;
279}
280
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500281ldap_base::Config::SearchScope
282 Config::lDAPSearchScope(ldap_base::Config::SearchScope value)
283{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500284 ldap_base::Config::SearchScope val;
285 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500286 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500287 if (value == lDAPSearchScope())
288 {
289 return value;
290 }
291
292 val = ConfigIface::lDAPSearchScope(value);
293 writeConfig();
294 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500295 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500296 catch (const InternalFailure& e)
297 {
298 throw;
299 }
300 catch (const std::exception& e)
301 {
302 log<level::ERR>(e.what());
303 elog<InternalFailure>();
304 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500305 return val;
306}
307
308ldap_base::Config::Type Config::lDAPType(ldap_base::Config::Type value)
309{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500310 ldap_base::Config::Type val;
311 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500312 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500313 if (value == lDAPType())
314 {
315 return value;
316 }
317
318 val = ConfigIface::lDAPType(value);
319 writeConfig();
320 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500321 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500322 catch (const InternalFailure& e)
323 {
324 throw;
325 }
326 catch (const std::exception& e)
327 {
328 log<level::ERR>(e.what());
329 elog<InternalFailure>();
330 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500331 return val;
332}
333
334void ConfigMgr::restartService(const std::string& service)
335{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500336 try
337 {
338 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
339 SYSTEMD_INTERFACE, "RestartUnit");
340 method.append(service.c_str(), "replace");
341 bus.call_noreply(method);
342 }
343 catch (const sdbusplus::exception::SdBusError& ex)
344 {
345 log<level::ERR>("Failed to restart nslcd service",
346 entry("ERR=%s", ex.what()));
347 elog<InternalFailure>();
348 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500349}
350
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500351void ConfigMgr::stopService(const std::string& service)
352{
353 try
354 {
355 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
356 SYSTEMD_INTERFACE, "StopUnit");
357 method.append(service.c_str(), "replace");
358 bus.call_noreply(method);
359 }
360 catch (const sdbusplus::exception::SdBusError& ex)
361 {
362 log<level::ERR>("Failed to stop nslcd service",
363 entry("ERR=%s", ex.what()));
364 elog<InternalFailure>();
365 }
366}
367
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500368void ConfigMgr::deleteObject()
369{
370 configPtr.reset(nullptr);
371}
372
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500373std::string
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600374 ConfigMgr::createConfig(std::string lDAPServerURI, std::string lDAPBindDN,
375 std::string lDAPBaseDN,
376 std::string lDAPBindDNPassword,
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500377 ldap_base::Create::SearchScope lDAPSearchScope,
378 ldap_base::Create::Type lDAPType)
379{
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600380 bool secureLDAP = false;
381
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500382 if (isValidLDAPURI(lDAPServerURI, LDAPSscheme))
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500383 {
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600384 secureLDAP = true;
385 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500386 else if (isValidLDAPURI(lDAPServerURI, LDAPscheme))
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600387 {
388 secureLDAP = false;
389 }
390 else
391 {
392 log<level::ERR>("bad LDAP Server URI",
393 entry("LDAPSERVERURI=%s", lDAPServerURI.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500394 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"),
395 Argument::ARGUMENT_VALUE(lDAPServerURI.c_str()));
396 }
397
398 if (lDAPBindDN.empty())
399 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500400 log<level::ERR>("Not a valid LDAP BINDDN",
401 entry("LDAPBINDDN=%s", lDAPBindDN.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500402 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBindDN"),
403 Argument::ARGUMENT_VALUE(lDAPBindDN.c_str()));
404 }
405
406 if (lDAPBaseDN.empty())
407 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500408 log<level::ERR>("Not a valid LDAP BASEDN",
409 entry("LDAPBASEDN=%s", lDAPBaseDN.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500410 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBaseDN"),
411 Argument::ARGUMENT_VALUE(lDAPBaseDN.c_str()));
412 }
413
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500414 // With current implementation we support only one LDAP server.
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500415 deleteObject();
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500416 try
417 {
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500418 fs::copy_file(LDAPNsSwitchFile, nsSwitchFile,
419 fs::copy_options::overwrite_existing);
420 }
421 catch (const std::exception& e)
422 {
423 log<level::ERR>("Failed to rename Config Files while creating Object",
424 entry("ERR=%s", e.what()));
425 elog<InternalFailure>();
426 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500427
428 auto objPath = std::string(LDAP_CONFIG_DBUS_OBJ_PATH);
429 configPtr = std::make_unique<Config>(
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600430 bus, objPath.c_str(), configFilePath.c_str(), secureLDAP, lDAPServerURI,
431 lDAPBindDN, lDAPBaseDN, std::move(lDAPBindDNPassword),
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500432 static_cast<ldap_base::Config::SearchScope>(lDAPSearchScope),
433 static_cast<ldap_base::Config::Type>(lDAPType), *this);
434
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500435 restartService(nslcdService);
436 restartService(nscdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500437 return objPath;
438}
439
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500440void ConfigMgr::restore(const char* filePath)
441{
442 if (!fs::exists(filePath))
443 {
444 log<level::ERR>("Config file doesn't exists",
445 entry("LDAP_CONFIG_FILE=%s", LDAP_CONFIG_FILE));
446 return;
447 }
448
449 ConfigInfo configValues;
450
451 try
452 {
453 std::fstream stream(filePath, std::fstream::in);
454 Line line;
455 // read characters from stream and places them into line
456 while (std::getline(stream, line))
457 {
458 // remove leading and trailing extra spaces
459 auto firstScan = line.find_first_not_of(' ');
460 auto first =
461 (firstScan == std::string::npos ? line.length() : firstScan);
462 auto last = line.find_last_not_of(' ');
463 line = line.substr(first, last - first + 1);
464 // reduce multiple spaces between two words to a single space
465 auto pred = [](char a, char b) {
466 return (a == b && a == ' ') ? true : false;
467 };
468
469 auto lastPos = std::unique(line.begin(), line.end(), pred);
470
471 line.erase(lastPos, line.end());
472
473 // Ignore if line is empty or starts with '#'
474 if (line.empty() || line.at(0) == '#')
475 {
476 continue;
477 }
478
479 Key key;
480 std::istringstream isLine(line);
481 // extract characters from isLine and stores them into
482 // key until the delimitation character ' ' is found.
483 // If the delimiter is found, it is extracted and discarded
484 // the next input operation will begin after it.
485 if (std::getline(isLine, key, ' '))
486 {
487 Val value;
488 // extract characters after delimitation character ' '
489 if (std::getline(isLine, value, ' '))
490 {
491 // skip line if it starts with "base shadow" or
492 // "base passwd" because we would have 3 entries
493 // ("base lDAPBaseDN" , "base passwd lDAPBaseDN" and
494 // "base shadow lDAPBaseDN") for the property "lDAPBaseDN",
495 // one is enough to restore it.
496
497 if ((key == "base") &&
498 (value == "passwd" || value == "shadow"))
499 {
500 continue;
501 }
502 // skip the line if it starts with "map passwd".
503 // if config type is AD "map group" entry would be add to
504 // the map configValues. For OpenLdap config file no map
505 // entry would be there.
506 if ((key == "map") && (value == "passwd"))
507 {
508 continue;
509 }
510 configValues[key] = value;
511 }
512 }
513 }
514
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500515 ldap_base::Create::SearchScope lDAPSearchScope;
516 if (configValues["scope"] == "sub")
517 {
518 lDAPSearchScope = ldap_base::Create::SearchScope::sub;
519 }
520 else if (configValues["scope"] == "one")
521 {
522 lDAPSearchScope = ldap_base::Create::SearchScope::one;
523 }
524 else
525 {
526 lDAPSearchScope = ldap_base::Create::SearchScope::base;
527 }
528
529 ldap_base::Create::Type lDAPType;
530 // If the file is having a line which starts with "map group"
531 if (configValues["map"] == "group")
532 {
533 lDAPType = ldap_base::Create::Type::ActiveDirectory;
534 }
535 else
536 {
537 lDAPType = ldap_base::Create::Type::OpenLdap;
538 }
539
Ratan Guptac9c86a22018-10-18 00:47:01 +0530540 // Don't create the config object if either of the field is empty.
541 if (configValues["uri"] == "" || configValues["binddn"] == "" ||
542 configValues["base"] == "")
543 {
544 log<level::INFO>(
545 "LDAP config parameter value missing",
546 entry("URI=%s", configValues["uri"].c_str()),
547 entry("BASEDN=%s", configValues["base"].c_str()),
548 entry("BINDDN=%s", configValues["binddn"].c_str()));
549 return;
550 }
551
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500552 createConfig(
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600553 std::move(configValues["uri"]), std::move(configValues["binddn"]),
554 std::move(configValues["base"]), std::move(configValues["bindpw"]),
555 lDAPSearchScope, lDAPType);
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500556 }
557 catch (const InvalidArgument& e)
558 {
559 // Don't throw - we don't want to create a D-Bus
560 // object upon finding empty values in config, as
561 // this can be a default config.
562 }
563 catch (const InternalFailure& e)
564 {
565 throw;
566 }
567 catch (const std::exception& e)
568 {
569 log<level::ERR>(e.what());
570 elog<InternalFailure>();
571 }
572}
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500573} // namespace ldap
574} // namespace phosphor