Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 1 | #include "ldap_config_mgr.hpp" |
Ratan Gupta | 37fb3fe | 2019-04-13 12:54:18 +0530 | [diff] [blame] | 2 | #include "ldap_config.hpp" |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 3 | #include "utils.hpp" |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 4 | |
| 5 | #include <cereal/types/string.hpp> |
| 6 | #include <cereal/types/vector.hpp> |
| 7 | #include <cereal/archives/binary.hpp> |
Ratan Gupta | 7b04c35 | 2019-04-12 21:46:29 +0530 | [diff] [blame] | 8 | #include "ldap_mapper_serialize.hpp" |
| 9 | |
| 10 | #include <xyz/openbmc_project/Common/error.hpp> |
| 11 | #include <xyz/openbmc_project/User/Common/error.hpp> |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 12 | #include <filesystem> |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 13 | #include <fstream> |
| 14 | #include <sstream> |
| 15 | |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 16 | // Register class version |
| 17 | // From cereal documentation; |
| 18 | // "This macro should be placed at global scope" |
| 19 | CEREAL_CLASS_VERSION(phosphor::ldap::Config, CLASS_VERSION); |
| 20 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 21 | namespace phosphor |
| 22 | { |
| 23 | namespace ldap |
| 24 | { |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 25 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 26 | constexpr auto nslcdService = "nslcd.service"; |
Nagaraju Goruganti | dccee2b | 2018-09-25 08:51:06 -0500 | [diff] [blame] | 27 | constexpr auto nscdService = "nscd.service"; |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 28 | constexpr auto LDAPscheme = "ldap"; |
| 29 | constexpr auto LDAPSscheme = "ldaps"; |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 30 | constexpr auto certObjPath = "/xyz/openbmc_project/certs/client/ldap/1"; |
| 31 | constexpr auto certRootPath = "/xyz/openbmc_project/certs/client/ldap"; |
| 32 | constexpr auto certIface = "xyz.openbmc_project.Certs.Certificate"; |
| 33 | constexpr auto certProperty = "CertificateString"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 34 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 35 | using namespace phosphor::logging; |
| 36 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 95a2931 | 2019-02-18 20:34:10 +0530 | [diff] [blame] | 37 | namespace fs = std::filesystem; |
Ratan Gupta | 7b04c35 | 2019-04-12 21:46:29 +0530 | [diff] [blame] | 38 | |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 39 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 40 | using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 41 | using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed; |
Ratan Gupta | 7b04c35 | 2019-04-12 21:46:29 +0530 | [diff] [blame] | 42 | using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common:: |
| 43 | Error::PrivilegeMappingExists; |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 44 | |
| 45 | using Line = std::string; |
| 46 | using Key = std::string; |
| 47 | using Val = std::string; |
| 48 | using ConfigInfo = std::map<Key, Val>; |
| 49 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 50 | Config::Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath, |
Ratan Gupta | 22f13f1 | 2019-04-29 15:36:40 +0530 | [diff] [blame] | 51 | const char* caCertFile, const char* certFile, bool secureLDAP, |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 52 | std::string lDAPServerURI, std::string lDAPBindDN, |
| 53 | std::string lDAPBaseDN, std::string&& lDAPBindDNPassword, |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 54 | ConfigIface::SearchScope lDAPSearchScope, |
| 55 | ConfigIface::Type lDAPType, bool lDAPServiceEnabled, |
| 56 | std::string userNameAttr, std::string groupNameAttr, |
| 57 | ConfigMgr& parent) : |
| 58 | Ifaces(bus, path, true), |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 59 | secureLDAP(secureLDAP), lDAPBindPassword(std::move(lDAPBindDNPassword)), |
Ratan Gupta | 22f13f1 | 2019-04-29 15:36:40 +0530 | [diff] [blame] | 60 | tlsCacertFile(caCertFile), tlsCertFile(certFile), configFilePath(filePath), |
| 61 | objectPath(path), bus(bus), parent(parent), |
| 62 | certificateInstalledSignal( |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 63 | bus, sdbusplus::bus::match::rules::interfacesAdded(certRootPath), |
Ratan Gupta | 22f13f1 | 2019-04-29 15:36:40 +0530 | [diff] [blame] | 64 | std::bind(std::mem_fn(&Config::certificateInstalled), this, |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 65 | std::placeholders::_1)), |
| 66 | certificateChangedSignal( |
| 67 | bus, |
| 68 | sdbusplus::bus::match::rules::propertiesChanged(certObjPath, certIface), |
| 69 | std::bind(std::mem_fn(&Config::certificateChanged), this, |
Ratan Gupta | 22f13f1 | 2019-04-29 15:36:40 +0530 | [diff] [blame] | 70 | std::placeholders::_1)) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 71 | { |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 72 | ConfigIface::lDAPServerURI(lDAPServerURI); |
| 73 | ConfigIface::lDAPBindDN(lDAPBindDN); |
| 74 | ConfigIface::lDAPBaseDN(lDAPBaseDN); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 75 | ConfigIface::lDAPSearchScope(lDAPSearchScope); |
| 76 | ConfigIface::lDAPType(lDAPType); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 77 | EnableIface::enabled(lDAPServiceEnabled); |
| 78 | ConfigIface::userNameAttribute(userNameAttr); |
| 79 | ConfigIface::groupNameAttribute(groupNameAttr); |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 80 | // NOTE: Don't update the bindDN password under ConfigIface |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 81 | if (enabled()) |
| 82 | { |
| 83 | writeConfig(); |
| 84 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 85 | // save the config. |
| 86 | configPersistPath = parent.dbusPersistentPath; |
| 87 | configPersistPath += objectPath; |
| 88 | |
| 89 | // create the persistent directory |
| 90 | fs::create_directories(configPersistPath); |
| 91 | |
| 92 | configPersistPath += "/config"; |
| 93 | |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 94 | serialize(); |
| 95 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 96 | // Emit deferred signal. |
| 97 | this->emit_object_added(); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 98 | parent.startOrStopService(nslcdService, enabled()); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 101 | Config::Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath, |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 102 | const char* caCertFile, const char* certFile, |
| 103 | ConfigIface::Type lDAPType, ConfigMgr& parent) : |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 104 | Ifaces(bus, path, true), |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 105 | tlsCacertFile(caCertFile), tlsCertFile(certFile), configFilePath(filePath), |
| 106 | objectPath(path), bus(bus), parent(parent), |
Ratan Gupta | 22f13f1 | 2019-04-29 15:36:40 +0530 | [diff] [blame] | 107 | certificateInstalledSignal( |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 108 | bus, sdbusplus::bus::match::rules::interfacesAdded(certRootPath), |
Ratan Gupta | 22f13f1 | 2019-04-29 15:36:40 +0530 | [diff] [blame] | 109 | std::bind(std::mem_fn(&Config::certificateInstalled), this, |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 110 | std::placeholders::_1)), |
| 111 | certificateChangedSignal( |
| 112 | bus, |
| 113 | sdbusplus::bus::match::rules::propertiesChanged(certObjPath, certIface), |
| 114 | std::bind(std::mem_fn(&Config::certificateChanged), this, |
Ratan Gupta | 22f13f1 | 2019-04-29 15:36:40 +0530 | [diff] [blame] | 115 | std::placeholders::_1)) |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 116 | { |
| 117 | ConfigIface::lDAPType(lDAPType); |
| 118 | |
| 119 | configPersistPath = parent.dbusPersistentPath; |
| 120 | configPersistPath += objectPath; |
| 121 | |
| 122 | // create the persistent directory |
| 123 | fs::create_directories(configPersistPath); |
| 124 | |
| 125 | configPersistPath += "/config"; |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 126 | } |
| 127 | |
Ratan Gupta | 22f13f1 | 2019-04-29 15:36:40 +0530 | [diff] [blame] | 128 | void Config::certificateInstalled(sdbusplus::message::message& msg) |
| 129 | { |
| 130 | try |
| 131 | { |
| 132 | if (enabled()) |
| 133 | { |
| 134 | writeConfig(); |
| 135 | } |
| 136 | parent.startOrStopService(nslcdService, enabled()); |
| 137 | } |
| 138 | catch (const InternalFailure& e) |
| 139 | { |
| 140 | throw; |
| 141 | } |
| 142 | catch (const std::exception& e) |
| 143 | { |
| 144 | log<level::ERR>(e.what()); |
| 145 | elog<InternalFailure>(); |
| 146 | } |
| 147 | } |
| 148 | |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 149 | void Config::certificateChanged(sdbusplus::message::message& msg) |
| 150 | { |
| 151 | // TODO: Property filtering needs to be done, we need to write |
| 152 | // the config only when the property is "Certificate String". |
| 153 | try |
| 154 | { |
| 155 | if (enabled()) |
| 156 | { |
| 157 | writeConfig(); |
| 158 | } |
| 159 | parent.startOrStopService(nslcdService, enabled()); |
| 160 | } |
| 161 | catch (const InternalFailure& e) |
| 162 | { |
| 163 | throw; |
| 164 | } |
| 165 | catch (const std::exception& e) |
| 166 | { |
| 167 | log<level::ERR>(e.what()); |
| 168 | elog<InternalFailure>(); |
| 169 | } |
| 170 | } |
| 171 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 172 | void Config::writeConfig() |
| 173 | { |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 174 | std::stringstream confData; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 175 | auto isPwdTobeWritten = false; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 176 | std::string userNameAttr; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 177 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 178 | confData << "uid root\n"; |
| 179 | confData << "gid root\n\n"; |
| 180 | confData << "ldap_version 3\n\n"; |
| 181 | confData << "timelimit 30\n"; |
| 182 | confData << "bind_timelimit 30\n"; |
| 183 | confData << "pagesize 1000\n"; |
| 184 | confData << "referrals off\n\n"; |
| 185 | confData << "uri " << lDAPServerURI() << "\n\n"; |
| 186 | confData << "base " << lDAPBaseDN() << "\n\n"; |
| 187 | confData << "binddn " << lDAPBindDN() << "\n"; |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 188 | if (!lDAPBindPassword.empty()) |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 189 | { |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 190 | confData << "bindpw " << lDAPBindPassword << "\n"; |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 191 | isPwdTobeWritten = true; |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 192 | } |
| 193 | confData << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 194 | switch (lDAPSearchScope()) |
| 195 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 196 | case ConfigIface::SearchScope::sub: |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 197 | confData << "scope sub\n\n"; |
| 198 | break; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 199 | case ConfigIface::SearchScope::one: |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 200 | confData << "scope one\n\n"; |
| 201 | break; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 202 | case ConfigIface::SearchScope::base: |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 203 | confData << "scope base\n\n"; |
| 204 | break; |
| 205 | } |
| 206 | confData << "base passwd " << lDAPBaseDN() << "\n"; |
| 207 | confData << "base shadow " << lDAPBaseDN() << "\n\n"; |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 208 | if (secureLDAP == true) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 209 | { |
| 210 | confData << "ssl on\n"; |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 211 | confData << "tls_reqcert hard\n"; |
| 212 | confData << "tls_cacertFile " << tlsCacertFile.c_str() << "\n"; |
Ratan Gupta | 22f13f1 | 2019-04-29 15:36:40 +0530 | [diff] [blame] | 213 | if (fs::exists(tlsCertFile.c_str())) |
| 214 | { |
| 215 | confData << "tls_cert " << tlsCertFile.c_str() << "\n"; |
| 216 | confData << "tls_key " << tlsCertFile.c_str() << "\n"; |
| 217 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 218 | } |
| 219 | else |
| 220 | { |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 221 | confData << "ssl off\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 222 | } |
Nagaraju Goruganti | 1567547 | 2018-10-05 07:03:05 -0500 | [diff] [blame] | 223 | confData << "\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 224 | if (lDAPType() == ConfigIface::Type::ActiveDirectory) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 225 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 226 | if (ConfigIface::userNameAttribute().empty()) |
| 227 | { |
| 228 | ConfigIface::userNameAttribute("sAMAccountName"); |
| 229 | } |
| 230 | if (ConfigIface::groupNameAttribute().empty()) |
| 231 | { |
| 232 | ConfigIface::groupNameAttribute("primaryGroupID"); |
| 233 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 234 | confData << "filter passwd (&(objectClass=user)(objectClass=person)" |
| 235 | "(!(objectClass=computer)))\n"; |
| 236 | confData |
| 237 | << "filter group (|(objectclass=group)(objectclass=groupofnames) " |
| 238 | "(objectclass=groupofuniquenames))\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 239 | confData << "map passwd uid " |
| 240 | << ConfigIface::userNameAttribute() << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 241 | confData << "map passwd uidNumber " |
| 242 | "objectSid:S-1-5-21-3623811015-3361044348-30300820\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 243 | confData << "map passwd gidNumber " |
| 244 | << ConfigIface::groupNameAttribute() << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 245 | confData << "map passwd homeDirectory \"/home/$sAMAccountName\"\n"; |
| 246 | confData << "map passwd gecos displayName\n"; |
| 247 | confData << "map passwd loginShell \"/bin/bash\"\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 248 | confData << "map group gidNumber " |
| 249 | "objectSid:S-1-5-21-3623811015-3361044348-30300820\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 250 | confData << "map group cn " |
| 251 | << ConfigIface::userNameAttribute() << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 252 | } |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 253 | else if (lDAPType() == ConfigIface::Type::OpenLdap) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 254 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 255 | if (ConfigIface::userNameAttribute().empty()) |
| 256 | { |
raviteja-b | c3f56c5 | 2019-04-02 11:09:04 -0500 | [diff] [blame] | 257 | ConfigIface::userNameAttribute("cn"); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 258 | } |
| 259 | if (ConfigIface::groupNameAttribute().empty()) |
| 260 | { |
raviteja-b | c3f56c5 | 2019-04-02 11:09:04 -0500 | [diff] [blame] | 261 | ConfigIface::groupNameAttribute("gidNumber"); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 262 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 263 | confData << "filter passwd (objectclass=*)\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 264 | confData << "map passwd gecos displayName\n"; |
Nagaraju Goruganti | 808eda4 | 2018-10-10 08:48:12 -0500 | [diff] [blame] | 265 | confData << "filter group (objectclass=posixGroup)\n"; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 266 | confData << "map passwd uid " |
| 267 | << ConfigIface::userNameAttribute() << "\n"; |
| 268 | confData << "map passwd gidNumber " |
| 269 | << ConfigIface::groupNameAttribute() << "\n"; |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 270 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 271 | try |
| 272 | { |
| 273 | std::fstream stream(configFilePath.c_str(), std::fstream::out); |
Ratan Gupta | 9891f2f | 2018-10-06 12:07:35 +0530 | [diff] [blame] | 274 | // remove the read permission from others if password is being written. |
| 275 | // nslcd forces this behaviour. |
| 276 | auto permission = fs::perms::owner_read | fs::perms::owner_write | |
| 277 | fs::perms::group_read; |
| 278 | if (isPwdTobeWritten) |
| 279 | { |
| 280 | fs::permissions(configFilePath, permission); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | fs::permissions(configFilePath, |
| 285 | permission | fs::perms::others_read); |
| 286 | } |
| 287 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 288 | stream << confData.str(); |
| 289 | stream.flush(); |
| 290 | stream.close(); |
| 291 | } |
| 292 | catch (const std::exception& e) |
| 293 | { |
| 294 | log<level::ERR>(e.what()); |
| 295 | elog<InternalFailure>(); |
| 296 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 300 | std::string Config::lDAPBindDNPassword(std::string value) |
| 301 | { |
| 302 | // Don't update the D-bus object, this is just to |
| 303 | // facilitate if user wants to change the bind dn password |
| 304 | // once d-bus object gets created. |
| 305 | lDAPBindPassword = value; |
| 306 | try |
| 307 | { |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 308 | if (enabled()) |
| 309 | { |
| 310 | writeConfig(); |
| 311 | parent.startOrStopService(nslcdService, enabled()); |
| 312 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 313 | serialize(); |
Ratan Gupta | 3a1c274 | 2019-03-20 06:49:42 +0530 | [diff] [blame] | 314 | } |
| 315 | catch (const InternalFailure& e) |
| 316 | { |
| 317 | throw; |
| 318 | } |
| 319 | catch (const std::exception& e) |
| 320 | { |
| 321 | log<level::ERR>(e.what()); |
| 322 | elog<InternalFailure>(); |
| 323 | } |
| 324 | return value; |
| 325 | } |
| 326 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 327 | std::string Config::lDAPServerURI(std::string value) |
| 328 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 329 | std::string val; |
| 330 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 331 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 332 | if (value == lDAPServerURI()) |
| 333 | { |
| 334 | return value; |
| 335 | } |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 336 | if (isValidLDAPURI(value, LDAPSscheme)) |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 337 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 338 | secureLDAP = true; |
| 339 | } |
| 340 | else if (isValidLDAPURI(value, LDAPscheme)) |
| 341 | { |
| 342 | secureLDAP = false; |
Nagaraju Goruganti | db60f58 | 2018-11-08 03:14:48 -0600 | [diff] [blame] | 343 | } |
| 344 | else |
| 345 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 346 | log<level::ERR>("bad LDAP Server URI", |
| 347 | entry("LDAPSERVERURI=%s", value.c_str())); |
| 348 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPServerURI"), |
| 349 | Argument::ARGUMENT_VALUE(value.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 350 | } |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 351 | |
| 352 | if (secureLDAP && !fs::exists(tlsCacertFile.c_str())) |
| 353 | { |
| 354 | log<level::ERR>("LDAP server's CA certificate not provided", |
| 355 | entry("TLSCACERTFILE=%s", tlsCacertFile.c_str())); |
| 356 | elog<NoCACertificate>(); |
| 357 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 358 | val = ConfigIface::lDAPServerURI(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 359 | if (enabled()) |
| 360 | { |
| 361 | writeConfig(); |
| 362 | parent.startOrStopService(nslcdService, enabled()); |
| 363 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 364 | // save the object. |
| 365 | serialize(); |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 366 | } |
| 367 | catch (const InternalFailure& e) |
| 368 | { |
| 369 | throw; |
| 370 | } |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 371 | catch (const InvalidArgument& e) |
| 372 | { |
| 373 | throw; |
| 374 | } |
Nagaraju Goruganti | 3b4d06a | 2018-11-08 03:13:38 -0600 | [diff] [blame] | 375 | catch (const NoCACertificate& e) |
| 376 | { |
| 377 | throw; |
| 378 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 379 | catch (const std::exception& e) |
| 380 | { |
| 381 | log<level::ERR>(e.what()); |
| 382 | elog<InternalFailure>(); |
| 383 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 384 | return val; |
| 385 | } |
| 386 | |
| 387 | std::string Config::lDAPBindDN(std::string value) |
| 388 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 389 | std::string val; |
| 390 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 391 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 392 | if (value == lDAPBindDN()) |
| 393 | { |
| 394 | return value; |
| 395 | } |
| 396 | |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 397 | if (value.empty()) |
| 398 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 399 | log<level::ERR>("Not a valid LDAP BINDDN", |
| 400 | entry("LDAPBINDDN=%s", value.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 401 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBindDN"), |
| 402 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 403 | } |
| 404 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 405 | val = ConfigIface::lDAPBindDN(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 406 | if (enabled()) |
| 407 | { |
| 408 | writeConfig(); |
| 409 | parent.startOrStopService(nslcdService, enabled()); |
| 410 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 411 | // save the object. |
| 412 | serialize(); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 413 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 414 | catch (const InternalFailure& e) |
| 415 | { |
| 416 | throw; |
| 417 | } |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 418 | catch (const InvalidArgument& e) |
| 419 | { |
| 420 | throw; |
| 421 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 422 | catch (const std::exception& e) |
| 423 | { |
| 424 | log<level::ERR>(e.what()); |
| 425 | elog<InternalFailure>(); |
| 426 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 427 | return val; |
| 428 | } |
| 429 | |
| 430 | std::string Config::lDAPBaseDN(std::string value) |
| 431 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 432 | std::string val; |
| 433 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 434 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 435 | if (value == lDAPBaseDN()) |
| 436 | { |
| 437 | return value; |
| 438 | } |
| 439 | |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 440 | if (value.empty()) |
| 441 | { |
Nagaraju Goruganti | 59287f0 | 2018-10-12 07:00:20 -0500 | [diff] [blame] | 442 | log<level::ERR>("Not a valid LDAP BASEDN", |
| 443 | entry("BASEDN=%s", value.c_str())); |
Nagaraju Goruganti | b26799a | 2018-09-28 13:12:19 -0500 | [diff] [blame] | 444 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("lDAPBaseDN"), |
| 445 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 446 | } |
| 447 | |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 448 | val = ConfigIface::lDAPBaseDN(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 449 | if (enabled()) |
| 450 | { |
| 451 | writeConfig(); |
| 452 | parent.startOrStopService(nslcdService, enabled()); |
| 453 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 454 | // save the object. |
| 455 | serialize(); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 456 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 457 | catch (const InternalFailure& e) |
| 458 | { |
| 459 | throw; |
| 460 | } |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 461 | catch (const InvalidArgument& e) |
| 462 | { |
| 463 | throw; |
| 464 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 465 | catch (const std::exception& e) |
| 466 | { |
| 467 | log<level::ERR>(e.what()); |
| 468 | elog<InternalFailure>(); |
| 469 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 470 | return val; |
| 471 | } |
| 472 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 473 | ConfigIface::SearchScope Config::lDAPSearchScope(ConfigIface::SearchScope value) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 474 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 475 | ConfigIface::SearchScope val; |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 476 | try |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 477 | { |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 478 | if (value == lDAPSearchScope()) |
| 479 | { |
| 480 | return value; |
| 481 | } |
| 482 | |
| 483 | val = ConfigIface::lDAPSearchScope(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 484 | if (enabled()) |
| 485 | { |
| 486 | writeConfig(); |
| 487 | |
| 488 | parent.startOrStopService(nslcdService, enabled()); |
| 489 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 490 | // save the object. |
| 491 | serialize(); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 492 | } |
Nagaraju Goruganti | f1940d9 | 2018-09-18 05:05:50 -0500 | [diff] [blame] | 493 | catch (const InternalFailure& e) |
| 494 | { |
| 495 | throw; |
| 496 | } |
| 497 | catch (const std::exception& e) |
| 498 | { |
| 499 | log<level::ERR>(e.what()); |
| 500 | elog<InternalFailure>(); |
| 501 | } |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 502 | return val; |
| 503 | } |
| 504 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 505 | ConfigIface::Type Config::lDAPType(ConfigIface::Type value) |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 506 | { |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 507 | elog<NotAllowed>(NotAllowedArgument::REASON("ReadOnly Property")); |
| 508 | return lDAPType(); |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 509 | } |
| 510 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 511 | bool Config::enabled(bool value) |
| 512 | { |
Ratan Gupta | c5481d1 | 2019-04-12 18:31:05 +0530 | [diff] [blame] | 513 | if (value == enabled()) |
| 514 | { |
| 515 | return value; |
| 516 | } |
| 517 | // Let parent decide that can we enable this config. |
| 518 | // It may happen that other config is already enabled, |
| 519 | // Current implementation support only one config can |
| 520 | // be active at a time. |
| 521 | return parent.enableService(*this, value); |
| 522 | } |
| 523 | |
| 524 | bool Config::enableService(bool value) |
| 525 | { |
| 526 | bool isEnable = false; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 527 | try |
| 528 | { |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 529 | isEnable = EnableIface::enabled(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 530 | if (isEnable) |
| 531 | { |
| 532 | writeConfig(); |
| 533 | } |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 534 | parent.startOrStopService(nslcdService, value); |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 535 | serialize(); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 536 | } |
| 537 | catch (const InternalFailure& e) |
| 538 | { |
| 539 | throw; |
| 540 | } |
| 541 | catch (const std::exception& e) |
| 542 | { |
| 543 | log<level::ERR>(e.what()); |
| 544 | elog<InternalFailure>(); |
| 545 | } |
| 546 | return isEnable; |
| 547 | } |
| 548 | |
| 549 | std::string Config::userNameAttribute(std::string value) |
| 550 | { |
| 551 | std::string val; |
| 552 | try |
| 553 | { |
| 554 | if (value == userNameAttribute()) |
| 555 | { |
| 556 | return value; |
| 557 | } |
| 558 | |
| 559 | val = ConfigIface::userNameAttribute(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 560 | if (enabled()) |
| 561 | { |
| 562 | writeConfig(); |
| 563 | |
| 564 | parent.startOrStopService(nslcdService, enabled()); |
| 565 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 566 | // save the object. |
| 567 | serialize(); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 568 | } |
| 569 | catch (const InternalFailure& e) |
| 570 | { |
| 571 | throw; |
| 572 | } |
| 573 | catch (const std::exception& e) |
| 574 | { |
| 575 | log<level::ERR>(e.what()); |
| 576 | elog<InternalFailure>(); |
| 577 | } |
| 578 | return val; |
| 579 | } |
| 580 | |
| 581 | std::string Config::groupNameAttribute(std::string value) |
| 582 | { |
| 583 | std::string val; |
| 584 | try |
| 585 | { |
| 586 | if (value == groupNameAttribute()) |
| 587 | { |
| 588 | return value; |
| 589 | } |
| 590 | |
| 591 | val = ConfigIface::groupNameAttribute(value); |
Ratan Gupta | ec11754 | 2019-04-25 18:38:29 +0530 | [diff] [blame] | 592 | if (enabled()) |
| 593 | { |
| 594 | writeConfig(); |
| 595 | |
| 596 | parent.startOrStopService(nslcdService, enabled()); |
| 597 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 598 | // save the object. |
| 599 | serialize(); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 600 | } |
| 601 | catch (const InternalFailure& e) |
| 602 | { |
| 603 | throw; |
| 604 | } |
| 605 | catch (const std::exception& e) |
| 606 | { |
| 607 | log<level::ERR>(e.what()); |
| 608 | elog<InternalFailure>(); |
| 609 | } |
| 610 | return val; |
| 611 | } |
| 612 | |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 613 | template <class Archive> |
| 614 | void Config::save(Archive& archive, const std::uint32_t version) const |
| 615 | { |
| 616 | archive(this->enabled()); |
| 617 | archive(lDAPServerURI()); |
| 618 | archive(lDAPBindDN()); |
| 619 | archive(lDAPBaseDN()); |
| 620 | archive(lDAPSearchScope()); |
| 621 | archive(lDAPBindPassword); |
| 622 | archive(userNameAttribute()); |
| 623 | archive(groupNameAttribute()); |
| 624 | } |
| 625 | |
| 626 | template <class Archive> |
| 627 | void Config::load(Archive& archive, const std::uint32_t version) |
| 628 | { |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 629 | bool bVal; |
| 630 | archive(bVal); |
| 631 | EnableIface::enabled(bVal); |
| 632 | |
| 633 | std::string str; |
| 634 | archive(str); |
| 635 | ConfigIface::lDAPServerURI(str); |
| 636 | |
| 637 | archive(str); |
| 638 | ConfigIface::lDAPBindDN(str); |
| 639 | |
| 640 | archive(str); |
| 641 | ConfigIface::lDAPBaseDN(str); |
| 642 | |
| 643 | ConfigIface::SearchScope scope; |
| 644 | archive(scope); |
| 645 | ConfigIface::lDAPSearchScope(scope); |
| 646 | |
| 647 | archive(str); |
| 648 | lDAPBindPassword = str; |
| 649 | |
| 650 | archive(str); |
| 651 | ConfigIface::userNameAttribute(str); |
| 652 | |
| 653 | archive(str); |
| 654 | ConfigIface::groupNameAttribute(str); |
| 655 | } |
| 656 | |
| 657 | void Config::serialize() |
| 658 | { |
Ravi Teja | 59dba44 | 2019-05-20 09:31:28 -0500 | [diff] [blame^] | 659 | |
| 660 | if (!fs::exists(configPersistPath.c_str())) |
| 661 | { |
| 662 | std::ofstream os(configPersistPath.string(), |
| 663 | std::ios::binary | std::ios::out); |
| 664 | auto permission = fs::perms::owner_read | fs::perms::owner_write | |
| 665 | fs::perms::group_read; |
| 666 | fs::permissions(configPersistPath, permission); |
| 667 | cereal::BinaryOutputArchive oarchive(os); |
| 668 | oarchive(*this); |
| 669 | } |
| 670 | else |
| 671 | { |
| 672 | std::ofstream os(configPersistPath.string(), |
| 673 | std::ios::binary | std::ios::out); |
| 674 | cereal::BinaryOutputArchive oarchive(os); |
| 675 | oarchive(*this); |
| 676 | } |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 677 | return; |
| 678 | } |
| 679 | |
| 680 | bool Config::deserialize() |
| 681 | { |
| 682 | try |
| 683 | { |
| 684 | if (fs::exists(configPersistPath)) |
| 685 | { |
| 686 | std::ifstream is(configPersistPath.c_str(), |
| 687 | std::ios::in | std::ios::binary); |
| 688 | cereal::BinaryInputArchive iarchive(is); |
| 689 | iarchive(*this); |
| 690 | return true; |
| 691 | } |
| 692 | return false; |
| 693 | } |
| 694 | catch (cereal::Exception& e) |
| 695 | { |
| 696 | log<level::ERR>(e.what()); |
| 697 | std::error_code ec; |
| 698 | fs::remove(configPersistPath, ec); |
| 699 | return false; |
| 700 | } |
| 701 | catch (const fs::filesystem_error& e) |
| 702 | { |
| 703 | return false; |
| 704 | } |
| 705 | } |
| 706 | |
Ratan Gupta | 7b04c35 | 2019-04-12 21:46:29 +0530 | [diff] [blame] | 707 | ObjectPath Config::create(std::string groupName, std::string privilege) |
| 708 | { |
| 709 | checkPrivilegeMapper(groupName); |
| 710 | checkPrivilegeLevel(privilege); |
| 711 | |
| 712 | entryId++; |
| 713 | |
| 714 | // Object path for the LDAP group privilege mapper entry |
| 715 | fs::path mapperObjectPath = objectPath; |
| 716 | mapperObjectPath /= "role_map"; |
| 717 | mapperObjectPath /= std::to_string(entryId); |
| 718 | |
| 719 | fs::path persistPath = parent.dbusPersistentPath; |
| 720 | persistPath += mapperObjectPath; |
| 721 | |
| 722 | // Create mapping for LDAP privilege mapper entry |
| 723 | auto entry = std::make_unique<LDAPMapperEntry>( |
| 724 | bus, mapperObjectPath.string().c_str(), persistPath.string().c_str(), |
| 725 | groupName, privilege, *this); |
| 726 | |
| 727 | phosphor::ldap::serialize(*entry, std::move(persistPath)); |
| 728 | |
| 729 | PrivilegeMapperList.emplace(entryId, std::move(entry)); |
| 730 | return mapperObjectPath.string(); |
| 731 | } |
| 732 | |
| 733 | void Config::deletePrivilegeMapper(Id id) |
| 734 | { |
| 735 | fs::path mapperObjectPath = objectPath; |
| 736 | mapperObjectPath /= "role_map"; |
| 737 | mapperObjectPath /= std::to_string(id); |
| 738 | |
| 739 | fs::path persistPath = parent.dbusPersistentPath; |
| 740 | persistPath += std::move(mapperObjectPath); |
| 741 | |
| 742 | // Delete the persistent representation of the privilege mapper. |
| 743 | fs::remove(std::move(persistPath)); |
| 744 | |
| 745 | PrivilegeMapperList.erase(id); |
| 746 | } |
| 747 | void Config::checkPrivilegeMapper(const std::string& groupName) |
| 748 | { |
| 749 | if (groupName.empty()) |
| 750 | { |
| 751 | log<level::ERR>("Group name is empty"); |
| 752 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group name"), |
| 753 | Argument::ARGUMENT_VALUE("Null")); |
| 754 | } |
| 755 | |
| 756 | for (const auto& val : PrivilegeMapperList) |
| 757 | { |
| 758 | if (val.second.get()->groupName() == groupName) |
| 759 | { |
| 760 | log<level::ERR>("Group name already exists"); |
| 761 | elog<PrivilegeMappingExists>(); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | void Config::checkPrivilegeLevel(const std::string& privilege) |
| 767 | { |
| 768 | if (privilege.empty()) |
| 769 | { |
| 770 | log<level::ERR>("Privilege level is empty"); |
| 771 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"), |
| 772 | Argument::ARGUMENT_VALUE("Null")); |
| 773 | } |
| 774 | |
| 775 | if (std::find(privMgr.begin(), privMgr.end(), privilege) == privMgr.end()) |
| 776 | { |
| 777 | log<level::ERR>("Invalid privilege"); |
| 778 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"), |
| 779 | Argument::ARGUMENT_VALUE(privilege.c_str())); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | void Config::restoreRoleMapping() |
| 784 | { |
| 785 | namespace fs = std::filesystem; |
| 786 | fs::path dir = parent.dbusPersistentPath; |
| 787 | dir += objectPath; |
| 788 | dir /= "role_map"; |
| 789 | |
| 790 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 791 | { |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | for (auto& file : fs::directory_iterator(dir)) |
| 796 | { |
| 797 | std::string id = file.path().filename().c_str(); |
| 798 | size_t idNum = std::stol(id); |
| 799 | |
| 800 | auto entryPath = objectPath + '/' + "role_map" + '/' + id; |
| 801 | auto persistPath = parent.dbusPersistentPath + entryPath; |
| 802 | auto entry = std::make_unique<LDAPMapperEntry>( |
| 803 | bus, entryPath.c_str(), persistPath.c_str(), *this); |
| 804 | if (phosphor::ldap::deserialize(file.path(), *entry)) |
| 805 | { |
| 806 | entry->Interfaces::emit_object_added(); |
| 807 | PrivilegeMapperList.emplace(idNum, std::move(entry)); |
| 808 | if (idNum > entryId) |
| 809 | { |
| 810 | entryId = idNum; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
Nagaraju Goruganti | 997f5e0 | 2018-08-30 03:05:11 -0500 | [diff] [blame] | 816 | } // namespace ldap |
| 817 | } // namespace phosphor |