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