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 187e860f15SJohn Edward Broadbent #include <app.hpp> 1924c8542dSRatan Gupta #include <dbus_utility.hpp> 2065b0dc32SEd Tanous #include <error_messages.hpp> 21b9b2e0b2SEd Tanous #include <openbmc_dbus_rest.hpp> 2252cc112dSEd Tanous #include <persistent_data.hpp> 23a840879dSEd Tanous #include <utils/json_utils.hpp> 241214b7e7SGunnar Mills 25abf2add6SEd Tanous #include <variant> 26b9b2e0b2SEd Tanous 271abe55efSEd Tanous namespace redfish 281abe55efSEd Tanous { 2988d16c9aSLewanczyk, Dawid 3023a21a1cSEd Tanous constexpr const char* ldapConfigObjectName = 316973a582SRatan Gupta "/xyz/openbmc_project/user/ldap/openldap"; 322c70f800SEd Tanous constexpr const char* adConfigObject = 33ab828d7cSRatan Gupta "/xyz/openbmc_project/user/ldap/active_directory"; 34ab828d7cSRatan Gupta 356973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap"; 366973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config"; 376973a582SRatan Gupta constexpr const char* ldapConfigInterface = 386973a582SRatan Gupta "xyz.openbmc_project.User.Ldap.Config"; 396973a582SRatan Gupta constexpr const char* ldapCreateInterface = 406973a582SRatan Gupta "xyz.openbmc_project.User.Ldap.Create"; 416973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable"; 4206785244SRatan Gupta constexpr const char* ldapPrivMapperInterface = 4306785244SRatan Gupta "xyz.openbmc_project.User.PrivilegeMapper"; 446973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager"; 456973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties"; 466973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper"; 476973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper"; 486973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper"; 496973a582SRatan Gupta 5054fc587aSNagaraju Goruganti struct LDAPRoleMapData 5154fc587aSNagaraju Goruganti { 5254fc587aSNagaraju Goruganti std::string groupName; 5354fc587aSNagaraju Goruganti std::string privilege; 5454fc587aSNagaraju Goruganti }; 5554fc587aSNagaraju Goruganti 566973a582SRatan Gupta struct LDAPConfigData 576973a582SRatan Gupta { 586973a582SRatan Gupta std::string uri{}; 596973a582SRatan Gupta std::string bindDN{}; 606973a582SRatan Gupta std::string baseDN{}; 616973a582SRatan Gupta std::string searchScope{}; 626973a582SRatan Gupta std::string serverType{}; 636973a582SRatan Gupta bool serviceEnabled = false; 646973a582SRatan Gupta std::string userNameAttribute{}; 656973a582SRatan Gupta std::string groupAttribute{}; 6654fc587aSNagaraju Goruganti std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList; 676973a582SRatan Gupta }; 686973a582SRatan Gupta 6919bd78d9SPatrick Williams using DbusVariantType = std::variant<bool, int32_t, std::string>; 70107077deSPrzemyslaw Czarnowski 71107077deSPrzemyslaw Czarnowski using DbusInterfaceType = boost::container::flat_map< 72107077deSPrzemyslaw Czarnowski std::string, boost::container::flat_map<std::string, DbusVariantType>>; 73107077deSPrzemyslaw Czarnowski 74107077deSPrzemyslaw Czarnowski using ManagedObjectType = 75107077deSPrzemyslaw Czarnowski std::vector<std::pair<sdbusplus::message::object_path, DbusInterfaceType>>; 76107077deSPrzemyslaw Czarnowski 776973a582SRatan Gupta using GetObjectType = 786973a582SRatan Gupta std::vector<std::pair<std::string, std::vector<std::string>>>; 7984e12cb7SAppaRao Puli 8054fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role) 8184e12cb7SAppaRao Puli { 8284e12cb7SAppaRao Puli if (role == "priv-admin") 8384e12cb7SAppaRao Puli { 8484e12cb7SAppaRao Puli return "Administrator"; 8584e12cb7SAppaRao Puli } 863174e4dfSEd Tanous if (role == "priv-user") 8784e12cb7SAppaRao Puli { 88c80fee55SAppaRao Puli return "ReadOnly"; 8984e12cb7SAppaRao Puli } 903174e4dfSEd Tanous if (role == "priv-operator") 9184e12cb7SAppaRao Puli { 9284e12cb7SAppaRao Puli return "Operator"; 9384e12cb7SAppaRao Puli } 943174e4dfSEd Tanous if ((role == "") || (role == "priv-noaccess")) 95e9e6d240Sjayaprakash Mutyala { 96e9e6d240Sjayaprakash Mutyala return "NoAccess"; 97e9e6d240Sjayaprakash Mutyala } 9884e12cb7SAppaRao Puli return ""; 9984e12cb7SAppaRao Puli } 10054fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role) 10184e12cb7SAppaRao Puli { 10284e12cb7SAppaRao Puli if (role == "Administrator") 10384e12cb7SAppaRao Puli { 10484e12cb7SAppaRao Puli return "priv-admin"; 10584e12cb7SAppaRao Puli } 1063174e4dfSEd Tanous if (role == "ReadOnly") 10784e12cb7SAppaRao Puli { 10884e12cb7SAppaRao Puli return "priv-user"; 10984e12cb7SAppaRao Puli } 1103174e4dfSEd Tanous if (role == "Operator") 11184e12cb7SAppaRao Puli { 11284e12cb7SAppaRao Puli return "priv-operator"; 11384e12cb7SAppaRao Puli } 1143174e4dfSEd Tanous if ((role == "NoAccess") || (role == "")) 115e9e6d240Sjayaprakash Mutyala { 116e9e6d240Sjayaprakash Mutyala return "priv-noaccess"; 117e9e6d240Sjayaprakash Mutyala } 11884e12cb7SAppaRao Puli return ""; 11984e12cb7SAppaRao Puli } 120b9b2e0b2SEd Tanous 1218d1b46d7Szhanghch05 inline void userErrorMessageHandler( 1228d1b46d7Szhanghch05 const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1238d1b46d7Szhanghch05 const std::string& newUser, const std::string& username) 12466b5ca76Sjayaprakash Mutyala { 12566b5ca76Sjayaprakash Mutyala if (e == nullptr) 12666b5ca76Sjayaprakash Mutyala { 12766b5ca76Sjayaprakash Mutyala messages::internalError(asyncResp->res); 12866b5ca76Sjayaprakash Mutyala return; 12966b5ca76Sjayaprakash Mutyala } 13066b5ca76Sjayaprakash Mutyala 131055806b3SManojkiran Eda const char* errorMessage = e->name; 13266b5ca76Sjayaprakash Mutyala if (strcmp(errorMessage, 13366b5ca76Sjayaprakash Mutyala "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0) 13466b5ca76Sjayaprakash Mutyala { 13566b5ca76Sjayaprakash Mutyala messages::resourceAlreadyExists(asyncResp->res, 1368114bd4dSGunnar Mills "#ManagerAccount.v1_4_0.ManagerAccount", 13766b5ca76Sjayaprakash Mutyala "UserName", newUser); 13866b5ca76Sjayaprakash Mutyala } 13966b5ca76Sjayaprakash Mutyala else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error." 14066b5ca76Sjayaprakash Mutyala "UserNameDoesNotExist") == 0) 14166b5ca76Sjayaprakash Mutyala { 14266b5ca76Sjayaprakash Mutyala messages::resourceNotFound( 1438114bd4dSGunnar Mills asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount", username); 14466b5ca76Sjayaprakash Mutyala } 145d4d25793SEd Tanous else if ((strcmp(errorMessage, 146d4d25793SEd Tanous "xyz.openbmc_project.Common.Error.InvalidArgument") == 147d4d25793SEd Tanous 0) || 148d4d25793SEd Tanous (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error." 149d4d25793SEd Tanous "UserNameGroupFail") == 0)) 15066b5ca76Sjayaprakash Mutyala { 15166b5ca76Sjayaprakash Mutyala messages::propertyValueFormatError(asyncResp->res, newUser, "UserName"); 15266b5ca76Sjayaprakash Mutyala } 15366b5ca76Sjayaprakash Mutyala else if (strcmp(errorMessage, 15466b5ca76Sjayaprakash Mutyala "xyz.openbmc_project.User.Common.Error.NoResource") == 0) 15566b5ca76Sjayaprakash Mutyala { 15666b5ca76Sjayaprakash Mutyala messages::createLimitReachedForResource(asyncResp->res); 15766b5ca76Sjayaprakash Mutyala } 15866b5ca76Sjayaprakash Mutyala else 15966b5ca76Sjayaprakash Mutyala { 16066b5ca76Sjayaprakash Mutyala messages::internalError(asyncResp->res); 16166b5ca76Sjayaprakash Mutyala } 16266b5ca76Sjayaprakash Mutyala 16366b5ca76Sjayaprakash Mutyala return; 16466b5ca76Sjayaprakash Mutyala } 16566b5ca76Sjayaprakash Mutyala 16681ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse, 167ab828d7cSRatan Gupta const LDAPConfigData& confData, 168ab828d7cSRatan Gupta const std::string& ldapType) 1696973a582SRatan Gupta { 170ab828d7cSRatan Gupta std::string service = 171ab828d7cSRatan Gupta (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService"; 17237cce918SMarri Devender Rao nlohmann::json ldap = { 1736973a582SRatan Gupta {"ServiceEnabled", confData.serviceEnabled}, 1746973a582SRatan Gupta {"ServiceAddresses", nlohmann::json::array({confData.uri})}, 1756973a582SRatan Gupta {"Authentication", 1766973a582SRatan Gupta {{"AuthenticationType", "UsernameAndPassword"}, 1776973a582SRatan Gupta {"Username", confData.bindDN}, 1786973a582SRatan Gupta {"Password", nullptr}}}, 1796973a582SRatan Gupta {"LDAPService", 1806973a582SRatan Gupta {{"SearchSettings", 1816973a582SRatan Gupta {{"BaseDistinguishedNames", 1826973a582SRatan Gupta nlohmann::json::array({confData.baseDN})}, 1836973a582SRatan Gupta {"UsernameAttribute", confData.userNameAttribute}, 1846973a582SRatan Gupta {"GroupsAttribute", confData.groupAttribute}}}}}, 1856973a582SRatan Gupta }; 18654fc587aSNagaraju Goruganti 18781ce609eSEd Tanous jsonResponse[ldapType].update(ldap); 18854fc587aSNagaraju Goruganti 18981ce609eSEd Tanous nlohmann::json& roleMapArray = jsonResponse[ldapType]["RemoteRoleMapping"]; 19054fc587aSNagaraju Goruganti roleMapArray = nlohmann::json::array(); 19154fc587aSNagaraju Goruganti for (auto& obj : confData.groupRoleList) 19254fc587aSNagaraju Goruganti { 19354fc587aSNagaraju Goruganti BMCWEB_LOG_DEBUG << "Pushing the data groupName=" 19454fc587aSNagaraju Goruganti << obj.second.groupName << "\n"; 19554fc587aSNagaraju Goruganti roleMapArray.push_back( 19654fc587aSNagaraju Goruganti {nlohmann::json::array({"RemoteGroup", obj.second.groupName}), 19754fc587aSNagaraju Goruganti nlohmann::json::array( 19854fc587aSNagaraju Goruganti {"LocalRole", getRoleIdFromPrivilege(obj.second.privilege)})}); 19954fc587aSNagaraju Goruganti } 2006973a582SRatan Gupta } 2016973a582SRatan Gupta 2026973a582SRatan Gupta /** 20306785244SRatan Gupta * @brief validates given JSON input and then calls appropriate method to 20406785244SRatan Gupta * create, to delete or to set Rolemapping object based on the given input. 20506785244SRatan Gupta * 20606785244SRatan Gupta */ 20723a21a1cSEd Tanous inline void handleRoleMapPatch( 2088d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 20906785244SRatan Gupta const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData, 210f23b7296SEd Tanous const std::string& serverType, const std::vector<nlohmann::json>& input) 21106785244SRatan Gupta { 21206785244SRatan Gupta for (size_t index = 0; index < input.size(); index++) 21306785244SRatan Gupta { 214f23b7296SEd Tanous const nlohmann::json& thisJson = input[index]; 21506785244SRatan Gupta 21606785244SRatan Gupta if (thisJson.is_null()) 21706785244SRatan Gupta { 21806785244SRatan Gupta // delete the existing object 21906785244SRatan Gupta if (index < roleMapObjData.size()) 22006785244SRatan Gupta { 22106785244SRatan Gupta crow::connections::systemBus->async_method_call( 22206785244SRatan Gupta [asyncResp, roleMapObjData, serverType, 22306785244SRatan Gupta index](const boost::system::error_code ec) { 22406785244SRatan Gupta if (ec) 22506785244SRatan Gupta { 22606785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " << ec; 22706785244SRatan Gupta messages::internalError(asyncResp->res); 22806785244SRatan Gupta return; 22906785244SRatan Gupta } 23006785244SRatan Gupta asyncResp->res 23106785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"][index] = 23206785244SRatan Gupta nullptr; 23306785244SRatan Gupta }, 23406785244SRatan Gupta ldapDbusService, roleMapObjData[index].first, 23506785244SRatan Gupta "xyz.openbmc_project.Object.Delete", "Delete"); 23606785244SRatan Gupta } 23706785244SRatan Gupta else 23806785244SRatan Gupta { 23906785244SRatan Gupta BMCWEB_LOG_ERROR << "Can't delete the object"; 24006785244SRatan Gupta messages::propertyValueTypeError( 24171f52d96SEd Tanous asyncResp->res, 24271f52d96SEd Tanous thisJson.dump(2, ' ', true, 24371f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 24406785244SRatan Gupta "RemoteRoleMapping/" + std::to_string(index)); 24506785244SRatan Gupta return; 24606785244SRatan Gupta } 24706785244SRatan Gupta } 24806785244SRatan Gupta else if (thisJson.empty()) 24906785244SRatan Gupta { 25006785244SRatan Gupta // Don't do anything for the empty objects,parse next json 25106785244SRatan Gupta // eg {"RemoteRoleMapping",[{}]} 25206785244SRatan Gupta } 25306785244SRatan Gupta else 25406785244SRatan Gupta { 25506785244SRatan Gupta // update/create the object 25606785244SRatan Gupta std::optional<std::string> remoteGroup; 25706785244SRatan Gupta std::optional<std::string> localRole; 25806785244SRatan Gupta 259f23b7296SEd Tanous // This is a copy, but it's required in this case because of how 260f23b7296SEd Tanous // readJson is structured 261f23b7296SEd Tanous nlohmann::json thisJsonCopy = thisJson; 262f23b7296SEd Tanous if (!json_util::readJson(thisJsonCopy, asyncResp->res, 263f23b7296SEd Tanous "RemoteGroup", remoteGroup, "LocalRole", 264f23b7296SEd Tanous localRole)) 26506785244SRatan Gupta { 26606785244SRatan Gupta continue; 26706785244SRatan Gupta } 26806785244SRatan Gupta 26906785244SRatan Gupta // Update existing RoleMapping Object 27006785244SRatan Gupta if (index < roleMapObjData.size()) 27106785244SRatan Gupta { 27206785244SRatan Gupta BMCWEB_LOG_DEBUG << "Update Role Map Object"; 27306785244SRatan Gupta // If "RemoteGroup" info is provided 27406785244SRatan Gupta if (remoteGroup) 27506785244SRatan Gupta { 27606785244SRatan Gupta crow::connections::systemBus->async_method_call( 27706785244SRatan Gupta [asyncResp, roleMapObjData, serverType, index, 27806785244SRatan Gupta remoteGroup](const boost::system::error_code ec) { 27906785244SRatan Gupta if (ec) 28006785244SRatan Gupta { 28106785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " 28206785244SRatan Gupta << ec; 28306785244SRatan Gupta messages::internalError(asyncResp->res); 28406785244SRatan Gupta return; 28506785244SRatan Gupta } 28606785244SRatan Gupta asyncResp->res 28706785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"] 28806785244SRatan Gupta [index]["RemoteGroup"] = *remoteGroup; 28906785244SRatan Gupta }, 29006785244SRatan Gupta ldapDbusService, roleMapObjData[index].first, 29106785244SRatan Gupta propertyInterface, "Set", 29206785244SRatan Gupta "xyz.openbmc_project.User.PrivilegeMapperEntry", 29306785244SRatan Gupta "GroupName", 29406785244SRatan Gupta std::variant<std::string>(std::move(*remoteGroup))); 29506785244SRatan Gupta } 29606785244SRatan Gupta 29706785244SRatan Gupta // If "LocalRole" info is provided 29806785244SRatan Gupta if (localRole) 29906785244SRatan Gupta { 30006785244SRatan Gupta crow::connections::systemBus->async_method_call( 30106785244SRatan Gupta [asyncResp, roleMapObjData, serverType, index, 30206785244SRatan Gupta localRole](const boost::system::error_code ec) { 30306785244SRatan Gupta if (ec) 30406785244SRatan Gupta { 30506785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " 30606785244SRatan Gupta << ec; 30706785244SRatan Gupta messages::internalError(asyncResp->res); 30806785244SRatan Gupta return; 30906785244SRatan Gupta } 31006785244SRatan Gupta asyncResp->res 31106785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"] 31206785244SRatan Gupta [index]["LocalRole"] = *localRole; 31306785244SRatan Gupta }, 31406785244SRatan Gupta ldapDbusService, roleMapObjData[index].first, 31506785244SRatan Gupta propertyInterface, "Set", 31606785244SRatan Gupta "xyz.openbmc_project.User.PrivilegeMapperEntry", 31706785244SRatan Gupta "Privilege", 31806785244SRatan Gupta std::variant<std::string>( 31906785244SRatan Gupta getPrivilegeFromRoleId(std::move(*localRole)))); 32006785244SRatan Gupta } 32106785244SRatan Gupta } 32206785244SRatan Gupta // Create a new RoleMapping Object. 32306785244SRatan Gupta else 32406785244SRatan Gupta { 32506785244SRatan Gupta BMCWEB_LOG_DEBUG 32606785244SRatan Gupta << "setRoleMappingProperties: Creating new Object"; 32706785244SRatan Gupta std::string pathString = 32806785244SRatan Gupta "RemoteRoleMapping/" + std::to_string(index); 32906785244SRatan Gupta 33006785244SRatan Gupta if (!localRole) 33106785244SRatan Gupta { 33206785244SRatan Gupta messages::propertyMissing(asyncResp->res, 33306785244SRatan Gupta pathString + "/LocalRole"); 33406785244SRatan Gupta continue; 33506785244SRatan Gupta } 33606785244SRatan Gupta if (!remoteGroup) 33706785244SRatan Gupta { 33806785244SRatan Gupta messages::propertyMissing(asyncResp->res, 33906785244SRatan Gupta pathString + "/RemoteGroup"); 34006785244SRatan Gupta continue; 34106785244SRatan Gupta } 34206785244SRatan Gupta 34306785244SRatan Gupta std::string dbusObjectPath; 34406785244SRatan Gupta if (serverType == "ActiveDirectory") 34506785244SRatan Gupta { 3462c70f800SEd Tanous dbusObjectPath = adConfigObject; 34706785244SRatan Gupta } 34806785244SRatan Gupta else if (serverType == "LDAP") 34906785244SRatan Gupta { 35023a21a1cSEd Tanous dbusObjectPath = ldapConfigObjectName; 35106785244SRatan Gupta } 35206785244SRatan Gupta 35306785244SRatan Gupta BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup 35406785244SRatan Gupta << ",LocalRole=" << *localRole; 35506785244SRatan Gupta 35606785244SRatan Gupta crow::connections::systemBus->async_method_call( 357271584abSEd Tanous [asyncResp, serverType, localRole, 35806785244SRatan Gupta remoteGroup](const boost::system::error_code ec) { 35906785244SRatan Gupta if (ec) 36006785244SRatan Gupta { 36106785244SRatan Gupta BMCWEB_LOG_ERROR << "DBUS response error: " << ec; 36206785244SRatan Gupta messages::internalError(asyncResp->res); 36306785244SRatan Gupta return; 36406785244SRatan Gupta } 36506785244SRatan Gupta nlohmann::json& remoteRoleJson = 36606785244SRatan Gupta asyncResp->res 36706785244SRatan Gupta .jsonValue[serverType]["RemoteRoleMapping"]; 36806785244SRatan Gupta remoteRoleJson.push_back( 36906785244SRatan Gupta {{"LocalRole", *localRole}, 37006785244SRatan Gupta {"RemoteGroup", *remoteGroup}}); 37106785244SRatan Gupta }, 37206785244SRatan Gupta ldapDbusService, dbusObjectPath, ldapPrivMapperInterface, 3733174e4dfSEd Tanous "Create", *remoteGroup, 37406785244SRatan Gupta getPrivilegeFromRoleId(std::move(*localRole))); 37506785244SRatan Gupta } 37606785244SRatan Gupta } 37706785244SRatan Gupta } 37806785244SRatan Gupta } 37906785244SRatan Gupta 38006785244SRatan Gupta /** 3816973a582SRatan Gupta * Function that retrieves all properties for LDAP config object 3826973a582SRatan Gupta * into JSON 3836973a582SRatan Gupta */ 3846973a582SRatan Gupta template <typename CallbackFunc> 3856973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType, 3866973a582SRatan Gupta CallbackFunc&& callback) 3876973a582SRatan Gupta { 38854fc587aSNagaraju Goruganti 38954fc587aSNagaraju Goruganti const std::array<const char*, 2> interfaces = {ldapEnableInterface, 39054fc587aSNagaraju Goruganti ldapConfigInterface}; 39154fc587aSNagaraju Goruganti 39254fc587aSNagaraju Goruganti crow::connections::systemBus->async_method_call( 39354fc587aSNagaraju Goruganti [callback, ldapType](const boost::system::error_code ec, 39454fc587aSNagaraju Goruganti const GetObjectType& resp) { 39554fc587aSNagaraju Goruganti if (ec || resp.empty()) 39654fc587aSNagaraju Goruganti { 39754fc587aSNagaraju Goruganti BMCWEB_LOG_ERROR << "DBUS response error during getting of " 39854fc587aSNagaraju Goruganti "service name: " 39954fc587aSNagaraju Goruganti << ec; 40023a21a1cSEd Tanous LDAPConfigData empty{}; 40123a21a1cSEd Tanous callback(false, empty, ldapType); 40254fc587aSNagaraju Goruganti return; 40354fc587aSNagaraju Goruganti } 40454fc587aSNagaraju Goruganti std::string service = resp.begin()->first; 40554fc587aSNagaraju Goruganti crow::connections::systemBus->async_method_call( 40681ce609eSEd Tanous [callback, ldapType](const boost::system::error_code errorCode, 4076973a582SRatan Gupta const ManagedObjectType& ldapObjects) { 4086973a582SRatan Gupta LDAPConfigData confData{}; 40981ce609eSEd Tanous if (errorCode) 4106973a582SRatan Gupta { 411ab828d7cSRatan Gupta callback(false, confData, ldapType); 41254fc587aSNagaraju Goruganti BMCWEB_LOG_ERROR << "D-Bus responses error: " 41381ce609eSEd Tanous << errorCode; 4146973a582SRatan Gupta return; 4156973a582SRatan Gupta } 416ab828d7cSRatan Gupta 417ab828d7cSRatan Gupta std::string ldapDbusType; 41854fc587aSNagaraju Goruganti std::string searchString; 41954fc587aSNagaraju Goruganti 420ab828d7cSRatan Gupta if (ldapType == "LDAP") 421ab828d7cSRatan Gupta { 42254fc587aSNagaraju Goruganti ldapDbusType = "xyz.openbmc_project.User.Ldap.Config." 42354fc587aSNagaraju Goruganti "Type.OpenLdap"; 42454fc587aSNagaraju Goruganti searchString = "openldap"; 425ab828d7cSRatan Gupta } 426ab828d7cSRatan Gupta else if (ldapType == "ActiveDirectory") 427ab828d7cSRatan Gupta { 42854fc587aSNagaraju Goruganti ldapDbusType = 42954fc587aSNagaraju Goruganti "xyz.openbmc_project.User.Ldap.Config.Type." 430ab828d7cSRatan Gupta "ActiveDirectory"; 43154fc587aSNagaraju Goruganti searchString = "active_directory"; 432ab828d7cSRatan Gupta } 433ab828d7cSRatan Gupta else 434ab828d7cSRatan Gupta { 43554fc587aSNagaraju Goruganti BMCWEB_LOG_ERROR 43654fc587aSNagaraju Goruganti << "Can't get the DbusType for the given type=" 437ab828d7cSRatan Gupta << ldapType; 438ab828d7cSRatan Gupta callback(false, confData, ldapType); 439ab828d7cSRatan Gupta return; 440ab828d7cSRatan Gupta } 441ab828d7cSRatan Gupta 442ab828d7cSRatan Gupta std::string ldapEnableInterfaceStr = ldapEnableInterface; 443ab828d7cSRatan Gupta std::string ldapConfigInterfaceStr = ldapConfigInterface; 444ab828d7cSRatan Gupta 4456973a582SRatan Gupta for (const auto& object : ldapObjects) 4466973a582SRatan Gupta { 44754fc587aSNagaraju Goruganti // let's find the object whose ldap type is equal to the 44854fc587aSNagaraju Goruganti // given type 44954fc587aSNagaraju Goruganti if (object.first.str.find(searchString) == 45054fc587aSNagaraju Goruganti std::string::npos) 4516973a582SRatan Gupta { 452ab828d7cSRatan Gupta continue; 453ab828d7cSRatan Gupta } 454ab828d7cSRatan Gupta 4556973a582SRatan Gupta for (const auto& interface : object.second) 4566973a582SRatan Gupta { 4576973a582SRatan Gupta if (interface.first == ldapEnableInterfaceStr) 4586973a582SRatan Gupta { 4596973a582SRatan Gupta // rest of the properties are string. 4606973a582SRatan Gupta for (const auto& property : interface.second) 4616973a582SRatan Gupta { 4626973a582SRatan Gupta if (property.first == "Enabled") 4636973a582SRatan Gupta { 4646973a582SRatan Gupta const bool* value = 4656973a582SRatan Gupta std::get_if<bool>(&property.second); 4666973a582SRatan Gupta if (value == nullptr) 4676973a582SRatan Gupta { 4686973a582SRatan Gupta continue; 4696973a582SRatan Gupta } 4706973a582SRatan Gupta confData.serviceEnabled = *value; 4716973a582SRatan Gupta break; 4726973a582SRatan Gupta } 4736973a582SRatan Gupta } 4746973a582SRatan Gupta } 4756973a582SRatan Gupta else if (interface.first == ldapConfigInterfaceStr) 4766973a582SRatan Gupta { 4776973a582SRatan Gupta 4786973a582SRatan Gupta for (const auto& property : interface.second) 4796973a582SRatan Gupta { 480271584abSEd Tanous const std::string* strValue = 48154fc587aSNagaraju Goruganti std::get_if<std::string>( 48254fc587aSNagaraju Goruganti &property.second); 483271584abSEd Tanous if (strValue == nullptr) 4846973a582SRatan Gupta { 4856973a582SRatan Gupta continue; 4866973a582SRatan Gupta } 4876973a582SRatan Gupta if (property.first == "LDAPServerURI") 4886973a582SRatan Gupta { 489271584abSEd Tanous confData.uri = *strValue; 4906973a582SRatan Gupta } 4916973a582SRatan Gupta else if (property.first == "LDAPBindDN") 4926973a582SRatan Gupta { 493271584abSEd Tanous confData.bindDN = *strValue; 4946973a582SRatan Gupta } 4956973a582SRatan Gupta else if (property.first == "LDAPBaseDN") 4966973a582SRatan Gupta { 497271584abSEd Tanous confData.baseDN = *strValue; 4986973a582SRatan Gupta } 49954fc587aSNagaraju Goruganti else if (property.first == 50054fc587aSNagaraju Goruganti "LDAPSearchScope") 5016973a582SRatan Gupta { 502271584abSEd Tanous confData.searchScope = *strValue; 5036973a582SRatan Gupta } 50454fc587aSNagaraju Goruganti else if (property.first == 50554fc587aSNagaraju Goruganti "GroupNameAttribute") 5066973a582SRatan Gupta { 507271584abSEd Tanous confData.groupAttribute = *strValue; 5086973a582SRatan Gupta } 50954fc587aSNagaraju Goruganti else if (property.first == 51054fc587aSNagaraju Goruganti "UserNameAttribute") 5116973a582SRatan Gupta { 512271584abSEd Tanous confData.userNameAttribute = *strValue; 5136973a582SRatan Gupta } 51454fc587aSNagaraju Goruganti else if (property.first == "LDAPType") 515ab828d7cSRatan Gupta { 516271584abSEd Tanous confData.serverType = *strValue; 51754fc587aSNagaraju Goruganti } 51854fc587aSNagaraju Goruganti } 51954fc587aSNagaraju Goruganti } 52054fc587aSNagaraju Goruganti else if (interface.first == 52154fc587aSNagaraju Goruganti "xyz.openbmc_project.User." 52254fc587aSNagaraju Goruganti "PrivilegeMapperEntry") 52354fc587aSNagaraju Goruganti { 52454fc587aSNagaraju Goruganti LDAPRoleMapData roleMapData{}; 52554fc587aSNagaraju Goruganti for (const auto& property : interface.second) 52654fc587aSNagaraju Goruganti { 527271584abSEd Tanous const std::string* strValue = 52854fc587aSNagaraju Goruganti std::get_if<std::string>( 52954fc587aSNagaraju Goruganti &property.second); 53054fc587aSNagaraju Goruganti 531271584abSEd Tanous if (strValue == nullptr) 53254fc587aSNagaraju Goruganti { 53354fc587aSNagaraju Goruganti continue; 53454fc587aSNagaraju Goruganti } 53554fc587aSNagaraju Goruganti 53654fc587aSNagaraju Goruganti if (property.first == "GroupName") 53754fc587aSNagaraju Goruganti { 538271584abSEd Tanous roleMapData.groupName = *strValue; 53954fc587aSNagaraju Goruganti } 54054fc587aSNagaraju Goruganti else if (property.first == "Privilege") 54154fc587aSNagaraju Goruganti { 542271584abSEd Tanous roleMapData.privilege = *strValue; 54354fc587aSNagaraju Goruganti } 54454fc587aSNagaraju Goruganti } 54554fc587aSNagaraju Goruganti 5460f0353b6SEd Tanous confData.groupRoleList.emplace_back( 5470f0353b6SEd Tanous object.first.str, roleMapData); 54854fc587aSNagaraju Goruganti } 54954fc587aSNagaraju Goruganti } 55054fc587aSNagaraju Goruganti } 551ab828d7cSRatan Gupta callback(true, confData, ldapType); 55254fc587aSNagaraju Goruganti }, 55354fc587aSNagaraju Goruganti service, ldapRootObject, dbusObjManagerIntf, 5546973a582SRatan Gupta "GetManagedObjects"); 55554fc587aSNagaraju Goruganti }, 55654fc587aSNagaraju Goruganti mapperBusName, mapperObjectPath, mapperIntf, "GetObject", 55723a21a1cSEd Tanous ldapConfigObjectName, interfaces); 5586973a582SRatan Gupta } 5596973a582SRatan Gupta 5608a07d286SRatan Gupta /** 5618a07d286SRatan Gupta * @brief parses the authentication section under the LDAP 5628a07d286SRatan Gupta * @param input JSON data 5638a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 5648a07d286SRatan Gupta * @param userName userName to be filled from the given JSON. 5658a07d286SRatan Gupta * @param password password to be filled from the given JSON. 5668a07d286SRatan Gupta */ 5678d1b46d7Szhanghch05 void parseLDAPAuthenticationJson( 5686c51eab1SEd Tanous nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 5696c51eab1SEd Tanous std::optional<std::string>& username, std::optional<std::string>& password) 5708a07d286SRatan Gupta { 5718a07d286SRatan Gupta std::optional<std::string> authType; 5728a07d286SRatan Gupta 5738a07d286SRatan Gupta if (!json_util::readJson(input, asyncResp->res, "AuthenticationType", 5748a07d286SRatan Gupta authType, "Username", username, "Password", 5758a07d286SRatan Gupta password)) 5768a07d286SRatan Gupta { 5778a07d286SRatan Gupta return; 5788a07d286SRatan Gupta } 5798a07d286SRatan Gupta if (!authType) 5808a07d286SRatan Gupta { 5818a07d286SRatan Gupta return; 5828a07d286SRatan Gupta } 5838a07d286SRatan Gupta if (*authType != "UsernameAndPassword") 5848a07d286SRatan Gupta { 5858a07d286SRatan Gupta messages::propertyValueNotInList(asyncResp->res, *authType, 5868a07d286SRatan Gupta "AuthenticationType"); 5878a07d286SRatan Gupta return; 5888a07d286SRatan Gupta } 5898a07d286SRatan Gupta } 5908a07d286SRatan Gupta /** 5918a07d286SRatan Gupta * @brief parses the LDAPService section under the LDAP 5928a07d286SRatan Gupta * @param input JSON data 5938a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 5948a07d286SRatan Gupta * @param baseDNList baseDN to be filled from the given JSON. 5958a07d286SRatan Gupta * @param userNameAttribute userName to be filled from the given JSON. 5968a07d286SRatan Gupta * @param groupaAttribute password to be filled from the given JSON. 5978a07d286SRatan Gupta */ 5988a07d286SRatan Gupta 5996c51eab1SEd Tanous void parseLDAPServiceJson(nlohmann::json input, 6008d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6018a07d286SRatan Gupta std::optional<std::vector<std::string>>& baseDNList, 6028a07d286SRatan Gupta std::optional<std::string>& userNameAttribute, 6038a07d286SRatan Gupta std::optional<std::string>& groupsAttribute) 6048a07d286SRatan Gupta { 6058a07d286SRatan Gupta std::optional<nlohmann::json> searchSettings; 6068a07d286SRatan Gupta 6078a07d286SRatan Gupta if (!json_util::readJson(input, asyncResp->res, "SearchSettings", 6088a07d286SRatan Gupta searchSettings)) 6098a07d286SRatan Gupta { 6108a07d286SRatan Gupta return; 6118a07d286SRatan Gupta } 6128a07d286SRatan Gupta if (!searchSettings) 6138a07d286SRatan Gupta { 6148a07d286SRatan Gupta return; 6158a07d286SRatan Gupta } 6168a07d286SRatan Gupta if (!json_util::readJson(*searchSettings, asyncResp->res, 6178a07d286SRatan Gupta "BaseDistinguishedNames", baseDNList, 6188a07d286SRatan Gupta "UsernameAttribute", userNameAttribute, 6198a07d286SRatan Gupta "GroupsAttribute", groupsAttribute)) 6208a07d286SRatan Gupta { 6218a07d286SRatan Gupta return; 6228a07d286SRatan Gupta } 6238a07d286SRatan Gupta } 6248a07d286SRatan Gupta /** 6258a07d286SRatan Gupta * @brief updates the LDAP server address and updates the 6268a07d286SRatan Gupta json response with the new value. 6278a07d286SRatan Gupta * @param serviceAddressList address to be updated. 6288a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 6298a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 6308a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 6318a07d286SRatan Gupta */ 6328a07d286SRatan Gupta 6338a07d286SRatan Gupta void handleServiceAddressPatch( 6348a07d286SRatan Gupta const std::vector<std::string>& serviceAddressList, 6358d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6368a07d286SRatan Gupta const std::string& ldapServerElementName, 6378a07d286SRatan Gupta const std::string& ldapConfigObject) 6388a07d286SRatan Gupta { 6398a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 6408a07d286SRatan Gupta [asyncResp, ldapServerElementName, 6418a07d286SRatan Gupta serviceAddressList](const boost::system::error_code ec) { 6428a07d286SRatan Gupta if (ec) 6438a07d286SRatan Gupta { 6448a07d286SRatan Gupta BMCWEB_LOG_DEBUG 645c61704abSGunnar Mills << "Error Occurred in updating the service address"; 6468a07d286SRatan Gupta messages::internalError(asyncResp->res); 6478a07d286SRatan Gupta return; 6488a07d286SRatan Gupta } 6498a07d286SRatan Gupta std::vector<std::string> modifiedserviceAddressList = { 6508a07d286SRatan Gupta serviceAddressList.front()}; 6518a07d286SRatan Gupta asyncResp->res 6528a07d286SRatan Gupta .jsonValue[ldapServerElementName]["ServiceAddresses"] = 6538a07d286SRatan Gupta modifiedserviceAddressList; 6548a07d286SRatan Gupta if ((serviceAddressList).size() > 1) 6558a07d286SRatan Gupta { 6568a07d286SRatan Gupta messages::propertyValueModified(asyncResp->res, 6578a07d286SRatan Gupta "ServiceAddresses", 6588a07d286SRatan Gupta serviceAddressList.front()); 6598a07d286SRatan Gupta } 6608a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the service address"; 6618a07d286SRatan Gupta }, 6628a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 6638a07d286SRatan Gupta ldapConfigInterface, "LDAPServerURI", 6648a07d286SRatan Gupta std::variant<std::string>(serviceAddressList.front())); 6658a07d286SRatan Gupta } 6668a07d286SRatan Gupta /** 6678a07d286SRatan Gupta * @brief updates the LDAP Bind DN and updates the 6688a07d286SRatan Gupta json response with the new value. 6698a07d286SRatan Gupta * @param username name of the user which needs to be updated. 6708a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 6718a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 6728a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 6738a07d286SRatan Gupta */ 6748a07d286SRatan Gupta 6756c51eab1SEd Tanous void handleUserNamePatch(const std::string& username, 6768d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6778a07d286SRatan Gupta const std::string& ldapServerElementName, 6788a07d286SRatan Gupta const std::string& ldapConfigObject) 6798a07d286SRatan Gupta { 6808a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 6818a07d286SRatan Gupta [asyncResp, username, 6828a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 6838a07d286SRatan Gupta if (ec) 6848a07d286SRatan Gupta { 6856c51eab1SEd Tanous BMCWEB_LOG_DEBUG << "Error occurred in updating the username"; 6868a07d286SRatan Gupta messages::internalError(asyncResp->res); 6878a07d286SRatan Gupta return; 6888a07d286SRatan Gupta } 6896c51eab1SEd Tanous asyncResp->res.jsonValue[ldapServerElementName]["Authentication"] 6906c51eab1SEd Tanous ["Username"] = username; 6918a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the username"; 6928a07d286SRatan Gupta }, 6938a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 6946c51eab1SEd Tanous ldapConfigInterface, "LDAPBindDN", std::variant<std::string>(username)); 6958a07d286SRatan Gupta } 6968a07d286SRatan Gupta 6978a07d286SRatan Gupta /** 6988a07d286SRatan Gupta * @brief updates the LDAP password 6998a07d286SRatan Gupta * @param password : ldap password which needs to be updated. 7008a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 7018a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 7028a07d286SRatan Gupta * server(openLDAP/ActiveDirectory) 7038a07d286SRatan Gupta */ 7048a07d286SRatan Gupta 7056c51eab1SEd Tanous void handlePasswordPatch(const std::string& password, 7068d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 7078a07d286SRatan Gupta const std::string& ldapServerElementName, 7088a07d286SRatan Gupta const std::string& ldapConfigObject) 7098a07d286SRatan Gupta { 7108a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 7118a07d286SRatan Gupta [asyncResp, password, 7128a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 7138a07d286SRatan Gupta if (ec) 7148a07d286SRatan Gupta { 7156c51eab1SEd Tanous BMCWEB_LOG_DEBUG << "Error occurred in updating the password"; 7168a07d286SRatan Gupta messages::internalError(asyncResp->res); 7178a07d286SRatan Gupta return; 7188a07d286SRatan Gupta } 7196c51eab1SEd Tanous asyncResp->res.jsonValue[ldapServerElementName]["Authentication"] 7206c51eab1SEd Tanous ["Password"] = ""; 7218a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the password"; 7228a07d286SRatan Gupta }, 7238a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 7248a07d286SRatan Gupta ldapConfigInterface, "LDAPBindDNPassword", 7258a07d286SRatan Gupta std::variant<std::string>(password)); 7268a07d286SRatan Gupta } 7278a07d286SRatan Gupta 7288a07d286SRatan Gupta /** 7298a07d286SRatan Gupta * @brief updates the LDAP BaseDN and updates the 7308a07d286SRatan Gupta json response with the new value. 7318a07d286SRatan Gupta * @param baseDNList baseDN list which needs to be updated. 7328a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 7338a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 7348a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 7358a07d286SRatan Gupta */ 7368a07d286SRatan Gupta 7378a07d286SRatan Gupta void handleBaseDNPatch(const std::vector<std::string>& baseDNList, 7388d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 7398a07d286SRatan Gupta const std::string& ldapServerElementName, 7408a07d286SRatan Gupta const std::string& ldapConfigObject) 7418a07d286SRatan Gupta { 7428a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 7438a07d286SRatan Gupta [asyncResp, baseDNList, 7448a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 7458a07d286SRatan Gupta if (ec) 7468a07d286SRatan Gupta { 7476c51eab1SEd Tanous BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN"; 7488a07d286SRatan Gupta messages::internalError(asyncResp->res); 7498a07d286SRatan Gupta return; 7508a07d286SRatan Gupta } 7518a07d286SRatan Gupta auto& serverTypeJson = 7528a07d286SRatan Gupta asyncResp->res.jsonValue[ldapServerElementName]; 7538a07d286SRatan Gupta auto& searchSettingsJson = 7548a07d286SRatan Gupta serverTypeJson["LDAPService"]["SearchSettings"]; 7556c51eab1SEd Tanous std::vector<std::string> modifiedBaseDNList = {baseDNList.front()}; 7566c51eab1SEd Tanous searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList; 7578a07d286SRatan Gupta if (baseDNList.size() > 1) 7588a07d286SRatan Gupta { 7598a07d286SRatan Gupta messages::propertyValueModified(asyncResp->res, 7608a07d286SRatan Gupta "BaseDistinguishedNames", 7618a07d286SRatan Gupta baseDNList.front()); 7628a07d286SRatan Gupta } 7638a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the base DN"; 7648a07d286SRatan Gupta }, 7658a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 7668a07d286SRatan Gupta ldapConfigInterface, "LDAPBaseDN", 7678a07d286SRatan Gupta std::variant<std::string>(baseDNList.front())); 7688a07d286SRatan Gupta } 7698a07d286SRatan Gupta /** 7708a07d286SRatan Gupta * @brief updates the LDAP user name attribute and updates the 7718a07d286SRatan Gupta json response with the new value. 7728a07d286SRatan Gupta * @param userNameAttribute attribute to be updated. 7738a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 7748a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 7758a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 7768a07d286SRatan Gupta */ 7778a07d286SRatan Gupta 7788d1b46d7Szhanghch05 void handleUserNameAttrPatch( 7798d1b46d7Szhanghch05 const std::string& userNameAttribute, 7808d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 7818a07d286SRatan Gupta const std::string& ldapServerElementName, 7828a07d286SRatan Gupta const std::string& ldapConfigObject) 7838a07d286SRatan Gupta { 7848a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 7858a07d286SRatan Gupta [asyncResp, userNameAttribute, 7868a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 7878a07d286SRatan Gupta if (ec) 7888a07d286SRatan Gupta { 789c61704abSGunnar Mills BMCWEB_LOG_DEBUG << "Error Occurred in Updating the " 7908a07d286SRatan Gupta "username attribute"; 7918a07d286SRatan Gupta messages::internalError(asyncResp->res); 7928a07d286SRatan Gupta return; 7938a07d286SRatan Gupta } 7948a07d286SRatan Gupta auto& serverTypeJson = 7958a07d286SRatan Gupta asyncResp->res.jsonValue[ldapServerElementName]; 7968a07d286SRatan Gupta auto& searchSettingsJson = 7978a07d286SRatan Gupta serverTypeJson["LDAPService"]["SearchSettings"]; 7988a07d286SRatan Gupta searchSettingsJson["UsernameAttribute"] = userNameAttribute; 7998a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the user name attr."; 8008a07d286SRatan Gupta }, 8018a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 8028a07d286SRatan Gupta ldapConfigInterface, "UserNameAttribute", 8038a07d286SRatan Gupta std::variant<std::string>(userNameAttribute)); 8048a07d286SRatan Gupta } 8058a07d286SRatan Gupta /** 8068a07d286SRatan Gupta * @brief updates the LDAP group attribute and updates the 8078a07d286SRatan Gupta json response with the new value. 8088a07d286SRatan Gupta * @param groupsAttribute attribute to be updated. 8098a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 8108a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 8118a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 8128a07d286SRatan Gupta */ 8138a07d286SRatan Gupta 8148d1b46d7Szhanghch05 void handleGroupNameAttrPatch( 8158d1b46d7Szhanghch05 const std::string& groupsAttribute, 8168d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8178a07d286SRatan Gupta const std::string& ldapServerElementName, 8188a07d286SRatan Gupta const std::string& ldapConfigObject) 8198a07d286SRatan Gupta { 8208a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 8218a07d286SRatan Gupta [asyncResp, groupsAttribute, 8228a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 8238a07d286SRatan Gupta if (ec) 8248a07d286SRatan Gupta { 825c61704abSGunnar Mills BMCWEB_LOG_DEBUG << "Error Occurred in Updating the " 8268a07d286SRatan Gupta "groupname attribute"; 8278a07d286SRatan Gupta messages::internalError(asyncResp->res); 8288a07d286SRatan Gupta return; 8298a07d286SRatan Gupta } 8308a07d286SRatan Gupta auto& serverTypeJson = 8318a07d286SRatan Gupta asyncResp->res.jsonValue[ldapServerElementName]; 8328a07d286SRatan Gupta auto& searchSettingsJson = 8338a07d286SRatan Gupta serverTypeJson["LDAPService"]["SearchSettings"]; 8348a07d286SRatan Gupta searchSettingsJson["GroupsAttribute"] = groupsAttribute; 8358a07d286SRatan Gupta BMCWEB_LOG_DEBUG << "Updated the groupname attr"; 8368a07d286SRatan Gupta }, 8378a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 8388a07d286SRatan Gupta ldapConfigInterface, "GroupNameAttribute", 8398a07d286SRatan Gupta std::variant<std::string>(groupsAttribute)); 8408a07d286SRatan Gupta } 8418a07d286SRatan Gupta /** 8428a07d286SRatan Gupta * @brief updates the LDAP service enable and updates the 8438a07d286SRatan Gupta json response with the new value. 8448a07d286SRatan Gupta * @param input JSON data. 8458a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 8468a07d286SRatan Gupta * @param ldapServerElementName Type of LDAP 8478a07d286SRatan Gupta server(openLDAP/ActiveDirectory) 8488a07d286SRatan Gupta */ 8498a07d286SRatan Gupta 8508d1b46d7Szhanghch05 void handleServiceEnablePatch( 8516c51eab1SEd Tanous bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8528a07d286SRatan Gupta const std::string& ldapServerElementName, 8538a07d286SRatan Gupta const std::string& ldapConfigObject) 8548a07d286SRatan Gupta { 8558a07d286SRatan Gupta crow::connections::systemBus->async_method_call( 8568a07d286SRatan Gupta [asyncResp, serviceEnabled, 8578a07d286SRatan Gupta ldapServerElementName](const boost::system::error_code ec) { 8588a07d286SRatan Gupta if (ec) 8598a07d286SRatan Gupta { 8608a07d286SRatan Gupta BMCWEB_LOG_DEBUG 861c61704abSGunnar Mills << "Error Occurred in Updating the service enable"; 8628a07d286SRatan Gupta messages::internalError(asyncResp->res); 8638a07d286SRatan Gupta return; 8648a07d286SRatan Gupta } 8656c51eab1SEd Tanous asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] = 8668a07d286SRatan Gupta serviceEnabled; 8676c51eab1SEd Tanous BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled; 8688a07d286SRatan Gupta }, 8698a07d286SRatan Gupta ldapDbusService, ldapConfigObject, propertyInterface, "Set", 8708a07d286SRatan Gupta ldapEnableInterface, "Enabled", std::variant<bool>(serviceEnabled)); 8718a07d286SRatan Gupta } 8728a07d286SRatan Gupta 8736c51eab1SEd Tanous void handleAuthMethodsPatch(nlohmann::json& input, 8748d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 87578158631SZbigniew Kurzynski { 87678158631SZbigniew Kurzynski std::optional<bool> basicAuth; 87778158631SZbigniew Kurzynski std::optional<bool> cookie; 87878158631SZbigniew Kurzynski std::optional<bool> sessionToken; 87978158631SZbigniew Kurzynski std::optional<bool> xToken; 880501f1e58SZbigniew Kurzynski std::optional<bool> tls; 88178158631SZbigniew Kurzynski 88278158631SZbigniew Kurzynski if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth, 88378158631SZbigniew Kurzynski "Cookie", cookie, "SessionToken", sessionToken, 884501f1e58SZbigniew Kurzynski "XToken", xToken, "TLS", tls)) 88578158631SZbigniew Kurzynski { 88678158631SZbigniew Kurzynski BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag"; 88778158631SZbigniew Kurzynski return; 88878158631SZbigniew Kurzynski } 88978158631SZbigniew Kurzynski 89078158631SZbigniew Kurzynski // Make a copy of methods configuration 89152cc112dSEd Tanous persistent_data::AuthConfigMethods authMethodsConfig = 89252cc112dSEd Tanous persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); 89378158631SZbigniew Kurzynski 89478158631SZbigniew Kurzynski if (basicAuth) 89578158631SZbigniew Kurzynski { 896f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION 897f16f6263SAlan Kuo messages::actionNotSupported( 898f16f6263SAlan Kuo asyncResp->res, "Setting BasicAuth when basic-auth feature " 899f16f6263SAlan Kuo "is disabled"); 900f16f6263SAlan Kuo return; 901f16f6263SAlan Kuo #endif 90278158631SZbigniew Kurzynski authMethodsConfig.basic = *basicAuth; 90378158631SZbigniew Kurzynski } 90478158631SZbigniew Kurzynski 90578158631SZbigniew Kurzynski if (cookie) 90678158631SZbigniew Kurzynski { 907f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION 9086c51eab1SEd Tanous messages::actionNotSupported(asyncResp->res, 9096c51eab1SEd Tanous "Setting Cookie when cookie-auth feature " 910f16f6263SAlan Kuo "is disabled"); 911f16f6263SAlan Kuo return; 912f16f6263SAlan Kuo #endif 91378158631SZbigniew Kurzynski authMethodsConfig.cookie = *cookie; 91478158631SZbigniew Kurzynski } 91578158631SZbigniew Kurzynski 91678158631SZbigniew Kurzynski if (sessionToken) 91778158631SZbigniew Kurzynski { 918f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION 919f16f6263SAlan Kuo messages::actionNotSupported( 9206c51eab1SEd Tanous asyncResp->res, "Setting SessionToken when session-auth feature " 921f16f6263SAlan Kuo "is disabled"); 922f16f6263SAlan Kuo return; 923f16f6263SAlan Kuo #endif 92478158631SZbigniew Kurzynski authMethodsConfig.sessionToken = *sessionToken; 92578158631SZbigniew Kurzynski } 92678158631SZbigniew Kurzynski 92778158631SZbigniew Kurzynski if (xToken) 92878158631SZbigniew Kurzynski { 929f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION 9306c51eab1SEd Tanous messages::actionNotSupported(asyncResp->res, 9316c51eab1SEd Tanous "Setting XToken when xtoken-auth feature " 932f16f6263SAlan Kuo "is disabled"); 933f16f6263SAlan Kuo return; 934f16f6263SAlan Kuo #endif 93578158631SZbigniew Kurzynski authMethodsConfig.xtoken = *xToken; 93678158631SZbigniew Kurzynski } 93778158631SZbigniew Kurzynski 938501f1e58SZbigniew Kurzynski if (tls) 939501f1e58SZbigniew Kurzynski { 940f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION 9416c51eab1SEd Tanous messages::actionNotSupported(asyncResp->res, 9426c51eab1SEd Tanous "Setting TLS when mutual-tls-auth feature " 943f16f6263SAlan Kuo "is disabled"); 944f16f6263SAlan Kuo return; 945f16f6263SAlan Kuo #endif 946501f1e58SZbigniew Kurzynski authMethodsConfig.tls = *tls; 947501f1e58SZbigniew Kurzynski } 948501f1e58SZbigniew Kurzynski 94978158631SZbigniew Kurzynski if (!authMethodsConfig.basic && !authMethodsConfig.cookie && 950501f1e58SZbigniew Kurzynski !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken && 951501f1e58SZbigniew Kurzynski !authMethodsConfig.tls) 95278158631SZbigniew Kurzynski { 95378158631SZbigniew Kurzynski // Do not allow user to disable everything 95478158631SZbigniew Kurzynski messages::actionNotSupported(asyncResp->res, 95578158631SZbigniew Kurzynski "of disabling all available methods"); 95678158631SZbigniew Kurzynski return; 95778158631SZbigniew Kurzynski } 95878158631SZbigniew Kurzynski 95952cc112dSEd Tanous persistent_data::SessionStore::getInstance().updateAuthMethodsConfig( 96052cc112dSEd Tanous authMethodsConfig); 96178158631SZbigniew Kurzynski // Save configuration immediately 96252cc112dSEd Tanous persistent_data::getConfig().writeData(); 96378158631SZbigniew Kurzynski 96478158631SZbigniew Kurzynski messages::success(asyncResp->res); 96578158631SZbigniew Kurzynski } 96678158631SZbigniew Kurzynski 9678a07d286SRatan Gupta /** 9688a07d286SRatan Gupta * @brief Get the required values from the given JSON, validates the 9698a07d286SRatan Gupta * value and create the LDAP config object. 9708a07d286SRatan Gupta * @param input JSON data 9718a07d286SRatan Gupta * @param asyncResp pointer to the JSON response 9728a07d286SRatan Gupta * @param serverType Type of LDAP server(openLDAP/ActiveDirectory) 9738a07d286SRatan Gupta */ 9748a07d286SRatan Gupta 9756c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input, 9768d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 9778a07d286SRatan Gupta const std::string& serverType) 9788a07d286SRatan Gupta { 979eb2bbe56SRatan Gupta std::string dbusObjectPath; 980eb2bbe56SRatan Gupta if (serverType == "ActiveDirectory") 981eb2bbe56SRatan Gupta { 9822c70f800SEd Tanous dbusObjectPath = adConfigObject; 983eb2bbe56SRatan Gupta } 984eb2bbe56SRatan Gupta else if (serverType == "LDAP") 985eb2bbe56SRatan Gupta { 98623a21a1cSEd Tanous dbusObjectPath = ldapConfigObjectName; 987eb2bbe56SRatan Gupta } 988cb13a392SEd Tanous else 989cb13a392SEd Tanous { 990cb13a392SEd Tanous return; 991cb13a392SEd Tanous } 992eb2bbe56SRatan Gupta 9938a07d286SRatan Gupta std::optional<nlohmann::json> authentication; 9948a07d286SRatan Gupta std::optional<nlohmann::json> ldapService; 9958a07d286SRatan Gupta std::optional<std::vector<std::string>> serviceAddressList; 9968a07d286SRatan Gupta std::optional<bool> serviceEnabled; 9978a07d286SRatan Gupta std::optional<std::vector<std::string>> baseDNList; 9988a07d286SRatan Gupta std::optional<std::string> userNameAttribute; 9998a07d286SRatan Gupta std::optional<std::string> groupsAttribute; 10008a07d286SRatan Gupta std::optional<std::string> userName; 10018a07d286SRatan Gupta std::optional<std::string> password; 100206785244SRatan Gupta std::optional<std::vector<nlohmann::json>> remoteRoleMapData; 10038a07d286SRatan Gupta 10048a07d286SRatan Gupta if (!json_util::readJson(input, asyncResp->res, "Authentication", 10058a07d286SRatan Gupta authentication, "LDAPService", ldapService, 10068a07d286SRatan Gupta "ServiceAddresses", serviceAddressList, 100706785244SRatan Gupta "ServiceEnabled", serviceEnabled, 100806785244SRatan Gupta "RemoteRoleMapping", remoteRoleMapData)) 10098a07d286SRatan Gupta { 10108a07d286SRatan Gupta return; 10118a07d286SRatan Gupta } 10128a07d286SRatan Gupta 10138a07d286SRatan Gupta if (authentication) 10148a07d286SRatan Gupta { 10158a07d286SRatan Gupta parseLDAPAuthenticationJson(*authentication, asyncResp, userName, 10168a07d286SRatan Gupta password); 10178a07d286SRatan Gupta } 10188a07d286SRatan Gupta if (ldapService) 10198a07d286SRatan Gupta { 10208a07d286SRatan Gupta parseLDAPServiceJson(*ldapService, asyncResp, baseDNList, 10218a07d286SRatan Gupta userNameAttribute, groupsAttribute); 10228a07d286SRatan Gupta } 10238a07d286SRatan Gupta if (serviceAddressList) 10248a07d286SRatan Gupta { 10258a07d286SRatan Gupta if ((*serviceAddressList).size() == 0) 10268a07d286SRatan Gupta { 10278a07d286SRatan Gupta messages::propertyValueNotInList(asyncResp->res, "[]", 10288a07d286SRatan Gupta "ServiceAddress"); 10298a07d286SRatan Gupta return; 10308a07d286SRatan Gupta } 10318a07d286SRatan Gupta } 10328a07d286SRatan Gupta if (baseDNList) 10338a07d286SRatan Gupta { 10348a07d286SRatan Gupta if ((*baseDNList).size() == 0) 10358a07d286SRatan Gupta { 10368a07d286SRatan Gupta messages::propertyValueNotInList(asyncResp->res, "[]", 10378a07d286SRatan Gupta "BaseDistinguishedNames"); 10388a07d286SRatan Gupta return; 10398a07d286SRatan Gupta } 10408a07d286SRatan Gupta } 10418a07d286SRatan Gupta 10428a07d286SRatan Gupta // nothing to update, then return 10438a07d286SRatan Gupta if (!userName && !password && !serviceAddressList && !baseDNList && 104406785244SRatan Gupta !userNameAttribute && !groupsAttribute && !serviceEnabled && 104506785244SRatan Gupta !remoteRoleMapData) 10468a07d286SRatan Gupta { 10478a07d286SRatan Gupta return; 10488a07d286SRatan Gupta } 10498a07d286SRatan Gupta 10508a07d286SRatan Gupta // Get the existing resource first then keep modifying 10518a07d286SRatan Gupta // whenever any property gets updated. 10526c51eab1SEd Tanous getLDAPConfigData(serverType, [asyncResp, userName, password, baseDNList, 10536c51eab1SEd Tanous userNameAttribute, groupsAttribute, 10546c51eab1SEd Tanous serviceAddressList, serviceEnabled, 10556c51eab1SEd Tanous dbusObjectPath, remoteRoleMapData]( 10566c51eab1SEd Tanous bool success, 10576c51eab1SEd Tanous const LDAPConfigData& confData, 105823a21a1cSEd Tanous const std::string& serverT) { 10598a07d286SRatan Gupta if (!success) 10608a07d286SRatan Gupta { 10618a07d286SRatan Gupta messages::internalError(asyncResp->res); 10628a07d286SRatan Gupta return; 10638a07d286SRatan Gupta } 10646c51eab1SEd Tanous parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT); 10658a07d286SRatan Gupta if (confData.serviceEnabled) 10668a07d286SRatan Gupta { 10678a07d286SRatan Gupta // Disable the service first and update the rest of 10688a07d286SRatan Gupta // the properties. 10696c51eab1SEd Tanous handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath); 10708a07d286SRatan Gupta } 10718a07d286SRatan Gupta 10728a07d286SRatan Gupta if (serviceAddressList) 10738a07d286SRatan Gupta { 10746c51eab1SEd Tanous handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT, 10756c51eab1SEd Tanous dbusObjectPath); 10768a07d286SRatan Gupta } 10778a07d286SRatan Gupta if (userName) 10788a07d286SRatan Gupta { 10796c51eab1SEd Tanous handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath); 10808a07d286SRatan Gupta } 10818a07d286SRatan Gupta if (password) 10828a07d286SRatan Gupta { 10836c51eab1SEd Tanous handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath); 10848a07d286SRatan Gupta } 10858a07d286SRatan Gupta 10868a07d286SRatan Gupta if (baseDNList) 10878a07d286SRatan Gupta { 10886c51eab1SEd Tanous handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath); 10898a07d286SRatan Gupta } 10908a07d286SRatan Gupta if (userNameAttribute) 10918a07d286SRatan Gupta { 10926c51eab1SEd Tanous handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT, 10936c51eab1SEd Tanous dbusObjectPath); 10948a07d286SRatan Gupta } 10958a07d286SRatan Gupta if (groupsAttribute) 10968a07d286SRatan Gupta { 10976c51eab1SEd Tanous handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT, 10986c51eab1SEd Tanous dbusObjectPath); 10998a07d286SRatan Gupta } 11008a07d286SRatan Gupta if (serviceEnabled) 11018a07d286SRatan Gupta { 11028a07d286SRatan Gupta // if user has given the value as true then enable 11038a07d286SRatan Gupta // the service. if user has given false then no-op 11048a07d286SRatan Gupta // as service is already stopped. 11058a07d286SRatan Gupta if (*serviceEnabled) 11068a07d286SRatan Gupta { 11076c51eab1SEd Tanous handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT, 11086c51eab1SEd Tanous dbusObjectPath); 11098a07d286SRatan Gupta } 11108a07d286SRatan Gupta } 11118a07d286SRatan Gupta else 11128a07d286SRatan Gupta { 11138a07d286SRatan Gupta // if user has not given the service enabled value 11148a07d286SRatan Gupta // then revert it to the same state as it was 11158a07d286SRatan Gupta // before. 11168a07d286SRatan Gupta handleServiceEnablePatch(confData.serviceEnabled, asyncResp, 111723a21a1cSEd Tanous serverT, dbusObjectPath); 11188a07d286SRatan Gupta } 111906785244SRatan Gupta 112006785244SRatan Gupta if (remoteRoleMapData) 112106785244SRatan Gupta { 11226c51eab1SEd Tanous handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT, 11236c51eab1SEd Tanous *remoteRoleMapData); 112406785244SRatan Gupta } 11258a07d286SRatan Gupta }); 11268a07d286SRatan Gupta } 1127d4b5443fSEd Tanous 11286c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp, 11296c51eab1SEd Tanous const std::string& username, 11306c51eab1SEd Tanous std::optional<std::string> password, 11316c51eab1SEd Tanous std::optional<bool> enabled, 11326c51eab1SEd Tanous std::optional<std::string> roleId, 11336c51eab1SEd Tanous std::optional<bool> locked) 11341abe55efSEd Tanous { 11356c51eab1SEd Tanous std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username; 11366c51eab1SEd Tanous dbus::utility::escapePathForDbus(dbusObjectPath); 11376c51eab1SEd Tanous 11386c51eab1SEd Tanous dbus::utility::checkDbusPathExists( 11396c51eab1SEd Tanous dbusObjectPath, 11406c51eab1SEd Tanous [dbusObjectPath(std::move(dbusObjectPath)), username, 11416c51eab1SEd Tanous password(std::move(password)), roleId(std::move(roleId)), enabled, 11426c51eab1SEd Tanous locked, asyncResp{std::move(asyncResp)}](int rc) { 11436c51eab1SEd Tanous if (!rc) 11446c51eab1SEd Tanous { 11456c51eab1SEd Tanous messages::resourceNotFound( 11466c51eab1SEd Tanous asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount", 11476c51eab1SEd Tanous username); 11486c51eab1SEd Tanous return; 11496c51eab1SEd Tanous } 11506c51eab1SEd Tanous 11516c51eab1SEd Tanous if (password) 11526c51eab1SEd Tanous { 11536c51eab1SEd Tanous int retval = pamUpdatePassword(username, *password); 11546c51eab1SEd Tanous 11556c51eab1SEd Tanous if (retval == PAM_USER_UNKNOWN) 11566c51eab1SEd Tanous { 11576c51eab1SEd Tanous messages::resourceNotFound( 11586c51eab1SEd Tanous asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount", 11596c51eab1SEd Tanous username); 11606c51eab1SEd Tanous } 11616c51eab1SEd Tanous else if (retval == PAM_AUTHTOK_ERR) 11626c51eab1SEd Tanous { 11636c51eab1SEd Tanous // If password is invalid 11646c51eab1SEd Tanous messages::propertyValueFormatError(asyncResp->res, 11656c51eab1SEd Tanous *password, "Password"); 11666c51eab1SEd Tanous BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; 11676c51eab1SEd Tanous } 11686c51eab1SEd Tanous else if (retval != PAM_SUCCESS) 11696c51eab1SEd Tanous { 11706c51eab1SEd Tanous messages::internalError(asyncResp->res); 11716c51eab1SEd Tanous return; 11726c51eab1SEd Tanous } 11736c51eab1SEd Tanous } 11746c51eab1SEd Tanous 11756c51eab1SEd Tanous if (enabled) 11766c51eab1SEd Tanous { 11776c51eab1SEd Tanous crow::connections::systemBus->async_method_call( 11786c51eab1SEd Tanous [asyncResp](const boost::system::error_code ec) { 11796c51eab1SEd Tanous if (ec) 11806c51eab1SEd Tanous { 11816c51eab1SEd Tanous BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 11826c51eab1SEd Tanous messages::internalError(asyncResp->res); 11836c51eab1SEd Tanous return; 11846c51eab1SEd Tanous } 11856c51eab1SEd Tanous messages::success(asyncResp->res); 11866c51eab1SEd Tanous return; 11876c51eab1SEd Tanous }, 11886c51eab1SEd Tanous "xyz.openbmc_project.User.Manager", dbusObjectPath.c_str(), 11896c51eab1SEd Tanous "org.freedesktop.DBus.Properties", "Set", 11906c51eab1SEd Tanous "xyz.openbmc_project.User.Attributes", "UserEnabled", 11916c51eab1SEd Tanous std::variant<bool>{*enabled}); 11926c51eab1SEd Tanous } 11936c51eab1SEd Tanous 11946c51eab1SEd Tanous if (roleId) 11956c51eab1SEd Tanous { 11966c51eab1SEd Tanous std::string priv = getPrivilegeFromRoleId(*roleId); 11976c51eab1SEd Tanous if (priv.empty()) 11986c51eab1SEd Tanous { 11996c51eab1SEd Tanous messages::propertyValueNotInList(asyncResp->res, *roleId, 12006c51eab1SEd Tanous "RoleId"); 12016c51eab1SEd Tanous return; 12026c51eab1SEd Tanous } 12036c51eab1SEd Tanous if (priv == "priv-noaccess") 12046c51eab1SEd Tanous { 12056c51eab1SEd Tanous priv = ""; 12066c51eab1SEd Tanous } 12076c51eab1SEd Tanous 12086c51eab1SEd Tanous crow::connections::systemBus->async_method_call( 12096c51eab1SEd Tanous [asyncResp](const boost::system::error_code ec) { 12106c51eab1SEd Tanous if (ec) 12116c51eab1SEd Tanous { 12126c51eab1SEd Tanous BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 12136c51eab1SEd Tanous messages::internalError(asyncResp->res); 12146c51eab1SEd Tanous return; 12156c51eab1SEd Tanous } 12166c51eab1SEd Tanous messages::success(asyncResp->res); 12176c51eab1SEd Tanous }, 12186c51eab1SEd Tanous "xyz.openbmc_project.User.Manager", dbusObjectPath.c_str(), 12196c51eab1SEd Tanous "org.freedesktop.DBus.Properties", "Set", 12206c51eab1SEd Tanous "xyz.openbmc_project.User.Attributes", "UserPrivilege", 12216c51eab1SEd Tanous std::variant<std::string>{priv}); 12226c51eab1SEd Tanous } 12236c51eab1SEd Tanous 12246c51eab1SEd Tanous if (locked) 12256c51eab1SEd Tanous { 12266c51eab1SEd Tanous // admin can unlock the account which is locked by 12276c51eab1SEd Tanous // successive authentication failures but admin should 12286c51eab1SEd Tanous // not be allowed to lock an account. 12296c51eab1SEd Tanous if (*locked) 12306c51eab1SEd Tanous { 12316c51eab1SEd Tanous messages::propertyValueNotInList(asyncResp->res, "true", 12326c51eab1SEd Tanous "Locked"); 12336c51eab1SEd Tanous return; 12346c51eab1SEd Tanous } 12356c51eab1SEd Tanous 12366c51eab1SEd Tanous crow::connections::systemBus->async_method_call( 12376c51eab1SEd Tanous [asyncResp](const boost::system::error_code ec) { 12386c51eab1SEd Tanous if (ec) 12396c51eab1SEd Tanous { 12406c51eab1SEd Tanous BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 12416c51eab1SEd Tanous messages::internalError(asyncResp->res); 12426c51eab1SEd Tanous return; 12436c51eab1SEd Tanous } 12446c51eab1SEd Tanous messages::success(asyncResp->res); 12456c51eab1SEd Tanous return; 12466c51eab1SEd Tanous }, 12476c51eab1SEd Tanous "xyz.openbmc_project.User.Manager", dbusObjectPath.c_str(), 12486c51eab1SEd Tanous "org.freedesktop.DBus.Properties", "Set", 12496c51eab1SEd Tanous "xyz.openbmc_project.User.Attributes", 12506c51eab1SEd Tanous "UserLockedForFailedAttempt", std::variant<bool>{*locked}); 12516c51eab1SEd Tanous } 12526c51eab1SEd Tanous }); 12536c51eab1SEd Tanous } 12546c51eab1SEd Tanous 12556c51eab1SEd Tanous inline void requestAccountServiceRoutes(App& app) 12566c51eab1SEd Tanous { 12576c51eab1SEd Tanous 12586c51eab1SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/") 12596c51eab1SEd Tanous .privileges({{"Login"}}) 12606c51eab1SEd Tanous .methods( 1261*72048780SAbhishek Patel boost::beast::http::verb::get)([](const crow::Request& req, 12626c51eab1SEd Tanous const std::shared_ptr< 12636c51eab1SEd Tanous bmcweb::AsyncResp>& asyncResp) 12646c51eab1SEd Tanous -> void { 126552cc112dSEd Tanous const persistent_data::AuthConfigMethods& authMethodsConfig = 12666c51eab1SEd Tanous persistent_data::SessionStore::getInstance() 12676c51eab1SEd Tanous .getAuthMethodsConfig(); 126878158631SZbigniew Kurzynski 12698d1b46d7Szhanghch05 asyncResp->res.jsonValue = { 12703d958bbcSAppaRao Puli {"@odata.id", "/redfish/v1/AccountService"}, 12713d958bbcSAppaRao Puli {"@odata.type", "#AccountService." 1272ba9dd4a8SGunnar Mills "v1_5_0.AccountService"}, 12733d958bbcSAppaRao Puli {"Id", "AccountService"}, 12743d958bbcSAppaRao Puli {"Name", "Account Service"}, 12753d958bbcSAppaRao Puli {"Description", "Account Service"}, 12763d958bbcSAppaRao Puli {"ServiceEnabled", true}, 1277343ff2e1SAppaRao Puli {"MaxPasswordLength", 20}, 12783d958bbcSAppaRao Puli {"Accounts", 12793d958bbcSAppaRao Puli {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}}, 128037cce918SMarri Devender Rao {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}}, 128178158631SZbigniew Kurzynski {"Oem", 128278158631SZbigniew Kurzynski {{"OpenBMC", 128378158631SZbigniew Kurzynski {{"@odata.type", "#OemAccountService.v1_0_0.AccountService"}, 128478158631SZbigniew Kurzynski {"AuthMethods", 128578158631SZbigniew Kurzynski { 128678158631SZbigniew Kurzynski {"BasicAuth", authMethodsConfig.basic}, 128778158631SZbigniew Kurzynski {"SessionToken", authMethodsConfig.sessionToken}, 128878158631SZbigniew Kurzynski {"XToken", authMethodsConfig.xtoken}, 128978158631SZbigniew Kurzynski {"Cookie", authMethodsConfig.cookie}, 1290501f1e58SZbigniew Kurzynski {"TLS", authMethodsConfig.tls}, 1291*72048780SAbhishek Patel }}}}}}}; 1292*72048780SAbhishek Patel // /redfish/v1/AccountService/LDAP/Certificates is something only 1293*72048780SAbhishek Patel // ConfigureManager can access then only display when the user has 1294*72048780SAbhishek Patel // permissions ConfigureManager 1295*72048780SAbhishek Patel Privileges effectiveUserPrivileges = 1296*72048780SAbhishek Patel redfish::getUserPrivileges(req.userRole); 1297*72048780SAbhishek Patel 1298*72048780SAbhishek Patel if (isOperationAllowedWithPrivileges({{"ConfigureManager"}}, 1299*72048780SAbhishek Patel effectiveUserPrivileges)) 1300*72048780SAbhishek Patel { 1301*72048780SAbhishek Patel asyncResp->res.jsonValue["LDAP"] = { 1302*72048780SAbhishek Patel {"Certificates", 130337cce918SMarri Devender Rao {{"@odata.id", 1304*72048780SAbhishek Patel "/redfish/v1/AccountService/LDAP/Certificates"}}}}; 1305*72048780SAbhishek Patel } 13063d958bbcSAppaRao Puli crow::connections::systemBus->async_method_call( 13073d958bbcSAppaRao Puli [asyncResp]( 13083d958bbcSAppaRao Puli const boost::system::error_code ec, 13096c51eab1SEd Tanous const std::vector< 13106c51eab1SEd Tanous std::pair<std::string, 13116c51eab1SEd Tanous std::variant<uint32_t, uint16_t, uint8_t>>>& 13123d958bbcSAppaRao Puli propertiesList) { 13133d958bbcSAppaRao Puli if (ec) 13143d958bbcSAppaRao Puli { 13153d958bbcSAppaRao Puli messages::internalError(asyncResp->res); 13163d958bbcSAppaRao Puli return; 13173d958bbcSAppaRao Puli } 13183d958bbcSAppaRao Puli BMCWEB_LOG_DEBUG << "Got " << propertiesList.size() 13193d958bbcSAppaRao Puli << "properties for AccountService"; 13203d958bbcSAppaRao Puli for (const std::pair<std::string, 13216c51eab1SEd Tanous std::variant<uint32_t, uint16_t, 13226c51eab1SEd Tanous uint8_t>>& property : 13236c51eab1SEd Tanous propertiesList) 13243d958bbcSAppaRao Puli { 13253d958bbcSAppaRao Puli if (property.first == "MinPasswordLength") 13263d958bbcSAppaRao Puli { 13273d958bbcSAppaRao Puli const uint8_t* value = 1328abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 13293d958bbcSAppaRao Puli if (value != nullptr) 13303d958bbcSAppaRao Puli { 13313d958bbcSAppaRao Puli asyncResp->res.jsonValue["MinPasswordLength"] = 13323d958bbcSAppaRao Puli *value; 13333d958bbcSAppaRao Puli } 13343d958bbcSAppaRao Puli } 13353d958bbcSAppaRao Puli if (property.first == "AccountUnlockTimeout") 13363d958bbcSAppaRao Puli { 13373d958bbcSAppaRao Puli const uint32_t* value = 1338abf2add6SEd Tanous std::get_if<uint32_t>(&property.second); 13393d958bbcSAppaRao Puli if (value != nullptr) 13403d958bbcSAppaRao Puli { 13416c51eab1SEd Tanous asyncResp->res 13426c51eab1SEd Tanous .jsonValue["AccountLockoutDuration"] = 13433d958bbcSAppaRao Puli *value; 13443d958bbcSAppaRao Puli } 13453d958bbcSAppaRao Puli } 13463d958bbcSAppaRao Puli if (property.first == "MaxLoginAttemptBeforeLockout") 13473d958bbcSAppaRao Puli { 13483d958bbcSAppaRao Puli const uint16_t* value = 1349abf2add6SEd Tanous std::get_if<uint16_t>(&property.second); 13503d958bbcSAppaRao Puli if (value != nullptr) 13513d958bbcSAppaRao Puli { 13523d958bbcSAppaRao Puli asyncResp->res 13536c51eab1SEd Tanous .jsonValue["AccountLockoutThreshold"] = 13546c51eab1SEd Tanous *value; 13553d958bbcSAppaRao Puli } 13563d958bbcSAppaRao Puli } 13573d958bbcSAppaRao Puli } 13583d958bbcSAppaRao Puli }, 13593d958bbcSAppaRao Puli "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 13603d958bbcSAppaRao Puli "org.freedesktop.DBus.Properties", "GetAll", 13613d958bbcSAppaRao Puli "xyz.openbmc_project.User.AccountPolicy"); 13626973a582SRatan Gupta 1363ab828d7cSRatan Gupta auto callback = [asyncResp](bool success, LDAPConfigData& confData, 1364ab828d7cSRatan Gupta const std::string& ldapType) { 1365cb13a392SEd Tanous if (!success) 1366cb13a392SEd Tanous { 1367cb13a392SEd Tanous return; 1368cb13a392SEd Tanous } 13696c51eab1SEd Tanous parseLDAPConfigData(asyncResp->res.jsonValue, confData, 13706c51eab1SEd Tanous ldapType); 1371ab828d7cSRatan Gupta }; 1372ab828d7cSRatan Gupta 1373ab828d7cSRatan Gupta getLDAPConfigData("LDAP", callback); 1374ab828d7cSRatan Gupta getLDAPConfigData("ActiveDirectory", callback); 13756c51eab1SEd Tanous }); 13766973a582SRatan Gupta 13776c51eab1SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/") 1378cef1ddfbSEd Tanous .privileges({{"Login"}}) 13796c51eab1SEd Tanous .methods(boost::beast::http::verb::get)( 13806c51eab1SEd Tanous [](const crow::Request& req, 13816c51eab1SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void { 13828d1b46d7Szhanghch05 asyncResp->res.jsonValue = { 13838d1b46d7Szhanghch05 {"@odata.id", "/redfish/v1/AccountService/Accounts"}, 13840f74e643SEd Tanous {"@odata.type", "#ManagerAccountCollection." 13850f74e643SEd Tanous "ManagerAccountCollection"}, 13860f74e643SEd Tanous {"Name", "Accounts Collection"}, 13870f74e643SEd Tanous {"Description", "BMC User Accounts"}}; 13880f74e643SEd Tanous 13896c51eab1SEd Tanous Privileges effectiveUserPrivileges = 13906c51eab1SEd Tanous redfish::getUserPrivileges(req.userRole); 13916c51eab1SEd Tanous 13926c51eab1SEd Tanous std::string thisUser = req.session->username; 13936c51eab1SEd Tanous 1394b9b2e0b2SEd Tanous crow::connections::systemBus->async_method_call( 1395cef1ddfbSEd Tanous [asyncResp, thisUser, effectiveUserPrivileges]( 1396cef1ddfbSEd Tanous const boost::system::error_code ec, 1397b9b2e0b2SEd Tanous const ManagedObjectType& users) { 1398b9b2e0b2SEd Tanous if (ec) 1399b9b2e0b2SEd Tanous { 1400f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1401b9b2e0b2SEd Tanous return; 1402b9b2e0b2SEd Tanous } 1403b9b2e0b2SEd Tanous 1404cef1ddfbSEd Tanous bool userCanSeeAllAccounts = 1405cef1ddfbSEd Tanous effectiveUserPrivileges.isSupersetOf( 1406cef1ddfbSEd Tanous {{"ConfigureUsers"}}); 1407cef1ddfbSEd Tanous 1408cef1ddfbSEd Tanous bool userCanSeeSelf = 1409cef1ddfbSEd Tanous effectiveUserPrivileges.isSupersetOf( 1410cef1ddfbSEd Tanous {{"ConfigureSelf"}}); 1411cef1ddfbSEd Tanous 1412b9b2e0b2SEd Tanous nlohmann::json& memberArray = 1413b9b2e0b2SEd Tanous asyncResp->res.jsonValue["Members"]; 1414b9b2e0b2SEd Tanous memberArray = nlohmann::json::array(); 1415b9b2e0b2SEd Tanous 14162dfd18efSEd Tanous for (auto& userpath : users) 1417b9b2e0b2SEd Tanous { 14182dfd18efSEd Tanous std::string user = userpath.first.filename(); 14192dfd18efSEd Tanous if (user.empty()) 1420b9b2e0b2SEd Tanous { 14212dfd18efSEd Tanous messages::internalError(asyncResp->res); 14222dfd18efSEd Tanous BMCWEB_LOG_ERROR << "Invalid firmware ID"; 14232dfd18efSEd Tanous 14242dfd18efSEd Tanous return; 1425b9b2e0b2SEd Tanous } 1426f365910cSGunnar Mills 1427f365910cSGunnar Mills // As clarified by Redfish here: 1428f365910cSGunnar Mills // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration 14296c51eab1SEd Tanous // Users without ConfigureUsers, only see their own 14306c51eab1SEd Tanous // account. Users with ConfigureUsers, see all 14316c51eab1SEd Tanous // accounts. 1432cef1ddfbSEd Tanous if (userCanSeeAllAccounts || 1433cef1ddfbSEd Tanous (thisUser == user && userCanSeeSelf)) 1434f365910cSGunnar Mills { 1435b9b2e0b2SEd Tanous memberArray.push_back( 1436f365910cSGunnar Mills {{"@odata.id", 14376c51eab1SEd Tanous "/redfish/v1/AccountService/Accounts/" + 14386c51eab1SEd Tanous user}}); 1439b9b2e0b2SEd Tanous } 1440f365910cSGunnar Mills } 1441f365910cSGunnar Mills asyncResp->res.jsonValue["Members@odata.count"] = 1442f365910cSGunnar Mills memberArray.size(); 1443b9b2e0b2SEd Tanous }, 14446c51eab1SEd Tanous "xyz.openbmc_project.User.Manager", 14456c51eab1SEd Tanous "/xyz/openbmc_project/user", 1446b9b2e0b2SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 14476c51eab1SEd Tanous }); 14486c51eab1SEd Tanous 14496c51eab1SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/") 14506c51eab1SEd Tanous .privileges({{"ConfigureUsers"}}) 14516c51eab1SEd Tanous .methods(boost::beast::http::verb::post)([](const crow::Request& req, 14526c51eab1SEd Tanous const std::shared_ptr< 14536c51eab1SEd Tanous bmcweb::AsyncResp>& 14546c51eab1SEd Tanous asyncResp) -> void { 14559712f8acSEd Tanous std::string username; 14569712f8acSEd Tanous std::string password; 1457a24526dcSEd Tanous std::optional<std::string> roleId("User"); 1458a24526dcSEd Tanous std::optional<bool> enabled = true; 14598d1b46d7Szhanghch05 if (!json_util::readJson(req, asyncResp->res, "UserName", username, 14608d1b46d7Szhanghch05 "Password", password, "RoleId", roleId, 14618d1b46d7Szhanghch05 "Enabled", enabled)) 146204ae99ecSEd Tanous { 146304ae99ecSEd Tanous return; 146404ae99ecSEd Tanous } 146504ae99ecSEd Tanous 146654fc587aSNagaraju Goruganti std::string priv = getPrivilegeFromRoleId(*roleId); 146784e12cb7SAppaRao Puli if (priv.empty()) 146804ae99ecSEd Tanous { 14696c51eab1SEd Tanous messages::propertyValueNotInList(asyncResp->res, *roleId, 14706c51eab1SEd Tanous "RoleId"); 147104ae99ecSEd Tanous return; 147204ae99ecSEd Tanous } 147396200606Sjayaprakash Mutyala // TODO: Following override will be reverted once support in 14746c51eab1SEd Tanous // phosphor-user-manager is added. In order to avoid dependency 14756c51eab1SEd Tanous // issues, this is added in bmcweb, which will removed, once 147696200606Sjayaprakash Mutyala // phosphor-user-manager supports priv-noaccess. 147796200606Sjayaprakash Mutyala if (priv == "priv-noaccess") 147896200606Sjayaprakash Mutyala { 147996200606Sjayaprakash Mutyala roleId = ""; 148096200606Sjayaprakash Mutyala } 148196200606Sjayaprakash Mutyala else 148296200606Sjayaprakash Mutyala { 14839712f8acSEd Tanous roleId = priv; 148496200606Sjayaprakash Mutyala } 148504ae99ecSEd Tanous 1486599c71d8SAyushi Smriti // Reading AllGroups property 1487599c71d8SAyushi Smriti crow::connections::systemBus->async_method_call( 1488599c71d8SAyushi Smriti [asyncResp, username, password{std::move(password)}, roleId, 14896c51eab1SEd Tanous enabled]( 14906c51eab1SEd Tanous const boost::system::error_code ec, 1491599c71d8SAyushi Smriti const std::variant<std::vector<std::string>>& allGroups) { 1492599c71d8SAyushi Smriti if (ec) 1493599c71d8SAyushi Smriti { 1494599c71d8SAyushi Smriti BMCWEB_LOG_DEBUG << "ERROR with async_method_call"; 1495599c71d8SAyushi Smriti messages::internalError(asyncResp->res); 1496599c71d8SAyushi Smriti return; 1497599c71d8SAyushi Smriti } 1498599c71d8SAyushi Smriti 1499599c71d8SAyushi Smriti const std::vector<std::string>* allGroupsList = 1500599c71d8SAyushi Smriti std::get_if<std::vector<std::string>>(&allGroups); 1501599c71d8SAyushi Smriti 1502599c71d8SAyushi Smriti if (allGroupsList == nullptr || allGroupsList->empty()) 1503599c71d8SAyushi Smriti { 1504599c71d8SAyushi Smriti messages::internalError(asyncResp->res); 1505599c71d8SAyushi Smriti return; 1506599c71d8SAyushi Smriti } 1507599c71d8SAyushi Smriti 150804ae99ecSEd Tanous crow::connections::systemBus->async_method_call( 1509f23b7296SEd Tanous [asyncResp, username, 1510f23b7296SEd Tanous password](const boost::system::error_code ec2, 15110d4197e0Sanil kumar appana sdbusplus::message::message& m) { 151223a21a1cSEd Tanous if (ec2) 151304ae99ecSEd Tanous { 15146c51eab1SEd Tanous userErrorMessageHandler( 15156c51eab1SEd Tanous m.get_error(), asyncResp, username, ""); 151604ae99ecSEd Tanous return; 151704ae99ecSEd Tanous } 151804ae99ecSEd Tanous 151966b5ca76Sjayaprakash Mutyala if (pamUpdatePassword(username, password) != 152066b5ca76Sjayaprakash Mutyala PAM_SUCCESS) 152104ae99ecSEd Tanous { 15226c51eab1SEd Tanous // At this point we have a user that's been 15236c51eab1SEd Tanous // created, but the password set 15246c51eab1SEd Tanous // failed.Something is wrong, so delete the user 15256c51eab1SEd Tanous // that we've already created 152604ae99ecSEd Tanous crow::connections::systemBus->async_method_call( 152723a21a1cSEd Tanous [asyncResp, password]( 152823a21a1cSEd Tanous const boost::system::error_code ec3) { 152923a21a1cSEd Tanous if (ec3) 153004ae99ecSEd Tanous { 15316c51eab1SEd Tanous messages::internalError( 15326c51eab1SEd Tanous asyncResp->res); 153304ae99ecSEd Tanous return; 153404ae99ecSEd Tanous } 153504ae99ecSEd Tanous 15360d4197e0Sanil kumar appana // If password is invalid 15370d4197e0Sanil kumar appana messages::propertyValueFormatError( 15386c51eab1SEd Tanous asyncResp->res, password, 15396c51eab1SEd Tanous "Password"); 154004ae99ecSEd Tanous }, 154104ae99ecSEd Tanous "xyz.openbmc_project.User.Manager", 154204ae99ecSEd Tanous "/xyz/openbmc_project/user/" + username, 15436c51eab1SEd Tanous "xyz.openbmc_project.Object.Delete", 15446c51eab1SEd Tanous "Delete"); 154504ae99ecSEd Tanous 154604ae99ecSEd Tanous BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; 154704ae99ecSEd Tanous return; 154804ae99ecSEd Tanous } 154904ae99ecSEd Tanous 1550f12894f8SJason M. Bills messages::created(asyncResp->res); 155104ae99ecSEd Tanous asyncResp->res.addHeader( 155204ae99ecSEd Tanous "Location", 15536c51eab1SEd Tanous "/redfish/v1/AccountService/Accounts/" + 15546c51eab1SEd Tanous username); 155504ae99ecSEd Tanous }, 1556599c71d8SAyushi Smriti "xyz.openbmc_project.User.Manager", 1557599c71d8SAyushi Smriti "/xyz/openbmc_project/user", 15586c51eab1SEd Tanous "xyz.openbmc_project.User.Manager", "CreateUser", 15596c51eab1SEd Tanous username, *allGroupsList, *roleId, *enabled); 1560599c71d8SAyushi Smriti }, 1561599c71d8SAyushi Smriti "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1562599c71d8SAyushi Smriti "org.freedesktop.DBus.Properties", "Get", 1563599c71d8SAyushi Smriti "xyz.openbmc_project.User.Manager", "AllGroups"); 15646c51eab1SEd Tanous }); 1565b9b2e0b2SEd Tanous 15666c51eab1SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") 15676c51eab1SEd Tanous .privileges( 15686c51eab1SEd Tanous {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}) 15696c51eab1SEd Tanous .methods( 15706c51eab1SEd Tanous boost::beast::http::verb:: 15716c51eab1SEd Tanous get)([](const crow::Request& req, 15726c51eab1SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 15736c51eab1SEd Tanous const std::string& accountName) -> void { 15746c51eab1SEd Tanous if (req.session->username != accountName) 1575b9b2e0b2SEd Tanous { 15766c51eab1SEd Tanous // At this point we've determined that the user is trying to 15776c51eab1SEd Tanous // modify a user that isn't them. We need to verify that they 15786c51eab1SEd Tanous // have permissions to modify other users, so re-run the auth 15796c51eab1SEd Tanous // check with the same permissions, minus ConfigureSelf. 15806c51eab1SEd Tanous Privileges effectiveUserPrivileges = 15816c51eab1SEd Tanous redfish::getUserPrivileges(req.userRole); 15826c51eab1SEd Tanous Privileges requiredPermissionsToChangeNonSelf = { 15836c51eab1SEd Tanous {"ConfigureUsers"}, {"ConfigureManager"}}; 15846c51eab1SEd Tanous if (!effectiveUserPrivileges.isSupersetOf( 15856c51eab1SEd Tanous requiredPermissionsToChangeNonSelf)) 1586900f9497SJoseph Reynolds { 1587900f9497SJoseph Reynolds BMCWEB_LOG_DEBUG << "GET Account denied access"; 1588900f9497SJoseph Reynolds messages::insufficientPrivilege(asyncResp->res); 1589900f9497SJoseph Reynolds return; 1590900f9497SJoseph Reynolds } 1591900f9497SJoseph Reynolds } 1592900f9497SJoseph Reynolds 1593b9b2e0b2SEd Tanous crow::connections::systemBus->async_method_call( 15946c51eab1SEd Tanous [asyncResp, accountName](const boost::system::error_code ec, 1595b9b2e0b2SEd Tanous const ManagedObjectType& users) { 1596b9b2e0b2SEd Tanous if (ec) 1597b9b2e0b2SEd Tanous { 1598f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1599b9b2e0b2SEd Tanous return; 1600b9b2e0b2SEd Tanous } 160184e12cb7SAppaRao Puli auto userIt = users.begin(); 1602b9b2e0b2SEd Tanous 160384e12cb7SAppaRao Puli for (; userIt != users.end(); userIt++) 1604b9b2e0b2SEd Tanous { 16056c51eab1SEd Tanous if (boost::ends_with(userIt->first.str, 16066c51eab1SEd Tanous "/" + accountName)) 1607b9b2e0b2SEd Tanous { 160884e12cb7SAppaRao Puli break; 1609b9b2e0b2SEd Tanous } 1610b9b2e0b2SEd Tanous } 161184e12cb7SAppaRao Puli if (userIt == users.end()) 1612b9b2e0b2SEd Tanous { 16136c51eab1SEd Tanous messages::resourceNotFound( 16146c51eab1SEd Tanous asyncResp->res, "ManagerAccount", accountName); 161584e12cb7SAppaRao Puli return; 161684e12cb7SAppaRao Puli } 16174e68c45bSAyushi Smriti 16184e68c45bSAyushi Smriti asyncResp->res.jsonValue = { 16196c51eab1SEd Tanous {"@odata.type", 16206c51eab1SEd Tanous "#ManagerAccount.v1_4_0.ManagerAccount"}, 16214e68c45bSAyushi Smriti {"Name", "User Account"}, 16224e68c45bSAyushi Smriti {"Description", "User Account"}, 16238114bd4dSGunnar Mills {"Password", nullptr}, 16248114bd4dSGunnar Mills {"AccountTypes", {"Redfish"}}}; 16254e68c45bSAyushi Smriti 162684e12cb7SAppaRao Puli for (const auto& interface : userIt->second) 162765b0dc32SEd Tanous { 162865b0dc32SEd Tanous if (interface.first == 162965b0dc32SEd Tanous "xyz.openbmc_project.User.Attributes") 163065b0dc32SEd Tanous { 163165b0dc32SEd Tanous for (const auto& property : interface.second) 163265b0dc32SEd Tanous { 163365b0dc32SEd Tanous if (property.first == "UserEnabled") 163465b0dc32SEd Tanous { 163565b0dc32SEd Tanous const bool* userEnabled = 1636abf2add6SEd Tanous std::get_if<bool>(&property.second); 163765b0dc32SEd Tanous if (userEnabled == nullptr) 163865b0dc32SEd Tanous { 163965b0dc32SEd Tanous BMCWEB_LOG_ERROR 164065b0dc32SEd Tanous << "UserEnabled wasn't a bool"; 164184e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 164284e12cb7SAppaRao Puli return; 164365b0dc32SEd Tanous } 164465b0dc32SEd Tanous asyncResp->res.jsonValue["Enabled"] = 164565b0dc32SEd Tanous *userEnabled; 164665b0dc32SEd Tanous } 164765b0dc32SEd Tanous else if (property.first == 164865b0dc32SEd Tanous "UserLockedForFailedAttempt") 164965b0dc32SEd Tanous { 165065b0dc32SEd Tanous const bool* userLocked = 1651abf2add6SEd Tanous std::get_if<bool>(&property.second); 165265b0dc32SEd Tanous if (userLocked == nullptr) 165365b0dc32SEd Tanous { 165484e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "UserLockedForF" 165584e12cb7SAppaRao Puli "ailedAttempt " 165684e12cb7SAppaRao Puli "wasn't a bool"; 165784e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 165884e12cb7SAppaRao Puli return; 165965b0dc32SEd Tanous } 166065b0dc32SEd Tanous asyncResp->res.jsonValue["Locked"] = 166165b0dc32SEd Tanous *userLocked; 166224c8542dSRatan Gupta asyncResp->res.jsonValue 166324c8542dSRatan Gupta ["Locked@Redfish.AllowableValues"] = { 16643bf4e632SJoseph Reynolds "false"}; // can only unlock accounts 166565b0dc32SEd Tanous } 166684e12cb7SAppaRao Puli else if (property.first == "UserPrivilege") 166784e12cb7SAppaRao Puli { 166854fc587aSNagaraju Goruganti const std::string* userPrivPtr = 16696c51eab1SEd Tanous std::get_if<std::string>( 16706c51eab1SEd Tanous &property.second); 167154fc587aSNagaraju Goruganti if (userPrivPtr == nullptr) 167284e12cb7SAppaRao Puli { 167384e12cb7SAppaRao Puli BMCWEB_LOG_ERROR 167484e12cb7SAppaRao Puli << "UserPrivilege wasn't a " 167584e12cb7SAppaRao Puli "string"; 167684e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 167784e12cb7SAppaRao Puli return; 167884e12cb7SAppaRao Puli } 167954fc587aSNagaraju Goruganti std::string role = 168054fc587aSNagaraju Goruganti getRoleIdFromPrivilege(*userPrivPtr); 168154fc587aSNagaraju Goruganti if (role.empty()) 168284e12cb7SAppaRao Puli { 168384e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "Invalid user role"; 168484e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 168584e12cb7SAppaRao Puli return; 168684e12cb7SAppaRao Puli } 168754fc587aSNagaraju Goruganti asyncResp->res.jsonValue["RoleId"] = role; 168884e12cb7SAppaRao Puli 16896c51eab1SEd Tanous asyncResp->res.jsonValue["Links"]["Role"] = 16906c51eab1SEd Tanous {{"@odata.id", 16916c51eab1SEd Tanous "/redfish/v1/AccountService/" 169284e12cb7SAppaRao Puli "Roles/" + 169354fc587aSNagaraju Goruganti role}}; 169484e12cb7SAppaRao Puli } 16956c51eab1SEd Tanous else if (property.first == 16966c51eab1SEd Tanous "UserPasswordExpired") 16973bf4e632SJoseph Reynolds { 16983bf4e632SJoseph Reynolds const bool* userPasswordExpired = 16993bf4e632SJoseph Reynolds std::get_if<bool>(&property.second); 17003bf4e632SJoseph Reynolds if (userPasswordExpired == nullptr) 17013bf4e632SJoseph Reynolds { 17023bf4e632SJoseph Reynolds BMCWEB_LOG_ERROR << "UserPassword" 17033bf4e632SJoseph Reynolds "Expired " 17043bf4e632SJoseph Reynolds "wasn't a bool"; 17053bf4e632SJoseph Reynolds messages::internalError(asyncResp->res); 17063bf4e632SJoseph Reynolds return; 17073bf4e632SJoseph Reynolds } 17083bf4e632SJoseph Reynolds asyncResp->res 17093bf4e632SJoseph Reynolds .jsonValue["PasswordChangeRequired"] = 17103bf4e632SJoseph Reynolds *userPasswordExpired; 17113bf4e632SJoseph Reynolds } 171265b0dc32SEd Tanous } 171365b0dc32SEd Tanous } 171465b0dc32SEd Tanous } 171565b0dc32SEd Tanous 1716b9b2e0b2SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 171784e12cb7SAppaRao Puli "/redfish/v1/AccountService/Accounts/" + accountName; 1718b9b2e0b2SEd Tanous asyncResp->res.jsonValue["Id"] = accountName; 1719b9b2e0b2SEd Tanous asyncResp->res.jsonValue["UserName"] = accountName; 1720b9b2e0b2SEd Tanous }, 1721b9b2e0b2SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1722b9b2e0b2SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 17236c51eab1SEd Tanous }); 1724a840879dSEd Tanous 17256c51eab1SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") 17266c51eab1SEd Tanous .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}}) 17276c51eab1SEd Tanous .methods(boost::beast::http::verb::patch)( 17286c51eab1SEd Tanous [](const crow::Request& req, 17296c51eab1SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 17306c51eab1SEd Tanous const std::string& username) -> void { 1731a24526dcSEd Tanous std::optional<std::string> newUserName; 1732a24526dcSEd Tanous std::optional<std::string> password; 1733a24526dcSEd Tanous std::optional<bool> enabled; 1734a24526dcSEd Tanous std::optional<std::string> roleId; 173524c8542dSRatan Gupta std::optional<bool> locked; 17366c51eab1SEd Tanous if (!json_util::readJson(req, asyncResp->res, "UserName", 17376c51eab1SEd Tanous newUserName, "Password", password, 17386c51eab1SEd Tanous "RoleId", roleId, "Enabled", enabled, 17396c51eab1SEd Tanous "Locked", locked)) 1740a840879dSEd Tanous { 1741a840879dSEd Tanous return; 1742a840879dSEd Tanous } 1743a840879dSEd Tanous 1744900f9497SJoseph Reynolds // Perform a proper ConfigureSelf authority check. If the 1745900f9497SJoseph Reynolds // session is being used to PATCH a property other than 1746900f9497SJoseph Reynolds // Password, then the ConfigureSelf privilege does not apply. 1747900f9497SJoseph Reynolds // If the user is operating on an account not their own, then 1748900f9497SJoseph Reynolds // their ConfigureSelf privilege does not apply. In either 1749900f9497SJoseph Reynolds // case, perform the authority check again without the user's 1750900f9497SJoseph Reynolds // ConfigureSelf privilege. 17516c51eab1SEd Tanous if ((username != req.session->username)) 1752900f9497SJoseph Reynolds { 17536c51eab1SEd Tanous Privileges requiredPermissionsToChangeNonSelf = { 17546c51eab1SEd Tanous {"ConfigureUsers"}}; 17556c51eab1SEd Tanous Privileges effectiveUserPrivileges = 17566c51eab1SEd Tanous redfish::getUserPrivileges(req.userRole); 17576c51eab1SEd Tanous 17586c51eab1SEd Tanous if (!effectiveUserPrivileges.isSupersetOf( 17596c51eab1SEd Tanous requiredPermissionsToChangeNonSelf)) 1760900f9497SJoseph Reynolds { 1761900f9497SJoseph Reynolds messages::insufficientPrivilege(asyncResp->res); 1762900f9497SJoseph Reynolds return; 1763900f9497SJoseph Reynolds } 1764900f9497SJoseph Reynolds } 1765900f9497SJoseph Reynolds 176666b5ca76Sjayaprakash Mutyala // if user name is not provided in the patch method or if it 17676c51eab1SEd Tanous // matches the user name in the URI, then we are treating it as 17686c51eab1SEd Tanous // updating user properties other then username. If username 17696c51eab1SEd Tanous // provided doesn't match the URI, then we are treating this as 17706c51eab1SEd Tanous // user rename request. 177166b5ca76Sjayaprakash Mutyala if (!newUserName || (newUserName.value() == username)) 1772a840879dSEd Tanous { 17736c51eab1SEd Tanous updateUserProperties(asyncResp, username, password, enabled, 17746c51eab1SEd Tanous roleId, locked); 177584e12cb7SAppaRao Puli return; 177684e12cb7SAppaRao Puli } 177784e12cb7SAppaRao Puli crow::connections::systemBus->async_method_call( 17786c51eab1SEd Tanous [asyncResp, username, password(std::move(password)), 1779f23b7296SEd Tanous roleId(std::move(roleId)), enabled, 178066b5ca76Sjayaprakash Mutyala newUser{std::string(*newUserName)}, 1781f23b7296SEd Tanous locked](const boost::system::error_code ec, 178266b5ca76Sjayaprakash Mutyala sdbusplus::message::message& m) { 178384e12cb7SAppaRao Puli if (ec) 178484e12cb7SAppaRao Puli { 17856c51eab1SEd Tanous userErrorMessageHandler(m.get_error(), asyncResp, 17866c51eab1SEd Tanous newUser, username); 1787a840879dSEd Tanous return; 1788a840879dSEd Tanous } 1789a840879dSEd Tanous 17906c51eab1SEd Tanous updateUserProperties(asyncResp, newUser, password, 17916c51eab1SEd Tanous enabled, roleId, locked); 179284e12cb7SAppaRao Puli }, 17936c51eab1SEd Tanous "xyz.openbmc_project.User.Manager", 17946c51eab1SEd Tanous "/xyz/openbmc_project/user", 179584e12cb7SAppaRao Puli "xyz.openbmc_project.User.Manager", "RenameUser", username, 179684e12cb7SAppaRao Puli *newUserName); 17976c51eab1SEd Tanous }); 179884e12cb7SAppaRao Puli 17996c51eab1SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") 18006c51eab1SEd Tanous .privileges({{"ConfigureUsers"}}) 18016c51eab1SEd Tanous .methods(boost::beast::http::verb::delete_)( 18026c51eab1SEd Tanous [](const crow::Request& /*req*/, 18036c51eab1SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 18046c51eab1SEd Tanous const std::string& username) -> void { 18056c51eab1SEd Tanous const std::string userPath = 18066c51eab1SEd Tanous "/xyz/openbmc_project/user/" + username; 180724c8542dSRatan Gupta 18086c51eab1SEd Tanous crow::connections::systemBus->async_method_call( 18096c51eab1SEd Tanous [asyncResp, username](const boost::system::error_code ec) { 18106c51eab1SEd Tanous if (ec) 181166b5ca76Sjayaprakash Mutyala { 181266b5ca76Sjayaprakash Mutyala messages::resourceNotFound( 181366b5ca76Sjayaprakash Mutyala asyncResp->res, 18146c51eab1SEd Tanous "#ManagerAccount.v1_4_0.ManagerAccount", 1815f12894f8SJason M. Bills username); 181606e086d9SEd Tanous return; 181706e086d9SEd Tanous } 181806e086d9SEd Tanous 1819f12894f8SJason M. Bills messages::accountRemoved(asyncResp->res); 182006e086d9SEd Tanous }, 182106e086d9SEd Tanous "xyz.openbmc_project.User.Manager", userPath, 182206e086d9SEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 18236c51eab1SEd Tanous }); 182406e086d9SEd Tanous } 182588d16c9aSLewanczyk, Dawid 182688d16c9aSLewanczyk, Dawid } // namespace redfish 1827