blob: a60e8f8c9a5402a0e81c05b7e23452e0a6002416 [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"
3#include "ldap_config_serialize.hpp"
Ratan Guptae1f4db62019-04-11 18:57:42 +05304
5#include "utils.hpp"
6#include <filesystem>
7#include <fstream>
8#include <sstream>
9
10namespace phosphor
11{
12namespace ldap
13{
14
15constexpr auto nscdService = "nscd.service";
16constexpr auto LDAPscheme = "ldap";
17constexpr auto LDAPSscheme = "ldaps";
18
19using namespace phosphor::logging;
20using namespace sdbusplus::xyz::openbmc_project::Common::Error;
21namespace fs = std::filesystem;
22using Argument = xyz::openbmc_project::Common::InvalidArgument;
23
24using Line = std::string;
25using Key = std::string;
26using Val = std::string;
27using ConfigInfo = std::map<Key, Val>;
28
29void ConfigMgr::startOrStopService(const std::string& service, bool start)
30{
31 if (start)
32 {
33 restartService(service);
34 }
35 else
36 {
37 stopService(service);
38 }
39}
40
41void ConfigMgr::restartService(const std::string& service)
42{
43 try
44 {
45 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
46 SYSTEMD_INTERFACE, "RestartUnit");
47 method.append(service.c_str(), "replace");
48 bus.call_noreply(method);
49 }
50 catch (const sdbusplus::exception::SdBusError& ex)
51 {
52 log<level::ERR>("Failed to restart service",
53 entry("SERVICE=%s", service.c_str()),
54 entry("ERR=%s", ex.what()));
55 elog<InternalFailure>();
56 }
57}
58void ConfigMgr::stopService(const std::string& service)
59{
60 try
61 {
62 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
63 SYSTEMD_INTERFACE, "StopUnit");
64 method.append(service.c_str(), "replace");
65 bus.call_noreply(method);
66 }
67 catch (const sdbusplus::exception::SdBusError& ex)
68 {
69 log<level::ERR>("Failed to stop service",
70 entry("SERVICE=%s", service.c_str()),
71 entry("ERR=%s", ex.what()));
72 elog<InternalFailure>();
73 }
74}
75
76void ConfigMgr::deleteObject()
77{
Ratan Gupta27d4c012019-04-12 13:03:35 +053078 // TODO Not needed the delete functionality.
79 // will do in later commit
Ratan Guptae1f4db62019-04-11 18:57:42 +053080}
81
82std::string ConfigMgr::createConfig(
83 std::string lDAPServerURI, std::string lDAPBindDN, std::string lDAPBaseDN,
84 std::string lDAPBindDNPassword, CreateIface::SearchScope lDAPSearchScope,
85 CreateIface::Create::Type lDAPType, std::string groupNameAttribute,
86 std::string userNameAttribute)
87{
88 bool secureLDAP = false;
89
90 if (isValidLDAPURI(lDAPServerURI, LDAPSscheme))
91 {
92 secureLDAP = true;
93 }
94 else if (isValidLDAPURI(lDAPServerURI, LDAPscheme))
95 {
96 secureLDAP = false;
97 }
98 else
99 {
100 log<level::ERR>("bad LDAP Server URI",
101 entry("LDAPSERVERURI=%s", lDAPServerURI.c_str()));
102 elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"),
103 Argument::ARGUMENT_VALUE(lDAPServerURI.c_str()));
104 }
105
106 if (secureLDAP && !fs::exists(tlsCacertFile.c_str()))
107 {
108 log<level::ERR>("LDAP server's CA certificate not provided",
109 entry("TLSCACERTFILE=%s", tlsCacertFile.c_str()));
110 elog<NoCACertificate>();
111 }
112
113 if (lDAPBindDN.empty())
114 {
115 log<level::ERR>("Not a valid LDAP BINDDN",
116 entry("LDAPBINDDN=%s", lDAPBindDN.c_str()));
117 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBindDN"),
118 Argument::ARGUMENT_VALUE(lDAPBindDN.c_str()));
119 }
120
121 if (lDAPBaseDN.empty())
122 {
123 log<level::ERR>("Not a valid LDAP BASEDN",
124 entry("LDAPBASEDN=%s", lDAPBaseDN.c_str()));
125 elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBaseDN"),
126 Argument::ARGUMENT_VALUE(lDAPBaseDN.c_str()));
127 }
128
Ratan Gupta27d4c012019-04-12 13:03:35 +0530129 // With current implementation we support only two default LDAP server.
130 // which will be always there but when the support comes for additional
131 // account providers then the create config would be used to create the
132 // additional config.
Ratan Guptae1f4db62019-04-11 18:57:42 +0530133
Ratan Gupta27d4c012019-04-12 13:03:35 +0530134 std::string objPath;
Ratan Guptae1f4db62019-04-11 18:57:42 +0530135
Ratan Gupta27d4c012019-04-12 13:03:35 +0530136 if (static_cast<ConfigIface::Type>(lDAPType) == ConfigIface::Type::OpenLdap)
137 {
138 openLDAPConfigPtr.reset(nullptr);
139 objPath = openLDAPDbusObjectPath;
140 openLDAPConfigPtr = std::make_unique<Config>(
141 bus, objPath.c_str(), configFilePath.c_str(), tlsCacertFile.c_str(),
142 secureLDAP, lDAPServerURI, lDAPBindDN, lDAPBaseDN,
143 std::move(lDAPBindDNPassword),
144 static_cast<ConfigIface::SearchScope>(lDAPSearchScope),
145 static_cast<ConfigIface::Type>(lDAPType), false, groupNameAttribute,
146 userNameAttribute, *this);
147 }
148 else
149 {
150 ADConfigPtr.reset(nullptr);
151 objPath = ADDbusObjectPath;
152 ADConfigPtr = std::make_unique<Config>(
153 bus, objPath.c_str(), configFilePath.c_str(), tlsCacertFile.c_str(),
154 secureLDAP, lDAPServerURI, lDAPBindDN, lDAPBaseDN,
155 std::move(lDAPBindDNPassword),
156 static_cast<ConfigIface::SearchScope>(lDAPSearchScope),
157 static_cast<ConfigIface::Type>(lDAPType), false, groupNameAttribute,
158 userNameAttribute, *this);
159 }
Ratan Guptae1f4db62019-04-11 18:57:42 +0530160 restartService(nscdService);
161 return objPath;
162}
163
Ratan Gupta27d4c012019-04-12 13:03:35 +0530164void ConfigMgr::createDefaultObjects()
Ratan Guptae1f4db62019-04-11 18:57:42 +0530165{
Ratan Gupta27d4c012019-04-12 13:03:35 +0530166 if (!openLDAPConfigPtr)
Ratan Guptae1f4db62019-04-11 18:57:42 +0530167 {
Ratan Gupta27d4c012019-04-12 13:03:35 +0530168 openLDAPConfigPtr = std::make_unique<Config>(
169 bus, openLDAPDbusObjectPath.c_str(), configFilePath.c_str(),
170 tlsCacertFile.c_str(), false, "", "", "", "",
171 ConfigIface::SearchScope::sub, ConfigIface::Type::OpenLdap, false,
172 "", "", *this);
Ratan Guptae1f4db62019-04-11 18:57:42 +0530173 }
Ratan Gupta27d4c012019-04-12 13:03:35 +0530174 if (!ADConfigPtr)
Ratan Guptae1f4db62019-04-11 18:57:42 +0530175 {
Ratan Gupta27d4c012019-04-12 13:03:35 +0530176 ADConfigPtr = std::make_unique<Config>(
177 bus, ADDbusObjectPath.c_str(), configFilePath.c_str(),
178 tlsCacertFile.c_str(), false, "", "", "", "",
179 ConfigIface::SearchScope::sub, ConfigIface::Type::ActiveDirectory,
180 false, "", "", *this);
Ratan Guptae1f4db62019-04-11 18:57:42 +0530181 }
182}
Ratan Gupta27d4c012019-04-12 13:03:35 +0530183
Ratan Guptae1f4db62019-04-11 18:57:42 +0530184} // namespace ldap
185} // namespace phosphor