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