Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 1 | #include "ldap_config_mgr.hpp" |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 2 | |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 3 | #include "ldap_config.hpp" |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 4 | #include "utils.hpp" |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 5 | |
Jiaqing Zhao | e8d664d | 2022-07-05 21:22:54 +0800 | [diff] [blame] | 6 | #include <phosphor-logging/elog-errors.hpp> |
| 7 | #include <phosphor-logging/elog.hpp> |
| 8 | #include <phosphor-logging/lg2.hpp> |
| 9 | #include <xyz/openbmc_project/Common/error.hpp> |
| 10 | |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 11 | #include <filesystem> |
| 12 | #include <fstream> |
| 13 | #include <sstream> |
| 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace ldap |
| 18 | { |
| 19 | |
Alexander Filippov | 372c566 | 2021-06-30 20:23:39 +0300 | [diff] [blame] | 20 | constexpr auto nslcdService = "nslcd.service"; |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 21 | constexpr auto nscdService = "nscd.service"; |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 22 | constexpr auto ldapScheme = "ldap"; |
| 23 | constexpr auto ldapsScheme = "ldaps"; |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 24 | |
Jiaqing Zhao | e8d664d | 2022-07-05 21:22:54 +0800 | [diff] [blame] | 25 | using namespace phosphor::logging; |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 26 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 27 | namespace fs = std::filesystem; |
| 28 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | c5481d1 | 2019-04-12 18:31:05 +0530 | [diff] [blame] | 29 | using NotAllowedArgument = xyz::openbmc_project::Common::NotAllowed; |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 30 | |
| 31 | using Line = std::string; |
| 32 | using Key = std::string; |
| 33 | using Val = std::string; |
| 34 | using ConfigInfo = std::map<Key, Val>; |
| 35 | |
| 36 | void ConfigMgr::startOrStopService(const std::string& service, bool start) |
| 37 | { |
| 38 | if (start) |
| 39 | { |
| 40 | restartService(service); |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | stopService(service); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void ConfigMgr::restartService(const std::string& service) |
| 49 | { |
| 50 | try |
| 51 | { |
| 52 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 53 | SYSTEMD_INTERFACE, "RestartUnit"); |
| 54 | method.append(service.c_str(), "replace"); |
| 55 | bus.call_noreply(method); |
| 56 | } |
Patrick Williams | b3ef4e1 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 57 | catch (const sdbusplus::exception_t& ex) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 58 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 59 | lg2::error("Failed to restart service {SERVICE}: {ERR}", "SERVICE", |
| 60 | service, "ERR", ex); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 61 | elog<InternalFailure>(); |
| 62 | } |
| 63 | } |
| 64 | void ConfigMgr::stopService(const std::string& service) |
| 65 | { |
| 66 | try |
| 67 | { |
| 68 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 69 | SYSTEMD_INTERFACE, "StopUnit"); |
| 70 | method.append(service.c_str(), "replace"); |
| 71 | bus.call_noreply(method); |
| 72 | } |
Patrick Williams | b3ef4e1 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 73 | catch (const sdbusplus::exception_t& ex) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 74 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 75 | lg2::error("Failed to stop service {SERVICE}: {ERR}", "SERVICE", |
| 76 | service, "ERR", ex); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 77 | elog<InternalFailure>(); |
| 78 | } |
| 79 | } |
| 80 | |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 81 | std::string ConfigMgr::createConfig( |
Patrick Williams | e6500a4 | 2021-05-01 05:58:23 -0500 | [diff] [blame] | 82 | std::string ldapServerURI, std::string ldapBindDN, std::string ldapBaseDN, |
| 83 | std::string ldapBindDNPassword, CreateIface::SearchScope ldapSearchScope, |
| 84 | CreateIface::Create::Type ldapType, std::string groupNameAttribute, |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 85 | std::string userNameAttribute) |
| 86 | { |
| 87 | bool secureLDAP = false; |
| 88 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 89 | if (isValidLDAPURI(ldapServerURI, ldapsScheme)) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 90 | { |
| 91 | secureLDAP = true; |
| 92 | } |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 93 | else if (isValidLDAPURI(ldapServerURI, ldapScheme)) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 94 | { |
| 95 | secureLDAP = false; |
| 96 | } |
| 97 | else |
| 98 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 99 | lg2::error("Bad LDAP Server URI {URI}", "URI", ldapServerURI); |
Patrick Williams | e6500a4 | 2021-05-01 05:58:23 -0500 | [diff] [blame] | 100 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ldapServerURI"), |
| 101 | Argument::ARGUMENT_VALUE(ldapServerURI.c_str())); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (secureLDAP && !fs::exists(tlsCacertFile.c_str())) |
| 105 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 106 | lg2::error("LDAP server CA certificate not found at {PATH}", "PATH", |
| 107 | tlsCacertFile); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 108 | elog<NoCACertificate>(); |
| 109 | } |
| 110 | |
Patrick Williams | e6500a4 | 2021-05-01 05:58:23 -0500 | [diff] [blame] | 111 | if (ldapBindDN.empty()) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 112 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 113 | lg2::error("'{BINDDN}' is not a valid LDAP BindDN", "BINDDN", |
| 114 | ldapBindDN); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 115 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBindDN"), |
Patrick Williams | e6500a4 | 2021-05-01 05:58:23 -0500 | [diff] [blame] | 116 | Argument::ARGUMENT_VALUE(ldapBindDN.c_str())); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 117 | } |
| 118 | |
Patrick Williams | e6500a4 | 2021-05-01 05:58:23 -0500 | [diff] [blame] | 119 | if (ldapBaseDN.empty()) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 120 | { |
Jiaqing Zhao | 11ec666 | 2022-07-05 20:55:34 +0800 | [diff] [blame] | 121 | lg2::error("'{BASEDN}' is not a valid LDAP BaseDN", "BASEDN", |
| 122 | ldapBaseDN); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 123 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("LDAPBaseDN"), |
Patrick Williams | e6500a4 | 2021-05-01 05:58:23 -0500 | [diff] [blame] | 124 | Argument::ARGUMENT_VALUE(ldapBaseDN.c_str())); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 125 | } |
| 126 | |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 127 | // With current implementation we support only two default LDAP server. |
| 128 | // which will be always there but when the support comes for additional |
| 129 | // account providers then the create config would be used to create the |
| 130 | // additional config. |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 131 | |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 132 | std::string objPath; |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 133 | |
Patrick Williams | e6500a4 | 2021-05-01 05:58:23 -0500 | [diff] [blame] | 134 | if (static_cast<ConfigIface::Type>(ldapType) == ConfigIface::Type::OpenLdap) |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 135 | { |
| 136 | openLDAPConfigPtr.reset(nullptr); |
| 137 | objPath = openLDAPDbusObjectPath; |
| 138 | openLDAPConfigPtr = std::make_unique<Config>( |
| 139 | bus, objPath.c_str(), configFilePath.c_str(), tlsCacertFile.c_str(), |
Patrick Williams | e6500a4 | 2021-05-01 05:58:23 -0500 | [diff] [blame] | 140 | tlsCertFile.c_str(), secureLDAP, ldapServerURI, ldapBindDN, |
| 141 | ldapBaseDN, std::move(ldapBindDNPassword), |
| 142 | static_cast<ConfigIface::SearchScope>(ldapSearchScope), |
| 143 | static_cast<ConfigIface::Type>(ldapType), false, groupNameAttribute, |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 144 | userNameAttribute, *this); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | ADConfigPtr.reset(nullptr); |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 149 | objPath = adDbusObjectPath; |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 150 | ADConfigPtr = std::make_unique<Config>( |
| 151 | bus, objPath.c_str(), configFilePath.c_str(), tlsCacertFile.c_str(), |
Patrick Williams | e6500a4 | 2021-05-01 05:58:23 -0500 | [diff] [blame] | 152 | tlsCertFile.c_str(), secureLDAP, ldapServerURI, ldapBindDN, |
| 153 | ldapBaseDN, std::move(ldapBindDNPassword), |
| 154 | static_cast<ConfigIface::SearchScope>(ldapSearchScope), |
| 155 | static_cast<ConfigIface::Type>(ldapType), false, groupNameAttribute, |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 156 | userNameAttribute, *this); |
| 157 | } |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 158 | restartService(nscdService); |
| 159 | return objPath; |
| 160 | } |
| 161 | |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 162 | void ConfigMgr::createDefaultObjects() |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 163 | { |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 164 | if (!openLDAPConfigPtr) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 165 | { |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 166 | openLDAPConfigPtr = std::make_unique<Config>( |
| 167 | bus, openLDAPDbusObjectPath.c_str(), configFilePath.c_str(), |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 168 | tlsCacertFile.c_str(), tlsCertFile.c_str(), |
| 169 | ConfigIface::Type::OpenLdap, *this); |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 170 | openLDAPConfigPtr->emit_object_added(); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 171 | } |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 172 | if (!ADConfigPtr) |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 173 | { |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 174 | ADConfigPtr = std::make_unique<Config>( |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 175 | bus, adDbusObjectPath.c_str(), configFilePath.c_str(), |
Ratan Gupta | ab4fcb4 | 2019-04-29 19:39:51 +0530 | [diff] [blame] | 176 | tlsCacertFile.c_str(), tlsCertFile.c_str(), |
| 177 | ConfigIface::Type::ActiveDirectory, *this); |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 178 | ADConfigPtr->emit_object_added(); |
| 179 | } |
| 180 | } |
| 181 | |
Ratan Gupta | c5481d1 | 2019-04-12 18:31:05 +0530 | [diff] [blame] | 182 | bool ConfigMgr::enableService(Config& config, bool value) |
| 183 | { |
| 184 | if (value) |
| 185 | { |
| 186 | if (openLDAPConfigPtr && openLDAPConfigPtr->enabled()) |
| 187 | { |
| 188 | elog<NotAllowed>(NotAllowedArgument::REASON( |
| 189 | "OpenLDAP service is already active")); |
| 190 | } |
| 191 | if (ADConfigPtr && ADConfigPtr->enabled()) |
| 192 | { |
| 193 | elog<NotAllowed>(NotAllowedArgument::REASON( |
| 194 | "ActiveDirectory service is already active")); |
| 195 | } |
| 196 | } |
| 197 | return config.enableService(value); |
| 198 | } |
| 199 | |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 200 | void ConfigMgr::restore() |
| 201 | { |
| 202 | createDefaultObjects(); |
| 203 | // Restore the ldap config and their mappings |
| 204 | if (ADConfigPtr->deserialize()) |
| 205 | { |
Ratan Gupta | 7b04c35 | 2019-04-12 21:46:29 +0530 | [diff] [blame] | 206 | // Restore the role mappings |
| 207 | ADConfigPtr->restoreRoleMapping(); |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 208 | ADConfigPtr->emit_object_added(); |
| 209 | } |
| 210 | if (openLDAPConfigPtr->deserialize()) |
| 211 | { |
Ratan Gupta | 7b04c35 | 2019-04-12 21:46:29 +0530 | [diff] [blame] | 212 | // Restore the role mappings |
| 213 | openLDAPConfigPtr->restoreRoleMapping(); |
Ratan Gupta | 21e88cb | 2019-04-12 17:15:52 +0530 | [diff] [blame] | 214 | openLDAPConfigPtr->emit_object_added(); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 215 | } |
Alexander Filippov | 372c566 | 2021-06-30 20:23:39 +0300 | [diff] [blame] | 216 | |
| 217 | startOrStopService(phosphor::ldap::nslcdService, |
| 218 | ADConfigPtr->enabled() || openLDAPConfigPtr->enabled()); |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 219 | } |
Ratan Gupta | 27d4c01 | 2019-04-12 13:03:35 +0530 | [diff] [blame] | 220 | |
Ratan Gupta | e1f4db6 | 2019-04-11 18:57:42 +0530 | [diff] [blame] | 221 | } // namespace ldap |
| 222 | } // namespace phosphor |