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