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