xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 613dabea03cd2f5cb999be37aaf539280c63ea7a)
188d16c9aSLewanczyk, Dawid /*
288d16c9aSLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
388d16c9aSLewanczyk, Dawid //
488d16c9aSLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
588d16c9aSLewanczyk, Dawid // you may not use this file except in compliance with the License.
688d16c9aSLewanczyk, Dawid // You may obtain a copy of the License at
788d16c9aSLewanczyk, Dawid //
888d16c9aSLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
988d16c9aSLewanczyk, Dawid //
1088d16c9aSLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1188d16c9aSLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1288d16c9aSLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388d16c9aSLewanczyk, Dawid // See the License for the specific language governing permissions and
1488d16c9aSLewanczyk, Dawid // limitations under the License.
1588d16c9aSLewanczyk, Dawid */
1688d16c9aSLewanczyk, Dawid #pragma once
1788d16c9aSLewanczyk, Dawid 
187e860f15SJohn Edward Broadbent #include <app.hpp>
1924c8542dSRatan Gupta #include <dbus_utility.hpp>
2065b0dc32SEd Tanous #include <error_messages.hpp>
21b9b2e0b2SEd Tanous #include <openbmc_dbus_rest.hpp>
2252cc112dSEd Tanous #include <persistent_data.hpp>
2345ca1b86SEd Tanous #include <query.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
251e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
26d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
27d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
28a840879dSEd Tanous #include <utils/json_utils.hpp>
291214b7e7SGunnar Mills 
301abe55efSEd Tanous namespace redfish
311abe55efSEd Tanous {
3288d16c9aSLewanczyk, Dawid 
3323a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
346973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
352c70f800SEd Tanous constexpr const char* adConfigObject =
36ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
37ab828d7cSRatan Gupta 
38b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
396973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
406973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
416973a582SRatan Gupta constexpr const char* ldapConfigInterface =
426973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
436973a582SRatan Gupta constexpr const char* ldapCreateInterface =
446973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
456973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
4606785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
4706785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
486973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
496973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
506973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
516973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
526973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
536973a582SRatan Gupta 
5454fc587aSNagaraju Goruganti struct LDAPRoleMapData
5554fc587aSNagaraju Goruganti {
5654fc587aSNagaraju Goruganti     std::string groupName;
5754fc587aSNagaraju Goruganti     std::string privilege;
5854fc587aSNagaraju Goruganti };
5954fc587aSNagaraju Goruganti 
606973a582SRatan Gupta struct LDAPConfigData
616973a582SRatan Gupta {
626973a582SRatan Gupta     std::string uri{};
636973a582SRatan Gupta     std::string bindDN{};
646973a582SRatan Gupta     std::string baseDN{};
656973a582SRatan Gupta     std::string searchScope{};
666973a582SRatan Gupta     std::string serverType{};
676973a582SRatan Gupta     bool serviceEnabled = false;
686973a582SRatan Gupta     std::string userNameAttribute{};
696973a582SRatan Gupta     std::string groupAttribute{};
7054fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
716973a582SRatan Gupta };
726973a582SRatan Gupta 
7354fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7484e12cb7SAppaRao Puli {
7584e12cb7SAppaRao Puli     if (role == "priv-admin")
7684e12cb7SAppaRao Puli     {
7784e12cb7SAppaRao Puli         return "Administrator";
7884e12cb7SAppaRao Puli     }
793174e4dfSEd Tanous     if (role == "priv-user")
8084e12cb7SAppaRao Puli     {
81c80fee55SAppaRao Puli         return "ReadOnly";
8284e12cb7SAppaRao Puli     }
833174e4dfSEd Tanous     if (role == "priv-operator")
8484e12cb7SAppaRao Puli     {
8584e12cb7SAppaRao Puli         return "Operator";
8684e12cb7SAppaRao Puli     }
8726f6976fSEd Tanous     if (role.empty() || (role == "priv-noaccess"))
88e9e6d240Sjayaprakash Mutyala     {
89e9e6d240Sjayaprakash Mutyala         return "NoAccess";
90e9e6d240Sjayaprakash Mutyala     }
9184e12cb7SAppaRao Puli     return "";
9284e12cb7SAppaRao Puli }
9354fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9484e12cb7SAppaRao Puli {
9584e12cb7SAppaRao Puli     if (role == "Administrator")
9684e12cb7SAppaRao Puli     {
9784e12cb7SAppaRao Puli         return "priv-admin";
9884e12cb7SAppaRao Puli     }
993174e4dfSEd Tanous     if (role == "ReadOnly")
10084e12cb7SAppaRao Puli     {
10184e12cb7SAppaRao Puli         return "priv-user";
10284e12cb7SAppaRao Puli     }
1033174e4dfSEd Tanous     if (role == "Operator")
10484e12cb7SAppaRao Puli     {
10584e12cb7SAppaRao Puli         return "priv-operator";
10684e12cb7SAppaRao Puli     }
10726f6976fSEd Tanous     if ((role == "NoAccess") || (role.empty()))
108e9e6d240Sjayaprakash Mutyala     {
109e9e6d240Sjayaprakash Mutyala         return "priv-noaccess";
110e9e6d240Sjayaprakash Mutyala     }
11184e12cb7SAppaRao Puli     return "";
11284e12cb7SAppaRao Puli }
113b9b2e0b2SEd Tanous 
1148d1b46d7Szhanghch05 inline void userErrorMessageHandler(
1158d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1168d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
11766b5ca76Sjayaprakash Mutyala {
11866b5ca76Sjayaprakash Mutyala     if (e == nullptr)
11966b5ca76Sjayaprakash Mutyala     {
12066b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
12166b5ca76Sjayaprakash Mutyala         return;
12266b5ca76Sjayaprakash Mutyala     }
12366b5ca76Sjayaprakash Mutyala 
124055806b3SManojkiran Eda     const char* errorMessage = e->name;
12566b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
12666b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
12766b5ca76Sjayaprakash Mutyala     {
128d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
12966b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
13066b5ca76Sjayaprakash Mutyala     }
13166b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
13266b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
13366b5ca76Sjayaprakash Mutyala     {
134d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
13566b5ca76Sjayaprakash Mutyala     }
136d4d25793SEd Tanous     else if ((strcmp(errorMessage,
137d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
138d4d25793SEd Tanous               0) ||
1390fda0f12SGeorge Liu              (strcmp(
1400fda0f12SGeorge Liu                   errorMessage,
1410fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
1420fda0f12SGeorge Liu               0))
14366b5ca76Sjayaprakash Mutyala     {
14466b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
14566b5ca76Sjayaprakash Mutyala     }
14666b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
14766b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
14866b5ca76Sjayaprakash Mutyala     {
14966b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
15066b5ca76Sjayaprakash Mutyala     }
15166b5ca76Sjayaprakash Mutyala     else
15266b5ca76Sjayaprakash Mutyala     {
15366b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
15466b5ca76Sjayaprakash Mutyala     }
15566b5ca76Sjayaprakash Mutyala }
15666b5ca76Sjayaprakash Mutyala 
15781ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
158ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
159ab828d7cSRatan Gupta                                 const std::string& ldapType)
1606973a582SRatan Gupta {
161ab828d7cSRatan Gupta     std::string service =
162ab828d7cSRatan Gupta         (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService";
16354fc587aSNagaraju Goruganti 
1641476687dSEd Tanous     nlohmann::json& ldap = jsonResponse[ldapType];
16554fc587aSNagaraju Goruganti 
1661476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
1671476687dSEd Tanous     ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri});
1681476687dSEd Tanous     ldap["Authentication"]["AuthenticationType"] = "UsernameAndPassword";
1691476687dSEd Tanous     ldap["Authentication"]["Username"] = confData.bindDN;
1701476687dSEd Tanous     ldap["Authentication"]["Password"] = nullptr;
1711476687dSEd Tanous 
1721476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] =
1731476687dSEd Tanous         nlohmann::json::array({confData.baseDN});
1741476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] =
1751476687dSEd Tanous         confData.userNameAttribute;
1761476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] =
1771476687dSEd Tanous         confData.groupAttribute;
1781476687dSEd Tanous 
1791476687dSEd Tanous     nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"];
18054fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
1819eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
18254fc587aSNagaraju Goruganti     {
18354fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
18454fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
185*613dabeaSEd Tanous 
186*613dabeaSEd Tanous         nlohmann::json::array_t remoteGroupArray;
187*613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
188*613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
189*613dabeaSEd Tanous         remoteGroupArray.emplace_back(std::move(remoteGroup));
190*613dabeaSEd Tanous         roleMapArray.emplace_back(std::move(remoteGroupArray));
191*613dabeaSEd Tanous 
192*613dabeaSEd Tanous         nlohmann::json::array_t localRoleArray;
193*613dabeaSEd Tanous         nlohmann::json::object_t localRole;
194*613dabeaSEd Tanous         localRole["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
195*613dabeaSEd Tanous         localRoleArray.emplace_back(std::move(localRole));
196*613dabeaSEd Tanous         roleMapArray.emplace_back(std::move(localRoleArray));
19754fc587aSNagaraju Goruganti     }
1986973a582SRatan Gupta }
1996973a582SRatan Gupta 
2006973a582SRatan Gupta /**
20106785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
20206785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
20306785244SRatan Gupta  *
20406785244SRatan Gupta  */
20523a21a1cSEd Tanous inline void handleRoleMapPatch(
2068d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20706785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
208f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
20906785244SRatan Gupta {
21006785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
21106785244SRatan Gupta     {
212f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
21306785244SRatan Gupta 
21406785244SRatan Gupta         if (thisJson.is_null())
21506785244SRatan Gupta         {
21606785244SRatan Gupta             // delete the existing object
21706785244SRatan Gupta             if (index < roleMapObjData.size())
21806785244SRatan Gupta             {
21906785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
22006785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
22106785244SRatan Gupta                      index](const boost::system::error_code ec) {
22206785244SRatan Gupta                     if (ec)
22306785244SRatan Gupta                     {
22406785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
22506785244SRatan Gupta                         messages::internalError(asyncResp->res);
22606785244SRatan Gupta                         return;
22706785244SRatan Gupta                     }
22806785244SRatan Gupta                     asyncResp->res
22906785244SRatan Gupta                         .jsonValue[serverType]["RemoteRoleMapping"][index] =
23006785244SRatan Gupta                         nullptr;
23106785244SRatan Gupta                     },
23206785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
23306785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
23406785244SRatan Gupta             }
23506785244SRatan Gupta             else
23606785244SRatan Gupta             {
23706785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
23806785244SRatan Gupta                 messages::propertyValueTypeError(
23971f52d96SEd Tanous                     asyncResp->res,
24071f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
24171f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
24206785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
24306785244SRatan Gupta                 return;
24406785244SRatan Gupta             }
24506785244SRatan Gupta         }
24606785244SRatan Gupta         else if (thisJson.empty())
24706785244SRatan Gupta         {
24806785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
24906785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
25006785244SRatan Gupta         }
25106785244SRatan Gupta         else
25206785244SRatan Gupta         {
25306785244SRatan Gupta             // update/create the object
25406785244SRatan Gupta             std::optional<std::string> remoteGroup;
25506785244SRatan Gupta             std::optional<std::string> localRole;
25606785244SRatan Gupta 
257f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
258f23b7296SEd Tanous             // readJson is structured
259f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
260f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
261f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
262f23b7296SEd Tanous                                      localRole))
26306785244SRatan Gupta             {
26406785244SRatan Gupta                 continue;
26506785244SRatan Gupta             }
26606785244SRatan Gupta 
26706785244SRatan Gupta             // Update existing RoleMapping Object
26806785244SRatan Gupta             if (index < roleMapObjData.size())
26906785244SRatan Gupta             {
27006785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
27106785244SRatan Gupta                 // If "RemoteGroup" info is provided
27206785244SRatan Gupta                 if (remoteGroup)
27306785244SRatan Gupta                 {
27406785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
27506785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
27606785244SRatan Gupta                          remoteGroup](const boost::system::error_code ec) {
27706785244SRatan Gupta                         if (ec)
27806785244SRatan Gupta                         {
279002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
28006785244SRatan Gupta                             messages::internalError(asyncResp->res);
28106785244SRatan Gupta                             return;
28206785244SRatan Gupta                         }
28306785244SRatan Gupta                         asyncResp->res
284002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
285002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
28606785244SRatan Gupta                         },
28706785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
28806785244SRatan Gupta                         propertyInterface, "Set",
28906785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
29006785244SRatan Gupta                         "GroupName",
291168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
292168e20c1SEd Tanous                             std::move(*remoteGroup)));
29306785244SRatan Gupta                 }
29406785244SRatan Gupta 
29506785244SRatan Gupta                 // If "LocalRole" info is provided
29606785244SRatan Gupta                 if (localRole)
29706785244SRatan Gupta                 {
29806785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
29906785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
30006785244SRatan Gupta                          localRole](const boost::system::error_code ec) {
30106785244SRatan Gupta                         if (ec)
30206785244SRatan Gupta                         {
303002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
30406785244SRatan Gupta                             messages::internalError(asyncResp->res);
30506785244SRatan Gupta                             return;
30606785244SRatan Gupta                         }
30706785244SRatan Gupta                         asyncResp->res
308002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
309002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
31006785244SRatan Gupta                         },
31106785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
31206785244SRatan Gupta                         propertyInterface, "Set",
31306785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
31406785244SRatan Gupta                         "Privilege",
315168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
31606785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
31706785244SRatan Gupta                 }
31806785244SRatan Gupta             }
31906785244SRatan Gupta             // Create a new RoleMapping Object.
32006785244SRatan Gupta             else
32106785244SRatan Gupta             {
32206785244SRatan Gupta                 BMCWEB_LOG_DEBUG
32306785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
32406785244SRatan Gupta                 std::string pathString =
32506785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index);
32606785244SRatan Gupta 
32706785244SRatan Gupta                 if (!localRole)
32806785244SRatan Gupta                 {
32906785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
33006785244SRatan Gupta                                               pathString + "/LocalRole");
33106785244SRatan Gupta                     continue;
33206785244SRatan Gupta                 }
33306785244SRatan Gupta                 if (!remoteGroup)
33406785244SRatan Gupta                 {
33506785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
33606785244SRatan Gupta                                               pathString + "/RemoteGroup");
33706785244SRatan Gupta                     continue;
33806785244SRatan Gupta                 }
33906785244SRatan Gupta 
34006785244SRatan Gupta                 std::string dbusObjectPath;
34106785244SRatan Gupta                 if (serverType == "ActiveDirectory")
34206785244SRatan Gupta                 {
3432c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
34406785244SRatan Gupta                 }
34506785244SRatan Gupta                 else if (serverType == "LDAP")
34606785244SRatan Gupta                 {
34723a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
34806785244SRatan Gupta                 }
34906785244SRatan Gupta 
35006785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
35106785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
35206785244SRatan Gupta 
35306785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
354271584abSEd Tanous                     [asyncResp, serverType, localRole,
35506785244SRatan Gupta                      remoteGroup](const boost::system::error_code ec) {
35606785244SRatan Gupta                     if (ec)
35706785244SRatan Gupta                     {
35806785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
35906785244SRatan Gupta                         messages::internalError(asyncResp->res);
36006785244SRatan Gupta                         return;
36106785244SRatan Gupta                     }
36206785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
36306785244SRatan Gupta                         asyncResp->res
36406785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
3651476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
3661476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
3671476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
3681476687dSEd Tanous                     remoteRoleJson.push_back(std::move(roleMapEntry));
36906785244SRatan Gupta                     },
37006785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
3713174e4dfSEd Tanous                     "Create", *remoteGroup,
37206785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
37306785244SRatan Gupta             }
37406785244SRatan Gupta         }
37506785244SRatan Gupta     }
37606785244SRatan Gupta }
37706785244SRatan Gupta 
37806785244SRatan Gupta /**
3796973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
3806973a582SRatan Gupta  * into JSON
3816973a582SRatan Gupta  */
3826973a582SRatan Gupta template <typename CallbackFunc>
3836973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
3846973a582SRatan Gupta                               CallbackFunc&& callback)
3856973a582SRatan Gupta {
38654fc587aSNagaraju Goruganti 
38754fc587aSNagaraju Goruganti     const std::array<const char*, 2> interfaces = {ldapEnableInterface,
38854fc587aSNagaraju Goruganti                                                    ldapConfigInterface};
38954fc587aSNagaraju Goruganti 
39054fc587aSNagaraju Goruganti     crow::connections::systemBus->async_method_call(
39154fc587aSNagaraju Goruganti         [callback, ldapType](const boost::system::error_code ec,
392b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
39354fc587aSNagaraju Goruganti         if (ec || resp.empty())
39454fc587aSNagaraju Goruganti         {
3950fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
396002d39b4SEd Tanous                 << "DBUS response error during getting of service name: " << ec;
39723a21a1cSEd Tanous             LDAPConfigData empty{};
39823a21a1cSEd Tanous             callback(false, empty, ldapType);
39954fc587aSNagaraju Goruganti             return;
40054fc587aSNagaraju Goruganti         }
40154fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
40254fc587aSNagaraju Goruganti         crow::connections::systemBus->async_method_call(
403002d39b4SEd Tanous             [callback,
404002d39b4SEd Tanous              ldapType](const boost::system::error_code errorCode,
405711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
4066973a582SRatan Gupta             LDAPConfigData confData{};
40781ce609eSEd Tanous             if (errorCode)
4086973a582SRatan Gupta             {
409ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
410002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode;
4116973a582SRatan Gupta                 return;
4126973a582SRatan Gupta             }
413ab828d7cSRatan Gupta 
414ab828d7cSRatan Gupta             std::string ldapDbusType;
41554fc587aSNagaraju Goruganti             std::string searchString;
41654fc587aSNagaraju Goruganti 
417ab828d7cSRatan Gupta             if (ldapType == "LDAP")
418ab828d7cSRatan Gupta             {
4190fda0f12SGeorge Liu                 ldapDbusType =
4200fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
42154fc587aSNagaraju Goruganti                 searchString = "openldap";
422ab828d7cSRatan Gupta             }
423ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
424ab828d7cSRatan Gupta             {
42554fc587aSNagaraju Goruganti                 ldapDbusType =
4260fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
42754fc587aSNagaraju Goruganti                 searchString = "active_directory";
428ab828d7cSRatan Gupta             }
429ab828d7cSRatan Gupta             else
430ab828d7cSRatan Gupta             {
431002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type="
432ab828d7cSRatan Gupta                                  << ldapType;
433ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
434ab828d7cSRatan Gupta                 return;
435ab828d7cSRatan Gupta             }
436ab828d7cSRatan Gupta 
437ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
438ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
439ab828d7cSRatan Gupta 
4406973a582SRatan Gupta             for (const auto& object : ldapObjects)
4416973a582SRatan Gupta             {
44254fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
44354fc587aSNagaraju Goruganti                 // given type
444002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
4456973a582SRatan Gupta                 {
446ab828d7cSRatan Gupta                     continue;
447ab828d7cSRatan Gupta                 }
448ab828d7cSRatan Gupta 
4496973a582SRatan Gupta                 for (const auto& interface : object.second)
4506973a582SRatan Gupta                 {
4516973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
4526973a582SRatan Gupta                     {
4536973a582SRatan Gupta                         // rest of the properties are string.
4546973a582SRatan Gupta                         for (const auto& property : interface.second)
4556973a582SRatan Gupta                         {
4566973a582SRatan Gupta                             if (property.first == "Enabled")
4576973a582SRatan Gupta                             {
4586973a582SRatan Gupta                                 const bool* value =
4596973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
4606973a582SRatan Gupta                                 if (value == nullptr)
4616973a582SRatan Gupta                                 {
4626973a582SRatan Gupta                                     continue;
4636973a582SRatan Gupta                                 }
4646973a582SRatan Gupta                                 confData.serviceEnabled = *value;
4656973a582SRatan Gupta                                 break;
4666973a582SRatan Gupta                             }
4676973a582SRatan Gupta                         }
4686973a582SRatan Gupta                     }
4696973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
4706973a582SRatan Gupta                     {
4716973a582SRatan Gupta 
4726973a582SRatan Gupta                         for (const auto& property : interface.second)
4736973a582SRatan Gupta                         {
474271584abSEd Tanous                             const std::string* strValue =
475002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
476271584abSEd Tanous                             if (strValue == nullptr)
4776973a582SRatan Gupta                             {
4786973a582SRatan Gupta                                 continue;
4796973a582SRatan Gupta                             }
4806973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
4816973a582SRatan Gupta                             {
482271584abSEd Tanous                                 confData.uri = *strValue;
4836973a582SRatan Gupta                             }
4846973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
4856973a582SRatan Gupta                             {
486271584abSEd Tanous                                 confData.bindDN = *strValue;
4876973a582SRatan Gupta                             }
4886973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
4896973a582SRatan Gupta                             {
490271584abSEd Tanous                                 confData.baseDN = *strValue;
4916973a582SRatan Gupta                             }
492002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
4936973a582SRatan Gupta                             {
494271584abSEd Tanous                                 confData.searchScope = *strValue;
4956973a582SRatan Gupta                             }
496002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
4976973a582SRatan Gupta                             {
498271584abSEd Tanous                                 confData.groupAttribute = *strValue;
4996973a582SRatan Gupta                             }
500002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
5016973a582SRatan Gupta                             {
502271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
5036973a582SRatan Gupta                             }
50454fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
505ab828d7cSRatan Gupta                             {
506271584abSEd Tanous                                 confData.serverType = *strValue;
50754fc587aSNagaraju Goruganti                             }
50854fc587aSNagaraju Goruganti                         }
50954fc587aSNagaraju Goruganti                     }
510002d39b4SEd Tanous                     else if (interface.first ==
5110fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
51254fc587aSNagaraju Goruganti                     {
51354fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
51454fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
51554fc587aSNagaraju Goruganti                         {
516271584abSEd Tanous                             const std::string* strValue =
517002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
51854fc587aSNagaraju Goruganti 
519271584abSEd Tanous                             if (strValue == nullptr)
52054fc587aSNagaraju Goruganti                             {
52154fc587aSNagaraju Goruganti                                 continue;
52254fc587aSNagaraju Goruganti                             }
52354fc587aSNagaraju Goruganti 
52454fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
52554fc587aSNagaraju Goruganti                             {
526271584abSEd Tanous                                 roleMapData.groupName = *strValue;
52754fc587aSNagaraju Goruganti                             }
52854fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
52954fc587aSNagaraju Goruganti                             {
530271584abSEd Tanous                                 roleMapData.privilege = *strValue;
53154fc587aSNagaraju Goruganti                             }
53254fc587aSNagaraju Goruganti                         }
53354fc587aSNagaraju Goruganti 
534002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
535002d39b4SEd Tanous                                                             roleMapData);
53654fc587aSNagaraju Goruganti                     }
53754fc587aSNagaraju Goruganti                 }
53854fc587aSNagaraju Goruganti             }
539ab828d7cSRatan Gupta             callback(true, confData, ldapType);
54054fc587aSNagaraju Goruganti             },
541002d39b4SEd Tanous             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
54254fc587aSNagaraju Goruganti         },
54354fc587aSNagaraju Goruganti         mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
54423a21a1cSEd Tanous         ldapConfigObjectName, interfaces);
5456973a582SRatan Gupta }
5466973a582SRatan Gupta 
5478a07d286SRatan Gupta /**
5488a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
5498a07d286SRatan Gupta  * @param input JSON data
5508a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5518a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
5528a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
5538a07d286SRatan Gupta  */
5544f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
5556c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5566c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
5578a07d286SRatan Gupta {
5588a07d286SRatan Gupta     std::optional<std::string> authType;
5598a07d286SRatan Gupta 
5608a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
5618a07d286SRatan Gupta                              authType, "Username", username, "Password",
5628a07d286SRatan Gupta                              password))
5638a07d286SRatan Gupta     {
5648a07d286SRatan Gupta         return;
5658a07d286SRatan Gupta     }
5668a07d286SRatan Gupta     if (!authType)
5678a07d286SRatan Gupta     {
5688a07d286SRatan Gupta         return;
5698a07d286SRatan Gupta     }
5708a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
5718a07d286SRatan Gupta     {
5728a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
5738a07d286SRatan Gupta                                          "AuthenticationType");
5748a07d286SRatan Gupta         return;
5758a07d286SRatan Gupta     }
5768a07d286SRatan Gupta }
5778a07d286SRatan Gupta /**
5788a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
5798a07d286SRatan Gupta  * @param input JSON data
5808a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5818a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
5828a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
5838a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
5848a07d286SRatan Gupta  */
5858a07d286SRatan Gupta 
5864f48d5f6SEd Tanous inline void
5874f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
5888d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5898a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
5908a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
5918a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
5928a07d286SRatan Gupta {
5938a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
5948a07d286SRatan Gupta 
5958a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
5968a07d286SRatan Gupta                              searchSettings))
5978a07d286SRatan Gupta     {
5988a07d286SRatan Gupta         return;
5998a07d286SRatan Gupta     }
6008a07d286SRatan Gupta     if (!searchSettings)
6018a07d286SRatan Gupta     {
6028a07d286SRatan Gupta         return;
6038a07d286SRatan Gupta     }
6048a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
6058a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
6068a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
6078a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
6088a07d286SRatan Gupta     {
6098a07d286SRatan Gupta         return;
6108a07d286SRatan Gupta     }
6118a07d286SRatan Gupta }
6128a07d286SRatan Gupta /**
6138a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6148a07d286SRatan Gupta           json response with the new value.
6158a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6168a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6178a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6188a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6198a07d286SRatan Gupta  */
6208a07d286SRatan Gupta 
6214f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6228a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6238d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6248a07d286SRatan Gupta     const std::string& ldapServerElementName,
6258a07d286SRatan Gupta     const std::string& ldapConfigObject)
6268a07d286SRatan Gupta {
6278a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6288a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
6298a07d286SRatan Gupta          serviceAddressList](const boost::system::error_code ec) {
6308a07d286SRatan Gupta         if (ec)
6318a07d286SRatan Gupta         {
6328a07d286SRatan Gupta             BMCWEB_LOG_DEBUG
633c61704abSGunnar Mills                 << "Error Occurred in updating the service address";
6348a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6358a07d286SRatan Gupta             return;
6368a07d286SRatan Gupta         }
6378a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
6388a07d286SRatan Gupta             serviceAddressList.front()};
639002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
6408a07d286SRatan Gupta             modifiedserviceAddressList;
6418a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
6428a07d286SRatan Gupta         {
643002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
6448a07d286SRatan Gupta                                             serviceAddressList.front());
6458a07d286SRatan Gupta         }
6468a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
6478a07d286SRatan Gupta         },
6488a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6498a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
650168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
6518a07d286SRatan Gupta }
6528a07d286SRatan Gupta /**
6538a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
6548a07d286SRatan Gupta           json response with the new value.
6558a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
6568a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6578a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6588a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6598a07d286SRatan Gupta  */
6608a07d286SRatan Gupta 
6614f48d5f6SEd Tanous inline void
6624f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
6638d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6648a07d286SRatan Gupta                         const std::string& ldapServerElementName,
6658a07d286SRatan Gupta                         const std::string& ldapConfigObject)
6668a07d286SRatan Gupta {
6678a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6688a07d286SRatan Gupta         [asyncResp, username,
6698a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
6708a07d286SRatan Gupta         if (ec)
6718a07d286SRatan Gupta         {
6726c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
6738a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6748a07d286SRatan Gupta             return;
6758a07d286SRatan Gupta         }
676002d39b4SEd Tanous         asyncResp->res
677002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Username"] =
678002d39b4SEd Tanous             username;
6798a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
6808a07d286SRatan Gupta         },
6818a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
682168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
683168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
6848a07d286SRatan Gupta }
6858a07d286SRatan Gupta 
6868a07d286SRatan Gupta /**
6878a07d286SRatan Gupta  * @brief updates the LDAP password
6888a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
6898a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6908a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6918a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
6928a07d286SRatan Gupta  */
6938a07d286SRatan Gupta 
6944f48d5f6SEd Tanous inline void
6954f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
6968d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6978a07d286SRatan Gupta                         const std::string& ldapServerElementName,
6988a07d286SRatan Gupta                         const std::string& ldapConfigObject)
6998a07d286SRatan Gupta {
7008a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7018a07d286SRatan Gupta         [asyncResp, password,
7028a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7038a07d286SRatan Gupta         if (ec)
7048a07d286SRatan Gupta         {
7056c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
7068a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7078a07d286SRatan Gupta             return;
7088a07d286SRatan Gupta         }
709002d39b4SEd Tanous         asyncResp->res
710002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Password"] =
711002d39b4SEd Tanous             "";
7128a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
7138a07d286SRatan Gupta         },
7148a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7158a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
716168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
7178a07d286SRatan Gupta }
7188a07d286SRatan Gupta 
7198a07d286SRatan Gupta /**
7208a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7218a07d286SRatan Gupta           json response with the new value.
7228a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7238a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7248a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7258a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7268a07d286SRatan Gupta  */
7278a07d286SRatan Gupta 
7284f48d5f6SEd Tanous inline void
7294f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7308d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7318a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7328a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7338a07d286SRatan Gupta {
7348a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7358a07d286SRatan Gupta         [asyncResp, baseDNList,
7368a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7378a07d286SRatan Gupta         if (ec)
7388a07d286SRatan Gupta         {
7396c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
7408a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7418a07d286SRatan Gupta             return;
7428a07d286SRatan Gupta         }
743002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7448a07d286SRatan Gupta         auto& searchSettingsJson =
7458a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7466c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
7476c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
7488a07d286SRatan Gupta         if (baseDNList.size() > 1)
7498a07d286SRatan Gupta         {
750002d39b4SEd Tanous             messages::propertyValueModified(
751002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
7528a07d286SRatan Gupta         }
7538a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
7548a07d286SRatan Gupta         },
7558a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7568a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
757168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
7588a07d286SRatan Gupta }
7598a07d286SRatan Gupta /**
7608a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
7618a07d286SRatan Gupta           json response with the new value.
7628a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
7638a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7648a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7658a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7668a07d286SRatan Gupta  */
7678a07d286SRatan Gupta 
7684f48d5f6SEd Tanous inline void
7694f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
7708d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7718a07d286SRatan Gupta                             const std::string& ldapServerElementName,
7728a07d286SRatan Gupta                             const std::string& ldapConfigObject)
7738a07d286SRatan Gupta {
7748a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7758a07d286SRatan Gupta         [asyncResp, userNameAttribute,
7768a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7778a07d286SRatan Gupta         if (ec)
7788a07d286SRatan Gupta         {
779c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
7808a07d286SRatan Gupta                                 "username attribute";
7818a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7828a07d286SRatan Gupta             return;
7838a07d286SRatan Gupta         }
784002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7858a07d286SRatan Gupta         auto& searchSettingsJson =
7868a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7878a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
7888a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
7898a07d286SRatan Gupta         },
7908a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7918a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
792168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
7938a07d286SRatan Gupta }
7948a07d286SRatan Gupta /**
7958a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
7968a07d286SRatan Gupta           json response with the new value.
7978a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
7988a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7998a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8008a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8018a07d286SRatan Gupta  */
8028a07d286SRatan Gupta 
8034f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
8048d1b46d7Szhanghch05     const std::string& groupsAttribute,
8058d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8068a07d286SRatan Gupta     const std::string& ldapServerElementName,
8078a07d286SRatan Gupta     const std::string& ldapConfigObject)
8088a07d286SRatan Gupta {
8098a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8108a07d286SRatan Gupta         [asyncResp, groupsAttribute,
8118a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8128a07d286SRatan Gupta         if (ec)
8138a07d286SRatan Gupta         {
814c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8158a07d286SRatan Gupta                                 "groupname attribute";
8168a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8178a07d286SRatan Gupta             return;
8188a07d286SRatan Gupta         }
819002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8208a07d286SRatan Gupta         auto& searchSettingsJson =
8218a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8228a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8238a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8248a07d286SRatan Gupta         },
8258a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8268a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
827168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
8288a07d286SRatan Gupta }
8298a07d286SRatan Gupta /**
8308a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
8318a07d286SRatan Gupta           json response with the new value.
8328a07d286SRatan Gupta  * @param input JSON data.
8338a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8348a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8358a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8368a07d286SRatan Gupta  */
8378a07d286SRatan Gupta 
8384f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8396c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8408a07d286SRatan Gupta     const std::string& ldapServerElementName,
8418a07d286SRatan Gupta     const std::string& ldapConfigObject)
8428a07d286SRatan Gupta {
8438a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8448a07d286SRatan Gupta         [asyncResp, serviceEnabled,
8458a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8468a07d286SRatan Gupta         if (ec)
8478a07d286SRatan Gupta         {
848002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
8498a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8508a07d286SRatan Gupta             return;
8518a07d286SRatan Gupta         }
8526c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
8538a07d286SRatan Gupta             serviceEnabled;
8546c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
8558a07d286SRatan Gupta         },
8568a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
857168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
858168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
8598a07d286SRatan Gupta }
8608a07d286SRatan Gupta 
8614f48d5f6SEd Tanous inline void
8624f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
8638d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
86478158631SZbigniew Kurzynski {
86578158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
86678158631SZbigniew Kurzynski     std::optional<bool> cookie;
86778158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
86878158631SZbigniew Kurzynski     std::optional<bool> xToken;
869501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
87078158631SZbigniew Kurzynski 
87178158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
87278158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
873501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
87478158631SZbigniew Kurzynski     {
87578158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
87678158631SZbigniew Kurzynski         return;
87778158631SZbigniew Kurzynski     }
87878158631SZbigniew Kurzynski 
87978158631SZbigniew Kurzynski     // Make a copy of methods configuration
88052cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
88152cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
88278158631SZbigniew Kurzynski 
88378158631SZbigniew Kurzynski     if (basicAuth)
88478158631SZbigniew Kurzynski     {
885f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
886f16f6263SAlan Kuo         messages::actionNotSupported(
8870fda0f12SGeorge Liu             asyncResp->res,
8880fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
889f16f6263SAlan Kuo         return;
890f16f6263SAlan Kuo #endif
89178158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
89278158631SZbigniew Kurzynski     }
89378158631SZbigniew Kurzynski 
89478158631SZbigniew Kurzynski     if (cookie)
89578158631SZbigniew Kurzynski     {
896f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
8970fda0f12SGeorge Liu         messages::actionNotSupported(
8980fda0f12SGeorge Liu             asyncResp->res,
8990fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
900f16f6263SAlan Kuo         return;
901f16f6263SAlan Kuo #endif
90278158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
90378158631SZbigniew Kurzynski     }
90478158631SZbigniew Kurzynski 
90578158631SZbigniew Kurzynski     if (sessionToken)
90678158631SZbigniew Kurzynski     {
907f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
908f16f6263SAlan Kuo         messages::actionNotSupported(
9090fda0f12SGeorge Liu             asyncResp->res,
9100fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
911f16f6263SAlan Kuo         return;
912f16f6263SAlan Kuo #endif
91378158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
91478158631SZbigniew Kurzynski     }
91578158631SZbigniew Kurzynski 
91678158631SZbigniew Kurzynski     if (xToken)
91778158631SZbigniew Kurzynski     {
918f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
9190fda0f12SGeorge Liu         messages::actionNotSupported(
9200fda0f12SGeorge Liu             asyncResp->res,
9210fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
922f16f6263SAlan Kuo         return;
923f16f6263SAlan Kuo #endif
92478158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
92578158631SZbigniew Kurzynski     }
92678158631SZbigniew Kurzynski 
927501f1e58SZbigniew Kurzynski     if (tls)
928501f1e58SZbigniew Kurzynski     {
929f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
9300fda0f12SGeorge Liu         messages::actionNotSupported(
9310fda0f12SGeorge Liu             asyncResp->res,
9320fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
933f16f6263SAlan Kuo         return;
934f16f6263SAlan Kuo #endif
935501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
936501f1e58SZbigniew Kurzynski     }
937501f1e58SZbigniew Kurzynski 
93878158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
939501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
940501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
94178158631SZbigniew Kurzynski     {
94278158631SZbigniew Kurzynski         // Do not allow user to disable everything
94378158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
94478158631SZbigniew Kurzynski                                      "of disabling all available methods");
94578158631SZbigniew Kurzynski         return;
94678158631SZbigniew Kurzynski     }
94778158631SZbigniew Kurzynski 
94852cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
94952cc112dSEd Tanous         authMethodsConfig);
95078158631SZbigniew Kurzynski     // Save configuration immediately
95152cc112dSEd Tanous     persistent_data::getConfig().writeData();
95278158631SZbigniew Kurzynski 
95378158631SZbigniew Kurzynski     messages::success(asyncResp->res);
95478158631SZbigniew Kurzynski }
95578158631SZbigniew Kurzynski 
9568a07d286SRatan Gupta /**
9578a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
9588a07d286SRatan Gupta  *        value and create the LDAP config object.
9598a07d286SRatan Gupta  * @param input JSON data
9608a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9618a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
9628a07d286SRatan Gupta  */
9638a07d286SRatan Gupta 
9646c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
9658d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9668a07d286SRatan Gupta                             const std::string& serverType)
9678a07d286SRatan Gupta {
968eb2bbe56SRatan Gupta     std::string dbusObjectPath;
969eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
970eb2bbe56SRatan Gupta     {
9712c70f800SEd Tanous         dbusObjectPath = adConfigObject;
972eb2bbe56SRatan Gupta     }
973eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
974eb2bbe56SRatan Gupta     {
97523a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
976eb2bbe56SRatan Gupta     }
977cb13a392SEd Tanous     else
978cb13a392SEd Tanous     {
979cb13a392SEd Tanous         return;
980cb13a392SEd Tanous     }
981eb2bbe56SRatan Gupta 
9828a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
9838a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
9848a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
9858a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
9868a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
9878a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
9888a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
9898a07d286SRatan Gupta     std::optional<std::string> userName;
9908a07d286SRatan Gupta     std::optional<std::string> password;
99106785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
9928a07d286SRatan Gupta 
9938a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
9948a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
9958a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
99606785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
99706785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
9988a07d286SRatan Gupta     {
9998a07d286SRatan Gupta         return;
10008a07d286SRatan Gupta     }
10018a07d286SRatan Gupta 
10028a07d286SRatan Gupta     if (authentication)
10038a07d286SRatan Gupta     {
10048a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
10058a07d286SRatan Gupta                                     password);
10068a07d286SRatan Gupta     }
10078a07d286SRatan Gupta     if (ldapService)
10088a07d286SRatan Gupta     {
10098a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10108a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
10118a07d286SRatan Gupta     }
10128a07d286SRatan Gupta     if (serviceAddressList)
10138a07d286SRatan Gupta     {
101426f6976fSEd Tanous         if (serviceAddressList->empty())
10158a07d286SRatan Gupta         {
10168a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10178a07d286SRatan Gupta                                              "ServiceAddress");
10188a07d286SRatan Gupta             return;
10198a07d286SRatan Gupta         }
10208a07d286SRatan Gupta     }
10218a07d286SRatan Gupta     if (baseDNList)
10228a07d286SRatan Gupta     {
102326f6976fSEd Tanous         if (baseDNList->empty())
10248a07d286SRatan Gupta         {
10258a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10268a07d286SRatan Gupta                                              "BaseDistinguishedNames");
10278a07d286SRatan Gupta             return;
10288a07d286SRatan Gupta         }
10298a07d286SRatan Gupta     }
10308a07d286SRatan Gupta 
10318a07d286SRatan Gupta     // nothing to update, then return
10328a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
103306785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
103406785244SRatan Gupta         !remoteRoleMapData)
10358a07d286SRatan Gupta     {
10368a07d286SRatan Gupta         return;
10378a07d286SRatan Gupta     }
10388a07d286SRatan Gupta 
10398a07d286SRatan Gupta     // Get the existing resource first then keep modifying
10408a07d286SRatan Gupta     // whenever any property gets updated.
1041002d39b4SEd Tanous     getLDAPConfigData(
1042002d39b4SEd Tanous         serverType,
1043002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1044002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1045002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
104623a21a1cSEd Tanous                             const std::string& serverT) {
10478a07d286SRatan Gupta         if (!success)
10488a07d286SRatan Gupta         {
10498a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10508a07d286SRatan Gupta             return;
10518a07d286SRatan Gupta         }
10526c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
10538a07d286SRatan Gupta         if (confData.serviceEnabled)
10548a07d286SRatan Gupta         {
10558a07d286SRatan Gupta             // Disable the service first and update the rest of
10568a07d286SRatan Gupta             // the properties.
10576c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
10588a07d286SRatan Gupta         }
10598a07d286SRatan Gupta 
10608a07d286SRatan Gupta         if (serviceAddressList)
10618a07d286SRatan Gupta         {
10626c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
10636c51eab1SEd Tanous                                       dbusObjectPath);
10648a07d286SRatan Gupta         }
10658a07d286SRatan Gupta         if (userName)
10668a07d286SRatan Gupta         {
10676c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
10688a07d286SRatan Gupta         }
10698a07d286SRatan Gupta         if (password)
10708a07d286SRatan Gupta         {
10716c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
10728a07d286SRatan Gupta         }
10738a07d286SRatan Gupta 
10748a07d286SRatan Gupta         if (baseDNList)
10758a07d286SRatan Gupta         {
10766c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
10778a07d286SRatan Gupta         }
10788a07d286SRatan Gupta         if (userNameAttribute)
10798a07d286SRatan Gupta         {
10806c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
10816c51eab1SEd Tanous                                     dbusObjectPath);
10828a07d286SRatan Gupta         }
10838a07d286SRatan Gupta         if (groupsAttribute)
10848a07d286SRatan Gupta         {
10856c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
10866c51eab1SEd Tanous                                      dbusObjectPath);
10878a07d286SRatan Gupta         }
10888a07d286SRatan Gupta         if (serviceEnabled)
10898a07d286SRatan Gupta         {
10908a07d286SRatan Gupta             // if user has given the value as true then enable
10918a07d286SRatan Gupta             // the service. if user has given false then no-op
10928a07d286SRatan Gupta             // as service is already stopped.
10938a07d286SRatan Gupta             if (*serviceEnabled)
10948a07d286SRatan Gupta             {
10956c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
10966c51eab1SEd Tanous                                          dbusObjectPath);
10978a07d286SRatan Gupta             }
10988a07d286SRatan Gupta         }
10998a07d286SRatan Gupta         else
11008a07d286SRatan Gupta         {
11018a07d286SRatan Gupta             // if user has not given the service enabled value
11028a07d286SRatan Gupta             // then revert it to the same state as it was
11038a07d286SRatan Gupta             // before.
11048a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
110523a21a1cSEd Tanous                                      serverT, dbusObjectPath);
11068a07d286SRatan Gupta         }
110706785244SRatan Gupta 
110806785244SRatan Gupta         if (remoteRoleMapData)
110906785244SRatan Gupta         {
11106c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
11116c51eab1SEd Tanous                                *remoteRoleMapData);
111206785244SRatan Gupta         }
11138a07d286SRatan Gupta         });
11148a07d286SRatan Gupta }
1115d4b5443fSEd Tanous 
11166c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
11176c51eab1SEd Tanous                                  const std::string& username,
11186c51eab1SEd Tanous                                  std::optional<std::string> password,
11196c51eab1SEd Tanous                                  std::optional<bool> enabled,
11206c51eab1SEd Tanous                                  std::optional<std::string> roleId,
11216c51eab1SEd Tanous                                  std::optional<bool> locked)
11221abe55efSEd Tanous {
1123b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1124b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1125b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
11266c51eab1SEd Tanous 
11276c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
11286c51eab1SEd Tanous         dbusObjectPath,
11291106333aSEd Tanous         [dbusObjectPath, username, password(std::move(password)),
11301106333aSEd Tanous          roleId(std::move(roleId)), enabled, locked,
11311106333aSEd Tanous          asyncResp{std::move(asyncResp)}](int rc) {
1132e662eae8SEd Tanous         if (rc <= 0)
11336c51eab1SEd Tanous         {
1134d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11356c51eab1SEd Tanous                                        username);
11366c51eab1SEd Tanous             return;
11376c51eab1SEd Tanous         }
11386c51eab1SEd Tanous 
11396c51eab1SEd Tanous         if (password)
11406c51eab1SEd Tanous         {
11416c51eab1SEd Tanous             int retval = pamUpdatePassword(username, *password);
11426c51eab1SEd Tanous 
11436c51eab1SEd Tanous             if (retval == PAM_USER_UNKNOWN)
11446c51eab1SEd Tanous             {
1145d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11466c51eab1SEd Tanous                                            username);
11476c51eab1SEd Tanous             }
11486c51eab1SEd Tanous             else if (retval == PAM_AUTHTOK_ERR)
11496c51eab1SEd Tanous             {
11506c51eab1SEd Tanous                 // If password is invalid
1151002d39b4SEd Tanous                 messages::propertyValueFormatError(asyncResp->res, *password,
1152002d39b4SEd Tanous                                                    "Password");
11536c51eab1SEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
11546c51eab1SEd Tanous             }
11556c51eab1SEd Tanous             else if (retval != PAM_SUCCESS)
11566c51eab1SEd Tanous             {
11576c51eab1SEd Tanous                 messages::internalError(asyncResp->res);
11586c51eab1SEd Tanous                 return;
11596c51eab1SEd Tanous             }
1160e7b1b62bSEd Tanous             else
1161e7b1b62bSEd Tanous             {
1162e7b1b62bSEd Tanous                 messages::success(asyncResp->res);
1163e7b1b62bSEd Tanous             }
11646c51eab1SEd Tanous         }
11656c51eab1SEd Tanous 
11666c51eab1SEd Tanous         if (enabled)
11676c51eab1SEd Tanous         {
11686c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
11696c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
11706c51eab1SEd Tanous                 if (ec)
11716c51eab1SEd Tanous                 {
11726c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
11736c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11746c51eab1SEd Tanous                     return;
11756c51eab1SEd Tanous                 }
11766c51eab1SEd Tanous                 messages::success(asyncResp->res);
11776c51eab1SEd Tanous                 return;
11786c51eab1SEd Tanous                 },
1179e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
11806c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
11816c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserEnabled",
1182168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*enabled});
11836c51eab1SEd Tanous         }
11846c51eab1SEd Tanous 
11856c51eab1SEd Tanous         if (roleId)
11866c51eab1SEd Tanous         {
11876c51eab1SEd Tanous             std::string priv = getPrivilegeFromRoleId(*roleId);
11886c51eab1SEd Tanous             if (priv.empty())
11896c51eab1SEd Tanous             {
11906c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, *roleId,
11916c51eab1SEd Tanous                                                  "RoleId");
11926c51eab1SEd Tanous                 return;
11936c51eab1SEd Tanous             }
11946c51eab1SEd Tanous             if (priv == "priv-noaccess")
11956c51eab1SEd Tanous             {
11966c51eab1SEd Tanous                 priv = "";
11976c51eab1SEd Tanous             }
11986c51eab1SEd Tanous 
11996c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
12006c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
12016c51eab1SEd Tanous                 if (ec)
12026c51eab1SEd Tanous                 {
12036c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12046c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12056c51eab1SEd Tanous                     return;
12066c51eab1SEd Tanous                 }
12076c51eab1SEd Tanous                 messages::success(asyncResp->res);
12086c51eab1SEd Tanous                 },
1209e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12106c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12116c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1212168e20c1SEd Tanous                 dbus::utility::DbusVariantType{priv});
12136c51eab1SEd Tanous         }
12146c51eab1SEd Tanous 
12156c51eab1SEd Tanous         if (locked)
12166c51eab1SEd Tanous         {
12176c51eab1SEd Tanous             // admin can unlock the account which is locked by
12186c51eab1SEd Tanous             // successive authentication failures but admin should
12196c51eab1SEd Tanous             // not be allowed to lock an account.
12206c51eab1SEd Tanous             if (*locked)
12216c51eab1SEd Tanous             {
12226c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, "true",
12236c51eab1SEd Tanous                                                  "Locked");
12246c51eab1SEd Tanous                 return;
12256c51eab1SEd Tanous             }
12266c51eab1SEd Tanous 
12276c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
12286c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
12296c51eab1SEd Tanous                 if (ec)
12306c51eab1SEd Tanous                 {
12316c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12326c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12336c51eab1SEd Tanous                     return;
12346c51eab1SEd Tanous                 }
12356c51eab1SEd Tanous                 messages::success(asyncResp->res);
12366c51eab1SEd Tanous                 return;
12376c51eab1SEd Tanous                 },
1238e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12396c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12406c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes",
1241168e20c1SEd Tanous                 "UserLockedForFailedAttempt",
1242168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*locked});
12436c51eab1SEd Tanous         }
12446c51eab1SEd Tanous         });
12456c51eab1SEd Tanous }
12466c51eab1SEd Tanous 
12474c7d4d33SEd Tanous inline void handleAccountServiceHead(
12484c7d4d33SEd Tanous     App& app, const crow::Request& req,
12491ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12506c51eab1SEd Tanous {
12514c7d4d33SEd Tanous 
12523ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
125345ca1b86SEd Tanous     {
125445ca1b86SEd Tanous         return;
125545ca1b86SEd Tanous     }
12564c7d4d33SEd Tanous     asyncResp->res.addHeader(
12574c7d4d33SEd Tanous         boost::beast::http::field::link,
12584c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
12594c7d4d33SEd Tanous }
12604c7d4d33SEd Tanous 
12614c7d4d33SEd Tanous inline void
12624c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
12634c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12644c7d4d33SEd Tanous {
12654c7d4d33SEd Tanous     handleAccountServiceHead(app, req, asyncResp);
126652cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
12671ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
126878158631SZbigniew Kurzynski 
12691476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
12701476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
12711476687dSEd Tanous     json["@odata.type"] = "#AccountService."
12721476687dSEd Tanous                           "v1_10_0.AccountService";
12731476687dSEd Tanous     json["Id"] = "AccountService";
12741476687dSEd Tanous     json["Name"] = "Account Service";
12751476687dSEd Tanous     json["Description"] = "Account Service";
12761476687dSEd Tanous     json["ServiceEnabled"] = true;
12771476687dSEd Tanous     json["MaxPasswordLength"] = 20;
12781ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
12791476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
12801476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
12811476687dSEd Tanous         "#OemAccountService.v1_0_0.AccountService";
12821476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
12831476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
12841476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
12851476687dSEd Tanous         authMethodsConfig.basic;
12861476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
12871476687dSEd Tanous         authMethodsConfig.sessionToken;
12881ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
12891ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
12901ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
12911476687dSEd Tanous 
12921ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
12931ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
12941ef4c342SEd Tanous     // permissions ConfigureManager
129572048780SAbhishek Patel     Privileges effectiveUserPrivileges =
129672048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
129772048780SAbhishek Patel 
129872048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
129972048780SAbhishek Patel                                          effectiveUserPrivileges))
130072048780SAbhishek Patel     {
13011ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
13021476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
130372048780SAbhishek Patel     }
1304d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1305d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1306d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
1307002d39b4SEd Tanous         [asyncResp](const boost::system::error_code ec,
13081ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
13093d958bbcSAppaRao Puli         if (ec)
13103d958bbcSAppaRao Puli         {
13113d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
13123d958bbcSAppaRao Puli             return;
13133d958bbcSAppaRao Puli         }
1314d1bde9e5SKrzysztof Grobelny 
13153d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
13163d958bbcSAppaRao Puli                          << "properties for AccountService";
1317d1bde9e5SKrzysztof Grobelny 
1318d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1319d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1320d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1321d1bde9e5SKrzysztof Grobelny 
1322d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1323d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1324d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1325d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1326d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1327d1bde9e5SKrzysztof Grobelny 
1328d1bde9e5SKrzysztof Grobelny         if (!success)
13293d958bbcSAppaRao Puli         {
1330d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1331d1bde9e5SKrzysztof Grobelny             return;
13323d958bbcSAppaRao Puli         }
1333d1bde9e5SKrzysztof Grobelny 
1334d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
13353d958bbcSAppaRao Puli         {
1336d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
13373d958bbcSAppaRao Puli         }
1338d1bde9e5SKrzysztof Grobelny 
1339d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
13403d958bbcSAppaRao Puli         {
1341d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1342d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1343d1bde9e5SKrzysztof Grobelny         }
1344d1bde9e5SKrzysztof Grobelny 
1345d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
13463d958bbcSAppaRao Puli         {
1347002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1348d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
13493d958bbcSAppaRao Puli         }
1350d1bde9e5SKrzysztof Grobelny         });
13516973a582SRatan Gupta 
135202cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1353ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1354cb13a392SEd Tanous         if (!success)
1355cb13a392SEd Tanous         {
1356cb13a392SEd Tanous             return;
1357cb13a392SEd Tanous         }
1358002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1359ab828d7cSRatan Gupta     };
1360ab828d7cSRatan Gupta 
1361ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1362ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
13631ef4c342SEd Tanous }
13646973a582SRatan Gupta 
13651ef4c342SEd Tanous inline void handleAccountServicePatch(
13661ef4c342SEd Tanous     App& app, const crow::Request& req,
13671ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13681ef4c342SEd Tanous {
13693ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
137045ca1b86SEd Tanous     {
137145ca1b86SEd Tanous         return;
137245ca1b86SEd Tanous     }
1373f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1374f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1375ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1376f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1377f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1378f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1379f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1380f5ffd806SEd Tanous 
138115ed6780SWilly Tu     if (!json_util::readJsonPatch(
13821ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
13831ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
13841ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
13851ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1386f5ffd806SEd Tanous             oemObject))
1387f5ffd806SEd Tanous     {
1388f5ffd806SEd Tanous         return;
1389f5ffd806SEd Tanous     }
1390f5ffd806SEd Tanous 
1391f5ffd806SEd Tanous     if (minPasswordLength)
1392f5ffd806SEd Tanous     {
1393ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
1394ef73ad0dSPaul Fertser             [asyncResp](const boost::system::error_code ec) {
1395ef73ad0dSPaul Fertser             if (ec)
1396ef73ad0dSPaul Fertser             {
1397ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1398ef73ad0dSPaul Fertser                 return;
1399ef73ad0dSPaul Fertser             }
1400ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1401ef73ad0dSPaul Fertser             },
14021ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1403ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
14041ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1405ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1406f5ffd806SEd Tanous     }
1407f5ffd806SEd Tanous 
1408f5ffd806SEd Tanous     if (maxPasswordLength)
1409f5ffd806SEd Tanous     {
14101ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1411f5ffd806SEd Tanous     }
1412f5ffd806SEd Tanous 
1413f5ffd806SEd Tanous     if (ldapObject)
1414f5ffd806SEd Tanous     {
1415f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1416f5ffd806SEd Tanous     }
1417f5ffd806SEd Tanous 
1418f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
14191ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1420f5ffd806SEd Tanous                                          oemOpenBMCObject))
1421f5ffd806SEd Tanous     {
1422f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1423f5ffd806SEd Tanous             oemOpenBMCObject &&
1424f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1425f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1426f5ffd806SEd Tanous         {
1427f5ffd806SEd Tanous             if (authMethodsObject)
1428f5ffd806SEd Tanous             {
14291ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1430f5ffd806SEd Tanous             }
1431f5ffd806SEd Tanous         }
1432f5ffd806SEd Tanous     }
1433f5ffd806SEd Tanous 
1434f5ffd806SEd Tanous     if (activeDirectoryObject)
1435f5ffd806SEd Tanous     {
14361ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1437f5ffd806SEd Tanous     }
1438f5ffd806SEd Tanous 
1439f5ffd806SEd Tanous     if (unlockTimeout)
1440f5ffd806SEd Tanous     {
1441f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1442f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1443f5ffd806SEd Tanous             if (ec)
1444f5ffd806SEd Tanous             {
1445f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1446f5ffd806SEd Tanous                 return;
1447f5ffd806SEd Tanous             }
1448f5ffd806SEd Tanous             messages::success(asyncResp->res);
1449f5ffd806SEd Tanous             },
14501ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1451f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
14521ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1453168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1454f5ffd806SEd Tanous     }
1455f5ffd806SEd Tanous     if (lockoutThreshold)
1456f5ffd806SEd Tanous     {
1457f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1458f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1459f5ffd806SEd Tanous             if (ec)
1460f5ffd806SEd Tanous             {
1461f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1462f5ffd806SEd Tanous                 return;
1463f5ffd806SEd Tanous             }
1464f5ffd806SEd Tanous             messages::success(asyncResp->res);
1465f5ffd806SEd Tanous             },
14661ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1467f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1468f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1469f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1470168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1471f5ffd806SEd Tanous     }
14721ef4c342SEd Tanous }
1473f5ffd806SEd Tanous 
14744c7d4d33SEd Tanous inline void handleAccountCollectionHead(
14751ef4c342SEd Tanous     App& app, const crow::Request& req,
14761ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14771ef4c342SEd Tanous {
14784c7d4d33SEd Tanous 
14793ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
148045ca1b86SEd Tanous     {
148145ca1b86SEd Tanous         return;
148245ca1b86SEd Tanous     }
14834c7d4d33SEd Tanous     asyncResp->res.addHeader(
14844c7d4d33SEd Tanous         boost::beast::http::field::link,
14854c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
14864c7d4d33SEd Tanous }
14874c7d4d33SEd Tanous 
14884c7d4d33SEd Tanous inline void handleAccountCollectionGet(
14894c7d4d33SEd Tanous     App& app, const crow::Request& req,
14904c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14914c7d4d33SEd Tanous {
14924c7d4d33SEd Tanous     handleAccountCollectionHead(app, req, asyncResp);
14931476687dSEd Tanous 
14941476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
14951476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
14961ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
14971476687dSEd Tanous                                               "ManagerAccountCollection";
14981476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
14991476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
15000f74e643SEd Tanous 
15016c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
15026c51eab1SEd Tanous         redfish::getUserPrivileges(req.userRole);
15036c51eab1SEd Tanous 
1504f5e29f33SJunLin Chen     std::string thisUser;
1505f5e29f33SJunLin Chen     if (req.session)
1506f5e29f33SJunLin Chen     {
1507f5e29f33SJunLin Chen         thisUser = req.session->username;
1508f5e29f33SJunLin Chen     }
1509b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
1510cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
1511cef1ddfbSEd Tanous             const boost::system::error_code ec,
1512711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1513b9b2e0b2SEd Tanous         if (ec)
1514b9b2e0b2SEd Tanous         {
1515f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1516b9b2e0b2SEd Tanous             return;
1517b9b2e0b2SEd Tanous         }
1518b9b2e0b2SEd Tanous 
1519cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1520002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1521cef1ddfbSEd Tanous 
1522cef1ddfbSEd Tanous         bool userCanSeeSelf =
1523002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1524cef1ddfbSEd Tanous 
1525002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1526b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1527b9b2e0b2SEd Tanous 
15289eb808c1SEd Tanous         for (const auto& userpath : users)
1529b9b2e0b2SEd Tanous         {
15302dfd18efSEd Tanous             std::string user = userpath.first.filename();
15312dfd18efSEd Tanous             if (user.empty())
1532b9b2e0b2SEd Tanous             {
15332dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
15342dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
15352dfd18efSEd Tanous 
15362dfd18efSEd Tanous                 return;
1537b9b2e0b2SEd Tanous             }
1538f365910cSGunnar Mills 
1539f365910cSGunnar Mills             // As clarified by Redfish here:
1540f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
15416c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
15426c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
15436c51eab1SEd Tanous             // accounts.
15441ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1545f365910cSGunnar Mills             {
15461476687dSEd Tanous                 nlohmann::json::object_t member;
15471476687dSEd Tanous                 member["@odata.id"] =
1548002d39b4SEd Tanous                     "/redfish/v1/AccountService/Accounts/" + user;
15491476687dSEd Tanous                 memberArray.push_back(std::move(member));
1550b9b2e0b2SEd Tanous             }
1551f365910cSGunnar Mills         }
15521ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1553b9b2e0b2SEd Tanous         },
15541ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1555b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15561ef4c342SEd Tanous }
15576c51eab1SEd Tanous 
15581ef4c342SEd Tanous inline void handleAccountCollectionPost(
15591ef4c342SEd Tanous     App& app, const crow::Request& req,
15601ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15611ef4c342SEd Tanous {
15623ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
156345ca1b86SEd Tanous     {
156445ca1b86SEd Tanous         return;
156545ca1b86SEd Tanous     }
15669712f8acSEd Tanous     std::string username;
15679712f8acSEd Tanous     std::string password;
1568a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1569a24526dcSEd Tanous     std::optional<bool> enabled = true;
15701ef4c342SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
15711ef4c342SEd Tanous                                   "Password", password, "RoleId", roleId,
15721ef4c342SEd Tanous                                   "Enabled", enabled))
157304ae99ecSEd Tanous     {
157404ae99ecSEd Tanous         return;
157504ae99ecSEd Tanous     }
157604ae99ecSEd Tanous 
157754fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
157884e12cb7SAppaRao Puli     if (priv.empty())
157904ae99ecSEd Tanous     {
15801ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
158104ae99ecSEd Tanous         return;
158204ae99ecSEd Tanous     }
158396200606Sjayaprakash Mutyala     // TODO: Following override will be reverted once support in
15846c51eab1SEd Tanous     // phosphor-user-manager is added. In order to avoid dependency
15856c51eab1SEd Tanous     // issues, this is added in bmcweb, which will removed, once
158696200606Sjayaprakash Mutyala     // phosphor-user-manager supports priv-noaccess.
158796200606Sjayaprakash Mutyala     if (priv == "priv-noaccess")
158896200606Sjayaprakash Mutyala     {
158996200606Sjayaprakash Mutyala         roleId = "";
159096200606Sjayaprakash Mutyala     }
159196200606Sjayaprakash Mutyala     else
159296200606Sjayaprakash Mutyala     {
15939712f8acSEd Tanous         roleId = priv;
159496200606Sjayaprakash Mutyala     }
159504ae99ecSEd Tanous 
1596599c71d8SAyushi Smriti     // Reading AllGroups property
15971e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
15981ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
15991ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
16001ef4c342SEd Tanous         "AllGroups",
1601599c71d8SAyushi Smriti         [asyncResp, username, password{std::move(password)}, roleId,
1602168e20c1SEd Tanous          enabled](const boost::system::error_code ec,
16031e1e598dSJonathan Doman                   const std::vector<std::string>& allGroupsList) {
1604599c71d8SAyushi Smriti         if (ec)
1605599c71d8SAyushi Smriti         {
1606599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1607599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1608599c71d8SAyushi Smriti             return;
1609599c71d8SAyushi Smriti         }
1610599c71d8SAyushi Smriti 
16111e1e598dSJonathan Doman         if (allGroupsList.empty())
1612599c71d8SAyushi Smriti         {
1613599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1614599c71d8SAyushi Smriti             return;
1615599c71d8SAyushi Smriti         }
1616599c71d8SAyushi Smriti 
161704ae99ecSEd Tanous         crow::connections::systemBus->async_method_call(
16181ef4c342SEd Tanous             [asyncResp, username, password](const boost::system::error_code ec2,
161959d494eeSPatrick Williams                                             sdbusplus::message_t& m) {
162023a21a1cSEd Tanous             if (ec2)
162104ae99ecSEd Tanous             {
16221ef4c342SEd Tanous                 userErrorMessageHandler(m.get_error(), asyncResp, username, "");
162304ae99ecSEd Tanous                 return;
162404ae99ecSEd Tanous             }
162504ae99ecSEd Tanous 
1626002d39b4SEd Tanous             if (pamUpdatePassword(username, password) != PAM_SUCCESS)
162704ae99ecSEd Tanous             {
16286c51eab1SEd Tanous                 // At this point we have a user that's been
16296c51eab1SEd Tanous                 // created, but the password set
16306c51eab1SEd Tanous                 // failed.Something is wrong, so delete the user
16316c51eab1SEd Tanous                 // that we've already created
16321ef4c342SEd Tanous                 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1633b477fd44SP Dheeraj Srujan Kumar                 tempObjPath /= username;
1634b477fd44SP Dheeraj Srujan Kumar                 const std::string userPath(tempObjPath);
1635b477fd44SP Dheeraj Srujan Kumar 
163604ae99ecSEd Tanous                 crow::connections::systemBus->async_method_call(
16371ef4c342SEd Tanous                     [asyncResp, password](const boost::system::error_code ec3) {
163823a21a1cSEd Tanous                     if (ec3)
163904ae99ecSEd Tanous                     {
1640002d39b4SEd Tanous                         messages::internalError(asyncResp->res);
164104ae99ecSEd Tanous                         return;
164204ae99ecSEd Tanous                     }
164304ae99ecSEd Tanous 
16440d4197e0Sanil kumar appana                     // If password is invalid
16451ef4c342SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, password,
16461ef4c342SEd Tanous                                                        "Password");
164704ae99ecSEd Tanous                     },
1648002d39b4SEd Tanous                     "xyz.openbmc_project.User.Manager", userPath,
1649002d39b4SEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
165004ae99ecSEd Tanous 
165104ae99ecSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
165204ae99ecSEd Tanous                 return;
165304ae99ecSEd Tanous             }
165404ae99ecSEd Tanous 
1655f12894f8SJason M. Bills             messages::created(asyncResp->res);
165604ae99ecSEd Tanous             asyncResp->res.addHeader(
16571ef4c342SEd Tanous                 "Location", "/redfish/v1/AccountService/Accounts/" + username);
165804ae99ecSEd Tanous             },
1659002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1660002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "CreateUser", username,
1661002d39b4SEd Tanous             allGroupsList, *roleId, *enabled);
16621e1e598dSJonathan Doman         });
16631ef4c342SEd Tanous }
1664b9b2e0b2SEd Tanous 
16651ef4c342SEd Tanous inline void
16664c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
16676c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16684c7d4d33SEd Tanous                       const std::string& /*accountName*/)
16691ef4c342SEd Tanous {
16704c7d4d33SEd Tanous 
16713ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
167245ca1b86SEd Tanous     {
167345ca1b86SEd Tanous         return;
167445ca1b86SEd Tanous     }
16754c7d4d33SEd Tanous     asyncResp->res.addHeader(
16764c7d4d33SEd Tanous         boost::beast::http::field::link,
16774c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
16784c7d4d33SEd Tanous }
16794c7d4d33SEd Tanous inline void
16804c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
16814c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16824c7d4d33SEd Tanous                      const std::string& accountName)
16834c7d4d33SEd Tanous {
16844c7d4d33SEd Tanous     handleAccountHead(app, req, asyncResp, accountName);
16851ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1686031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1687d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1688031514fbSJunLin Chen     return;
1689031514fbSJunLin Chen 
16901ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1691031514fbSJunLin Chen     if (req.session == nullptr)
1692031514fbSJunLin Chen     {
1693031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1694031514fbSJunLin Chen         return;
1695031514fbSJunLin Chen     }
16966c51eab1SEd Tanous     if (req.session->username != accountName)
1697b9b2e0b2SEd Tanous     {
16986c51eab1SEd Tanous         // At this point we've determined that the user is trying to
16991ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
17001ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
17011ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
17026c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
17036c51eab1SEd Tanous             redfish::getUserPrivileges(req.userRole);
17041ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
17051ef4c342SEd Tanous                                                          "ConfigureManager"};
17066c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
17076c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1708900f9497SJoseph Reynolds         {
1709900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
1710900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1711900f9497SJoseph Reynolds             return;
1712900f9497SJoseph Reynolds         }
1713900f9497SJoseph Reynolds     }
1714900f9497SJoseph Reynolds 
1715b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
17161ef4c342SEd Tanous         [asyncResp,
17171ef4c342SEd Tanous          accountName](const boost::system::error_code ec,
1718711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1719b9b2e0b2SEd Tanous         if (ec)
1720b9b2e0b2SEd Tanous         {
1721f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1722b9b2e0b2SEd Tanous             return;
1723b9b2e0b2SEd Tanous         }
1724711ac7a9SEd Tanous         const auto userIt = std::find_if(
1725b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
1726b477fd44SP Dheeraj Srujan Kumar             [accountName](
1727b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
1728002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
172955f79e6fSEd Tanous             return accountName == user.first.filename();
1730b477fd44SP Dheeraj Srujan Kumar             });
1731b9b2e0b2SEd Tanous 
173284e12cb7SAppaRao Puli         if (userIt == users.end())
1733b9b2e0b2SEd Tanous         {
1734002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1735002d39b4SEd Tanous                                        accountName);
173684e12cb7SAppaRao Puli             return;
173784e12cb7SAppaRao Puli         }
17384e68c45bSAyushi Smriti 
17391476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
17401476687dSEd Tanous             "#ManagerAccount.v1_4_0.ManagerAccount";
17411476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
17421476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
17431476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
1744*613dabeaSEd Tanous         asyncResp->res.jsonValue["AccountTypes"] =
1745*613dabeaSEd Tanous             nlohmann::json::array_t({"Redfish"});
17464e68c45bSAyushi Smriti 
174784e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
174865b0dc32SEd Tanous         {
1749002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
175065b0dc32SEd Tanous             {
175165b0dc32SEd Tanous                 for (const auto& property : interface.second)
175265b0dc32SEd Tanous                 {
175365b0dc32SEd Tanous                     if (property.first == "UserEnabled")
175465b0dc32SEd Tanous                     {
175565b0dc32SEd Tanous                         const bool* userEnabled =
1756abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
175765b0dc32SEd Tanous                         if (userEnabled == nullptr)
175865b0dc32SEd Tanous                         {
1759002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
176084e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
176184e12cb7SAppaRao Puli                             return;
176265b0dc32SEd Tanous                         }
1763002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
176465b0dc32SEd Tanous                     }
1765002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
176665b0dc32SEd Tanous                     {
176765b0dc32SEd Tanous                         const bool* userLocked =
1768abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
176965b0dc32SEd Tanous                         if (userLocked == nullptr)
177065b0dc32SEd Tanous                         {
177184e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
177284e12cb7SAppaRao Puli                                                 "ailedAttempt "
177384e12cb7SAppaRao Puli                                                 "wasn't a bool";
177484e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
177584e12cb7SAppaRao Puli                             return;
177665b0dc32SEd Tanous                         }
1777002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
1778002d39b4SEd Tanous                         asyncResp->res
1779002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
17803bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
178165b0dc32SEd Tanous                     }
178284e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
178384e12cb7SAppaRao Puli                     {
178454fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1785002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
178654fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
178784e12cb7SAppaRao Puli                         {
1788002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
178984e12cb7SAppaRao Puli                                                 "string";
179084e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
179184e12cb7SAppaRao Puli                             return;
179284e12cb7SAppaRao Puli                         }
17931ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
179454fc587aSNagaraju Goruganti                         if (role.empty())
179584e12cb7SAppaRao Puli                         {
179684e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
179784e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
179884e12cb7SAppaRao Puli                             return;
179984e12cb7SAppaRao Puli                         }
180054fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
180184e12cb7SAppaRao Puli 
18021476687dSEd Tanous                         nlohmann::json& roleEntry =
1803002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
18041476687dSEd Tanous                         roleEntry["@odata.id"] =
1805002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
180684e12cb7SAppaRao Puli                     }
1807002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
18083bf4e632SJoseph Reynolds                     {
18093bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
18103bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
18113bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
18123bf4e632SJoseph Reynolds                         {
18130fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
18140fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
18153bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
18163bf4e632SJoseph Reynolds                             return;
18173bf4e632SJoseph Reynolds                         }
1818002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
18193bf4e632SJoseph Reynolds                             *userPasswordExpired;
18203bf4e632SJoseph Reynolds                     }
182165b0dc32SEd Tanous                 }
182265b0dc32SEd Tanous             }
182365b0dc32SEd Tanous         }
182465b0dc32SEd Tanous 
1825b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
182684e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
1827b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
1828b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
1829b9b2e0b2SEd Tanous         },
18301ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1831b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18321ef4c342SEd Tanous }
1833a840879dSEd Tanous 
18341ef4c342SEd Tanous inline void
18351ef4c342SEd Tanous     handleAccounttDelete(App& app, const crow::Request& req,
18366c51eab1SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18371ef4c342SEd Tanous                          const std::string& username)
18381ef4c342SEd Tanous {
18393ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
184045ca1b86SEd Tanous     {
184145ca1b86SEd Tanous         return;
184245ca1b86SEd Tanous     }
18431ef4c342SEd Tanous 
18441ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1845031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1846d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1847031514fbSJunLin Chen     return;
1848031514fbSJunLin Chen 
18491ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
18501ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
18511ef4c342SEd Tanous     tempObjPath /= username;
18521ef4c342SEd Tanous     const std::string userPath(tempObjPath);
18531ef4c342SEd Tanous 
18541ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
18551ef4c342SEd Tanous         [asyncResp, username](const boost::system::error_code ec) {
18561ef4c342SEd Tanous         if (ec)
18571ef4c342SEd Tanous         {
1858d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
18591ef4c342SEd Tanous                                        username);
18601ef4c342SEd Tanous             return;
18611ef4c342SEd Tanous         }
18621ef4c342SEd Tanous 
18631ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
18641ef4c342SEd Tanous         },
18651ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
18661ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
18671ef4c342SEd Tanous }
18681ef4c342SEd Tanous 
18691ef4c342SEd Tanous inline void
18701ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
18711ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18721ef4c342SEd Tanous                        const std::string& username)
18731ef4c342SEd Tanous {
18741ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18751ef4c342SEd Tanous     {
18761ef4c342SEd Tanous         return;
18771ef4c342SEd Tanous     }
18781ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
18791ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
1880d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
18811ef4c342SEd Tanous     return;
18821ef4c342SEd Tanous 
18831ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1884a24526dcSEd Tanous     std::optional<std::string> newUserName;
1885a24526dcSEd Tanous     std::optional<std::string> password;
1886a24526dcSEd Tanous     std::optional<bool> enabled;
1887a24526dcSEd Tanous     std::optional<std::string> roleId;
188824c8542dSRatan Gupta     std::optional<bool> locked;
1889e9cc5172SEd Tanous 
1890031514fbSJunLin Chen     if (req.session == nullptr)
1891031514fbSJunLin Chen     {
1892031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1893031514fbSJunLin Chen         return;
1894031514fbSJunLin Chen     }
1895031514fbSJunLin Chen 
1896e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
1897e9cc5172SEd Tanous         redfish::getUserPrivileges(req.userRole);
1898e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
1899e9cc5172SEd Tanous     bool userHasConfigureUsers =
1900e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
1901e9cc5172SEd Tanous     if (userHasConfigureUsers)
1902e9cc5172SEd Tanous     {
1903e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
19041ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "UserName",
19051ef4c342SEd Tanous                                       newUserName, "Password", password,
19061ef4c342SEd Tanous                                       "RoleId", roleId, "Enabled", enabled,
19071ef4c342SEd Tanous                                       "Locked", locked))
1908a840879dSEd Tanous         {
1909a840879dSEd Tanous             return;
1910a840879dSEd Tanous         }
1911e9cc5172SEd Tanous     }
1912e9cc5172SEd Tanous     else
1913900f9497SJoseph Reynolds     {
1914e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
1915e9cc5172SEd Tanous         if (username != req.session->username)
1916900f9497SJoseph Reynolds         {
1917900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1918900f9497SJoseph Reynolds             return;
1919900f9497SJoseph Reynolds         }
1920031514fbSJunLin Chen 
1921e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
19221ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
19231ef4c342SEd Tanous                                       password))
1924e9cc5172SEd Tanous         {
1925e9cc5172SEd Tanous             return;
1926e9cc5172SEd Tanous         }
1927900f9497SJoseph Reynolds     }
1928900f9497SJoseph Reynolds 
192966b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
19306c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
19316c51eab1SEd Tanous     // updating user properties other then username. If username
19326c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
19336c51eab1SEd Tanous     // user rename request.
193466b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
1935a840879dSEd Tanous     {
19361ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
19371ef4c342SEd Tanous                              locked);
193884e12cb7SAppaRao Puli         return;
193984e12cb7SAppaRao Puli     }
194084e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
19416c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
19421ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
194359d494eeSPatrick Williams          locked](const boost::system::error_code ec, sdbusplus::message_t& m) {
194484e12cb7SAppaRao Puli         if (ec)
194584e12cb7SAppaRao Puli         {
1946002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
1947002d39b4SEd Tanous                                     username);
1948a840879dSEd Tanous             return;
1949a840879dSEd Tanous         }
1950a840879dSEd Tanous 
1951002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
1952002d39b4SEd Tanous                              locked);
195384e12cb7SAppaRao Puli         },
19541ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
195584e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
195684e12cb7SAppaRao Puli         *newUserName);
19571ef4c342SEd Tanous }
19581ef4c342SEd Tanous 
19591ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
19601ef4c342SEd Tanous {
19611ef4c342SEd Tanous 
19621ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
19634c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
19644c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
19654c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
19664c7d4d33SEd Tanous 
19674c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
19681ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
19691ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
19701ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
19711ef4c342SEd Tanous 
19721ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
19731ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
19741ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
19751ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
19761ef4c342SEd Tanous 
19771ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
19784c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
19794c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
19804c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
19814c7d4d33SEd Tanous 
19824c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
19831ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
19841ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
19851ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
19861ef4c342SEd Tanous 
19871ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
19881ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
19891ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
19901ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
19911ef4c342SEd Tanous 
19921ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
19934c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
19944c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
19954c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
19964c7d4d33SEd Tanous 
19974c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
19981ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
19991ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20001ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
20011ef4c342SEd Tanous 
20021ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20031ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
20041ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
20051ef4c342SEd Tanous         // yet
20061ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
20071ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20081ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
200984e12cb7SAppaRao Puli 
20106c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2011ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
20126c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
20131ef4c342SEd Tanous             std::bind_front(handleAccounttDelete, std::ref(app)));
201406e086d9SEd Tanous }
201588d16c9aSLewanczyk, Dawid 
201688d16c9aSLewanczyk, Dawid } // namespace redfish
2017