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