188d16c9aSLewanczyk, Dawid /* 288d16c9aSLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation 388d16c9aSLewanczyk, Dawid // 488d16c9aSLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License"); 588d16c9aSLewanczyk, Dawid // you may not use this file except in compliance with the License. 688d16c9aSLewanczyk, Dawid // You may obtain a copy of the License at 788d16c9aSLewanczyk, Dawid // 888d16c9aSLewanczyk, Dawid // http://www.apache.org/licenses/LICENSE-2.0 988d16c9aSLewanczyk, Dawid // 1088d16c9aSLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software 1188d16c9aSLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS, 1288d16c9aSLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1388d16c9aSLewanczyk, Dawid // See the License for the specific language governing permissions and 1488d16c9aSLewanczyk, Dawid // limitations under the License. 1588d16c9aSLewanczyk, Dawid */ 1688d16c9aSLewanczyk, Dawid #pragma once 1788d16c9aSLewanczyk, Dawid 180ec8b83dSEd Tanous #include "generated/enums/account_service.hpp" 190ec8b83dSEd Tanous #include "registries/privilege_registry.hpp" 200ec8b83dSEd Tanous 217e860f15SJohn Edward Broadbent #include <app.hpp> 2224c8542dSRatan Gupta #include <dbus_utility.hpp> 2365b0dc32SEd Tanous #include <error_messages.hpp> 24b9b2e0b2SEd Tanous #include <openbmc_dbus_rest.hpp> 2552cc112dSEd Tanous #include <persistent_data.hpp> 2645ca1b86SEd Tanous #include <query.hpp> 271e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 28d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 29d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp> 30a840879dSEd Tanous #include <utils/json_utils.hpp> 311214b7e7SGunnar Mills 32c7229815SAbhishek Patel #include <optional> 33c7229815SAbhishek Patel #include <string> 34c7229815SAbhishek Patel #include <vector> 35c7229815SAbhishek Patel 361abe55efSEd Tanous namespace redfish 371abe55efSEd Tanous { 3888d16c9aSLewanczyk, Dawid 3923a21a1cSEd Tanous constexpr const char* ldapConfigObjectName = 406973a582SRatan Gupta "/xyz/openbmc_project/user/ldap/openldap"; 412c70f800SEd Tanous constexpr const char* adConfigObject = 42ab828d7cSRatan Gupta "/xyz/openbmc_project/user/ldap/active_directory"; 43ab828d7cSRatan Gupta 44b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/"; 456973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap"; 466973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config"; 476973a582SRatan Gupta constexpr const char* ldapConfigInterface = 486973a582SRatan Gupta "xyz.openbmc_project.User.Ldap.Config"; 496973a582SRatan Gupta constexpr const char* ldapCreateInterface = 506973a582SRatan Gupta "xyz.openbmc_project.User.Ldap.Create"; 516973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable"; 5206785244SRatan Gupta constexpr const char* ldapPrivMapperInterface = 5306785244SRatan Gupta "xyz.openbmc_project.User.PrivilegeMapper"; 546973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager"; 556973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties"; 566973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper"; 576973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper"; 586973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper"; 596973a582SRatan Gupta 6054fc587aSNagaraju Goruganti struct LDAPRoleMapData 6154fc587aSNagaraju Goruganti { 6254fc587aSNagaraju Goruganti std::string groupName; 6354fc587aSNagaraju Goruganti std::string privilege; 6454fc587aSNagaraju Goruganti }; 6554fc587aSNagaraju Goruganti 666973a582SRatan Gupta struct LDAPConfigData 676973a582SRatan Gupta { 686973a582SRatan Gupta std::string uri{}; 696973a582SRatan Gupta std::string bindDN{}; 706973a582SRatan Gupta std::string baseDN{}; 716973a582SRatan Gupta std::string searchScope{}; 726973a582SRatan Gupta std::string serverType{}; 736973a582SRatan Gupta bool serviceEnabled = false; 746973a582SRatan Gupta std::string userNameAttribute{}; 756973a582SRatan Gupta std::string groupAttribute{}; 7654fc587aSNagaraju Goruganti std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList; 776973a582SRatan Gupta }; 786973a582SRatan Gupta 7954fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role) 8084e12cb7SAppaRao Puli { 8184e12cb7SAppaRao Puli if (role == "priv-admin") 8284e12cb7SAppaRao Puli { 8384e12cb7SAppaRao Puli return "Administrator"; 8484e12cb7SAppaRao Puli } 853174e4dfSEd Tanous if (role == "priv-user") 8684e12cb7SAppaRao Puli { 87c80fee55SAppaRao Puli return "ReadOnly"; 8884e12cb7SAppaRao Puli } 893174e4dfSEd Tanous if (role == "priv-operator") 9084e12cb7SAppaRao Puli { 9184e12cb7SAppaRao Puli return "Operator"; 9284e12cb7SAppaRao Puli } 9384e12cb7SAppaRao Puli return ""; 9484e12cb7SAppaRao Puli } 9554fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role) 9684e12cb7SAppaRao Puli { 9784e12cb7SAppaRao Puli if (role == "Administrator") 9884e12cb7SAppaRao Puli { 9984e12cb7SAppaRao Puli return "priv-admin"; 10084e12cb7SAppaRao Puli } 1013174e4dfSEd Tanous if (role == "ReadOnly") 10284e12cb7SAppaRao Puli { 10384e12cb7SAppaRao Puli return "priv-user"; 10484e12cb7SAppaRao Puli } 1053174e4dfSEd Tanous if (role == "Operator") 10684e12cb7SAppaRao Puli { 10784e12cb7SAppaRao Puli return "priv-operator"; 10884e12cb7SAppaRao Puli } 10984e12cb7SAppaRao Puli return ""; 11084e12cb7SAppaRao Puli } 111b9b2e0b2SEd Tanous 112c7229815SAbhishek Patel /** 113c7229815SAbhishek Patel * @brief Maps user group names retrieved from D-Bus object to 114c7229815SAbhishek Patel * Account Types. 115c7229815SAbhishek Patel * 116c7229815SAbhishek Patel * @param[in] userGroups List of User groups 117c7229815SAbhishek Patel * @param[out] res AccountTypes populated 118c7229815SAbhishek Patel * 119c7229815SAbhishek Patel * @return true in case of success, false if UserGroups contains 120c7229815SAbhishek Patel * invalid group name(s). 121c7229815SAbhishek Patel */ 122c7229815SAbhishek Patel inline bool translateUserGroup(const std::vector<std::string>& userGroups, 123c7229815SAbhishek Patel crow::Response& res) 124c7229815SAbhishek Patel { 125c7229815SAbhishek Patel std::vector<std::string> accountTypes; 126c7229815SAbhishek Patel for (const auto& userGroup : userGroups) 127c7229815SAbhishek Patel { 128c7229815SAbhishek Patel if (userGroup == "redfish") 129c7229815SAbhishek Patel { 130c7229815SAbhishek Patel accountTypes.emplace_back("Redfish"); 131c7229815SAbhishek Patel accountTypes.emplace_back("WebUI"); 132c7229815SAbhishek Patel } 133c7229815SAbhishek Patel else if (userGroup == "ipmi") 134c7229815SAbhishek Patel { 135c7229815SAbhishek Patel accountTypes.emplace_back("IPMI"); 136c7229815SAbhishek Patel } 137c7229815SAbhishek Patel else if (userGroup == "ssh") 138c7229815SAbhishek Patel { 139c7229815SAbhishek Patel accountTypes.emplace_back("HostConsole"); 140c7229815SAbhishek Patel accountTypes.emplace_back("ManagerConsole"); 141c7229815SAbhishek Patel } 142c7229815SAbhishek Patel else if (userGroup == "web") 143c7229815SAbhishek Patel { 144c7229815SAbhishek Patel // 'web' is one of the valid groups in the UserGroups property of 145c7229815SAbhishek Patel // the user account in the D-Bus object. This group is currently not 146c7229815SAbhishek Patel // doing anything, and is considered to be equivalent to 'redfish'. 147c7229815SAbhishek Patel // 'redfish' user group is mapped to 'Redfish'and 'WebUI' 148c7229815SAbhishek Patel // AccountTypes, so do nothing here... 149c7229815SAbhishek Patel } 150c7229815SAbhishek Patel else 151c7229815SAbhishek Patel { 152c7229815SAbhishek Patel // Invalid user group name. Caller throws an excption. 153c7229815SAbhishek Patel return false; 154c7229815SAbhishek Patel } 155c7229815SAbhishek Patel } 156c7229815SAbhishek Patel 157c7229815SAbhishek Patel res.jsonValue["AccountTypes"] = std::move(accountTypes); 158c7229815SAbhishek Patel return true; 159c7229815SAbhishek Patel } 160c7229815SAbhishek Patel 1618d1b46d7Szhanghch05 inline void userErrorMessageHandler( 1628d1b46d7Szhanghch05 const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1638d1b46d7Szhanghch05 const std::string& newUser, const std::string& username) 16466b5ca76Sjayaprakash Mutyala { 16566b5ca76Sjayaprakash Mutyala if (e == nullptr) 16666b5ca76Sjayaprakash Mutyala { 16766b5ca76Sjayaprakash Mutyala messages::internalError(asyncResp->res); 16866b5ca76Sjayaprakash Mutyala return; 16966b5ca76Sjayaprakash Mutyala } 17066b5ca76Sjayaprakash Mutyala 171055806b3SManojkiran Eda const char* errorMessage = e->name; 17266b5ca76Sjayaprakash Mutyala if (strcmp(errorMessage, 17366b5ca76Sjayaprakash Mutyala "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0) 17466b5ca76Sjayaprakash Mutyala { 175d8a5d5d8SJiaqing Zhao messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount", 17666b5ca76Sjayaprakash Mutyala "UserName", newUser); 17766b5ca76Sjayaprakash Mutyala } 17866b5ca76Sjayaprakash Mutyala else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error." 17966b5ca76Sjayaprakash Mutyala "UserNameDoesNotExist") == 0) 18066b5ca76Sjayaprakash Mutyala { 181d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "ManagerAccount", username); 18266b5ca76Sjayaprakash Mutyala } 183d4d25793SEd Tanous else if ((strcmp(errorMessage, 184d4d25793SEd Tanous "xyz.openbmc_project.Common.Error.InvalidArgument") == 185d4d25793SEd Tanous 0) || 1860fda0f12SGeorge Liu (strcmp( 1870fda0f12SGeorge Liu errorMessage, 1880fda0f12SGeorge Liu "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") == 1890fda0f12SGeorge Liu 0)) 19066b5ca76Sjayaprakash Mutyala { 19166b5ca76Sjayaprakash Mutyala messages::propertyValueFormatError(asyncResp->res, newUser, "UserName"); 19266b5ca76Sjayaprakash Mutyala } 19366b5ca76Sjayaprakash Mutyala else if (strcmp(errorMessage, 19466b5ca76Sjayaprakash Mutyala "xyz.openbmc_project.User.Common.Error.NoResource") == 0) 19566b5ca76Sjayaprakash Mutyala { 19666b5ca76Sjayaprakash Mutyala messages::createLimitReachedForResource(asyncResp->res); 19766b5ca76Sjayaprakash Mutyala } 19866b5ca76Sjayaprakash Mutyala else 19966b5ca76Sjayaprakash Mutyala { 20066b5ca76Sjayaprakash Mutyala messages::internalError(asyncResp->res); 20166b5ca76Sjayaprakash Mutyala } 20266b5ca76Sjayaprakash Mutyala } 20366b5ca76Sjayaprakash Mutyala 20481ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse, 205ab828d7cSRatan Gupta const LDAPConfigData& confData, 206ab828d7cSRatan Gupta const std::string& ldapType) 2076973a582SRatan Gupta { 208ab828d7cSRatan Gupta std::string service = 209ab828d7cSRatan Gupta (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService"; 21054fc587aSNagaraju Goruganti 2111476687dSEd Tanous nlohmann::json& ldap = jsonResponse[ldapType]; 21254fc587aSNagaraju Goruganti 2131476687dSEd Tanous ldap["ServiceEnabled"] = confData.serviceEnabled; 2141476687dSEd Tanous ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri}); 2150ec8b83dSEd Tanous ldap["Authentication"]["AuthenticationType"] = 2160ec8b83dSEd Tanous account_service::AuthenticationTypes::UsernameAndPassword; 2171476687dSEd Tanous ldap["Authentication"]["Username"] = confData.bindDN; 2181476687dSEd Tanous ldap["Authentication"]["Password"] = nullptr; 2191476687dSEd Tanous 2201476687dSEd Tanous ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] = 2211476687dSEd Tanous nlohmann::json::array({confData.baseDN}); 2221476687dSEd Tanous ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] = 2231476687dSEd Tanous confData.userNameAttribute; 2241476687dSEd Tanous ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] = 2251476687dSEd Tanous confData.groupAttribute; 2261476687dSEd Tanous 2271476687dSEd Tanous nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"]; 22854fc587aSNagaraju Goruganti roleMapArray = nlohmann::json::array(); 2299eb808c1SEd Tanous for (const auto& obj : confData.groupRoleList) 23054fc587aSNagaraju Goruganti { 23154fc587aSNagaraju Goruganti BMCWEB_LOG_DEBUG << "Pushing the data groupName=" 23254fc587aSNagaraju Goruganti << obj.second.groupName << "\n"; 233613dabeaSEd Tanous 234613dabeaSEd Tanous nlohmann::json::array_t remoteGroupArray; 235613dabeaSEd Tanous nlohmann::json::object_t remoteGroup; 236613dabeaSEd Tanous remoteGroup["RemoteGroup"] = obj.second.groupName; 237613dabeaSEd Tanous remoteGroupArray.emplace_back(std::move(remoteGroup)); 238613dabeaSEd Tanous roleMapArray.emplace_back(std::move(remoteGroupArray)); 239613dabeaSEd Tanous 240613dabeaSEd Tanous nlohmann::json::array_t localRoleArray; 241613dabeaSEd Tanous nlohmann::json::object_t localRole; 242613dabeaSEd Tanous localRole["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege); 243613dabeaSEd Tanous localRoleArray.emplace_back(std::move(localRole)); 244613dabeaSEd Tanous roleMapArray.emplace_back(std::move(localRoleArray)); 24554fc587aSNagaraju Goruganti } 2466973a582SRatan Gupta } 2476973a582SRatan Gupta 2486973a582SRatan Gupta /** 24906785244SRatan Gupta * @brief validates given JSON input and then calls appropriate method to 25006785244SRatan Gupta * create, to delete or to set Rolemapping object based on the given input. 25106785244SRatan Gupta * 25206785244SRatan Gupta */ 25323a21a1cSEd Tanous inline void handleRoleMapPatch( 2548d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 25506785244SRatan Gupta const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData, 256f23b7296SEd Tanous const std::string& serverType, const std::vector<nlohmann::json>& input) 25706785244SRatan Gupta { 25806785244SRatan Gupta for (size_t index = 0; index < input.size(); index++) 25906785244SRatan Gupta { 260f23b7296SEd Tanous const nlohmann::json& thisJson = input[index]; 26106785244SRatan Gupta 26206785244SRatan Gupta if (thisJson.is_null()) 26306785244SRatan Gupta { 26406785244SRatan Gupta // delete the existing object 26506785244SRatan Gupta if (index < roleMapObjData.size()) 26606785244SRatan Gupta { 26706785244SRatan Gupta crow::connections::systemBus->async_method_call( 26806785244SRatan Gupta [asyncResp, roleMapObjData, serverType, 26906785244SRatan Gupta index](const boost::system::error_code ec) { 27006785244SRatan Gupta if (ec) 27106785244SRatan Gupta { 27206785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " << ec; 27306785244SRatan Gupta messages::internalError(asyncResp->res); 27406785244SRatan Gupta return; 27506785244SRatan Gupta } 27606785244SRatan Gupta asyncResp->res 27706785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"][index] = 27806785244SRatan Gupta nullptr; 27906785244SRatan Gupta }, 28006785244SRatan Gupta ldapDbusService, roleMapObjData[index].first, 28106785244SRatan Gupta "xyz.openbmc_project.Object.Delete", "Delete"); 28206785244SRatan Gupta } 28306785244SRatan Gupta else 28406785244SRatan Gupta { 28506785244SRatan Gupta BMCWEB_LOG_ERROR << "Can't delete the object"; 28606785244SRatan Gupta messages::propertyValueTypeError( 28771f52d96SEd Tanous asyncResp->res, 28871f52d96SEd Tanous thisJson.dump(2, ' ', true, 28971f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 29006785244SRatan Gupta "RemoteRoleMapping/" + std::to_string(index)); 29106785244SRatan Gupta return; 29206785244SRatan Gupta } 29306785244SRatan Gupta } 29406785244SRatan Gupta else if (thisJson.empty()) 29506785244SRatan Gupta { 29606785244SRatan Gupta // Don't do anything for the empty objects,parse next json 29706785244SRatan Gupta // eg {"RemoteRoleMapping",[{}]} 29806785244SRatan Gupta } 29906785244SRatan Gupta else 30006785244SRatan Gupta { 30106785244SRatan Gupta // update/create the object 30206785244SRatan Gupta std::optional<std::string> remoteGroup; 30306785244SRatan Gupta std::optional<std::string> localRole; 30406785244SRatan Gupta 305f23b7296SEd Tanous // This is a copy, but it's required in this case because of how 306f23b7296SEd Tanous // readJson is structured 307f23b7296SEd Tanous nlohmann::json thisJsonCopy = thisJson; 308f23b7296SEd Tanous if (!json_util::readJson(thisJsonCopy, asyncResp->res, 309f23b7296SEd Tanous "RemoteGroup", remoteGroup, "LocalRole", 310f23b7296SEd Tanous localRole)) 31106785244SRatan Gupta { 31206785244SRatan Gupta continue; 31306785244SRatan Gupta } 31406785244SRatan Gupta 31506785244SRatan Gupta // Update existing RoleMapping Object 31606785244SRatan Gupta if (index < roleMapObjData.size()) 31706785244SRatan Gupta { 31806785244SRatan Gupta BMCWEB_LOG_DEBUG << "Update Role Map Object"; 31906785244SRatan Gupta // If "RemoteGroup" info is provided 32006785244SRatan Gupta if (remoteGroup) 32106785244SRatan Gupta { 32206785244SRatan Gupta crow::connections::systemBus->async_method_call( 32306785244SRatan Gupta [asyncResp, roleMapObjData, serverType, index, 32406785244SRatan Gupta remoteGroup](const boost::system::error_code ec) { 32506785244SRatan Gupta if (ec) 32606785244SRatan Gupta { 327002d39b4SEd Tanous BMCWEB_LOG_ERROR << "DBUS response error: " << ec; 32806785244SRatan Gupta messages::internalError(asyncResp->res); 32906785244SRatan Gupta return; 33006785244SRatan Gupta } 33106785244SRatan Gupta asyncResp->res 332002d39b4SEd Tanous .jsonValue[serverType]["RemoteRoleMapping"][index] 333002d39b4SEd Tanous ["RemoteGroup"] = *remoteGroup; 33406785244SRatan Gupta }, 33506785244SRatan Gupta ldapDbusService, roleMapObjData[index].first, 33606785244SRatan Gupta propertyInterface, "Set", 33706785244SRatan Gupta "xyz.openbmc_project.User.PrivilegeMapperEntry", 33806785244SRatan Gupta "GroupName", 339168e20c1SEd Tanous dbus::utility::DbusVariantType( 340168e20c1SEd Tanous std::move(*remoteGroup))); 34106785244SRatan Gupta } 34206785244SRatan Gupta 34306785244SRatan Gupta // If "LocalRole" info is provided 34406785244SRatan Gupta if (localRole) 34506785244SRatan Gupta { 34606785244SRatan Gupta crow::connections::systemBus->async_method_call( 34706785244SRatan Gupta [asyncResp, roleMapObjData, serverType, index, 34806785244SRatan Gupta localRole](const boost::system::error_code ec) { 34906785244SRatan Gupta if (ec) 35006785244SRatan Gupta { 351002d39b4SEd Tanous BMCWEB_LOG_ERROR << "DBUS response error: " << ec; 35206785244SRatan Gupta messages::internalError(asyncResp->res); 35306785244SRatan Gupta return; 35406785244SRatan Gupta } 35506785244SRatan Gupta asyncResp->res 356002d39b4SEd Tanous .jsonValue[serverType]["RemoteRoleMapping"][index] 357002d39b4SEd Tanous ["LocalRole"] = *localRole; 35806785244SRatan Gupta }, 35906785244SRatan Gupta ldapDbusService, roleMapObjData[index].first, 36006785244SRatan Gupta propertyInterface, "Set", 36106785244SRatan Gupta "xyz.openbmc_project.User.PrivilegeMapperEntry", 36206785244SRatan Gupta "Privilege", 363168e20c1SEd Tanous dbus::utility::DbusVariantType( 36406785244SRatan Gupta getPrivilegeFromRoleId(std::move(*localRole)))); 36506785244SRatan Gupta } 36606785244SRatan Gupta } 36706785244SRatan Gupta // Create a new RoleMapping Object. 36806785244SRatan Gupta else 36906785244SRatan Gupta { 37006785244SRatan Gupta BMCWEB_LOG_DEBUG 37106785244SRatan Gupta << "setRoleMappingProperties: Creating new Object"; 37206785244SRatan Gupta std::string pathString = 37306785244SRatan Gupta "RemoteRoleMapping/" + std::to_string(index); 37406785244SRatan Gupta 37506785244SRatan Gupta if (!localRole) 37606785244SRatan Gupta { 37706785244SRatan Gupta messages::propertyMissing(asyncResp->res, 37806785244SRatan Gupta pathString + "/LocalRole"); 37906785244SRatan Gupta continue; 38006785244SRatan Gupta } 38106785244SRatan Gupta if (!remoteGroup) 38206785244SRatan Gupta { 38306785244SRatan Gupta messages::propertyMissing(asyncResp->res, 38406785244SRatan Gupta pathString + "/RemoteGroup"); 38506785244SRatan Gupta continue; 38606785244SRatan Gupta } 38706785244SRatan Gupta 38806785244SRatan Gupta std::string dbusObjectPath; 38906785244SRatan Gupta if (serverType == "ActiveDirectory") 39006785244SRatan Gupta { 3912c70f800SEd Tanous dbusObjectPath = adConfigObject; 39206785244SRatan Gupta } 39306785244SRatan Gupta else if (serverType == "LDAP") 39406785244SRatan Gupta { 39523a21a1cSEd Tanous dbusObjectPath = ldapConfigObjectName; 39606785244SRatan Gupta } 39706785244SRatan Gupta 39806785244SRatan Gupta BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup 39906785244SRatan Gupta << ",LocalRole=" << *localRole; 40006785244SRatan Gupta 40106785244SRatan Gupta crow::connections::systemBus->async_method_call( 402271584abSEd Tanous [asyncResp, serverType, localRole, 40306785244SRatan Gupta remoteGroup](const boost::system::error_code ec) { 40406785244SRatan Gupta if (ec) 40506785244SRatan Gupta { 40606785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " << ec; 40706785244SRatan Gupta messages::internalError(asyncResp->res); 40806785244SRatan Gupta return; 40906785244SRatan Gupta } 41006785244SRatan Gupta nlohmann::json& remoteRoleJson = 41106785244SRatan Gupta asyncResp->res 41206785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"]; 4131476687dSEd Tanous nlohmann::json::object_t roleMapEntry; 4141476687dSEd Tanous roleMapEntry["LocalRole"] = *localRole; 4151476687dSEd Tanous roleMapEntry["RemoteGroup"] = *remoteGroup; 4161476687dSEd Tanous remoteRoleJson.push_back(std::move(roleMapEntry)); 41706785244SRatan Gupta }, 41806785244SRatan Gupta ldapDbusService, dbusObjectPath, ldapPrivMapperInterface, 4193174e4dfSEd Tanous "Create", *remoteGroup, 42006785244SRatan Gupta getPrivilegeFromRoleId(std::move(*localRole))); 42106785244SRatan Gupta } 42206785244SRatan Gupta } 42306785244SRatan Gupta } 42406785244SRatan Gupta } 42506785244SRatan Gupta 42606785244SRatan Gupta /** 4276973a582SRatan Gupta * Function that retrieves all properties for LDAP config object 4286973a582SRatan Gupta * into JSON 4296973a582SRatan Gupta */ 4306973a582SRatan Gupta template <typename CallbackFunc> 4316973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType, 4326973a582SRatan Gupta CallbackFunc&& callback) 4336973a582SRatan Gupta { 43454fc587aSNagaraju Goruganti 43554fc587aSNagaraju Goruganti const std::array<const char*, 2> interfaces = {ldapEnableInterface, 43654fc587aSNagaraju Goruganti ldapConfigInterface}; 43754fc587aSNagaraju Goruganti 43854fc587aSNagaraju Goruganti crow::connections::systemBus->async_method_call( 43954fc587aSNagaraju Goruganti [callback, ldapType](const boost::system::error_code ec, 440b9d36b47SEd Tanous const dbus::utility::MapperGetObject& resp) { 44154fc587aSNagaraju Goruganti if (ec || resp.empty()) 44254fc587aSNagaraju Goruganti { 4430fda0f12SGeorge Liu BMCWEB_LOG_ERROR 444002d39b4SEd Tanous << "DBUS response error during getting of service name: " << ec; 44523a21a1cSEd Tanous LDAPConfigData empty{}; 44623a21a1cSEd Tanous callback(false, empty, ldapType); 44754fc587aSNagaraju Goruganti return; 44854fc587aSNagaraju Goruganti } 44954fc587aSNagaraju Goruganti std::string service = resp.begin()->first; 45054fc587aSNagaraju Goruganti crow::connections::systemBus->async_method_call( 451002d39b4SEd Tanous [callback, 452002d39b4SEd Tanous ldapType](const boost::system::error_code errorCode, 453711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& ldapObjects) { 4546973a582SRatan Gupta LDAPConfigData confData{}; 45581ce609eSEd Tanous if (errorCode) 4566973a582SRatan Gupta { 457ab828d7cSRatan Gupta callback(false, confData, ldapType); 458002d39b4SEd Tanous BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode; 4596973a582SRatan Gupta return; 4606973a582SRatan Gupta } 461ab828d7cSRatan Gupta 462ab828d7cSRatan Gupta std::string ldapDbusType; 46354fc587aSNagaraju Goruganti std::string searchString; 46454fc587aSNagaraju Goruganti 465ab828d7cSRatan Gupta if (ldapType == "LDAP") 466ab828d7cSRatan Gupta { 4670fda0f12SGeorge Liu ldapDbusType = 4680fda0f12SGeorge Liu "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap"; 46954fc587aSNagaraju Goruganti searchString = "openldap"; 470ab828d7cSRatan Gupta } 471ab828d7cSRatan Gupta else if (ldapType == "ActiveDirectory") 472ab828d7cSRatan Gupta { 47354fc587aSNagaraju Goruganti ldapDbusType = 4740fda0f12SGeorge Liu "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory"; 47554fc587aSNagaraju Goruganti searchString = "active_directory"; 476ab828d7cSRatan Gupta } 477ab828d7cSRatan Gupta else 478ab828d7cSRatan Gupta { 479002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type=" 480ab828d7cSRatan Gupta << ldapType; 481ab828d7cSRatan Gupta callback(false, confData, ldapType); 482ab828d7cSRatan Gupta return; 483ab828d7cSRatan Gupta } 484ab828d7cSRatan Gupta 485ab828d7cSRatan Gupta std::string ldapEnableInterfaceStr = ldapEnableInterface; 486ab828d7cSRatan Gupta std::string ldapConfigInterfaceStr = ldapConfigInterface; 487ab828d7cSRatan Gupta 4886973a582SRatan Gupta for (const auto& object : ldapObjects) 4896973a582SRatan Gupta { 49054fc587aSNagaraju Goruganti // let's find the object whose ldap type is equal to the 49154fc587aSNagaraju Goruganti // given type 492002d39b4SEd Tanous if (object.first.str.find(searchString) == std::string::npos) 4936973a582SRatan Gupta { 494ab828d7cSRatan Gupta continue; 495ab828d7cSRatan Gupta } 496ab828d7cSRatan Gupta 4976973a582SRatan Gupta for (const auto& interface : object.second) 4986973a582SRatan Gupta { 4996973a582SRatan Gupta if (interface.first == ldapEnableInterfaceStr) 5006973a582SRatan Gupta { 5016973a582SRatan Gupta // rest of the properties are string. 5026973a582SRatan Gupta for (const auto& property : interface.second) 5036973a582SRatan Gupta { 5046973a582SRatan Gupta if (property.first == "Enabled") 5056973a582SRatan Gupta { 5066973a582SRatan Gupta const bool* value = 5076973a582SRatan Gupta std::get_if<bool>(&property.second); 5086973a582SRatan Gupta if (value == nullptr) 5096973a582SRatan Gupta { 5106973a582SRatan Gupta continue; 5116973a582SRatan Gupta } 5126973a582SRatan Gupta confData.serviceEnabled = *value; 5136973a582SRatan Gupta break; 5146973a582SRatan Gupta } 5156973a582SRatan Gupta } 5166973a582SRatan Gupta } 5176973a582SRatan Gupta else if (interface.first == ldapConfigInterfaceStr) 5186973a582SRatan Gupta { 5196973a582SRatan Gupta 5206973a582SRatan Gupta for (const auto& property : interface.second) 5216973a582SRatan Gupta { 522271584abSEd Tanous const std::string* strValue = 523002d39b4SEd Tanous std::get_if<std::string>(&property.second); 524271584abSEd Tanous if (strValue == nullptr) 5256973a582SRatan Gupta { 5266973a582SRatan Gupta continue; 5276973a582SRatan Gupta } 5286973a582SRatan Gupta if (property.first == "LDAPServerURI") 5296973a582SRatan Gupta { 530271584abSEd Tanous confData.uri = *strValue; 5316973a582SRatan Gupta } 5326973a582SRatan Gupta else if (property.first == "LDAPBindDN") 5336973a582SRatan Gupta { 534271584abSEd Tanous confData.bindDN = *strValue; 5356973a582SRatan Gupta } 5366973a582SRatan Gupta else if (property.first == "LDAPBaseDN") 5376973a582SRatan Gupta { 538271584abSEd Tanous confData.baseDN = *strValue; 5396973a582SRatan Gupta } 540002d39b4SEd Tanous else if (property.first == "LDAPSearchScope") 5416973a582SRatan Gupta { 542271584abSEd Tanous confData.searchScope = *strValue; 5436973a582SRatan Gupta } 544002d39b4SEd Tanous else if (property.first == "GroupNameAttribute") 5456973a582SRatan Gupta { 546271584abSEd Tanous confData.groupAttribute = *strValue; 5476973a582SRatan Gupta } 548002d39b4SEd Tanous else if (property.first == "UserNameAttribute") 5496973a582SRatan Gupta { 550271584abSEd Tanous confData.userNameAttribute = *strValue; 5516973a582SRatan Gupta } 55254fc587aSNagaraju Goruganti else if (property.first == "LDAPType") 553ab828d7cSRatan Gupta { 554271584abSEd Tanous confData.serverType = *strValue; 55554fc587aSNagaraju Goruganti } 55654fc587aSNagaraju Goruganti } 55754fc587aSNagaraju Goruganti } 558002d39b4SEd Tanous else if (interface.first == 5590fda0f12SGeorge Liu "xyz.openbmc_project.User.PrivilegeMapperEntry") 56054fc587aSNagaraju Goruganti { 56154fc587aSNagaraju Goruganti LDAPRoleMapData roleMapData{}; 56254fc587aSNagaraju Goruganti for (const auto& property : interface.second) 56354fc587aSNagaraju Goruganti { 564271584abSEd Tanous const std::string* strValue = 565002d39b4SEd Tanous std::get_if<std::string>(&property.second); 56654fc587aSNagaraju Goruganti 567271584abSEd Tanous if (strValue == nullptr) 56854fc587aSNagaraju Goruganti { 56954fc587aSNagaraju Goruganti continue; 57054fc587aSNagaraju Goruganti } 57154fc587aSNagaraju Goruganti 57254fc587aSNagaraju Goruganti if (property.first == "GroupName") 57354fc587aSNagaraju Goruganti { 574271584abSEd Tanous roleMapData.groupName = *strValue; 57554fc587aSNagaraju Goruganti } 57654fc587aSNagaraju Goruganti else if (property.first == "Privilege") 57754fc587aSNagaraju Goruganti { 578271584abSEd Tanous roleMapData.privilege = *strValue; 57954fc587aSNagaraju Goruganti } 58054fc587aSNagaraju Goruganti } 58154fc587aSNagaraju Goruganti 582002d39b4SEd Tanous confData.groupRoleList.emplace_back(object.first.str, 583002d39b4SEd Tanous roleMapData); 58454fc587aSNagaraju Goruganti } 58554fc587aSNagaraju Goruganti } 58654fc587aSNagaraju Goruganti } 587ab828d7cSRatan Gupta callback(true, confData, ldapType); 58854fc587aSNagaraju Goruganti }, 589002d39b4SEd Tanous service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects"); 59054fc587aSNagaraju Goruganti }, 59154fc587aSNagaraju Goruganti mapperBusName, mapperObjectPath, mapperIntf, "GetObject", 59223a21a1cSEd Tanous ldapConfigObjectName, interfaces); 5936973a582SRatan Gupta } 5946973a582SRatan Gupta 5958a07d286SRatan Gupta /** 5968a07d286SRatan Gupta * @brief parses the authentication section under the LDAP 5978a07d286SRatan Gupta * @param input JSON data 5988a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 5998a07d286SRatan Gupta * @param userName userName to be filled from the given JSON. 6008a07d286SRatan Gupta * @param password password to be filled from the given JSON. 6018a07d286SRatan Gupta */ 6024f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson( 6036c51eab1SEd Tanous nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6046c51eab1SEd Tanous std::optional<std::string>& username, std::optional<std::string>& password) 6058a07d286SRatan Gupta { 6068a07d286SRatan Gupta std::optional<std::string> authType; 6078a07d286SRatan Gupta 6088a07d286SRatan Gupta if (!json_util::readJson(input, asyncResp->res, "AuthenticationType", 6098a07d286SRatan Gupta authType, "Username", username, "Password", 6108a07d286SRatan Gupta password)) 6118a07d286SRatan Gupta { 6128a07d286SRatan Gupta return; 6138a07d286SRatan Gupta } 6148a07d286SRatan Gupta if (!authType) 6158a07d286SRatan Gupta { 6168a07d286SRatan Gupta return; 6178a07d286SRatan Gupta } 6188a07d286SRatan Gupta if (*authType != "UsernameAndPassword") 6198a07d286SRatan Gupta { 6208a07d286SRatan Gupta messages::propertyValueNotInList(asyncResp->res, *authType, 6218a07d286SRatan Gupta "AuthenticationType"); 6228a07d286SRatan Gupta return; 6238a07d286SRatan Gupta } 6248a07d286SRatan Gupta } 6258a07d286SRatan Gupta /** 6268a07d286SRatan Gupta * @brief parses the LDAPService section under the LDAP 6278a07d286SRatan Gupta * @param input JSON data 6288a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 6298a07d286SRatan Gupta * @param baseDNList baseDN to be filled from the given JSON. 6308a07d286SRatan Gupta * @param userNameAttribute userName to be filled from the given JSON. 6318a07d286SRatan Gupta * @param groupaAttribute password to be filled from the given JSON. 6328a07d286SRatan Gupta */ 6338a07d286SRatan Gupta 6344f48d5f6SEd Tanous inline void 6354f48d5f6SEd Tanous parseLDAPServiceJson(nlohmann::json input, 6368d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6378a07d286SRatan Gupta std::optional<std::vector<std::string>>& baseDNList, 6388a07d286SRatan Gupta std::optional<std::string>& userNameAttribute, 6398a07d286SRatan Gupta std::optional<std::string>& groupsAttribute) 6408a07d286SRatan Gupta { 6418a07d286SRatan Gupta std::optional<nlohmann::json> searchSettings; 6428a07d286SRatan Gupta 6438a07d286SRatan Gupta if (!json_util::readJson(input, asyncResp->res, "SearchSettings", 6448a07d286SRatan Gupta searchSettings)) 6458a07d286SRatan Gupta { 6468a07d286SRatan Gupta return; 6478a07d286SRatan Gupta } 6488a07d286SRatan Gupta if (!searchSettings) 6498a07d286SRatan Gupta { 6508a07d286SRatan Gupta return; 6518a07d286SRatan Gupta } 6528a07d286SRatan Gupta if (!json_util::readJson(*searchSettings, asyncResp->res, 6538a07d286SRatan Gupta "BaseDistinguishedNames", baseDNList, 6548a07d286SRatan Gupta "UsernameAttribute", userNameAttribute, 6558a07d286SRatan Gupta "GroupsAttribute", groupsAttribute)) 6568a07d286SRatan Gupta { 6578a07d286SRatan Gupta return; 6588a07d286SRatan Gupta } 6598a07d286SRatan Gupta } 6608a07d286SRatan Gupta /** 6618a07d286SRatan Gupta * @brief updates the LDAP server address and updates the 6628a07d286SRatan Gupta json response with the new value. 6638a07d286SRatan Gupta * @param serviceAddressList address to be updated. 6648a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 6658a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 6668a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 6678a07d286SRatan Gupta */ 6688a07d286SRatan Gupta 6694f48d5f6SEd Tanous inline void handleServiceAddressPatch( 6708a07d286SRatan Gupta const std::vector<std::string>& serviceAddressList, 6718d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6728a07d286SRatan Gupta const std::string& ldapServerElementName, 6738a07d286SRatan Gupta const std::string& ldapConfigObject) 6748a07d286SRatan Gupta { 6758a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 6768a07d286SRatan Gupta [asyncResp, ldapServerElementName, 6778a07d286SRatan Gupta serviceAddressList](const boost::system::error_code ec) { 6788a07d286SRatan Gupta if (ec) 6798a07d286SRatan Gupta { 6808a07d286SRatan Gupta BMCWEB_LOG_DEBUG 681c61704abSGunnar Mills << "Error Occurred in updating the service address"; 6828a07d286SRatan Gupta messages::internalError(asyncResp->res); 6838a07d286SRatan Gupta return; 6848a07d286SRatan Gupta } 6858a07d286SRatan Gupta std::vector<std::string> modifiedserviceAddressList = { 6868a07d286SRatan Gupta serviceAddressList.front()}; 687002d39b4SEd Tanous asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] = 6888a07d286SRatan Gupta modifiedserviceAddressList; 6898a07d286SRatan Gupta if ((serviceAddressList).size() > 1) 6908a07d286SRatan Gupta { 691002d39b4SEd Tanous messages::propertyValueModified(asyncResp->res, "ServiceAddresses", 6928a07d286SRatan Gupta serviceAddressList.front()); 6938a07d286SRatan Gupta } 6948a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the service address"; 6958a07d286SRatan Gupta }, 6968a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 6978a07d286SRatan Gupta ldapConfigInterface, "LDAPServerURI", 698168e20c1SEd Tanous dbus::utility::DbusVariantType(serviceAddressList.front())); 6998a07d286SRatan Gupta } 7008a07d286SRatan Gupta /** 7018a07d286SRatan Gupta * @brief updates the LDAP Bind DN and updates the 7028a07d286SRatan Gupta json response with the new value. 7038a07d286SRatan Gupta * @param username name of the user which needs to be updated. 7048a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 7058a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 7068a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 7078a07d286SRatan Gupta */ 7088a07d286SRatan Gupta 7094f48d5f6SEd Tanous inline void 7104f48d5f6SEd Tanous handleUserNamePatch(const std::string& username, 7118d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 7128a07d286SRatan Gupta const std::string& ldapServerElementName, 7138a07d286SRatan Gupta const std::string& ldapConfigObject) 7148a07d286SRatan Gupta { 7158a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 7168a07d286SRatan Gupta [asyncResp, username, 7178a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 7188a07d286SRatan Gupta if (ec) 7198a07d286SRatan Gupta { 7206c51eab1SEd Tanous BMCWEB_LOG_DEBUG << "Error occurred in updating the username"; 7218a07d286SRatan Gupta messages::internalError(asyncResp->res); 7228a07d286SRatan Gupta return; 7238a07d286SRatan Gupta } 724002d39b4SEd Tanous asyncResp->res 725002d39b4SEd Tanous .jsonValue[ldapServerElementName]["Authentication"]["Username"] = 726002d39b4SEd Tanous username; 7278a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the username"; 7288a07d286SRatan Gupta }, 7298a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 730168e20c1SEd Tanous ldapConfigInterface, "LDAPBindDN", 731168e20c1SEd Tanous dbus::utility::DbusVariantType(username)); 7328a07d286SRatan Gupta } 7338a07d286SRatan Gupta 7348a07d286SRatan Gupta /** 7358a07d286SRatan Gupta * @brief updates the LDAP password 7368a07d286SRatan Gupta * @param password : ldap password which needs to be updated. 7378a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 7388a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 7398a07d286SRatan Gupta * server(openLDAP/ActiveDirectory) 7408a07d286SRatan Gupta */ 7418a07d286SRatan Gupta 7424f48d5f6SEd Tanous inline void 7434f48d5f6SEd Tanous handlePasswordPatch(const std::string& password, 7448d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 7458a07d286SRatan Gupta const std::string& ldapServerElementName, 7468a07d286SRatan Gupta const std::string& ldapConfigObject) 7478a07d286SRatan Gupta { 7488a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 7498a07d286SRatan Gupta [asyncResp, password, 7508a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 7518a07d286SRatan Gupta if (ec) 7528a07d286SRatan Gupta { 7536c51eab1SEd Tanous BMCWEB_LOG_DEBUG << "Error occurred in updating the password"; 7548a07d286SRatan Gupta messages::internalError(asyncResp->res); 7558a07d286SRatan Gupta return; 7568a07d286SRatan Gupta } 757002d39b4SEd Tanous asyncResp->res 758002d39b4SEd Tanous .jsonValue[ldapServerElementName]["Authentication"]["Password"] = 759002d39b4SEd Tanous ""; 7608a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the password"; 7618a07d286SRatan Gupta }, 7628a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 7638a07d286SRatan Gupta ldapConfigInterface, "LDAPBindDNPassword", 764168e20c1SEd Tanous dbus::utility::DbusVariantType(password)); 7658a07d286SRatan Gupta } 7668a07d286SRatan Gupta 7678a07d286SRatan Gupta /** 7688a07d286SRatan Gupta * @brief updates the LDAP BaseDN and updates the 7698a07d286SRatan Gupta json response with the new value. 7708a07d286SRatan Gupta * @param baseDNList baseDN list which needs to be updated. 7718a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 7728a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 7738a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 7748a07d286SRatan Gupta */ 7758a07d286SRatan Gupta 7764f48d5f6SEd Tanous inline void 7774f48d5f6SEd Tanous handleBaseDNPatch(const std::vector<std::string>& baseDNList, 7788d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 7798a07d286SRatan Gupta const std::string& ldapServerElementName, 7808a07d286SRatan Gupta const std::string& ldapConfigObject) 7818a07d286SRatan Gupta { 7828a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 7838a07d286SRatan Gupta [asyncResp, baseDNList, 7848a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 7858a07d286SRatan Gupta if (ec) 7868a07d286SRatan Gupta { 7876c51eab1SEd Tanous BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN"; 7888a07d286SRatan Gupta messages::internalError(asyncResp->res); 7898a07d286SRatan Gupta return; 7908a07d286SRatan Gupta } 791002d39b4SEd Tanous auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName]; 7928a07d286SRatan Gupta auto& searchSettingsJson = 7938a07d286SRatan Gupta serverTypeJson["LDAPService"]["SearchSettings"]; 7946c51eab1SEd Tanous std::vector<std::string> modifiedBaseDNList = {baseDNList.front()}; 7956c51eab1SEd Tanous searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList; 7968a07d286SRatan Gupta if (baseDNList.size() > 1) 7978a07d286SRatan Gupta { 798002d39b4SEd Tanous messages::propertyValueModified( 799002d39b4SEd Tanous asyncResp->res, "BaseDistinguishedNames", baseDNList.front()); 8008a07d286SRatan Gupta } 8018a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the base DN"; 8028a07d286SRatan Gupta }, 8038a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 8048a07d286SRatan Gupta ldapConfigInterface, "LDAPBaseDN", 805168e20c1SEd Tanous dbus::utility::DbusVariantType(baseDNList.front())); 8068a07d286SRatan Gupta } 8078a07d286SRatan Gupta /** 8088a07d286SRatan Gupta * @brief updates the LDAP user name attribute and updates the 8098a07d286SRatan Gupta json response with the new value. 8108a07d286SRatan Gupta * @param userNameAttribute attribute to be updated. 8118a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 8128a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 8138a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 8148a07d286SRatan Gupta */ 8158a07d286SRatan Gupta 8164f48d5f6SEd Tanous inline void 8174f48d5f6SEd Tanous handleUserNameAttrPatch(const std::string& userNameAttribute, 8188d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8198a07d286SRatan Gupta const std::string& ldapServerElementName, 8208a07d286SRatan Gupta const std::string& ldapConfigObject) 8218a07d286SRatan Gupta { 8228a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 8238a07d286SRatan Gupta [asyncResp, userNameAttribute, 8248a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 8258a07d286SRatan Gupta if (ec) 8268a07d286SRatan Gupta { 827c61704abSGunnar Mills BMCWEB_LOG_DEBUG << "Error Occurred in Updating the " 8288a07d286SRatan Gupta "username attribute"; 8298a07d286SRatan Gupta messages::internalError(asyncResp->res); 8308a07d286SRatan Gupta return; 8318a07d286SRatan Gupta } 832002d39b4SEd Tanous auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName]; 8338a07d286SRatan Gupta auto& searchSettingsJson = 8348a07d286SRatan Gupta serverTypeJson["LDAPService"]["SearchSettings"]; 8358a07d286SRatan Gupta searchSettingsJson["UsernameAttribute"] = userNameAttribute; 8368a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the user name attr."; 8378a07d286SRatan Gupta }, 8388a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 8398a07d286SRatan Gupta ldapConfigInterface, "UserNameAttribute", 840168e20c1SEd Tanous dbus::utility::DbusVariantType(userNameAttribute)); 8418a07d286SRatan Gupta } 8428a07d286SRatan Gupta /** 8438a07d286SRatan Gupta * @brief updates the LDAP group attribute and updates the 8448a07d286SRatan Gupta json response with the new value. 8458a07d286SRatan Gupta * @param groupsAttribute attribute to be updated. 8468a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 8478a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 8488a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 8498a07d286SRatan Gupta */ 8508a07d286SRatan Gupta 8514f48d5f6SEd Tanous inline void handleGroupNameAttrPatch( 8528d1b46d7Szhanghch05 const std::string& groupsAttribute, 8538d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8548a07d286SRatan Gupta const std::string& ldapServerElementName, 8558a07d286SRatan Gupta const std::string& ldapConfigObject) 8568a07d286SRatan Gupta { 8578a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 8588a07d286SRatan Gupta [asyncResp, groupsAttribute, 8598a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 8608a07d286SRatan Gupta if (ec) 8618a07d286SRatan Gupta { 862c61704abSGunnar Mills BMCWEB_LOG_DEBUG << "Error Occurred in Updating the " 8638a07d286SRatan Gupta "groupname attribute"; 8648a07d286SRatan Gupta messages::internalError(asyncResp->res); 8658a07d286SRatan Gupta return; 8668a07d286SRatan Gupta } 867002d39b4SEd Tanous auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName]; 8688a07d286SRatan Gupta auto& searchSettingsJson = 8698a07d286SRatan Gupta serverTypeJson["LDAPService"]["SearchSettings"]; 8708a07d286SRatan Gupta searchSettingsJson["GroupsAttribute"] = groupsAttribute; 8718a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the groupname attr"; 8728a07d286SRatan Gupta }, 8738a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 8748a07d286SRatan Gupta ldapConfigInterface, "GroupNameAttribute", 875168e20c1SEd Tanous dbus::utility::DbusVariantType(groupsAttribute)); 8768a07d286SRatan Gupta } 8778a07d286SRatan Gupta /** 8788a07d286SRatan Gupta * @brief updates the LDAP service enable and updates the 8798a07d286SRatan Gupta json response with the new value. 8808a07d286SRatan Gupta * @param input JSON data. 8818a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 8828a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 8838a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 8848a07d286SRatan Gupta */ 8858a07d286SRatan Gupta 8864f48d5f6SEd Tanous inline void handleServiceEnablePatch( 8876c51eab1SEd Tanous bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8888a07d286SRatan Gupta const std::string& ldapServerElementName, 8898a07d286SRatan Gupta const std::string& ldapConfigObject) 8908a07d286SRatan Gupta { 8918a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 8928a07d286SRatan Gupta [asyncResp, serviceEnabled, 8938a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 8948a07d286SRatan Gupta if (ec) 8958a07d286SRatan Gupta { 896002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable"; 8978a07d286SRatan Gupta messages::internalError(asyncResp->res); 8988a07d286SRatan Gupta return; 8998a07d286SRatan Gupta } 9006c51eab1SEd Tanous asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] = 9018a07d286SRatan Gupta serviceEnabled; 9026c51eab1SEd Tanous BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled; 9038a07d286SRatan Gupta }, 9048a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 905168e20c1SEd Tanous ldapEnableInterface, "Enabled", 906168e20c1SEd Tanous dbus::utility::DbusVariantType(serviceEnabled)); 9078a07d286SRatan Gupta } 9088a07d286SRatan Gupta 9094f48d5f6SEd Tanous inline void 9104f48d5f6SEd Tanous handleAuthMethodsPatch(nlohmann::json& input, 9118d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 91278158631SZbigniew Kurzynski { 91378158631SZbigniew Kurzynski std::optional<bool> basicAuth; 91478158631SZbigniew Kurzynski std::optional<bool> cookie; 91578158631SZbigniew Kurzynski std::optional<bool> sessionToken; 91678158631SZbigniew Kurzynski std::optional<bool> xToken; 917501f1e58SZbigniew Kurzynski std::optional<bool> tls; 91878158631SZbigniew Kurzynski 91978158631SZbigniew Kurzynski if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth, 92078158631SZbigniew Kurzynski "Cookie", cookie, "SessionToken", sessionToken, 921501f1e58SZbigniew Kurzynski "XToken", xToken, "TLS", tls)) 92278158631SZbigniew Kurzynski { 92378158631SZbigniew Kurzynski BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag"; 92478158631SZbigniew Kurzynski return; 92578158631SZbigniew Kurzynski } 92678158631SZbigniew Kurzynski 92778158631SZbigniew Kurzynski // Make a copy of methods configuration 92852cc112dSEd Tanous persistent_data::AuthConfigMethods authMethodsConfig = 92952cc112dSEd Tanous persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); 93078158631SZbigniew Kurzynski 93178158631SZbigniew Kurzynski if (basicAuth) 93278158631SZbigniew Kurzynski { 933f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION 934f16f6263SAlan Kuo messages::actionNotSupported( 9350fda0f12SGeorge Liu asyncResp->res, 9360fda0f12SGeorge Liu "Setting BasicAuth when basic-auth feature is disabled"); 937f16f6263SAlan Kuo return; 938f16f6263SAlan Kuo #endif 93978158631SZbigniew Kurzynski authMethodsConfig.basic = *basicAuth; 94078158631SZbigniew Kurzynski } 94178158631SZbigniew Kurzynski 94278158631SZbigniew Kurzynski if (cookie) 94378158631SZbigniew Kurzynski { 944f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION 9450fda0f12SGeorge Liu messages::actionNotSupported( 9460fda0f12SGeorge Liu asyncResp->res, 9470fda0f12SGeorge Liu "Setting Cookie when cookie-auth feature is disabled"); 948f16f6263SAlan Kuo return; 949f16f6263SAlan Kuo #endif 95078158631SZbigniew Kurzynski authMethodsConfig.cookie = *cookie; 95178158631SZbigniew Kurzynski } 95278158631SZbigniew Kurzynski 95378158631SZbigniew Kurzynski if (sessionToken) 95478158631SZbigniew Kurzynski { 955f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION 956f16f6263SAlan Kuo messages::actionNotSupported( 9570fda0f12SGeorge Liu asyncResp->res, 9580fda0f12SGeorge Liu "Setting SessionToken when session-auth feature is disabled"); 959f16f6263SAlan Kuo return; 960f16f6263SAlan Kuo #endif 96178158631SZbigniew Kurzynski authMethodsConfig.sessionToken = *sessionToken; 96278158631SZbigniew Kurzynski } 96378158631SZbigniew Kurzynski 96478158631SZbigniew Kurzynski if (xToken) 96578158631SZbigniew Kurzynski { 966f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION 9670fda0f12SGeorge Liu messages::actionNotSupported( 9680fda0f12SGeorge Liu asyncResp->res, 9690fda0f12SGeorge Liu "Setting XToken when xtoken-auth feature is disabled"); 970f16f6263SAlan Kuo return; 971f16f6263SAlan Kuo #endif 97278158631SZbigniew Kurzynski authMethodsConfig.xtoken = *xToken; 97378158631SZbigniew Kurzynski } 97478158631SZbigniew Kurzynski 975501f1e58SZbigniew Kurzynski if (tls) 976501f1e58SZbigniew Kurzynski { 977f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION 9780fda0f12SGeorge Liu messages::actionNotSupported( 9790fda0f12SGeorge Liu asyncResp->res, 9800fda0f12SGeorge Liu "Setting TLS when mutual-tls-auth feature is disabled"); 981f16f6263SAlan Kuo return; 982f16f6263SAlan Kuo #endif 983501f1e58SZbigniew Kurzynski authMethodsConfig.tls = *tls; 984501f1e58SZbigniew Kurzynski } 985501f1e58SZbigniew Kurzynski 98678158631SZbigniew Kurzynski if (!authMethodsConfig.basic && !authMethodsConfig.cookie && 987501f1e58SZbigniew Kurzynski !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken && 988501f1e58SZbigniew Kurzynski !authMethodsConfig.tls) 98978158631SZbigniew Kurzynski { 99078158631SZbigniew Kurzynski // Do not allow user to disable everything 99178158631SZbigniew Kurzynski messages::actionNotSupported(asyncResp->res, 99278158631SZbigniew Kurzynski "of disabling all available methods"); 99378158631SZbigniew Kurzynski return; 99478158631SZbigniew Kurzynski } 99578158631SZbigniew Kurzynski 99652cc112dSEd Tanous persistent_data::SessionStore::getInstance().updateAuthMethodsConfig( 99752cc112dSEd Tanous authMethodsConfig); 99878158631SZbigniew Kurzynski // Save configuration immediately 99952cc112dSEd Tanous persistent_data::getConfig().writeData(); 100078158631SZbigniew Kurzynski 100178158631SZbigniew Kurzynski messages::success(asyncResp->res); 100278158631SZbigniew Kurzynski } 100378158631SZbigniew Kurzynski 10048a07d286SRatan Gupta /** 10058a07d286SRatan Gupta * @brief Get the required values from the given JSON, validates the 10068a07d286SRatan Gupta * value and create the LDAP config object. 10078a07d286SRatan Gupta * @param input JSON data 10088a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 10098a07d286SRatan Gupta * @param serverType Type of LDAP server(openLDAP/ActiveDirectory) 10108a07d286SRatan Gupta */ 10118a07d286SRatan Gupta 10126c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input, 10138d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 10148a07d286SRatan Gupta const std::string& serverType) 10158a07d286SRatan Gupta { 1016eb2bbe56SRatan Gupta std::string dbusObjectPath; 1017eb2bbe56SRatan Gupta if (serverType == "ActiveDirectory") 1018eb2bbe56SRatan Gupta { 10192c70f800SEd Tanous dbusObjectPath = adConfigObject; 1020eb2bbe56SRatan Gupta } 1021eb2bbe56SRatan Gupta else if (serverType == "LDAP") 1022eb2bbe56SRatan Gupta { 102323a21a1cSEd Tanous dbusObjectPath = ldapConfigObjectName; 1024eb2bbe56SRatan Gupta } 1025cb13a392SEd Tanous else 1026cb13a392SEd Tanous { 1027cb13a392SEd Tanous return; 1028cb13a392SEd Tanous } 1029eb2bbe56SRatan Gupta 10308a07d286SRatan Gupta std::optional<nlohmann::json> authentication; 10318a07d286SRatan Gupta std::optional<nlohmann::json> ldapService; 10328a07d286SRatan Gupta std::optional<std::vector<std::string>> serviceAddressList; 10338a07d286SRatan Gupta std::optional<bool> serviceEnabled; 10348a07d286SRatan Gupta std::optional<std::vector<std::string>> baseDNList; 10358a07d286SRatan Gupta std::optional<std::string> userNameAttribute; 10368a07d286SRatan Gupta std::optional<std::string> groupsAttribute; 10378a07d286SRatan Gupta std::optional<std::string> userName; 10388a07d286SRatan Gupta std::optional<std::string> password; 103906785244SRatan Gupta std::optional<std::vector<nlohmann::json>> remoteRoleMapData; 10408a07d286SRatan Gupta 10418a07d286SRatan Gupta if (!json_util::readJson(input, asyncResp->res, "Authentication", 10428a07d286SRatan Gupta authentication, "LDAPService", ldapService, 10438a07d286SRatan Gupta "ServiceAddresses", serviceAddressList, 104406785244SRatan Gupta "ServiceEnabled", serviceEnabled, 104506785244SRatan Gupta "RemoteRoleMapping", remoteRoleMapData)) 10468a07d286SRatan Gupta { 10478a07d286SRatan Gupta return; 10488a07d286SRatan Gupta } 10498a07d286SRatan Gupta 10508a07d286SRatan Gupta if (authentication) 10518a07d286SRatan Gupta { 10528a07d286SRatan Gupta parseLDAPAuthenticationJson(*authentication, asyncResp, userName, 10538a07d286SRatan Gupta password); 10548a07d286SRatan Gupta } 10558a07d286SRatan Gupta if (ldapService) 10568a07d286SRatan Gupta { 10578a07d286SRatan Gupta parseLDAPServiceJson(*ldapService, asyncResp, baseDNList, 10588a07d286SRatan Gupta userNameAttribute, groupsAttribute); 10598a07d286SRatan Gupta } 10608a07d286SRatan Gupta if (serviceAddressList) 10618a07d286SRatan Gupta { 106226f6976fSEd Tanous if (serviceAddressList->empty()) 10638a07d286SRatan Gupta { 10648a07d286SRatan Gupta messages::propertyValueNotInList(asyncResp->res, "[]", 10658a07d286SRatan Gupta "ServiceAddress"); 10668a07d286SRatan Gupta return; 10678a07d286SRatan Gupta } 10688a07d286SRatan Gupta } 10698a07d286SRatan Gupta if (baseDNList) 10708a07d286SRatan Gupta { 107126f6976fSEd Tanous if (baseDNList->empty()) 10728a07d286SRatan Gupta { 10738a07d286SRatan Gupta messages::propertyValueNotInList(asyncResp->res, "[]", 10748a07d286SRatan Gupta "BaseDistinguishedNames"); 10758a07d286SRatan Gupta return; 10768a07d286SRatan Gupta } 10778a07d286SRatan Gupta } 10788a07d286SRatan Gupta 10798a07d286SRatan Gupta // nothing to update, then return 10808a07d286SRatan Gupta if (!userName && !password && !serviceAddressList && !baseDNList && 108106785244SRatan Gupta !userNameAttribute && !groupsAttribute && !serviceEnabled && 108206785244SRatan Gupta !remoteRoleMapData) 10838a07d286SRatan Gupta { 10848a07d286SRatan Gupta return; 10858a07d286SRatan Gupta } 10868a07d286SRatan Gupta 10878a07d286SRatan Gupta // Get the existing resource first then keep modifying 10888a07d286SRatan Gupta // whenever any property gets updated. 1089002d39b4SEd Tanous getLDAPConfigData( 1090002d39b4SEd Tanous serverType, 1091002d39b4SEd Tanous [asyncResp, userName, password, baseDNList, userNameAttribute, 1092002d39b4SEd Tanous groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath, 1093002d39b4SEd Tanous remoteRoleMapData](bool success, const LDAPConfigData& confData, 109423a21a1cSEd Tanous const std::string& serverT) { 10958a07d286SRatan Gupta if (!success) 10968a07d286SRatan Gupta { 10978a07d286SRatan Gupta messages::internalError(asyncResp->res); 10988a07d286SRatan Gupta return; 10998a07d286SRatan Gupta } 11006c51eab1SEd Tanous parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT); 11018a07d286SRatan Gupta if (confData.serviceEnabled) 11028a07d286SRatan Gupta { 11038a07d286SRatan Gupta // Disable the service first and update the rest of 11048a07d286SRatan Gupta // the properties. 11056c51eab1SEd Tanous handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath); 11068a07d286SRatan Gupta } 11078a07d286SRatan Gupta 11088a07d286SRatan Gupta if (serviceAddressList) 11098a07d286SRatan Gupta { 11106c51eab1SEd Tanous handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT, 11116c51eab1SEd Tanous dbusObjectPath); 11128a07d286SRatan Gupta } 11138a07d286SRatan Gupta if (userName) 11148a07d286SRatan Gupta { 11156c51eab1SEd Tanous handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath); 11168a07d286SRatan Gupta } 11178a07d286SRatan Gupta if (password) 11188a07d286SRatan Gupta { 11196c51eab1SEd Tanous handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath); 11208a07d286SRatan Gupta } 11218a07d286SRatan Gupta 11228a07d286SRatan Gupta if (baseDNList) 11238a07d286SRatan Gupta { 11246c51eab1SEd Tanous handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath); 11258a07d286SRatan Gupta } 11268a07d286SRatan Gupta if (userNameAttribute) 11278a07d286SRatan Gupta { 11286c51eab1SEd Tanous handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT, 11296c51eab1SEd Tanous dbusObjectPath); 11308a07d286SRatan Gupta } 11318a07d286SRatan Gupta if (groupsAttribute) 11328a07d286SRatan Gupta { 11336c51eab1SEd Tanous handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT, 11346c51eab1SEd Tanous dbusObjectPath); 11358a07d286SRatan Gupta } 11368a07d286SRatan Gupta if (serviceEnabled) 11378a07d286SRatan Gupta { 11388a07d286SRatan Gupta // if user has given the value as true then enable 11398a07d286SRatan Gupta // the service. if user has given false then no-op 11408a07d286SRatan Gupta // as service is already stopped. 11418a07d286SRatan Gupta if (*serviceEnabled) 11428a07d286SRatan Gupta { 11436c51eab1SEd Tanous handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT, 11446c51eab1SEd Tanous dbusObjectPath); 11458a07d286SRatan Gupta } 11468a07d286SRatan Gupta } 11478a07d286SRatan Gupta else 11488a07d286SRatan Gupta { 11498a07d286SRatan Gupta // if user has not given the service enabled value 11508a07d286SRatan Gupta // then revert it to the same state as it was 11518a07d286SRatan Gupta // before. 11528a07d286SRatan Gupta handleServiceEnablePatch(confData.serviceEnabled, asyncResp, 115323a21a1cSEd Tanous serverT, dbusObjectPath); 11548a07d286SRatan Gupta } 115506785244SRatan Gupta 115606785244SRatan Gupta if (remoteRoleMapData) 115706785244SRatan Gupta { 11586c51eab1SEd Tanous handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT, 11596c51eab1SEd Tanous *remoteRoleMapData); 116006785244SRatan Gupta } 11618a07d286SRatan Gupta }); 11628a07d286SRatan Gupta } 1163d4b5443fSEd Tanous 11646c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp, 11656c51eab1SEd Tanous const std::string& username, 1166*618c14b4SEd Tanous const std::optional<std::string>& password, 1167*618c14b4SEd Tanous const std::optional<bool>& enabled, 1168*618c14b4SEd Tanous const std::optional<std::string>& roleId, 1169*618c14b4SEd Tanous const std::optional<bool>& locked) 11701abe55efSEd Tanous { 1171b477fd44SP Dheeraj Srujan Kumar sdbusplus::message::object_path tempObjPath(rootUserDbusPath); 1172b477fd44SP Dheeraj Srujan Kumar tempObjPath /= username; 1173b477fd44SP Dheeraj Srujan Kumar std::string dbusObjectPath(tempObjPath); 11746c51eab1SEd Tanous 11756c51eab1SEd Tanous dbus::utility::checkDbusPathExists( 1176*618c14b4SEd Tanous dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled, 1177*618c14b4SEd Tanous locked, asyncResp{std::move(asyncResp)}](int rc) { 1178e662eae8SEd Tanous if (rc <= 0) 11796c51eab1SEd Tanous { 1180d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "ManagerAccount", 11816c51eab1SEd Tanous username); 11826c51eab1SEd Tanous return; 11836c51eab1SEd Tanous } 11846c51eab1SEd Tanous 11856c51eab1SEd Tanous if (password) 11866c51eab1SEd Tanous { 11876c51eab1SEd Tanous int retval = pamUpdatePassword(username, *password); 11886c51eab1SEd Tanous 11896c51eab1SEd Tanous if (retval == PAM_USER_UNKNOWN) 11906c51eab1SEd Tanous { 1191d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "ManagerAccount", 11926c51eab1SEd Tanous username); 11936c51eab1SEd Tanous } 11946c51eab1SEd Tanous else if (retval == PAM_AUTHTOK_ERR) 11956c51eab1SEd Tanous { 11966c51eab1SEd Tanous // If password is invalid 1197*618c14b4SEd Tanous messages::propertyValueFormatError(asyncResp->res, 1198*618c14b4SEd Tanous *password, "Password"); 11996c51eab1SEd Tanous BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; 12006c51eab1SEd Tanous } 12016c51eab1SEd Tanous else if (retval != PAM_SUCCESS) 12026c51eab1SEd Tanous { 12036c51eab1SEd Tanous messages::internalError(asyncResp->res); 12046c51eab1SEd Tanous return; 12056c51eab1SEd Tanous } 1206e7b1b62bSEd Tanous else 1207e7b1b62bSEd Tanous { 1208e7b1b62bSEd Tanous messages::success(asyncResp->res); 1209e7b1b62bSEd Tanous } 12106c51eab1SEd Tanous } 12116c51eab1SEd Tanous 12126c51eab1SEd Tanous if (enabled) 12136c51eab1SEd Tanous { 12146c51eab1SEd Tanous crow::connections::systemBus->async_method_call( 12156c51eab1SEd Tanous [asyncResp](const boost::system::error_code ec) { 12166c51eab1SEd Tanous if (ec) 12176c51eab1SEd Tanous { 12186c51eab1SEd Tanous BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 12196c51eab1SEd Tanous messages::internalError(asyncResp->res); 12206c51eab1SEd Tanous return; 12216c51eab1SEd Tanous } 12226c51eab1SEd Tanous messages::success(asyncResp->res); 12236c51eab1SEd Tanous return; 12246c51eab1SEd Tanous }, 1225e05aec50SEd Tanous "xyz.openbmc_project.User.Manager", dbusObjectPath, 12266c51eab1SEd Tanous "org.freedesktop.DBus.Properties", "Set", 12276c51eab1SEd Tanous "xyz.openbmc_project.User.Attributes", "UserEnabled", 1228168e20c1SEd Tanous dbus::utility::DbusVariantType{*enabled}); 12296c51eab1SEd Tanous } 12306c51eab1SEd Tanous 12316c51eab1SEd Tanous if (roleId) 12326c51eab1SEd Tanous { 12336c51eab1SEd Tanous std::string priv = getPrivilegeFromRoleId(*roleId); 12346c51eab1SEd Tanous if (priv.empty()) 12356c51eab1SEd Tanous { 12366c51eab1SEd Tanous messages::propertyValueNotInList(asyncResp->res, *roleId, 12376c51eab1SEd Tanous "RoleId"); 12386c51eab1SEd Tanous return; 12396c51eab1SEd Tanous } 12406c51eab1SEd Tanous 12416c51eab1SEd Tanous crow::connections::systemBus->async_method_call( 12426c51eab1SEd Tanous [asyncResp](const boost::system::error_code ec) { 12436c51eab1SEd Tanous if (ec) 12446c51eab1SEd Tanous { 12456c51eab1SEd Tanous BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 12466c51eab1SEd Tanous messages::internalError(asyncResp->res); 12476c51eab1SEd Tanous return; 12486c51eab1SEd Tanous } 12496c51eab1SEd Tanous messages::success(asyncResp->res); 12506c51eab1SEd Tanous }, 1251e05aec50SEd Tanous "xyz.openbmc_project.User.Manager", dbusObjectPath, 12526c51eab1SEd Tanous "org.freedesktop.DBus.Properties", "Set", 12536c51eab1SEd Tanous "xyz.openbmc_project.User.Attributes", "UserPrivilege", 1254168e20c1SEd Tanous dbus::utility::DbusVariantType{priv}); 12556c51eab1SEd Tanous } 12566c51eab1SEd Tanous 12576c51eab1SEd Tanous if (locked) 12586c51eab1SEd Tanous { 12596c51eab1SEd Tanous // admin can unlock the account which is locked by 12606c51eab1SEd Tanous // successive authentication failures but admin should 12616c51eab1SEd Tanous // not be allowed to lock an account. 12626c51eab1SEd Tanous if (*locked) 12636c51eab1SEd Tanous { 12646c51eab1SEd Tanous messages::propertyValueNotInList(asyncResp->res, "true", 12656c51eab1SEd Tanous "Locked"); 12666c51eab1SEd Tanous return; 12676c51eab1SEd Tanous } 12686c51eab1SEd Tanous 12696c51eab1SEd Tanous crow::connections::systemBus->async_method_call( 12706c51eab1SEd Tanous [asyncResp](const boost::system::error_code ec) { 12716c51eab1SEd Tanous if (ec) 12726c51eab1SEd Tanous { 12736c51eab1SEd Tanous BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 12746c51eab1SEd Tanous messages::internalError(asyncResp->res); 12756c51eab1SEd Tanous return; 12766c51eab1SEd Tanous } 12776c51eab1SEd Tanous messages::success(asyncResp->res); 12786c51eab1SEd Tanous return; 12796c51eab1SEd Tanous }, 1280e05aec50SEd Tanous "xyz.openbmc_project.User.Manager", dbusObjectPath, 12816c51eab1SEd Tanous "org.freedesktop.DBus.Properties", "Set", 12826c51eab1SEd Tanous "xyz.openbmc_project.User.Attributes", 1283168e20c1SEd Tanous "UserLockedForFailedAttempt", 1284168e20c1SEd Tanous dbus::utility::DbusVariantType{*locked}); 12856c51eab1SEd Tanous } 12866c51eab1SEd Tanous }); 12876c51eab1SEd Tanous } 12886c51eab1SEd Tanous 12894c7d4d33SEd Tanous inline void handleAccountServiceHead( 12904c7d4d33SEd Tanous App& app, const crow::Request& req, 12911ef4c342SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 12926c51eab1SEd Tanous { 12934c7d4d33SEd Tanous 12943ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 129545ca1b86SEd Tanous { 129645ca1b86SEd Tanous return; 129745ca1b86SEd Tanous } 12984c7d4d33SEd Tanous asyncResp->res.addHeader( 12994c7d4d33SEd Tanous boost::beast::http::field::link, 13004c7d4d33SEd Tanous "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby"); 13014c7d4d33SEd Tanous } 13024c7d4d33SEd Tanous 13034c7d4d33SEd Tanous inline void 13044c7d4d33SEd Tanous handleAccountServiceGet(App& app, const crow::Request& req, 13054c7d4d33SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 13064c7d4d33SEd Tanous { 13074c7d4d33SEd Tanous handleAccountServiceHead(app, req, asyncResp); 130852cc112dSEd Tanous const persistent_data::AuthConfigMethods& authMethodsConfig = 13091ef4c342SEd Tanous persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); 131078158631SZbigniew Kurzynski 13111476687dSEd Tanous nlohmann::json& json = asyncResp->res.jsonValue; 13121476687dSEd Tanous json["@odata.id"] = "/redfish/v1/AccountService"; 13131476687dSEd Tanous json["@odata.type"] = "#AccountService." 13141476687dSEd Tanous "v1_10_0.AccountService"; 13151476687dSEd Tanous json["Id"] = "AccountService"; 13161476687dSEd Tanous json["Name"] = "Account Service"; 13171476687dSEd Tanous json["Description"] = "Account Service"; 13181476687dSEd Tanous json["ServiceEnabled"] = true; 13191476687dSEd Tanous json["MaxPasswordLength"] = 20; 13201ef4c342SEd Tanous json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts"; 13211476687dSEd Tanous json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles"; 13221476687dSEd Tanous json["Oem"]["OpenBMC"]["@odata.type"] = 13231476687dSEd Tanous "#OemAccountService.v1_0_0.AccountService"; 13241476687dSEd Tanous json["Oem"]["OpenBMC"]["@odata.id"] = 13251476687dSEd Tanous "/redfish/v1/AccountService#/Oem/OpenBMC"; 13261476687dSEd Tanous json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] = 13271476687dSEd Tanous authMethodsConfig.basic; 13281476687dSEd Tanous json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] = 13291476687dSEd Tanous authMethodsConfig.sessionToken; 13301ef4c342SEd Tanous json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken; 13311ef4c342SEd Tanous json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie; 13321ef4c342SEd Tanous json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls; 13331476687dSEd Tanous 13341ef4c342SEd Tanous // /redfish/v1/AccountService/LDAP/Certificates is something only 13351ef4c342SEd Tanous // ConfigureManager can access then only display when the user has 13361ef4c342SEd Tanous // permissions ConfigureManager 133772048780SAbhishek Patel Privileges effectiveUserPrivileges = 133872048780SAbhishek Patel redfish::getUserPrivileges(req.userRole); 133972048780SAbhishek Patel 134072048780SAbhishek Patel if (isOperationAllowedWithPrivileges({{"ConfigureManager"}}, 134172048780SAbhishek Patel effectiveUserPrivileges)) 134272048780SAbhishek Patel { 13431ef4c342SEd Tanous asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] = 13441476687dSEd Tanous "/redfish/v1/AccountService/LDAP/Certificates"; 134572048780SAbhishek Patel } 1346d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 1347d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", 1348d1bde9e5SKrzysztof Grobelny "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy", 1349002d39b4SEd Tanous [asyncResp](const boost::system::error_code ec, 13501ef4c342SEd Tanous const dbus::utility::DBusPropertiesMap& propertiesList) { 13513d958bbcSAppaRao Puli if (ec) 13523d958bbcSAppaRao Puli { 13533d958bbcSAppaRao Puli messages::internalError(asyncResp->res); 13543d958bbcSAppaRao Puli return; 13553d958bbcSAppaRao Puli } 1356d1bde9e5SKrzysztof Grobelny 13573d958bbcSAppaRao Puli BMCWEB_LOG_DEBUG << "Got " << propertiesList.size() 13583d958bbcSAppaRao Puli << "properties for AccountService"; 1359d1bde9e5SKrzysztof Grobelny 1360d1bde9e5SKrzysztof Grobelny const uint8_t* minPasswordLength = nullptr; 1361d1bde9e5SKrzysztof Grobelny const uint32_t* accountUnlockTimeout = nullptr; 1362d1bde9e5SKrzysztof Grobelny const uint16_t* maxLoginAttemptBeforeLockout = nullptr; 1363d1bde9e5SKrzysztof Grobelny 1364d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 1365d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, 1366d1bde9e5SKrzysztof Grobelny "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout", 1367d1bde9e5SKrzysztof Grobelny accountUnlockTimeout, "MaxLoginAttemptBeforeLockout", 1368d1bde9e5SKrzysztof Grobelny maxLoginAttemptBeforeLockout); 1369d1bde9e5SKrzysztof Grobelny 1370d1bde9e5SKrzysztof Grobelny if (!success) 13713d958bbcSAppaRao Puli { 1372d1bde9e5SKrzysztof Grobelny messages::internalError(asyncResp->res); 1373d1bde9e5SKrzysztof Grobelny return; 13743d958bbcSAppaRao Puli } 1375d1bde9e5SKrzysztof Grobelny 1376d1bde9e5SKrzysztof Grobelny if (minPasswordLength != nullptr) 13773d958bbcSAppaRao Puli { 1378d1bde9e5SKrzysztof Grobelny asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength; 13793d958bbcSAppaRao Puli } 1380d1bde9e5SKrzysztof Grobelny 1381d1bde9e5SKrzysztof Grobelny if (accountUnlockTimeout != nullptr) 13823d958bbcSAppaRao Puli { 1383d1bde9e5SKrzysztof Grobelny asyncResp->res.jsonValue["AccountLockoutDuration"] = 1384d1bde9e5SKrzysztof Grobelny *accountUnlockTimeout; 1385d1bde9e5SKrzysztof Grobelny } 1386d1bde9e5SKrzysztof Grobelny 1387d1bde9e5SKrzysztof Grobelny if (maxLoginAttemptBeforeLockout != nullptr) 13883d958bbcSAppaRao Puli { 1389002d39b4SEd Tanous asyncResp->res.jsonValue["AccountLockoutThreshold"] = 1390d1bde9e5SKrzysztof Grobelny *maxLoginAttemptBeforeLockout; 13913d958bbcSAppaRao Puli } 1392d1bde9e5SKrzysztof Grobelny }); 13936973a582SRatan Gupta 139402cad96eSEd Tanous auto callback = [asyncResp](bool success, const LDAPConfigData& confData, 1395ab828d7cSRatan Gupta const std::string& ldapType) { 1396cb13a392SEd Tanous if (!success) 1397cb13a392SEd Tanous { 1398cb13a392SEd Tanous return; 1399cb13a392SEd Tanous } 1400002d39b4SEd Tanous parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType); 1401ab828d7cSRatan Gupta }; 1402ab828d7cSRatan Gupta 1403ab828d7cSRatan Gupta getLDAPConfigData("LDAP", callback); 1404ab828d7cSRatan Gupta getLDAPConfigData("ActiveDirectory", callback); 14051ef4c342SEd Tanous } 14066973a582SRatan Gupta 14071ef4c342SEd Tanous inline void handleAccountServicePatch( 14081ef4c342SEd Tanous App& app, const crow::Request& req, 14091ef4c342SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 14101ef4c342SEd Tanous { 14113ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 141245ca1b86SEd Tanous { 141345ca1b86SEd Tanous return; 141445ca1b86SEd Tanous } 1415f5ffd806SEd Tanous std::optional<uint32_t> unlockTimeout; 1416f5ffd806SEd Tanous std::optional<uint16_t> lockoutThreshold; 1417ef73ad0dSPaul Fertser std::optional<uint8_t> minPasswordLength; 1418f5ffd806SEd Tanous std::optional<uint16_t> maxPasswordLength; 1419f5ffd806SEd Tanous std::optional<nlohmann::json> ldapObject; 1420f5ffd806SEd Tanous std::optional<nlohmann::json> activeDirectoryObject; 1421f5ffd806SEd Tanous std::optional<nlohmann::json> oemObject; 1422f5ffd806SEd Tanous 142315ed6780SWilly Tu if (!json_util::readJsonPatch( 14241ef4c342SEd Tanous req, asyncResp->res, "AccountLockoutDuration", unlockTimeout, 14251ef4c342SEd Tanous "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength", 14261ef4c342SEd Tanous maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP", 14271ef4c342SEd Tanous ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem", 1428f5ffd806SEd Tanous oemObject)) 1429f5ffd806SEd Tanous { 1430f5ffd806SEd Tanous return; 1431f5ffd806SEd Tanous } 1432f5ffd806SEd Tanous 1433f5ffd806SEd Tanous if (minPasswordLength) 1434f5ffd806SEd Tanous { 1435ef73ad0dSPaul Fertser crow::connections::systemBus->async_method_call( 1436ef73ad0dSPaul Fertser [asyncResp](const boost::system::error_code ec) { 1437ef73ad0dSPaul Fertser if (ec) 1438ef73ad0dSPaul Fertser { 1439ef73ad0dSPaul Fertser messages::internalError(asyncResp->res); 1440ef73ad0dSPaul Fertser return; 1441ef73ad0dSPaul Fertser } 1442ef73ad0dSPaul Fertser messages::success(asyncResp->res); 1443ef73ad0dSPaul Fertser }, 14441ef4c342SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1445ef73ad0dSPaul Fertser "org.freedesktop.DBus.Properties", "Set", 14461ef4c342SEd Tanous "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength", 1447ef73ad0dSPaul Fertser dbus::utility::DbusVariantType(*minPasswordLength)); 1448f5ffd806SEd Tanous } 1449f5ffd806SEd Tanous 1450f5ffd806SEd Tanous if (maxPasswordLength) 1451f5ffd806SEd Tanous { 14521ef4c342SEd Tanous messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength"); 1453f5ffd806SEd Tanous } 1454f5ffd806SEd Tanous 1455f5ffd806SEd Tanous if (ldapObject) 1456f5ffd806SEd Tanous { 1457f5ffd806SEd Tanous handleLDAPPatch(*ldapObject, asyncResp, "LDAP"); 1458f5ffd806SEd Tanous } 1459f5ffd806SEd Tanous 1460f5ffd806SEd Tanous if (std::optional<nlohmann::json> oemOpenBMCObject; 14611ef4c342SEd Tanous oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC", 1462f5ffd806SEd Tanous oemOpenBMCObject)) 1463f5ffd806SEd Tanous { 1464f5ffd806SEd Tanous if (std::optional<nlohmann::json> authMethodsObject; 1465f5ffd806SEd Tanous oemOpenBMCObject && 1466f5ffd806SEd Tanous json_util::readJson(*oemOpenBMCObject, asyncResp->res, 1467f5ffd806SEd Tanous "AuthMethods", authMethodsObject)) 1468f5ffd806SEd Tanous { 1469f5ffd806SEd Tanous if (authMethodsObject) 1470f5ffd806SEd Tanous { 14711ef4c342SEd Tanous handleAuthMethodsPatch(*authMethodsObject, asyncResp); 1472f5ffd806SEd Tanous } 1473f5ffd806SEd Tanous } 1474f5ffd806SEd Tanous } 1475f5ffd806SEd Tanous 1476f5ffd806SEd Tanous if (activeDirectoryObject) 1477f5ffd806SEd Tanous { 14781ef4c342SEd Tanous handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory"); 1479f5ffd806SEd Tanous } 1480f5ffd806SEd Tanous 1481f5ffd806SEd Tanous if (unlockTimeout) 1482f5ffd806SEd Tanous { 1483f5ffd806SEd Tanous crow::connections::systemBus->async_method_call( 1484f5ffd806SEd Tanous [asyncResp](const boost::system::error_code ec) { 1485f5ffd806SEd Tanous if (ec) 1486f5ffd806SEd Tanous { 1487f5ffd806SEd Tanous messages::internalError(asyncResp->res); 1488f5ffd806SEd Tanous return; 1489f5ffd806SEd Tanous } 1490f5ffd806SEd Tanous messages::success(asyncResp->res); 1491f5ffd806SEd Tanous }, 14921ef4c342SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1493f5ffd806SEd Tanous "org.freedesktop.DBus.Properties", "Set", 14941ef4c342SEd Tanous "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout", 1495168e20c1SEd Tanous dbus::utility::DbusVariantType(*unlockTimeout)); 1496f5ffd806SEd Tanous } 1497f5ffd806SEd Tanous if (lockoutThreshold) 1498f5ffd806SEd Tanous { 1499f5ffd806SEd Tanous crow::connections::systemBus->async_method_call( 1500f5ffd806SEd Tanous [asyncResp](const boost::system::error_code ec) { 1501f5ffd806SEd Tanous if (ec) 1502f5ffd806SEd Tanous { 1503f5ffd806SEd Tanous messages::internalError(asyncResp->res); 1504f5ffd806SEd Tanous return; 1505f5ffd806SEd Tanous } 1506f5ffd806SEd Tanous messages::success(asyncResp->res); 1507f5ffd806SEd Tanous }, 15081ef4c342SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1509f5ffd806SEd Tanous "org.freedesktop.DBus.Properties", "Set", 1510f5ffd806SEd Tanous "xyz.openbmc_project.User.AccountPolicy", 1511f5ffd806SEd Tanous "MaxLoginAttemptBeforeLockout", 1512168e20c1SEd Tanous dbus::utility::DbusVariantType(*lockoutThreshold)); 1513f5ffd806SEd Tanous } 15141ef4c342SEd Tanous } 1515f5ffd806SEd Tanous 15164c7d4d33SEd Tanous inline void handleAccountCollectionHead( 15171ef4c342SEd Tanous App& app, const crow::Request& req, 15181ef4c342SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 15191ef4c342SEd Tanous { 15204c7d4d33SEd Tanous 15213ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 152245ca1b86SEd Tanous { 152345ca1b86SEd Tanous return; 152445ca1b86SEd Tanous } 15254c7d4d33SEd Tanous asyncResp->res.addHeader( 15264c7d4d33SEd Tanous boost::beast::http::field::link, 15274c7d4d33SEd Tanous "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby"); 15284c7d4d33SEd Tanous } 15294c7d4d33SEd Tanous 15304c7d4d33SEd Tanous inline void handleAccountCollectionGet( 15314c7d4d33SEd Tanous App& app, const crow::Request& req, 15324c7d4d33SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 15334c7d4d33SEd Tanous { 15344c7d4d33SEd Tanous handleAccountCollectionHead(app, req, asyncResp); 15351476687dSEd Tanous 15361476687dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 15371476687dSEd Tanous "/redfish/v1/AccountService/Accounts"; 15381ef4c342SEd Tanous asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection." 15391476687dSEd Tanous "ManagerAccountCollection"; 15401476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "Accounts Collection"; 15411476687dSEd Tanous asyncResp->res.jsonValue["Description"] = "BMC User Accounts"; 15420f74e643SEd Tanous 15436c51eab1SEd Tanous Privileges effectiveUserPrivileges = 15446c51eab1SEd Tanous redfish::getUserPrivileges(req.userRole); 15456c51eab1SEd Tanous 1546f5e29f33SJunLin Chen std::string thisUser; 1547f5e29f33SJunLin Chen if (req.session) 1548f5e29f33SJunLin Chen { 1549f5e29f33SJunLin Chen thisUser = req.session->username; 1550f5e29f33SJunLin Chen } 1551b9b2e0b2SEd Tanous crow::connections::systemBus->async_method_call( 1552cef1ddfbSEd Tanous [asyncResp, thisUser, effectiveUserPrivileges]( 1553cef1ddfbSEd Tanous const boost::system::error_code ec, 1554711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& users) { 1555b9b2e0b2SEd Tanous if (ec) 1556b9b2e0b2SEd Tanous { 1557f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1558b9b2e0b2SEd Tanous return; 1559b9b2e0b2SEd Tanous } 1560b9b2e0b2SEd Tanous 1561cef1ddfbSEd Tanous bool userCanSeeAllAccounts = 1562002d39b4SEd Tanous effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"}); 1563cef1ddfbSEd Tanous 1564cef1ddfbSEd Tanous bool userCanSeeSelf = 1565002d39b4SEd Tanous effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"}); 1566cef1ddfbSEd Tanous 1567002d39b4SEd Tanous nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"]; 1568b9b2e0b2SEd Tanous memberArray = nlohmann::json::array(); 1569b9b2e0b2SEd Tanous 15709eb808c1SEd Tanous for (const auto& userpath : users) 1571b9b2e0b2SEd Tanous { 15722dfd18efSEd Tanous std::string user = userpath.first.filename(); 15732dfd18efSEd Tanous if (user.empty()) 1574b9b2e0b2SEd Tanous { 15752dfd18efSEd Tanous messages::internalError(asyncResp->res); 15762dfd18efSEd Tanous BMCWEB_LOG_ERROR << "Invalid firmware ID"; 15772dfd18efSEd Tanous 15782dfd18efSEd Tanous return; 1579b9b2e0b2SEd Tanous } 1580f365910cSGunnar Mills 1581f365910cSGunnar Mills // As clarified by Redfish here: 1582f365910cSGunnar Mills // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration 15836c51eab1SEd Tanous // Users without ConfigureUsers, only see their own 15846c51eab1SEd Tanous // account. Users with ConfigureUsers, see all 15856c51eab1SEd Tanous // accounts. 15861ef4c342SEd Tanous if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf)) 1587f365910cSGunnar Mills { 15881476687dSEd Tanous nlohmann::json::object_t member; 15891476687dSEd Tanous member["@odata.id"] = 1590002d39b4SEd Tanous "/redfish/v1/AccountService/Accounts/" + user; 15911476687dSEd Tanous memberArray.push_back(std::move(member)); 1592b9b2e0b2SEd Tanous } 1593f365910cSGunnar Mills } 15941ef4c342SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size(); 1595b9b2e0b2SEd Tanous }, 15961ef4c342SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1597b9b2e0b2SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 15981ef4c342SEd Tanous } 15996c51eab1SEd Tanous 16001ef4c342SEd Tanous inline void handleAccountCollectionPost( 16011ef4c342SEd Tanous App& app, const crow::Request& req, 16021ef4c342SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 16031ef4c342SEd Tanous { 16043ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 160545ca1b86SEd Tanous { 160645ca1b86SEd Tanous return; 160745ca1b86SEd Tanous } 16089712f8acSEd Tanous std::string username; 16099712f8acSEd Tanous std::string password; 1610a24526dcSEd Tanous std::optional<std::string> roleId("User"); 1611a24526dcSEd Tanous std::optional<bool> enabled = true; 16121ef4c342SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username, 16131ef4c342SEd Tanous "Password", password, "RoleId", roleId, 16141ef4c342SEd Tanous "Enabled", enabled)) 161504ae99ecSEd Tanous { 161604ae99ecSEd Tanous return; 161704ae99ecSEd Tanous } 161804ae99ecSEd Tanous 161954fc587aSNagaraju Goruganti std::string priv = getPrivilegeFromRoleId(*roleId); 162084e12cb7SAppaRao Puli if (priv.empty()) 162104ae99ecSEd Tanous { 16221ef4c342SEd Tanous messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId"); 162304ae99ecSEd Tanous return; 162404ae99ecSEd Tanous } 16259712f8acSEd Tanous roleId = priv; 162604ae99ecSEd Tanous 1627599c71d8SAyushi Smriti // Reading AllGroups property 16281e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 16291ef4c342SEd Tanous *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", 16301ef4c342SEd Tanous "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager", 16311ef4c342SEd Tanous "AllGroups", 1632599c71d8SAyushi Smriti [asyncResp, username, password{std::move(password)}, roleId, 1633168e20c1SEd Tanous enabled](const boost::system::error_code ec, 16341e1e598dSJonathan Doman const std::vector<std::string>& allGroupsList) { 1635599c71d8SAyushi Smriti if (ec) 1636599c71d8SAyushi Smriti { 1637599c71d8SAyushi Smriti BMCWEB_LOG_DEBUG << "ERROR with async_method_call"; 1638599c71d8SAyushi Smriti messages::internalError(asyncResp->res); 1639599c71d8SAyushi Smriti return; 1640599c71d8SAyushi Smriti } 1641599c71d8SAyushi Smriti 16421e1e598dSJonathan Doman if (allGroupsList.empty()) 1643599c71d8SAyushi Smriti { 1644599c71d8SAyushi Smriti messages::internalError(asyncResp->res); 1645599c71d8SAyushi Smriti return; 1646599c71d8SAyushi Smriti } 1647599c71d8SAyushi Smriti 164804ae99ecSEd Tanous crow::connections::systemBus->async_method_call( 16491ef4c342SEd Tanous [asyncResp, username, password](const boost::system::error_code ec2, 165059d494eeSPatrick Williams sdbusplus::message_t& m) { 165123a21a1cSEd Tanous if (ec2) 165204ae99ecSEd Tanous { 16531ef4c342SEd Tanous userErrorMessageHandler(m.get_error(), asyncResp, username, ""); 165404ae99ecSEd Tanous return; 165504ae99ecSEd Tanous } 165604ae99ecSEd Tanous 1657002d39b4SEd Tanous if (pamUpdatePassword(username, password) != PAM_SUCCESS) 165804ae99ecSEd Tanous { 16596c51eab1SEd Tanous // At this point we have a user that's been 16606c51eab1SEd Tanous // created, but the password set 16616c51eab1SEd Tanous // failed.Something is wrong, so delete the user 16626c51eab1SEd Tanous // that we've already created 16631ef4c342SEd Tanous sdbusplus::message::object_path tempObjPath(rootUserDbusPath); 1664b477fd44SP Dheeraj Srujan Kumar tempObjPath /= username; 1665b477fd44SP Dheeraj Srujan Kumar const std::string userPath(tempObjPath); 1666b477fd44SP Dheeraj Srujan Kumar 166704ae99ecSEd Tanous crow::connections::systemBus->async_method_call( 16681ef4c342SEd Tanous [asyncResp, password](const boost::system::error_code ec3) { 166923a21a1cSEd Tanous if (ec3) 167004ae99ecSEd Tanous { 1671002d39b4SEd Tanous messages::internalError(asyncResp->res); 167204ae99ecSEd Tanous return; 167304ae99ecSEd Tanous } 167404ae99ecSEd Tanous 16750d4197e0Sanil kumar appana // If password is invalid 16761ef4c342SEd Tanous messages::propertyValueFormatError(asyncResp->res, password, 16771ef4c342SEd Tanous "Password"); 167804ae99ecSEd Tanous }, 1679002d39b4SEd Tanous "xyz.openbmc_project.User.Manager", userPath, 1680002d39b4SEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 168104ae99ecSEd Tanous 168204ae99ecSEd Tanous BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; 168304ae99ecSEd Tanous return; 168404ae99ecSEd Tanous } 168504ae99ecSEd Tanous 1686f12894f8SJason M. Bills messages::created(asyncResp->res); 168704ae99ecSEd Tanous asyncResp->res.addHeader( 16881ef4c342SEd Tanous "Location", "/redfish/v1/AccountService/Accounts/" + username); 168904ae99ecSEd Tanous }, 1690002d39b4SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1691002d39b4SEd Tanous "xyz.openbmc_project.User.Manager", "CreateUser", username, 1692002d39b4SEd Tanous allGroupsList, *roleId, *enabled); 16931e1e598dSJonathan Doman }); 16941ef4c342SEd Tanous } 1695b9b2e0b2SEd Tanous 16961ef4c342SEd Tanous inline void 16974c7d4d33SEd Tanous handleAccountHead(App& app, const crow::Request& req, 16986c51eab1SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 16994c7d4d33SEd Tanous const std::string& /*accountName*/) 17001ef4c342SEd Tanous { 17014c7d4d33SEd Tanous 17023ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 170345ca1b86SEd Tanous { 170445ca1b86SEd Tanous return; 170545ca1b86SEd Tanous } 17064c7d4d33SEd Tanous asyncResp->res.addHeader( 17074c7d4d33SEd Tanous boost::beast::http::field::link, 17084c7d4d33SEd Tanous "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby"); 17094c7d4d33SEd Tanous } 17104c7d4d33SEd Tanous inline void 17114c7d4d33SEd Tanous handleAccountGet(App& app, const crow::Request& req, 17124c7d4d33SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 17134c7d4d33SEd Tanous const std::string& accountName) 17144c7d4d33SEd Tanous { 17154c7d4d33SEd Tanous handleAccountHead(app, req, asyncResp, accountName); 17161ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION 1717031514fbSJunLin Chen // If authentication is disabled, there are no user accounts 1718d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName); 1719031514fbSJunLin Chen return; 1720031514fbSJunLin Chen 17211ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION 1722031514fbSJunLin Chen if (req.session == nullptr) 1723031514fbSJunLin Chen { 1724031514fbSJunLin Chen messages::internalError(asyncResp->res); 1725031514fbSJunLin Chen return; 1726031514fbSJunLin Chen } 17276c51eab1SEd Tanous if (req.session->username != accountName) 1728b9b2e0b2SEd Tanous { 17296c51eab1SEd Tanous // At this point we've determined that the user is trying to 17301ef4c342SEd Tanous // modify a user that isn't them. We need to verify that they 17311ef4c342SEd Tanous // have permissions to modify other users, so re-run the auth 17321ef4c342SEd Tanous // check with the same permissions, minus ConfigureSelf. 17336c51eab1SEd Tanous Privileges effectiveUserPrivileges = 17346c51eab1SEd Tanous redfish::getUserPrivileges(req.userRole); 17351ef4c342SEd Tanous Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers", 17361ef4c342SEd Tanous "ConfigureManager"}; 17376c51eab1SEd Tanous if (!effectiveUserPrivileges.isSupersetOf( 17386c51eab1SEd Tanous requiredPermissionsToChangeNonSelf)) 1739900f9497SJoseph Reynolds { 1740900f9497SJoseph Reynolds BMCWEB_LOG_DEBUG << "GET Account denied access"; 1741900f9497SJoseph Reynolds messages::insufficientPrivilege(asyncResp->res); 1742900f9497SJoseph Reynolds return; 1743900f9497SJoseph Reynolds } 1744900f9497SJoseph Reynolds } 1745900f9497SJoseph Reynolds 1746b9b2e0b2SEd Tanous crow::connections::systemBus->async_method_call( 17471ef4c342SEd Tanous [asyncResp, 17481ef4c342SEd Tanous accountName](const boost::system::error_code ec, 1749711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& users) { 1750b9b2e0b2SEd Tanous if (ec) 1751b9b2e0b2SEd Tanous { 1752f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1753b9b2e0b2SEd Tanous return; 1754b9b2e0b2SEd Tanous } 1755711ac7a9SEd Tanous const auto userIt = std::find_if( 1756b477fd44SP Dheeraj Srujan Kumar users.begin(), users.end(), 1757b477fd44SP Dheeraj Srujan Kumar [accountName]( 1758b477fd44SP Dheeraj Srujan Kumar const std::pair<sdbusplus::message::object_path, 1759002d39b4SEd Tanous dbus::utility::DBusInteracesMap>& user) { 176055f79e6fSEd Tanous return accountName == user.first.filename(); 1761b477fd44SP Dheeraj Srujan Kumar }); 1762b9b2e0b2SEd Tanous 176384e12cb7SAppaRao Puli if (userIt == users.end()) 1764b9b2e0b2SEd Tanous { 1765002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "ManagerAccount", 1766002d39b4SEd Tanous accountName); 176784e12cb7SAppaRao Puli return; 176884e12cb7SAppaRao Puli } 17694e68c45bSAyushi Smriti 17701476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = 17711476687dSEd Tanous "#ManagerAccount.v1_4_0.ManagerAccount"; 17721476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "User Account"; 17731476687dSEd Tanous asyncResp->res.jsonValue["Description"] = "User Account"; 17741476687dSEd Tanous asyncResp->res.jsonValue["Password"] = nullptr; 17754e68c45bSAyushi Smriti 177684e12cb7SAppaRao Puli for (const auto& interface : userIt->second) 177765b0dc32SEd Tanous { 1778002d39b4SEd Tanous if (interface.first == "xyz.openbmc_project.User.Attributes") 177965b0dc32SEd Tanous { 178065b0dc32SEd Tanous for (const auto& property : interface.second) 178165b0dc32SEd Tanous { 178265b0dc32SEd Tanous if (property.first == "UserEnabled") 178365b0dc32SEd Tanous { 178465b0dc32SEd Tanous const bool* userEnabled = 1785abf2add6SEd Tanous std::get_if<bool>(&property.second); 178665b0dc32SEd Tanous if (userEnabled == nullptr) 178765b0dc32SEd Tanous { 1788002d39b4SEd Tanous BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool"; 178984e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 179084e12cb7SAppaRao Puli return; 179165b0dc32SEd Tanous } 1792002d39b4SEd Tanous asyncResp->res.jsonValue["Enabled"] = *userEnabled; 179365b0dc32SEd Tanous } 1794002d39b4SEd Tanous else if (property.first == "UserLockedForFailedAttempt") 179565b0dc32SEd Tanous { 179665b0dc32SEd Tanous const bool* userLocked = 1797abf2add6SEd Tanous std::get_if<bool>(&property.second); 179865b0dc32SEd Tanous if (userLocked == nullptr) 179965b0dc32SEd Tanous { 180084e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "UserLockedForF" 180184e12cb7SAppaRao Puli "ailedAttempt " 180284e12cb7SAppaRao Puli "wasn't a bool"; 180384e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 180484e12cb7SAppaRao Puli return; 180565b0dc32SEd Tanous } 1806002d39b4SEd Tanous asyncResp->res.jsonValue["Locked"] = *userLocked; 1807002d39b4SEd Tanous asyncResp->res 1808002d39b4SEd Tanous .jsonValue["Locked@Redfish.AllowableValues"] = { 18093bf4e632SJoseph Reynolds "false"}; // can only unlock accounts 181065b0dc32SEd Tanous } 181184e12cb7SAppaRao Puli else if (property.first == "UserPrivilege") 181284e12cb7SAppaRao Puli { 181354fc587aSNagaraju Goruganti const std::string* userPrivPtr = 1814002d39b4SEd Tanous std::get_if<std::string>(&property.second); 181554fc587aSNagaraju Goruganti if (userPrivPtr == nullptr) 181684e12cb7SAppaRao Puli { 1817002d39b4SEd Tanous BMCWEB_LOG_ERROR << "UserPrivilege wasn't a " 181884e12cb7SAppaRao Puli "string"; 181984e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 182084e12cb7SAppaRao Puli return; 182184e12cb7SAppaRao Puli } 18221ef4c342SEd Tanous std::string role = getRoleIdFromPrivilege(*userPrivPtr); 182354fc587aSNagaraju Goruganti if (role.empty()) 182484e12cb7SAppaRao Puli { 182584e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "Invalid user role"; 182684e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 182784e12cb7SAppaRao Puli return; 182884e12cb7SAppaRao Puli } 182954fc587aSNagaraju Goruganti asyncResp->res.jsonValue["RoleId"] = role; 183084e12cb7SAppaRao Puli 18311476687dSEd Tanous nlohmann::json& roleEntry = 1832002d39b4SEd Tanous asyncResp->res.jsonValue["Links"]["Role"]; 18331476687dSEd Tanous roleEntry["@odata.id"] = 1834002d39b4SEd Tanous "/redfish/v1/AccountService/Roles/" + role; 183584e12cb7SAppaRao Puli } 1836002d39b4SEd Tanous else if (property.first == "UserPasswordExpired") 18373bf4e632SJoseph Reynolds { 18383bf4e632SJoseph Reynolds const bool* userPasswordExpired = 18393bf4e632SJoseph Reynolds std::get_if<bool>(&property.second); 18403bf4e632SJoseph Reynolds if (userPasswordExpired == nullptr) 18413bf4e632SJoseph Reynolds { 18420fda0f12SGeorge Liu BMCWEB_LOG_ERROR 18430fda0f12SGeorge Liu << "UserPasswordExpired wasn't a bool"; 18443bf4e632SJoseph Reynolds messages::internalError(asyncResp->res); 18453bf4e632SJoseph Reynolds return; 18463bf4e632SJoseph Reynolds } 1847002d39b4SEd Tanous asyncResp->res.jsonValue["PasswordChangeRequired"] = 18483bf4e632SJoseph Reynolds *userPasswordExpired; 18493bf4e632SJoseph Reynolds } 1850c7229815SAbhishek Patel else if (property.first == "UserGroups") 1851c7229815SAbhishek Patel { 1852c7229815SAbhishek Patel const std::vector<std::string>* userGroups = 1853c7229815SAbhishek Patel std::get_if<std::vector<std::string>>( 1854c7229815SAbhishek Patel &property.second); 1855c7229815SAbhishek Patel if (userGroups == nullptr) 1856c7229815SAbhishek Patel { 1857c7229815SAbhishek Patel BMCWEB_LOG_ERROR 1858c7229815SAbhishek Patel << "userGroups wasn't a string vector"; 1859c7229815SAbhishek Patel messages::internalError(asyncResp->res); 1860c7229815SAbhishek Patel return; 1861c7229815SAbhishek Patel } 1862c7229815SAbhishek Patel if (!translateUserGroup(*userGroups, asyncResp->res)) 1863c7229815SAbhishek Patel { 1864c7229815SAbhishek Patel BMCWEB_LOG_ERROR << "userGroups mapping failed"; 1865c7229815SAbhishek Patel messages::internalError(asyncResp->res); 1866c7229815SAbhishek Patel return; 1867c7229815SAbhishek Patel } 1868c7229815SAbhishek Patel } 186965b0dc32SEd Tanous } 187065b0dc32SEd Tanous } 187165b0dc32SEd Tanous } 187265b0dc32SEd Tanous 1873b9b2e0b2SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 187484e12cb7SAppaRao Puli "/redfish/v1/AccountService/Accounts/" + accountName; 1875b9b2e0b2SEd Tanous asyncResp->res.jsonValue["Id"] = accountName; 1876b9b2e0b2SEd Tanous asyncResp->res.jsonValue["UserName"] = accountName; 1877b9b2e0b2SEd Tanous }, 18781ef4c342SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1879b9b2e0b2SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 18801ef4c342SEd Tanous } 1881a840879dSEd Tanous 18821ef4c342SEd Tanous inline void 18831ef4c342SEd Tanous handleAccounttDelete(App& app, const crow::Request& req, 18846c51eab1SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 18851ef4c342SEd Tanous const std::string& username) 18861ef4c342SEd Tanous { 18873ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 188845ca1b86SEd Tanous { 188945ca1b86SEd Tanous return; 189045ca1b86SEd Tanous } 18911ef4c342SEd Tanous 18921ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION 1893031514fbSJunLin Chen // If authentication is disabled, there are no user accounts 1894d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "ManagerAccount", username); 1895031514fbSJunLin Chen return; 1896031514fbSJunLin Chen 18971ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION 18981ef4c342SEd Tanous sdbusplus::message::object_path tempObjPath(rootUserDbusPath); 18991ef4c342SEd Tanous tempObjPath /= username; 19001ef4c342SEd Tanous const std::string userPath(tempObjPath); 19011ef4c342SEd Tanous 19021ef4c342SEd Tanous crow::connections::systemBus->async_method_call( 19031ef4c342SEd Tanous [asyncResp, username](const boost::system::error_code ec) { 19041ef4c342SEd Tanous if (ec) 19051ef4c342SEd Tanous { 1906d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "ManagerAccount", 19071ef4c342SEd Tanous username); 19081ef4c342SEd Tanous return; 19091ef4c342SEd Tanous } 19101ef4c342SEd Tanous 19111ef4c342SEd Tanous messages::accountRemoved(asyncResp->res); 19121ef4c342SEd Tanous }, 19131ef4c342SEd Tanous "xyz.openbmc_project.User.Manager", userPath, 19141ef4c342SEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 19151ef4c342SEd Tanous } 19161ef4c342SEd Tanous 19171ef4c342SEd Tanous inline void 19181ef4c342SEd Tanous handleAccountPatch(App& app, const crow::Request& req, 19191ef4c342SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 19201ef4c342SEd Tanous const std::string& username) 19211ef4c342SEd Tanous { 19221ef4c342SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 19231ef4c342SEd Tanous { 19241ef4c342SEd Tanous return; 19251ef4c342SEd Tanous } 19261ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION 19271ef4c342SEd Tanous // If authentication is disabled, there are no user accounts 1928d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "ManagerAccount", username); 19291ef4c342SEd Tanous return; 19301ef4c342SEd Tanous 19311ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION 1932a24526dcSEd Tanous std::optional<std::string> newUserName; 1933a24526dcSEd Tanous std::optional<std::string> password; 1934a24526dcSEd Tanous std::optional<bool> enabled; 1935a24526dcSEd Tanous std::optional<std::string> roleId; 193624c8542dSRatan Gupta std::optional<bool> locked; 1937e9cc5172SEd Tanous 1938031514fbSJunLin Chen if (req.session == nullptr) 1939031514fbSJunLin Chen { 1940031514fbSJunLin Chen messages::internalError(asyncResp->res); 1941031514fbSJunLin Chen return; 1942031514fbSJunLin Chen } 1943031514fbSJunLin Chen 1944e9cc5172SEd Tanous Privileges effectiveUserPrivileges = 1945e9cc5172SEd Tanous redfish::getUserPrivileges(req.userRole); 1946e9cc5172SEd Tanous Privileges configureUsers = {"ConfigureUsers"}; 1947e9cc5172SEd Tanous bool userHasConfigureUsers = 1948e9cc5172SEd Tanous effectiveUserPrivileges.isSupersetOf(configureUsers); 1949e9cc5172SEd Tanous if (userHasConfigureUsers) 1950e9cc5172SEd Tanous { 1951e9cc5172SEd Tanous // Users with ConfigureUsers can modify for all users 19521ef4c342SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", 19531ef4c342SEd Tanous newUserName, "Password", password, 19541ef4c342SEd Tanous "RoleId", roleId, "Enabled", enabled, 19551ef4c342SEd Tanous "Locked", locked)) 1956a840879dSEd Tanous { 1957a840879dSEd Tanous return; 1958a840879dSEd Tanous } 1959e9cc5172SEd Tanous } 1960e9cc5172SEd Tanous else 1961900f9497SJoseph Reynolds { 1962e9cc5172SEd Tanous // ConfigureSelf accounts can only modify their own account 1963e9cc5172SEd Tanous if (username != req.session->username) 1964900f9497SJoseph Reynolds { 1965900f9497SJoseph Reynolds messages::insufficientPrivilege(asyncResp->res); 1966900f9497SJoseph Reynolds return; 1967900f9497SJoseph Reynolds } 1968031514fbSJunLin Chen 1969e9cc5172SEd Tanous // ConfigureSelf accounts can only modify their password 19701ef4c342SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "Password", 19711ef4c342SEd Tanous password)) 1972e9cc5172SEd Tanous { 1973e9cc5172SEd Tanous return; 1974e9cc5172SEd Tanous } 1975900f9497SJoseph Reynolds } 1976900f9497SJoseph Reynolds 197766b5ca76Sjayaprakash Mutyala // if user name is not provided in the patch method or if it 19786c51eab1SEd Tanous // matches the user name in the URI, then we are treating it as 19796c51eab1SEd Tanous // updating user properties other then username. If username 19806c51eab1SEd Tanous // provided doesn't match the URI, then we are treating this as 19816c51eab1SEd Tanous // user rename request. 198266b5ca76Sjayaprakash Mutyala if (!newUserName || (newUserName.value() == username)) 1983a840879dSEd Tanous { 19841ef4c342SEd Tanous updateUserProperties(asyncResp, username, password, enabled, roleId, 19851ef4c342SEd Tanous locked); 198684e12cb7SAppaRao Puli return; 198784e12cb7SAppaRao Puli } 198884e12cb7SAppaRao Puli crow::connections::systemBus->async_method_call( 19896c51eab1SEd Tanous [asyncResp, username, password(std::move(password)), 19901ef4c342SEd Tanous roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)}, 199159d494eeSPatrick Williams locked](const boost::system::error_code ec, sdbusplus::message_t& m) { 199284e12cb7SAppaRao Puli if (ec) 199384e12cb7SAppaRao Puli { 1994002d39b4SEd Tanous userErrorMessageHandler(m.get_error(), asyncResp, newUser, 1995002d39b4SEd Tanous username); 1996a840879dSEd Tanous return; 1997a840879dSEd Tanous } 1998a840879dSEd Tanous 1999002d39b4SEd Tanous updateUserProperties(asyncResp, newUser, password, enabled, roleId, 2000002d39b4SEd Tanous locked); 200184e12cb7SAppaRao Puli }, 20021ef4c342SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 200384e12cb7SAppaRao Puli "xyz.openbmc_project.User.Manager", "RenameUser", username, 200484e12cb7SAppaRao Puli *newUserName); 20051ef4c342SEd Tanous } 20061ef4c342SEd Tanous 20071ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app) 20081ef4c342SEd Tanous { 20091ef4c342SEd Tanous 20101ef4c342SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/") 20114c7d4d33SEd Tanous .privileges(redfish::privileges::headAccountService) 20124c7d4d33SEd Tanous .methods(boost::beast::http::verb::head)( 20134c7d4d33SEd Tanous std::bind_front(handleAccountServiceHead, std::ref(app))); 20144c7d4d33SEd Tanous 20154c7d4d33SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/") 20161ef4c342SEd Tanous .privileges(redfish::privileges::getAccountService) 20171ef4c342SEd Tanous .methods(boost::beast::http::verb::get)( 20181ef4c342SEd Tanous std::bind_front(handleAccountServiceGet, std::ref(app))); 20191ef4c342SEd Tanous 20201ef4c342SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/") 20211ef4c342SEd Tanous .privileges(redfish::privileges::patchAccountService) 20221ef4c342SEd Tanous .methods(boost::beast::http::verb::patch)( 20231ef4c342SEd Tanous std::bind_front(handleAccountServicePatch, std::ref(app))); 20241ef4c342SEd Tanous 20251ef4c342SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/") 20264c7d4d33SEd Tanous .privileges(redfish::privileges::headManagerAccountCollection) 20274c7d4d33SEd Tanous .methods(boost::beast::http::verb::head)( 20284c7d4d33SEd Tanous std::bind_front(handleAccountCollectionHead, std::ref(app))); 20294c7d4d33SEd Tanous 20304c7d4d33SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/") 20311ef4c342SEd Tanous .privileges(redfish::privileges::getManagerAccountCollection) 20321ef4c342SEd Tanous .methods(boost::beast::http::verb::get)( 20331ef4c342SEd Tanous std::bind_front(handleAccountCollectionGet, std::ref(app))); 20341ef4c342SEd Tanous 20351ef4c342SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/") 20361ef4c342SEd Tanous .privileges(redfish::privileges::postManagerAccountCollection) 20371ef4c342SEd Tanous .methods(boost::beast::http::verb::post)( 20381ef4c342SEd Tanous std::bind_front(handleAccountCollectionPost, std::ref(app))); 20391ef4c342SEd Tanous 20401ef4c342SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") 20414c7d4d33SEd Tanous .privileges(redfish::privileges::headManagerAccount) 20424c7d4d33SEd Tanous .methods(boost::beast::http::verb::head)( 20434c7d4d33SEd Tanous std::bind_front(handleAccountHead, std::ref(app))); 20444c7d4d33SEd Tanous 20454c7d4d33SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") 20461ef4c342SEd Tanous .privileges(redfish::privileges::getManagerAccount) 20471ef4c342SEd Tanous .methods(boost::beast::http::verb::get)( 20481ef4c342SEd Tanous std::bind_front(handleAccountGet, std::ref(app))); 20491ef4c342SEd Tanous 20501ef4c342SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") 20511ef4c342SEd Tanous // TODO this privilege should be using the generated endpoints, but 20521ef4c342SEd Tanous // because of the special handling of ConfigureSelf, it's not able to 20531ef4c342SEd Tanous // yet 20541ef4c342SEd Tanous .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}}) 20551ef4c342SEd Tanous .methods(boost::beast::http::verb::patch)( 20561ef4c342SEd Tanous std::bind_front(handleAccountPatch, std::ref(app))); 205784e12cb7SAppaRao Puli 20586c51eab1SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") 2059ed398213SEd Tanous .privileges(redfish::privileges::deleteManagerAccount) 20606c51eab1SEd Tanous .methods(boost::beast::http::verb::delete_)( 20611ef4c342SEd Tanous std::bind_front(handleAccounttDelete, std::ref(app))); 206206e086d9SEd Tanous } 206388d16c9aSLewanczyk, Dawid 206488d16c9aSLewanczyk, Dawid } // namespace redfish 2065