xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 3c5a376e005d97f5d586c5aa163267eb3b0381aa)
188d16c9aSLewanczyk, Dawid /*
288d16c9aSLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
388d16c9aSLewanczyk, Dawid //
488d16c9aSLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
588d16c9aSLewanczyk, Dawid // you may not use this file except in compliance with the License.
688d16c9aSLewanczyk, Dawid // You may obtain a copy of the License at
788d16c9aSLewanczyk, Dawid //
888d16c9aSLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
988d16c9aSLewanczyk, Dawid //
1088d16c9aSLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1188d16c9aSLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1288d16c9aSLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388d16c9aSLewanczyk, Dawid // See the License for the specific language governing permissions and
1488d16c9aSLewanczyk, Dawid // limitations under the License.
1588d16c9aSLewanczyk, Dawid */
1688d16c9aSLewanczyk, Dawid #pragma once
1788d16c9aSLewanczyk, Dawid #include "node.hpp"
1888d16c9aSLewanczyk, Dawid 
1924c8542dSRatan Gupta #include <dbus_utility.hpp>
2065b0dc32SEd Tanous #include <error_messages.hpp>
21b9b2e0b2SEd Tanous #include <openbmc_dbus_rest.hpp>
22a840879dSEd Tanous #include <utils/json_utils.hpp>
23abf2add6SEd Tanous #include <variant>
24b9b2e0b2SEd Tanous 
251abe55efSEd Tanous namespace redfish
261abe55efSEd Tanous {
2788d16c9aSLewanczyk, Dawid 
286973a582SRatan Gupta constexpr const char* ldapConfigObject =
296973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
30ab828d7cSRatan Gupta constexpr const char* ADConfigObject =
31ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
32ab828d7cSRatan Gupta 
336973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
346973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
356973a582SRatan Gupta constexpr const char* ldapConfigInterface =
366973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
376973a582SRatan Gupta constexpr const char* ldapCreateInterface =
386973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
396973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
4006785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
4106785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
426973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
436973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
446973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
456973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
466973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
476973a582SRatan Gupta 
4854fc587aSNagaraju Goruganti struct LDAPRoleMapData
4954fc587aSNagaraju Goruganti {
5054fc587aSNagaraju Goruganti     std::string groupName;
5154fc587aSNagaraju Goruganti     std::string privilege;
5254fc587aSNagaraju Goruganti };
5354fc587aSNagaraju Goruganti 
546973a582SRatan Gupta struct LDAPConfigData
556973a582SRatan Gupta {
566973a582SRatan Gupta     std::string uri{};
576973a582SRatan Gupta     std::string bindDN{};
586973a582SRatan Gupta     std::string baseDN{};
596973a582SRatan Gupta     std::string searchScope{};
606973a582SRatan Gupta     std::string serverType{};
616973a582SRatan Gupta     bool serviceEnabled = false;
626973a582SRatan Gupta     std::string userNameAttribute{};
636973a582SRatan Gupta     std::string groupAttribute{};
6454fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
656973a582SRatan Gupta };
666973a582SRatan Gupta 
67107077deSPrzemyslaw Czarnowski using DbusVariantType = sdbusplus::message::variant<bool, int32_t, std::string>;
68107077deSPrzemyslaw Czarnowski 
69107077deSPrzemyslaw Czarnowski using DbusInterfaceType = boost::container::flat_map<
70107077deSPrzemyslaw Czarnowski     std::string, boost::container::flat_map<std::string, DbusVariantType>>;
71107077deSPrzemyslaw Czarnowski 
72107077deSPrzemyslaw Czarnowski using ManagedObjectType =
73107077deSPrzemyslaw Czarnowski     std::vector<std::pair<sdbusplus::message::object_path, DbusInterfaceType>>;
74107077deSPrzemyslaw Czarnowski 
756973a582SRatan Gupta using GetObjectType =
766973a582SRatan Gupta     std::vector<std::pair<std::string, std::vector<std::string>>>;
7784e12cb7SAppaRao Puli 
7854fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7984e12cb7SAppaRao Puli {
8084e12cb7SAppaRao Puli     if (role == "priv-admin")
8184e12cb7SAppaRao Puli     {
8284e12cb7SAppaRao Puli         return "Administrator";
8384e12cb7SAppaRao Puli     }
8484e12cb7SAppaRao Puli     else if (role == "priv-user")
8584e12cb7SAppaRao Puli     {
86c80fee55SAppaRao Puli         return "ReadOnly";
8784e12cb7SAppaRao Puli     }
8884e12cb7SAppaRao Puli     else if (role == "priv-operator")
8984e12cb7SAppaRao Puli     {
9084e12cb7SAppaRao Puli         return "Operator";
9184e12cb7SAppaRao Puli     }
92e9e6d240Sjayaprakash Mutyala     else if ((role == "") || (role == "priv-noaccess"))
93e9e6d240Sjayaprakash Mutyala     {
94e9e6d240Sjayaprakash Mutyala         return "NoAccess";
95e9e6d240Sjayaprakash Mutyala     }
9684e12cb7SAppaRao Puli     return "";
9784e12cb7SAppaRao Puli }
9854fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9984e12cb7SAppaRao Puli {
10084e12cb7SAppaRao Puli     if (role == "Administrator")
10184e12cb7SAppaRao Puli     {
10284e12cb7SAppaRao Puli         return "priv-admin";
10384e12cb7SAppaRao Puli     }
104c80fee55SAppaRao Puli     else if (role == "ReadOnly")
10584e12cb7SAppaRao Puli     {
10684e12cb7SAppaRao Puli         return "priv-user";
10784e12cb7SAppaRao Puli     }
10884e12cb7SAppaRao Puli     else if (role == "Operator")
10984e12cb7SAppaRao Puli     {
11084e12cb7SAppaRao Puli         return "priv-operator";
11184e12cb7SAppaRao Puli     }
112e9e6d240Sjayaprakash Mutyala     else if (role == "NoAccess")
113e9e6d240Sjayaprakash Mutyala     {
114e9e6d240Sjayaprakash Mutyala         return "priv-noaccess";
115e9e6d240Sjayaprakash Mutyala     }
11684e12cb7SAppaRao Puli     return "";
11784e12cb7SAppaRao Puli }
118b9b2e0b2SEd Tanous 
11966b5ca76Sjayaprakash Mutyala void userErrorMessageHandler(const sd_bus_error* e,
12066b5ca76Sjayaprakash Mutyala                              std::shared_ptr<AsyncResp> asyncResp,
12166b5ca76Sjayaprakash Mutyala                              const std::string& newUser,
12266b5ca76Sjayaprakash Mutyala                              const std::string& username)
12366b5ca76Sjayaprakash Mutyala {
12466b5ca76Sjayaprakash Mutyala     const char* errorMessage = e->name;
12566b5ca76Sjayaprakash Mutyala     if (e == nullptr)
12666b5ca76Sjayaprakash Mutyala     {
12766b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
12866b5ca76Sjayaprakash Mutyala         return;
12966b5ca76Sjayaprakash Mutyala     }
13066b5ca76Sjayaprakash Mutyala 
13166b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
13266b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
13366b5ca76Sjayaprakash Mutyala     {
13466b5ca76Sjayaprakash Mutyala         messages::resourceAlreadyExists(asyncResp->res,
13566b5ca76Sjayaprakash Mutyala                                         "#ManagerAccount.v1_0_3.ManagerAccount",
13666b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
13766b5ca76Sjayaprakash Mutyala     }
13866b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
13966b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
14066b5ca76Sjayaprakash Mutyala     {
14166b5ca76Sjayaprakash Mutyala         messages::resourceNotFound(
14266b5ca76Sjayaprakash Mutyala             asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", username);
14366b5ca76Sjayaprakash Mutyala     }
14466b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
14566b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
14666b5ca76Sjayaprakash Mutyala     {
14766b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
14866b5ca76Sjayaprakash Mutyala     }
14966b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
15066b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
15166b5ca76Sjayaprakash Mutyala     {
15266b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
15366b5ca76Sjayaprakash Mutyala     }
15466b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
15566b5ca76Sjayaprakash Mutyala                                   "UserNameGroupFail") == 0)
15666b5ca76Sjayaprakash Mutyala     {
15766b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
15866b5ca76Sjayaprakash Mutyala     }
15966b5ca76Sjayaprakash Mutyala     else
16066b5ca76Sjayaprakash Mutyala     {
16166b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
16266b5ca76Sjayaprakash Mutyala     }
16366b5ca76Sjayaprakash Mutyala 
16466b5ca76Sjayaprakash Mutyala     return;
16566b5ca76Sjayaprakash Mutyala }
16666b5ca76Sjayaprakash Mutyala 
1676973a582SRatan Gupta void parseLDAPConfigData(nlohmann::json& json_response,
168ab828d7cSRatan Gupta                          const LDAPConfigData& confData,
169ab828d7cSRatan Gupta                          const std::string& ldapType)
1706973a582SRatan Gupta {
171ab828d7cSRatan Gupta     std::string service =
172ab828d7cSRatan Gupta         (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService";
17337cce918SMarri Devender Rao     nlohmann::json ldap = {
1746973a582SRatan Gupta         {"AccountProviderType", service},
1756973a582SRatan Gupta         {"ServiceEnabled", confData.serviceEnabled},
1766973a582SRatan Gupta         {"ServiceAddresses", nlohmann::json::array({confData.uri})},
1776973a582SRatan Gupta         {"Authentication",
1786973a582SRatan Gupta          {{"AuthenticationType", "UsernameAndPassword"},
1796973a582SRatan Gupta           {"Username", confData.bindDN},
1806973a582SRatan Gupta           {"Password", nullptr}}},
1816973a582SRatan Gupta         {"LDAPService",
1826973a582SRatan Gupta          {{"SearchSettings",
1836973a582SRatan Gupta            {{"BaseDistinguishedNames",
1846973a582SRatan Gupta              nlohmann::json::array({confData.baseDN})},
1856973a582SRatan Gupta             {"UsernameAttribute", confData.userNameAttribute},
1866973a582SRatan Gupta             {"GroupsAttribute", confData.groupAttribute}}}}},
1876973a582SRatan Gupta     };
18854fc587aSNagaraju Goruganti 
18937cce918SMarri Devender Rao     json_response[ldapType].update(std::move(ldap));
19054fc587aSNagaraju Goruganti 
19154fc587aSNagaraju Goruganti     nlohmann::json& roleMapArray = json_response[ldapType]["RemoteRoleMapping"];
19254fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
19354fc587aSNagaraju Goruganti     for (auto& obj : confData.groupRoleList)
19454fc587aSNagaraju Goruganti     {
19554fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
19654fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
19754fc587aSNagaraju Goruganti         roleMapArray.push_back(
19854fc587aSNagaraju Goruganti             {nlohmann::json::array({"RemoteGroup", obj.second.groupName}),
19954fc587aSNagaraju Goruganti              nlohmann::json::array(
20054fc587aSNagaraju Goruganti                  {"LocalRole", getRoleIdFromPrivilege(obj.second.privilege)})});
20154fc587aSNagaraju Goruganti     }
2026973a582SRatan Gupta }
2036973a582SRatan Gupta 
2046973a582SRatan Gupta /**
20506785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
20606785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
20706785244SRatan Gupta  *
20806785244SRatan Gupta  */
20906785244SRatan Gupta static void handleRoleMapPatch(
21006785244SRatan Gupta     const std::shared_ptr<AsyncResp>& asyncResp,
21106785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
21206785244SRatan Gupta     const std::string& serverType, std::vector<nlohmann::json>& input)
21306785244SRatan Gupta {
21406785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
21506785244SRatan Gupta     {
21606785244SRatan Gupta         nlohmann::json& thisJson = input[index];
21706785244SRatan Gupta 
21806785244SRatan Gupta         if (thisJson.is_null())
21906785244SRatan Gupta         {
22006785244SRatan Gupta             // delete the existing object
22106785244SRatan Gupta             if (index < roleMapObjData.size())
22206785244SRatan Gupta             {
22306785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
22406785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
22506785244SRatan Gupta                      index](const boost::system::error_code ec) {
22606785244SRatan Gupta                         if (ec)
22706785244SRatan Gupta                         {
22806785244SRatan Gupta                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
22906785244SRatan Gupta                             messages::internalError(asyncResp->res);
23006785244SRatan Gupta                             return;
23106785244SRatan Gupta                         }
23206785244SRatan Gupta                         asyncResp->res
23306785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"][index] =
23406785244SRatan Gupta                             nullptr;
23506785244SRatan Gupta                     },
23606785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
23706785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
23806785244SRatan Gupta             }
23906785244SRatan Gupta             else
24006785244SRatan Gupta             {
24106785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
24206785244SRatan Gupta                 messages::propertyValueTypeError(
24306785244SRatan Gupta                     asyncResp->res, thisJson.dump(),
24406785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
24506785244SRatan Gupta                 return;
24606785244SRatan Gupta             }
24706785244SRatan Gupta         }
24806785244SRatan Gupta         else if (thisJson.empty())
24906785244SRatan Gupta         {
25006785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
25106785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
25206785244SRatan Gupta         }
25306785244SRatan Gupta         else
25406785244SRatan Gupta         {
25506785244SRatan Gupta             // update/create the object
25606785244SRatan Gupta             std::optional<std::string> remoteGroup;
25706785244SRatan Gupta             std::optional<std::string> localRole;
25806785244SRatan Gupta 
25906785244SRatan Gupta             if (!json_util::readJson(thisJson, asyncResp->res, "RemoteGroup",
26006785244SRatan Gupta                                      remoteGroup, "LocalRole", localRole))
26106785244SRatan Gupta             {
26206785244SRatan Gupta                 continue;
26306785244SRatan Gupta             }
26406785244SRatan Gupta 
26506785244SRatan Gupta             // Update existing RoleMapping Object
26606785244SRatan Gupta             if (index < roleMapObjData.size())
26706785244SRatan Gupta             {
26806785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
26906785244SRatan Gupta                 // If "RemoteGroup" info is provided
27006785244SRatan Gupta                 if (remoteGroup)
27106785244SRatan Gupta                 {
27206785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
27306785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
27406785244SRatan Gupta                          remoteGroup](const boost::system::error_code ec) {
27506785244SRatan Gupta                             if (ec)
27606785244SRatan Gupta                             {
27706785244SRatan Gupta                                 BMCWEB_LOG_ERROR << "DBUS response error: "
27806785244SRatan Gupta                                                  << ec;
27906785244SRatan Gupta                                 messages::internalError(asyncResp->res);
28006785244SRatan Gupta                                 return;
28106785244SRatan Gupta                             }
28206785244SRatan Gupta                             asyncResp->res
28306785244SRatan Gupta                                 .jsonValue[serverType]["RemoteRoleMapping"]
28406785244SRatan Gupta                                           [index]["RemoteGroup"] = *remoteGroup;
28506785244SRatan Gupta                         },
28606785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
28706785244SRatan Gupta                         propertyInterface, "Set",
28806785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
28906785244SRatan Gupta                         "GroupName",
29006785244SRatan Gupta                         std::variant<std::string>(std::move(*remoteGroup)));
29106785244SRatan Gupta                 }
29206785244SRatan Gupta 
29306785244SRatan Gupta                 // If "LocalRole" info is provided
29406785244SRatan Gupta                 if (localRole)
29506785244SRatan Gupta                 {
29606785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
29706785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
29806785244SRatan Gupta                          localRole](const boost::system::error_code ec) {
29906785244SRatan Gupta                             if (ec)
30006785244SRatan Gupta                             {
30106785244SRatan Gupta                                 BMCWEB_LOG_ERROR << "DBUS response error: "
30206785244SRatan Gupta                                                  << ec;
30306785244SRatan Gupta                                 messages::internalError(asyncResp->res);
30406785244SRatan Gupta                                 return;
30506785244SRatan Gupta                             }
30606785244SRatan Gupta                             asyncResp->res
30706785244SRatan Gupta                                 .jsonValue[serverType]["RemoteRoleMapping"]
30806785244SRatan Gupta                                           [index]["LocalRole"] = *localRole;
30906785244SRatan Gupta                         },
31006785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
31106785244SRatan Gupta                         propertyInterface, "Set",
31206785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
31306785244SRatan Gupta                         "Privilege",
31406785244SRatan Gupta                         std::variant<std::string>(
31506785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
31606785244SRatan Gupta                 }
31706785244SRatan Gupta             }
31806785244SRatan Gupta             // Create a new RoleMapping Object.
31906785244SRatan Gupta             else
32006785244SRatan Gupta             {
32106785244SRatan Gupta                 BMCWEB_LOG_DEBUG
32206785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
32306785244SRatan Gupta                 std::string pathString =
32406785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index);
32506785244SRatan Gupta 
32606785244SRatan Gupta                 if (!localRole)
32706785244SRatan Gupta                 {
32806785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
32906785244SRatan Gupta                                               pathString + "/LocalRole");
33006785244SRatan Gupta                     continue;
33106785244SRatan Gupta                 }
33206785244SRatan Gupta                 if (!remoteGroup)
33306785244SRatan Gupta                 {
33406785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
33506785244SRatan Gupta                                               pathString + "/RemoteGroup");
33606785244SRatan Gupta                     continue;
33706785244SRatan Gupta                 }
33806785244SRatan Gupta 
33906785244SRatan Gupta                 std::string dbusObjectPath;
34006785244SRatan Gupta                 if (serverType == "ActiveDirectory")
34106785244SRatan Gupta                 {
34206785244SRatan Gupta                     dbusObjectPath = ADConfigObject;
34306785244SRatan Gupta                 }
34406785244SRatan Gupta                 else if (serverType == "LDAP")
34506785244SRatan Gupta                 {
34606785244SRatan Gupta                     dbusObjectPath = ldapConfigObject;
34706785244SRatan Gupta                 }
34806785244SRatan Gupta 
34906785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
35006785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
35106785244SRatan Gupta 
35206785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
353271584abSEd Tanous                     [asyncResp, serverType, localRole,
35406785244SRatan Gupta                      remoteGroup](const boost::system::error_code ec) {
35506785244SRatan Gupta                         if (ec)
35606785244SRatan Gupta                         {
35706785244SRatan Gupta                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
35806785244SRatan Gupta                             messages::internalError(asyncResp->res);
35906785244SRatan Gupta                             return;
36006785244SRatan Gupta                         }
36106785244SRatan Gupta                         nlohmann::json& remoteRoleJson =
36206785244SRatan Gupta                             asyncResp->res
36306785244SRatan Gupta                                 .jsonValue[serverType]["RemoteRoleMapping"];
36406785244SRatan Gupta                         remoteRoleJson.push_back(
36506785244SRatan Gupta                             {{"LocalRole", *localRole},
36606785244SRatan Gupta                              {"RemoteGroup", *remoteGroup}});
36706785244SRatan Gupta                     },
36806785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
36906785244SRatan Gupta                     "Create", std::move(*remoteGroup),
37006785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
37106785244SRatan Gupta             }
37206785244SRatan Gupta         }
37306785244SRatan Gupta     }
37406785244SRatan Gupta }
37506785244SRatan Gupta 
37606785244SRatan Gupta /**
3776973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
3786973a582SRatan Gupta  * into JSON
3796973a582SRatan Gupta  */
3806973a582SRatan Gupta template <typename CallbackFunc>
3816973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
3826973a582SRatan Gupta                               CallbackFunc&& callback)
3836973a582SRatan Gupta {
38454fc587aSNagaraju Goruganti 
38554fc587aSNagaraju Goruganti     const std::array<const char*, 2> interfaces = {ldapEnableInterface,
38654fc587aSNagaraju Goruganti                                                    ldapConfigInterface};
38754fc587aSNagaraju Goruganti 
38854fc587aSNagaraju Goruganti     crow::connections::systemBus->async_method_call(
38954fc587aSNagaraju Goruganti         [callback, ldapType](const boost::system::error_code ec,
39054fc587aSNagaraju Goruganti                              const GetObjectType& resp) {
39154fc587aSNagaraju Goruganti             LDAPConfigData confData{};
39254fc587aSNagaraju Goruganti             if (ec || resp.empty())
39354fc587aSNagaraju Goruganti             {
39454fc587aSNagaraju Goruganti                 BMCWEB_LOG_ERROR << "DBUS response error during getting of "
39554fc587aSNagaraju Goruganti                                     "service name: "
39654fc587aSNagaraju Goruganti                                  << ec;
39754fc587aSNagaraju Goruganti                 callback(false, confData, ldapType);
39854fc587aSNagaraju Goruganti                 return;
39954fc587aSNagaraju Goruganti             }
40054fc587aSNagaraju Goruganti             std::string service = resp.begin()->first;
40154fc587aSNagaraju Goruganti             crow::connections::systemBus->async_method_call(
40254fc587aSNagaraju Goruganti                 [callback, ldapType](const boost::system::error_code error_code,
4036973a582SRatan Gupta                                      const ManagedObjectType& ldapObjects) {
4046973a582SRatan Gupta                     LDAPConfigData confData{};
4056973a582SRatan Gupta                     if (error_code)
4066973a582SRatan Gupta                     {
407ab828d7cSRatan Gupta                         callback(false, confData, ldapType);
40854fc587aSNagaraju Goruganti                         BMCWEB_LOG_ERROR << "D-Bus responses error: "
40954fc587aSNagaraju Goruganti                                          << error_code;
4106973a582SRatan Gupta                         return;
4116973a582SRatan Gupta                     }
412ab828d7cSRatan Gupta 
413ab828d7cSRatan Gupta                     std::string ldapDbusType;
41454fc587aSNagaraju Goruganti                     std::string searchString;
41554fc587aSNagaraju Goruganti 
416ab828d7cSRatan Gupta                     if (ldapType == "LDAP")
417ab828d7cSRatan Gupta                     {
41854fc587aSNagaraju Goruganti                         ldapDbusType = "xyz.openbmc_project.User.Ldap.Config."
41954fc587aSNagaraju Goruganti                                        "Type.OpenLdap";
42054fc587aSNagaraju Goruganti                         searchString = "openldap";
421ab828d7cSRatan Gupta                     }
422ab828d7cSRatan Gupta                     else if (ldapType == "ActiveDirectory")
423ab828d7cSRatan Gupta                     {
42454fc587aSNagaraju Goruganti                         ldapDbusType =
42554fc587aSNagaraju Goruganti                             "xyz.openbmc_project.User.Ldap.Config.Type."
426ab828d7cSRatan Gupta                             "ActiveDirectory";
42754fc587aSNagaraju Goruganti                         searchString = "active_directory";
428ab828d7cSRatan Gupta                     }
429ab828d7cSRatan Gupta                     else
430ab828d7cSRatan Gupta                     {
43154fc587aSNagaraju Goruganti                         BMCWEB_LOG_ERROR
43254fc587aSNagaraju Goruganti                             << "Can't get the DbusType for the given type="
433ab828d7cSRatan Gupta                             << ldapType;
434ab828d7cSRatan Gupta                         callback(false, confData, ldapType);
435ab828d7cSRatan Gupta                         return;
436ab828d7cSRatan Gupta                     }
437ab828d7cSRatan Gupta 
438ab828d7cSRatan Gupta                     std::string ldapEnableInterfaceStr = ldapEnableInterface;
439ab828d7cSRatan Gupta                     std::string ldapConfigInterfaceStr = ldapConfigInterface;
440ab828d7cSRatan Gupta 
4416973a582SRatan Gupta                     for (const auto& object : ldapObjects)
4426973a582SRatan Gupta                     {
44354fc587aSNagaraju Goruganti                         // let's find the object whose ldap type is equal to the
44454fc587aSNagaraju Goruganti                         // given type
44554fc587aSNagaraju Goruganti                         if (object.first.str.find(searchString) ==
44654fc587aSNagaraju Goruganti                             std::string::npos)
4476973a582SRatan Gupta                         {
448ab828d7cSRatan Gupta                             continue;
449ab828d7cSRatan Gupta                         }
450ab828d7cSRatan Gupta 
4516973a582SRatan Gupta                         for (const auto& interface : object.second)
4526973a582SRatan Gupta                         {
4536973a582SRatan Gupta                             if (interface.first == ldapEnableInterfaceStr)
4546973a582SRatan Gupta                             {
4556973a582SRatan Gupta                                 // rest of the properties are string.
4566973a582SRatan Gupta                                 for (const auto& property : interface.second)
4576973a582SRatan Gupta                                 {
4586973a582SRatan Gupta                                     if (property.first == "Enabled")
4596973a582SRatan Gupta                                     {
4606973a582SRatan Gupta                                         const bool* value =
4616973a582SRatan Gupta                                             std::get_if<bool>(&property.second);
4626973a582SRatan Gupta                                         if (value == nullptr)
4636973a582SRatan Gupta                                         {
4646973a582SRatan Gupta                                             continue;
4656973a582SRatan Gupta                                         }
4666973a582SRatan Gupta                                         confData.serviceEnabled = *value;
4676973a582SRatan Gupta                                         break;
4686973a582SRatan Gupta                                     }
4696973a582SRatan Gupta                                 }
4706973a582SRatan Gupta                             }
4716973a582SRatan Gupta                             else if (interface.first == ldapConfigInterfaceStr)
4726973a582SRatan Gupta                             {
4736973a582SRatan Gupta 
4746973a582SRatan Gupta                                 for (const auto& property : interface.second)
4756973a582SRatan Gupta                                 {
476271584abSEd Tanous                                     const std::string* strValue =
47754fc587aSNagaraju Goruganti                                         std::get_if<std::string>(
47854fc587aSNagaraju Goruganti                                             &property.second);
479271584abSEd Tanous                                     if (strValue == nullptr)
4806973a582SRatan Gupta                                     {
4816973a582SRatan Gupta                                         continue;
4826973a582SRatan Gupta                                     }
4836973a582SRatan Gupta                                     if (property.first == "LDAPServerURI")
4846973a582SRatan Gupta                                     {
485271584abSEd Tanous                                         confData.uri = *strValue;
4866973a582SRatan Gupta                                     }
4876973a582SRatan Gupta                                     else if (property.first == "LDAPBindDN")
4886973a582SRatan Gupta                                     {
489271584abSEd Tanous                                         confData.bindDN = *strValue;
4906973a582SRatan Gupta                                     }
4916973a582SRatan Gupta                                     else if (property.first == "LDAPBaseDN")
4926973a582SRatan Gupta                                     {
493271584abSEd Tanous                                         confData.baseDN = *strValue;
4946973a582SRatan Gupta                                     }
49554fc587aSNagaraju Goruganti                                     else if (property.first ==
49654fc587aSNagaraju Goruganti                                              "LDAPSearchScope")
4976973a582SRatan Gupta                                     {
498271584abSEd Tanous                                         confData.searchScope = *strValue;
4996973a582SRatan Gupta                                     }
50054fc587aSNagaraju Goruganti                                     else if (property.first ==
50154fc587aSNagaraju Goruganti                                              "GroupNameAttribute")
5026973a582SRatan Gupta                                     {
503271584abSEd Tanous                                         confData.groupAttribute = *strValue;
5046973a582SRatan Gupta                                     }
50554fc587aSNagaraju Goruganti                                     else if (property.first ==
50654fc587aSNagaraju Goruganti                                              "UserNameAttribute")
5076973a582SRatan Gupta                                     {
508271584abSEd Tanous                                         confData.userNameAttribute = *strValue;
5096973a582SRatan Gupta                                     }
51054fc587aSNagaraju Goruganti                                     else if (property.first == "LDAPType")
511ab828d7cSRatan Gupta                                     {
512271584abSEd Tanous                                         confData.serverType = *strValue;
51354fc587aSNagaraju Goruganti                                     }
51454fc587aSNagaraju Goruganti                                 }
51554fc587aSNagaraju Goruganti                             }
51654fc587aSNagaraju Goruganti                             else if (interface.first ==
51754fc587aSNagaraju Goruganti                                      "xyz.openbmc_project.User."
51854fc587aSNagaraju Goruganti                                      "PrivilegeMapperEntry")
51954fc587aSNagaraju Goruganti                             {
52054fc587aSNagaraju Goruganti                                 LDAPRoleMapData roleMapData{};
52154fc587aSNagaraju Goruganti                                 for (const auto& property : interface.second)
52254fc587aSNagaraju Goruganti                                 {
523271584abSEd Tanous                                     const std::string* strValue =
52454fc587aSNagaraju Goruganti                                         std::get_if<std::string>(
52554fc587aSNagaraju Goruganti                                             &property.second);
52654fc587aSNagaraju Goruganti 
527271584abSEd Tanous                                     if (strValue == nullptr)
52854fc587aSNagaraju Goruganti                                     {
52954fc587aSNagaraju Goruganti                                         continue;
53054fc587aSNagaraju Goruganti                                     }
53154fc587aSNagaraju Goruganti 
53254fc587aSNagaraju Goruganti                                     if (property.first == "GroupName")
53354fc587aSNagaraju Goruganti                                     {
534271584abSEd Tanous                                         roleMapData.groupName = *strValue;
53554fc587aSNagaraju Goruganti                                     }
53654fc587aSNagaraju Goruganti                                     else if (property.first == "Privilege")
53754fc587aSNagaraju Goruganti                                     {
538271584abSEd Tanous                                         roleMapData.privilege = *strValue;
53954fc587aSNagaraju Goruganti                                     }
54054fc587aSNagaraju Goruganti                                 }
54154fc587aSNagaraju Goruganti 
5420f0353b6SEd Tanous                                 confData.groupRoleList.emplace_back(
5430f0353b6SEd Tanous                                     object.first.str, roleMapData);
54454fc587aSNagaraju Goruganti                             }
54554fc587aSNagaraju Goruganti                         }
54654fc587aSNagaraju Goruganti                     }
547ab828d7cSRatan Gupta                     callback(true, confData, ldapType);
54854fc587aSNagaraju Goruganti                 },
54954fc587aSNagaraju Goruganti                 service, ldapRootObject, dbusObjManagerIntf,
5506973a582SRatan Gupta                 "GetManagedObjects");
55154fc587aSNagaraju Goruganti         },
55254fc587aSNagaraju Goruganti         mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
55354fc587aSNagaraju Goruganti         ldapConfigObject, interfaces);
5546973a582SRatan Gupta }
5556973a582SRatan Gupta 
5561abe55efSEd Tanous class AccountService : public Node
5571abe55efSEd Tanous {
55888d16c9aSLewanczyk, Dawid   public:
55978158631SZbigniew Kurzynski     AccountService(CrowApp& app) :
56078158631SZbigniew Kurzynski         Node(app, "/redfish/v1/AccountService/"), app(app)
5611abe55efSEd Tanous     {
5623ebd75f7SEd Tanous         entityPrivileges = {
563*3c5a376eSGunnar Mills             {boost::beast::http::verb::get, {{"Login"}}},
564e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
565e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
566e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
567e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
568e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
56988d16c9aSLewanczyk, Dawid     }
57088d16c9aSLewanczyk, Dawid 
57188d16c9aSLewanczyk, Dawid   private:
5728a07d286SRatan Gupta     /**
5738a07d286SRatan Gupta      * @brief parses the authentication section under the LDAP
5748a07d286SRatan Gupta      * @param input JSON data
5758a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
5768a07d286SRatan Gupta      * @param userName  userName to be filled from the given JSON.
5778a07d286SRatan Gupta      * @param password  password to be filled from the given JSON.
5788a07d286SRatan Gupta      */
5798a07d286SRatan Gupta     void
5808a07d286SRatan Gupta         parseLDAPAuthenticationJson(nlohmann::json input,
5818a07d286SRatan Gupta                                     const std::shared_ptr<AsyncResp>& asyncResp,
5828a07d286SRatan Gupta                                     std::optional<std::string>& username,
5838a07d286SRatan Gupta                                     std::optional<std::string>& password)
5848a07d286SRatan Gupta     {
5858a07d286SRatan Gupta         std::optional<std::string> authType;
5868a07d286SRatan Gupta 
5878a07d286SRatan Gupta         if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
5888a07d286SRatan Gupta                                  authType, "Username", username, "Password",
5898a07d286SRatan Gupta                                  password))
5908a07d286SRatan Gupta         {
5918a07d286SRatan Gupta             return;
5928a07d286SRatan Gupta         }
5938a07d286SRatan Gupta         if (!authType)
5948a07d286SRatan Gupta         {
5958a07d286SRatan Gupta             return;
5968a07d286SRatan Gupta         }
5978a07d286SRatan Gupta         if (*authType != "UsernameAndPassword")
5988a07d286SRatan Gupta         {
5998a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, *authType,
6008a07d286SRatan Gupta                                              "AuthenticationType");
6018a07d286SRatan Gupta             return;
6028a07d286SRatan Gupta         }
6038a07d286SRatan Gupta     }
6048a07d286SRatan Gupta     /**
6058a07d286SRatan Gupta      * @brief parses the LDAPService section under the LDAP
6068a07d286SRatan Gupta      * @param input JSON data
6078a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
6088a07d286SRatan Gupta      * @param baseDNList baseDN to be filled from the given JSON.
6098a07d286SRatan Gupta      * @param userNameAttribute  userName to be filled from the given JSON.
6108a07d286SRatan Gupta      * @param groupaAttribute  password to be filled from the given JSON.
6118a07d286SRatan Gupta      */
6128a07d286SRatan Gupta 
6138a07d286SRatan Gupta     void parseLDAPServiceJson(
6148a07d286SRatan Gupta         nlohmann::json input, const std::shared_ptr<AsyncResp>& asyncResp,
6158a07d286SRatan Gupta         std::optional<std::vector<std::string>>& baseDNList,
6168a07d286SRatan Gupta         std::optional<std::string>& userNameAttribute,
6178a07d286SRatan Gupta         std::optional<std::string>& groupsAttribute)
6188a07d286SRatan Gupta     {
6198a07d286SRatan Gupta         std::optional<nlohmann::json> searchSettings;
6208a07d286SRatan Gupta 
6218a07d286SRatan Gupta         if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
6228a07d286SRatan Gupta                                  searchSettings))
6238a07d286SRatan Gupta         {
6248a07d286SRatan Gupta             return;
6258a07d286SRatan Gupta         }
6268a07d286SRatan Gupta         if (!searchSettings)
6278a07d286SRatan Gupta         {
6288a07d286SRatan Gupta             return;
6298a07d286SRatan Gupta         }
6308a07d286SRatan Gupta         if (!json_util::readJson(*searchSettings, asyncResp->res,
6318a07d286SRatan Gupta                                  "BaseDistinguishedNames", baseDNList,
6328a07d286SRatan Gupta                                  "UsernameAttribute", userNameAttribute,
6338a07d286SRatan Gupta                                  "GroupsAttribute", groupsAttribute))
6348a07d286SRatan Gupta         {
6358a07d286SRatan Gupta             return;
6368a07d286SRatan Gupta         }
6378a07d286SRatan Gupta     }
6388a07d286SRatan Gupta     /**
6398a07d286SRatan Gupta      * @brief updates the LDAP server address and updates the
6408a07d286SRatan Gupta               json response with the new value.
6418a07d286SRatan Gupta      * @param serviceAddressList address to be updated.
6428a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
6438a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
6448a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
6458a07d286SRatan Gupta      */
6468a07d286SRatan Gupta 
6478a07d286SRatan Gupta     void handleServiceAddressPatch(
6488a07d286SRatan Gupta         const std::vector<std::string>& serviceAddressList,
6498a07d286SRatan Gupta         const std::shared_ptr<AsyncResp>& asyncResp,
6508a07d286SRatan Gupta         const std::string& ldapServerElementName,
6518a07d286SRatan Gupta         const std::string& ldapConfigObject)
6528a07d286SRatan Gupta     {
6538a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
6548a07d286SRatan Gupta             [asyncResp, ldapServerElementName,
6558a07d286SRatan Gupta              serviceAddressList](const boost::system::error_code ec) {
6568a07d286SRatan Gupta                 if (ec)
6578a07d286SRatan Gupta                 {
6588a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG
6598a07d286SRatan Gupta                         << "Error Occured in updating the service address";
6608a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
6618a07d286SRatan Gupta                     return;
6628a07d286SRatan Gupta                 }
6638a07d286SRatan Gupta                 std::vector<std::string> modifiedserviceAddressList = {
6648a07d286SRatan Gupta                     serviceAddressList.front()};
6658a07d286SRatan Gupta                 asyncResp->res
6668a07d286SRatan Gupta                     .jsonValue[ldapServerElementName]["ServiceAddresses"] =
6678a07d286SRatan Gupta                     modifiedserviceAddressList;
6688a07d286SRatan Gupta                 if ((serviceAddressList).size() > 1)
6698a07d286SRatan Gupta                 {
6708a07d286SRatan Gupta                     messages::propertyValueModified(asyncResp->res,
6718a07d286SRatan Gupta                                                     "ServiceAddresses",
6728a07d286SRatan Gupta                                                     serviceAddressList.front());
6738a07d286SRatan Gupta                 }
6748a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the service address";
6758a07d286SRatan Gupta             },
6768a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6778a07d286SRatan Gupta             ldapConfigInterface, "LDAPServerURI",
6788a07d286SRatan Gupta             std::variant<std::string>(serviceAddressList.front()));
6798a07d286SRatan Gupta     }
6808a07d286SRatan Gupta     /**
6818a07d286SRatan Gupta      * @brief updates the LDAP Bind DN and updates the
6828a07d286SRatan Gupta               json response with the new value.
6838a07d286SRatan Gupta      * @param username name of the user which needs to be updated.
6848a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
6858a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
6868a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
6878a07d286SRatan Gupta      */
6888a07d286SRatan Gupta 
6898a07d286SRatan Gupta     void handleUserNamePatch(const std::string& username,
6908a07d286SRatan Gupta                              const std::shared_ptr<AsyncResp>& asyncResp,
6918a07d286SRatan Gupta                              const std::string& ldapServerElementName,
6928a07d286SRatan Gupta                              const std::string& ldapConfigObject)
6938a07d286SRatan Gupta     {
6948a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
6958a07d286SRatan Gupta             [asyncResp, username,
6968a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
6978a07d286SRatan Gupta                 if (ec)
6988a07d286SRatan Gupta                 {
6998a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG
7008a07d286SRatan Gupta                         << "Error occured in updating the username";
7018a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
7028a07d286SRatan Gupta                     return;
7038a07d286SRatan Gupta                 }
7048a07d286SRatan Gupta                 asyncResp->res.jsonValue[ldapServerElementName]
7058a07d286SRatan Gupta                                         ["Authentication"]["Username"] =
7068a07d286SRatan Gupta                     username;
7078a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the username";
7088a07d286SRatan Gupta             },
7098a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7108a07d286SRatan Gupta             ldapConfigInterface, "LDAPBindDN",
7118a07d286SRatan Gupta             std::variant<std::string>(username));
7128a07d286SRatan Gupta     }
7138a07d286SRatan Gupta 
7148a07d286SRatan Gupta     /**
7158a07d286SRatan Gupta      * @brief updates the LDAP password
7168a07d286SRatan Gupta      * @param password : ldap password which needs to be updated.
7178a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
7188a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
7198a07d286SRatan Gupta      *        server(openLDAP/ActiveDirectory)
7208a07d286SRatan Gupta      */
7218a07d286SRatan Gupta 
7228a07d286SRatan Gupta     void handlePasswordPatch(const std::string& password,
7238a07d286SRatan Gupta                              const std::shared_ptr<AsyncResp>& asyncResp,
7248a07d286SRatan Gupta                              const std::string& ldapServerElementName,
7258a07d286SRatan Gupta                              const std::string& ldapConfigObject)
7268a07d286SRatan Gupta     {
7278a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
7288a07d286SRatan Gupta             [asyncResp, password,
7298a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
7308a07d286SRatan Gupta                 if (ec)
7318a07d286SRatan Gupta                 {
7328a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG
7338a07d286SRatan Gupta                         << "Error occured in updating the password";
7348a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
7358a07d286SRatan Gupta                     return;
7368a07d286SRatan Gupta                 }
7378a07d286SRatan Gupta                 asyncResp->res.jsonValue[ldapServerElementName]
7388a07d286SRatan Gupta                                         ["Authentication"]["Password"] = "";
7398a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the password";
7408a07d286SRatan Gupta             },
7418a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7428a07d286SRatan Gupta             ldapConfigInterface, "LDAPBindDNPassword",
7438a07d286SRatan Gupta             std::variant<std::string>(password));
7448a07d286SRatan Gupta     }
7458a07d286SRatan Gupta 
7468a07d286SRatan Gupta     /**
7478a07d286SRatan Gupta      * @brief updates the LDAP BaseDN and updates the
7488a07d286SRatan Gupta               json response with the new value.
7498a07d286SRatan Gupta      * @param baseDNList baseDN list which needs to be updated.
7508a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
7518a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
7528a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
7538a07d286SRatan Gupta      */
7548a07d286SRatan Gupta 
7558a07d286SRatan Gupta     void handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7568a07d286SRatan Gupta                            const std::shared_ptr<AsyncResp>& asyncResp,
7578a07d286SRatan Gupta                            const std::string& ldapServerElementName,
7588a07d286SRatan Gupta                            const std::string& ldapConfigObject)
7598a07d286SRatan Gupta     {
7608a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
7618a07d286SRatan Gupta             [asyncResp, baseDNList,
7628a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
7638a07d286SRatan Gupta                 if (ec)
7648a07d286SRatan Gupta                 {
7658a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG << "Error Occured in Updating the base DN";
7668a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
7678a07d286SRatan Gupta                     return;
7688a07d286SRatan Gupta                 }
7698a07d286SRatan Gupta                 auto& serverTypeJson =
7708a07d286SRatan Gupta                     asyncResp->res.jsonValue[ldapServerElementName];
7718a07d286SRatan Gupta                 auto& searchSettingsJson =
7728a07d286SRatan Gupta                     serverTypeJson["LDAPService"]["SearchSettings"];
7738a07d286SRatan Gupta                 std::vector<std::string> modifiedBaseDNList = {
7748a07d286SRatan Gupta                     baseDNList.front()};
7758a07d286SRatan Gupta                 searchSettingsJson["BaseDistinguishedNames"] =
7768a07d286SRatan Gupta                     modifiedBaseDNList;
7778a07d286SRatan Gupta                 if (baseDNList.size() > 1)
7788a07d286SRatan Gupta                 {
7798a07d286SRatan Gupta                     messages::propertyValueModified(asyncResp->res,
7808a07d286SRatan Gupta                                                     "BaseDistinguishedNames",
7818a07d286SRatan Gupta                                                     baseDNList.front());
7828a07d286SRatan Gupta                 }
7838a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the base DN";
7848a07d286SRatan Gupta             },
7858a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7868a07d286SRatan Gupta             ldapConfigInterface, "LDAPBaseDN",
7878a07d286SRatan Gupta             std::variant<std::string>(baseDNList.front()));
7888a07d286SRatan Gupta     }
7898a07d286SRatan Gupta     /**
7908a07d286SRatan Gupta      * @brief updates the LDAP user name attribute and updates the
7918a07d286SRatan Gupta               json response with the new value.
7928a07d286SRatan Gupta      * @param userNameAttribute attribute to be updated.
7938a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
7948a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
7958a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
7968a07d286SRatan Gupta      */
7978a07d286SRatan Gupta 
7988a07d286SRatan Gupta     void handleUserNameAttrPatch(const std::string& userNameAttribute,
7998a07d286SRatan Gupta                                  const std::shared_ptr<AsyncResp>& asyncResp,
8008a07d286SRatan Gupta                                  const std::string& ldapServerElementName,
8018a07d286SRatan Gupta                                  const std::string& ldapConfigObject)
8028a07d286SRatan Gupta     {
8038a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
8048a07d286SRatan Gupta             [asyncResp, userNameAttribute,
8058a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
8068a07d286SRatan Gupta                 if (ec)
8078a07d286SRatan Gupta                 {
8088a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG << "Error Occured in Updating the "
8098a07d286SRatan Gupta                                         "username attribute";
8108a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
8118a07d286SRatan Gupta                     return;
8128a07d286SRatan Gupta                 }
8138a07d286SRatan Gupta                 auto& serverTypeJson =
8148a07d286SRatan Gupta                     asyncResp->res.jsonValue[ldapServerElementName];
8158a07d286SRatan Gupta                 auto& searchSettingsJson =
8168a07d286SRatan Gupta                     serverTypeJson["LDAPService"]["SearchSettings"];
8178a07d286SRatan Gupta                 searchSettingsJson["UsernameAttribute"] = userNameAttribute;
8188a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the user name attr.";
8198a07d286SRatan Gupta             },
8208a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8218a07d286SRatan Gupta             ldapConfigInterface, "UserNameAttribute",
8228a07d286SRatan Gupta             std::variant<std::string>(userNameAttribute));
8238a07d286SRatan Gupta     }
8248a07d286SRatan Gupta     /**
8258a07d286SRatan Gupta      * @brief updates the LDAP group attribute and updates the
8268a07d286SRatan Gupta               json response with the new value.
8278a07d286SRatan Gupta      * @param groupsAttribute attribute to be updated.
8288a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
8298a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
8308a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
8318a07d286SRatan Gupta      */
8328a07d286SRatan Gupta 
8338a07d286SRatan Gupta     void handleGroupNameAttrPatch(const std::string& groupsAttribute,
8348a07d286SRatan Gupta                                   const std::shared_ptr<AsyncResp>& asyncResp,
8358a07d286SRatan Gupta                                   const std::string& ldapServerElementName,
8368a07d286SRatan Gupta                                   const std::string& ldapConfigObject)
8378a07d286SRatan Gupta     {
8388a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
8398a07d286SRatan Gupta             [asyncResp, groupsAttribute,
8408a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
8418a07d286SRatan Gupta                 if (ec)
8428a07d286SRatan Gupta                 {
8438a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG << "Error Occured in Updating the "
8448a07d286SRatan Gupta                                         "groupname attribute";
8458a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
8468a07d286SRatan Gupta                     return;
8478a07d286SRatan Gupta                 }
8488a07d286SRatan Gupta                 auto& serverTypeJson =
8498a07d286SRatan Gupta                     asyncResp->res.jsonValue[ldapServerElementName];
8508a07d286SRatan Gupta                 auto& searchSettingsJson =
8518a07d286SRatan Gupta                     serverTypeJson["LDAPService"]["SearchSettings"];
8528a07d286SRatan Gupta                 searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8538a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8548a07d286SRatan Gupta             },
8558a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8568a07d286SRatan Gupta             ldapConfigInterface, "GroupNameAttribute",
8578a07d286SRatan Gupta             std::variant<std::string>(groupsAttribute));
8588a07d286SRatan Gupta     }
8598a07d286SRatan Gupta     /**
8608a07d286SRatan Gupta      * @brief updates the LDAP service enable and updates the
8618a07d286SRatan Gupta               json response with the new value.
8628a07d286SRatan Gupta      * @param input JSON data.
8638a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
8648a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
8658a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
8668a07d286SRatan Gupta      */
8678a07d286SRatan Gupta 
8688a07d286SRatan Gupta     void handleServiceEnablePatch(bool serviceEnabled,
8698a07d286SRatan Gupta                                   const std::shared_ptr<AsyncResp>& asyncResp,
8708a07d286SRatan Gupta                                   const std::string& ldapServerElementName,
8718a07d286SRatan Gupta                                   const std::string& ldapConfigObject)
8728a07d286SRatan Gupta     {
8738a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
8748a07d286SRatan Gupta             [asyncResp, serviceEnabled,
8758a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
8768a07d286SRatan Gupta                 if (ec)
8778a07d286SRatan Gupta                 {
8788a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG
8798a07d286SRatan Gupta                         << "Error Occured in Updating the service enable";
8808a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
8818a07d286SRatan Gupta                     return;
8828a07d286SRatan Gupta                 }
8838a07d286SRatan Gupta                 asyncResp->res
8848a07d286SRatan Gupta                     .jsonValue[ldapServerElementName]["ServiceEnabled"] =
8858a07d286SRatan Gupta                     serviceEnabled;
8868a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated Service enable = "
8878a07d286SRatan Gupta                                  << serviceEnabled;
8888a07d286SRatan Gupta             },
8898a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8908a07d286SRatan Gupta             ldapEnableInterface, "Enabled", std::variant<bool>(serviceEnabled));
8918a07d286SRatan Gupta     }
8928a07d286SRatan Gupta 
89378158631SZbigniew Kurzynski     void handleAuthMethodsPatch(nlohmann::json& input,
89478158631SZbigniew Kurzynski                                 const std::shared_ptr<AsyncResp>& asyncResp)
89578158631SZbigniew Kurzynski     {
89678158631SZbigniew Kurzynski         std::optional<bool> basicAuth;
89778158631SZbigniew Kurzynski         std::optional<bool> cookie;
89878158631SZbigniew Kurzynski         std::optional<bool> sessionToken;
89978158631SZbigniew Kurzynski         std::optional<bool> xToken;
900501f1e58SZbigniew Kurzynski         std::optional<bool> tls;
90178158631SZbigniew Kurzynski 
90278158631SZbigniew Kurzynski         if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
90378158631SZbigniew Kurzynski                                  "Cookie", cookie, "SessionToken", sessionToken,
904501f1e58SZbigniew Kurzynski                                  "XToken", xToken, "TLS", tls))
90578158631SZbigniew Kurzynski         {
90678158631SZbigniew Kurzynski             BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
90778158631SZbigniew Kurzynski             return;
90878158631SZbigniew Kurzynski         }
90978158631SZbigniew Kurzynski 
91078158631SZbigniew Kurzynski         // Make a copy of methods configuration
91178158631SZbigniew Kurzynski         crow::persistent_data::AuthConfigMethods authMethodsConfig =
91278158631SZbigniew Kurzynski             crow::persistent_data::SessionStore::getInstance()
91378158631SZbigniew Kurzynski                 .getAuthMethodsConfig();
91478158631SZbigniew Kurzynski 
91578158631SZbigniew Kurzynski         if (basicAuth)
91678158631SZbigniew Kurzynski         {
91778158631SZbigniew Kurzynski             authMethodsConfig.basic = *basicAuth;
91878158631SZbigniew Kurzynski         }
91978158631SZbigniew Kurzynski 
92078158631SZbigniew Kurzynski         if (cookie)
92178158631SZbigniew Kurzynski         {
92278158631SZbigniew Kurzynski             authMethodsConfig.cookie = *cookie;
92378158631SZbigniew Kurzynski         }
92478158631SZbigniew Kurzynski 
92578158631SZbigniew Kurzynski         if (sessionToken)
92678158631SZbigniew Kurzynski         {
92778158631SZbigniew Kurzynski             authMethodsConfig.sessionToken = *sessionToken;
92878158631SZbigniew Kurzynski         }
92978158631SZbigniew Kurzynski 
93078158631SZbigniew Kurzynski         if (xToken)
93178158631SZbigniew Kurzynski         {
93278158631SZbigniew Kurzynski             authMethodsConfig.xtoken = *xToken;
93378158631SZbigniew Kurzynski         }
93478158631SZbigniew Kurzynski 
935501f1e58SZbigniew Kurzynski         if (tls)
936501f1e58SZbigniew Kurzynski         {
937501f1e58SZbigniew Kurzynski             authMethodsConfig.tls = *tls;
938501f1e58SZbigniew Kurzynski         }
939501f1e58SZbigniew Kurzynski 
94078158631SZbigniew Kurzynski         if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
941501f1e58SZbigniew Kurzynski             !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
942501f1e58SZbigniew Kurzynski             !authMethodsConfig.tls)
94378158631SZbigniew Kurzynski         {
94478158631SZbigniew Kurzynski             // Do not allow user to disable everything
94578158631SZbigniew Kurzynski             messages::actionNotSupported(asyncResp->res,
94678158631SZbigniew Kurzynski                                          "of disabling all available methods");
94778158631SZbigniew Kurzynski             return;
94878158631SZbigniew Kurzynski         }
94978158631SZbigniew Kurzynski 
95078158631SZbigniew Kurzynski         crow::persistent_data::SessionStore::getInstance()
95178158631SZbigniew Kurzynski             .updateAuthMethodsConfig(authMethodsConfig);
95278158631SZbigniew Kurzynski         // Save configuration immediately
95378158631SZbigniew Kurzynski         app.template getMiddleware<crow::persistent_data::Middleware>()
95478158631SZbigniew Kurzynski             .writeData();
95578158631SZbigniew Kurzynski 
95678158631SZbigniew Kurzynski         messages::success(asyncResp->res);
95778158631SZbigniew Kurzynski     }
95878158631SZbigniew Kurzynski 
9598a07d286SRatan Gupta     /**
9608a07d286SRatan Gupta      * @brief Get the required values from the given JSON, validates the
9618a07d286SRatan Gupta      *        value and create the LDAP config object.
9628a07d286SRatan Gupta      * @param input JSON data
9638a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
9648a07d286SRatan Gupta      * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
9658a07d286SRatan Gupta      */
9668a07d286SRatan Gupta 
9678a07d286SRatan Gupta     void handleLDAPPatch(nlohmann::json& input,
9688a07d286SRatan Gupta                          const std::shared_ptr<AsyncResp>& asyncResp,
9698a07d286SRatan Gupta                          const crow::Request& req,
9708a07d286SRatan Gupta                          const std::vector<std::string>& params,
9718a07d286SRatan Gupta                          const std::string& serverType)
9728a07d286SRatan Gupta     {
973eb2bbe56SRatan Gupta         std::string dbusObjectPath;
974eb2bbe56SRatan Gupta         if (serverType == "ActiveDirectory")
975eb2bbe56SRatan Gupta         {
976eb2bbe56SRatan Gupta             dbusObjectPath = ADConfigObject;
977eb2bbe56SRatan Gupta         }
978eb2bbe56SRatan Gupta         else if (serverType == "LDAP")
979eb2bbe56SRatan Gupta         {
980eb2bbe56SRatan Gupta             dbusObjectPath = ldapConfigObject;
981eb2bbe56SRatan Gupta         }
982eb2bbe56SRatan Gupta 
9838a07d286SRatan Gupta         std::optional<nlohmann::json> authentication;
9848a07d286SRatan Gupta         std::optional<nlohmann::json> ldapService;
9858a07d286SRatan Gupta         std::optional<std::string> accountProviderType;
9868a07d286SRatan Gupta         std::optional<std::vector<std::string>> serviceAddressList;
9878a07d286SRatan Gupta         std::optional<bool> serviceEnabled;
9888a07d286SRatan Gupta         std::optional<std::vector<std::string>> baseDNList;
9898a07d286SRatan Gupta         std::optional<std::string> userNameAttribute;
9908a07d286SRatan Gupta         std::optional<std::string> groupsAttribute;
9918a07d286SRatan Gupta         std::optional<std::string> userName;
9928a07d286SRatan Gupta         std::optional<std::string> password;
99306785244SRatan Gupta         std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
9948a07d286SRatan Gupta 
9958a07d286SRatan Gupta         if (!json_util::readJson(input, asyncResp->res, "Authentication",
9968a07d286SRatan Gupta                                  authentication, "LDAPService", ldapService,
9978a07d286SRatan Gupta                                  "ServiceAddresses", serviceAddressList,
9988a07d286SRatan Gupta                                  "AccountProviderType", accountProviderType,
99906785244SRatan Gupta                                  "ServiceEnabled", serviceEnabled,
100006785244SRatan Gupta                                  "RemoteRoleMapping", remoteRoleMapData))
10018a07d286SRatan Gupta         {
10028a07d286SRatan Gupta             return;
10038a07d286SRatan Gupta         }
10048a07d286SRatan Gupta 
10058a07d286SRatan Gupta         if (authentication)
10068a07d286SRatan Gupta         {
10078a07d286SRatan Gupta             parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
10088a07d286SRatan Gupta                                         password);
10098a07d286SRatan Gupta         }
10108a07d286SRatan Gupta         if (ldapService)
10118a07d286SRatan Gupta         {
10128a07d286SRatan Gupta             parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10138a07d286SRatan Gupta                                  userNameAttribute, groupsAttribute);
10148a07d286SRatan Gupta         }
10158a07d286SRatan Gupta         if (accountProviderType)
10168a07d286SRatan Gupta         {
10178a07d286SRatan Gupta             messages::propertyNotWritable(asyncResp->res,
10188a07d286SRatan Gupta                                           "AccountProviderType");
10198a07d286SRatan Gupta         }
10208a07d286SRatan Gupta         if (serviceAddressList)
10218a07d286SRatan Gupta         {
10228a07d286SRatan Gupta             if ((*serviceAddressList).size() == 0)
10238a07d286SRatan Gupta             {
10248a07d286SRatan Gupta                 messages::propertyValueNotInList(asyncResp->res, "[]",
10258a07d286SRatan Gupta                                                  "ServiceAddress");
10268a07d286SRatan Gupta                 return;
10278a07d286SRatan Gupta             }
10288a07d286SRatan Gupta         }
10298a07d286SRatan Gupta         if (baseDNList)
10308a07d286SRatan Gupta         {
10318a07d286SRatan Gupta             if ((*baseDNList).size() == 0)
10328a07d286SRatan Gupta             {
10338a07d286SRatan Gupta                 messages::propertyValueNotInList(asyncResp->res, "[]",
10348a07d286SRatan Gupta                                                  "BaseDistinguishedNames");
10358a07d286SRatan Gupta                 return;
10368a07d286SRatan Gupta             }
10378a07d286SRatan Gupta         }
10388a07d286SRatan Gupta 
10398a07d286SRatan Gupta         // nothing to update, then return
10408a07d286SRatan Gupta         if (!userName && !password && !serviceAddressList && !baseDNList &&
104106785244SRatan Gupta             !userNameAttribute && !groupsAttribute && !serviceEnabled &&
104206785244SRatan Gupta             !remoteRoleMapData)
10438a07d286SRatan Gupta         {
10448a07d286SRatan Gupta             return;
10458a07d286SRatan Gupta         }
10468a07d286SRatan Gupta 
10478a07d286SRatan Gupta         // Get the existing resource first then keep modifying
10488a07d286SRatan Gupta         // whenever any property gets updated.
1049ab828d7cSRatan Gupta         getLDAPConfigData(serverType, [this, asyncResp, userName, password,
1050ab828d7cSRatan Gupta                                        baseDNList, userNameAttribute,
1051ab828d7cSRatan Gupta                                        groupsAttribute, accountProviderType,
1052eb2bbe56SRatan Gupta                                        serviceAddressList, serviceEnabled,
105306785244SRatan Gupta                                        dbusObjectPath, remoteRoleMapData](
1054ab828d7cSRatan Gupta                                           bool success, LDAPConfigData confData,
1055ab828d7cSRatan Gupta                                           const std::string& serverType) {
10568a07d286SRatan Gupta             if (!success)
10578a07d286SRatan Gupta             {
10588a07d286SRatan Gupta                 messages::internalError(asyncResp->res);
10598a07d286SRatan Gupta                 return;
10608a07d286SRatan Gupta             }
1061ab828d7cSRatan Gupta             parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverType);
10628a07d286SRatan Gupta             if (confData.serviceEnabled)
10638a07d286SRatan Gupta             {
10648a07d286SRatan Gupta                 // Disable the service first and update the rest of
10658a07d286SRatan Gupta                 // the properties.
10668a07d286SRatan Gupta                 handleServiceEnablePatch(false, asyncResp, serverType,
1067eb2bbe56SRatan Gupta                                          dbusObjectPath);
10688a07d286SRatan Gupta             }
10698a07d286SRatan Gupta 
10708a07d286SRatan Gupta             if (serviceAddressList)
10718a07d286SRatan Gupta             {
10728a07d286SRatan Gupta                 handleServiceAddressPatch(*serviceAddressList, asyncResp,
1073eb2bbe56SRatan Gupta                                           serverType, dbusObjectPath);
10748a07d286SRatan Gupta             }
10758a07d286SRatan Gupta             if (userName)
10768a07d286SRatan Gupta             {
10778a07d286SRatan Gupta                 handleUserNamePatch(*userName, asyncResp, serverType,
1078eb2bbe56SRatan Gupta                                     dbusObjectPath);
10798a07d286SRatan Gupta             }
10808a07d286SRatan Gupta             if (password)
10818a07d286SRatan Gupta             {
10828a07d286SRatan Gupta                 handlePasswordPatch(*password, asyncResp, serverType,
1083eb2bbe56SRatan Gupta                                     dbusObjectPath);
10848a07d286SRatan Gupta             }
10858a07d286SRatan Gupta 
10868a07d286SRatan Gupta             if (baseDNList)
10878a07d286SRatan Gupta             {
10888a07d286SRatan Gupta                 handleBaseDNPatch(*baseDNList, asyncResp, serverType,
1089eb2bbe56SRatan Gupta                                   dbusObjectPath);
10908a07d286SRatan Gupta             }
10918a07d286SRatan Gupta             if (userNameAttribute)
10928a07d286SRatan Gupta             {
10938a07d286SRatan Gupta                 handleUserNameAttrPatch(*userNameAttribute, asyncResp,
1094eb2bbe56SRatan Gupta                                         serverType, dbusObjectPath);
10958a07d286SRatan Gupta             }
10968a07d286SRatan Gupta             if (groupsAttribute)
10978a07d286SRatan Gupta             {
10988a07d286SRatan Gupta                 handleGroupNameAttrPatch(*groupsAttribute, asyncResp,
1099eb2bbe56SRatan Gupta                                          serverType, 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                 {
11088a07d286SRatan Gupta                     handleServiceEnablePatch(*serviceEnabled, asyncResp,
1109eb2bbe56SRatan Gupta                                              serverType, 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,
1118eb2bbe56SRatan Gupta                                          serverType, dbusObjectPath);
11198a07d286SRatan Gupta             }
112006785244SRatan Gupta 
112106785244SRatan Gupta             if (remoteRoleMapData)
112206785244SRatan Gupta             {
112306785244SRatan Gupta                 std::vector<nlohmann::json> remoteRoleMap =
112406785244SRatan Gupta                     std::move(*remoteRoleMapData);
112506785244SRatan Gupta 
112606785244SRatan Gupta                 handleRoleMapPatch(asyncResp, confData.groupRoleList,
112706785244SRatan Gupta                                    serverType, remoteRoleMap);
112806785244SRatan Gupta             }
11298a07d286SRatan Gupta         });
11308a07d286SRatan Gupta     }
1131d4b5443fSEd Tanous 
113255c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
11331abe55efSEd Tanous                const std::vector<std::string>& params) override
11341abe55efSEd Tanous     {
113578158631SZbigniew Kurzynski         const crow::persistent_data::AuthConfigMethods& authMethodsConfig =
113678158631SZbigniew Kurzynski             crow::persistent_data::SessionStore::getInstance()
113778158631SZbigniew Kurzynski                 .getAuthMethodsConfig();
113878158631SZbigniew Kurzynski 
11393d958bbcSAppaRao Puli         auto asyncResp = std::make_shared<AsyncResp>(res);
11403d958bbcSAppaRao Puli         res.jsonValue = {
11413d958bbcSAppaRao Puli             {"@odata.context", "/redfish/v1/"
11423d958bbcSAppaRao Puli                                "$metadata#AccountService.AccountService"},
11433d958bbcSAppaRao Puli             {"@odata.id", "/redfish/v1/AccountService"},
11443d958bbcSAppaRao Puli             {"@odata.type", "#AccountService."
114537cce918SMarri Devender Rao                             "v1_4_0.AccountService"},
11463d958bbcSAppaRao Puli             {"Id", "AccountService"},
11473d958bbcSAppaRao Puli             {"Name", "Account Service"},
11483d958bbcSAppaRao Puli             {"Description", "Account Service"},
11493d958bbcSAppaRao Puli             {"ServiceEnabled", true},
1150343ff2e1SAppaRao Puli             {"MaxPasswordLength", 20},
11513d958bbcSAppaRao Puli             {"Accounts",
11523d958bbcSAppaRao Puli              {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}},
115337cce918SMarri Devender Rao             {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}},
115478158631SZbigniew Kurzynski             {"Oem",
115578158631SZbigniew Kurzynski              {{"OpenBMC",
115678158631SZbigniew Kurzynski                {{"@odata.type", "#OemAccountService.v1_0_0.AccountService"},
115778158631SZbigniew Kurzynski                 {"AuthMethods",
115878158631SZbigniew Kurzynski                  {
115978158631SZbigniew Kurzynski                      {"BasicAuth", authMethodsConfig.basic},
116078158631SZbigniew Kurzynski                      {"SessionToken", authMethodsConfig.sessionToken},
116178158631SZbigniew Kurzynski                      {"XToken", authMethodsConfig.xtoken},
116278158631SZbigniew Kurzynski                      {"Cookie", authMethodsConfig.cookie},
1163501f1e58SZbigniew Kurzynski                      {"TLS", authMethodsConfig.tls},
116478158631SZbigniew Kurzynski                  }}}}}},
116537cce918SMarri Devender Rao             {"LDAP",
116637cce918SMarri Devender Rao              {{"Certificates",
116737cce918SMarri Devender Rao                {{"@odata.id",
116837cce918SMarri Devender Rao                  "/redfish/v1/AccountService/LDAP/Certificates"}}}}}};
11693d958bbcSAppaRao Puli         crow::connections::systemBus->async_method_call(
11703d958bbcSAppaRao Puli             [asyncResp](
11713d958bbcSAppaRao Puli                 const boost::system::error_code ec,
11723d958bbcSAppaRao Puli                 const std::vector<std::pair<
1173abf2add6SEd Tanous                     std::string, std::variant<uint32_t, uint16_t, uint8_t>>>&
11743d958bbcSAppaRao Puli                     propertiesList) {
11753d958bbcSAppaRao Puli                 if (ec)
11763d958bbcSAppaRao Puli                 {
11773d958bbcSAppaRao Puli                     messages::internalError(asyncResp->res);
11783d958bbcSAppaRao Puli                     return;
11793d958bbcSAppaRao Puli                 }
11803d958bbcSAppaRao Puli                 BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
11813d958bbcSAppaRao Puli                                  << "properties for AccountService";
11823d958bbcSAppaRao Puli                 for (const std::pair<std::string,
1183abf2add6SEd Tanous                                      std::variant<uint32_t, uint16_t, uint8_t>>&
11843d958bbcSAppaRao Puli                          property : propertiesList)
11853d958bbcSAppaRao Puli                 {
11863d958bbcSAppaRao Puli                     if (property.first == "MinPasswordLength")
11873d958bbcSAppaRao Puli                     {
11883d958bbcSAppaRao Puli                         const uint8_t* value =
1189abf2add6SEd Tanous                             std::get_if<uint8_t>(&property.second);
11903d958bbcSAppaRao Puli                         if (value != nullptr)
11913d958bbcSAppaRao Puli                         {
11923d958bbcSAppaRao Puli                             asyncResp->res.jsonValue["MinPasswordLength"] =
11933d958bbcSAppaRao Puli                                 *value;
11943d958bbcSAppaRao Puli                         }
11953d958bbcSAppaRao Puli                     }
11963d958bbcSAppaRao Puli                     if (property.first == "AccountUnlockTimeout")
11973d958bbcSAppaRao Puli                     {
11983d958bbcSAppaRao Puli                         const uint32_t* value =
1199abf2add6SEd Tanous                             std::get_if<uint32_t>(&property.second);
12003d958bbcSAppaRao Puli                         if (value != nullptr)
12013d958bbcSAppaRao Puli                         {
12023d958bbcSAppaRao Puli                             asyncResp->res.jsonValue["AccountLockoutDuration"] =
12033d958bbcSAppaRao Puli                                 *value;
12043d958bbcSAppaRao Puli                         }
12053d958bbcSAppaRao Puli                     }
12063d958bbcSAppaRao Puli                     if (property.first == "MaxLoginAttemptBeforeLockout")
12073d958bbcSAppaRao Puli                     {
12083d958bbcSAppaRao Puli                         const uint16_t* value =
1209abf2add6SEd Tanous                             std::get_if<uint16_t>(&property.second);
12103d958bbcSAppaRao Puli                         if (value != nullptr)
12113d958bbcSAppaRao Puli                         {
12123d958bbcSAppaRao Puli                             asyncResp->res
12133d958bbcSAppaRao Puli                                 .jsonValue["AccountLockoutThreshold"] = *value;
12143d958bbcSAppaRao Puli                         }
12153d958bbcSAppaRao Puli                     }
12163d958bbcSAppaRao Puli                 }
12173d958bbcSAppaRao Puli             },
12183d958bbcSAppaRao Puli             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
12193d958bbcSAppaRao Puli             "org.freedesktop.DBus.Properties", "GetAll",
12203d958bbcSAppaRao Puli             "xyz.openbmc_project.User.AccountPolicy");
12216973a582SRatan Gupta 
1222ab828d7cSRatan Gupta         auto callback = [asyncResp](bool success, LDAPConfigData& confData,
1223ab828d7cSRatan Gupta                                     const std::string& ldapType) {
1224ab828d7cSRatan Gupta             parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1225ab828d7cSRatan Gupta         };
1226ab828d7cSRatan Gupta 
1227ab828d7cSRatan Gupta         getLDAPConfigData("LDAP", callback);
1228ab828d7cSRatan Gupta         getLDAPConfigData("ActiveDirectory", callback);
12293d958bbcSAppaRao Puli     }
12306973a582SRatan Gupta 
12313d958bbcSAppaRao Puli     void doPatch(crow::Response& res, const crow::Request& req,
12323d958bbcSAppaRao Puli                  const std::vector<std::string>& params) override
12333d958bbcSAppaRao Puli     {
12343d958bbcSAppaRao Puli         auto asyncResp = std::make_shared<AsyncResp>(res);
12353d958bbcSAppaRao Puli 
12363d958bbcSAppaRao Puli         std::optional<uint32_t> unlockTimeout;
12373d958bbcSAppaRao Puli         std::optional<uint16_t> lockoutThreshold;
123819fb6e71SRatan Gupta         std::optional<uint16_t> minPasswordLength;
123919fb6e71SRatan Gupta         std::optional<uint16_t> maxPasswordLength;
12408a07d286SRatan Gupta         std::optional<nlohmann::json> ldapObject;
1241eb2bbe56SRatan Gupta         std::optional<nlohmann::json> activeDirectoryObject;
124278158631SZbigniew Kurzynski         std::optional<nlohmann::json> oemObject;
124319fb6e71SRatan Gupta 
124478158631SZbigniew Kurzynski         if (!json_util::readJson(
124578158631SZbigniew Kurzynski                 req, res, "AccountLockoutDuration", unlockTimeout,
124678158631SZbigniew Kurzynski                 "AccountLockoutThreshold", lockoutThreshold,
124778158631SZbigniew Kurzynski                 "MaxPasswordLength", maxPasswordLength, "MinPasswordLength",
124878158631SZbigniew Kurzynski                 minPasswordLength, "LDAP", ldapObject, "ActiveDirectory",
124978158631SZbigniew Kurzynski                 activeDirectoryObject, "Oem", oemObject))
12503d958bbcSAppaRao Puli         {
12513d958bbcSAppaRao Puli             return;
12523d958bbcSAppaRao Puli         }
125319fb6e71SRatan Gupta 
125419fb6e71SRatan Gupta         if (minPasswordLength)
125519fb6e71SRatan Gupta         {
125619fb6e71SRatan Gupta             messages::propertyNotWritable(asyncResp->res, "MinPasswordLength");
125719fb6e71SRatan Gupta         }
125819fb6e71SRatan Gupta 
125919fb6e71SRatan Gupta         if (maxPasswordLength)
126019fb6e71SRatan Gupta         {
126119fb6e71SRatan Gupta             messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
126219fb6e71SRatan Gupta         }
126319fb6e71SRatan Gupta 
12648a07d286SRatan Gupta         if (ldapObject)
12658a07d286SRatan Gupta         {
12668a07d286SRatan Gupta             handleLDAPPatch(*ldapObject, asyncResp, req, params, "LDAP");
12678a07d286SRatan Gupta         }
12688a07d286SRatan Gupta 
126978158631SZbigniew Kurzynski         if (std::optional<nlohmann::json> oemOpenBMCObject;
127078158631SZbigniew Kurzynski             oemObject &&
127178158631SZbigniew Kurzynski             json_util::readJson(*oemObject, res, "OpenBMC", oemOpenBMCObject))
127278158631SZbigniew Kurzynski         {
127378158631SZbigniew Kurzynski             if (std::optional<nlohmann::json> authMethodsObject;
127478158631SZbigniew Kurzynski                 oemOpenBMCObject &&
127578158631SZbigniew Kurzynski                 json_util::readJson(*oemOpenBMCObject, res, "AuthMethods",
127678158631SZbigniew Kurzynski                                     authMethodsObject))
127778158631SZbigniew Kurzynski             {
127878158631SZbigniew Kurzynski                 if (authMethodsObject)
127978158631SZbigniew Kurzynski                 {
128078158631SZbigniew Kurzynski                     handleAuthMethodsPatch(*authMethodsObject, asyncResp);
128178158631SZbigniew Kurzynski                 }
128278158631SZbigniew Kurzynski             }
128378158631SZbigniew Kurzynski         }
128478158631SZbigniew Kurzynski 
1285eb2bbe56SRatan Gupta         if (activeDirectoryObject)
1286eb2bbe56SRatan Gupta         {
1287eb2bbe56SRatan Gupta             handleLDAPPatch(*activeDirectoryObject, asyncResp, req, params,
1288eb2bbe56SRatan Gupta                             "ActiveDirectory");
1289eb2bbe56SRatan Gupta         }
1290eb2bbe56SRatan Gupta 
12913d958bbcSAppaRao Puli         if (unlockTimeout)
12923d958bbcSAppaRao Puli         {
12933d958bbcSAppaRao Puli             crow::connections::systemBus->async_method_call(
12943d958bbcSAppaRao Puli                 [asyncResp](const boost::system::error_code ec) {
12953d958bbcSAppaRao Puli                     if (ec)
12963d958bbcSAppaRao Puli                     {
12973d958bbcSAppaRao Puli                         messages::internalError(asyncResp->res);
12983d958bbcSAppaRao Puli                         return;
12993d958bbcSAppaRao Puli                     }
1300add6133bSRatan Gupta                     messages::success(asyncResp->res);
13013d958bbcSAppaRao Puli                 },
13023d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
13033d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "Set",
13043d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy",
1305abf2add6SEd Tanous                 "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout));
13063d958bbcSAppaRao Puli         }
13073d958bbcSAppaRao Puli         if (lockoutThreshold)
13083d958bbcSAppaRao Puli         {
13093d958bbcSAppaRao Puli             crow::connections::systemBus->async_method_call(
13103d958bbcSAppaRao Puli                 [asyncResp](const boost::system::error_code ec) {
13113d958bbcSAppaRao Puli                     if (ec)
13123d958bbcSAppaRao Puli                     {
13133d958bbcSAppaRao Puli                         messages::internalError(asyncResp->res);
13143d958bbcSAppaRao Puli                         return;
13153d958bbcSAppaRao Puli                     }
1316add6133bSRatan Gupta                     messages::success(asyncResp->res);
13173d958bbcSAppaRao Puli                 },
13183d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
13193d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "Set",
13203d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy",
13213d958bbcSAppaRao Puli                 "MaxLoginAttemptBeforeLockout",
1322abf2add6SEd Tanous                 std::variant<uint16_t>(*lockoutThreshold));
13233d958bbcSAppaRao Puli         }
132488d16c9aSLewanczyk, Dawid     }
132578158631SZbigniew Kurzynski 
132678158631SZbigniew Kurzynski     CrowApp& app;
132788d16c9aSLewanczyk, Dawid };
1328f00032dbSTanous 
1329b9b2e0b2SEd Tanous class AccountsCollection : public Node
1330b9b2e0b2SEd Tanous {
1331b9b2e0b2SEd Tanous   public:
1332b9b2e0b2SEd Tanous     AccountsCollection(CrowApp& app) :
1333b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/")
1334b9b2e0b2SEd Tanous     {
1335b9b2e0b2SEd Tanous         entityPrivileges = {
1336b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
1337b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}}},
1338b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1339b9b2e0b2SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
1340b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
1341b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
1342b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
1343b9b2e0b2SEd Tanous     }
1344b9b2e0b2SEd Tanous 
1345b9b2e0b2SEd Tanous   private:
1346b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
1347b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
1348b9b2e0b2SEd Tanous     {
1349b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
13500f74e643SEd Tanous         res.jsonValue = {{"@odata.context",
13510f74e643SEd Tanous                           "/redfish/v1/"
13520f74e643SEd Tanous                           "$metadata#ManagerAccountCollection."
13530f74e643SEd Tanous                           "ManagerAccountCollection"},
13540f74e643SEd Tanous                          {"@odata.id", "/redfish/v1/AccountService/Accounts"},
13550f74e643SEd Tanous                          {"@odata.type", "#ManagerAccountCollection."
13560f74e643SEd Tanous                                          "ManagerAccountCollection"},
13570f74e643SEd Tanous                          {"Name", "Accounts Collection"},
13580f74e643SEd Tanous                          {"Description", "BMC User Accounts"}};
13590f74e643SEd Tanous 
1360b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
1361b9b2e0b2SEd Tanous             [asyncResp](const boost::system::error_code ec,
1362b9b2e0b2SEd Tanous                         const ManagedObjectType& users) {
1363b9b2e0b2SEd Tanous                 if (ec)
1364b9b2e0b2SEd Tanous                 {
1365f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
1366b9b2e0b2SEd Tanous                     return;
1367b9b2e0b2SEd Tanous                 }
1368b9b2e0b2SEd Tanous 
1369b9b2e0b2SEd Tanous                 nlohmann::json& memberArray =
1370b9b2e0b2SEd Tanous                     asyncResp->res.jsonValue["Members"];
1371b9b2e0b2SEd Tanous                 memberArray = nlohmann::json::array();
1372b9b2e0b2SEd Tanous 
1373b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = users.size();
1374b9b2e0b2SEd Tanous                 for (auto& user : users)
1375b9b2e0b2SEd Tanous                 {
1376b9b2e0b2SEd Tanous                     const std::string& path =
1377b9b2e0b2SEd Tanous                         static_cast<const std::string&>(user.first);
1378b9b2e0b2SEd Tanous                     std::size_t lastIndex = path.rfind("/");
1379b9b2e0b2SEd Tanous                     if (lastIndex == std::string::npos)
1380b9b2e0b2SEd Tanous                     {
1381b9b2e0b2SEd Tanous                         lastIndex = 0;
1382b9b2e0b2SEd Tanous                     }
1383b9b2e0b2SEd Tanous                     else
1384b9b2e0b2SEd Tanous                     {
1385b9b2e0b2SEd Tanous                         lastIndex += 1;
1386b9b2e0b2SEd Tanous                     }
1387b9b2e0b2SEd Tanous                     memberArray.push_back(
1388b9b2e0b2SEd Tanous                         {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
1389b9b2e0b2SEd Tanous                                            path.substr(lastIndex)}});
1390b9b2e0b2SEd Tanous                 }
1391b9b2e0b2SEd Tanous             },
1392b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1393b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1394b9b2e0b2SEd Tanous     }
139504ae99ecSEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
139604ae99ecSEd Tanous                 const std::vector<std::string>& params) override
139704ae99ecSEd Tanous     {
139804ae99ecSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
139904ae99ecSEd Tanous 
14009712f8acSEd Tanous         std::string username;
14019712f8acSEd Tanous         std::string password;
1402a24526dcSEd Tanous         std::optional<std::string> roleId("User");
1403a24526dcSEd Tanous         std::optional<bool> enabled = true;
14049712f8acSEd Tanous         if (!json_util::readJson(req, res, "UserName", username, "Password",
14059712f8acSEd Tanous                                  password, "RoleId", roleId, "Enabled",
14069712f8acSEd Tanous                                  enabled))
140704ae99ecSEd Tanous         {
140804ae99ecSEd Tanous             return;
140904ae99ecSEd Tanous         }
141004ae99ecSEd Tanous 
141154fc587aSNagaraju Goruganti         std::string priv = getPrivilegeFromRoleId(*roleId);
141284e12cb7SAppaRao Puli         if (priv.empty())
141304ae99ecSEd Tanous         {
1414f12894f8SJason M. Bills             messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
141504ae99ecSEd Tanous             return;
141604ae99ecSEd Tanous         }
14179712f8acSEd Tanous         roleId = priv;
141804ae99ecSEd Tanous 
1419599c71d8SAyushi Smriti         // Reading AllGroups property
1420599c71d8SAyushi Smriti         crow::connections::systemBus->async_method_call(
1421599c71d8SAyushi Smriti             [asyncResp, username, password{std::move(password)}, roleId,
1422599c71d8SAyushi Smriti              enabled](const boost::system::error_code ec,
1423599c71d8SAyushi Smriti                       const std::variant<std::vector<std::string>>& allGroups) {
1424599c71d8SAyushi Smriti                 if (ec)
1425599c71d8SAyushi Smriti                 {
1426599c71d8SAyushi Smriti                     BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1427599c71d8SAyushi Smriti                     messages::internalError(asyncResp->res);
1428599c71d8SAyushi Smriti                     return;
1429599c71d8SAyushi Smriti                 }
1430599c71d8SAyushi Smriti 
1431599c71d8SAyushi Smriti                 const std::vector<std::string>* allGroupsList =
1432599c71d8SAyushi Smriti                     std::get_if<std::vector<std::string>>(&allGroups);
1433599c71d8SAyushi Smriti 
1434599c71d8SAyushi Smriti                 if (allGroupsList == nullptr || allGroupsList->empty())
1435599c71d8SAyushi Smriti                 {
1436599c71d8SAyushi Smriti                     messages::internalError(asyncResp->res);
1437599c71d8SAyushi Smriti                     return;
1438599c71d8SAyushi Smriti                 }
1439599c71d8SAyushi Smriti 
144004ae99ecSEd Tanous                 crow::connections::systemBus->async_method_call(
14419712f8acSEd Tanous                     [asyncResp, username, password{std::move(password)}](
14420d4197e0Sanil kumar appana                         const boost::system::error_code ec,
14430d4197e0Sanil kumar appana                         sdbusplus::message::message& m) {
144404ae99ecSEd Tanous                         if (ec)
144504ae99ecSEd Tanous                         {
14460d4197e0Sanil kumar appana                             userErrorMessageHandler(m.get_error(), asyncResp,
14470d4197e0Sanil kumar appana                                                     username, "");
144804ae99ecSEd Tanous                             return;
144904ae99ecSEd Tanous                         }
145004ae99ecSEd Tanous 
145166b5ca76Sjayaprakash Mutyala                         if (pamUpdatePassword(username, password) !=
145266b5ca76Sjayaprakash Mutyala                             PAM_SUCCESS)
145304ae99ecSEd Tanous                         {
1454599c71d8SAyushi Smriti                             // At this point we have a user that's been created,
1455599c71d8SAyushi Smriti                             // but the password set failed.Something is wrong,
1456599c71d8SAyushi Smriti                             // so delete the user that we've already created
145704ae99ecSEd Tanous                             crow::connections::systemBus->async_method_call(
14580d4197e0Sanil kumar appana                                 [asyncResp,
14590d4197e0Sanil kumar appana                                  password](const boost::system::error_code ec) {
146004ae99ecSEd Tanous                                     if (ec)
146104ae99ecSEd Tanous                                     {
1462f12894f8SJason M. Bills                                         messages::internalError(asyncResp->res);
146304ae99ecSEd Tanous                                         return;
146404ae99ecSEd Tanous                                     }
146504ae99ecSEd Tanous 
14660d4197e0Sanil kumar appana                                     // If password is invalid
14670d4197e0Sanil kumar appana                                     messages::propertyValueFormatError(
14680d4197e0Sanil kumar appana                                         asyncResp->res, password, "Password");
146904ae99ecSEd Tanous                                 },
147004ae99ecSEd Tanous                                 "xyz.openbmc_project.User.Manager",
147104ae99ecSEd Tanous                                 "/xyz/openbmc_project/user/" + username,
147204ae99ecSEd Tanous                                 "xyz.openbmc_project.Object.Delete", "Delete");
147304ae99ecSEd Tanous 
147404ae99ecSEd Tanous                             BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
147504ae99ecSEd Tanous                             return;
147604ae99ecSEd Tanous                         }
147704ae99ecSEd Tanous 
1478f12894f8SJason M. Bills                         messages::created(asyncResp->res);
147904ae99ecSEd Tanous                         asyncResp->res.addHeader(
148004ae99ecSEd Tanous                             "Location",
148104ae99ecSEd Tanous                             "/redfish/v1/AccountService/Accounts/" + username);
148204ae99ecSEd Tanous                     },
1483599c71d8SAyushi Smriti                     "xyz.openbmc_project.User.Manager",
1484599c71d8SAyushi Smriti                     "/xyz/openbmc_project/user",
14859712f8acSEd Tanous                     "xyz.openbmc_project.User.Manager", "CreateUser", username,
1486599c71d8SAyushi Smriti                     *allGroupsList, *roleId, *enabled);
1487599c71d8SAyushi Smriti             },
1488599c71d8SAyushi Smriti             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1489599c71d8SAyushi Smriti             "org.freedesktop.DBus.Properties", "Get",
1490599c71d8SAyushi Smriti             "xyz.openbmc_project.User.Manager", "AllGroups");
149104ae99ecSEd Tanous     }
1492b9b2e0b2SEd Tanous };
1493b9b2e0b2SEd Tanous 
1494b9b2e0b2SEd Tanous class ManagerAccount : public Node
1495b9b2e0b2SEd Tanous {
1496b9b2e0b2SEd Tanous   public:
1497b9b2e0b2SEd Tanous     ManagerAccount(CrowApp& app) :
1498b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string())
1499b9b2e0b2SEd Tanous     {
1500b9b2e0b2SEd Tanous         entityPrivileges = {
1501b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
1502b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}},
1503b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1504900f9497SJoseph Reynolds             {boost::beast::http::verb::patch,
1505900f9497SJoseph Reynolds              {{"ConfigureUsers"}, {"ConfigureSelf"}}},
1506b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
1507b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
1508b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
1509b9b2e0b2SEd Tanous     }
1510b9b2e0b2SEd Tanous 
1511b9b2e0b2SEd Tanous   private:
1512b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
1513b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
1514b9b2e0b2SEd Tanous     {
1515b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1516b9b2e0b2SEd Tanous 
1517b9b2e0b2SEd Tanous         if (params.size() != 1)
1518b9b2e0b2SEd Tanous         {
1519f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1520b9b2e0b2SEd Tanous             return;
1521b9b2e0b2SEd Tanous         }
1522b9b2e0b2SEd Tanous 
1523900f9497SJoseph Reynolds         // Perform a proper ConfigureSelf authority check.  If the
1524900f9497SJoseph Reynolds         // user is operating on an account not their own, then their
1525900f9497SJoseph Reynolds         // ConfigureSelf privilege does not apply.  In this case,
1526900f9497SJoseph Reynolds         // perform the authority check again without the user's
1527900f9497SJoseph Reynolds         // ConfigureSelf privilege.
1528900f9497SJoseph Reynolds         if (req.session->username != params[0])
1529900f9497SJoseph Reynolds         {
1530900f9497SJoseph Reynolds             if (!isAllowedWithoutConfigureSelf(req))
1531900f9497SJoseph Reynolds             {
1532900f9497SJoseph Reynolds                 BMCWEB_LOG_DEBUG << "GET Account denied access";
1533900f9497SJoseph Reynolds                 messages::insufficientPrivilege(asyncResp->res);
1534900f9497SJoseph Reynolds                 return;
1535900f9497SJoseph Reynolds             }
1536900f9497SJoseph Reynolds         }
1537900f9497SJoseph Reynolds 
1538b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
1539b9b2e0b2SEd Tanous             [asyncResp, accountName{std::string(params[0])}](
1540b9b2e0b2SEd Tanous                 const boost::system::error_code ec,
1541b9b2e0b2SEd Tanous                 const ManagedObjectType& users) {
1542b9b2e0b2SEd Tanous                 if (ec)
1543b9b2e0b2SEd Tanous                 {
1544f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
1545b9b2e0b2SEd Tanous                     return;
1546b9b2e0b2SEd Tanous                 }
154784e12cb7SAppaRao Puli                 auto userIt = users.begin();
1548b9b2e0b2SEd Tanous 
154984e12cb7SAppaRao Puli                 for (; userIt != users.end(); userIt++)
1550b9b2e0b2SEd Tanous                 {
155184e12cb7SAppaRao Puli                     if (boost::ends_with(userIt->first.str, "/" + accountName))
1552b9b2e0b2SEd Tanous                     {
155384e12cb7SAppaRao Puli                         break;
1554b9b2e0b2SEd Tanous                     }
1555b9b2e0b2SEd Tanous                 }
155684e12cb7SAppaRao Puli                 if (userIt == users.end())
1557b9b2e0b2SEd Tanous                 {
155884e12cb7SAppaRao Puli                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
155984e12cb7SAppaRao Puli                                                accountName);
156084e12cb7SAppaRao Puli                     return;
156184e12cb7SAppaRao Puli                 }
15624e68c45bSAyushi Smriti 
15634e68c45bSAyushi Smriti                 asyncResp->res.jsonValue = {
15644e68c45bSAyushi Smriti                     {"@odata.context",
15654e68c45bSAyushi Smriti                      "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
15664e68c45bSAyushi Smriti                     {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"},
15674e68c45bSAyushi Smriti                     {"Name", "User Account"},
15684e68c45bSAyushi Smriti                     {"Description", "User Account"},
15694e68c45bSAyushi Smriti                     {"Password", nullptr}};
15704e68c45bSAyushi Smriti 
157184e12cb7SAppaRao Puli                 for (const auto& interface : userIt->second)
157265b0dc32SEd Tanous                 {
157365b0dc32SEd Tanous                     if (interface.first ==
157465b0dc32SEd Tanous                         "xyz.openbmc_project.User.Attributes")
157565b0dc32SEd Tanous                     {
157665b0dc32SEd Tanous                         for (const auto& property : interface.second)
157765b0dc32SEd Tanous                         {
157865b0dc32SEd Tanous                             if (property.first == "UserEnabled")
157965b0dc32SEd Tanous                             {
158065b0dc32SEd Tanous                                 const bool* userEnabled =
1581abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
158265b0dc32SEd Tanous                                 if (userEnabled == nullptr)
158365b0dc32SEd Tanous                                 {
158465b0dc32SEd Tanous                                     BMCWEB_LOG_ERROR
158565b0dc32SEd Tanous                                         << "UserEnabled wasn't a bool";
158684e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
158784e12cb7SAppaRao Puli                                     return;
158865b0dc32SEd Tanous                                 }
158965b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Enabled"] =
159065b0dc32SEd Tanous                                     *userEnabled;
159165b0dc32SEd Tanous                             }
159265b0dc32SEd Tanous                             else if (property.first ==
159365b0dc32SEd Tanous                                      "UserLockedForFailedAttempt")
159465b0dc32SEd Tanous                             {
159565b0dc32SEd Tanous                                 const bool* userLocked =
1596abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
159765b0dc32SEd Tanous                                 if (userLocked == nullptr)
159865b0dc32SEd Tanous                                 {
159984e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "UserLockedForF"
160084e12cb7SAppaRao Puli                                                         "ailedAttempt "
160184e12cb7SAppaRao Puli                                                         "wasn't a bool";
160284e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
160384e12cb7SAppaRao Puli                                     return;
160465b0dc32SEd Tanous                                 }
160565b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Locked"] =
160665b0dc32SEd Tanous                                     *userLocked;
160724c8542dSRatan Gupta                                 asyncResp->res.jsonValue
160824c8542dSRatan Gupta                                     ["Locked@Redfish.AllowableValues"] = {
16094d64ce34SGunnar Mills                                     "false"};
161065b0dc32SEd Tanous                             }
161184e12cb7SAppaRao Puli                             else if (property.first == "UserPrivilege")
161284e12cb7SAppaRao Puli                             {
161354fc587aSNagaraju Goruganti                                 const std::string* userPrivPtr =
1614abf2add6SEd Tanous                                     std::get_if<std::string>(&property.second);
161554fc587aSNagaraju Goruganti                                 if (userPrivPtr == nullptr)
161684e12cb7SAppaRao Puli                                 {
161784e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR
161884e12cb7SAppaRao Puli                                         << "UserPrivilege wasn't a "
161984e12cb7SAppaRao Puli                                            "string";
162084e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
162184e12cb7SAppaRao Puli                                     return;
162284e12cb7SAppaRao Puli                                 }
162354fc587aSNagaraju Goruganti                                 std::string role =
162454fc587aSNagaraju Goruganti                                     getRoleIdFromPrivilege(*userPrivPtr);
162554fc587aSNagaraju Goruganti                                 if (role.empty())
162684e12cb7SAppaRao Puli                                 {
162784e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "Invalid user role";
162884e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
162984e12cb7SAppaRao Puli                                     return;
163084e12cb7SAppaRao Puli                                 }
163154fc587aSNagaraju Goruganti                                 asyncResp->res.jsonValue["RoleId"] = role;
163284e12cb7SAppaRao Puli 
163384e12cb7SAppaRao Puli                                 asyncResp->res.jsonValue["Links"]["Role"] = {
163484e12cb7SAppaRao Puli                                     {"@odata.id", "/redfish/v1/AccountService/"
163584e12cb7SAppaRao Puli                                                   "Roles/" +
163654fc587aSNagaraju Goruganti                                                       role}};
163784e12cb7SAppaRao Puli                             }
163865b0dc32SEd Tanous                         }
163965b0dc32SEd Tanous                     }
164065b0dc32SEd Tanous                 }
164165b0dc32SEd Tanous 
1642b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
164384e12cb7SAppaRao Puli                     "/redfish/v1/AccountService/Accounts/" + accountName;
1644b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Id"] = accountName;
1645b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["UserName"] = accountName;
1646b9b2e0b2SEd Tanous             },
1647b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1648b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1649b9b2e0b2SEd Tanous     }
1650a840879dSEd Tanous 
1651a840879dSEd Tanous     void doPatch(crow::Response& res, const crow::Request& req,
1652a840879dSEd Tanous                  const std::vector<std::string>& params) override
1653a840879dSEd Tanous     {
1654a840879dSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1655a840879dSEd Tanous         if (params.size() != 1)
1656a840879dSEd Tanous         {
1657f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1658a840879dSEd Tanous             return;
1659a840879dSEd Tanous         }
1660a840879dSEd Tanous 
1661a24526dcSEd Tanous         std::optional<std::string> newUserName;
1662a24526dcSEd Tanous         std::optional<std::string> password;
1663a24526dcSEd Tanous         std::optional<bool> enabled;
1664a24526dcSEd Tanous         std::optional<std::string> roleId;
166524c8542dSRatan Gupta         std::optional<bool> locked;
166684e12cb7SAppaRao Puli         if (!json_util::readJson(req, res, "UserName", newUserName, "Password",
166724c8542dSRatan Gupta                                  password, "RoleId", roleId, "Enabled", enabled,
166824c8542dSRatan Gupta                                  "Locked", locked))
1669a840879dSEd Tanous         {
1670a840879dSEd Tanous             return;
1671a840879dSEd Tanous         }
1672a840879dSEd Tanous 
167384e12cb7SAppaRao Puli         const std::string& username = params[0];
167484e12cb7SAppaRao Puli 
1675900f9497SJoseph Reynolds         // Perform a proper ConfigureSelf authority check.  If the
1676900f9497SJoseph Reynolds         // session is being used to PATCH a property other than
1677900f9497SJoseph Reynolds         // Password, then the ConfigureSelf privilege does not apply.
1678900f9497SJoseph Reynolds         // If the user is operating on an account not their own, then
1679900f9497SJoseph Reynolds         // their ConfigureSelf privilege does not apply.  In either
1680900f9497SJoseph Reynolds         // case, perform the authority check again without the user's
1681900f9497SJoseph Reynolds         // ConfigureSelf privilege.
1682900f9497SJoseph Reynolds         if ((username != req.session->username) ||
1683900f9497SJoseph Reynolds             (newUserName || enabled || roleId || locked))
1684900f9497SJoseph Reynolds         {
1685900f9497SJoseph Reynolds             if (!isAllowedWithoutConfigureSelf(req))
1686900f9497SJoseph Reynolds             {
1687900f9497SJoseph Reynolds                 BMCWEB_LOG_WARNING << "PATCH Password denied access";
1688900f9497SJoseph Reynolds                 asyncResp->res.clear();
1689900f9497SJoseph Reynolds                 messages::insufficientPrivilege(asyncResp->res);
1690900f9497SJoseph Reynolds                 return;
1691900f9497SJoseph Reynolds             }
1692900f9497SJoseph Reynolds         }
1693900f9497SJoseph Reynolds 
169466b5ca76Sjayaprakash Mutyala         // if user name is not provided in the patch method or if it
169566b5ca76Sjayaprakash Mutyala         // matches the user name in the URI, then we are treating it as updating
169666b5ca76Sjayaprakash Mutyala         // user properties other then username. If username provided doesn't
169766b5ca76Sjayaprakash Mutyala         // match the URI, then we are treating this as user rename request.
169866b5ca76Sjayaprakash Mutyala         if (!newUserName || (newUserName.value() == username))
1699a840879dSEd Tanous         {
170024c8542dSRatan Gupta             updateUserProperties(asyncResp, username, password, enabled, roleId,
170124c8542dSRatan Gupta                                  locked);
170284e12cb7SAppaRao Puli             return;
170384e12cb7SAppaRao Puli         }
170484e12cb7SAppaRao Puli         else
170584e12cb7SAppaRao Puli         {
170684e12cb7SAppaRao Puli             crow::connections::systemBus->async_method_call(
170784e12cb7SAppaRao Puli                 [this, asyncResp, username, password(std::move(password)),
170884e12cb7SAppaRao Puli                  roleId(std::move(roleId)), enabled(std::move(enabled)),
170966b5ca76Sjayaprakash Mutyala                  newUser{std::string(*newUserName)},
171066b5ca76Sjayaprakash Mutyala                  locked(std::move(locked))](const boost::system::error_code ec,
171166b5ca76Sjayaprakash Mutyala                                             sdbusplus::message::message& m) {
171284e12cb7SAppaRao Puli                     if (ec)
171384e12cb7SAppaRao Puli                     {
171466b5ca76Sjayaprakash Mutyala                         userErrorMessageHandler(m.get_error(), asyncResp,
171566b5ca76Sjayaprakash Mutyala                                                 newUser, username);
1716a840879dSEd Tanous                         return;
1717a840879dSEd Tanous                     }
1718a840879dSEd Tanous 
171984e12cb7SAppaRao Puli                     updateUserProperties(asyncResp, newUser, password, enabled,
172024c8542dSRatan Gupta                                          roleId, locked);
172184e12cb7SAppaRao Puli                 },
172284e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
172384e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "RenameUser", username,
172484e12cb7SAppaRao Puli                 *newUserName);
172584e12cb7SAppaRao Puli         }
172684e12cb7SAppaRao Puli     }
172784e12cb7SAppaRao Puli 
172884e12cb7SAppaRao Puli     void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp,
172984e12cb7SAppaRao Puli                               const std::string& username,
1730a24526dcSEd Tanous                               std::optional<std::string> password,
1731a24526dcSEd Tanous                               std::optional<bool> enabled,
173224c8542dSRatan Gupta                               std::optional<std::string> roleId,
173324c8542dSRatan Gupta                               std::optional<bool> locked)
173484e12cb7SAppaRao Puli     {
173524c8542dSRatan Gupta         std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username;
173624c8542dSRatan Gupta         dbus::utility::escapePathForDbus(dbusObjectPath);
173724c8542dSRatan Gupta 
173822c33710SRatan Gupta         dbus::utility::checkDbusPathExists(
173924c8542dSRatan Gupta             dbusObjectPath,
174024c8542dSRatan Gupta             [dbusObjectPath(std::move(dbusObjectPath)), username,
174124c8542dSRatan Gupta              password(std::move(password)), roleId(std::move(roleId)),
174224c8542dSRatan Gupta              enabled(std::move(enabled)), locked(std::move(locked)),
174324c8542dSRatan Gupta              asyncResp{std::move(asyncResp)}](int rc) {
174424c8542dSRatan Gupta                 if (!rc)
174524c8542dSRatan Gupta                 {
174666b5ca76Sjayaprakash Mutyala                     messages::resourceNotFound(
174766b5ca76Sjayaprakash Mutyala                         asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
174866b5ca76Sjayaprakash Mutyala                         username);
174924c8542dSRatan Gupta                     return;
175024c8542dSRatan Gupta                 }
175166b5ca76Sjayaprakash Mutyala 
175266b5ca76Sjayaprakash Mutyala                 if (password)
175366b5ca76Sjayaprakash Mutyala                 {
175466b5ca76Sjayaprakash Mutyala                     int retval = pamUpdatePassword(username, *password);
175566b5ca76Sjayaprakash Mutyala 
175666b5ca76Sjayaprakash Mutyala                     if (retval == PAM_USER_UNKNOWN)
175766b5ca76Sjayaprakash Mutyala                     {
175866b5ca76Sjayaprakash Mutyala                         messages::resourceNotFound(
175966b5ca76Sjayaprakash Mutyala                             asyncResp->res,
176066b5ca76Sjayaprakash Mutyala                             "#ManagerAccount.v1_0_3.ManagerAccount", username);
176166b5ca76Sjayaprakash Mutyala                     }
176266b5ca76Sjayaprakash Mutyala                     else if (retval == PAM_AUTHTOK_ERR)
176366b5ca76Sjayaprakash Mutyala                     {
176466b5ca76Sjayaprakash Mutyala                         // If password is invalid
176566b5ca76Sjayaprakash Mutyala                         messages::propertyValueFormatError(
176666b5ca76Sjayaprakash Mutyala                             asyncResp->res, *password, "Password");
176766b5ca76Sjayaprakash Mutyala                         BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
176866b5ca76Sjayaprakash Mutyala                     }
176966b5ca76Sjayaprakash Mutyala                     else if (retval != PAM_SUCCESS)
177066b5ca76Sjayaprakash Mutyala                     {
177166b5ca76Sjayaprakash Mutyala                         messages::internalError(asyncResp->res);
177266b5ca76Sjayaprakash Mutyala                         return;
177366b5ca76Sjayaprakash Mutyala                     }
177466b5ca76Sjayaprakash Mutyala                 }
177566b5ca76Sjayaprakash Mutyala 
17769712f8acSEd Tanous                 if (enabled)
1777a840879dSEd Tanous                 {
1778a840879dSEd Tanous                     crow::connections::systemBus->async_method_call(
1779a840879dSEd Tanous                         [asyncResp](const boost::system::error_code ec) {
1780a840879dSEd Tanous                             if (ec)
1781a840879dSEd Tanous                             {
178224c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
178324c8542dSRatan Gupta                                                  << ec;
1784f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
1785a840879dSEd Tanous                                 return;
1786a840879dSEd Tanous                             }
178784e12cb7SAppaRao Puli                             messages::success(asyncResp->res);
178884e12cb7SAppaRao Puli                             return;
178984e12cb7SAppaRao Puli                         },
179084e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Manager",
179124c8542dSRatan Gupta                         dbusObjectPath.c_str(),
179284e12cb7SAppaRao Puli                         "org.freedesktop.DBus.Properties", "Set",
179384e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserEnabled",
1794abf2add6SEd Tanous                         std::variant<bool>{*enabled});
179584e12cb7SAppaRao Puli                 }
179684e12cb7SAppaRao Puli 
179784e12cb7SAppaRao Puli                 if (roleId)
179884e12cb7SAppaRao Puli                 {
179954fc587aSNagaraju Goruganti                     std::string priv = getPrivilegeFromRoleId(*roleId);
180084e12cb7SAppaRao Puli                     if (priv.empty())
180184e12cb7SAppaRao Puli                     {
180224c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res,
180324c8542dSRatan Gupta                                                          *roleId, "RoleId");
180484e12cb7SAppaRao Puli                         return;
180584e12cb7SAppaRao Puli                     }
180684e12cb7SAppaRao Puli 
180784e12cb7SAppaRao Puli                     crow::connections::systemBus->async_method_call(
180884e12cb7SAppaRao Puli                         [asyncResp](const boost::system::error_code ec) {
180984e12cb7SAppaRao Puli                             if (ec)
181084e12cb7SAppaRao Puli                             {
181124c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
181224c8542dSRatan Gupta                                                  << ec;
181384e12cb7SAppaRao Puli                                 messages::internalError(asyncResp->res);
181484e12cb7SAppaRao Puli                                 return;
181584e12cb7SAppaRao Puli                             }
1816f12894f8SJason M. Bills                             messages::success(asyncResp->res);
1817a840879dSEd Tanous                         },
1818a840879dSEd Tanous                         "xyz.openbmc_project.User.Manager",
181924c8542dSRatan Gupta                         dbusObjectPath.c_str(),
1820a840879dSEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
182184e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1822abf2add6SEd Tanous                         std::variant<std::string>{priv});
1823a840879dSEd Tanous                 }
182424c8542dSRatan Gupta 
182524c8542dSRatan Gupta                 if (locked)
182624c8542dSRatan Gupta                 {
182724c8542dSRatan Gupta                     // admin can unlock the account which is locked by
182854fc587aSNagaraju Goruganti                     // successive authentication failures but admin should
182954fc587aSNagaraju Goruganti                     // not be allowed to lock an account.
183024c8542dSRatan Gupta                     if (*locked)
183124c8542dSRatan Gupta                     {
183224c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res, "true",
183324c8542dSRatan Gupta                                                          "Locked");
183424c8542dSRatan Gupta                         return;
183524c8542dSRatan Gupta                     }
183624c8542dSRatan Gupta 
183724c8542dSRatan Gupta                     crow::connections::systemBus->async_method_call(
183824c8542dSRatan Gupta                         [asyncResp](const boost::system::error_code ec) {
183924c8542dSRatan Gupta                             if (ec)
184024c8542dSRatan Gupta                             {
184124c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
184224c8542dSRatan Gupta                                                  << ec;
184324c8542dSRatan Gupta                                 messages::internalError(asyncResp->res);
184424c8542dSRatan Gupta                                 return;
184524c8542dSRatan Gupta                             }
184624c8542dSRatan Gupta                             messages::success(asyncResp->res);
184724c8542dSRatan Gupta                             return;
184824c8542dSRatan Gupta                         },
184924c8542dSRatan Gupta                         "xyz.openbmc_project.User.Manager",
185024c8542dSRatan Gupta                         dbusObjectPath.c_str(),
185124c8542dSRatan Gupta                         "org.freedesktop.DBus.Properties", "Set",
185224c8542dSRatan Gupta                         "xyz.openbmc_project.User.Attributes",
185324c8542dSRatan Gupta                         "UserLockedForFailedAttempt",
185424c8542dSRatan Gupta                         sdbusplus::message::variant<bool>{*locked});
185524c8542dSRatan Gupta                 }
185624c8542dSRatan Gupta             });
1857a840879dSEd Tanous     }
185806e086d9SEd Tanous 
185906e086d9SEd Tanous     void doDelete(crow::Response& res, const crow::Request& req,
186006e086d9SEd Tanous                   const std::vector<std::string>& params) override
186106e086d9SEd Tanous     {
186206e086d9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
186306e086d9SEd Tanous 
186406e086d9SEd Tanous         if (params.size() != 1)
186506e086d9SEd Tanous         {
1866f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
186706e086d9SEd Tanous             return;
186806e086d9SEd Tanous         }
186906e086d9SEd Tanous 
187006e086d9SEd Tanous         const std::string userPath = "/xyz/openbmc_project/user/" + params[0];
187106e086d9SEd Tanous 
187206e086d9SEd Tanous         crow::connections::systemBus->async_method_call(
187306e086d9SEd Tanous             [asyncResp, username{std::move(params[0])}](
187406e086d9SEd Tanous                 const boost::system::error_code ec) {
187506e086d9SEd Tanous                 if (ec)
187606e086d9SEd Tanous                 {
187706e086d9SEd Tanous                     messages::resourceNotFound(
1878f12894f8SJason M. Bills                         asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
1879f12894f8SJason M. Bills                         username);
188006e086d9SEd Tanous                     return;
188106e086d9SEd Tanous                 }
188206e086d9SEd Tanous 
1883f12894f8SJason M. Bills                 messages::accountRemoved(asyncResp->res);
188406e086d9SEd Tanous             },
188506e086d9SEd Tanous             "xyz.openbmc_project.User.Manager", userPath,
188606e086d9SEd Tanous             "xyz.openbmc_project.Object.Delete", "Delete");
188706e086d9SEd Tanous     }
188884e12cb7SAppaRao Puli };
188988d16c9aSLewanczyk, Dawid 
189088d16c9aSLewanczyk, Dawid } // namespace redfish
1891