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 #include "node.hpp" 1888d16c9aSLewanczyk, Dawid 1924c8542dSRatan Gupta #include <dbus_utility.hpp> 2065b0dc32SEd Tanous #include <error_messages.hpp> 21b9b2e0b2SEd Tanous #include <openbmc_dbus_rest.hpp> 22a840879dSEd Tanous #include <utils/json_utils.hpp> 23abf2add6SEd Tanous #include <variant> 24b9b2e0b2SEd Tanous 251abe55efSEd Tanous namespace redfish 261abe55efSEd Tanous { 2788d16c9aSLewanczyk, Dawid 286973a582SRatan Gupta constexpr const char* ldapConfigObject = 296973a582SRatan Gupta "/xyz/openbmc_project/user/ldap/openldap"; 30ab828d7cSRatan Gupta constexpr const char* ADConfigObject = 31ab828d7cSRatan Gupta "/xyz/openbmc_project/user/ldap/active_directory"; 32ab828d7cSRatan Gupta 336973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap"; 346973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config"; 356973a582SRatan Gupta constexpr const char* ldapConfigInterface = 366973a582SRatan Gupta "xyz.openbmc_project.User.Ldap.Config"; 376973a582SRatan Gupta constexpr const char* ldapCreateInterface = 386973a582SRatan Gupta "xyz.openbmc_project.User.Ldap.Create"; 396973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable"; 4006785244SRatan Gupta constexpr const char* ldapPrivMapperInterface = 4106785244SRatan Gupta "xyz.openbmc_project.User.PrivilegeMapper"; 426973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager"; 436973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties"; 446973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper"; 456973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper"; 466973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper"; 476973a582SRatan Gupta 4854fc587aSNagaraju Goruganti struct LDAPRoleMapData 4954fc587aSNagaraju Goruganti { 5054fc587aSNagaraju Goruganti std::string groupName; 5154fc587aSNagaraju Goruganti std::string privilege; 5254fc587aSNagaraju Goruganti }; 5354fc587aSNagaraju Goruganti 546973a582SRatan Gupta struct LDAPConfigData 556973a582SRatan Gupta { 566973a582SRatan Gupta std::string uri{}; 576973a582SRatan Gupta std::string bindDN{}; 586973a582SRatan Gupta std::string baseDN{}; 596973a582SRatan Gupta std::string searchScope{}; 606973a582SRatan Gupta std::string serverType{}; 616973a582SRatan Gupta bool serviceEnabled = false; 626973a582SRatan Gupta std::string userNameAttribute{}; 636973a582SRatan Gupta std::string groupAttribute{}; 6454fc587aSNagaraju Goruganti std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList; 656973a582SRatan Gupta }; 666973a582SRatan Gupta 67107077deSPrzemyslaw Czarnowski using DbusVariantType = sdbusplus::message::variant<bool, int32_t, std::string>; 68107077deSPrzemyslaw Czarnowski 69107077deSPrzemyslaw Czarnowski using DbusInterfaceType = boost::container::flat_map< 70107077deSPrzemyslaw Czarnowski std::string, boost::container::flat_map<std::string, DbusVariantType>>; 71107077deSPrzemyslaw Czarnowski 72107077deSPrzemyslaw Czarnowski using ManagedObjectType = 73107077deSPrzemyslaw Czarnowski std::vector<std::pair<sdbusplus::message::object_path, DbusInterfaceType>>; 74107077deSPrzemyslaw Czarnowski 756973a582SRatan Gupta using GetObjectType = 766973a582SRatan Gupta std::vector<std::pair<std::string, std::vector<std::string>>>; 7784e12cb7SAppaRao Puli 7854fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role) 7984e12cb7SAppaRao Puli { 8084e12cb7SAppaRao Puli if (role == "priv-admin") 8184e12cb7SAppaRao Puli { 8284e12cb7SAppaRao Puli return "Administrator"; 8384e12cb7SAppaRao Puli } 8484e12cb7SAppaRao Puli else if (role == "priv-user") 8584e12cb7SAppaRao Puli { 86c80fee55SAppaRao Puli return "ReadOnly"; 8784e12cb7SAppaRao Puli } 8884e12cb7SAppaRao Puli else if (role == "priv-operator") 8984e12cb7SAppaRao Puli { 9084e12cb7SAppaRao Puli return "Operator"; 9184e12cb7SAppaRao Puli } 92e9e6d240Sjayaprakash Mutyala else if ((role == "") || (role == "priv-noaccess")) 93e9e6d240Sjayaprakash Mutyala { 94e9e6d240Sjayaprakash Mutyala return "NoAccess"; 95e9e6d240Sjayaprakash Mutyala } 9684e12cb7SAppaRao Puli return ""; 9784e12cb7SAppaRao Puli } 9854fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role) 9984e12cb7SAppaRao Puli { 10084e12cb7SAppaRao Puli if (role == "Administrator") 10184e12cb7SAppaRao Puli { 10284e12cb7SAppaRao Puli return "priv-admin"; 10384e12cb7SAppaRao Puli } 104c80fee55SAppaRao Puli else if (role == "ReadOnly") 10584e12cb7SAppaRao Puli { 10684e12cb7SAppaRao Puli return "priv-user"; 10784e12cb7SAppaRao Puli } 10884e12cb7SAppaRao Puli else if (role == "Operator") 10984e12cb7SAppaRao Puli { 11084e12cb7SAppaRao Puli return "priv-operator"; 11184e12cb7SAppaRao Puli } 112e9e6d240Sjayaprakash Mutyala else if (role == "NoAccess") 113e9e6d240Sjayaprakash Mutyala { 114e9e6d240Sjayaprakash Mutyala return "priv-noaccess"; 115e9e6d240Sjayaprakash Mutyala } 11684e12cb7SAppaRao Puli return ""; 11784e12cb7SAppaRao Puli } 118b9b2e0b2SEd Tanous 11966b5ca76Sjayaprakash Mutyala void userErrorMessageHandler(const sd_bus_error* e, 12066b5ca76Sjayaprakash Mutyala std::shared_ptr<AsyncResp> asyncResp, 12166b5ca76Sjayaprakash Mutyala const std::string& newUser, 12266b5ca76Sjayaprakash Mutyala const std::string& username) 12366b5ca76Sjayaprakash Mutyala { 12466b5ca76Sjayaprakash Mutyala const char* errorMessage = e->name; 12566b5ca76Sjayaprakash Mutyala if (e == nullptr) 12666b5ca76Sjayaprakash Mutyala { 12766b5ca76Sjayaprakash Mutyala messages::internalError(asyncResp->res); 12866b5ca76Sjayaprakash Mutyala return; 12966b5ca76Sjayaprakash Mutyala } 13066b5ca76Sjayaprakash Mutyala 13166b5ca76Sjayaprakash Mutyala if (strcmp(errorMessage, 13266b5ca76Sjayaprakash Mutyala "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0) 13366b5ca76Sjayaprakash Mutyala { 13466b5ca76Sjayaprakash Mutyala messages::resourceAlreadyExists(asyncResp->res, 13566b5ca76Sjayaprakash Mutyala "#ManagerAccount.v1_0_3.ManagerAccount", 13666b5ca76Sjayaprakash Mutyala "UserName", newUser); 13766b5ca76Sjayaprakash Mutyala } 13866b5ca76Sjayaprakash Mutyala else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error." 13966b5ca76Sjayaprakash Mutyala "UserNameDoesNotExist") == 0) 14066b5ca76Sjayaprakash Mutyala { 14166b5ca76Sjayaprakash Mutyala messages::resourceNotFound( 14266b5ca76Sjayaprakash Mutyala asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", username); 14366b5ca76Sjayaprakash Mutyala } 14466b5ca76Sjayaprakash Mutyala else if (strcmp(errorMessage, 14566b5ca76Sjayaprakash Mutyala "xyz.openbmc_project.Common.Error.InvalidArgument") == 0) 14666b5ca76Sjayaprakash Mutyala { 14766b5ca76Sjayaprakash Mutyala messages::propertyValueFormatError(asyncResp->res, newUser, "UserName"); 14866b5ca76Sjayaprakash Mutyala } 14966b5ca76Sjayaprakash Mutyala else if (strcmp(errorMessage, 15066b5ca76Sjayaprakash Mutyala "xyz.openbmc_project.User.Common.Error.NoResource") == 0) 15166b5ca76Sjayaprakash Mutyala { 15266b5ca76Sjayaprakash Mutyala messages::createLimitReachedForResource(asyncResp->res); 15366b5ca76Sjayaprakash Mutyala } 15466b5ca76Sjayaprakash Mutyala else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error." 15566b5ca76Sjayaprakash Mutyala "UserNameGroupFail") == 0) 15666b5ca76Sjayaprakash Mutyala { 15766b5ca76Sjayaprakash Mutyala messages::propertyValueFormatError(asyncResp->res, newUser, "UserName"); 15866b5ca76Sjayaprakash Mutyala } 15966b5ca76Sjayaprakash Mutyala else 16066b5ca76Sjayaprakash Mutyala { 16166b5ca76Sjayaprakash Mutyala messages::internalError(asyncResp->res); 16266b5ca76Sjayaprakash Mutyala } 16366b5ca76Sjayaprakash Mutyala 16466b5ca76Sjayaprakash Mutyala return; 16566b5ca76Sjayaprakash Mutyala } 16666b5ca76Sjayaprakash Mutyala 1676973a582SRatan Gupta void parseLDAPConfigData(nlohmann::json& json_response, 168ab828d7cSRatan Gupta const LDAPConfigData& confData, 169ab828d7cSRatan Gupta const std::string& ldapType) 1706973a582SRatan Gupta { 171ab828d7cSRatan Gupta std::string service = 172ab828d7cSRatan Gupta (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService"; 17337cce918SMarri Devender Rao nlohmann::json ldap = { 1746973a582SRatan Gupta {"ServiceEnabled", confData.serviceEnabled}, 1756973a582SRatan Gupta {"ServiceAddresses", nlohmann::json::array({confData.uri})}, 1766973a582SRatan Gupta {"Authentication", 1776973a582SRatan Gupta {{"AuthenticationType", "UsernameAndPassword"}, 1786973a582SRatan Gupta {"Username", confData.bindDN}, 1796973a582SRatan Gupta {"Password", nullptr}}}, 1806973a582SRatan Gupta {"LDAPService", 1816973a582SRatan Gupta {{"SearchSettings", 1826973a582SRatan Gupta {{"BaseDistinguishedNames", 1836973a582SRatan Gupta nlohmann::json::array({confData.baseDN})}, 1846973a582SRatan Gupta {"UsernameAttribute", confData.userNameAttribute}, 1856973a582SRatan Gupta {"GroupsAttribute", confData.groupAttribute}}}}}, 1866973a582SRatan Gupta }; 18754fc587aSNagaraju Goruganti 18837cce918SMarri Devender Rao json_response[ldapType].update(std::move(ldap)); 18954fc587aSNagaraju Goruganti 19054fc587aSNagaraju Goruganti nlohmann::json& roleMapArray = json_response[ldapType]["RemoteRoleMapping"]; 19154fc587aSNagaraju Goruganti roleMapArray = nlohmann::json::array(); 19254fc587aSNagaraju Goruganti for (auto& obj : confData.groupRoleList) 19354fc587aSNagaraju Goruganti { 19454fc587aSNagaraju Goruganti BMCWEB_LOG_DEBUG << "Pushing the data groupName=" 19554fc587aSNagaraju Goruganti << obj.second.groupName << "\n"; 19654fc587aSNagaraju Goruganti roleMapArray.push_back( 19754fc587aSNagaraju Goruganti {nlohmann::json::array({"RemoteGroup", obj.second.groupName}), 19854fc587aSNagaraju Goruganti nlohmann::json::array( 19954fc587aSNagaraju Goruganti {"LocalRole", getRoleIdFromPrivilege(obj.second.privilege)})}); 20054fc587aSNagaraju Goruganti } 2016973a582SRatan Gupta } 2026973a582SRatan Gupta 2036973a582SRatan Gupta /** 20406785244SRatan Gupta * @brief validates given JSON input and then calls appropriate method to 20506785244SRatan Gupta * create, to delete or to set Rolemapping object based on the given input. 20606785244SRatan Gupta * 20706785244SRatan Gupta */ 20806785244SRatan Gupta static void handleRoleMapPatch( 20906785244SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 21006785244SRatan Gupta const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData, 21106785244SRatan Gupta const std::string& serverType, std::vector<nlohmann::json>& input) 21206785244SRatan Gupta { 21306785244SRatan Gupta for (size_t index = 0; index < input.size(); index++) 21406785244SRatan Gupta { 21506785244SRatan Gupta nlohmann::json& thisJson = input[index]; 21606785244SRatan Gupta 21706785244SRatan Gupta if (thisJson.is_null()) 21806785244SRatan Gupta { 21906785244SRatan Gupta // delete the existing object 22006785244SRatan Gupta if (index < roleMapObjData.size()) 22106785244SRatan Gupta { 22206785244SRatan Gupta crow::connections::systemBus->async_method_call( 22306785244SRatan Gupta [asyncResp, roleMapObjData, serverType, 22406785244SRatan Gupta index](const boost::system::error_code ec) { 22506785244SRatan Gupta if (ec) 22606785244SRatan Gupta { 22706785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " << ec; 22806785244SRatan Gupta messages::internalError(asyncResp->res); 22906785244SRatan Gupta return; 23006785244SRatan Gupta } 23106785244SRatan Gupta asyncResp->res 23206785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"][index] = 23306785244SRatan Gupta nullptr; 23406785244SRatan Gupta }, 23506785244SRatan Gupta ldapDbusService, roleMapObjData[index].first, 23606785244SRatan Gupta "xyz.openbmc_project.Object.Delete", "Delete"); 23706785244SRatan Gupta } 23806785244SRatan Gupta else 23906785244SRatan Gupta { 24006785244SRatan Gupta BMCWEB_LOG_ERROR << "Can't delete the object"; 24106785244SRatan Gupta messages::propertyValueTypeError( 24206785244SRatan Gupta asyncResp->res, thisJson.dump(), 24306785244SRatan Gupta "RemoteRoleMapping/" + std::to_string(index)); 24406785244SRatan Gupta return; 24506785244SRatan Gupta } 24606785244SRatan Gupta } 24706785244SRatan Gupta else if (thisJson.empty()) 24806785244SRatan Gupta { 24906785244SRatan Gupta // Don't do anything for the empty objects,parse next json 25006785244SRatan Gupta // eg {"RemoteRoleMapping",[{}]} 25106785244SRatan Gupta } 25206785244SRatan Gupta else 25306785244SRatan Gupta { 25406785244SRatan Gupta // update/create the object 25506785244SRatan Gupta std::optional<std::string> remoteGroup; 25606785244SRatan Gupta std::optional<std::string> localRole; 25706785244SRatan Gupta 25806785244SRatan Gupta if (!json_util::readJson(thisJson, asyncResp->res, "RemoteGroup", 25906785244SRatan Gupta remoteGroup, "LocalRole", localRole)) 26006785244SRatan Gupta { 26106785244SRatan Gupta continue; 26206785244SRatan Gupta } 26306785244SRatan Gupta 26406785244SRatan Gupta // Update existing RoleMapping Object 26506785244SRatan Gupta if (index < roleMapObjData.size()) 26606785244SRatan Gupta { 26706785244SRatan Gupta BMCWEB_LOG_DEBUG << "Update Role Map Object"; 26806785244SRatan Gupta // If "RemoteGroup" info is provided 26906785244SRatan Gupta if (remoteGroup) 27006785244SRatan Gupta { 27106785244SRatan Gupta crow::connections::systemBus->async_method_call( 27206785244SRatan Gupta [asyncResp, roleMapObjData, serverType, index, 27306785244SRatan Gupta remoteGroup](const boost::system::error_code ec) { 27406785244SRatan Gupta if (ec) 27506785244SRatan Gupta { 27606785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " 27706785244SRatan Gupta << ec; 27806785244SRatan Gupta messages::internalError(asyncResp->res); 27906785244SRatan Gupta return; 28006785244SRatan Gupta } 28106785244SRatan Gupta asyncResp->res 28206785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"] 28306785244SRatan Gupta [index]["RemoteGroup"] = *remoteGroup; 28406785244SRatan Gupta }, 28506785244SRatan Gupta ldapDbusService, roleMapObjData[index].first, 28606785244SRatan Gupta propertyInterface, "Set", 28706785244SRatan Gupta "xyz.openbmc_project.User.PrivilegeMapperEntry", 28806785244SRatan Gupta "GroupName", 28906785244SRatan Gupta std::variant<std::string>(std::move(*remoteGroup))); 29006785244SRatan Gupta } 29106785244SRatan Gupta 29206785244SRatan Gupta // If "LocalRole" info is provided 29306785244SRatan Gupta if (localRole) 29406785244SRatan Gupta { 29506785244SRatan Gupta crow::connections::systemBus->async_method_call( 29606785244SRatan Gupta [asyncResp, roleMapObjData, serverType, index, 29706785244SRatan Gupta localRole](const boost::system::error_code ec) { 29806785244SRatan Gupta if (ec) 29906785244SRatan Gupta { 30006785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " 30106785244SRatan Gupta << ec; 30206785244SRatan Gupta messages::internalError(asyncResp->res); 30306785244SRatan Gupta return; 30406785244SRatan Gupta } 30506785244SRatan Gupta asyncResp->res 30606785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"] 30706785244SRatan Gupta [index]["LocalRole"] = *localRole; 30806785244SRatan Gupta }, 30906785244SRatan Gupta ldapDbusService, roleMapObjData[index].first, 31006785244SRatan Gupta propertyInterface, "Set", 31106785244SRatan Gupta "xyz.openbmc_project.User.PrivilegeMapperEntry", 31206785244SRatan Gupta "Privilege", 31306785244SRatan Gupta std::variant<std::string>( 31406785244SRatan Gupta getPrivilegeFromRoleId(std::move(*localRole)))); 31506785244SRatan Gupta } 31606785244SRatan Gupta } 31706785244SRatan Gupta // Create a new RoleMapping Object. 31806785244SRatan Gupta else 31906785244SRatan Gupta { 32006785244SRatan Gupta BMCWEB_LOG_DEBUG 32106785244SRatan Gupta << "setRoleMappingProperties: Creating new Object"; 32206785244SRatan Gupta std::string pathString = 32306785244SRatan Gupta "RemoteRoleMapping/" + std::to_string(index); 32406785244SRatan Gupta 32506785244SRatan Gupta if (!localRole) 32606785244SRatan Gupta { 32706785244SRatan Gupta messages::propertyMissing(asyncResp->res, 32806785244SRatan Gupta pathString + "/LocalRole"); 32906785244SRatan Gupta continue; 33006785244SRatan Gupta } 33106785244SRatan Gupta if (!remoteGroup) 33206785244SRatan Gupta { 33306785244SRatan Gupta messages::propertyMissing(asyncResp->res, 33406785244SRatan Gupta pathString + "/RemoteGroup"); 33506785244SRatan Gupta continue; 33606785244SRatan Gupta } 33706785244SRatan Gupta 33806785244SRatan Gupta std::string dbusObjectPath; 33906785244SRatan Gupta if (serverType == "ActiveDirectory") 34006785244SRatan Gupta { 34106785244SRatan Gupta dbusObjectPath = ADConfigObject; 34206785244SRatan Gupta } 34306785244SRatan Gupta else if (serverType == "LDAP") 34406785244SRatan Gupta { 34506785244SRatan Gupta dbusObjectPath = ldapConfigObject; 34606785244SRatan Gupta } 34706785244SRatan Gupta 34806785244SRatan Gupta BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup 34906785244SRatan Gupta << ",LocalRole=" << *localRole; 35006785244SRatan Gupta 35106785244SRatan Gupta crow::connections::systemBus->async_method_call( 352271584abSEd Tanous [asyncResp, serverType, localRole, 35306785244SRatan Gupta remoteGroup](const boost::system::error_code ec) { 35406785244SRatan Gupta if (ec) 35506785244SRatan Gupta { 35606785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " << ec; 35706785244SRatan Gupta messages::internalError(asyncResp->res); 35806785244SRatan Gupta return; 35906785244SRatan Gupta } 36006785244SRatan Gupta nlohmann::json& remoteRoleJson = 36106785244SRatan Gupta asyncResp->res 36206785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"]; 36306785244SRatan Gupta remoteRoleJson.push_back( 36406785244SRatan Gupta {{"LocalRole", *localRole}, 36506785244SRatan Gupta {"RemoteGroup", *remoteGroup}}); 36606785244SRatan Gupta }, 36706785244SRatan Gupta ldapDbusService, dbusObjectPath, ldapPrivMapperInterface, 36806785244SRatan Gupta "Create", std::move(*remoteGroup), 36906785244SRatan Gupta getPrivilegeFromRoleId(std::move(*localRole))); 37006785244SRatan Gupta } 37106785244SRatan Gupta } 37206785244SRatan Gupta } 37306785244SRatan Gupta } 37406785244SRatan Gupta 37506785244SRatan Gupta /** 3766973a582SRatan Gupta * Function that retrieves all properties for LDAP config object 3776973a582SRatan Gupta * into JSON 3786973a582SRatan Gupta */ 3796973a582SRatan Gupta template <typename CallbackFunc> 3806973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType, 3816973a582SRatan Gupta CallbackFunc&& callback) 3826973a582SRatan Gupta { 38354fc587aSNagaraju Goruganti 38454fc587aSNagaraju Goruganti const std::array<const char*, 2> interfaces = {ldapEnableInterface, 38554fc587aSNagaraju Goruganti ldapConfigInterface}; 38654fc587aSNagaraju Goruganti 38754fc587aSNagaraju Goruganti crow::connections::systemBus->async_method_call( 38854fc587aSNagaraju Goruganti [callback, ldapType](const boost::system::error_code ec, 38954fc587aSNagaraju Goruganti const GetObjectType& resp) { 39054fc587aSNagaraju Goruganti LDAPConfigData confData{}; 39154fc587aSNagaraju Goruganti if (ec || resp.empty()) 39254fc587aSNagaraju Goruganti { 39354fc587aSNagaraju Goruganti BMCWEB_LOG_ERROR << "DBUS response error during getting of " 39454fc587aSNagaraju Goruganti "service name: " 39554fc587aSNagaraju Goruganti << ec; 39654fc587aSNagaraju Goruganti callback(false, confData, ldapType); 39754fc587aSNagaraju Goruganti return; 39854fc587aSNagaraju Goruganti } 39954fc587aSNagaraju Goruganti std::string service = resp.begin()->first; 40054fc587aSNagaraju Goruganti crow::connections::systemBus->async_method_call( 40154fc587aSNagaraju Goruganti [callback, ldapType](const boost::system::error_code error_code, 4026973a582SRatan Gupta const ManagedObjectType& ldapObjects) { 4036973a582SRatan Gupta LDAPConfigData confData{}; 4046973a582SRatan Gupta if (error_code) 4056973a582SRatan Gupta { 406ab828d7cSRatan Gupta callback(false, confData, ldapType); 40754fc587aSNagaraju Goruganti BMCWEB_LOG_ERROR << "D-Bus responses error: " 40854fc587aSNagaraju Goruganti << error_code; 4096973a582SRatan Gupta return; 4106973a582SRatan Gupta } 411ab828d7cSRatan Gupta 412ab828d7cSRatan Gupta std::string ldapDbusType; 41354fc587aSNagaraju Goruganti std::string searchString; 41454fc587aSNagaraju Goruganti 415ab828d7cSRatan Gupta if (ldapType == "LDAP") 416ab828d7cSRatan Gupta { 41754fc587aSNagaraju Goruganti ldapDbusType = "xyz.openbmc_project.User.Ldap.Config." 41854fc587aSNagaraju Goruganti "Type.OpenLdap"; 41954fc587aSNagaraju Goruganti searchString = "openldap"; 420ab828d7cSRatan Gupta } 421ab828d7cSRatan Gupta else if (ldapType == "ActiveDirectory") 422ab828d7cSRatan Gupta { 42354fc587aSNagaraju Goruganti ldapDbusType = 42454fc587aSNagaraju Goruganti "xyz.openbmc_project.User.Ldap.Config.Type." 425ab828d7cSRatan Gupta "ActiveDirectory"; 42654fc587aSNagaraju Goruganti searchString = "active_directory"; 427ab828d7cSRatan Gupta } 428ab828d7cSRatan Gupta else 429ab828d7cSRatan Gupta { 43054fc587aSNagaraju Goruganti BMCWEB_LOG_ERROR 43154fc587aSNagaraju Goruganti << "Can't get the DbusType for the given type=" 432ab828d7cSRatan Gupta << ldapType; 433ab828d7cSRatan Gupta callback(false, confData, ldapType); 434ab828d7cSRatan Gupta return; 435ab828d7cSRatan Gupta } 436ab828d7cSRatan Gupta 437ab828d7cSRatan Gupta std::string ldapEnableInterfaceStr = ldapEnableInterface; 438ab828d7cSRatan Gupta std::string ldapConfigInterfaceStr = ldapConfigInterface; 439ab828d7cSRatan Gupta 4406973a582SRatan Gupta for (const auto& object : ldapObjects) 4416973a582SRatan Gupta { 44254fc587aSNagaraju Goruganti // let's find the object whose ldap type is equal to the 44354fc587aSNagaraju Goruganti // given type 44454fc587aSNagaraju Goruganti if (object.first.str.find(searchString) == 44554fc587aSNagaraju Goruganti std::string::npos) 4466973a582SRatan Gupta { 447ab828d7cSRatan Gupta continue; 448ab828d7cSRatan Gupta } 449ab828d7cSRatan Gupta 4506973a582SRatan Gupta for (const auto& interface : object.second) 4516973a582SRatan Gupta { 4526973a582SRatan Gupta if (interface.first == ldapEnableInterfaceStr) 4536973a582SRatan Gupta { 4546973a582SRatan Gupta // rest of the properties are string. 4556973a582SRatan Gupta for (const auto& property : interface.second) 4566973a582SRatan Gupta { 4576973a582SRatan Gupta if (property.first == "Enabled") 4586973a582SRatan Gupta { 4596973a582SRatan Gupta const bool* value = 4606973a582SRatan Gupta std::get_if<bool>(&property.second); 4616973a582SRatan Gupta if (value == nullptr) 4626973a582SRatan Gupta { 4636973a582SRatan Gupta continue; 4646973a582SRatan Gupta } 4656973a582SRatan Gupta confData.serviceEnabled = *value; 4666973a582SRatan Gupta break; 4676973a582SRatan Gupta } 4686973a582SRatan Gupta } 4696973a582SRatan Gupta } 4706973a582SRatan Gupta else if (interface.first == ldapConfigInterfaceStr) 4716973a582SRatan Gupta { 4726973a582SRatan Gupta 4736973a582SRatan Gupta for (const auto& property : interface.second) 4746973a582SRatan Gupta { 475271584abSEd Tanous const std::string* strValue = 47654fc587aSNagaraju Goruganti std::get_if<std::string>( 47754fc587aSNagaraju Goruganti &property.second); 478271584abSEd Tanous if (strValue == nullptr) 4796973a582SRatan Gupta { 4806973a582SRatan Gupta continue; 4816973a582SRatan Gupta } 4826973a582SRatan Gupta if (property.first == "LDAPServerURI") 4836973a582SRatan Gupta { 484271584abSEd Tanous confData.uri = *strValue; 4856973a582SRatan Gupta } 4866973a582SRatan Gupta else if (property.first == "LDAPBindDN") 4876973a582SRatan Gupta { 488271584abSEd Tanous confData.bindDN = *strValue; 4896973a582SRatan Gupta } 4906973a582SRatan Gupta else if (property.first == "LDAPBaseDN") 4916973a582SRatan Gupta { 492271584abSEd Tanous confData.baseDN = *strValue; 4936973a582SRatan Gupta } 49454fc587aSNagaraju Goruganti else if (property.first == 49554fc587aSNagaraju Goruganti "LDAPSearchScope") 4966973a582SRatan Gupta { 497271584abSEd Tanous confData.searchScope = *strValue; 4986973a582SRatan Gupta } 49954fc587aSNagaraju Goruganti else if (property.first == 50054fc587aSNagaraju Goruganti "GroupNameAttribute") 5016973a582SRatan Gupta { 502271584abSEd Tanous confData.groupAttribute = *strValue; 5036973a582SRatan Gupta } 50454fc587aSNagaraju Goruganti else if (property.first == 50554fc587aSNagaraju Goruganti "UserNameAttribute") 5066973a582SRatan Gupta { 507271584abSEd Tanous confData.userNameAttribute = *strValue; 5086973a582SRatan Gupta } 50954fc587aSNagaraju Goruganti else if (property.first == "LDAPType") 510ab828d7cSRatan Gupta { 511271584abSEd Tanous confData.serverType = *strValue; 51254fc587aSNagaraju Goruganti } 51354fc587aSNagaraju Goruganti } 51454fc587aSNagaraju Goruganti } 51554fc587aSNagaraju Goruganti else if (interface.first == 51654fc587aSNagaraju Goruganti "xyz.openbmc_project.User." 51754fc587aSNagaraju Goruganti "PrivilegeMapperEntry") 51854fc587aSNagaraju Goruganti { 51954fc587aSNagaraju Goruganti LDAPRoleMapData roleMapData{}; 52054fc587aSNagaraju Goruganti for (const auto& property : interface.second) 52154fc587aSNagaraju Goruganti { 522271584abSEd Tanous const std::string* strValue = 52354fc587aSNagaraju Goruganti std::get_if<std::string>( 52454fc587aSNagaraju Goruganti &property.second); 52554fc587aSNagaraju Goruganti 526271584abSEd Tanous if (strValue == nullptr) 52754fc587aSNagaraju Goruganti { 52854fc587aSNagaraju Goruganti continue; 52954fc587aSNagaraju Goruganti } 53054fc587aSNagaraju Goruganti 53154fc587aSNagaraju Goruganti if (property.first == "GroupName") 53254fc587aSNagaraju Goruganti { 533271584abSEd Tanous roleMapData.groupName = *strValue; 53454fc587aSNagaraju Goruganti } 53554fc587aSNagaraju Goruganti else if (property.first == "Privilege") 53654fc587aSNagaraju Goruganti { 537271584abSEd Tanous roleMapData.privilege = *strValue; 53854fc587aSNagaraju Goruganti } 53954fc587aSNagaraju Goruganti } 54054fc587aSNagaraju Goruganti 5410f0353b6SEd Tanous confData.groupRoleList.emplace_back( 5420f0353b6SEd Tanous object.first.str, roleMapData); 54354fc587aSNagaraju Goruganti } 54454fc587aSNagaraju Goruganti } 54554fc587aSNagaraju Goruganti } 546ab828d7cSRatan Gupta callback(true, confData, ldapType); 54754fc587aSNagaraju Goruganti }, 54854fc587aSNagaraju Goruganti service, ldapRootObject, dbusObjManagerIntf, 5496973a582SRatan Gupta "GetManagedObjects"); 55054fc587aSNagaraju Goruganti }, 55154fc587aSNagaraju Goruganti mapperBusName, mapperObjectPath, mapperIntf, "GetObject", 55254fc587aSNagaraju Goruganti ldapConfigObject, interfaces); 5536973a582SRatan Gupta } 5546973a582SRatan Gupta 5551abe55efSEd Tanous class AccountService : public Node 5561abe55efSEd Tanous { 55788d16c9aSLewanczyk, Dawid public: 55878158631SZbigniew Kurzynski AccountService(CrowApp& app) : 55978158631SZbigniew Kurzynski Node(app, "/redfish/v1/AccountService/"), app(app) 5601abe55efSEd Tanous { 5613ebd75f7SEd Tanous entityPrivileges = { 5623c5a376eSGunnar Mills {boost::beast::http::verb::get, {{"Login"}}}, 563e0d918bcSEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 564e0d918bcSEd Tanous {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, 565e0d918bcSEd Tanous {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, 566e0d918bcSEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, 567e0d918bcSEd Tanous {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; 56888d16c9aSLewanczyk, Dawid } 56988d16c9aSLewanczyk, Dawid 57088d16c9aSLewanczyk, Dawid private: 5718a07d286SRatan Gupta /** 5728a07d286SRatan Gupta * @brief parses the authentication section under the LDAP 5738a07d286SRatan Gupta * @param input JSON data 5748a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 5758a07d286SRatan Gupta * @param userName userName to be filled from the given JSON. 5768a07d286SRatan Gupta * @param password password to be filled from the given JSON. 5778a07d286SRatan Gupta */ 5788a07d286SRatan Gupta void 5798a07d286SRatan Gupta parseLDAPAuthenticationJson(nlohmann::json input, 5808a07d286SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 5818a07d286SRatan Gupta std::optional<std::string>& username, 5828a07d286SRatan Gupta std::optional<std::string>& password) 5838a07d286SRatan Gupta { 5848a07d286SRatan Gupta std::optional<std::string> authType; 5858a07d286SRatan Gupta 5868a07d286SRatan Gupta if (!json_util::readJson(input, asyncResp->res, "AuthenticationType", 5878a07d286SRatan Gupta authType, "Username", username, "Password", 5888a07d286SRatan Gupta password)) 5898a07d286SRatan Gupta { 5908a07d286SRatan Gupta return; 5918a07d286SRatan Gupta } 5928a07d286SRatan Gupta if (!authType) 5938a07d286SRatan Gupta { 5948a07d286SRatan Gupta return; 5958a07d286SRatan Gupta } 5968a07d286SRatan Gupta if (*authType != "UsernameAndPassword") 5978a07d286SRatan Gupta { 5988a07d286SRatan Gupta messages::propertyValueNotInList(asyncResp->res, *authType, 5998a07d286SRatan Gupta "AuthenticationType"); 6008a07d286SRatan Gupta return; 6018a07d286SRatan Gupta } 6028a07d286SRatan Gupta } 6038a07d286SRatan Gupta /** 6048a07d286SRatan Gupta * @brief parses the LDAPService section under the LDAP 6058a07d286SRatan Gupta * @param input JSON data 6068a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 6078a07d286SRatan Gupta * @param baseDNList baseDN to be filled from the given JSON. 6088a07d286SRatan Gupta * @param userNameAttribute userName to be filled from the given JSON. 6098a07d286SRatan Gupta * @param groupaAttribute password to be filled from the given JSON. 6108a07d286SRatan Gupta */ 6118a07d286SRatan Gupta 6128a07d286SRatan Gupta void parseLDAPServiceJson( 6138a07d286SRatan Gupta nlohmann::json input, const std::shared_ptr<AsyncResp>& asyncResp, 6148a07d286SRatan Gupta std::optional<std::vector<std::string>>& baseDNList, 6158a07d286SRatan Gupta std::optional<std::string>& userNameAttribute, 6168a07d286SRatan Gupta std::optional<std::string>& groupsAttribute) 6178a07d286SRatan Gupta { 6188a07d286SRatan Gupta std::optional<nlohmann::json> searchSettings; 6198a07d286SRatan Gupta 6208a07d286SRatan Gupta if (!json_util::readJson(input, asyncResp->res, "SearchSettings", 6218a07d286SRatan Gupta searchSettings)) 6228a07d286SRatan Gupta { 6238a07d286SRatan Gupta return; 6248a07d286SRatan Gupta } 6258a07d286SRatan Gupta if (!searchSettings) 6268a07d286SRatan Gupta { 6278a07d286SRatan Gupta return; 6288a07d286SRatan Gupta } 6298a07d286SRatan Gupta if (!json_util::readJson(*searchSettings, asyncResp->res, 6308a07d286SRatan Gupta "BaseDistinguishedNames", baseDNList, 6318a07d286SRatan Gupta "UsernameAttribute", userNameAttribute, 6328a07d286SRatan Gupta "GroupsAttribute", groupsAttribute)) 6338a07d286SRatan Gupta { 6348a07d286SRatan Gupta return; 6358a07d286SRatan Gupta } 6368a07d286SRatan Gupta } 6378a07d286SRatan Gupta /** 6388a07d286SRatan Gupta * @brief updates the LDAP server address and updates the 6398a07d286SRatan Gupta json response with the new value. 6408a07d286SRatan Gupta * @param serviceAddressList address to be updated. 6418a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 6428a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 6438a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 6448a07d286SRatan Gupta */ 6458a07d286SRatan Gupta 6468a07d286SRatan Gupta void handleServiceAddressPatch( 6478a07d286SRatan Gupta const std::vector<std::string>& serviceAddressList, 6488a07d286SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 6498a07d286SRatan Gupta const std::string& ldapServerElementName, 6508a07d286SRatan Gupta const std::string& ldapConfigObject) 6518a07d286SRatan Gupta { 6528a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 6538a07d286SRatan Gupta [asyncResp, ldapServerElementName, 6548a07d286SRatan Gupta serviceAddressList](const boost::system::error_code ec) { 6558a07d286SRatan Gupta if (ec) 6568a07d286SRatan Gupta { 6578a07d286SRatan Gupta BMCWEB_LOG_DEBUG 6588a07d286SRatan Gupta << "Error Occured in updating the service address"; 6598a07d286SRatan Gupta messages::internalError(asyncResp->res); 6608a07d286SRatan Gupta return; 6618a07d286SRatan Gupta } 6628a07d286SRatan Gupta std::vector<std::string> modifiedserviceAddressList = { 6638a07d286SRatan Gupta serviceAddressList.front()}; 6648a07d286SRatan Gupta asyncResp->res 6658a07d286SRatan Gupta .jsonValue[ldapServerElementName]["ServiceAddresses"] = 6668a07d286SRatan Gupta modifiedserviceAddressList; 6678a07d286SRatan Gupta if ((serviceAddressList).size() > 1) 6688a07d286SRatan Gupta { 6698a07d286SRatan Gupta messages::propertyValueModified(asyncResp->res, 6708a07d286SRatan Gupta "ServiceAddresses", 6718a07d286SRatan Gupta serviceAddressList.front()); 6728a07d286SRatan Gupta } 6738a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the service address"; 6748a07d286SRatan Gupta }, 6758a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 6768a07d286SRatan Gupta ldapConfigInterface, "LDAPServerURI", 6778a07d286SRatan Gupta std::variant<std::string>(serviceAddressList.front())); 6788a07d286SRatan Gupta } 6798a07d286SRatan Gupta /** 6808a07d286SRatan Gupta * @brief updates the LDAP Bind DN and updates the 6818a07d286SRatan Gupta json response with the new value. 6828a07d286SRatan Gupta * @param username name of the user which needs to be updated. 6838a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 6848a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 6858a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 6868a07d286SRatan Gupta */ 6878a07d286SRatan Gupta 6888a07d286SRatan Gupta void handleUserNamePatch(const std::string& username, 6898a07d286SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 6908a07d286SRatan Gupta const std::string& ldapServerElementName, 6918a07d286SRatan Gupta const std::string& ldapConfigObject) 6928a07d286SRatan Gupta { 6938a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 6948a07d286SRatan Gupta [asyncResp, username, 6958a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 6968a07d286SRatan Gupta if (ec) 6978a07d286SRatan Gupta { 6988a07d286SRatan Gupta BMCWEB_LOG_DEBUG 6998a07d286SRatan Gupta << "Error occured in updating the username"; 7008a07d286SRatan Gupta messages::internalError(asyncResp->res); 7018a07d286SRatan Gupta return; 7028a07d286SRatan Gupta } 7038a07d286SRatan Gupta asyncResp->res.jsonValue[ldapServerElementName] 7048a07d286SRatan Gupta ["Authentication"]["Username"] = 7058a07d286SRatan Gupta username; 7068a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the username"; 7078a07d286SRatan Gupta }, 7088a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 7098a07d286SRatan Gupta ldapConfigInterface, "LDAPBindDN", 7108a07d286SRatan Gupta std::variant<std::string>(username)); 7118a07d286SRatan Gupta } 7128a07d286SRatan Gupta 7138a07d286SRatan Gupta /** 7148a07d286SRatan Gupta * @brief updates the LDAP password 7158a07d286SRatan Gupta * @param password : ldap password which needs to be updated. 7168a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 7178a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 7188a07d286SRatan Gupta * server(openLDAP/ActiveDirectory) 7198a07d286SRatan Gupta */ 7208a07d286SRatan Gupta 7218a07d286SRatan Gupta void handlePasswordPatch(const std::string& password, 7228a07d286SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 7238a07d286SRatan Gupta const std::string& ldapServerElementName, 7248a07d286SRatan Gupta const std::string& ldapConfigObject) 7258a07d286SRatan Gupta { 7268a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 7278a07d286SRatan Gupta [asyncResp, password, 7288a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 7298a07d286SRatan Gupta if (ec) 7308a07d286SRatan Gupta { 7318a07d286SRatan Gupta BMCWEB_LOG_DEBUG 7328a07d286SRatan Gupta << "Error occured in updating the password"; 7338a07d286SRatan Gupta messages::internalError(asyncResp->res); 7348a07d286SRatan Gupta return; 7358a07d286SRatan Gupta } 7368a07d286SRatan Gupta asyncResp->res.jsonValue[ldapServerElementName] 7378a07d286SRatan Gupta ["Authentication"]["Password"] = ""; 7388a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the password"; 7398a07d286SRatan Gupta }, 7408a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 7418a07d286SRatan Gupta ldapConfigInterface, "LDAPBindDNPassword", 7428a07d286SRatan Gupta std::variant<std::string>(password)); 7438a07d286SRatan Gupta } 7448a07d286SRatan Gupta 7458a07d286SRatan Gupta /** 7468a07d286SRatan Gupta * @brief updates the LDAP BaseDN and updates the 7478a07d286SRatan Gupta json response with the new value. 7488a07d286SRatan Gupta * @param baseDNList baseDN list which needs to be updated. 7498a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 7508a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 7518a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 7528a07d286SRatan Gupta */ 7538a07d286SRatan Gupta 7548a07d286SRatan Gupta void handleBaseDNPatch(const std::vector<std::string>& baseDNList, 7558a07d286SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 7568a07d286SRatan Gupta const std::string& ldapServerElementName, 7578a07d286SRatan Gupta const std::string& ldapConfigObject) 7588a07d286SRatan Gupta { 7598a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 7608a07d286SRatan Gupta [asyncResp, baseDNList, 7618a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 7628a07d286SRatan Gupta if (ec) 7638a07d286SRatan Gupta { 7648a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Error Occured in Updating the base DN"; 7658a07d286SRatan Gupta messages::internalError(asyncResp->res); 7668a07d286SRatan Gupta return; 7678a07d286SRatan Gupta } 7688a07d286SRatan Gupta auto& serverTypeJson = 7698a07d286SRatan Gupta asyncResp->res.jsonValue[ldapServerElementName]; 7708a07d286SRatan Gupta auto& searchSettingsJson = 7718a07d286SRatan Gupta serverTypeJson["LDAPService"]["SearchSettings"]; 7728a07d286SRatan Gupta std::vector<std::string> modifiedBaseDNList = { 7738a07d286SRatan Gupta baseDNList.front()}; 7748a07d286SRatan Gupta searchSettingsJson["BaseDistinguishedNames"] = 7758a07d286SRatan Gupta modifiedBaseDNList; 7768a07d286SRatan Gupta if (baseDNList.size() > 1) 7778a07d286SRatan Gupta { 7788a07d286SRatan Gupta messages::propertyValueModified(asyncResp->res, 7798a07d286SRatan Gupta "BaseDistinguishedNames", 7808a07d286SRatan Gupta baseDNList.front()); 7818a07d286SRatan Gupta } 7828a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the base DN"; 7838a07d286SRatan Gupta }, 7848a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 7858a07d286SRatan Gupta ldapConfigInterface, "LDAPBaseDN", 7868a07d286SRatan Gupta std::variant<std::string>(baseDNList.front())); 7878a07d286SRatan Gupta } 7888a07d286SRatan Gupta /** 7898a07d286SRatan Gupta * @brief updates the LDAP user name attribute and updates the 7908a07d286SRatan Gupta json response with the new value. 7918a07d286SRatan Gupta * @param userNameAttribute attribute to be updated. 7928a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 7938a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 7948a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 7958a07d286SRatan Gupta */ 7968a07d286SRatan Gupta 7978a07d286SRatan Gupta void handleUserNameAttrPatch(const std::string& userNameAttribute, 7988a07d286SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 7998a07d286SRatan Gupta const std::string& ldapServerElementName, 8008a07d286SRatan Gupta const std::string& ldapConfigObject) 8018a07d286SRatan Gupta { 8028a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 8038a07d286SRatan Gupta [asyncResp, userNameAttribute, 8048a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 8058a07d286SRatan Gupta if (ec) 8068a07d286SRatan Gupta { 8078a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Error Occured in Updating the " 8088a07d286SRatan Gupta "username attribute"; 8098a07d286SRatan Gupta messages::internalError(asyncResp->res); 8108a07d286SRatan Gupta return; 8118a07d286SRatan Gupta } 8128a07d286SRatan Gupta auto& serverTypeJson = 8138a07d286SRatan Gupta asyncResp->res.jsonValue[ldapServerElementName]; 8148a07d286SRatan Gupta auto& searchSettingsJson = 8158a07d286SRatan Gupta serverTypeJson["LDAPService"]["SearchSettings"]; 8168a07d286SRatan Gupta searchSettingsJson["UsernameAttribute"] = userNameAttribute; 8178a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the user name attr."; 8188a07d286SRatan Gupta }, 8198a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 8208a07d286SRatan Gupta ldapConfigInterface, "UserNameAttribute", 8218a07d286SRatan Gupta std::variant<std::string>(userNameAttribute)); 8228a07d286SRatan Gupta } 8238a07d286SRatan Gupta /** 8248a07d286SRatan Gupta * @brief updates the LDAP group attribute and updates the 8258a07d286SRatan Gupta json response with the new value. 8268a07d286SRatan Gupta * @param groupsAttribute attribute to be updated. 8278a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 8288a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 8298a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 8308a07d286SRatan Gupta */ 8318a07d286SRatan Gupta 8328a07d286SRatan Gupta void handleGroupNameAttrPatch(const std::string& groupsAttribute, 8338a07d286SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 8348a07d286SRatan Gupta const std::string& ldapServerElementName, 8358a07d286SRatan Gupta const std::string& ldapConfigObject) 8368a07d286SRatan Gupta { 8378a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 8388a07d286SRatan Gupta [asyncResp, groupsAttribute, 8398a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 8408a07d286SRatan Gupta if (ec) 8418a07d286SRatan Gupta { 8428a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Error Occured in Updating the " 8438a07d286SRatan Gupta "groupname attribute"; 8448a07d286SRatan Gupta messages::internalError(asyncResp->res); 8458a07d286SRatan Gupta return; 8468a07d286SRatan Gupta } 8478a07d286SRatan Gupta auto& serverTypeJson = 8488a07d286SRatan Gupta asyncResp->res.jsonValue[ldapServerElementName]; 8498a07d286SRatan Gupta auto& searchSettingsJson = 8508a07d286SRatan Gupta serverTypeJson["LDAPService"]["SearchSettings"]; 8518a07d286SRatan Gupta searchSettingsJson["GroupsAttribute"] = groupsAttribute; 8528a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the groupname attr"; 8538a07d286SRatan Gupta }, 8548a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 8558a07d286SRatan Gupta ldapConfigInterface, "GroupNameAttribute", 8568a07d286SRatan Gupta std::variant<std::string>(groupsAttribute)); 8578a07d286SRatan Gupta } 8588a07d286SRatan Gupta /** 8598a07d286SRatan Gupta * @brief updates the LDAP service enable and updates the 8608a07d286SRatan Gupta json response with the new value. 8618a07d286SRatan Gupta * @param input JSON data. 8628a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 8638a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 8648a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 8658a07d286SRatan Gupta */ 8668a07d286SRatan Gupta 8678a07d286SRatan Gupta void handleServiceEnablePatch(bool serviceEnabled, 8688a07d286SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 8698a07d286SRatan Gupta const std::string& ldapServerElementName, 8708a07d286SRatan Gupta const std::string& ldapConfigObject) 8718a07d286SRatan Gupta { 8728a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 8738a07d286SRatan Gupta [asyncResp, serviceEnabled, 8748a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 8758a07d286SRatan Gupta if (ec) 8768a07d286SRatan Gupta { 8778a07d286SRatan Gupta BMCWEB_LOG_DEBUG 8788a07d286SRatan Gupta << "Error Occured in Updating the service enable"; 8798a07d286SRatan Gupta messages::internalError(asyncResp->res); 8808a07d286SRatan Gupta return; 8818a07d286SRatan Gupta } 8828a07d286SRatan Gupta asyncResp->res 8838a07d286SRatan Gupta .jsonValue[ldapServerElementName]["ServiceEnabled"] = 8848a07d286SRatan Gupta serviceEnabled; 8858a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated Service enable = " 8868a07d286SRatan Gupta << serviceEnabled; 8878a07d286SRatan Gupta }, 8888a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 8898a07d286SRatan Gupta ldapEnableInterface, "Enabled", std::variant<bool>(serviceEnabled)); 8908a07d286SRatan Gupta } 8918a07d286SRatan Gupta 89278158631SZbigniew Kurzynski void handleAuthMethodsPatch(nlohmann::json& input, 89378158631SZbigniew Kurzynski const std::shared_ptr<AsyncResp>& asyncResp) 89478158631SZbigniew Kurzynski { 89578158631SZbigniew Kurzynski std::optional<bool> basicAuth; 89678158631SZbigniew Kurzynski std::optional<bool> cookie; 89778158631SZbigniew Kurzynski std::optional<bool> sessionToken; 89878158631SZbigniew Kurzynski std::optional<bool> xToken; 899501f1e58SZbigniew Kurzynski std::optional<bool> tls; 90078158631SZbigniew Kurzynski 90178158631SZbigniew Kurzynski if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth, 90278158631SZbigniew Kurzynski "Cookie", cookie, "SessionToken", sessionToken, 903501f1e58SZbigniew Kurzynski "XToken", xToken, "TLS", tls)) 90478158631SZbigniew Kurzynski { 90578158631SZbigniew Kurzynski BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag"; 90678158631SZbigniew Kurzynski return; 90778158631SZbigniew Kurzynski } 90878158631SZbigniew Kurzynski 90978158631SZbigniew Kurzynski // Make a copy of methods configuration 91078158631SZbigniew Kurzynski crow::persistent_data::AuthConfigMethods authMethodsConfig = 91178158631SZbigniew Kurzynski crow::persistent_data::SessionStore::getInstance() 91278158631SZbigniew Kurzynski .getAuthMethodsConfig(); 91378158631SZbigniew Kurzynski 91478158631SZbigniew Kurzynski if (basicAuth) 91578158631SZbigniew Kurzynski { 91678158631SZbigniew Kurzynski authMethodsConfig.basic = *basicAuth; 91778158631SZbigniew Kurzynski } 91878158631SZbigniew Kurzynski 91978158631SZbigniew Kurzynski if (cookie) 92078158631SZbigniew Kurzynski { 92178158631SZbigniew Kurzynski authMethodsConfig.cookie = *cookie; 92278158631SZbigniew Kurzynski } 92378158631SZbigniew Kurzynski 92478158631SZbigniew Kurzynski if (sessionToken) 92578158631SZbigniew Kurzynski { 92678158631SZbigniew Kurzynski authMethodsConfig.sessionToken = *sessionToken; 92778158631SZbigniew Kurzynski } 92878158631SZbigniew Kurzynski 92978158631SZbigniew Kurzynski if (xToken) 93078158631SZbigniew Kurzynski { 93178158631SZbigniew Kurzynski authMethodsConfig.xtoken = *xToken; 93278158631SZbigniew Kurzynski } 93378158631SZbigniew Kurzynski 934501f1e58SZbigniew Kurzynski if (tls) 935501f1e58SZbigniew Kurzynski { 936501f1e58SZbigniew Kurzynski authMethodsConfig.tls = *tls; 937501f1e58SZbigniew Kurzynski } 938501f1e58SZbigniew Kurzynski 93978158631SZbigniew Kurzynski if (!authMethodsConfig.basic && !authMethodsConfig.cookie && 940501f1e58SZbigniew Kurzynski !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken && 941501f1e58SZbigniew Kurzynski !authMethodsConfig.tls) 94278158631SZbigniew Kurzynski { 94378158631SZbigniew Kurzynski // Do not allow user to disable everything 94478158631SZbigniew Kurzynski messages::actionNotSupported(asyncResp->res, 94578158631SZbigniew Kurzynski "of disabling all available methods"); 94678158631SZbigniew Kurzynski return; 94778158631SZbigniew Kurzynski } 94878158631SZbigniew Kurzynski 94978158631SZbigniew Kurzynski crow::persistent_data::SessionStore::getInstance() 95078158631SZbigniew Kurzynski .updateAuthMethodsConfig(authMethodsConfig); 95178158631SZbigniew Kurzynski // Save configuration immediately 95278158631SZbigniew Kurzynski app.template getMiddleware<crow::persistent_data::Middleware>() 95378158631SZbigniew Kurzynski .writeData(); 95478158631SZbigniew Kurzynski 95578158631SZbigniew Kurzynski messages::success(asyncResp->res); 95678158631SZbigniew Kurzynski } 95778158631SZbigniew Kurzynski 9588a07d286SRatan Gupta /** 9598a07d286SRatan Gupta * @brief Get the required values from the given JSON, validates the 9608a07d286SRatan Gupta * value and create the LDAP config object. 9618a07d286SRatan Gupta * @param input JSON data 9628a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 9638a07d286SRatan Gupta * @param serverType Type of LDAP server(openLDAP/ActiveDirectory) 9648a07d286SRatan Gupta */ 9658a07d286SRatan Gupta 9668a07d286SRatan Gupta void handleLDAPPatch(nlohmann::json& input, 9678a07d286SRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp, 9688a07d286SRatan Gupta const crow::Request& req, 9698a07d286SRatan Gupta const std::vector<std::string>& params, 9708a07d286SRatan Gupta const std::string& serverType) 9718a07d286SRatan Gupta { 972eb2bbe56SRatan Gupta std::string dbusObjectPath; 973eb2bbe56SRatan Gupta if (serverType == "ActiveDirectory") 974eb2bbe56SRatan Gupta { 975eb2bbe56SRatan Gupta dbusObjectPath = ADConfigObject; 976eb2bbe56SRatan Gupta } 977eb2bbe56SRatan Gupta else if (serverType == "LDAP") 978eb2bbe56SRatan Gupta { 979eb2bbe56SRatan Gupta dbusObjectPath = ldapConfigObject; 980eb2bbe56SRatan Gupta } 981eb2bbe56SRatan Gupta 9828a07d286SRatan Gupta std::optional<nlohmann::json> authentication; 9838a07d286SRatan Gupta std::optional<nlohmann::json> ldapService; 9848a07d286SRatan Gupta std::optional<std::vector<std::string>> serviceAddressList; 9858a07d286SRatan Gupta std::optional<bool> serviceEnabled; 9868a07d286SRatan Gupta std::optional<std::vector<std::string>> baseDNList; 9878a07d286SRatan Gupta std::optional<std::string> userNameAttribute; 9888a07d286SRatan Gupta std::optional<std::string> groupsAttribute; 9898a07d286SRatan Gupta std::optional<std::string> userName; 9908a07d286SRatan Gupta std::optional<std::string> password; 99106785244SRatan Gupta std::optional<std::vector<nlohmann::json>> remoteRoleMapData; 9928a07d286SRatan Gupta 9938a07d286SRatan Gupta if (!json_util::readJson(input, asyncResp->res, "Authentication", 9948a07d286SRatan Gupta authentication, "LDAPService", ldapService, 9958a07d286SRatan Gupta "ServiceAddresses", serviceAddressList, 99606785244SRatan Gupta "ServiceEnabled", serviceEnabled, 99706785244SRatan Gupta "RemoteRoleMapping", remoteRoleMapData)) 9988a07d286SRatan Gupta { 9998a07d286SRatan Gupta return; 10008a07d286SRatan Gupta } 10018a07d286SRatan Gupta 10028a07d286SRatan Gupta if (authentication) 10038a07d286SRatan Gupta { 10048a07d286SRatan Gupta parseLDAPAuthenticationJson(*authentication, asyncResp, userName, 10058a07d286SRatan Gupta password); 10068a07d286SRatan Gupta } 10078a07d286SRatan Gupta if (ldapService) 10088a07d286SRatan Gupta { 10098a07d286SRatan Gupta parseLDAPServiceJson(*ldapService, asyncResp, baseDNList, 10108a07d286SRatan Gupta userNameAttribute, groupsAttribute); 10118a07d286SRatan Gupta } 10128a07d286SRatan Gupta if (serviceAddressList) 10138a07d286SRatan Gupta { 10148a07d286SRatan Gupta if ((*serviceAddressList).size() == 0) 10158a07d286SRatan Gupta { 10168a07d286SRatan Gupta messages::propertyValueNotInList(asyncResp->res, "[]", 10178a07d286SRatan Gupta "ServiceAddress"); 10188a07d286SRatan Gupta return; 10198a07d286SRatan Gupta } 10208a07d286SRatan Gupta } 10218a07d286SRatan Gupta if (baseDNList) 10228a07d286SRatan Gupta { 10238a07d286SRatan Gupta if ((*baseDNList).size() == 0) 10248a07d286SRatan Gupta { 10258a07d286SRatan Gupta messages::propertyValueNotInList(asyncResp->res, "[]", 10268a07d286SRatan Gupta "BaseDistinguishedNames"); 10278a07d286SRatan Gupta return; 10288a07d286SRatan Gupta } 10298a07d286SRatan Gupta } 10308a07d286SRatan Gupta 10318a07d286SRatan Gupta // nothing to update, then return 10328a07d286SRatan Gupta if (!userName && !password && !serviceAddressList && !baseDNList && 103306785244SRatan Gupta !userNameAttribute && !groupsAttribute && !serviceEnabled && 103406785244SRatan Gupta !remoteRoleMapData) 10358a07d286SRatan Gupta { 10368a07d286SRatan Gupta return; 10378a07d286SRatan Gupta } 10388a07d286SRatan Gupta 10398a07d286SRatan Gupta // Get the existing resource first then keep modifying 10408a07d286SRatan Gupta // whenever any property gets updated. 1041ab828d7cSRatan Gupta getLDAPConfigData(serverType, [this, asyncResp, userName, password, 1042ab828d7cSRatan Gupta baseDNList, userNameAttribute, 1043*ba9dd4a8SGunnar Mills groupsAttribute, serviceAddressList, 1044*ba9dd4a8SGunnar Mills serviceEnabled, dbusObjectPath, 1045*ba9dd4a8SGunnar Mills remoteRoleMapData]( 1046ab828d7cSRatan Gupta bool success, LDAPConfigData confData, 1047ab828d7cSRatan Gupta const std::string& serverType) { 10488a07d286SRatan Gupta if (!success) 10498a07d286SRatan Gupta { 10508a07d286SRatan Gupta messages::internalError(asyncResp->res); 10518a07d286SRatan Gupta return; 10528a07d286SRatan Gupta } 1053ab828d7cSRatan Gupta parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverType); 10548a07d286SRatan Gupta if (confData.serviceEnabled) 10558a07d286SRatan Gupta { 10568a07d286SRatan Gupta // Disable the service first and update the rest of 10578a07d286SRatan Gupta // the properties. 10588a07d286SRatan Gupta handleServiceEnablePatch(false, asyncResp, serverType, 1059eb2bbe56SRatan Gupta dbusObjectPath); 10608a07d286SRatan Gupta } 10618a07d286SRatan Gupta 10628a07d286SRatan Gupta if (serviceAddressList) 10638a07d286SRatan Gupta { 10648a07d286SRatan Gupta handleServiceAddressPatch(*serviceAddressList, asyncResp, 1065eb2bbe56SRatan Gupta serverType, dbusObjectPath); 10668a07d286SRatan Gupta } 10678a07d286SRatan Gupta if (userName) 10688a07d286SRatan Gupta { 10698a07d286SRatan Gupta handleUserNamePatch(*userName, asyncResp, serverType, 1070eb2bbe56SRatan Gupta dbusObjectPath); 10718a07d286SRatan Gupta } 10728a07d286SRatan Gupta if (password) 10738a07d286SRatan Gupta { 10748a07d286SRatan Gupta handlePasswordPatch(*password, asyncResp, serverType, 1075eb2bbe56SRatan Gupta dbusObjectPath); 10768a07d286SRatan Gupta } 10778a07d286SRatan Gupta 10788a07d286SRatan Gupta if (baseDNList) 10798a07d286SRatan Gupta { 10808a07d286SRatan Gupta handleBaseDNPatch(*baseDNList, asyncResp, serverType, 1081eb2bbe56SRatan Gupta dbusObjectPath); 10828a07d286SRatan Gupta } 10838a07d286SRatan Gupta if (userNameAttribute) 10848a07d286SRatan Gupta { 10858a07d286SRatan Gupta handleUserNameAttrPatch(*userNameAttribute, asyncResp, 1086eb2bbe56SRatan Gupta serverType, dbusObjectPath); 10878a07d286SRatan Gupta } 10888a07d286SRatan Gupta if (groupsAttribute) 10898a07d286SRatan Gupta { 10908a07d286SRatan Gupta handleGroupNameAttrPatch(*groupsAttribute, asyncResp, 1091eb2bbe56SRatan Gupta serverType, dbusObjectPath); 10928a07d286SRatan Gupta } 10938a07d286SRatan Gupta if (serviceEnabled) 10948a07d286SRatan Gupta { 10958a07d286SRatan Gupta // if user has given the value as true then enable 10968a07d286SRatan Gupta // the service. if user has given false then no-op 10978a07d286SRatan Gupta // as service is already stopped. 10988a07d286SRatan Gupta if (*serviceEnabled) 10998a07d286SRatan Gupta { 11008a07d286SRatan Gupta handleServiceEnablePatch(*serviceEnabled, asyncResp, 1101eb2bbe56SRatan Gupta serverType, dbusObjectPath); 11028a07d286SRatan Gupta } 11038a07d286SRatan Gupta } 11048a07d286SRatan Gupta else 11058a07d286SRatan Gupta { 11068a07d286SRatan Gupta // if user has not given the service enabled value 11078a07d286SRatan Gupta // then revert it to the same state as it was 11088a07d286SRatan Gupta // before. 11098a07d286SRatan Gupta handleServiceEnablePatch(confData.serviceEnabled, asyncResp, 1110eb2bbe56SRatan Gupta serverType, dbusObjectPath); 11118a07d286SRatan Gupta } 111206785244SRatan Gupta 111306785244SRatan Gupta if (remoteRoleMapData) 111406785244SRatan Gupta { 111506785244SRatan Gupta std::vector<nlohmann::json> remoteRoleMap = 111606785244SRatan Gupta std::move(*remoteRoleMapData); 111706785244SRatan Gupta 111806785244SRatan Gupta handleRoleMapPatch(asyncResp, confData.groupRoleList, 111906785244SRatan Gupta serverType, remoteRoleMap); 112006785244SRatan Gupta } 11218a07d286SRatan Gupta }); 11228a07d286SRatan Gupta } 1123d4b5443fSEd Tanous 112455c7b7a2SEd Tanous void doGet(crow::Response& res, const crow::Request& req, 11251abe55efSEd Tanous const std::vector<std::string>& params) override 11261abe55efSEd Tanous { 112778158631SZbigniew Kurzynski const crow::persistent_data::AuthConfigMethods& authMethodsConfig = 112878158631SZbigniew Kurzynski crow::persistent_data::SessionStore::getInstance() 112978158631SZbigniew Kurzynski .getAuthMethodsConfig(); 113078158631SZbigniew Kurzynski 11313d958bbcSAppaRao Puli auto asyncResp = std::make_shared<AsyncResp>(res); 11323d958bbcSAppaRao Puli res.jsonValue = { 11333d958bbcSAppaRao Puli {"@odata.id", "/redfish/v1/AccountService"}, 11343d958bbcSAppaRao Puli {"@odata.type", "#AccountService." 1135*ba9dd4a8SGunnar Mills "v1_5_0.AccountService"}, 11363d958bbcSAppaRao Puli {"Id", "AccountService"}, 11373d958bbcSAppaRao Puli {"Name", "Account Service"}, 11383d958bbcSAppaRao Puli {"Description", "Account Service"}, 11393d958bbcSAppaRao Puli {"ServiceEnabled", true}, 1140343ff2e1SAppaRao Puli {"MaxPasswordLength", 20}, 11413d958bbcSAppaRao Puli {"Accounts", 11423d958bbcSAppaRao Puli {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}}, 114337cce918SMarri Devender Rao {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}}, 114478158631SZbigniew Kurzynski {"Oem", 114578158631SZbigniew Kurzynski {{"OpenBMC", 114678158631SZbigniew Kurzynski {{"@odata.type", "#OemAccountService.v1_0_0.AccountService"}, 114778158631SZbigniew Kurzynski {"AuthMethods", 114878158631SZbigniew Kurzynski { 114978158631SZbigniew Kurzynski {"BasicAuth", authMethodsConfig.basic}, 115078158631SZbigniew Kurzynski {"SessionToken", authMethodsConfig.sessionToken}, 115178158631SZbigniew Kurzynski {"XToken", authMethodsConfig.xtoken}, 115278158631SZbigniew Kurzynski {"Cookie", authMethodsConfig.cookie}, 1153501f1e58SZbigniew Kurzynski {"TLS", authMethodsConfig.tls}, 115478158631SZbigniew Kurzynski }}}}}}, 115537cce918SMarri Devender Rao {"LDAP", 115637cce918SMarri Devender Rao {{"Certificates", 115737cce918SMarri Devender Rao {{"@odata.id", 115837cce918SMarri Devender Rao "/redfish/v1/AccountService/LDAP/Certificates"}}}}}}; 11593d958bbcSAppaRao Puli crow::connections::systemBus->async_method_call( 11603d958bbcSAppaRao Puli [asyncResp]( 11613d958bbcSAppaRao Puli const boost::system::error_code ec, 11623d958bbcSAppaRao Puli const std::vector<std::pair< 1163abf2add6SEd Tanous std::string, std::variant<uint32_t, uint16_t, uint8_t>>>& 11643d958bbcSAppaRao Puli propertiesList) { 11653d958bbcSAppaRao Puli if (ec) 11663d958bbcSAppaRao Puli { 11673d958bbcSAppaRao Puli messages::internalError(asyncResp->res); 11683d958bbcSAppaRao Puli return; 11693d958bbcSAppaRao Puli } 11703d958bbcSAppaRao Puli BMCWEB_LOG_DEBUG << "Got " << propertiesList.size() 11713d958bbcSAppaRao Puli << "properties for AccountService"; 11723d958bbcSAppaRao Puli for (const std::pair<std::string, 1173abf2add6SEd Tanous std::variant<uint32_t, uint16_t, uint8_t>>& 11743d958bbcSAppaRao Puli property : propertiesList) 11753d958bbcSAppaRao Puli { 11763d958bbcSAppaRao Puli if (property.first == "MinPasswordLength") 11773d958bbcSAppaRao Puli { 11783d958bbcSAppaRao Puli const uint8_t* value = 1179abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 11803d958bbcSAppaRao Puli if (value != nullptr) 11813d958bbcSAppaRao Puli { 11823d958bbcSAppaRao Puli asyncResp->res.jsonValue["MinPasswordLength"] = 11833d958bbcSAppaRao Puli *value; 11843d958bbcSAppaRao Puli } 11853d958bbcSAppaRao Puli } 11863d958bbcSAppaRao Puli if (property.first == "AccountUnlockTimeout") 11873d958bbcSAppaRao Puli { 11883d958bbcSAppaRao Puli const uint32_t* value = 1189abf2add6SEd Tanous std::get_if<uint32_t>(&property.second); 11903d958bbcSAppaRao Puli if (value != nullptr) 11913d958bbcSAppaRao Puli { 11923d958bbcSAppaRao Puli asyncResp->res.jsonValue["AccountLockoutDuration"] = 11933d958bbcSAppaRao Puli *value; 11943d958bbcSAppaRao Puli } 11953d958bbcSAppaRao Puli } 11963d958bbcSAppaRao Puli if (property.first == "MaxLoginAttemptBeforeLockout") 11973d958bbcSAppaRao Puli { 11983d958bbcSAppaRao Puli const uint16_t* value = 1199abf2add6SEd Tanous std::get_if<uint16_t>(&property.second); 12003d958bbcSAppaRao Puli if (value != nullptr) 12013d958bbcSAppaRao Puli { 12023d958bbcSAppaRao Puli asyncResp->res 12033d958bbcSAppaRao Puli .jsonValue["AccountLockoutThreshold"] = *value; 12043d958bbcSAppaRao Puli } 12053d958bbcSAppaRao Puli } 12063d958bbcSAppaRao Puli } 12073d958bbcSAppaRao Puli }, 12083d958bbcSAppaRao Puli "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 12093d958bbcSAppaRao Puli "org.freedesktop.DBus.Properties", "GetAll", 12103d958bbcSAppaRao Puli "xyz.openbmc_project.User.AccountPolicy"); 12116973a582SRatan Gupta 1212ab828d7cSRatan Gupta auto callback = [asyncResp](bool success, LDAPConfigData& confData, 1213ab828d7cSRatan Gupta const std::string& ldapType) { 1214ab828d7cSRatan Gupta parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType); 1215ab828d7cSRatan Gupta }; 1216ab828d7cSRatan Gupta 1217ab828d7cSRatan Gupta getLDAPConfigData("LDAP", callback); 1218ab828d7cSRatan Gupta getLDAPConfigData("ActiveDirectory", callback); 12193d958bbcSAppaRao Puli } 12206973a582SRatan Gupta 12213d958bbcSAppaRao Puli void doPatch(crow::Response& res, const crow::Request& req, 12223d958bbcSAppaRao Puli const std::vector<std::string>& params) override 12233d958bbcSAppaRao Puli { 12243d958bbcSAppaRao Puli auto asyncResp = std::make_shared<AsyncResp>(res); 12253d958bbcSAppaRao Puli 12263d958bbcSAppaRao Puli std::optional<uint32_t> unlockTimeout; 12273d958bbcSAppaRao Puli std::optional<uint16_t> lockoutThreshold; 122819fb6e71SRatan Gupta std::optional<uint16_t> minPasswordLength; 122919fb6e71SRatan Gupta std::optional<uint16_t> maxPasswordLength; 12308a07d286SRatan Gupta std::optional<nlohmann::json> ldapObject; 1231eb2bbe56SRatan Gupta std::optional<nlohmann::json> activeDirectoryObject; 123278158631SZbigniew Kurzynski std::optional<nlohmann::json> oemObject; 123319fb6e71SRatan Gupta 123478158631SZbigniew Kurzynski if (!json_util::readJson( 123578158631SZbigniew Kurzynski req, res, "AccountLockoutDuration", unlockTimeout, 123678158631SZbigniew Kurzynski "AccountLockoutThreshold", lockoutThreshold, 123778158631SZbigniew Kurzynski "MaxPasswordLength", maxPasswordLength, "MinPasswordLength", 123878158631SZbigniew Kurzynski minPasswordLength, "LDAP", ldapObject, "ActiveDirectory", 123978158631SZbigniew Kurzynski activeDirectoryObject, "Oem", oemObject)) 12403d958bbcSAppaRao Puli { 12413d958bbcSAppaRao Puli return; 12423d958bbcSAppaRao Puli } 124319fb6e71SRatan Gupta 124419fb6e71SRatan Gupta if (minPasswordLength) 124519fb6e71SRatan Gupta { 124619fb6e71SRatan Gupta messages::propertyNotWritable(asyncResp->res, "MinPasswordLength"); 124719fb6e71SRatan Gupta } 124819fb6e71SRatan Gupta 124919fb6e71SRatan Gupta if (maxPasswordLength) 125019fb6e71SRatan Gupta { 125119fb6e71SRatan Gupta messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength"); 125219fb6e71SRatan Gupta } 125319fb6e71SRatan Gupta 12548a07d286SRatan Gupta if (ldapObject) 12558a07d286SRatan Gupta { 12568a07d286SRatan Gupta handleLDAPPatch(*ldapObject, asyncResp, req, params, "LDAP"); 12578a07d286SRatan Gupta } 12588a07d286SRatan Gupta 125978158631SZbigniew Kurzynski if (std::optional<nlohmann::json> oemOpenBMCObject; 126078158631SZbigniew Kurzynski oemObject && 126178158631SZbigniew Kurzynski json_util::readJson(*oemObject, res, "OpenBMC", oemOpenBMCObject)) 126278158631SZbigniew Kurzynski { 126378158631SZbigniew Kurzynski if (std::optional<nlohmann::json> authMethodsObject; 126478158631SZbigniew Kurzynski oemOpenBMCObject && 126578158631SZbigniew Kurzynski json_util::readJson(*oemOpenBMCObject, res, "AuthMethods", 126678158631SZbigniew Kurzynski authMethodsObject)) 126778158631SZbigniew Kurzynski { 126878158631SZbigniew Kurzynski if (authMethodsObject) 126978158631SZbigniew Kurzynski { 127078158631SZbigniew Kurzynski handleAuthMethodsPatch(*authMethodsObject, asyncResp); 127178158631SZbigniew Kurzynski } 127278158631SZbigniew Kurzynski } 127378158631SZbigniew Kurzynski } 127478158631SZbigniew Kurzynski 1275eb2bbe56SRatan Gupta if (activeDirectoryObject) 1276eb2bbe56SRatan Gupta { 1277eb2bbe56SRatan Gupta handleLDAPPatch(*activeDirectoryObject, asyncResp, req, params, 1278eb2bbe56SRatan Gupta "ActiveDirectory"); 1279eb2bbe56SRatan Gupta } 1280eb2bbe56SRatan Gupta 12813d958bbcSAppaRao Puli if (unlockTimeout) 12823d958bbcSAppaRao Puli { 12833d958bbcSAppaRao Puli crow::connections::systemBus->async_method_call( 12843d958bbcSAppaRao Puli [asyncResp](const boost::system::error_code ec) { 12853d958bbcSAppaRao Puli if (ec) 12863d958bbcSAppaRao Puli { 12873d958bbcSAppaRao Puli messages::internalError(asyncResp->res); 12883d958bbcSAppaRao Puli return; 12893d958bbcSAppaRao Puli } 1290add6133bSRatan Gupta messages::success(asyncResp->res); 12913d958bbcSAppaRao Puli }, 12923d958bbcSAppaRao Puli "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 12933d958bbcSAppaRao Puli "org.freedesktop.DBus.Properties", "Set", 12943d958bbcSAppaRao Puli "xyz.openbmc_project.User.AccountPolicy", 1295abf2add6SEd Tanous "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout)); 12963d958bbcSAppaRao Puli } 12973d958bbcSAppaRao Puli if (lockoutThreshold) 12983d958bbcSAppaRao Puli { 12993d958bbcSAppaRao Puli crow::connections::systemBus->async_method_call( 13003d958bbcSAppaRao Puli [asyncResp](const boost::system::error_code ec) { 13013d958bbcSAppaRao Puli if (ec) 13023d958bbcSAppaRao Puli { 13033d958bbcSAppaRao Puli messages::internalError(asyncResp->res); 13043d958bbcSAppaRao Puli return; 13053d958bbcSAppaRao Puli } 1306add6133bSRatan Gupta messages::success(asyncResp->res); 13073d958bbcSAppaRao Puli }, 13083d958bbcSAppaRao Puli "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 13093d958bbcSAppaRao Puli "org.freedesktop.DBus.Properties", "Set", 13103d958bbcSAppaRao Puli "xyz.openbmc_project.User.AccountPolicy", 13113d958bbcSAppaRao Puli "MaxLoginAttemptBeforeLockout", 1312abf2add6SEd Tanous std::variant<uint16_t>(*lockoutThreshold)); 13133d958bbcSAppaRao Puli } 131488d16c9aSLewanczyk, Dawid } 131578158631SZbigniew Kurzynski 131678158631SZbigniew Kurzynski CrowApp& app; 131788d16c9aSLewanczyk, Dawid }; 1318f00032dbSTanous 1319b9b2e0b2SEd Tanous class AccountsCollection : public Node 1320b9b2e0b2SEd Tanous { 1321b9b2e0b2SEd Tanous public: 1322b9b2e0b2SEd Tanous AccountsCollection(CrowApp& app) : 1323b9b2e0b2SEd Tanous Node(app, "/redfish/v1/AccountService/Accounts/") 1324b9b2e0b2SEd Tanous { 1325b9b2e0b2SEd Tanous entityPrivileges = { 1326b9b2e0b2SEd Tanous {boost::beast::http::verb::get, 1327b9b2e0b2SEd Tanous {{"ConfigureUsers"}, {"ConfigureManager"}}}, 1328b9b2e0b2SEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 1329b9b2e0b2SEd Tanous {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, 1330b9b2e0b2SEd Tanous {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, 1331b9b2e0b2SEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, 1332b9b2e0b2SEd Tanous {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; 1333b9b2e0b2SEd Tanous } 1334b9b2e0b2SEd Tanous 1335b9b2e0b2SEd Tanous private: 1336b9b2e0b2SEd Tanous void doGet(crow::Response& res, const crow::Request& req, 1337b9b2e0b2SEd Tanous const std::vector<std::string>& params) override 1338b9b2e0b2SEd Tanous { 1339b9b2e0b2SEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 134094400960SGunnar Mills res.jsonValue = {{"@odata.id", "/redfish/v1/AccountService/Accounts"}, 13410f74e643SEd Tanous {"@odata.type", "#ManagerAccountCollection." 13420f74e643SEd Tanous "ManagerAccountCollection"}, 13430f74e643SEd Tanous {"Name", "Accounts Collection"}, 13440f74e643SEd Tanous {"Description", "BMC User Accounts"}}; 13450f74e643SEd Tanous 1346b9b2e0b2SEd Tanous crow::connections::systemBus->async_method_call( 1347b9b2e0b2SEd Tanous [asyncResp](const boost::system::error_code ec, 1348b9b2e0b2SEd Tanous const ManagedObjectType& users) { 1349b9b2e0b2SEd Tanous if (ec) 1350b9b2e0b2SEd Tanous { 1351f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1352b9b2e0b2SEd Tanous return; 1353b9b2e0b2SEd Tanous } 1354b9b2e0b2SEd Tanous 1355b9b2e0b2SEd Tanous nlohmann::json& memberArray = 1356b9b2e0b2SEd Tanous asyncResp->res.jsonValue["Members"]; 1357b9b2e0b2SEd Tanous memberArray = nlohmann::json::array(); 1358b9b2e0b2SEd Tanous 1359b9b2e0b2SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = users.size(); 1360b9b2e0b2SEd Tanous for (auto& user : users) 1361b9b2e0b2SEd Tanous { 1362b9b2e0b2SEd Tanous const std::string& path = 1363b9b2e0b2SEd Tanous static_cast<const std::string&>(user.first); 1364b9b2e0b2SEd Tanous std::size_t lastIndex = path.rfind("/"); 1365b9b2e0b2SEd Tanous if (lastIndex == std::string::npos) 1366b9b2e0b2SEd Tanous { 1367b9b2e0b2SEd Tanous lastIndex = 0; 1368b9b2e0b2SEd Tanous } 1369b9b2e0b2SEd Tanous else 1370b9b2e0b2SEd Tanous { 1371b9b2e0b2SEd Tanous lastIndex += 1; 1372b9b2e0b2SEd Tanous } 1373b9b2e0b2SEd Tanous memberArray.push_back( 1374b9b2e0b2SEd Tanous {{"@odata.id", "/redfish/v1/AccountService/Accounts/" + 1375b9b2e0b2SEd Tanous path.substr(lastIndex)}}); 1376b9b2e0b2SEd Tanous } 1377b9b2e0b2SEd Tanous }, 1378b9b2e0b2SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1379b9b2e0b2SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 1380b9b2e0b2SEd Tanous } 138104ae99ecSEd Tanous void doPost(crow::Response& res, const crow::Request& req, 138204ae99ecSEd Tanous const std::vector<std::string>& params) override 138304ae99ecSEd Tanous { 138404ae99ecSEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 138504ae99ecSEd Tanous 13869712f8acSEd Tanous std::string username; 13879712f8acSEd Tanous std::string password; 1388a24526dcSEd Tanous std::optional<std::string> roleId("User"); 1389a24526dcSEd Tanous std::optional<bool> enabled = true; 13909712f8acSEd Tanous if (!json_util::readJson(req, res, "UserName", username, "Password", 13919712f8acSEd Tanous password, "RoleId", roleId, "Enabled", 13929712f8acSEd Tanous enabled)) 139304ae99ecSEd Tanous { 139404ae99ecSEd Tanous return; 139504ae99ecSEd Tanous } 139604ae99ecSEd Tanous 139754fc587aSNagaraju Goruganti std::string priv = getPrivilegeFromRoleId(*roleId); 139884e12cb7SAppaRao Puli if (priv.empty()) 139904ae99ecSEd Tanous { 1400f12894f8SJason M. Bills messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId"); 140104ae99ecSEd Tanous return; 140204ae99ecSEd Tanous } 14039712f8acSEd Tanous roleId = priv; 140404ae99ecSEd Tanous 1405599c71d8SAyushi Smriti // Reading AllGroups property 1406599c71d8SAyushi Smriti crow::connections::systemBus->async_method_call( 1407599c71d8SAyushi Smriti [asyncResp, username, password{std::move(password)}, roleId, 1408599c71d8SAyushi Smriti enabled](const boost::system::error_code ec, 1409599c71d8SAyushi Smriti const std::variant<std::vector<std::string>>& allGroups) { 1410599c71d8SAyushi Smriti if (ec) 1411599c71d8SAyushi Smriti { 1412599c71d8SAyushi Smriti BMCWEB_LOG_DEBUG << "ERROR with async_method_call"; 1413599c71d8SAyushi Smriti messages::internalError(asyncResp->res); 1414599c71d8SAyushi Smriti return; 1415599c71d8SAyushi Smriti } 1416599c71d8SAyushi Smriti 1417599c71d8SAyushi Smriti const std::vector<std::string>* allGroupsList = 1418599c71d8SAyushi Smriti std::get_if<std::vector<std::string>>(&allGroups); 1419599c71d8SAyushi Smriti 1420599c71d8SAyushi Smriti if (allGroupsList == nullptr || allGroupsList->empty()) 1421599c71d8SAyushi Smriti { 1422599c71d8SAyushi Smriti messages::internalError(asyncResp->res); 1423599c71d8SAyushi Smriti return; 1424599c71d8SAyushi Smriti } 1425599c71d8SAyushi Smriti 142604ae99ecSEd Tanous crow::connections::systemBus->async_method_call( 14279712f8acSEd Tanous [asyncResp, username, password{std::move(password)}]( 14280d4197e0Sanil kumar appana const boost::system::error_code ec, 14290d4197e0Sanil kumar appana sdbusplus::message::message& m) { 143004ae99ecSEd Tanous if (ec) 143104ae99ecSEd Tanous { 14320d4197e0Sanil kumar appana userErrorMessageHandler(m.get_error(), asyncResp, 14330d4197e0Sanil kumar appana username, ""); 143404ae99ecSEd Tanous return; 143504ae99ecSEd Tanous } 143604ae99ecSEd Tanous 143766b5ca76Sjayaprakash Mutyala if (pamUpdatePassword(username, password) != 143866b5ca76Sjayaprakash Mutyala PAM_SUCCESS) 143904ae99ecSEd Tanous { 1440599c71d8SAyushi Smriti // At this point we have a user that's been created, 1441599c71d8SAyushi Smriti // but the password set failed.Something is wrong, 1442599c71d8SAyushi Smriti // so delete the user that we've already created 144304ae99ecSEd Tanous crow::connections::systemBus->async_method_call( 14440d4197e0Sanil kumar appana [asyncResp, 14450d4197e0Sanil kumar appana password](const boost::system::error_code ec) { 144604ae99ecSEd Tanous if (ec) 144704ae99ecSEd Tanous { 1448f12894f8SJason M. Bills messages::internalError(asyncResp->res); 144904ae99ecSEd Tanous return; 145004ae99ecSEd Tanous } 145104ae99ecSEd Tanous 14520d4197e0Sanil kumar appana // If password is invalid 14530d4197e0Sanil kumar appana messages::propertyValueFormatError( 14540d4197e0Sanil kumar appana asyncResp->res, password, "Password"); 145504ae99ecSEd Tanous }, 145604ae99ecSEd Tanous "xyz.openbmc_project.User.Manager", 145704ae99ecSEd Tanous "/xyz/openbmc_project/user/" + username, 145804ae99ecSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 145904ae99ecSEd Tanous 146004ae99ecSEd Tanous BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; 146104ae99ecSEd Tanous return; 146204ae99ecSEd Tanous } 146304ae99ecSEd Tanous 1464f12894f8SJason M. Bills messages::created(asyncResp->res); 146504ae99ecSEd Tanous asyncResp->res.addHeader( 146604ae99ecSEd Tanous "Location", 146704ae99ecSEd Tanous "/redfish/v1/AccountService/Accounts/" + username); 146804ae99ecSEd Tanous }, 1469599c71d8SAyushi Smriti "xyz.openbmc_project.User.Manager", 1470599c71d8SAyushi Smriti "/xyz/openbmc_project/user", 14719712f8acSEd Tanous "xyz.openbmc_project.User.Manager", "CreateUser", username, 1472599c71d8SAyushi Smriti *allGroupsList, *roleId, *enabled); 1473599c71d8SAyushi Smriti }, 1474599c71d8SAyushi Smriti "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1475599c71d8SAyushi Smriti "org.freedesktop.DBus.Properties", "Get", 1476599c71d8SAyushi Smriti "xyz.openbmc_project.User.Manager", "AllGroups"); 147704ae99ecSEd Tanous } 1478b9b2e0b2SEd Tanous }; 1479b9b2e0b2SEd Tanous 1480b9b2e0b2SEd Tanous class ManagerAccount : public Node 1481b9b2e0b2SEd Tanous { 1482b9b2e0b2SEd Tanous public: 1483b9b2e0b2SEd Tanous ManagerAccount(CrowApp& app) : 1484b9b2e0b2SEd Tanous Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string()) 1485b9b2e0b2SEd Tanous { 1486b9b2e0b2SEd Tanous entityPrivileges = { 1487b9b2e0b2SEd Tanous {boost::beast::http::verb::get, 1488b9b2e0b2SEd Tanous {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}}, 1489b9b2e0b2SEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 1490900f9497SJoseph Reynolds {boost::beast::http::verb::patch, 1491900f9497SJoseph Reynolds {{"ConfigureUsers"}, {"ConfigureSelf"}}}, 1492b9b2e0b2SEd Tanous {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, 1493b9b2e0b2SEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, 1494b9b2e0b2SEd Tanous {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; 1495b9b2e0b2SEd Tanous } 1496b9b2e0b2SEd Tanous 1497b9b2e0b2SEd Tanous private: 1498b9b2e0b2SEd Tanous void doGet(crow::Response& res, const crow::Request& req, 1499b9b2e0b2SEd Tanous const std::vector<std::string>& params) override 1500b9b2e0b2SEd Tanous { 1501b9b2e0b2SEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 1502b9b2e0b2SEd Tanous 1503b9b2e0b2SEd Tanous if (params.size() != 1) 1504b9b2e0b2SEd Tanous { 1505f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1506b9b2e0b2SEd Tanous return; 1507b9b2e0b2SEd Tanous } 1508b9b2e0b2SEd Tanous 1509900f9497SJoseph Reynolds // Perform a proper ConfigureSelf authority check. If the 1510900f9497SJoseph Reynolds // user is operating on an account not their own, then their 1511900f9497SJoseph Reynolds // ConfigureSelf privilege does not apply. In this case, 1512900f9497SJoseph Reynolds // perform the authority check again without the user's 1513900f9497SJoseph Reynolds // ConfigureSelf privilege. 1514900f9497SJoseph Reynolds if (req.session->username != params[0]) 1515900f9497SJoseph Reynolds { 1516900f9497SJoseph Reynolds if (!isAllowedWithoutConfigureSelf(req)) 1517900f9497SJoseph Reynolds { 1518900f9497SJoseph Reynolds BMCWEB_LOG_DEBUG << "GET Account denied access"; 1519900f9497SJoseph Reynolds messages::insufficientPrivilege(asyncResp->res); 1520900f9497SJoseph Reynolds return; 1521900f9497SJoseph Reynolds } 1522900f9497SJoseph Reynolds } 1523900f9497SJoseph Reynolds 1524b9b2e0b2SEd Tanous crow::connections::systemBus->async_method_call( 1525b9b2e0b2SEd Tanous [asyncResp, accountName{std::string(params[0])}]( 1526b9b2e0b2SEd Tanous const boost::system::error_code ec, 1527b9b2e0b2SEd Tanous const ManagedObjectType& users) { 1528b9b2e0b2SEd Tanous if (ec) 1529b9b2e0b2SEd Tanous { 1530f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1531b9b2e0b2SEd Tanous return; 1532b9b2e0b2SEd Tanous } 153384e12cb7SAppaRao Puli auto userIt = users.begin(); 1534b9b2e0b2SEd Tanous 153584e12cb7SAppaRao Puli for (; userIt != users.end(); userIt++) 1536b9b2e0b2SEd Tanous { 153784e12cb7SAppaRao Puli if (boost::ends_with(userIt->first.str, "/" + accountName)) 1538b9b2e0b2SEd Tanous { 153984e12cb7SAppaRao Puli break; 1540b9b2e0b2SEd Tanous } 1541b9b2e0b2SEd Tanous } 154284e12cb7SAppaRao Puli if (userIt == users.end()) 1543b9b2e0b2SEd Tanous { 154484e12cb7SAppaRao Puli messages::resourceNotFound(asyncResp->res, "ManagerAccount", 154584e12cb7SAppaRao Puli accountName); 154684e12cb7SAppaRao Puli return; 154784e12cb7SAppaRao Puli } 15484e68c45bSAyushi Smriti 15494e68c45bSAyushi Smriti asyncResp->res.jsonValue = { 15504e68c45bSAyushi Smriti {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"}, 15514e68c45bSAyushi Smriti {"Name", "User Account"}, 15524e68c45bSAyushi Smriti {"Description", "User Account"}, 15534e68c45bSAyushi Smriti {"Password", nullptr}}; 15544e68c45bSAyushi Smriti 155584e12cb7SAppaRao Puli for (const auto& interface : userIt->second) 155665b0dc32SEd Tanous { 155765b0dc32SEd Tanous if (interface.first == 155865b0dc32SEd Tanous "xyz.openbmc_project.User.Attributes") 155965b0dc32SEd Tanous { 156065b0dc32SEd Tanous for (const auto& property : interface.second) 156165b0dc32SEd Tanous { 156265b0dc32SEd Tanous if (property.first == "UserEnabled") 156365b0dc32SEd Tanous { 156465b0dc32SEd Tanous const bool* userEnabled = 1565abf2add6SEd Tanous std::get_if<bool>(&property.second); 156665b0dc32SEd Tanous if (userEnabled == nullptr) 156765b0dc32SEd Tanous { 156865b0dc32SEd Tanous BMCWEB_LOG_ERROR 156965b0dc32SEd Tanous << "UserEnabled wasn't a bool"; 157084e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 157184e12cb7SAppaRao Puli return; 157265b0dc32SEd Tanous } 157365b0dc32SEd Tanous asyncResp->res.jsonValue["Enabled"] = 157465b0dc32SEd Tanous *userEnabled; 157565b0dc32SEd Tanous } 157665b0dc32SEd Tanous else if (property.first == 157765b0dc32SEd Tanous "UserLockedForFailedAttempt") 157865b0dc32SEd Tanous { 157965b0dc32SEd Tanous const bool* userLocked = 1580abf2add6SEd Tanous std::get_if<bool>(&property.second); 158165b0dc32SEd Tanous if (userLocked == nullptr) 158265b0dc32SEd Tanous { 158384e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "UserLockedForF" 158484e12cb7SAppaRao Puli "ailedAttempt " 158584e12cb7SAppaRao Puli "wasn't a bool"; 158684e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 158784e12cb7SAppaRao Puli return; 158865b0dc32SEd Tanous } 158965b0dc32SEd Tanous asyncResp->res.jsonValue["Locked"] = 159065b0dc32SEd Tanous *userLocked; 159124c8542dSRatan Gupta asyncResp->res.jsonValue 159224c8542dSRatan Gupta ["Locked@Redfish.AllowableValues"] = { 15934d64ce34SGunnar Mills "false"}; 159465b0dc32SEd Tanous } 159584e12cb7SAppaRao Puli else if (property.first == "UserPrivilege") 159684e12cb7SAppaRao Puli { 159754fc587aSNagaraju Goruganti const std::string* userPrivPtr = 1598abf2add6SEd Tanous std::get_if<std::string>(&property.second); 159954fc587aSNagaraju Goruganti if (userPrivPtr == nullptr) 160084e12cb7SAppaRao Puli { 160184e12cb7SAppaRao Puli BMCWEB_LOG_ERROR 160284e12cb7SAppaRao Puli << "UserPrivilege wasn't a " 160384e12cb7SAppaRao Puli "string"; 160484e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 160584e12cb7SAppaRao Puli return; 160684e12cb7SAppaRao Puli } 160754fc587aSNagaraju Goruganti std::string role = 160854fc587aSNagaraju Goruganti getRoleIdFromPrivilege(*userPrivPtr); 160954fc587aSNagaraju Goruganti if (role.empty()) 161084e12cb7SAppaRao Puli { 161184e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "Invalid user role"; 161284e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 161384e12cb7SAppaRao Puli return; 161484e12cb7SAppaRao Puli } 161554fc587aSNagaraju Goruganti asyncResp->res.jsonValue["RoleId"] = role; 161684e12cb7SAppaRao Puli 161784e12cb7SAppaRao Puli asyncResp->res.jsonValue["Links"]["Role"] = { 161884e12cb7SAppaRao Puli {"@odata.id", "/redfish/v1/AccountService/" 161984e12cb7SAppaRao Puli "Roles/" + 162054fc587aSNagaraju Goruganti role}}; 162184e12cb7SAppaRao Puli } 162265b0dc32SEd Tanous } 162365b0dc32SEd Tanous } 162465b0dc32SEd Tanous } 162565b0dc32SEd Tanous 1626b9b2e0b2SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 162784e12cb7SAppaRao Puli "/redfish/v1/AccountService/Accounts/" + accountName; 1628b9b2e0b2SEd Tanous asyncResp->res.jsonValue["Id"] = accountName; 1629b9b2e0b2SEd Tanous asyncResp->res.jsonValue["UserName"] = accountName; 1630b9b2e0b2SEd Tanous }, 1631b9b2e0b2SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1632b9b2e0b2SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 1633b9b2e0b2SEd Tanous } 1634a840879dSEd Tanous 1635a840879dSEd Tanous void doPatch(crow::Response& res, const crow::Request& req, 1636a840879dSEd Tanous const std::vector<std::string>& params) override 1637a840879dSEd Tanous { 1638a840879dSEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 1639a840879dSEd Tanous if (params.size() != 1) 1640a840879dSEd Tanous { 1641f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1642a840879dSEd Tanous return; 1643a840879dSEd Tanous } 1644a840879dSEd Tanous 1645a24526dcSEd Tanous std::optional<std::string> newUserName; 1646a24526dcSEd Tanous std::optional<std::string> password; 1647a24526dcSEd Tanous std::optional<bool> enabled; 1648a24526dcSEd Tanous std::optional<std::string> roleId; 164924c8542dSRatan Gupta std::optional<bool> locked; 165084e12cb7SAppaRao Puli if (!json_util::readJson(req, res, "UserName", newUserName, "Password", 165124c8542dSRatan Gupta password, "RoleId", roleId, "Enabled", enabled, 165224c8542dSRatan Gupta "Locked", locked)) 1653a840879dSEd Tanous { 1654a840879dSEd Tanous return; 1655a840879dSEd Tanous } 1656a840879dSEd Tanous 165784e12cb7SAppaRao Puli const std::string& username = params[0]; 165884e12cb7SAppaRao Puli 1659900f9497SJoseph Reynolds // Perform a proper ConfigureSelf authority check. If the 1660900f9497SJoseph Reynolds // session is being used to PATCH a property other than 1661900f9497SJoseph Reynolds // Password, then the ConfigureSelf privilege does not apply. 1662900f9497SJoseph Reynolds // If the user is operating on an account not their own, then 1663900f9497SJoseph Reynolds // their ConfigureSelf privilege does not apply. In either 1664900f9497SJoseph Reynolds // case, perform the authority check again without the user's 1665900f9497SJoseph Reynolds // ConfigureSelf privilege. 1666900f9497SJoseph Reynolds if ((username != req.session->username) || 1667900f9497SJoseph Reynolds (newUserName || enabled || roleId || locked)) 1668900f9497SJoseph Reynolds { 1669900f9497SJoseph Reynolds if (!isAllowedWithoutConfigureSelf(req)) 1670900f9497SJoseph Reynolds { 1671900f9497SJoseph Reynolds BMCWEB_LOG_WARNING << "PATCH Password denied access"; 1672900f9497SJoseph Reynolds asyncResp->res.clear(); 1673900f9497SJoseph Reynolds messages::insufficientPrivilege(asyncResp->res); 1674900f9497SJoseph Reynolds return; 1675900f9497SJoseph Reynolds } 1676900f9497SJoseph Reynolds } 1677900f9497SJoseph Reynolds 167866b5ca76Sjayaprakash Mutyala // if user name is not provided in the patch method or if it 167966b5ca76Sjayaprakash Mutyala // matches the user name in the URI, then we are treating it as updating 168066b5ca76Sjayaprakash Mutyala // user properties other then username. If username provided doesn't 168166b5ca76Sjayaprakash Mutyala // match the URI, then we are treating this as user rename request. 168266b5ca76Sjayaprakash Mutyala if (!newUserName || (newUserName.value() == username)) 1683a840879dSEd Tanous { 168424c8542dSRatan Gupta updateUserProperties(asyncResp, username, password, enabled, roleId, 168524c8542dSRatan Gupta locked); 168684e12cb7SAppaRao Puli return; 168784e12cb7SAppaRao Puli } 168884e12cb7SAppaRao Puli else 168984e12cb7SAppaRao Puli { 169084e12cb7SAppaRao Puli crow::connections::systemBus->async_method_call( 169184e12cb7SAppaRao Puli [this, asyncResp, username, password(std::move(password)), 169284e12cb7SAppaRao Puli roleId(std::move(roleId)), enabled(std::move(enabled)), 169366b5ca76Sjayaprakash Mutyala newUser{std::string(*newUserName)}, 169466b5ca76Sjayaprakash Mutyala locked(std::move(locked))](const boost::system::error_code ec, 169566b5ca76Sjayaprakash Mutyala sdbusplus::message::message& m) { 169684e12cb7SAppaRao Puli if (ec) 169784e12cb7SAppaRao Puli { 169866b5ca76Sjayaprakash Mutyala userErrorMessageHandler(m.get_error(), asyncResp, 169966b5ca76Sjayaprakash Mutyala newUser, username); 1700a840879dSEd Tanous return; 1701a840879dSEd Tanous } 1702a840879dSEd Tanous 170384e12cb7SAppaRao Puli updateUserProperties(asyncResp, newUser, password, enabled, 170424c8542dSRatan Gupta roleId, locked); 170584e12cb7SAppaRao Puli }, 170684e12cb7SAppaRao Puli "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 170784e12cb7SAppaRao Puli "xyz.openbmc_project.User.Manager", "RenameUser", username, 170884e12cb7SAppaRao Puli *newUserName); 170984e12cb7SAppaRao Puli } 171084e12cb7SAppaRao Puli } 171184e12cb7SAppaRao Puli 171284e12cb7SAppaRao Puli void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp, 171384e12cb7SAppaRao Puli const std::string& username, 1714a24526dcSEd Tanous std::optional<std::string> password, 1715a24526dcSEd Tanous std::optional<bool> enabled, 171624c8542dSRatan Gupta std::optional<std::string> roleId, 171724c8542dSRatan Gupta std::optional<bool> locked) 171884e12cb7SAppaRao Puli { 171924c8542dSRatan Gupta std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username; 172024c8542dSRatan Gupta dbus::utility::escapePathForDbus(dbusObjectPath); 172124c8542dSRatan Gupta 172222c33710SRatan Gupta dbus::utility::checkDbusPathExists( 172324c8542dSRatan Gupta dbusObjectPath, 172424c8542dSRatan Gupta [dbusObjectPath(std::move(dbusObjectPath)), username, 172524c8542dSRatan Gupta password(std::move(password)), roleId(std::move(roleId)), 172624c8542dSRatan Gupta enabled(std::move(enabled)), locked(std::move(locked)), 172724c8542dSRatan Gupta asyncResp{std::move(asyncResp)}](int rc) { 172824c8542dSRatan Gupta if (!rc) 172924c8542dSRatan Gupta { 173066b5ca76Sjayaprakash Mutyala messages::resourceNotFound( 173166b5ca76Sjayaprakash Mutyala asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", 173266b5ca76Sjayaprakash Mutyala username); 173324c8542dSRatan Gupta return; 173424c8542dSRatan Gupta } 173566b5ca76Sjayaprakash Mutyala 173666b5ca76Sjayaprakash Mutyala if (password) 173766b5ca76Sjayaprakash Mutyala { 173866b5ca76Sjayaprakash Mutyala int retval = pamUpdatePassword(username, *password); 173966b5ca76Sjayaprakash Mutyala 174066b5ca76Sjayaprakash Mutyala if (retval == PAM_USER_UNKNOWN) 174166b5ca76Sjayaprakash Mutyala { 174266b5ca76Sjayaprakash Mutyala messages::resourceNotFound( 174366b5ca76Sjayaprakash Mutyala asyncResp->res, 174466b5ca76Sjayaprakash Mutyala "#ManagerAccount.v1_0_3.ManagerAccount", username); 174566b5ca76Sjayaprakash Mutyala } 174666b5ca76Sjayaprakash Mutyala else if (retval == PAM_AUTHTOK_ERR) 174766b5ca76Sjayaprakash Mutyala { 174866b5ca76Sjayaprakash Mutyala // If password is invalid 174966b5ca76Sjayaprakash Mutyala messages::propertyValueFormatError( 175066b5ca76Sjayaprakash Mutyala asyncResp->res, *password, "Password"); 175166b5ca76Sjayaprakash Mutyala BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; 175266b5ca76Sjayaprakash Mutyala } 175366b5ca76Sjayaprakash Mutyala else if (retval != PAM_SUCCESS) 175466b5ca76Sjayaprakash Mutyala { 175566b5ca76Sjayaprakash Mutyala messages::internalError(asyncResp->res); 175666b5ca76Sjayaprakash Mutyala return; 175766b5ca76Sjayaprakash Mutyala } 175866b5ca76Sjayaprakash Mutyala } 175966b5ca76Sjayaprakash Mutyala 17609712f8acSEd Tanous if (enabled) 1761a840879dSEd Tanous { 1762a840879dSEd Tanous crow::connections::systemBus->async_method_call( 1763a840879dSEd Tanous [asyncResp](const boost::system::error_code ec) { 1764a840879dSEd Tanous if (ec) 1765a840879dSEd Tanous { 176624c8542dSRatan Gupta BMCWEB_LOG_ERROR << "D-Bus responses error: " 176724c8542dSRatan Gupta << ec; 1768f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1769a840879dSEd Tanous return; 1770a840879dSEd Tanous } 177184e12cb7SAppaRao Puli messages::success(asyncResp->res); 177284e12cb7SAppaRao Puli return; 177384e12cb7SAppaRao Puli }, 177484e12cb7SAppaRao Puli "xyz.openbmc_project.User.Manager", 177524c8542dSRatan Gupta dbusObjectPath.c_str(), 177684e12cb7SAppaRao Puli "org.freedesktop.DBus.Properties", "Set", 177784e12cb7SAppaRao Puli "xyz.openbmc_project.User.Attributes", "UserEnabled", 1778abf2add6SEd Tanous std::variant<bool>{*enabled}); 177984e12cb7SAppaRao Puli } 178084e12cb7SAppaRao Puli 178184e12cb7SAppaRao Puli if (roleId) 178284e12cb7SAppaRao Puli { 178354fc587aSNagaraju Goruganti std::string priv = getPrivilegeFromRoleId(*roleId); 178484e12cb7SAppaRao Puli if (priv.empty()) 178584e12cb7SAppaRao Puli { 178624c8542dSRatan Gupta messages::propertyValueNotInList(asyncResp->res, 178724c8542dSRatan Gupta *roleId, "RoleId"); 178884e12cb7SAppaRao Puli return; 178984e12cb7SAppaRao Puli } 179084e12cb7SAppaRao Puli 179184e12cb7SAppaRao Puli crow::connections::systemBus->async_method_call( 179284e12cb7SAppaRao Puli [asyncResp](const boost::system::error_code ec) { 179384e12cb7SAppaRao Puli if (ec) 179484e12cb7SAppaRao Puli { 179524c8542dSRatan Gupta BMCWEB_LOG_ERROR << "D-Bus responses error: " 179624c8542dSRatan Gupta << ec; 179784e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 179884e12cb7SAppaRao Puli return; 179984e12cb7SAppaRao Puli } 1800f12894f8SJason M. Bills messages::success(asyncResp->res); 1801a840879dSEd Tanous }, 1802a840879dSEd Tanous "xyz.openbmc_project.User.Manager", 180324c8542dSRatan Gupta dbusObjectPath.c_str(), 1804a840879dSEd Tanous "org.freedesktop.DBus.Properties", "Set", 180584e12cb7SAppaRao Puli "xyz.openbmc_project.User.Attributes", "UserPrivilege", 1806abf2add6SEd Tanous std::variant<std::string>{priv}); 1807a840879dSEd Tanous } 180824c8542dSRatan Gupta 180924c8542dSRatan Gupta if (locked) 181024c8542dSRatan Gupta { 181124c8542dSRatan Gupta // admin can unlock the account which is locked by 181254fc587aSNagaraju Goruganti // successive authentication failures but admin should 181354fc587aSNagaraju Goruganti // not be allowed to lock an account. 181424c8542dSRatan Gupta if (*locked) 181524c8542dSRatan Gupta { 181624c8542dSRatan Gupta messages::propertyValueNotInList(asyncResp->res, "true", 181724c8542dSRatan Gupta "Locked"); 181824c8542dSRatan Gupta return; 181924c8542dSRatan Gupta } 182024c8542dSRatan Gupta 182124c8542dSRatan Gupta crow::connections::systemBus->async_method_call( 182224c8542dSRatan Gupta [asyncResp](const boost::system::error_code ec) { 182324c8542dSRatan Gupta if (ec) 182424c8542dSRatan Gupta { 182524c8542dSRatan Gupta BMCWEB_LOG_ERROR << "D-Bus responses error: " 182624c8542dSRatan Gupta << ec; 182724c8542dSRatan Gupta messages::internalError(asyncResp->res); 182824c8542dSRatan Gupta return; 182924c8542dSRatan Gupta } 183024c8542dSRatan Gupta messages::success(asyncResp->res); 183124c8542dSRatan Gupta return; 183224c8542dSRatan Gupta }, 183324c8542dSRatan Gupta "xyz.openbmc_project.User.Manager", 183424c8542dSRatan Gupta dbusObjectPath.c_str(), 183524c8542dSRatan Gupta "org.freedesktop.DBus.Properties", "Set", 183624c8542dSRatan Gupta "xyz.openbmc_project.User.Attributes", 183724c8542dSRatan Gupta "UserLockedForFailedAttempt", 183824c8542dSRatan Gupta sdbusplus::message::variant<bool>{*locked}); 183924c8542dSRatan Gupta } 184024c8542dSRatan Gupta }); 1841a840879dSEd Tanous } 184206e086d9SEd Tanous 184306e086d9SEd Tanous void doDelete(crow::Response& res, const crow::Request& req, 184406e086d9SEd Tanous const std::vector<std::string>& params) override 184506e086d9SEd Tanous { 184606e086d9SEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 184706e086d9SEd Tanous 184806e086d9SEd Tanous if (params.size() != 1) 184906e086d9SEd Tanous { 1850f12894f8SJason M. Bills messages::internalError(asyncResp->res); 185106e086d9SEd Tanous return; 185206e086d9SEd Tanous } 185306e086d9SEd Tanous 185406e086d9SEd Tanous const std::string userPath = "/xyz/openbmc_project/user/" + params[0]; 185506e086d9SEd Tanous 185606e086d9SEd Tanous crow::connections::systemBus->async_method_call( 185706e086d9SEd Tanous [asyncResp, username{std::move(params[0])}]( 185806e086d9SEd Tanous const boost::system::error_code ec) { 185906e086d9SEd Tanous if (ec) 186006e086d9SEd Tanous { 186106e086d9SEd Tanous messages::resourceNotFound( 1862f12894f8SJason M. Bills asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", 1863f12894f8SJason M. Bills username); 186406e086d9SEd Tanous return; 186506e086d9SEd Tanous } 186606e086d9SEd Tanous 1867f12894f8SJason M. Bills messages::accountRemoved(asyncResp->res); 186806e086d9SEd Tanous }, 186906e086d9SEd Tanous "xyz.openbmc_project.User.Manager", userPath, 187006e086d9SEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 187106e086d9SEd Tanous } 187284e12cb7SAppaRao Puli }; 187388d16c9aSLewanczyk, Dawid 187488d16c9aSLewanczyk, Dawid } // namespace redfish 1875