xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision e662eae819f545ef07193f216b42e161b7ff7a46)
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>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
241e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
25a840879dSEd Tanous #include <utils/json_utils.hpp>
261214b7e7SGunnar Mills 
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 
35b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
366973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
376973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
386973a582SRatan Gupta constexpr const char* ldapConfigInterface =
396973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
406973a582SRatan Gupta constexpr const char* ldapCreateInterface =
416973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
426973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
4306785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
4406785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
456973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
466973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
476973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
486973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
496973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
506973a582SRatan Gupta 
5154fc587aSNagaraju Goruganti struct LDAPRoleMapData
5254fc587aSNagaraju Goruganti {
5354fc587aSNagaraju Goruganti     std::string groupName;
5454fc587aSNagaraju Goruganti     std::string privilege;
5554fc587aSNagaraju Goruganti };
5654fc587aSNagaraju Goruganti 
576973a582SRatan Gupta struct LDAPConfigData
586973a582SRatan Gupta {
596973a582SRatan Gupta     std::string uri{};
606973a582SRatan Gupta     std::string bindDN{};
616973a582SRatan Gupta     std::string baseDN{};
626973a582SRatan Gupta     std::string searchScope{};
636973a582SRatan Gupta     std::string serverType{};
646973a582SRatan Gupta     bool serviceEnabled = false;
656973a582SRatan Gupta     std::string userNameAttribute{};
666973a582SRatan Gupta     std::string groupAttribute{};
6754fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
686973a582SRatan Gupta };
696973a582SRatan Gupta 
706973a582SRatan Gupta using GetObjectType =
716973a582SRatan Gupta     std::vector<std::pair<std::string, std::vector<std::string>>>;
7284e12cb7SAppaRao Puli 
7354fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7484e12cb7SAppaRao Puli {
7584e12cb7SAppaRao Puli     if (role == "priv-admin")
7684e12cb7SAppaRao Puli     {
7784e12cb7SAppaRao Puli         return "Administrator";
7884e12cb7SAppaRao Puli     }
793174e4dfSEd Tanous     if (role == "priv-user")
8084e12cb7SAppaRao Puli     {
81c80fee55SAppaRao Puli         return "ReadOnly";
8284e12cb7SAppaRao Puli     }
833174e4dfSEd Tanous     if (role == "priv-operator")
8484e12cb7SAppaRao Puli     {
8584e12cb7SAppaRao Puli         return "Operator";
8684e12cb7SAppaRao Puli     }
8726f6976fSEd Tanous     if (role.empty() || (role == "priv-noaccess"))
88e9e6d240Sjayaprakash Mutyala     {
89e9e6d240Sjayaprakash Mutyala         return "NoAccess";
90e9e6d240Sjayaprakash Mutyala     }
9184e12cb7SAppaRao Puli     return "";
9284e12cb7SAppaRao Puli }
9354fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9484e12cb7SAppaRao Puli {
9584e12cb7SAppaRao Puli     if (role == "Administrator")
9684e12cb7SAppaRao Puli     {
9784e12cb7SAppaRao Puli         return "priv-admin";
9884e12cb7SAppaRao Puli     }
993174e4dfSEd Tanous     if (role == "ReadOnly")
10084e12cb7SAppaRao Puli     {
10184e12cb7SAppaRao Puli         return "priv-user";
10284e12cb7SAppaRao Puli     }
1033174e4dfSEd Tanous     if (role == "Operator")
10484e12cb7SAppaRao Puli     {
10584e12cb7SAppaRao Puli         return "priv-operator";
10684e12cb7SAppaRao Puli     }
10726f6976fSEd Tanous     if ((role == "NoAccess") || (role.empty()))
108e9e6d240Sjayaprakash Mutyala     {
109e9e6d240Sjayaprakash Mutyala         return "priv-noaccess";
110e9e6d240Sjayaprakash Mutyala     }
11184e12cb7SAppaRao Puli     return "";
11284e12cb7SAppaRao Puli }
113b9b2e0b2SEd Tanous 
1148d1b46d7Szhanghch05 inline void userErrorMessageHandler(
1158d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1168d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
11766b5ca76Sjayaprakash Mutyala {
11866b5ca76Sjayaprakash Mutyala     if (e == nullptr)
11966b5ca76Sjayaprakash Mutyala     {
12066b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
12166b5ca76Sjayaprakash Mutyala         return;
12266b5ca76Sjayaprakash Mutyala     }
12366b5ca76Sjayaprakash Mutyala 
124055806b3SManojkiran Eda     const char* errorMessage = e->name;
12566b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
12666b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
12766b5ca76Sjayaprakash Mutyala     {
12866b5ca76Sjayaprakash Mutyala         messages::resourceAlreadyExists(asyncResp->res,
1298114bd4dSGunnar Mills                                         "#ManagerAccount.v1_4_0.ManagerAccount",
13066b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
13166b5ca76Sjayaprakash Mutyala     }
13266b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
13366b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
13466b5ca76Sjayaprakash Mutyala     {
13566b5ca76Sjayaprakash Mutyala         messages::resourceNotFound(
1368114bd4dSGunnar Mills             asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount", username);
13766b5ca76Sjayaprakash Mutyala     }
138d4d25793SEd Tanous     else if ((strcmp(errorMessage,
139d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
140d4d25793SEd Tanous               0) ||
1410fda0f12SGeorge Liu              (strcmp(
1420fda0f12SGeorge Liu                   errorMessage,
1430fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
1440fda0f12SGeorge Liu               0))
14566b5ca76Sjayaprakash Mutyala     {
14666b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
14766b5ca76Sjayaprakash Mutyala     }
14866b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
14966b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
15066b5ca76Sjayaprakash Mutyala     {
15166b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
15266b5ca76Sjayaprakash Mutyala     }
15366b5ca76Sjayaprakash Mutyala     else
15466b5ca76Sjayaprakash Mutyala     {
15566b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
15666b5ca76Sjayaprakash Mutyala     }
15766b5ca76Sjayaprakash Mutyala }
15866b5ca76Sjayaprakash Mutyala 
15981ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
160ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
161ab828d7cSRatan Gupta                                 const std::string& ldapType)
1626973a582SRatan Gupta {
163ab828d7cSRatan Gupta     std::string service =
164ab828d7cSRatan Gupta         (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService";
16537cce918SMarri Devender Rao     nlohmann::json ldap = {
1666973a582SRatan Gupta         {"ServiceEnabled", confData.serviceEnabled},
1676973a582SRatan Gupta         {"ServiceAddresses", nlohmann::json::array({confData.uri})},
1686973a582SRatan Gupta         {"Authentication",
1696973a582SRatan Gupta          {{"AuthenticationType", "UsernameAndPassword"},
1706973a582SRatan Gupta           {"Username", confData.bindDN},
1716973a582SRatan Gupta           {"Password", nullptr}}},
1726973a582SRatan Gupta         {"LDAPService",
1736973a582SRatan Gupta          {{"SearchSettings",
1746973a582SRatan Gupta            {{"BaseDistinguishedNames",
1756973a582SRatan Gupta              nlohmann::json::array({confData.baseDN})},
1766973a582SRatan Gupta             {"UsernameAttribute", confData.userNameAttribute},
1776973a582SRatan Gupta             {"GroupsAttribute", confData.groupAttribute}}}}},
1786973a582SRatan Gupta     };
17954fc587aSNagaraju Goruganti 
18081ce609eSEd Tanous     jsonResponse[ldapType].update(ldap);
18154fc587aSNagaraju Goruganti 
18281ce609eSEd Tanous     nlohmann::json& roleMapArray = jsonResponse[ldapType]["RemoteRoleMapping"];
18354fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
1849eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
18554fc587aSNagaraju Goruganti     {
18654fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
18754fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
18854fc587aSNagaraju Goruganti         roleMapArray.push_back(
18954fc587aSNagaraju Goruganti             {nlohmann::json::array({"RemoteGroup", obj.second.groupName}),
19054fc587aSNagaraju Goruganti              nlohmann::json::array(
19154fc587aSNagaraju Goruganti                  {"LocalRole", getRoleIdFromPrivilege(obj.second.privilege)})});
19254fc587aSNagaraju Goruganti     }
1936973a582SRatan Gupta }
1946973a582SRatan Gupta 
1956973a582SRatan Gupta /**
19606785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
19706785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
19806785244SRatan Gupta  *
19906785244SRatan Gupta  */
20023a21a1cSEd Tanous inline void handleRoleMapPatch(
2018d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20206785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
203f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
20406785244SRatan Gupta {
20506785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
20606785244SRatan Gupta     {
207f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
20806785244SRatan Gupta 
20906785244SRatan Gupta         if (thisJson.is_null())
21006785244SRatan Gupta         {
21106785244SRatan Gupta             // delete the existing object
21206785244SRatan Gupta             if (index < roleMapObjData.size())
21306785244SRatan Gupta             {
21406785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
21506785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
21606785244SRatan Gupta                      index](const boost::system::error_code ec) {
21706785244SRatan Gupta                         if (ec)
21806785244SRatan Gupta                         {
21906785244SRatan Gupta                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
22006785244SRatan Gupta                             messages::internalError(asyncResp->res);
22106785244SRatan Gupta                             return;
22206785244SRatan Gupta                         }
22306785244SRatan Gupta                         asyncResp->res
22406785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"][index] =
22506785244SRatan Gupta                             nullptr;
22606785244SRatan Gupta                     },
22706785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
22806785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
22906785244SRatan Gupta             }
23006785244SRatan Gupta             else
23106785244SRatan Gupta             {
23206785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
23306785244SRatan Gupta                 messages::propertyValueTypeError(
23471f52d96SEd Tanous                     asyncResp->res,
23571f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
23671f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
23706785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
23806785244SRatan Gupta                 return;
23906785244SRatan Gupta             }
24006785244SRatan Gupta         }
24106785244SRatan Gupta         else if (thisJson.empty())
24206785244SRatan Gupta         {
24306785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
24406785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
24506785244SRatan Gupta         }
24606785244SRatan Gupta         else
24706785244SRatan Gupta         {
24806785244SRatan Gupta             // update/create the object
24906785244SRatan Gupta             std::optional<std::string> remoteGroup;
25006785244SRatan Gupta             std::optional<std::string> localRole;
25106785244SRatan Gupta 
252f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
253f23b7296SEd Tanous             // readJson is structured
254f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
255f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
256f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
257f23b7296SEd Tanous                                      localRole))
25806785244SRatan Gupta             {
25906785244SRatan Gupta                 continue;
26006785244SRatan Gupta             }
26106785244SRatan Gupta 
26206785244SRatan Gupta             // Update existing RoleMapping Object
26306785244SRatan Gupta             if (index < roleMapObjData.size())
26406785244SRatan Gupta             {
26506785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
26606785244SRatan Gupta                 // If "RemoteGroup" info is provided
26706785244SRatan Gupta                 if (remoteGroup)
26806785244SRatan Gupta                 {
26906785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
27006785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
27106785244SRatan Gupta                          remoteGroup](const boost::system::error_code ec) {
27206785244SRatan Gupta                             if (ec)
27306785244SRatan Gupta                             {
27406785244SRatan Gupta                                 BMCWEB_LOG_ERROR << "DBUS response error: "
27506785244SRatan Gupta                                                  << ec;
27606785244SRatan Gupta                                 messages::internalError(asyncResp->res);
27706785244SRatan Gupta                                 return;
27806785244SRatan Gupta                             }
27906785244SRatan Gupta                             asyncResp->res
28006785244SRatan Gupta                                 .jsonValue[serverType]["RemoteRoleMapping"]
28106785244SRatan Gupta                                           [index]["RemoteGroup"] = *remoteGroup;
28206785244SRatan Gupta                         },
28306785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
28406785244SRatan Gupta                         propertyInterface, "Set",
28506785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
28606785244SRatan Gupta                         "GroupName",
287168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
288168e20c1SEd Tanous                             std::move(*remoteGroup)));
28906785244SRatan Gupta                 }
29006785244SRatan Gupta 
29106785244SRatan Gupta                 // If "LocalRole" info is provided
29206785244SRatan Gupta                 if (localRole)
29306785244SRatan Gupta                 {
29406785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
29506785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
29606785244SRatan Gupta                          localRole](const boost::system::error_code ec) {
29706785244SRatan Gupta                             if (ec)
29806785244SRatan Gupta                             {
29906785244SRatan Gupta                                 BMCWEB_LOG_ERROR << "DBUS response error: "
30006785244SRatan Gupta                                                  << ec;
30106785244SRatan Gupta                                 messages::internalError(asyncResp->res);
30206785244SRatan Gupta                                 return;
30306785244SRatan Gupta                             }
30406785244SRatan Gupta                             asyncResp->res
30506785244SRatan Gupta                                 .jsonValue[serverType]["RemoteRoleMapping"]
30606785244SRatan Gupta                                           [index]["LocalRole"] = *localRole;
30706785244SRatan Gupta                         },
30806785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
30906785244SRatan Gupta                         propertyInterface, "Set",
31006785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
31106785244SRatan Gupta                         "Privilege",
312168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
31306785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
31406785244SRatan Gupta                 }
31506785244SRatan Gupta             }
31606785244SRatan Gupta             // Create a new RoleMapping Object.
31706785244SRatan Gupta             else
31806785244SRatan Gupta             {
31906785244SRatan Gupta                 BMCWEB_LOG_DEBUG
32006785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
32106785244SRatan Gupta                 std::string pathString =
32206785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index);
32306785244SRatan Gupta 
32406785244SRatan Gupta                 if (!localRole)
32506785244SRatan Gupta                 {
32606785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
32706785244SRatan Gupta                                               pathString + "/LocalRole");
32806785244SRatan Gupta                     continue;
32906785244SRatan Gupta                 }
33006785244SRatan Gupta                 if (!remoteGroup)
33106785244SRatan Gupta                 {
33206785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
33306785244SRatan Gupta                                               pathString + "/RemoteGroup");
33406785244SRatan Gupta                     continue;
33506785244SRatan Gupta                 }
33606785244SRatan Gupta 
33706785244SRatan Gupta                 std::string dbusObjectPath;
33806785244SRatan Gupta                 if (serverType == "ActiveDirectory")
33906785244SRatan Gupta                 {
3402c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
34106785244SRatan Gupta                 }
34206785244SRatan Gupta                 else if (serverType == "LDAP")
34306785244SRatan Gupta                 {
34423a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
34506785244SRatan Gupta                 }
34606785244SRatan Gupta 
34706785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
34806785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
34906785244SRatan Gupta 
35006785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
351271584abSEd Tanous                     [asyncResp, serverType, localRole,
35206785244SRatan Gupta                      remoteGroup](const boost::system::error_code ec) {
35306785244SRatan Gupta                         if (ec)
35406785244SRatan Gupta                         {
35506785244SRatan Gupta                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
35606785244SRatan Gupta                             messages::internalError(asyncResp->res);
35706785244SRatan Gupta                             return;
35806785244SRatan Gupta                         }
35906785244SRatan Gupta                         nlohmann::json& remoteRoleJson =
36006785244SRatan Gupta                             asyncResp->res
36106785244SRatan Gupta                                 .jsonValue[serverType]["RemoteRoleMapping"];
36206785244SRatan Gupta                         remoteRoleJson.push_back(
36306785244SRatan Gupta                             {{"LocalRole", *localRole},
36406785244SRatan Gupta                              {"RemoteGroup", *remoteGroup}});
36506785244SRatan Gupta                     },
36606785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
3673174e4dfSEd Tanous                     "Create", *remoteGroup,
36806785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
36906785244SRatan Gupta             }
37006785244SRatan Gupta         }
37106785244SRatan Gupta     }
37206785244SRatan Gupta }
37306785244SRatan Gupta 
37406785244SRatan Gupta /**
3756973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
3766973a582SRatan Gupta  * into JSON
3776973a582SRatan Gupta  */
3786973a582SRatan Gupta template <typename CallbackFunc>
3796973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
3806973a582SRatan Gupta                               CallbackFunc&& callback)
3816973a582SRatan Gupta {
38254fc587aSNagaraju Goruganti 
38354fc587aSNagaraju Goruganti     const std::array<const char*, 2> interfaces = {ldapEnableInterface,
38454fc587aSNagaraju Goruganti                                                    ldapConfigInterface};
38554fc587aSNagaraju Goruganti 
38654fc587aSNagaraju Goruganti     crow::connections::systemBus->async_method_call(
38754fc587aSNagaraju Goruganti         [callback, ldapType](const boost::system::error_code ec,
38854fc587aSNagaraju Goruganti                              const GetObjectType& resp) {
38954fc587aSNagaraju Goruganti             if (ec || resp.empty())
39054fc587aSNagaraju Goruganti             {
3910fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
3920fda0f12SGeorge Liu                     << "DBUS response error during getting of service name: "
39354fc587aSNagaraju Goruganti                     << ec;
39423a21a1cSEd Tanous                 LDAPConfigData empty{};
39523a21a1cSEd Tanous                 callback(false, empty, ldapType);
39654fc587aSNagaraju Goruganti                 return;
39754fc587aSNagaraju Goruganti             }
39854fc587aSNagaraju Goruganti             std::string service = resp.begin()->first;
39954fc587aSNagaraju Goruganti             crow::connections::systemBus->async_method_call(
400711ac7a9SEd Tanous                 [callback, ldapType](
401711ac7a9SEd Tanous                     const boost::system::error_code errorCode,
402711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& ldapObjects) {
4036973a582SRatan Gupta                     LDAPConfigData confData{};
40481ce609eSEd Tanous                     if (errorCode)
4056973a582SRatan Gupta                     {
406ab828d7cSRatan Gupta                         callback(false, confData, ldapType);
40754fc587aSNagaraju Goruganti                         BMCWEB_LOG_ERROR << "D-Bus responses error: "
40881ce609eSEd Tanous                                          << errorCode;
4096973a582SRatan Gupta                         return;
4106973a582SRatan Gupta                     }
411ab828d7cSRatan Gupta 
412ab828d7cSRatan Gupta                     std::string ldapDbusType;
41354fc587aSNagaraju Goruganti                     std::string searchString;
41454fc587aSNagaraju Goruganti 
415ab828d7cSRatan Gupta                     if (ldapType == "LDAP")
416ab828d7cSRatan Gupta                     {
4170fda0f12SGeorge Liu                         ldapDbusType =
4180fda0f12SGeorge Liu                             "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
41954fc587aSNagaraju Goruganti                         searchString = "openldap";
420ab828d7cSRatan Gupta                     }
421ab828d7cSRatan Gupta                     else if (ldapType == "ActiveDirectory")
422ab828d7cSRatan Gupta                     {
42354fc587aSNagaraju Goruganti                         ldapDbusType =
4240fda0f12SGeorge Liu                             "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
42554fc587aSNagaraju Goruganti                         searchString = "active_directory";
426ab828d7cSRatan Gupta                     }
427ab828d7cSRatan Gupta                     else
428ab828d7cSRatan Gupta                     {
42954fc587aSNagaraju Goruganti                         BMCWEB_LOG_ERROR
43054fc587aSNagaraju Goruganti                             << "Can't get the DbusType for the given type="
431ab828d7cSRatan Gupta                             << ldapType;
432ab828d7cSRatan Gupta                         callback(false, confData, ldapType);
433ab828d7cSRatan Gupta                         return;
434ab828d7cSRatan Gupta                     }
435ab828d7cSRatan Gupta 
436ab828d7cSRatan Gupta                     std::string ldapEnableInterfaceStr = ldapEnableInterface;
437ab828d7cSRatan Gupta                     std::string ldapConfigInterfaceStr = ldapConfigInterface;
438ab828d7cSRatan Gupta 
4396973a582SRatan Gupta                     for (const auto& object : ldapObjects)
4406973a582SRatan Gupta                     {
44154fc587aSNagaraju Goruganti                         // let's find the object whose ldap type is equal to the
44254fc587aSNagaraju Goruganti                         // given type
44354fc587aSNagaraju Goruganti                         if (object.first.str.find(searchString) ==
44454fc587aSNagaraju Goruganti                             std::string::npos)
4456973a582SRatan Gupta                         {
446ab828d7cSRatan Gupta                             continue;
447ab828d7cSRatan Gupta                         }
448ab828d7cSRatan Gupta 
4496973a582SRatan Gupta                         for (const auto& interface : object.second)
4506973a582SRatan Gupta                         {
4516973a582SRatan Gupta                             if (interface.first == ldapEnableInterfaceStr)
4526973a582SRatan Gupta                             {
4536973a582SRatan Gupta                                 // rest of the properties are string.
4546973a582SRatan Gupta                                 for (const auto& property : interface.second)
4556973a582SRatan Gupta                                 {
4566973a582SRatan Gupta                                     if (property.first == "Enabled")
4576973a582SRatan Gupta                                     {
4586973a582SRatan Gupta                                         const bool* value =
4596973a582SRatan Gupta                                             std::get_if<bool>(&property.second);
4606973a582SRatan Gupta                                         if (value == nullptr)
4616973a582SRatan Gupta                                         {
4626973a582SRatan Gupta                                             continue;
4636973a582SRatan Gupta                                         }
4646973a582SRatan Gupta                                         confData.serviceEnabled = *value;
4656973a582SRatan Gupta                                         break;
4666973a582SRatan Gupta                                     }
4676973a582SRatan Gupta                                 }
4686973a582SRatan Gupta                             }
4696973a582SRatan Gupta                             else if (interface.first == ldapConfigInterfaceStr)
4706973a582SRatan Gupta                             {
4716973a582SRatan Gupta 
4726973a582SRatan Gupta                                 for (const auto& property : interface.second)
4736973a582SRatan Gupta                                 {
474271584abSEd Tanous                                     const std::string* strValue =
47554fc587aSNagaraju Goruganti                                         std::get_if<std::string>(
47654fc587aSNagaraju Goruganti                                             &property.second);
477271584abSEd Tanous                                     if (strValue == nullptr)
4786973a582SRatan Gupta                                     {
4796973a582SRatan Gupta                                         continue;
4806973a582SRatan Gupta                                     }
4816973a582SRatan Gupta                                     if (property.first == "LDAPServerURI")
4826973a582SRatan Gupta                                     {
483271584abSEd Tanous                                         confData.uri = *strValue;
4846973a582SRatan Gupta                                     }
4856973a582SRatan Gupta                                     else if (property.first == "LDAPBindDN")
4866973a582SRatan Gupta                                     {
487271584abSEd Tanous                                         confData.bindDN = *strValue;
4886973a582SRatan Gupta                                     }
4896973a582SRatan Gupta                                     else if (property.first == "LDAPBaseDN")
4906973a582SRatan Gupta                                     {
491271584abSEd Tanous                                         confData.baseDN = *strValue;
4926973a582SRatan Gupta                                     }
49354fc587aSNagaraju Goruganti                                     else if (property.first ==
49454fc587aSNagaraju Goruganti                                              "LDAPSearchScope")
4956973a582SRatan Gupta                                     {
496271584abSEd Tanous                                         confData.searchScope = *strValue;
4976973a582SRatan Gupta                                     }
49854fc587aSNagaraju Goruganti                                     else if (property.first ==
49954fc587aSNagaraju Goruganti                                              "GroupNameAttribute")
5006973a582SRatan Gupta                                     {
501271584abSEd Tanous                                         confData.groupAttribute = *strValue;
5026973a582SRatan Gupta                                     }
50354fc587aSNagaraju Goruganti                                     else if (property.first ==
50454fc587aSNagaraju Goruganti                                              "UserNameAttribute")
5056973a582SRatan Gupta                                     {
506271584abSEd Tanous                                         confData.userNameAttribute = *strValue;
5076973a582SRatan Gupta                                     }
50854fc587aSNagaraju Goruganti                                     else if (property.first == "LDAPType")
509ab828d7cSRatan Gupta                                     {
510271584abSEd Tanous                                         confData.serverType = *strValue;
51154fc587aSNagaraju Goruganti                                     }
51254fc587aSNagaraju Goruganti                                 }
51354fc587aSNagaraju Goruganti                             }
5140fda0f12SGeorge Liu                             else if (
5150fda0f12SGeorge Liu                                 interface.first ==
5160fda0f12SGeorge Liu                                 "xyz.openbmc_project.User.PrivilegeMapperEntry")
51754fc587aSNagaraju Goruganti                             {
51854fc587aSNagaraju Goruganti                                 LDAPRoleMapData roleMapData{};
51954fc587aSNagaraju Goruganti                                 for (const auto& property : interface.second)
52054fc587aSNagaraju Goruganti                                 {
521271584abSEd Tanous                                     const std::string* strValue =
52254fc587aSNagaraju Goruganti                                         std::get_if<std::string>(
52354fc587aSNagaraju Goruganti                                             &property.second);
52454fc587aSNagaraju Goruganti 
525271584abSEd Tanous                                     if (strValue == nullptr)
52654fc587aSNagaraju Goruganti                                     {
52754fc587aSNagaraju Goruganti                                         continue;
52854fc587aSNagaraju Goruganti                                     }
52954fc587aSNagaraju Goruganti 
53054fc587aSNagaraju Goruganti                                     if (property.first == "GroupName")
53154fc587aSNagaraju Goruganti                                     {
532271584abSEd Tanous                                         roleMapData.groupName = *strValue;
53354fc587aSNagaraju Goruganti                                     }
53454fc587aSNagaraju Goruganti                                     else if (property.first == "Privilege")
53554fc587aSNagaraju Goruganti                                     {
536271584abSEd Tanous                                         roleMapData.privilege = *strValue;
53754fc587aSNagaraju Goruganti                                     }
53854fc587aSNagaraju Goruganti                                 }
53954fc587aSNagaraju Goruganti 
5400f0353b6SEd Tanous                                 confData.groupRoleList.emplace_back(
5410f0353b6SEd Tanous                                     object.first.str, roleMapData);
54254fc587aSNagaraju Goruganti                             }
54354fc587aSNagaraju Goruganti                         }
54454fc587aSNagaraju Goruganti                     }
545ab828d7cSRatan Gupta                     callback(true, confData, ldapType);
54654fc587aSNagaraju Goruganti                 },
54754fc587aSNagaraju Goruganti                 service, ldapRootObject, dbusObjManagerIntf,
5486973a582SRatan Gupta                 "GetManagedObjects");
54954fc587aSNagaraju Goruganti         },
55054fc587aSNagaraju Goruganti         mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
55123a21a1cSEd Tanous         ldapConfigObjectName, interfaces);
5526973a582SRatan Gupta }
5536973a582SRatan Gupta 
5548a07d286SRatan Gupta /**
5558a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
5568a07d286SRatan Gupta  * @param input JSON data
5578a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5588a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
5598a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
5608a07d286SRatan Gupta  */
5614f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
5626c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5636c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
5648a07d286SRatan Gupta {
5658a07d286SRatan Gupta     std::optional<std::string> authType;
5668a07d286SRatan Gupta 
5678a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
5688a07d286SRatan Gupta                              authType, "Username", username, "Password",
5698a07d286SRatan Gupta                              password))
5708a07d286SRatan Gupta     {
5718a07d286SRatan Gupta         return;
5728a07d286SRatan Gupta     }
5738a07d286SRatan Gupta     if (!authType)
5748a07d286SRatan Gupta     {
5758a07d286SRatan Gupta         return;
5768a07d286SRatan Gupta     }
5778a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
5788a07d286SRatan Gupta     {
5798a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
5808a07d286SRatan Gupta                                          "AuthenticationType");
5818a07d286SRatan Gupta         return;
5828a07d286SRatan Gupta     }
5838a07d286SRatan Gupta }
5848a07d286SRatan Gupta /**
5858a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
5868a07d286SRatan Gupta  * @param input JSON data
5878a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5888a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
5898a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
5908a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
5918a07d286SRatan Gupta  */
5928a07d286SRatan Gupta 
5934f48d5f6SEd Tanous inline void
5944f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
5958d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5968a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
5978a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
5988a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
5998a07d286SRatan Gupta {
6008a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
6018a07d286SRatan Gupta 
6028a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
6038a07d286SRatan Gupta                              searchSettings))
6048a07d286SRatan Gupta     {
6058a07d286SRatan Gupta         return;
6068a07d286SRatan Gupta     }
6078a07d286SRatan Gupta     if (!searchSettings)
6088a07d286SRatan Gupta     {
6098a07d286SRatan Gupta         return;
6108a07d286SRatan Gupta     }
6118a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
6128a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
6138a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
6148a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
6158a07d286SRatan Gupta     {
6168a07d286SRatan Gupta         return;
6178a07d286SRatan Gupta     }
6188a07d286SRatan Gupta }
6198a07d286SRatan Gupta /**
6208a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6218a07d286SRatan Gupta           json response with the new value.
6228a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6238a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6248a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6258a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6268a07d286SRatan Gupta  */
6278a07d286SRatan Gupta 
6284f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6298a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6308d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6318a07d286SRatan Gupta     const std::string& ldapServerElementName,
6328a07d286SRatan Gupta     const std::string& ldapConfigObject)
6338a07d286SRatan Gupta {
6348a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6358a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
6368a07d286SRatan Gupta          serviceAddressList](const boost::system::error_code ec) {
6378a07d286SRatan Gupta             if (ec)
6388a07d286SRatan Gupta             {
6398a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG
640c61704abSGunnar Mills                     << "Error Occurred in updating the service address";
6418a07d286SRatan Gupta                 messages::internalError(asyncResp->res);
6428a07d286SRatan Gupta                 return;
6438a07d286SRatan Gupta             }
6448a07d286SRatan Gupta             std::vector<std::string> modifiedserviceAddressList = {
6458a07d286SRatan Gupta                 serviceAddressList.front()};
6468a07d286SRatan Gupta             asyncResp->res
6478a07d286SRatan Gupta                 .jsonValue[ldapServerElementName]["ServiceAddresses"] =
6488a07d286SRatan Gupta                 modifiedserviceAddressList;
6498a07d286SRatan Gupta             if ((serviceAddressList).size() > 1)
6508a07d286SRatan Gupta             {
6518a07d286SRatan Gupta                 messages::propertyValueModified(asyncResp->res,
6528a07d286SRatan Gupta                                                 "ServiceAddresses",
6538a07d286SRatan Gupta                                                 serviceAddressList.front());
6548a07d286SRatan Gupta             }
6558a07d286SRatan Gupta             BMCWEB_LOG_DEBUG << "Updated the service address";
6568a07d286SRatan Gupta         },
6578a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6588a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
659168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
6608a07d286SRatan Gupta }
6618a07d286SRatan Gupta /**
6628a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
6638a07d286SRatan Gupta           json response with the new value.
6648a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
6658a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6668a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6678a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6688a07d286SRatan Gupta  */
6698a07d286SRatan Gupta 
6704f48d5f6SEd Tanous inline void
6714f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
6728d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6738a07d286SRatan Gupta                         const std::string& ldapServerElementName,
6748a07d286SRatan Gupta                         const std::string& ldapConfigObject)
6758a07d286SRatan Gupta {
6768a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6778a07d286SRatan Gupta         [asyncResp, username,
6788a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
6798a07d286SRatan Gupta             if (ec)
6808a07d286SRatan Gupta             {
6816c51eab1SEd Tanous                 BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
6828a07d286SRatan Gupta                 messages::internalError(asyncResp->res);
6838a07d286SRatan Gupta                 return;
6848a07d286SRatan Gupta             }
6856c51eab1SEd Tanous             asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
6866c51eab1SEd Tanous                                     ["Username"] = username;
6878a07d286SRatan Gupta             BMCWEB_LOG_DEBUG << "Updated the username";
6888a07d286SRatan Gupta         },
6898a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
690168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
691168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
6928a07d286SRatan Gupta }
6938a07d286SRatan Gupta 
6948a07d286SRatan Gupta /**
6958a07d286SRatan Gupta  * @brief updates the LDAP password
6968a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
6978a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6988a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6998a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
7008a07d286SRatan Gupta  */
7018a07d286SRatan Gupta 
7024f48d5f6SEd Tanous inline void
7034f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
7048d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7058a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7068a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7078a07d286SRatan Gupta {
7088a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7098a07d286SRatan Gupta         [asyncResp, password,
7108a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7118a07d286SRatan Gupta             if (ec)
7128a07d286SRatan Gupta             {
7136c51eab1SEd Tanous                 BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
7148a07d286SRatan Gupta                 messages::internalError(asyncResp->res);
7158a07d286SRatan Gupta                 return;
7168a07d286SRatan Gupta             }
7176c51eab1SEd Tanous             asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
7186c51eab1SEd Tanous                                     ["Password"] = "";
7198a07d286SRatan Gupta             BMCWEB_LOG_DEBUG << "Updated the password";
7208a07d286SRatan Gupta         },
7218a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7228a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
723168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
7248a07d286SRatan Gupta }
7258a07d286SRatan Gupta 
7268a07d286SRatan Gupta /**
7278a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7288a07d286SRatan Gupta           json response with the new value.
7298a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7308a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7318a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7328a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7338a07d286SRatan Gupta  */
7348a07d286SRatan Gupta 
7354f48d5f6SEd Tanous inline void
7364f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7378d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7388a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7398a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7408a07d286SRatan Gupta {
7418a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7428a07d286SRatan Gupta         [asyncResp, baseDNList,
7438a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7448a07d286SRatan Gupta             if (ec)
7458a07d286SRatan Gupta             {
7466c51eab1SEd Tanous                 BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
7478a07d286SRatan Gupta                 messages::internalError(asyncResp->res);
7488a07d286SRatan Gupta                 return;
7498a07d286SRatan Gupta             }
7508a07d286SRatan Gupta             auto& serverTypeJson =
7518a07d286SRatan Gupta                 asyncResp->res.jsonValue[ldapServerElementName];
7528a07d286SRatan Gupta             auto& searchSettingsJson =
7538a07d286SRatan Gupta                 serverTypeJson["LDAPService"]["SearchSettings"];
7546c51eab1SEd Tanous             std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
7556c51eab1SEd Tanous             searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
7568a07d286SRatan Gupta             if (baseDNList.size() > 1)
7578a07d286SRatan Gupta             {
7588a07d286SRatan Gupta                 messages::propertyValueModified(asyncResp->res,
7598a07d286SRatan Gupta                                                 "BaseDistinguishedNames",
7608a07d286SRatan Gupta                                                 baseDNList.front());
7618a07d286SRatan Gupta             }
7628a07d286SRatan Gupta             BMCWEB_LOG_DEBUG << "Updated the base DN";
7638a07d286SRatan Gupta         },
7648a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7658a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
766168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
7678a07d286SRatan Gupta }
7688a07d286SRatan Gupta /**
7698a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
7708a07d286SRatan Gupta           json response with the new value.
7718a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
7728a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7738a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7748a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7758a07d286SRatan Gupta  */
7768a07d286SRatan Gupta 
7774f48d5f6SEd Tanous inline void
7784f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
7798d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7808a07d286SRatan Gupta                             const std::string& ldapServerElementName,
7818a07d286SRatan Gupta                             const std::string& ldapConfigObject)
7828a07d286SRatan Gupta {
7838a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7848a07d286SRatan Gupta         [asyncResp, userNameAttribute,
7858a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7868a07d286SRatan Gupta             if (ec)
7878a07d286SRatan Gupta             {
788c61704abSGunnar Mills                 BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
7898a07d286SRatan Gupta                                     "username attribute";
7908a07d286SRatan Gupta                 messages::internalError(asyncResp->res);
7918a07d286SRatan Gupta                 return;
7928a07d286SRatan Gupta             }
7938a07d286SRatan Gupta             auto& serverTypeJson =
7948a07d286SRatan Gupta                 asyncResp->res.jsonValue[ldapServerElementName];
7958a07d286SRatan Gupta             auto& searchSettingsJson =
7968a07d286SRatan Gupta                 serverTypeJson["LDAPService"]["SearchSettings"];
7978a07d286SRatan Gupta             searchSettingsJson["UsernameAttribute"] = userNameAttribute;
7988a07d286SRatan Gupta             BMCWEB_LOG_DEBUG << "Updated the user name attr.";
7998a07d286SRatan Gupta         },
8008a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8018a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
802168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
8038a07d286SRatan Gupta }
8048a07d286SRatan Gupta /**
8058a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
8068a07d286SRatan Gupta           json response with the new value.
8078a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
8088a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8098a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8108a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8118a07d286SRatan Gupta  */
8128a07d286SRatan Gupta 
8134f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
8148d1b46d7Szhanghch05     const std::string& groupsAttribute,
8158d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8168a07d286SRatan Gupta     const std::string& ldapServerElementName,
8178a07d286SRatan Gupta     const std::string& ldapConfigObject)
8188a07d286SRatan Gupta {
8198a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8208a07d286SRatan Gupta         [asyncResp, groupsAttribute,
8218a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8228a07d286SRatan Gupta             if (ec)
8238a07d286SRatan Gupta             {
824c61704abSGunnar Mills                 BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8258a07d286SRatan Gupta                                     "groupname attribute";
8268a07d286SRatan Gupta                 messages::internalError(asyncResp->res);
8278a07d286SRatan Gupta                 return;
8288a07d286SRatan Gupta             }
8298a07d286SRatan Gupta             auto& serverTypeJson =
8308a07d286SRatan Gupta                 asyncResp->res.jsonValue[ldapServerElementName];
8318a07d286SRatan Gupta             auto& searchSettingsJson =
8328a07d286SRatan Gupta                 serverTypeJson["LDAPService"]["SearchSettings"];
8338a07d286SRatan Gupta             searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8348a07d286SRatan Gupta             BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8358a07d286SRatan Gupta         },
8368a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8378a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
838168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
8398a07d286SRatan Gupta }
8408a07d286SRatan Gupta /**
8418a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
8428a07d286SRatan Gupta           json response with the new value.
8438a07d286SRatan Gupta  * @param input JSON data.
8448a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8458a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8468a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8478a07d286SRatan Gupta  */
8488a07d286SRatan Gupta 
8494f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8506c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8518a07d286SRatan Gupta     const std::string& ldapServerElementName,
8528a07d286SRatan Gupta     const std::string& ldapConfigObject)
8538a07d286SRatan Gupta {
8548a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8558a07d286SRatan Gupta         [asyncResp, serviceEnabled,
8568a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8578a07d286SRatan Gupta             if (ec)
8588a07d286SRatan Gupta             {
8598a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG
860c61704abSGunnar Mills                     << "Error Occurred in Updating the service enable";
8618a07d286SRatan Gupta                 messages::internalError(asyncResp->res);
8628a07d286SRatan Gupta                 return;
8638a07d286SRatan Gupta             }
8646c51eab1SEd Tanous             asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
8658a07d286SRatan Gupta                 serviceEnabled;
8666c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
8678a07d286SRatan Gupta         },
8688a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
869168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
870168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
8718a07d286SRatan Gupta }
8728a07d286SRatan Gupta 
8734f48d5f6SEd Tanous inline void
8744f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
8758d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
87678158631SZbigniew Kurzynski {
87778158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
87878158631SZbigniew Kurzynski     std::optional<bool> cookie;
87978158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
88078158631SZbigniew Kurzynski     std::optional<bool> xToken;
881501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
88278158631SZbigniew Kurzynski 
88378158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
88478158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
885501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
88678158631SZbigniew Kurzynski     {
88778158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
88878158631SZbigniew Kurzynski         return;
88978158631SZbigniew Kurzynski     }
89078158631SZbigniew Kurzynski 
89178158631SZbigniew Kurzynski     // Make a copy of methods configuration
89252cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
89352cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
89478158631SZbigniew Kurzynski 
89578158631SZbigniew Kurzynski     if (basicAuth)
89678158631SZbigniew Kurzynski     {
897f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
898f16f6263SAlan Kuo         messages::actionNotSupported(
8990fda0f12SGeorge Liu             asyncResp->res,
9000fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
901f16f6263SAlan Kuo         return;
902f16f6263SAlan Kuo #endif
90378158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
90478158631SZbigniew Kurzynski     }
90578158631SZbigniew Kurzynski 
90678158631SZbigniew Kurzynski     if (cookie)
90778158631SZbigniew Kurzynski     {
908f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
9090fda0f12SGeorge Liu         messages::actionNotSupported(
9100fda0f12SGeorge Liu             asyncResp->res,
9110fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
912f16f6263SAlan Kuo         return;
913f16f6263SAlan Kuo #endif
91478158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
91578158631SZbigniew Kurzynski     }
91678158631SZbigniew Kurzynski 
91778158631SZbigniew Kurzynski     if (sessionToken)
91878158631SZbigniew Kurzynski     {
919f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
920f16f6263SAlan Kuo         messages::actionNotSupported(
9210fda0f12SGeorge Liu             asyncResp->res,
9220fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
923f16f6263SAlan Kuo         return;
924f16f6263SAlan Kuo #endif
92578158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
92678158631SZbigniew Kurzynski     }
92778158631SZbigniew Kurzynski 
92878158631SZbigniew Kurzynski     if (xToken)
92978158631SZbigniew Kurzynski     {
930f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
9310fda0f12SGeorge Liu         messages::actionNotSupported(
9320fda0f12SGeorge Liu             asyncResp->res,
9330fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
934f16f6263SAlan Kuo         return;
935f16f6263SAlan Kuo #endif
93678158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
93778158631SZbigniew Kurzynski     }
93878158631SZbigniew Kurzynski 
939501f1e58SZbigniew Kurzynski     if (tls)
940501f1e58SZbigniew Kurzynski     {
941f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
9420fda0f12SGeorge Liu         messages::actionNotSupported(
9430fda0f12SGeorge Liu             asyncResp->res,
9440fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
945f16f6263SAlan Kuo         return;
946f16f6263SAlan Kuo #endif
947501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
948501f1e58SZbigniew Kurzynski     }
949501f1e58SZbigniew Kurzynski 
95078158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
951501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
952501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
95378158631SZbigniew Kurzynski     {
95478158631SZbigniew Kurzynski         // Do not allow user to disable everything
95578158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
95678158631SZbigniew Kurzynski                                      "of disabling all available methods");
95778158631SZbigniew Kurzynski         return;
95878158631SZbigniew Kurzynski     }
95978158631SZbigniew Kurzynski 
96052cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
96152cc112dSEd Tanous         authMethodsConfig);
96278158631SZbigniew Kurzynski     // Save configuration immediately
96352cc112dSEd Tanous     persistent_data::getConfig().writeData();
96478158631SZbigniew Kurzynski 
96578158631SZbigniew Kurzynski     messages::success(asyncResp->res);
96678158631SZbigniew Kurzynski }
96778158631SZbigniew Kurzynski 
9688a07d286SRatan Gupta /**
9698a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
9708a07d286SRatan Gupta  *        value and create the LDAP config object.
9718a07d286SRatan Gupta  * @param input JSON data
9728a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9738a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
9748a07d286SRatan Gupta  */
9758a07d286SRatan Gupta 
9766c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
9778d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9788a07d286SRatan Gupta                             const std::string& serverType)
9798a07d286SRatan Gupta {
980eb2bbe56SRatan Gupta     std::string dbusObjectPath;
981eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
982eb2bbe56SRatan Gupta     {
9832c70f800SEd Tanous         dbusObjectPath = adConfigObject;
984eb2bbe56SRatan Gupta     }
985eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
986eb2bbe56SRatan Gupta     {
98723a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
988eb2bbe56SRatan Gupta     }
989cb13a392SEd Tanous     else
990cb13a392SEd Tanous     {
991cb13a392SEd Tanous         return;
992cb13a392SEd Tanous     }
993eb2bbe56SRatan Gupta 
9948a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
9958a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
9968a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
9978a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
9988a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
9998a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
10008a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
10018a07d286SRatan Gupta     std::optional<std::string> userName;
10028a07d286SRatan Gupta     std::optional<std::string> password;
100306785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
10048a07d286SRatan Gupta 
10058a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
10068a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
10078a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
100806785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
100906785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
10108a07d286SRatan Gupta     {
10118a07d286SRatan Gupta         return;
10128a07d286SRatan Gupta     }
10138a07d286SRatan Gupta 
10148a07d286SRatan Gupta     if (authentication)
10158a07d286SRatan Gupta     {
10168a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
10178a07d286SRatan Gupta                                     password);
10188a07d286SRatan Gupta     }
10198a07d286SRatan Gupta     if (ldapService)
10208a07d286SRatan Gupta     {
10218a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10228a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
10238a07d286SRatan Gupta     }
10248a07d286SRatan Gupta     if (serviceAddressList)
10258a07d286SRatan Gupta     {
102626f6976fSEd Tanous         if (serviceAddressList->empty())
10278a07d286SRatan Gupta         {
10288a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10298a07d286SRatan Gupta                                              "ServiceAddress");
10308a07d286SRatan Gupta             return;
10318a07d286SRatan Gupta         }
10328a07d286SRatan Gupta     }
10338a07d286SRatan Gupta     if (baseDNList)
10348a07d286SRatan Gupta     {
103526f6976fSEd Tanous         if (baseDNList->empty())
10368a07d286SRatan Gupta         {
10378a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10388a07d286SRatan Gupta                                              "BaseDistinguishedNames");
10398a07d286SRatan Gupta             return;
10408a07d286SRatan Gupta         }
10418a07d286SRatan Gupta     }
10428a07d286SRatan Gupta 
10438a07d286SRatan Gupta     // nothing to update, then return
10448a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
104506785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
104606785244SRatan Gupta         !remoteRoleMapData)
10478a07d286SRatan Gupta     {
10488a07d286SRatan Gupta         return;
10498a07d286SRatan Gupta     }
10508a07d286SRatan Gupta 
10518a07d286SRatan Gupta     // Get the existing resource first then keep modifying
10528a07d286SRatan Gupta     // whenever any property gets updated.
10536c51eab1SEd Tanous     getLDAPConfigData(serverType, [asyncResp, userName, password, baseDNList,
10546c51eab1SEd Tanous                                    userNameAttribute, groupsAttribute,
10556c51eab1SEd Tanous                                    serviceAddressList, serviceEnabled,
10566c51eab1SEd Tanous                                    dbusObjectPath, remoteRoleMapData](
10576c51eab1SEd Tanous                                       bool success,
10586c51eab1SEd Tanous                                       const LDAPConfigData& confData,
105923a21a1cSEd Tanous                                       const std::string& serverT) {
10608a07d286SRatan Gupta         if (!success)
10618a07d286SRatan Gupta         {
10628a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10638a07d286SRatan Gupta             return;
10648a07d286SRatan Gupta         }
10656c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
10668a07d286SRatan Gupta         if (confData.serviceEnabled)
10678a07d286SRatan Gupta         {
10688a07d286SRatan Gupta             // Disable the service first and update the rest of
10698a07d286SRatan Gupta             // the properties.
10706c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
10718a07d286SRatan Gupta         }
10728a07d286SRatan Gupta 
10738a07d286SRatan Gupta         if (serviceAddressList)
10748a07d286SRatan Gupta         {
10756c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
10766c51eab1SEd Tanous                                       dbusObjectPath);
10778a07d286SRatan Gupta         }
10788a07d286SRatan Gupta         if (userName)
10798a07d286SRatan Gupta         {
10806c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
10818a07d286SRatan Gupta         }
10828a07d286SRatan Gupta         if (password)
10838a07d286SRatan Gupta         {
10846c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
10858a07d286SRatan Gupta         }
10868a07d286SRatan Gupta 
10878a07d286SRatan Gupta         if (baseDNList)
10888a07d286SRatan Gupta         {
10896c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
10908a07d286SRatan Gupta         }
10918a07d286SRatan Gupta         if (userNameAttribute)
10928a07d286SRatan Gupta         {
10936c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
10946c51eab1SEd Tanous                                     dbusObjectPath);
10958a07d286SRatan Gupta         }
10968a07d286SRatan Gupta         if (groupsAttribute)
10978a07d286SRatan Gupta         {
10986c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
10996c51eab1SEd Tanous                                      dbusObjectPath);
11008a07d286SRatan Gupta         }
11018a07d286SRatan Gupta         if (serviceEnabled)
11028a07d286SRatan Gupta         {
11038a07d286SRatan Gupta             // if user has given the value as true then enable
11048a07d286SRatan Gupta             // the service. if user has given false then no-op
11058a07d286SRatan Gupta             // as service is already stopped.
11068a07d286SRatan Gupta             if (*serviceEnabled)
11078a07d286SRatan Gupta             {
11086c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
11096c51eab1SEd Tanous                                          dbusObjectPath);
11108a07d286SRatan Gupta             }
11118a07d286SRatan Gupta         }
11128a07d286SRatan Gupta         else
11138a07d286SRatan Gupta         {
11148a07d286SRatan Gupta             // if user has not given the service enabled value
11158a07d286SRatan Gupta             // then revert it to the same state as it was
11168a07d286SRatan Gupta             // before.
11178a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
111823a21a1cSEd Tanous                                      serverT, dbusObjectPath);
11198a07d286SRatan Gupta         }
112006785244SRatan Gupta 
112106785244SRatan Gupta         if (remoteRoleMapData)
112206785244SRatan Gupta         {
11236c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
11246c51eab1SEd Tanous                                *remoteRoleMapData);
112506785244SRatan Gupta         }
11268a07d286SRatan Gupta     });
11278a07d286SRatan Gupta }
1128d4b5443fSEd Tanous 
11296c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
11306c51eab1SEd Tanous                                  const std::string& username,
11316c51eab1SEd Tanous                                  std::optional<std::string> password,
11326c51eab1SEd Tanous                                  std::optional<bool> enabled,
11336c51eab1SEd Tanous                                  std::optional<std::string> roleId,
11346c51eab1SEd Tanous                                  std::optional<bool> locked)
11351abe55efSEd Tanous {
1136b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1137b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1138b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
11396c51eab1SEd Tanous 
11406c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
11416c51eab1SEd Tanous         dbusObjectPath,
11421106333aSEd Tanous         [dbusObjectPath, username, password(std::move(password)),
11431106333aSEd Tanous          roleId(std::move(roleId)), enabled, locked,
11441106333aSEd Tanous          asyncResp{std::move(asyncResp)}](int rc) {
1145*e662eae8SEd Tanous             if (rc <= 0)
11466c51eab1SEd Tanous             {
11476c51eab1SEd Tanous                 messages::resourceNotFound(
11486c51eab1SEd Tanous                     asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
11496c51eab1SEd Tanous                     username);
11506c51eab1SEd Tanous                 return;
11516c51eab1SEd Tanous             }
11526c51eab1SEd Tanous 
11536c51eab1SEd Tanous             if (password)
11546c51eab1SEd Tanous             {
11556c51eab1SEd Tanous                 int retval = pamUpdatePassword(username, *password);
11566c51eab1SEd Tanous 
11576c51eab1SEd Tanous                 if (retval == PAM_USER_UNKNOWN)
11586c51eab1SEd Tanous                 {
11596c51eab1SEd Tanous                     messages::resourceNotFound(
11606c51eab1SEd Tanous                         asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
11616c51eab1SEd Tanous                         username);
11626c51eab1SEd Tanous                 }
11636c51eab1SEd Tanous                 else if (retval == PAM_AUTHTOK_ERR)
11646c51eab1SEd Tanous                 {
11656c51eab1SEd Tanous                     // If password is invalid
11666c51eab1SEd Tanous                     messages::propertyValueFormatError(asyncResp->res,
11676c51eab1SEd Tanous                                                        *password, "Password");
11686c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
11696c51eab1SEd Tanous                 }
11706c51eab1SEd Tanous                 else if (retval != PAM_SUCCESS)
11716c51eab1SEd Tanous                 {
11726c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11736c51eab1SEd Tanous                     return;
11746c51eab1SEd Tanous                 }
11756c51eab1SEd Tanous             }
11766c51eab1SEd Tanous 
11776c51eab1SEd Tanous             if (enabled)
11786c51eab1SEd Tanous             {
11796c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
11806c51eab1SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
11816c51eab1SEd Tanous                         if (ec)
11826c51eab1SEd Tanous                         {
11836c51eab1SEd Tanous                             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
11846c51eab1SEd Tanous                             messages::internalError(asyncResp->res);
11856c51eab1SEd Tanous                             return;
11866c51eab1SEd Tanous                         }
11876c51eab1SEd Tanous                         messages::success(asyncResp->res);
11886c51eab1SEd Tanous                         return;
11896c51eab1SEd Tanous                     },
1190e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
11916c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
11926c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserEnabled",
1193168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*enabled});
11946c51eab1SEd Tanous             }
11956c51eab1SEd Tanous 
11966c51eab1SEd Tanous             if (roleId)
11976c51eab1SEd Tanous             {
11986c51eab1SEd Tanous                 std::string priv = getPrivilegeFromRoleId(*roleId);
11996c51eab1SEd Tanous                 if (priv.empty())
12006c51eab1SEd Tanous                 {
12016c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, *roleId,
12026c51eab1SEd Tanous                                                      "RoleId");
12036c51eab1SEd Tanous                     return;
12046c51eab1SEd Tanous                 }
12056c51eab1SEd Tanous                 if (priv == "priv-noaccess")
12066c51eab1SEd Tanous                 {
12076c51eab1SEd Tanous                     priv = "";
12086c51eab1SEd Tanous                 }
12096c51eab1SEd Tanous 
12106c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12116c51eab1SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
12126c51eab1SEd Tanous                         if (ec)
12136c51eab1SEd Tanous                         {
12146c51eab1SEd Tanous                             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12156c51eab1SEd Tanous                             messages::internalError(asyncResp->res);
12166c51eab1SEd Tanous                             return;
12176c51eab1SEd Tanous                         }
12186c51eab1SEd Tanous                         messages::success(asyncResp->res);
12196c51eab1SEd Tanous                     },
1220e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12216c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12226c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1223168e20c1SEd Tanous                     dbus::utility::DbusVariantType{priv});
12246c51eab1SEd Tanous             }
12256c51eab1SEd Tanous 
12266c51eab1SEd Tanous             if (locked)
12276c51eab1SEd Tanous             {
12286c51eab1SEd Tanous                 // admin can unlock the account which is locked by
12296c51eab1SEd Tanous                 // successive authentication failures but admin should
12306c51eab1SEd Tanous                 // not be allowed to lock an account.
12316c51eab1SEd Tanous                 if (*locked)
12326c51eab1SEd Tanous                 {
12336c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, "true",
12346c51eab1SEd Tanous                                                      "Locked");
12356c51eab1SEd Tanous                     return;
12366c51eab1SEd Tanous                 }
12376c51eab1SEd Tanous 
12386c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12396c51eab1SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
12406c51eab1SEd Tanous                         if (ec)
12416c51eab1SEd Tanous                         {
12426c51eab1SEd Tanous                             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12436c51eab1SEd Tanous                             messages::internalError(asyncResp->res);
12446c51eab1SEd Tanous                             return;
12456c51eab1SEd Tanous                         }
12466c51eab1SEd Tanous                         messages::success(asyncResp->res);
12476c51eab1SEd Tanous                         return;
12486c51eab1SEd Tanous                     },
1249e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12506c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12516c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes",
1252168e20c1SEd Tanous                     "UserLockedForFailedAttempt",
1253168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*locked});
12546c51eab1SEd Tanous             }
12556c51eab1SEd Tanous         });
12566c51eab1SEd Tanous }
12576c51eab1SEd Tanous 
12586c51eab1SEd Tanous inline void requestAccountServiceRoutes(App& app)
12596c51eab1SEd Tanous {
12606c51eab1SEd Tanous 
12616c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
1262ed398213SEd Tanous         .privileges(redfish::privileges::getAccountService)
12636c51eab1SEd Tanous         .methods(
126472048780SAbhishek Patel             boost::beast::http::verb::get)([](const crow::Request& req,
12656c51eab1SEd Tanous                                               const std::shared_ptr<
12666c51eab1SEd Tanous                                                   bmcweb::AsyncResp>& asyncResp)
12676c51eab1SEd Tanous                                                -> void {
126852cc112dSEd Tanous             const persistent_data::AuthConfigMethods& authMethodsConfig =
12696c51eab1SEd Tanous                 persistent_data::SessionStore::getInstance()
12706c51eab1SEd Tanous                     .getAuthMethodsConfig();
127178158631SZbigniew Kurzynski 
12728d1b46d7Szhanghch05             asyncResp->res.jsonValue = {
12733d958bbcSAppaRao Puli                 {"@odata.id", "/redfish/v1/AccountService"},
12743d958bbcSAppaRao Puli                 {"@odata.type", "#AccountService."
1275ba9dd4a8SGunnar Mills                                 "v1_5_0.AccountService"},
12763d958bbcSAppaRao Puli                 {"Id", "AccountService"},
12773d958bbcSAppaRao Puli                 {"Name", "Account Service"},
12783d958bbcSAppaRao Puli                 {"Description", "Account Service"},
12793d958bbcSAppaRao Puli                 {"ServiceEnabled", true},
1280343ff2e1SAppaRao Puli                 {"MaxPasswordLength", 20},
12813d958bbcSAppaRao Puli                 {"Accounts",
12823d958bbcSAppaRao Puli                  {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}},
128337cce918SMarri Devender Rao                 {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}},
128478158631SZbigniew Kurzynski                 {"Oem",
128578158631SZbigniew Kurzynski                  {{"OpenBMC",
128678158631SZbigniew Kurzynski                    {{"@odata.type", "#OemAccountService.v1_0_0.AccountService"},
1287d8e3c29aSEd Tanous                     {"@odata.id", "/redfish/v1/AccountService#/Oem/OpenBMC"},
128878158631SZbigniew Kurzynski                     {"AuthMethods",
128978158631SZbigniew Kurzynski                      {
129078158631SZbigniew Kurzynski                          {"BasicAuth", authMethodsConfig.basic},
129178158631SZbigniew Kurzynski                          {"SessionToken", authMethodsConfig.sessionToken},
129278158631SZbigniew Kurzynski                          {"XToken", authMethodsConfig.xtoken},
129378158631SZbigniew Kurzynski                          {"Cookie", authMethodsConfig.cookie},
1294501f1e58SZbigniew Kurzynski                          {"TLS", authMethodsConfig.tls},
129572048780SAbhishek Patel                      }}}}}}};
129672048780SAbhishek Patel             // /redfish/v1/AccountService/LDAP/Certificates is something only
129772048780SAbhishek Patel             // ConfigureManager can access then only display when the user has
129872048780SAbhishek Patel             // permissions ConfigureManager
129972048780SAbhishek Patel             Privileges effectiveUserPrivileges =
130072048780SAbhishek Patel                 redfish::getUserPrivileges(req.userRole);
130172048780SAbhishek Patel 
130272048780SAbhishek Patel             if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
130372048780SAbhishek Patel                                                  effectiveUserPrivileges))
130472048780SAbhishek Patel             {
130572048780SAbhishek Patel                 asyncResp->res.jsonValue["LDAP"] = {
130672048780SAbhishek Patel                     {"Certificates",
130737cce918SMarri Devender Rao                      {{"@odata.id",
130872048780SAbhishek Patel                        "/redfish/v1/AccountService/LDAP/Certificates"}}}};
130972048780SAbhishek Patel             }
13103d958bbcSAppaRao Puli             crow::connections::systemBus->async_method_call(
13113d958bbcSAppaRao Puli                 [asyncResp](
13123d958bbcSAppaRao Puli                     const boost::system::error_code ec,
13136c51eab1SEd Tanous                     const std::vector<
1314168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
13153d958bbcSAppaRao Puli                         propertiesList) {
13163d958bbcSAppaRao Puli                     if (ec)
13173d958bbcSAppaRao Puli                     {
13183d958bbcSAppaRao Puli                         messages::internalError(asyncResp->res);
13193d958bbcSAppaRao Puli                         return;
13203d958bbcSAppaRao Puli                     }
13213d958bbcSAppaRao Puli                     BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
13223d958bbcSAppaRao Puli                                      << "properties for AccountService";
13233d958bbcSAppaRao Puli                     for (const std::pair<std::string,
1324168e20c1SEd Tanous                                          dbus::utility::DbusVariantType>&
1325168e20c1SEd Tanous                              property : propertiesList)
13263d958bbcSAppaRao Puli                     {
13273d958bbcSAppaRao Puli                         if (property.first == "MinPasswordLength")
13283d958bbcSAppaRao Puli                         {
13293d958bbcSAppaRao Puli                             const uint8_t* value =
1330abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
13313d958bbcSAppaRao Puli                             if (value != nullptr)
13323d958bbcSAppaRao Puli                             {
13333d958bbcSAppaRao Puli                                 asyncResp->res.jsonValue["MinPasswordLength"] =
13343d958bbcSAppaRao Puli                                     *value;
13353d958bbcSAppaRao Puli                             }
13363d958bbcSAppaRao Puli                         }
13373d958bbcSAppaRao Puli                         if (property.first == "AccountUnlockTimeout")
13383d958bbcSAppaRao Puli                         {
13393d958bbcSAppaRao Puli                             const uint32_t* value =
1340abf2add6SEd Tanous                                 std::get_if<uint32_t>(&property.second);
13413d958bbcSAppaRao Puli                             if (value != nullptr)
13423d958bbcSAppaRao Puli                             {
13436c51eab1SEd Tanous                                 asyncResp->res
13446c51eab1SEd Tanous                                     .jsonValue["AccountLockoutDuration"] =
13453d958bbcSAppaRao Puli                                     *value;
13463d958bbcSAppaRao Puli                             }
13473d958bbcSAppaRao Puli                         }
13483d958bbcSAppaRao Puli                         if (property.first == "MaxLoginAttemptBeforeLockout")
13493d958bbcSAppaRao Puli                         {
13503d958bbcSAppaRao Puli                             const uint16_t* value =
1351abf2add6SEd Tanous                                 std::get_if<uint16_t>(&property.second);
13523d958bbcSAppaRao Puli                             if (value != nullptr)
13533d958bbcSAppaRao Puli                             {
13543d958bbcSAppaRao Puli                                 asyncResp->res
13556c51eab1SEd Tanous                                     .jsonValue["AccountLockoutThreshold"] =
13566c51eab1SEd Tanous                                     *value;
13573d958bbcSAppaRao Puli                             }
13583d958bbcSAppaRao Puli                         }
13593d958bbcSAppaRao Puli                     }
13603d958bbcSAppaRao Puli                 },
13613d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
13623d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "GetAll",
13633d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy");
13646973a582SRatan Gupta 
1365ab828d7cSRatan Gupta             auto callback = [asyncResp](bool success, LDAPConfigData& confData,
1366ab828d7cSRatan Gupta                                         const std::string& ldapType) {
1367cb13a392SEd Tanous                 if (!success)
1368cb13a392SEd Tanous                 {
1369cb13a392SEd Tanous                     return;
1370cb13a392SEd Tanous                 }
13716c51eab1SEd Tanous                 parseLDAPConfigData(asyncResp->res.jsonValue, confData,
13726c51eab1SEd Tanous                                     ldapType);
1373ab828d7cSRatan Gupta             };
1374ab828d7cSRatan Gupta 
1375ab828d7cSRatan Gupta             getLDAPConfigData("LDAP", callback);
1376ab828d7cSRatan Gupta             getLDAPConfigData("ActiveDirectory", callback);
13776c51eab1SEd Tanous         });
13786973a582SRatan Gupta 
1379f5ffd806SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
13801ec43ee3SGunnar Mills         .privileges(redfish::privileges::patchAccountService)
1381f5ffd806SEd Tanous         .methods(boost::beast::http::verb::patch)(
1382f5ffd806SEd Tanous             [](const crow::Request& req,
1383f5ffd806SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
1384f5ffd806SEd Tanous                 std::optional<uint32_t> unlockTimeout;
1385f5ffd806SEd Tanous                 std::optional<uint16_t> lockoutThreshold;
1386ef73ad0dSPaul Fertser                 std::optional<uint8_t> minPasswordLength;
1387f5ffd806SEd Tanous                 std::optional<uint16_t> maxPasswordLength;
1388f5ffd806SEd Tanous                 std::optional<nlohmann::json> ldapObject;
1389f5ffd806SEd Tanous                 std::optional<nlohmann::json> activeDirectoryObject;
1390f5ffd806SEd Tanous                 std::optional<nlohmann::json> oemObject;
1391f5ffd806SEd Tanous 
1392f5ffd806SEd Tanous                 if (!json_util::readJson(
1393f5ffd806SEd Tanous                         req, asyncResp->res, "AccountLockoutDuration",
1394f5ffd806SEd Tanous                         unlockTimeout, "AccountLockoutThreshold",
1395f5ffd806SEd Tanous                         lockoutThreshold, "MaxPasswordLength",
1396f5ffd806SEd Tanous                         maxPasswordLength, "MinPasswordLength",
1397f5ffd806SEd Tanous                         minPasswordLength, "LDAP", ldapObject,
1398f5ffd806SEd Tanous                         "ActiveDirectory", activeDirectoryObject, "Oem",
1399f5ffd806SEd Tanous                         oemObject))
1400f5ffd806SEd Tanous                 {
1401f5ffd806SEd Tanous                     return;
1402f5ffd806SEd Tanous                 }
1403f5ffd806SEd Tanous 
1404f5ffd806SEd Tanous                 if (minPasswordLength)
1405f5ffd806SEd Tanous                 {
1406ef73ad0dSPaul Fertser                     crow::connections::systemBus->async_method_call(
1407ef73ad0dSPaul Fertser                         [asyncResp](const boost::system::error_code ec) {
1408ef73ad0dSPaul Fertser                             if (ec)
1409ef73ad0dSPaul Fertser                             {
1410ef73ad0dSPaul Fertser                                 messages::internalError(asyncResp->res);
1411ef73ad0dSPaul Fertser                                 return;
1412ef73ad0dSPaul Fertser                             }
1413ef73ad0dSPaul Fertser                             messages::success(asyncResp->res);
1414ef73ad0dSPaul Fertser                         },
1415ef73ad0dSPaul Fertser                         "xyz.openbmc_project.User.Manager",
1416ef73ad0dSPaul Fertser                         "/xyz/openbmc_project/user",
1417ef73ad0dSPaul Fertser                         "org.freedesktop.DBus.Properties", "Set",
1418ef73ad0dSPaul Fertser                         "xyz.openbmc_project.User.AccountPolicy",
1419ef73ad0dSPaul Fertser                         "MinPasswordLength",
1420ef73ad0dSPaul Fertser                         dbus::utility::DbusVariantType(*minPasswordLength));
1421f5ffd806SEd Tanous                 }
1422f5ffd806SEd Tanous 
1423f5ffd806SEd Tanous                 if (maxPasswordLength)
1424f5ffd806SEd Tanous                 {
1425f5ffd806SEd Tanous                     messages::propertyNotWritable(asyncResp->res,
1426f5ffd806SEd Tanous                                                   "MaxPasswordLength");
1427f5ffd806SEd Tanous                 }
1428f5ffd806SEd Tanous 
1429f5ffd806SEd Tanous                 if (ldapObject)
1430f5ffd806SEd Tanous                 {
1431f5ffd806SEd Tanous                     handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1432f5ffd806SEd Tanous                 }
1433f5ffd806SEd Tanous 
1434f5ffd806SEd Tanous                 if (std::optional<nlohmann::json> oemOpenBMCObject;
1435f5ffd806SEd Tanous                     oemObject &&
1436f5ffd806SEd Tanous                     json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1437f5ffd806SEd Tanous                                         oemOpenBMCObject))
1438f5ffd806SEd Tanous                 {
1439f5ffd806SEd Tanous                     if (std::optional<nlohmann::json> authMethodsObject;
1440f5ffd806SEd Tanous                         oemOpenBMCObject &&
1441f5ffd806SEd Tanous                         json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1442f5ffd806SEd Tanous                                             "AuthMethods", authMethodsObject))
1443f5ffd806SEd Tanous                     {
1444f5ffd806SEd Tanous                         if (authMethodsObject)
1445f5ffd806SEd Tanous                         {
1446f5ffd806SEd Tanous                             handleAuthMethodsPatch(*authMethodsObject,
1447f5ffd806SEd Tanous                                                    asyncResp);
1448f5ffd806SEd Tanous                         }
1449f5ffd806SEd Tanous                     }
1450f5ffd806SEd Tanous                 }
1451f5ffd806SEd Tanous 
1452f5ffd806SEd Tanous                 if (activeDirectoryObject)
1453f5ffd806SEd Tanous                 {
1454f5ffd806SEd Tanous                     handleLDAPPatch(*activeDirectoryObject, asyncResp,
1455f5ffd806SEd Tanous                                     "ActiveDirectory");
1456f5ffd806SEd Tanous                 }
1457f5ffd806SEd Tanous 
1458f5ffd806SEd Tanous                 if (unlockTimeout)
1459f5ffd806SEd Tanous                 {
1460f5ffd806SEd Tanous                     crow::connections::systemBus->async_method_call(
1461f5ffd806SEd Tanous                         [asyncResp](const boost::system::error_code ec) {
1462f5ffd806SEd Tanous                             if (ec)
1463f5ffd806SEd Tanous                             {
1464f5ffd806SEd Tanous                                 messages::internalError(asyncResp->res);
1465f5ffd806SEd Tanous                                 return;
1466f5ffd806SEd Tanous                             }
1467f5ffd806SEd Tanous                             messages::success(asyncResp->res);
1468f5ffd806SEd Tanous                         },
1469f5ffd806SEd Tanous                         "xyz.openbmc_project.User.Manager",
1470f5ffd806SEd Tanous                         "/xyz/openbmc_project/user",
1471f5ffd806SEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
1472f5ffd806SEd Tanous                         "xyz.openbmc_project.User.AccountPolicy",
1473f5ffd806SEd Tanous                         "AccountUnlockTimeout",
1474168e20c1SEd Tanous                         dbus::utility::DbusVariantType(*unlockTimeout));
1475f5ffd806SEd Tanous                 }
1476f5ffd806SEd Tanous                 if (lockoutThreshold)
1477f5ffd806SEd Tanous                 {
1478f5ffd806SEd Tanous                     crow::connections::systemBus->async_method_call(
1479f5ffd806SEd Tanous                         [asyncResp](const boost::system::error_code ec) {
1480f5ffd806SEd Tanous                             if (ec)
1481f5ffd806SEd Tanous                             {
1482f5ffd806SEd Tanous                                 messages::internalError(asyncResp->res);
1483f5ffd806SEd Tanous                                 return;
1484f5ffd806SEd Tanous                             }
1485f5ffd806SEd Tanous                             messages::success(asyncResp->res);
1486f5ffd806SEd Tanous                         },
1487f5ffd806SEd Tanous                         "xyz.openbmc_project.User.Manager",
1488f5ffd806SEd Tanous                         "/xyz/openbmc_project/user",
1489f5ffd806SEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
1490f5ffd806SEd Tanous                         "xyz.openbmc_project.User.AccountPolicy",
1491f5ffd806SEd Tanous                         "MaxLoginAttemptBeforeLockout",
1492168e20c1SEd Tanous                         dbus::utility::DbusVariantType(*lockoutThreshold));
1493f5ffd806SEd Tanous                 }
1494f5ffd806SEd Tanous             });
1495f5ffd806SEd Tanous 
14966c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
1497ed398213SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
14986c51eab1SEd Tanous         .methods(boost::beast::http::verb::get)(
14996c51eab1SEd Tanous             [](const crow::Request& req,
15006c51eab1SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
15018d1b46d7Szhanghch05                 asyncResp->res.jsonValue = {
15028d1b46d7Szhanghch05                     {"@odata.id", "/redfish/v1/AccountService/Accounts"},
15030f74e643SEd Tanous                     {"@odata.type", "#ManagerAccountCollection."
15040f74e643SEd Tanous                                     "ManagerAccountCollection"},
15050f74e643SEd Tanous                     {"Name", "Accounts Collection"},
15060f74e643SEd Tanous                     {"Description", "BMC User Accounts"}};
15070f74e643SEd Tanous 
15086c51eab1SEd Tanous                 Privileges effectiveUserPrivileges =
15096c51eab1SEd Tanous                     redfish::getUserPrivileges(req.userRole);
15106c51eab1SEd Tanous 
1511f5e29f33SJunLin Chen                 std::string thisUser;
1512f5e29f33SJunLin Chen                 if (req.session)
1513f5e29f33SJunLin Chen                 {
1514f5e29f33SJunLin Chen                     thisUser = req.session->username;
1515f5e29f33SJunLin Chen                 }
1516b9b2e0b2SEd Tanous                 crow::connections::systemBus->async_method_call(
1517cef1ddfbSEd Tanous                     [asyncResp, thisUser, effectiveUserPrivileges](
1518cef1ddfbSEd Tanous                         const boost::system::error_code ec,
1519711ac7a9SEd Tanous                         const dbus::utility::ManagedObjectType& users) {
1520b9b2e0b2SEd Tanous                         if (ec)
1521b9b2e0b2SEd Tanous                         {
1522f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
1523b9b2e0b2SEd Tanous                             return;
1524b9b2e0b2SEd Tanous                         }
1525b9b2e0b2SEd Tanous 
1526cef1ddfbSEd Tanous                         bool userCanSeeAllAccounts =
1527cef1ddfbSEd Tanous                             effectiveUserPrivileges.isSupersetOf(
15284f48d5f6SEd Tanous                                 {"ConfigureUsers"});
1529cef1ddfbSEd Tanous 
1530cef1ddfbSEd Tanous                         bool userCanSeeSelf =
1531cef1ddfbSEd Tanous                             effectiveUserPrivileges.isSupersetOf(
15324f48d5f6SEd Tanous                                 {"ConfigureSelf"});
1533cef1ddfbSEd Tanous 
1534b9b2e0b2SEd Tanous                         nlohmann::json& memberArray =
1535b9b2e0b2SEd Tanous                             asyncResp->res.jsonValue["Members"];
1536b9b2e0b2SEd Tanous                         memberArray = nlohmann::json::array();
1537b9b2e0b2SEd Tanous 
15389eb808c1SEd Tanous                         for (const auto& userpath : users)
1539b9b2e0b2SEd Tanous                         {
15402dfd18efSEd Tanous                             std::string user = userpath.first.filename();
15412dfd18efSEd Tanous                             if (user.empty())
1542b9b2e0b2SEd Tanous                             {
15432dfd18efSEd Tanous                                 messages::internalError(asyncResp->res);
15442dfd18efSEd Tanous                                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
15452dfd18efSEd Tanous 
15462dfd18efSEd Tanous                                 return;
1547b9b2e0b2SEd Tanous                             }
1548f365910cSGunnar Mills 
1549f365910cSGunnar Mills                             // As clarified by Redfish here:
1550f365910cSGunnar Mills                             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
15516c51eab1SEd Tanous                             // Users without ConfigureUsers, only see their own
15526c51eab1SEd Tanous                             // account. Users with ConfigureUsers, see all
15536c51eab1SEd Tanous                             // accounts.
1554cef1ddfbSEd Tanous                             if (userCanSeeAllAccounts ||
1555cef1ddfbSEd Tanous                                 (thisUser == user && userCanSeeSelf))
1556f365910cSGunnar Mills                             {
1557b9b2e0b2SEd Tanous                                 memberArray.push_back(
1558f365910cSGunnar Mills                                     {{"@odata.id",
15596c51eab1SEd Tanous                                       "/redfish/v1/AccountService/Accounts/" +
15606c51eab1SEd Tanous                                           user}});
1561b9b2e0b2SEd Tanous                             }
1562f365910cSGunnar Mills                         }
1563f365910cSGunnar Mills                         asyncResp->res.jsonValue["Members@odata.count"] =
1564f365910cSGunnar Mills                             memberArray.size();
1565b9b2e0b2SEd Tanous                     },
15666c51eab1SEd Tanous                     "xyz.openbmc_project.User.Manager",
15676c51eab1SEd Tanous                     "/xyz/openbmc_project/user",
1568b9b2e0b2SEd Tanous                     "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15696c51eab1SEd Tanous             });
15706c51eab1SEd Tanous 
15716c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
1572ed398213SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
15736c51eab1SEd Tanous         .methods(boost::beast::http::verb::post)([](const crow::Request& req,
15746c51eab1SEd Tanous                                                     const std::shared_ptr<
15756c51eab1SEd Tanous                                                         bmcweb::AsyncResp>&
15766c51eab1SEd Tanous                                                         asyncResp) -> void {
15779712f8acSEd Tanous             std::string username;
15789712f8acSEd Tanous             std::string password;
1579a24526dcSEd Tanous             std::optional<std::string> roleId("User");
1580a24526dcSEd Tanous             std::optional<bool> enabled = true;
15818d1b46d7Szhanghch05             if (!json_util::readJson(req, asyncResp->res, "UserName", username,
15828d1b46d7Szhanghch05                                      "Password", password, "RoleId", roleId,
15838d1b46d7Szhanghch05                                      "Enabled", enabled))
158404ae99ecSEd Tanous             {
158504ae99ecSEd Tanous                 return;
158604ae99ecSEd Tanous             }
158704ae99ecSEd Tanous 
158854fc587aSNagaraju Goruganti             std::string priv = getPrivilegeFromRoleId(*roleId);
158984e12cb7SAppaRao Puli             if (priv.empty())
159004ae99ecSEd Tanous             {
15916c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, *roleId,
15926c51eab1SEd Tanous                                                  "RoleId");
159304ae99ecSEd Tanous                 return;
159404ae99ecSEd Tanous             }
159596200606Sjayaprakash Mutyala             // TODO: Following override will be reverted once support in
15966c51eab1SEd Tanous             // phosphor-user-manager is added. In order to avoid dependency
15976c51eab1SEd Tanous             // issues, this is added in bmcweb, which will removed, once
159896200606Sjayaprakash Mutyala             // phosphor-user-manager supports priv-noaccess.
159996200606Sjayaprakash Mutyala             if (priv == "priv-noaccess")
160096200606Sjayaprakash Mutyala             {
160196200606Sjayaprakash Mutyala                 roleId = "";
160296200606Sjayaprakash Mutyala             }
160396200606Sjayaprakash Mutyala             else
160496200606Sjayaprakash Mutyala             {
16059712f8acSEd Tanous                 roleId = priv;
160696200606Sjayaprakash Mutyala             }
160704ae99ecSEd Tanous 
1608599c71d8SAyushi Smriti             // Reading AllGroups property
16091e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::vector<std::string>>(
16101e1e598dSJonathan Doman                 *crow::connections::systemBus,
16111e1e598dSJonathan Doman                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
16121e1e598dSJonathan Doman                 "xyz.openbmc_project.User.Manager", "AllGroups",
1613599c71d8SAyushi Smriti                 [asyncResp, username, password{std::move(password)}, roleId,
1614168e20c1SEd Tanous                  enabled](const boost::system::error_code ec,
16151e1e598dSJonathan Doman                           const std::vector<std::string>& allGroupsList) {
1616599c71d8SAyushi Smriti                     if (ec)
1617599c71d8SAyushi Smriti                     {
1618599c71d8SAyushi Smriti                         BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1619599c71d8SAyushi Smriti                         messages::internalError(asyncResp->res);
1620599c71d8SAyushi Smriti                         return;
1621599c71d8SAyushi Smriti                     }
1622599c71d8SAyushi Smriti 
16231e1e598dSJonathan Doman                     if (allGroupsList.empty())
1624599c71d8SAyushi Smriti                     {
1625599c71d8SAyushi Smriti                         messages::internalError(asyncResp->res);
1626599c71d8SAyushi Smriti                         return;
1627599c71d8SAyushi Smriti                     }
1628599c71d8SAyushi Smriti 
162904ae99ecSEd Tanous                     crow::connections::systemBus->async_method_call(
1630f23b7296SEd Tanous                         [asyncResp, username,
1631f23b7296SEd Tanous                          password](const boost::system::error_code ec2,
16320d4197e0Sanil kumar appana                                    sdbusplus::message::message& m) {
163323a21a1cSEd Tanous                             if (ec2)
163404ae99ecSEd Tanous                             {
16356c51eab1SEd Tanous                                 userErrorMessageHandler(
16366c51eab1SEd Tanous                                     m.get_error(), asyncResp, username, "");
163704ae99ecSEd Tanous                                 return;
163804ae99ecSEd Tanous                             }
163904ae99ecSEd Tanous 
164066b5ca76Sjayaprakash Mutyala                             if (pamUpdatePassword(username, password) !=
164166b5ca76Sjayaprakash Mutyala                                 PAM_SUCCESS)
164204ae99ecSEd Tanous                             {
16436c51eab1SEd Tanous                                 // At this point we have a user that's been
16446c51eab1SEd Tanous                                 // created, but the password set
16456c51eab1SEd Tanous                                 // failed.Something is wrong, so delete the user
16466c51eab1SEd Tanous                                 // that we've already created
1647b477fd44SP Dheeraj Srujan Kumar                                 sdbusplus::message::object_path tempObjPath(
1648b477fd44SP Dheeraj Srujan Kumar                                     rootUserDbusPath);
1649b477fd44SP Dheeraj Srujan Kumar                                 tempObjPath /= username;
1650b477fd44SP Dheeraj Srujan Kumar                                 const std::string userPath(tempObjPath);
1651b477fd44SP Dheeraj Srujan Kumar 
165204ae99ecSEd Tanous                                 crow::connections::systemBus->async_method_call(
165323a21a1cSEd Tanous                                     [asyncResp, password](
165423a21a1cSEd Tanous                                         const boost::system::error_code ec3) {
165523a21a1cSEd Tanous                                         if (ec3)
165604ae99ecSEd Tanous                                         {
16576c51eab1SEd Tanous                                             messages::internalError(
16586c51eab1SEd Tanous                                                 asyncResp->res);
165904ae99ecSEd Tanous                                             return;
166004ae99ecSEd Tanous                                         }
166104ae99ecSEd Tanous 
16620d4197e0Sanil kumar appana                                         // If password is invalid
16630d4197e0Sanil kumar appana                                         messages::propertyValueFormatError(
16646c51eab1SEd Tanous                                             asyncResp->res, password,
16656c51eab1SEd Tanous                                             "Password");
166604ae99ecSEd Tanous                                     },
166704ae99ecSEd Tanous                                     "xyz.openbmc_project.User.Manager",
1668b477fd44SP Dheeraj Srujan Kumar                                     userPath,
16696c51eab1SEd Tanous                                     "xyz.openbmc_project.Object.Delete",
16706c51eab1SEd Tanous                                     "Delete");
167104ae99ecSEd Tanous 
167204ae99ecSEd Tanous                                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
167304ae99ecSEd Tanous                                 return;
167404ae99ecSEd Tanous                             }
167504ae99ecSEd Tanous 
1676f12894f8SJason M. Bills                             messages::created(asyncResp->res);
167704ae99ecSEd Tanous                             asyncResp->res.addHeader(
167804ae99ecSEd Tanous                                 "Location",
16796c51eab1SEd Tanous                                 "/redfish/v1/AccountService/Accounts/" +
16806c51eab1SEd Tanous                                     username);
168104ae99ecSEd Tanous                         },
1682599c71d8SAyushi Smriti                         "xyz.openbmc_project.User.Manager",
1683599c71d8SAyushi Smriti                         "/xyz/openbmc_project/user",
16846c51eab1SEd Tanous                         "xyz.openbmc_project.User.Manager", "CreateUser",
16851e1e598dSJonathan Doman                         username, allGroupsList, *roleId, *enabled);
16861e1e598dSJonathan Doman                 });
16876c51eab1SEd Tanous         });
1688b9b2e0b2SEd Tanous 
16896c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
1690ed398213SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
16916c51eab1SEd Tanous         .methods(
16926c51eab1SEd Tanous             boost::beast::http::verb::
16936c51eab1SEd Tanous                 get)([](const crow::Request& req,
16946c51eab1SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16956c51eab1SEd Tanous                         const std::string& accountName) -> void {
16966c51eab1SEd Tanous             if (req.session->username != accountName)
1697b9b2e0b2SEd Tanous             {
16986c51eab1SEd Tanous                 // At this point we've determined that the user is trying to
16996c51eab1SEd Tanous                 // modify a user that isn't them.  We need to verify that they
17006c51eab1SEd Tanous                 // have permissions to modify other users, so re-run the auth
17016c51eab1SEd Tanous                 // check with the same permissions, minus ConfigureSelf.
17026c51eab1SEd Tanous                 Privileges effectiveUserPrivileges =
17036c51eab1SEd Tanous                     redfish::getUserPrivileges(req.userRole);
17046c51eab1SEd Tanous                 Privileges requiredPermissionsToChangeNonSelf = {
17054f48d5f6SEd Tanous                     "ConfigureUsers", "ConfigureManager"};
17066c51eab1SEd Tanous                 if (!effectiveUserPrivileges.isSupersetOf(
17076c51eab1SEd Tanous                         requiredPermissionsToChangeNonSelf))
1708900f9497SJoseph Reynolds                 {
1709900f9497SJoseph Reynolds                     BMCWEB_LOG_DEBUG << "GET Account denied access";
1710900f9497SJoseph Reynolds                     messages::insufficientPrivilege(asyncResp->res);
1711900f9497SJoseph Reynolds                     return;
1712900f9497SJoseph Reynolds                 }
1713900f9497SJoseph Reynolds             }
1714900f9497SJoseph Reynolds 
1715b9b2e0b2SEd Tanous             crow::connections::systemBus->async_method_call(
1716711ac7a9SEd Tanous                 [asyncResp,
1717711ac7a9SEd Tanous                  accountName](const boost::system::error_code ec,
1718711ac7a9SEd Tanous                               const dbus::utility::ManagedObjectType& users) {
1719b9b2e0b2SEd Tanous                     if (ec)
1720b9b2e0b2SEd Tanous                     {
1721f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
1722b9b2e0b2SEd Tanous                         return;
1723b9b2e0b2SEd Tanous                     }
1724711ac7a9SEd Tanous                     const auto userIt = std::find_if(
1725b477fd44SP Dheeraj Srujan Kumar                         users.begin(), users.end(),
1726b477fd44SP Dheeraj Srujan Kumar                         [accountName](
1727b477fd44SP Dheeraj Srujan Kumar                             const std::pair<sdbusplus::message::object_path,
1728711ac7a9SEd Tanous                                             dbus::utility::DBusInteracesMap>&
1729711ac7a9SEd Tanous                                 user) {
1730*e662eae8SEd Tanous                             return accountName.compare(user.first.filename()) ==
1731*e662eae8SEd Tanous                                    0;
1732b477fd44SP Dheeraj Srujan Kumar                         });
1733b9b2e0b2SEd Tanous 
173484e12cb7SAppaRao Puli                     if (userIt == users.end())
1735b9b2e0b2SEd Tanous                     {
17366c51eab1SEd Tanous                         messages::resourceNotFound(
17376c51eab1SEd Tanous                             asyncResp->res, "ManagerAccount", accountName);
173884e12cb7SAppaRao Puli                         return;
173984e12cb7SAppaRao Puli                     }
17404e68c45bSAyushi Smriti 
17414e68c45bSAyushi Smriti                     asyncResp->res.jsonValue = {
17426c51eab1SEd Tanous                         {"@odata.type",
17436c51eab1SEd Tanous                          "#ManagerAccount.v1_4_0.ManagerAccount"},
17444e68c45bSAyushi Smriti                         {"Name", "User Account"},
17454e68c45bSAyushi Smriti                         {"Description", "User Account"},
17468114bd4dSGunnar Mills                         {"Password", nullptr},
17478114bd4dSGunnar Mills                         {"AccountTypes", {"Redfish"}}};
17484e68c45bSAyushi Smriti 
174984e12cb7SAppaRao Puli                     for (const auto& interface : userIt->second)
175065b0dc32SEd Tanous                     {
175165b0dc32SEd Tanous                         if (interface.first ==
175265b0dc32SEd Tanous                             "xyz.openbmc_project.User.Attributes")
175365b0dc32SEd Tanous                         {
175465b0dc32SEd Tanous                             for (const auto& property : interface.second)
175565b0dc32SEd Tanous                             {
175665b0dc32SEd Tanous                                 if (property.first == "UserEnabled")
175765b0dc32SEd Tanous                                 {
175865b0dc32SEd Tanous                                     const bool* userEnabled =
1759abf2add6SEd Tanous                                         std::get_if<bool>(&property.second);
176065b0dc32SEd Tanous                                     if (userEnabled == nullptr)
176165b0dc32SEd Tanous                                     {
176265b0dc32SEd Tanous                                         BMCWEB_LOG_ERROR
176365b0dc32SEd Tanous                                             << "UserEnabled wasn't a bool";
176484e12cb7SAppaRao Puli                                         messages::internalError(asyncResp->res);
176584e12cb7SAppaRao Puli                                         return;
176665b0dc32SEd Tanous                                     }
176765b0dc32SEd Tanous                                     asyncResp->res.jsonValue["Enabled"] =
176865b0dc32SEd Tanous                                         *userEnabled;
176965b0dc32SEd Tanous                                 }
177065b0dc32SEd Tanous                                 else if (property.first ==
177165b0dc32SEd Tanous                                          "UserLockedForFailedAttempt")
177265b0dc32SEd Tanous                                 {
177365b0dc32SEd Tanous                                     const bool* userLocked =
1774abf2add6SEd Tanous                                         std::get_if<bool>(&property.second);
177565b0dc32SEd Tanous                                     if (userLocked == nullptr)
177665b0dc32SEd Tanous                                     {
177784e12cb7SAppaRao Puli                                         BMCWEB_LOG_ERROR << "UserLockedForF"
177884e12cb7SAppaRao Puli                                                             "ailedAttempt "
177984e12cb7SAppaRao Puli                                                             "wasn't a bool";
178084e12cb7SAppaRao Puli                                         messages::internalError(asyncResp->res);
178184e12cb7SAppaRao Puli                                         return;
178265b0dc32SEd Tanous                                     }
178365b0dc32SEd Tanous                                     asyncResp->res.jsonValue["Locked"] =
178465b0dc32SEd Tanous                                         *userLocked;
178524c8542dSRatan Gupta                                     asyncResp->res.jsonValue
178624c8542dSRatan Gupta                                         ["Locked@Redfish.AllowableValues"] = {
17873bf4e632SJoseph Reynolds                                         "false"}; // can only unlock accounts
178865b0dc32SEd Tanous                                 }
178984e12cb7SAppaRao Puli                                 else if (property.first == "UserPrivilege")
179084e12cb7SAppaRao Puli                                 {
179154fc587aSNagaraju Goruganti                                     const std::string* userPrivPtr =
17926c51eab1SEd Tanous                                         std::get_if<std::string>(
17936c51eab1SEd Tanous                                             &property.second);
179454fc587aSNagaraju Goruganti                                     if (userPrivPtr == nullptr)
179584e12cb7SAppaRao Puli                                     {
179684e12cb7SAppaRao Puli                                         BMCWEB_LOG_ERROR
179784e12cb7SAppaRao Puli                                             << "UserPrivilege wasn't a "
179884e12cb7SAppaRao Puli                                                "string";
179984e12cb7SAppaRao Puli                                         messages::internalError(asyncResp->res);
180084e12cb7SAppaRao Puli                                         return;
180184e12cb7SAppaRao Puli                                     }
180254fc587aSNagaraju Goruganti                                     std::string role =
180354fc587aSNagaraju Goruganti                                         getRoleIdFromPrivilege(*userPrivPtr);
180454fc587aSNagaraju Goruganti                                     if (role.empty())
180584e12cb7SAppaRao Puli                                     {
180684e12cb7SAppaRao Puli                                         BMCWEB_LOG_ERROR << "Invalid user role";
180784e12cb7SAppaRao Puli                                         messages::internalError(asyncResp->res);
180884e12cb7SAppaRao Puli                                         return;
180984e12cb7SAppaRao Puli                                     }
181054fc587aSNagaraju Goruganti                                     asyncResp->res.jsonValue["RoleId"] = role;
181184e12cb7SAppaRao Puli 
18126c51eab1SEd Tanous                                     asyncResp->res.jsonValue["Links"]["Role"] =
18136c51eab1SEd Tanous                                         {{"@odata.id",
18140fda0f12SGeorge Liu                                           "/redfish/v1/AccountService/Roles/" +
181554fc587aSNagaraju Goruganti                                               role}};
181684e12cb7SAppaRao Puli                                 }
18176c51eab1SEd Tanous                                 else if (property.first ==
18186c51eab1SEd Tanous                                          "UserPasswordExpired")
18193bf4e632SJoseph Reynolds                                 {
18203bf4e632SJoseph Reynolds                                     const bool* userPasswordExpired =
18213bf4e632SJoseph Reynolds                                         std::get_if<bool>(&property.second);
18223bf4e632SJoseph Reynolds                                     if (userPasswordExpired == nullptr)
18233bf4e632SJoseph Reynolds                                     {
18240fda0f12SGeorge Liu                                         BMCWEB_LOG_ERROR
18250fda0f12SGeorge Liu                                             << "UserPasswordExpired wasn't a bool";
18263bf4e632SJoseph Reynolds                                         messages::internalError(asyncResp->res);
18273bf4e632SJoseph Reynolds                                         return;
18283bf4e632SJoseph Reynolds                                     }
18293bf4e632SJoseph Reynolds                                     asyncResp->res
18303bf4e632SJoseph Reynolds                                         .jsonValue["PasswordChangeRequired"] =
18313bf4e632SJoseph Reynolds                                         *userPasswordExpired;
18323bf4e632SJoseph Reynolds                                 }
183365b0dc32SEd Tanous                             }
183465b0dc32SEd Tanous                         }
183565b0dc32SEd Tanous                     }
183665b0dc32SEd Tanous 
1837b9b2e0b2SEd Tanous                     asyncResp->res.jsonValue["@odata.id"] =
183884e12cb7SAppaRao Puli                         "/redfish/v1/AccountService/Accounts/" + accountName;
1839b9b2e0b2SEd Tanous                     asyncResp->res.jsonValue["Id"] = accountName;
1840b9b2e0b2SEd Tanous                     asyncResp->res.jsonValue["UserName"] = accountName;
1841b9b2e0b2SEd Tanous                 },
1842b9b2e0b2SEd Tanous                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1843b9b2e0b2SEd Tanous                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18446c51eab1SEd Tanous         });
1845a840879dSEd Tanous 
18466c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
1847ed398213SEd Tanous         // TODO this privilege should be using the generated endpoints, but
1848ed398213SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
1849ed398213SEd Tanous         // yet
18506c51eab1SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
18516c51eab1SEd Tanous         .methods(boost::beast::http::verb::patch)(
18526c51eab1SEd Tanous             [](const crow::Request& req,
18536c51eab1SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18546c51eab1SEd Tanous                const std::string& username) -> void {
1855a24526dcSEd Tanous                 std::optional<std::string> newUserName;
1856a24526dcSEd Tanous                 std::optional<std::string> password;
1857a24526dcSEd Tanous                 std::optional<bool> enabled;
1858a24526dcSEd Tanous                 std::optional<std::string> roleId;
185924c8542dSRatan Gupta                 std::optional<bool> locked;
1860e9cc5172SEd Tanous 
1861e9cc5172SEd Tanous                 Privileges effectiveUserPrivileges =
1862e9cc5172SEd Tanous                     redfish::getUserPrivileges(req.userRole);
1863e9cc5172SEd Tanous                 Privileges configureUsers = {"ConfigureUsers"};
1864e9cc5172SEd Tanous                 bool userHasConfigureUsers =
1865e9cc5172SEd Tanous                     effectiveUserPrivileges.isSupersetOf(configureUsers);
1866e9cc5172SEd Tanous                 if (userHasConfigureUsers)
1867e9cc5172SEd Tanous                 {
1868e9cc5172SEd Tanous                     // Users with ConfigureUsers can modify for all users
18696c51eab1SEd Tanous                     if (!json_util::readJson(req, asyncResp->res, "UserName",
18706c51eab1SEd Tanous                                              newUserName, "Password", password,
1871e9cc5172SEd Tanous                                              "RoleId", roleId, "Enabled",
1872e9cc5172SEd Tanous                                              enabled, "Locked", locked))
1873a840879dSEd Tanous                     {
1874a840879dSEd Tanous                         return;
1875a840879dSEd Tanous                     }
1876e9cc5172SEd Tanous                 }
1877e9cc5172SEd Tanous                 else
1878900f9497SJoseph Reynolds                 {
1879e9cc5172SEd Tanous                     // ConfigureSelf accounts can only modify their own account
1880e9cc5172SEd Tanous                     if (username != req.session->username)
1881900f9497SJoseph Reynolds                     {
1882900f9497SJoseph Reynolds                         messages::insufficientPrivilege(asyncResp->res);
1883900f9497SJoseph Reynolds                         return;
1884900f9497SJoseph Reynolds                     }
1885e9cc5172SEd Tanous                     // ConfigureSelf accounts can only modify their password
1886e9cc5172SEd Tanous                     if (!json_util::readJson(req, asyncResp->res, "Password",
1887e9cc5172SEd Tanous                                              password))
1888e9cc5172SEd Tanous                     {
1889e9cc5172SEd Tanous                         return;
1890e9cc5172SEd Tanous                     }
1891900f9497SJoseph Reynolds                 }
1892900f9497SJoseph Reynolds 
189366b5ca76Sjayaprakash Mutyala                 // if user name is not provided in the patch method or if it
18946c51eab1SEd Tanous                 // matches the user name in the URI, then we are treating it as
18956c51eab1SEd Tanous                 // updating user properties other then username. If username
18966c51eab1SEd Tanous                 // provided doesn't match the URI, then we are treating this as
18976c51eab1SEd Tanous                 // user rename request.
189866b5ca76Sjayaprakash Mutyala                 if (!newUserName || (newUserName.value() == username))
1899a840879dSEd Tanous                 {
19006c51eab1SEd Tanous                     updateUserProperties(asyncResp, username, password, enabled,
19016c51eab1SEd Tanous                                          roleId, locked);
190284e12cb7SAppaRao Puli                     return;
190384e12cb7SAppaRao Puli                 }
190484e12cb7SAppaRao Puli                 crow::connections::systemBus->async_method_call(
19056c51eab1SEd Tanous                     [asyncResp, username, password(std::move(password)),
1906f23b7296SEd Tanous                      roleId(std::move(roleId)), enabled,
190766b5ca76Sjayaprakash Mutyala                      newUser{std::string(*newUserName)},
1908f23b7296SEd Tanous                      locked](const boost::system::error_code ec,
190966b5ca76Sjayaprakash Mutyala                              sdbusplus::message::message& m) {
191084e12cb7SAppaRao Puli                         if (ec)
191184e12cb7SAppaRao Puli                         {
19126c51eab1SEd Tanous                             userErrorMessageHandler(m.get_error(), asyncResp,
19136c51eab1SEd Tanous                                                     newUser, username);
1914a840879dSEd Tanous                             return;
1915a840879dSEd Tanous                         }
1916a840879dSEd Tanous 
19176c51eab1SEd Tanous                         updateUserProperties(asyncResp, newUser, password,
19186c51eab1SEd Tanous                                              enabled, roleId, locked);
191984e12cb7SAppaRao Puli                     },
19206c51eab1SEd Tanous                     "xyz.openbmc_project.User.Manager",
19216c51eab1SEd Tanous                     "/xyz/openbmc_project/user",
192284e12cb7SAppaRao Puli                     "xyz.openbmc_project.User.Manager", "RenameUser", username,
192384e12cb7SAppaRao Puli                     *newUserName);
19246c51eab1SEd Tanous             });
192584e12cb7SAppaRao Puli 
19266c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
1927ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
19286c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
19296c51eab1SEd Tanous             [](const crow::Request& /*req*/,
19306c51eab1SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19316c51eab1SEd Tanous                const std::string& username) -> void {
1932b477fd44SP Dheeraj Srujan Kumar                 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1933b477fd44SP Dheeraj Srujan Kumar                 tempObjPath /= username;
1934b477fd44SP Dheeraj Srujan Kumar                 const std::string userPath(tempObjPath);
193524c8542dSRatan Gupta 
19366c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
19376c51eab1SEd Tanous                     [asyncResp, username](const boost::system::error_code ec) {
19386c51eab1SEd Tanous                         if (ec)
193966b5ca76Sjayaprakash Mutyala                         {
194066b5ca76Sjayaprakash Mutyala                             messages::resourceNotFound(
194166b5ca76Sjayaprakash Mutyala                                 asyncResp->res,
19426c51eab1SEd Tanous                                 "#ManagerAccount.v1_4_0.ManagerAccount",
1943f12894f8SJason M. Bills                                 username);
194406e086d9SEd Tanous                             return;
194506e086d9SEd Tanous                         }
194606e086d9SEd Tanous 
1947f12894f8SJason M. Bills                         messages::accountRemoved(asyncResp->res);
194806e086d9SEd Tanous                     },
194906e086d9SEd Tanous                     "xyz.openbmc_project.User.Manager", userPath,
195006e086d9SEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
19516c51eab1SEd Tanous             });
195206e086d9SEd Tanous }
195388d16c9aSLewanczyk, Dawid 
195488d16c9aSLewanczyk, Dawid } // namespace redfish
1955