blob: 31e4e7560b2f7904fa5ed8589ca90321cb2ff96b [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 {
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060052 fs::path configDir = fs::path(configFilePath.c_str()).parent_path();
53
54 fs::copy_file(configDir / defaultNslcdFile, LDAP_CONFIG_FILE,
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -050055 fs::copy_options::overwrite_existing);
Ratan Gupta3e7a72e2018-10-18 09:57:14 +053056
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060057 fs::copy_file(configDir / linuxNsSwitchFile, configDir / nsSwitchFile,
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -050058 fs::copy_options::overwrite_existing);
59 }
60 catch (const std::exception& e)
61 {
62 log<level::ERR>("Failed to rename Config Files while deleting Object",
63 entry("ERR=%s", e.what()));
64 elog<InternalFailure>();
65 }
66
67 parent.restartService(nscdService);
68 parent.stopService(nslcdService);
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -050069}
70
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050071void Config::writeConfig()
72{
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050073 std::stringstream confData;
Ratan Gupta9891f2f2018-10-06 12:07:35 +053074 auto isPwdTobeWritten = false;
75
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050076 confData << "uid root\n";
77 confData << "gid root\n\n";
78 confData << "ldap_version 3\n\n";
79 confData << "timelimit 30\n";
80 confData << "bind_timelimit 30\n";
81 confData << "pagesize 1000\n";
82 confData << "referrals off\n\n";
83 confData << "uri " << lDAPServerURI() << "\n\n";
84 confData << "base " << lDAPBaseDN() << "\n\n";
85 confData << "binddn " << lDAPBindDN() << "\n";
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060086 if (!lDAPBindDNPassword.empty())
Nagaraju Goruganti15675472018-10-05 07:03:05 -050087 {
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -060088 confData << "bindpw " << lDAPBindDNPassword << "\n";
Ratan Gupta9891f2f2018-10-06 12:07:35 +053089 isPwdTobeWritten = true;
Nagaraju Goruganti15675472018-10-05 07:03:05 -050090 }
91 confData << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -050092 switch (lDAPSearchScope())
93 {
94 case ldap_base::Config::SearchScope::sub:
95 confData << "scope sub\n\n";
96 break;
97 case ldap_base::Config::SearchScope::one:
98 confData << "scope one\n\n";
99 break;
100 case ldap_base::Config::SearchScope::base:
101 confData << "scope base\n\n";
102 break;
103 }
104 confData << "base passwd " << lDAPBaseDN() << "\n";
105 confData << "base shadow " << lDAPBaseDN() << "\n\n";
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600106 if (secureLDAP == true)
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500107 {
108 confData << "ssl on\n";
109 confData << "tls_reqcert allow\n";
110 confData << "tls_cert /etc/nslcd/certs/cert.pem\n";
111 }
112 else
113 {
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500114 confData << "ssl off\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500115 }
Nagaraju Goruganti15675472018-10-05 07:03:05 -0500116 confData << "\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500117 if (lDAPType() == ldap_base::Config::Type::ActiveDirectory)
118 {
119 confData << "filter passwd (&(objectClass=user)(objectClass=person)"
120 "(!(objectClass=computer)))\n";
121 confData
122 << "filter group (|(objectclass=group)(objectclass=groupofnames) "
123 "(objectclass=groupofuniquenames))\n";
124 confData << "map passwd uid sAMAccountName\n";
125 confData << "map passwd uidNumber "
126 "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
127 confData << "map passwd gidNumber primaryGroupID\n";
128 confData << "map passwd homeDirectory \"/home/$sAMAccountName\"\n";
129 confData << "map passwd gecos displayName\n";
130 confData << "map passwd loginShell \"/bin/bash\"\n";
131 confData << "map group gidNumber primaryGroupID\n";
132 confData << "map group gidNumber "
133 "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
134 confData << "map group cn sAMAccountName\n";
135 }
136 else if (lDAPType() == ldap_base::Config::Type::OpenLdap)
137 {
138 confData << "filter passwd (objectclass=*)\n";
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500139 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 }
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600241 catch (const InvalidArgument& e)
242 {
243 throw;
244 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500245 catch (const std::exception& e)
246 {
247 log<level::ERR>(e.what());
248 elog<InternalFailure>();
249 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500250 return val;
251}
252
253std::string Config::lDAPBaseDN(std::string value)
254{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500255 std::string val;
256 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500257 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500258 if (value == lDAPBaseDN())
259 {
260 return value;
261 }
262
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500263 if (value.empty())
264 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500265 log<level::ERR>("Not a valid LDAP BASEDN",
266 entry("BASEDN=%s", value.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500267 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBaseDN"),
268 Argument::ARGUMENT_VALUE(value.c_str()));
269 }
270
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500271 val = ConfigIface::lDAPBaseDN(value);
272 writeConfig();
273 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500274 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500275 catch (const InternalFailure& e)
276 {
277 throw;
278 }
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600279 catch (const InvalidArgument& e)
280 {
281 throw;
282 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500283 catch (const std::exception& e)
284 {
285 log<level::ERR>(e.what());
286 elog<InternalFailure>();
287 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500288 return val;
289}
290
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500291ldap_base::Config::SearchScope
292 Config::lDAPSearchScope(ldap_base::Config::SearchScope value)
293{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500294 ldap_base::Config::SearchScope val;
295 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500296 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500297 if (value == lDAPSearchScope())
298 {
299 return value;
300 }
301
302 val = ConfigIface::lDAPSearchScope(value);
303 writeConfig();
304 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500305 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500306 catch (const InternalFailure& e)
307 {
308 throw;
309 }
310 catch (const std::exception& e)
311 {
312 log<level::ERR>(e.what());
313 elog<InternalFailure>();
314 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500315 return val;
316}
317
318ldap_base::Config::Type Config::lDAPType(ldap_base::Config::Type value)
319{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500320 ldap_base::Config::Type val;
321 try
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500322 {
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500323 if (value == lDAPType())
324 {
325 return value;
326 }
327
328 val = ConfigIface::lDAPType(value);
329 writeConfig();
330 parent.restartService(nslcdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500331 }
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500332 catch (const InternalFailure& e)
333 {
334 throw;
335 }
336 catch (const std::exception& e)
337 {
338 log<level::ERR>(e.what());
339 elog<InternalFailure>();
340 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500341 return val;
342}
343
344void ConfigMgr::restartService(const std::string& service)
345{
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500346 try
347 {
348 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
349 SYSTEMD_INTERFACE, "RestartUnit");
350 method.append(service.c_str(), "replace");
351 bus.call_noreply(method);
352 }
353 catch (const sdbusplus::exception::SdBusError& ex)
354 {
355 log<level::ERR>("Failed to restart nslcd service",
356 entry("ERR=%s", ex.what()));
357 elog<InternalFailure>();
358 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500359}
360
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500361void ConfigMgr::stopService(const std::string& service)
362{
363 try
364 {
365 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
366 SYSTEMD_INTERFACE, "StopUnit");
367 method.append(service.c_str(), "replace");
368 bus.call_noreply(method);
369 }
370 catch (const sdbusplus::exception::SdBusError& ex)
371 {
372 log<level::ERR>("Failed to stop nslcd service",
373 entry("ERR=%s", ex.what()));
374 elog<InternalFailure>();
375 }
376}
377
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500378void ConfigMgr::deleteObject()
379{
380 configPtr.reset(nullptr);
381}
382
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500383std::string
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600384 ConfigMgr::createConfig(std::string lDAPServerURI, std::string lDAPBindDN,
385 std::string lDAPBaseDN,
386 std::string lDAPBindDNPassword,
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500387 ldap_base::Create::SearchScope lDAPSearchScope,
388 ldap_base::Create::Type lDAPType)
389{
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600390 bool secureLDAP = false;
391
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500392 if (isValidLDAPURI(lDAPServerURI, LDAPSscheme))
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500393 {
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600394 secureLDAP = true;
395 }
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500396 else if (isValidLDAPURI(lDAPServerURI, LDAPscheme))
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600397 {
398 secureLDAP = false;
399 }
400 else
401 {
402 log<level::ERR>("bad LDAP Server URI",
403 entry("LDAPSERVERURI=%s", lDAPServerURI.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500404 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"),
405 Argument::ARGUMENT_VALUE(lDAPServerURI.c_str()));
406 }
407
408 if (lDAPBindDN.empty())
409 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500410 log<level::ERR>("Not a valid LDAP BINDDN",
411 entry("LDAPBINDDN=%s", lDAPBindDN.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500412 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBindDN"),
413 Argument::ARGUMENT_VALUE(lDAPBindDN.c_str()));
414 }
415
416 if (lDAPBaseDN.empty())
417 {
Nagaraju Goruganti59287f02018-10-12 07:00:20 -0500418 log<level::ERR>("Not a valid LDAP BASEDN",
419 entry("LDAPBASEDN=%s", lDAPBaseDN.c_str()));
Nagaraju Gorugantib26799a2018-09-28 13:12:19 -0500420 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBaseDN"),
421 Argument::ARGUMENT_VALUE(lDAPBaseDN.c_str()));
422 }
423
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500424 // With current implementation we support only one LDAP server.
Nagaraju Goruganti24194bd2018-09-18 09:55:09 -0500425 deleteObject();
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500426 try
427 {
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600428 fs::path configDir = fs::path(configFilePath.c_str()).parent_path();
429 fs::copy_file(configDir / LDAPNsSwitchFile, configDir / nsSwitchFile,
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500430 fs::copy_options::overwrite_existing);
431 }
432 catch (const std::exception& e)
433 {
434 log<level::ERR>("Failed to rename Config Files while creating Object",
435 entry("ERR=%s", e.what()));
436 elog<InternalFailure>();
437 }
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500438
439 auto objPath = std::string(LDAP_CONFIG_DBUS_OBJ_PATH);
440 configPtr = std::make_unique<Config>(
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600441 bus, objPath.c_str(), configFilePath.c_str(), secureLDAP, lDAPServerURI,
442 lDAPBindDN, lDAPBaseDN, std::move(lDAPBindDNPassword),
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500443 static_cast<ldap_base::Config::SearchScope>(lDAPSearchScope),
444 static_cast<ldap_base::Config::Type>(lDAPType), *this);
445
Nagaraju Gorugantidccee2b2018-09-25 08:51:06 -0500446 restartService(nslcdService);
447 restartService(nscdService);
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500448 return objPath;
449}
450
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500451void ConfigMgr::restore(const char* filePath)
452{
453 if (!fs::exists(filePath))
454 {
455 log<level::ERR>("Config file doesn't exists",
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -0600456 entry("LDAP_CONFIG_FILE=%s", configFilePath.c_str()));
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500457 return;
458 }
459
460 ConfigInfo configValues;
461
462 try
463 {
464 std::fstream stream(filePath, std::fstream::in);
465 Line line;
466 // read characters from stream and places them into line
467 while (std::getline(stream, line))
468 {
469 // remove leading and trailing extra spaces
470 auto firstScan = line.find_first_not_of(' ');
471 auto first =
472 (firstScan == std::string::npos ? line.length() : firstScan);
473 auto last = line.find_last_not_of(' ');
474 line = line.substr(first, last - first + 1);
475 // reduce multiple spaces between two words to a single space
476 auto pred = [](char a, char b) {
477 return (a == b && a == ' ') ? true : false;
478 };
479
480 auto lastPos = std::unique(line.begin(), line.end(), pred);
481
482 line.erase(lastPos, line.end());
483
484 // Ignore if line is empty or starts with '#'
485 if (line.empty() || line.at(0) == '#')
486 {
487 continue;
488 }
489
490 Key key;
491 std::istringstream isLine(line);
492 // extract characters from isLine and stores them into
493 // key until the delimitation character ' ' is found.
494 // If the delimiter is found, it is extracted and discarded
495 // the next input operation will begin after it.
496 if (std::getline(isLine, key, ' '))
497 {
498 Val value;
499 // extract characters after delimitation character ' '
500 if (std::getline(isLine, value, ' '))
501 {
502 // skip line if it starts with "base shadow" or
503 // "base passwd" because we would have 3 entries
504 // ("base lDAPBaseDN" , "base passwd lDAPBaseDN" and
505 // "base shadow lDAPBaseDN") for the property "lDAPBaseDN",
506 // one is enough to restore it.
507
508 if ((key == "base") &&
509 (value == "passwd" || value == "shadow"))
510 {
511 continue;
512 }
513 // skip the line if it starts with "map passwd".
514 // if config type is AD "map group" entry would be add to
515 // the map configValues. For OpenLdap config file no map
516 // entry would be there.
517 if ((key == "map") && (value == "passwd"))
518 {
519 continue;
520 }
521 configValues[key] = value;
522 }
523 }
524 }
525
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500526 ldap_base::Create::SearchScope lDAPSearchScope;
527 if (configValues["scope"] == "sub")
528 {
529 lDAPSearchScope = ldap_base::Create::SearchScope::sub;
530 }
531 else if (configValues["scope"] == "one")
532 {
533 lDAPSearchScope = ldap_base::Create::SearchScope::one;
534 }
535 else
536 {
537 lDAPSearchScope = ldap_base::Create::SearchScope::base;
538 }
539
540 ldap_base::Create::Type lDAPType;
541 // If the file is having a line which starts with "map group"
542 if (configValues["map"] == "group")
543 {
544 lDAPType = ldap_base::Create::Type::ActiveDirectory;
545 }
546 else
547 {
548 lDAPType = ldap_base::Create::Type::OpenLdap;
549 }
550
Ratan Guptac9c86a22018-10-18 00:47:01 +0530551 // Don't create the config object if either of the field is empty.
552 if (configValues["uri"] == "" || configValues["binddn"] == "" ||
553 configValues["base"] == "")
554 {
555 log<level::INFO>(
556 "LDAP config parameter value missing",
557 entry("URI=%s", configValues["uri"].c_str()),
558 entry("BASEDN=%s", configValues["base"].c_str()),
559 entry("BINDDN=%s", configValues["binddn"].c_str()));
560 return;
561 }
562
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500563 createConfig(
Nagaraju Gorugantidb60f582018-11-08 03:14:48 -0600564 std::move(configValues["uri"]), std::move(configValues["binddn"]),
565 std::move(configValues["base"]), std::move(configValues["bindpw"]),
566 lDAPSearchScope, lDAPType);
Nagaraju Gorugantif1940d92018-09-18 05:05:50 -0500567 }
568 catch (const InvalidArgument& e)
569 {
570 // Don't throw - we don't want to create a D-Bus
571 // object upon finding empty values in config, as
572 // this can be a default config.
573 }
574 catch (const InternalFailure& e)
575 {
576 throw;
577 }
578 catch (const std::exception& e)
579 {
580 log<level::ERR>(e.what());
581 elog<InternalFailure>();
582 }
583}
Nagaraju Goruganti997f5e02018-08-30 03:05:11 -0500584} // namespace ldap
585} // namespace phosphor