xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision d1bde9e590f165f28d948fda93e48d51b30bb463)
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>
26*d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
27*d1bde9e5SKrzysztof 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";
18554fc587aSNagaraju Goruganti         roleMapArray.push_back(
18654fc587aSNagaraju Goruganti             {nlohmann::json::array({"RemoteGroup", obj.second.groupName}),
18754fc587aSNagaraju Goruganti              nlohmann::json::array(
18854fc587aSNagaraju Goruganti                  {"LocalRole", getRoleIdFromPrivilege(obj.second.privilege)})});
18954fc587aSNagaraju Goruganti     }
1906973a582SRatan Gupta }
1916973a582SRatan Gupta 
1926973a582SRatan Gupta /**
19306785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
19406785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
19506785244SRatan Gupta  *
19606785244SRatan Gupta  */
19723a21a1cSEd Tanous inline void handleRoleMapPatch(
1988d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19906785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
200f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
20106785244SRatan Gupta {
20206785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
20306785244SRatan Gupta     {
204f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
20506785244SRatan Gupta 
20606785244SRatan Gupta         if (thisJson.is_null())
20706785244SRatan Gupta         {
20806785244SRatan Gupta             // delete the existing object
20906785244SRatan Gupta             if (index < roleMapObjData.size())
21006785244SRatan Gupta             {
21106785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
21206785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
21306785244SRatan Gupta                      index](const boost::system::error_code ec) {
21406785244SRatan Gupta                     if (ec)
21506785244SRatan Gupta                     {
21606785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
21706785244SRatan Gupta                         messages::internalError(asyncResp->res);
21806785244SRatan Gupta                         return;
21906785244SRatan Gupta                     }
22006785244SRatan Gupta                     asyncResp->res
22106785244SRatan Gupta                         .jsonValue[serverType]["RemoteRoleMapping"][index] =
22206785244SRatan Gupta                         nullptr;
22306785244SRatan Gupta                     },
22406785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
22506785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
22606785244SRatan Gupta             }
22706785244SRatan Gupta             else
22806785244SRatan Gupta             {
22906785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
23006785244SRatan Gupta                 messages::propertyValueTypeError(
23171f52d96SEd Tanous                     asyncResp->res,
23271f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
23371f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
23406785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
23506785244SRatan Gupta                 return;
23606785244SRatan Gupta             }
23706785244SRatan Gupta         }
23806785244SRatan Gupta         else if (thisJson.empty())
23906785244SRatan Gupta         {
24006785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
24106785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
24206785244SRatan Gupta         }
24306785244SRatan Gupta         else
24406785244SRatan Gupta         {
24506785244SRatan Gupta             // update/create the object
24606785244SRatan Gupta             std::optional<std::string> remoteGroup;
24706785244SRatan Gupta             std::optional<std::string> localRole;
24806785244SRatan Gupta 
249f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
250f23b7296SEd Tanous             // readJson is structured
251f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
252f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
253f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
254f23b7296SEd Tanous                                      localRole))
25506785244SRatan Gupta             {
25606785244SRatan Gupta                 continue;
25706785244SRatan Gupta             }
25806785244SRatan Gupta 
25906785244SRatan Gupta             // Update existing RoleMapping Object
26006785244SRatan Gupta             if (index < roleMapObjData.size())
26106785244SRatan Gupta             {
26206785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
26306785244SRatan Gupta                 // If "RemoteGroup" info is provided
26406785244SRatan Gupta                 if (remoteGroup)
26506785244SRatan Gupta                 {
26606785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
26706785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
26806785244SRatan Gupta                          remoteGroup](const boost::system::error_code ec) {
26906785244SRatan Gupta                         if (ec)
27006785244SRatan Gupta                         {
271002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
27206785244SRatan Gupta                             messages::internalError(asyncResp->res);
27306785244SRatan Gupta                             return;
27406785244SRatan Gupta                         }
27506785244SRatan Gupta                         asyncResp->res
276002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
277002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
27806785244SRatan Gupta                         },
27906785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
28006785244SRatan Gupta                         propertyInterface, "Set",
28106785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
28206785244SRatan Gupta                         "GroupName",
283168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
284168e20c1SEd Tanous                             std::move(*remoteGroup)));
28506785244SRatan Gupta                 }
28606785244SRatan Gupta 
28706785244SRatan Gupta                 // If "LocalRole" info is provided
28806785244SRatan Gupta                 if (localRole)
28906785244SRatan Gupta                 {
29006785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
29106785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
29206785244SRatan Gupta                          localRole](const boost::system::error_code ec) {
29306785244SRatan Gupta                         if (ec)
29406785244SRatan Gupta                         {
295002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
29606785244SRatan Gupta                             messages::internalError(asyncResp->res);
29706785244SRatan Gupta                             return;
29806785244SRatan Gupta                         }
29906785244SRatan Gupta                         asyncResp->res
300002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
301002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
30206785244SRatan Gupta                         },
30306785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
30406785244SRatan Gupta                         propertyInterface, "Set",
30506785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
30606785244SRatan Gupta                         "Privilege",
307168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
30806785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
30906785244SRatan Gupta                 }
31006785244SRatan Gupta             }
31106785244SRatan Gupta             // Create a new RoleMapping Object.
31206785244SRatan Gupta             else
31306785244SRatan Gupta             {
31406785244SRatan Gupta                 BMCWEB_LOG_DEBUG
31506785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
31606785244SRatan Gupta                 std::string pathString =
31706785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index);
31806785244SRatan Gupta 
31906785244SRatan Gupta                 if (!localRole)
32006785244SRatan Gupta                 {
32106785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
32206785244SRatan Gupta                                               pathString + "/LocalRole");
32306785244SRatan Gupta                     continue;
32406785244SRatan Gupta                 }
32506785244SRatan Gupta                 if (!remoteGroup)
32606785244SRatan Gupta                 {
32706785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
32806785244SRatan Gupta                                               pathString + "/RemoteGroup");
32906785244SRatan Gupta                     continue;
33006785244SRatan Gupta                 }
33106785244SRatan Gupta 
33206785244SRatan Gupta                 std::string dbusObjectPath;
33306785244SRatan Gupta                 if (serverType == "ActiveDirectory")
33406785244SRatan Gupta                 {
3352c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
33606785244SRatan Gupta                 }
33706785244SRatan Gupta                 else if (serverType == "LDAP")
33806785244SRatan Gupta                 {
33923a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
34006785244SRatan Gupta                 }
34106785244SRatan Gupta 
34206785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
34306785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
34406785244SRatan Gupta 
34506785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
346271584abSEd Tanous                     [asyncResp, serverType, localRole,
34706785244SRatan Gupta                      remoteGroup](const boost::system::error_code ec) {
34806785244SRatan Gupta                     if (ec)
34906785244SRatan Gupta                     {
35006785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
35106785244SRatan Gupta                         messages::internalError(asyncResp->res);
35206785244SRatan Gupta                         return;
35306785244SRatan Gupta                     }
35406785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
35506785244SRatan Gupta                         asyncResp->res
35606785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
3571476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
3581476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
3591476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
3601476687dSEd Tanous                     remoteRoleJson.push_back(std::move(roleMapEntry));
36106785244SRatan Gupta                     },
36206785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
3633174e4dfSEd Tanous                     "Create", *remoteGroup,
36406785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
36506785244SRatan Gupta             }
36606785244SRatan Gupta         }
36706785244SRatan Gupta     }
36806785244SRatan Gupta }
36906785244SRatan Gupta 
37006785244SRatan Gupta /**
3716973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
3726973a582SRatan Gupta  * into JSON
3736973a582SRatan Gupta  */
3746973a582SRatan Gupta template <typename CallbackFunc>
3756973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
3766973a582SRatan Gupta                               CallbackFunc&& callback)
3776973a582SRatan Gupta {
37854fc587aSNagaraju Goruganti 
37954fc587aSNagaraju Goruganti     const std::array<const char*, 2> interfaces = {ldapEnableInterface,
38054fc587aSNagaraju Goruganti                                                    ldapConfigInterface};
38154fc587aSNagaraju Goruganti 
38254fc587aSNagaraju Goruganti     crow::connections::systemBus->async_method_call(
38354fc587aSNagaraju Goruganti         [callback, ldapType](const boost::system::error_code ec,
384b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
38554fc587aSNagaraju Goruganti         if (ec || resp.empty())
38654fc587aSNagaraju Goruganti         {
3870fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
388002d39b4SEd Tanous                 << "DBUS response error during getting of service name: " << ec;
38923a21a1cSEd Tanous             LDAPConfigData empty{};
39023a21a1cSEd Tanous             callback(false, empty, ldapType);
39154fc587aSNagaraju Goruganti             return;
39254fc587aSNagaraju Goruganti         }
39354fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
39454fc587aSNagaraju Goruganti         crow::connections::systemBus->async_method_call(
395002d39b4SEd Tanous             [callback,
396002d39b4SEd Tanous              ldapType](const boost::system::error_code errorCode,
397711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
3986973a582SRatan Gupta             LDAPConfigData confData{};
39981ce609eSEd Tanous             if (errorCode)
4006973a582SRatan Gupta             {
401ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
402002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode;
4036973a582SRatan Gupta                 return;
4046973a582SRatan Gupta             }
405ab828d7cSRatan Gupta 
406ab828d7cSRatan Gupta             std::string ldapDbusType;
40754fc587aSNagaraju Goruganti             std::string searchString;
40854fc587aSNagaraju Goruganti 
409ab828d7cSRatan Gupta             if (ldapType == "LDAP")
410ab828d7cSRatan Gupta             {
4110fda0f12SGeorge Liu                 ldapDbusType =
4120fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
41354fc587aSNagaraju Goruganti                 searchString = "openldap";
414ab828d7cSRatan Gupta             }
415ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
416ab828d7cSRatan Gupta             {
41754fc587aSNagaraju Goruganti                 ldapDbusType =
4180fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
41954fc587aSNagaraju Goruganti                 searchString = "active_directory";
420ab828d7cSRatan Gupta             }
421ab828d7cSRatan Gupta             else
422ab828d7cSRatan Gupta             {
423002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type="
424ab828d7cSRatan Gupta                                  << ldapType;
425ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
426ab828d7cSRatan Gupta                 return;
427ab828d7cSRatan Gupta             }
428ab828d7cSRatan Gupta 
429ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
430ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
431ab828d7cSRatan Gupta 
4326973a582SRatan Gupta             for (const auto& object : ldapObjects)
4336973a582SRatan Gupta             {
43454fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
43554fc587aSNagaraju Goruganti                 // given type
436002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
4376973a582SRatan Gupta                 {
438ab828d7cSRatan Gupta                     continue;
439ab828d7cSRatan Gupta                 }
440ab828d7cSRatan Gupta 
4416973a582SRatan Gupta                 for (const auto& interface : object.second)
4426973a582SRatan Gupta                 {
4436973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
4446973a582SRatan Gupta                     {
4456973a582SRatan Gupta                         // rest of the properties are string.
4466973a582SRatan Gupta                         for (const auto& property : interface.second)
4476973a582SRatan Gupta                         {
4486973a582SRatan Gupta                             if (property.first == "Enabled")
4496973a582SRatan Gupta                             {
4506973a582SRatan Gupta                                 const bool* value =
4516973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
4526973a582SRatan Gupta                                 if (value == nullptr)
4536973a582SRatan Gupta                                 {
4546973a582SRatan Gupta                                     continue;
4556973a582SRatan Gupta                                 }
4566973a582SRatan Gupta                                 confData.serviceEnabled = *value;
4576973a582SRatan Gupta                                 break;
4586973a582SRatan Gupta                             }
4596973a582SRatan Gupta                         }
4606973a582SRatan Gupta                     }
4616973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
4626973a582SRatan Gupta                     {
4636973a582SRatan Gupta 
4646973a582SRatan Gupta                         for (const auto& property : interface.second)
4656973a582SRatan Gupta                         {
466271584abSEd Tanous                             const std::string* strValue =
467002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
468271584abSEd Tanous                             if (strValue == nullptr)
4696973a582SRatan Gupta                             {
4706973a582SRatan Gupta                                 continue;
4716973a582SRatan Gupta                             }
4726973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
4736973a582SRatan Gupta                             {
474271584abSEd Tanous                                 confData.uri = *strValue;
4756973a582SRatan Gupta                             }
4766973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
4776973a582SRatan Gupta                             {
478271584abSEd Tanous                                 confData.bindDN = *strValue;
4796973a582SRatan Gupta                             }
4806973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
4816973a582SRatan Gupta                             {
482271584abSEd Tanous                                 confData.baseDN = *strValue;
4836973a582SRatan Gupta                             }
484002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
4856973a582SRatan Gupta                             {
486271584abSEd Tanous                                 confData.searchScope = *strValue;
4876973a582SRatan Gupta                             }
488002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
4896973a582SRatan Gupta                             {
490271584abSEd Tanous                                 confData.groupAttribute = *strValue;
4916973a582SRatan Gupta                             }
492002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
4936973a582SRatan Gupta                             {
494271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
4956973a582SRatan Gupta                             }
49654fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
497ab828d7cSRatan Gupta                             {
498271584abSEd Tanous                                 confData.serverType = *strValue;
49954fc587aSNagaraju Goruganti                             }
50054fc587aSNagaraju Goruganti                         }
50154fc587aSNagaraju Goruganti                     }
502002d39b4SEd Tanous                     else if (interface.first ==
5030fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
50454fc587aSNagaraju Goruganti                     {
50554fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
50654fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
50754fc587aSNagaraju Goruganti                         {
508271584abSEd Tanous                             const std::string* strValue =
509002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
51054fc587aSNagaraju Goruganti 
511271584abSEd Tanous                             if (strValue == nullptr)
51254fc587aSNagaraju Goruganti                             {
51354fc587aSNagaraju Goruganti                                 continue;
51454fc587aSNagaraju Goruganti                             }
51554fc587aSNagaraju Goruganti 
51654fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
51754fc587aSNagaraju Goruganti                             {
518271584abSEd Tanous                                 roleMapData.groupName = *strValue;
51954fc587aSNagaraju Goruganti                             }
52054fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
52154fc587aSNagaraju Goruganti                             {
522271584abSEd Tanous                                 roleMapData.privilege = *strValue;
52354fc587aSNagaraju Goruganti                             }
52454fc587aSNagaraju Goruganti                         }
52554fc587aSNagaraju Goruganti 
526002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
527002d39b4SEd Tanous                                                             roleMapData);
52854fc587aSNagaraju Goruganti                     }
52954fc587aSNagaraju Goruganti                 }
53054fc587aSNagaraju Goruganti             }
531ab828d7cSRatan Gupta             callback(true, confData, ldapType);
53254fc587aSNagaraju Goruganti             },
533002d39b4SEd Tanous             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
53454fc587aSNagaraju Goruganti         },
53554fc587aSNagaraju Goruganti         mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
53623a21a1cSEd Tanous         ldapConfigObjectName, interfaces);
5376973a582SRatan Gupta }
5386973a582SRatan Gupta 
5398a07d286SRatan Gupta /**
5408a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
5418a07d286SRatan Gupta  * @param input JSON data
5428a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5438a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
5448a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
5458a07d286SRatan Gupta  */
5464f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
5476c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5486c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
5498a07d286SRatan Gupta {
5508a07d286SRatan Gupta     std::optional<std::string> authType;
5518a07d286SRatan Gupta 
5528a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
5538a07d286SRatan Gupta                              authType, "Username", username, "Password",
5548a07d286SRatan Gupta                              password))
5558a07d286SRatan Gupta     {
5568a07d286SRatan Gupta         return;
5578a07d286SRatan Gupta     }
5588a07d286SRatan Gupta     if (!authType)
5598a07d286SRatan Gupta     {
5608a07d286SRatan Gupta         return;
5618a07d286SRatan Gupta     }
5628a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
5638a07d286SRatan Gupta     {
5648a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
5658a07d286SRatan Gupta                                          "AuthenticationType");
5668a07d286SRatan Gupta         return;
5678a07d286SRatan Gupta     }
5688a07d286SRatan Gupta }
5698a07d286SRatan Gupta /**
5708a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
5718a07d286SRatan Gupta  * @param input JSON data
5728a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5738a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
5748a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
5758a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
5768a07d286SRatan Gupta  */
5778a07d286SRatan Gupta 
5784f48d5f6SEd Tanous inline void
5794f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
5808d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5818a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
5828a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
5838a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
5848a07d286SRatan Gupta {
5858a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
5868a07d286SRatan Gupta 
5878a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
5888a07d286SRatan Gupta                              searchSettings))
5898a07d286SRatan Gupta     {
5908a07d286SRatan Gupta         return;
5918a07d286SRatan Gupta     }
5928a07d286SRatan Gupta     if (!searchSettings)
5938a07d286SRatan Gupta     {
5948a07d286SRatan Gupta         return;
5958a07d286SRatan Gupta     }
5968a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
5978a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
5988a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
5998a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
6008a07d286SRatan Gupta     {
6018a07d286SRatan Gupta         return;
6028a07d286SRatan Gupta     }
6038a07d286SRatan Gupta }
6048a07d286SRatan Gupta /**
6058a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6068a07d286SRatan Gupta           json response with the new value.
6078a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6088a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6098a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6108a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6118a07d286SRatan Gupta  */
6128a07d286SRatan Gupta 
6134f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6148a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6158d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6168a07d286SRatan Gupta     const std::string& ldapServerElementName,
6178a07d286SRatan Gupta     const std::string& ldapConfigObject)
6188a07d286SRatan Gupta {
6198a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6208a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
6218a07d286SRatan Gupta          serviceAddressList](const boost::system::error_code ec) {
6228a07d286SRatan Gupta         if (ec)
6238a07d286SRatan Gupta         {
6248a07d286SRatan Gupta             BMCWEB_LOG_DEBUG
625c61704abSGunnar Mills                 << "Error Occurred in updating the service address";
6268a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6278a07d286SRatan Gupta             return;
6288a07d286SRatan Gupta         }
6298a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
6308a07d286SRatan Gupta             serviceAddressList.front()};
631002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
6328a07d286SRatan Gupta             modifiedserviceAddressList;
6338a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
6348a07d286SRatan Gupta         {
635002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
6368a07d286SRatan Gupta                                             serviceAddressList.front());
6378a07d286SRatan Gupta         }
6388a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
6398a07d286SRatan Gupta         },
6408a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6418a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
642168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
6438a07d286SRatan Gupta }
6448a07d286SRatan Gupta /**
6458a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
6468a07d286SRatan Gupta           json response with the new value.
6478a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
6488a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6498a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6508a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6518a07d286SRatan Gupta  */
6528a07d286SRatan Gupta 
6534f48d5f6SEd Tanous inline void
6544f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
6558d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6568a07d286SRatan Gupta                         const std::string& ldapServerElementName,
6578a07d286SRatan Gupta                         const std::string& ldapConfigObject)
6588a07d286SRatan Gupta {
6598a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6608a07d286SRatan Gupta         [asyncResp, username,
6618a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
6628a07d286SRatan Gupta         if (ec)
6638a07d286SRatan Gupta         {
6646c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
6658a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6668a07d286SRatan Gupta             return;
6678a07d286SRatan Gupta         }
668002d39b4SEd Tanous         asyncResp->res
669002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Username"] =
670002d39b4SEd Tanous             username;
6718a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
6728a07d286SRatan Gupta         },
6738a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
674168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
675168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
6768a07d286SRatan Gupta }
6778a07d286SRatan Gupta 
6788a07d286SRatan Gupta /**
6798a07d286SRatan Gupta  * @brief updates the LDAP password
6808a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
6818a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6828a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6838a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
6848a07d286SRatan Gupta  */
6858a07d286SRatan Gupta 
6864f48d5f6SEd Tanous inline void
6874f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
6888d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6898a07d286SRatan Gupta                         const std::string& ldapServerElementName,
6908a07d286SRatan Gupta                         const std::string& ldapConfigObject)
6918a07d286SRatan Gupta {
6928a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6938a07d286SRatan Gupta         [asyncResp, password,
6948a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
6958a07d286SRatan Gupta         if (ec)
6968a07d286SRatan Gupta         {
6976c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
6988a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6998a07d286SRatan Gupta             return;
7008a07d286SRatan Gupta         }
701002d39b4SEd Tanous         asyncResp->res
702002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Password"] =
703002d39b4SEd Tanous             "";
7048a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
7058a07d286SRatan Gupta         },
7068a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7078a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
708168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
7098a07d286SRatan Gupta }
7108a07d286SRatan Gupta 
7118a07d286SRatan Gupta /**
7128a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7138a07d286SRatan Gupta           json response with the new value.
7148a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7158a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7168a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7178a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7188a07d286SRatan Gupta  */
7198a07d286SRatan Gupta 
7204f48d5f6SEd Tanous inline void
7214f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7228d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7238a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7248a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7258a07d286SRatan Gupta {
7268a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7278a07d286SRatan Gupta         [asyncResp, baseDNList,
7288a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7298a07d286SRatan Gupta         if (ec)
7308a07d286SRatan Gupta         {
7316c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
7328a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7338a07d286SRatan Gupta             return;
7348a07d286SRatan Gupta         }
735002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7368a07d286SRatan Gupta         auto& searchSettingsJson =
7378a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7386c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
7396c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
7408a07d286SRatan Gupta         if (baseDNList.size() > 1)
7418a07d286SRatan Gupta         {
742002d39b4SEd Tanous             messages::propertyValueModified(
743002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
7448a07d286SRatan Gupta         }
7458a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
7468a07d286SRatan Gupta         },
7478a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7488a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
749168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
7508a07d286SRatan Gupta }
7518a07d286SRatan Gupta /**
7528a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
7538a07d286SRatan Gupta           json response with the new value.
7548a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
7558a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7568a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7578a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7588a07d286SRatan Gupta  */
7598a07d286SRatan Gupta 
7604f48d5f6SEd Tanous inline void
7614f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
7628d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7638a07d286SRatan Gupta                             const std::string& ldapServerElementName,
7648a07d286SRatan Gupta                             const std::string& ldapConfigObject)
7658a07d286SRatan Gupta {
7668a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7678a07d286SRatan Gupta         [asyncResp, userNameAttribute,
7688a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7698a07d286SRatan Gupta         if (ec)
7708a07d286SRatan Gupta         {
771c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
7728a07d286SRatan Gupta                                 "username attribute";
7738a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7748a07d286SRatan Gupta             return;
7758a07d286SRatan Gupta         }
776002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7778a07d286SRatan Gupta         auto& searchSettingsJson =
7788a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7798a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
7808a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
7818a07d286SRatan Gupta         },
7828a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7838a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
784168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
7858a07d286SRatan Gupta }
7868a07d286SRatan Gupta /**
7878a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
7888a07d286SRatan Gupta           json response with the new value.
7898a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
7908a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7918a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7928a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7938a07d286SRatan Gupta  */
7948a07d286SRatan Gupta 
7954f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
7968d1b46d7Szhanghch05     const std::string& groupsAttribute,
7978d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7988a07d286SRatan Gupta     const std::string& ldapServerElementName,
7998a07d286SRatan Gupta     const std::string& ldapConfigObject)
8008a07d286SRatan Gupta {
8018a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8028a07d286SRatan Gupta         [asyncResp, groupsAttribute,
8038a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8048a07d286SRatan Gupta         if (ec)
8058a07d286SRatan Gupta         {
806c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8078a07d286SRatan Gupta                                 "groupname attribute";
8088a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8098a07d286SRatan Gupta             return;
8108a07d286SRatan Gupta         }
811002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8128a07d286SRatan Gupta         auto& searchSettingsJson =
8138a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8148a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8158a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8168a07d286SRatan Gupta         },
8178a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8188a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
819168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
8208a07d286SRatan Gupta }
8218a07d286SRatan Gupta /**
8228a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
8238a07d286SRatan Gupta           json response with the new value.
8248a07d286SRatan Gupta  * @param input JSON data.
8258a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8268a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8278a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8288a07d286SRatan Gupta  */
8298a07d286SRatan Gupta 
8304f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8316c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8328a07d286SRatan Gupta     const std::string& ldapServerElementName,
8338a07d286SRatan Gupta     const std::string& ldapConfigObject)
8348a07d286SRatan Gupta {
8358a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8368a07d286SRatan Gupta         [asyncResp, serviceEnabled,
8378a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8388a07d286SRatan Gupta         if (ec)
8398a07d286SRatan Gupta         {
840002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
8418a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8428a07d286SRatan Gupta             return;
8438a07d286SRatan Gupta         }
8446c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
8458a07d286SRatan Gupta             serviceEnabled;
8466c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
8478a07d286SRatan Gupta         },
8488a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
849168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
850168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
8518a07d286SRatan Gupta }
8528a07d286SRatan Gupta 
8534f48d5f6SEd Tanous inline void
8544f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
8558d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
85678158631SZbigniew Kurzynski {
85778158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
85878158631SZbigniew Kurzynski     std::optional<bool> cookie;
85978158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
86078158631SZbigniew Kurzynski     std::optional<bool> xToken;
861501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
86278158631SZbigniew Kurzynski 
86378158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
86478158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
865501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
86678158631SZbigniew Kurzynski     {
86778158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
86878158631SZbigniew Kurzynski         return;
86978158631SZbigniew Kurzynski     }
87078158631SZbigniew Kurzynski 
87178158631SZbigniew Kurzynski     // Make a copy of methods configuration
87252cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
87352cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
87478158631SZbigniew Kurzynski 
87578158631SZbigniew Kurzynski     if (basicAuth)
87678158631SZbigniew Kurzynski     {
877f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
878f16f6263SAlan Kuo         messages::actionNotSupported(
8790fda0f12SGeorge Liu             asyncResp->res,
8800fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
881f16f6263SAlan Kuo         return;
882f16f6263SAlan Kuo #endif
88378158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
88478158631SZbigniew Kurzynski     }
88578158631SZbigniew Kurzynski 
88678158631SZbigniew Kurzynski     if (cookie)
88778158631SZbigniew Kurzynski     {
888f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
8890fda0f12SGeorge Liu         messages::actionNotSupported(
8900fda0f12SGeorge Liu             asyncResp->res,
8910fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
892f16f6263SAlan Kuo         return;
893f16f6263SAlan Kuo #endif
89478158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
89578158631SZbigniew Kurzynski     }
89678158631SZbigniew Kurzynski 
89778158631SZbigniew Kurzynski     if (sessionToken)
89878158631SZbigniew Kurzynski     {
899f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
900f16f6263SAlan Kuo         messages::actionNotSupported(
9010fda0f12SGeorge Liu             asyncResp->res,
9020fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
903f16f6263SAlan Kuo         return;
904f16f6263SAlan Kuo #endif
90578158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
90678158631SZbigniew Kurzynski     }
90778158631SZbigniew Kurzynski 
90878158631SZbigniew Kurzynski     if (xToken)
90978158631SZbigniew Kurzynski     {
910f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
9110fda0f12SGeorge Liu         messages::actionNotSupported(
9120fda0f12SGeorge Liu             asyncResp->res,
9130fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
914f16f6263SAlan Kuo         return;
915f16f6263SAlan Kuo #endif
91678158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
91778158631SZbigniew Kurzynski     }
91878158631SZbigniew Kurzynski 
919501f1e58SZbigniew Kurzynski     if (tls)
920501f1e58SZbigniew Kurzynski     {
921f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
9220fda0f12SGeorge Liu         messages::actionNotSupported(
9230fda0f12SGeorge Liu             asyncResp->res,
9240fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
925f16f6263SAlan Kuo         return;
926f16f6263SAlan Kuo #endif
927501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
928501f1e58SZbigniew Kurzynski     }
929501f1e58SZbigniew Kurzynski 
93078158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
931501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
932501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
93378158631SZbigniew Kurzynski     {
93478158631SZbigniew Kurzynski         // Do not allow user to disable everything
93578158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
93678158631SZbigniew Kurzynski                                      "of disabling all available methods");
93778158631SZbigniew Kurzynski         return;
93878158631SZbigniew Kurzynski     }
93978158631SZbigniew Kurzynski 
94052cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
94152cc112dSEd Tanous         authMethodsConfig);
94278158631SZbigniew Kurzynski     // Save configuration immediately
94352cc112dSEd Tanous     persistent_data::getConfig().writeData();
94478158631SZbigniew Kurzynski 
94578158631SZbigniew Kurzynski     messages::success(asyncResp->res);
94678158631SZbigniew Kurzynski }
94778158631SZbigniew Kurzynski 
9488a07d286SRatan Gupta /**
9498a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
9508a07d286SRatan Gupta  *        value and create the LDAP config object.
9518a07d286SRatan Gupta  * @param input JSON data
9528a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9538a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
9548a07d286SRatan Gupta  */
9558a07d286SRatan Gupta 
9566c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
9578d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9588a07d286SRatan Gupta                             const std::string& serverType)
9598a07d286SRatan Gupta {
960eb2bbe56SRatan Gupta     std::string dbusObjectPath;
961eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
962eb2bbe56SRatan Gupta     {
9632c70f800SEd Tanous         dbusObjectPath = adConfigObject;
964eb2bbe56SRatan Gupta     }
965eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
966eb2bbe56SRatan Gupta     {
96723a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
968eb2bbe56SRatan Gupta     }
969cb13a392SEd Tanous     else
970cb13a392SEd Tanous     {
971cb13a392SEd Tanous         return;
972cb13a392SEd Tanous     }
973eb2bbe56SRatan Gupta 
9748a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
9758a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
9768a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
9778a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
9788a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
9798a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
9808a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
9818a07d286SRatan Gupta     std::optional<std::string> userName;
9828a07d286SRatan Gupta     std::optional<std::string> password;
98306785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
9848a07d286SRatan Gupta 
9858a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
9868a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
9878a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
98806785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
98906785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
9908a07d286SRatan Gupta     {
9918a07d286SRatan Gupta         return;
9928a07d286SRatan Gupta     }
9938a07d286SRatan Gupta 
9948a07d286SRatan Gupta     if (authentication)
9958a07d286SRatan Gupta     {
9968a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
9978a07d286SRatan Gupta                                     password);
9988a07d286SRatan Gupta     }
9998a07d286SRatan Gupta     if (ldapService)
10008a07d286SRatan Gupta     {
10018a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10028a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
10038a07d286SRatan Gupta     }
10048a07d286SRatan Gupta     if (serviceAddressList)
10058a07d286SRatan Gupta     {
100626f6976fSEd Tanous         if (serviceAddressList->empty())
10078a07d286SRatan Gupta         {
10088a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10098a07d286SRatan Gupta                                              "ServiceAddress");
10108a07d286SRatan Gupta             return;
10118a07d286SRatan Gupta         }
10128a07d286SRatan Gupta     }
10138a07d286SRatan Gupta     if (baseDNList)
10148a07d286SRatan Gupta     {
101526f6976fSEd Tanous         if (baseDNList->empty())
10168a07d286SRatan Gupta         {
10178a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10188a07d286SRatan Gupta                                              "BaseDistinguishedNames");
10198a07d286SRatan Gupta             return;
10208a07d286SRatan Gupta         }
10218a07d286SRatan Gupta     }
10228a07d286SRatan Gupta 
10238a07d286SRatan Gupta     // nothing to update, then return
10248a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
102506785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
102606785244SRatan Gupta         !remoteRoleMapData)
10278a07d286SRatan Gupta     {
10288a07d286SRatan Gupta         return;
10298a07d286SRatan Gupta     }
10308a07d286SRatan Gupta 
10318a07d286SRatan Gupta     // Get the existing resource first then keep modifying
10328a07d286SRatan Gupta     // whenever any property gets updated.
1033002d39b4SEd Tanous     getLDAPConfigData(
1034002d39b4SEd Tanous         serverType,
1035002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1036002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1037002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
103823a21a1cSEd Tanous                             const std::string& serverT) {
10398a07d286SRatan Gupta         if (!success)
10408a07d286SRatan Gupta         {
10418a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10428a07d286SRatan Gupta             return;
10438a07d286SRatan Gupta         }
10446c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
10458a07d286SRatan Gupta         if (confData.serviceEnabled)
10468a07d286SRatan Gupta         {
10478a07d286SRatan Gupta             // Disable the service first and update the rest of
10488a07d286SRatan Gupta             // the properties.
10496c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
10508a07d286SRatan Gupta         }
10518a07d286SRatan Gupta 
10528a07d286SRatan Gupta         if (serviceAddressList)
10538a07d286SRatan Gupta         {
10546c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
10556c51eab1SEd Tanous                                       dbusObjectPath);
10568a07d286SRatan Gupta         }
10578a07d286SRatan Gupta         if (userName)
10588a07d286SRatan Gupta         {
10596c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
10608a07d286SRatan Gupta         }
10618a07d286SRatan Gupta         if (password)
10628a07d286SRatan Gupta         {
10636c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
10648a07d286SRatan Gupta         }
10658a07d286SRatan Gupta 
10668a07d286SRatan Gupta         if (baseDNList)
10678a07d286SRatan Gupta         {
10686c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
10698a07d286SRatan Gupta         }
10708a07d286SRatan Gupta         if (userNameAttribute)
10718a07d286SRatan Gupta         {
10726c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
10736c51eab1SEd Tanous                                     dbusObjectPath);
10748a07d286SRatan Gupta         }
10758a07d286SRatan Gupta         if (groupsAttribute)
10768a07d286SRatan Gupta         {
10776c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
10786c51eab1SEd Tanous                                      dbusObjectPath);
10798a07d286SRatan Gupta         }
10808a07d286SRatan Gupta         if (serviceEnabled)
10818a07d286SRatan Gupta         {
10828a07d286SRatan Gupta             // if user has given the value as true then enable
10838a07d286SRatan Gupta             // the service. if user has given false then no-op
10848a07d286SRatan Gupta             // as service is already stopped.
10858a07d286SRatan Gupta             if (*serviceEnabled)
10868a07d286SRatan Gupta             {
10876c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
10886c51eab1SEd Tanous                                          dbusObjectPath);
10898a07d286SRatan Gupta             }
10908a07d286SRatan Gupta         }
10918a07d286SRatan Gupta         else
10928a07d286SRatan Gupta         {
10938a07d286SRatan Gupta             // if user has not given the service enabled value
10948a07d286SRatan Gupta             // then revert it to the same state as it was
10958a07d286SRatan Gupta             // before.
10968a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
109723a21a1cSEd Tanous                                      serverT, dbusObjectPath);
10988a07d286SRatan Gupta         }
109906785244SRatan Gupta 
110006785244SRatan Gupta         if (remoteRoleMapData)
110106785244SRatan Gupta         {
11026c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
11036c51eab1SEd Tanous                                *remoteRoleMapData);
110406785244SRatan Gupta         }
11058a07d286SRatan Gupta         });
11068a07d286SRatan Gupta }
1107d4b5443fSEd Tanous 
11086c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
11096c51eab1SEd Tanous                                  const std::string& username,
11106c51eab1SEd Tanous                                  std::optional<std::string> password,
11116c51eab1SEd Tanous                                  std::optional<bool> enabled,
11126c51eab1SEd Tanous                                  std::optional<std::string> roleId,
11136c51eab1SEd Tanous                                  std::optional<bool> locked)
11141abe55efSEd Tanous {
1115b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1116b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1117b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
11186c51eab1SEd Tanous 
11196c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
11206c51eab1SEd Tanous         dbusObjectPath,
11211106333aSEd Tanous         [dbusObjectPath, username, password(std::move(password)),
11221106333aSEd Tanous          roleId(std::move(roleId)), enabled, locked,
11231106333aSEd Tanous          asyncResp{std::move(asyncResp)}](int rc) {
1124e662eae8SEd Tanous         if (rc <= 0)
11256c51eab1SEd Tanous         {
1126d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11276c51eab1SEd Tanous                                        username);
11286c51eab1SEd Tanous             return;
11296c51eab1SEd Tanous         }
11306c51eab1SEd Tanous 
11316c51eab1SEd Tanous         if (password)
11326c51eab1SEd Tanous         {
11336c51eab1SEd Tanous             int retval = pamUpdatePassword(username, *password);
11346c51eab1SEd Tanous 
11356c51eab1SEd Tanous             if (retval == PAM_USER_UNKNOWN)
11366c51eab1SEd Tanous             {
1137d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11386c51eab1SEd Tanous                                            username);
11396c51eab1SEd Tanous             }
11406c51eab1SEd Tanous             else if (retval == PAM_AUTHTOK_ERR)
11416c51eab1SEd Tanous             {
11426c51eab1SEd Tanous                 // If password is invalid
1143002d39b4SEd Tanous                 messages::propertyValueFormatError(asyncResp->res, *password,
1144002d39b4SEd Tanous                                                    "Password");
11456c51eab1SEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
11466c51eab1SEd Tanous             }
11476c51eab1SEd Tanous             else if (retval != PAM_SUCCESS)
11486c51eab1SEd Tanous             {
11496c51eab1SEd Tanous                 messages::internalError(asyncResp->res);
11506c51eab1SEd Tanous                 return;
11516c51eab1SEd Tanous             }
1152e7b1b62bSEd Tanous             else
1153e7b1b62bSEd Tanous             {
1154e7b1b62bSEd Tanous                 messages::success(asyncResp->res);
1155e7b1b62bSEd Tanous             }
11566c51eab1SEd Tanous         }
11576c51eab1SEd Tanous 
11586c51eab1SEd Tanous         if (enabled)
11596c51eab1SEd Tanous         {
11606c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
11616c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
11626c51eab1SEd Tanous                 if (ec)
11636c51eab1SEd Tanous                 {
11646c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
11656c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11666c51eab1SEd Tanous                     return;
11676c51eab1SEd Tanous                 }
11686c51eab1SEd Tanous                 messages::success(asyncResp->res);
11696c51eab1SEd Tanous                 return;
11706c51eab1SEd Tanous                 },
1171e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
11726c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
11736c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserEnabled",
1174168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*enabled});
11756c51eab1SEd Tanous         }
11766c51eab1SEd Tanous 
11776c51eab1SEd Tanous         if (roleId)
11786c51eab1SEd Tanous         {
11796c51eab1SEd Tanous             std::string priv = getPrivilegeFromRoleId(*roleId);
11806c51eab1SEd Tanous             if (priv.empty())
11816c51eab1SEd Tanous             {
11826c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, *roleId,
11836c51eab1SEd Tanous                                                  "RoleId");
11846c51eab1SEd Tanous                 return;
11856c51eab1SEd Tanous             }
11866c51eab1SEd Tanous             if (priv == "priv-noaccess")
11876c51eab1SEd Tanous             {
11886c51eab1SEd Tanous                 priv = "";
11896c51eab1SEd Tanous             }
11906c51eab1SEd Tanous 
11916c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
11926c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
11936c51eab1SEd Tanous                 if (ec)
11946c51eab1SEd Tanous                 {
11956c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
11966c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11976c51eab1SEd Tanous                     return;
11986c51eab1SEd Tanous                 }
11996c51eab1SEd Tanous                 messages::success(asyncResp->res);
12006c51eab1SEd Tanous                 },
1201e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12026c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12036c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1204168e20c1SEd Tanous                 dbus::utility::DbusVariantType{priv});
12056c51eab1SEd Tanous         }
12066c51eab1SEd Tanous 
12076c51eab1SEd Tanous         if (locked)
12086c51eab1SEd Tanous         {
12096c51eab1SEd Tanous             // admin can unlock the account which is locked by
12106c51eab1SEd Tanous             // successive authentication failures but admin should
12116c51eab1SEd Tanous             // not be allowed to lock an account.
12126c51eab1SEd Tanous             if (*locked)
12136c51eab1SEd Tanous             {
12146c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, "true",
12156c51eab1SEd Tanous                                                  "Locked");
12166c51eab1SEd Tanous                 return;
12176c51eab1SEd Tanous             }
12186c51eab1SEd Tanous 
12196c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
12206c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
12216c51eab1SEd Tanous                 if (ec)
12226c51eab1SEd Tanous                 {
12236c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12246c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12256c51eab1SEd Tanous                     return;
12266c51eab1SEd Tanous                 }
12276c51eab1SEd Tanous                 messages::success(asyncResp->res);
12286c51eab1SEd Tanous                 return;
12296c51eab1SEd Tanous                 },
1230e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12316c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12326c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes",
1233168e20c1SEd Tanous                 "UserLockedForFailedAttempt",
1234168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*locked});
12356c51eab1SEd Tanous         }
12366c51eab1SEd Tanous         });
12376c51eab1SEd Tanous }
12386c51eab1SEd Tanous 
12394c7d4d33SEd Tanous inline void handleAccountServiceHead(
12404c7d4d33SEd Tanous     App& app, const crow::Request& req,
12411ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12426c51eab1SEd Tanous {
12434c7d4d33SEd Tanous 
12443ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
124545ca1b86SEd Tanous     {
124645ca1b86SEd Tanous         return;
124745ca1b86SEd Tanous     }
12484c7d4d33SEd Tanous     asyncResp->res.addHeader(
12494c7d4d33SEd Tanous         boost::beast::http::field::link,
12504c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
12514c7d4d33SEd Tanous }
12524c7d4d33SEd Tanous 
12534c7d4d33SEd Tanous inline void
12544c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
12554c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12564c7d4d33SEd Tanous {
12574c7d4d33SEd Tanous     handleAccountServiceHead(app, req, asyncResp);
125852cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
12591ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
126078158631SZbigniew Kurzynski 
12611476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
12621476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
12631476687dSEd Tanous     json["@odata.type"] = "#AccountService."
12641476687dSEd Tanous                           "v1_10_0.AccountService";
12651476687dSEd Tanous     json["Id"] = "AccountService";
12661476687dSEd Tanous     json["Name"] = "Account Service";
12671476687dSEd Tanous     json["Description"] = "Account Service";
12681476687dSEd Tanous     json["ServiceEnabled"] = true;
12691476687dSEd Tanous     json["MaxPasswordLength"] = 20;
12701ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
12711476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
12721476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
12731476687dSEd Tanous         "#OemAccountService.v1_0_0.AccountService";
12741476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
12751476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
12761476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
12771476687dSEd Tanous         authMethodsConfig.basic;
12781476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
12791476687dSEd Tanous         authMethodsConfig.sessionToken;
12801ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
12811ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
12821ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
12831476687dSEd Tanous 
12841ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
12851ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
12861ef4c342SEd Tanous     // permissions ConfigureManager
128772048780SAbhishek Patel     Privileges effectiveUserPrivileges =
128872048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
128972048780SAbhishek Patel 
129072048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
129172048780SAbhishek Patel                                          effectiveUserPrivileges))
129272048780SAbhishek Patel     {
12931ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
12941476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
129572048780SAbhishek Patel     }
1296*d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1297*d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1298*d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
1299002d39b4SEd Tanous         [asyncResp](const boost::system::error_code ec,
13001ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
13013d958bbcSAppaRao Puli         if (ec)
13023d958bbcSAppaRao Puli         {
13033d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
13043d958bbcSAppaRao Puli             return;
13053d958bbcSAppaRao Puli         }
1306*d1bde9e5SKrzysztof Grobelny 
13073d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
13083d958bbcSAppaRao Puli                          << "properties for AccountService";
1309*d1bde9e5SKrzysztof Grobelny 
1310*d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1311*d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1312*d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1313*d1bde9e5SKrzysztof Grobelny 
1314*d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1315*d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1316*d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1317*d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1318*d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1319*d1bde9e5SKrzysztof Grobelny 
1320*d1bde9e5SKrzysztof Grobelny         if (!success)
13213d958bbcSAppaRao Puli         {
1322*d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1323*d1bde9e5SKrzysztof Grobelny             return;
13243d958bbcSAppaRao Puli         }
1325*d1bde9e5SKrzysztof Grobelny 
1326*d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
13273d958bbcSAppaRao Puli         {
1328*d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
13293d958bbcSAppaRao Puli         }
1330*d1bde9e5SKrzysztof Grobelny 
1331*d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
13323d958bbcSAppaRao Puli         {
1333*d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1334*d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1335*d1bde9e5SKrzysztof Grobelny         }
1336*d1bde9e5SKrzysztof Grobelny 
1337*d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
13383d958bbcSAppaRao Puli         {
1339002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1340*d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
13413d958bbcSAppaRao Puli         }
1342*d1bde9e5SKrzysztof Grobelny         });
13436973a582SRatan Gupta 
134402cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1345ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1346cb13a392SEd Tanous         if (!success)
1347cb13a392SEd Tanous         {
1348cb13a392SEd Tanous             return;
1349cb13a392SEd Tanous         }
1350002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1351ab828d7cSRatan Gupta     };
1352ab828d7cSRatan Gupta 
1353ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1354ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
13551ef4c342SEd Tanous }
13566973a582SRatan Gupta 
13571ef4c342SEd Tanous inline void handleAccountServicePatch(
13581ef4c342SEd Tanous     App& app, const crow::Request& req,
13591ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13601ef4c342SEd Tanous {
13613ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
136245ca1b86SEd Tanous     {
136345ca1b86SEd Tanous         return;
136445ca1b86SEd Tanous     }
1365f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1366f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1367ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1368f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1369f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1370f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1371f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1372f5ffd806SEd Tanous 
137315ed6780SWilly Tu     if (!json_util::readJsonPatch(
13741ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
13751ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
13761ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
13771ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1378f5ffd806SEd Tanous             oemObject))
1379f5ffd806SEd Tanous     {
1380f5ffd806SEd Tanous         return;
1381f5ffd806SEd Tanous     }
1382f5ffd806SEd Tanous 
1383f5ffd806SEd Tanous     if (minPasswordLength)
1384f5ffd806SEd Tanous     {
1385ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
1386ef73ad0dSPaul Fertser             [asyncResp](const boost::system::error_code ec) {
1387ef73ad0dSPaul Fertser             if (ec)
1388ef73ad0dSPaul Fertser             {
1389ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1390ef73ad0dSPaul Fertser                 return;
1391ef73ad0dSPaul Fertser             }
1392ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1393ef73ad0dSPaul Fertser             },
13941ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1395ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
13961ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1397ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1398f5ffd806SEd Tanous     }
1399f5ffd806SEd Tanous 
1400f5ffd806SEd Tanous     if (maxPasswordLength)
1401f5ffd806SEd Tanous     {
14021ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1403f5ffd806SEd Tanous     }
1404f5ffd806SEd Tanous 
1405f5ffd806SEd Tanous     if (ldapObject)
1406f5ffd806SEd Tanous     {
1407f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1408f5ffd806SEd Tanous     }
1409f5ffd806SEd Tanous 
1410f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
14111ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1412f5ffd806SEd Tanous                                          oemOpenBMCObject))
1413f5ffd806SEd Tanous     {
1414f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1415f5ffd806SEd Tanous             oemOpenBMCObject &&
1416f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1417f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1418f5ffd806SEd Tanous         {
1419f5ffd806SEd Tanous             if (authMethodsObject)
1420f5ffd806SEd Tanous             {
14211ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1422f5ffd806SEd Tanous             }
1423f5ffd806SEd Tanous         }
1424f5ffd806SEd Tanous     }
1425f5ffd806SEd Tanous 
1426f5ffd806SEd Tanous     if (activeDirectoryObject)
1427f5ffd806SEd Tanous     {
14281ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1429f5ffd806SEd Tanous     }
1430f5ffd806SEd Tanous 
1431f5ffd806SEd Tanous     if (unlockTimeout)
1432f5ffd806SEd Tanous     {
1433f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1434f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1435f5ffd806SEd Tanous             if (ec)
1436f5ffd806SEd Tanous             {
1437f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1438f5ffd806SEd Tanous                 return;
1439f5ffd806SEd Tanous             }
1440f5ffd806SEd Tanous             messages::success(asyncResp->res);
1441f5ffd806SEd Tanous             },
14421ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1443f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
14441ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1445168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1446f5ffd806SEd Tanous     }
1447f5ffd806SEd Tanous     if (lockoutThreshold)
1448f5ffd806SEd Tanous     {
1449f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1450f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1451f5ffd806SEd Tanous             if (ec)
1452f5ffd806SEd Tanous             {
1453f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1454f5ffd806SEd Tanous                 return;
1455f5ffd806SEd Tanous             }
1456f5ffd806SEd Tanous             messages::success(asyncResp->res);
1457f5ffd806SEd Tanous             },
14581ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1459f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1460f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1461f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1462168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1463f5ffd806SEd Tanous     }
14641ef4c342SEd Tanous }
1465f5ffd806SEd Tanous 
14664c7d4d33SEd Tanous inline void handleAccountCollectionHead(
14671ef4c342SEd Tanous     App& app, const crow::Request& req,
14681ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14691ef4c342SEd Tanous {
14704c7d4d33SEd Tanous 
14713ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
147245ca1b86SEd Tanous     {
147345ca1b86SEd Tanous         return;
147445ca1b86SEd Tanous     }
14754c7d4d33SEd Tanous     asyncResp->res.addHeader(
14764c7d4d33SEd Tanous         boost::beast::http::field::link,
14774c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
14784c7d4d33SEd Tanous }
14794c7d4d33SEd Tanous 
14804c7d4d33SEd Tanous inline void handleAccountCollectionGet(
14814c7d4d33SEd Tanous     App& app, const crow::Request& req,
14824c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14834c7d4d33SEd Tanous {
14844c7d4d33SEd Tanous     handleAccountCollectionHead(app, req, asyncResp);
14851476687dSEd Tanous 
14861476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
14871476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
14881ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
14891476687dSEd Tanous                                               "ManagerAccountCollection";
14901476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
14911476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
14920f74e643SEd Tanous 
14936c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
14946c51eab1SEd Tanous         redfish::getUserPrivileges(req.userRole);
14956c51eab1SEd Tanous 
1496f5e29f33SJunLin Chen     std::string thisUser;
1497f5e29f33SJunLin Chen     if (req.session)
1498f5e29f33SJunLin Chen     {
1499f5e29f33SJunLin Chen         thisUser = req.session->username;
1500f5e29f33SJunLin Chen     }
1501b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
1502cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
1503cef1ddfbSEd Tanous             const boost::system::error_code ec,
1504711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1505b9b2e0b2SEd Tanous         if (ec)
1506b9b2e0b2SEd Tanous         {
1507f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1508b9b2e0b2SEd Tanous             return;
1509b9b2e0b2SEd Tanous         }
1510b9b2e0b2SEd Tanous 
1511cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1512002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1513cef1ddfbSEd Tanous 
1514cef1ddfbSEd Tanous         bool userCanSeeSelf =
1515002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1516cef1ddfbSEd Tanous 
1517002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1518b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1519b9b2e0b2SEd Tanous 
15209eb808c1SEd Tanous         for (const auto& userpath : users)
1521b9b2e0b2SEd Tanous         {
15222dfd18efSEd Tanous             std::string user = userpath.first.filename();
15232dfd18efSEd Tanous             if (user.empty())
1524b9b2e0b2SEd Tanous             {
15252dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
15262dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
15272dfd18efSEd Tanous 
15282dfd18efSEd Tanous                 return;
1529b9b2e0b2SEd Tanous             }
1530f365910cSGunnar Mills 
1531f365910cSGunnar Mills             // As clarified by Redfish here:
1532f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
15336c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
15346c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
15356c51eab1SEd Tanous             // accounts.
15361ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1537f365910cSGunnar Mills             {
15381476687dSEd Tanous                 nlohmann::json::object_t member;
15391476687dSEd Tanous                 member["@odata.id"] =
1540002d39b4SEd Tanous                     "/redfish/v1/AccountService/Accounts/" + user;
15411476687dSEd Tanous                 memberArray.push_back(std::move(member));
1542b9b2e0b2SEd Tanous             }
1543f365910cSGunnar Mills         }
15441ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1545b9b2e0b2SEd Tanous         },
15461ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1547b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15481ef4c342SEd Tanous }
15496c51eab1SEd Tanous 
15501ef4c342SEd Tanous inline void handleAccountCollectionPost(
15511ef4c342SEd Tanous     App& app, const crow::Request& req,
15521ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15531ef4c342SEd Tanous {
15543ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
155545ca1b86SEd Tanous     {
155645ca1b86SEd Tanous         return;
155745ca1b86SEd Tanous     }
15589712f8acSEd Tanous     std::string username;
15599712f8acSEd Tanous     std::string password;
1560a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1561a24526dcSEd Tanous     std::optional<bool> enabled = true;
15621ef4c342SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
15631ef4c342SEd Tanous                                   "Password", password, "RoleId", roleId,
15641ef4c342SEd Tanous                                   "Enabled", enabled))
156504ae99ecSEd Tanous     {
156604ae99ecSEd Tanous         return;
156704ae99ecSEd Tanous     }
156804ae99ecSEd Tanous 
156954fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
157084e12cb7SAppaRao Puli     if (priv.empty())
157104ae99ecSEd Tanous     {
15721ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
157304ae99ecSEd Tanous         return;
157404ae99ecSEd Tanous     }
157596200606Sjayaprakash Mutyala     // TODO: Following override will be reverted once support in
15766c51eab1SEd Tanous     // phosphor-user-manager is added. In order to avoid dependency
15776c51eab1SEd Tanous     // issues, this is added in bmcweb, which will removed, once
157896200606Sjayaprakash Mutyala     // phosphor-user-manager supports priv-noaccess.
157996200606Sjayaprakash Mutyala     if (priv == "priv-noaccess")
158096200606Sjayaprakash Mutyala     {
158196200606Sjayaprakash Mutyala         roleId = "";
158296200606Sjayaprakash Mutyala     }
158396200606Sjayaprakash Mutyala     else
158496200606Sjayaprakash Mutyala     {
15859712f8acSEd Tanous         roleId = priv;
158696200606Sjayaprakash Mutyala     }
158704ae99ecSEd Tanous 
1588599c71d8SAyushi Smriti     // Reading AllGroups property
15891e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
15901ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
15911ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
15921ef4c342SEd Tanous         "AllGroups",
1593599c71d8SAyushi Smriti         [asyncResp, username, password{std::move(password)}, roleId,
1594168e20c1SEd Tanous          enabled](const boost::system::error_code ec,
15951e1e598dSJonathan Doman                   const std::vector<std::string>& allGroupsList) {
1596599c71d8SAyushi Smriti         if (ec)
1597599c71d8SAyushi Smriti         {
1598599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1599599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1600599c71d8SAyushi Smriti             return;
1601599c71d8SAyushi Smriti         }
1602599c71d8SAyushi Smriti 
16031e1e598dSJonathan Doman         if (allGroupsList.empty())
1604599c71d8SAyushi Smriti         {
1605599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1606599c71d8SAyushi Smriti             return;
1607599c71d8SAyushi Smriti         }
1608599c71d8SAyushi Smriti 
160904ae99ecSEd Tanous         crow::connections::systemBus->async_method_call(
16101ef4c342SEd Tanous             [asyncResp, username, password](const boost::system::error_code ec2,
161159d494eeSPatrick Williams                                             sdbusplus::message_t& m) {
161223a21a1cSEd Tanous             if (ec2)
161304ae99ecSEd Tanous             {
16141ef4c342SEd Tanous                 userErrorMessageHandler(m.get_error(), asyncResp, username, "");
161504ae99ecSEd Tanous                 return;
161604ae99ecSEd Tanous             }
161704ae99ecSEd Tanous 
1618002d39b4SEd Tanous             if (pamUpdatePassword(username, password) != PAM_SUCCESS)
161904ae99ecSEd Tanous             {
16206c51eab1SEd Tanous                 // At this point we have a user that's been
16216c51eab1SEd Tanous                 // created, but the password set
16226c51eab1SEd Tanous                 // failed.Something is wrong, so delete the user
16236c51eab1SEd Tanous                 // that we've already created
16241ef4c342SEd Tanous                 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1625b477fd44SP Dheeraj Srujan Kumar                 tempObjPath /= username;
1626b477fd44SP Dheeraj Srujan Kumar                 const std::string userPath(tempObjPath);
1627b477fd44SP Dheeraj Srujan Kumar 
162804ae99ecSEd Tanous                 crow::connections::systemBus->async_method_call(
16291ef4c342SEd Tanous                     [asyncResp, password](const boost::system::error_code ec3) {
163023a21a1cSEd Tanous                     if (ec3)
163104ae99ecSEd Tanous                     {
1632002d39b4SEd Tanous                         messages::internalError(asyncResp->res);
163304ae99ecSEd Tanous                         return;
163404ae99ecSEd Tanous                     }
163504ae99ecSEd Tanous 
16360d4197e0Sanil kumar appana                     // If password is invalid
16371ef4c342SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, password,
16381ef4c342SEd Tanous                                                        "Password");
163904ae99ecSEd Tanous                     },
1640002d39b4SEd Tanous                     "xyz.openbmc_project.User.Manager", userPath,
1641002d39b4SEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
164204ae99ecSEd Tanous 
164304ae99ecSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
164404ae99ecSEd Tanous                 return;
164504ae99ecSEd Tanous             }
164604ae99ecSEd Tanous 
1647f12894f8SJason M. Bills             messages::created(asyncResp->res);
164804ae99ecSEd Tanous             asyncResp->res.addHeader(
16491ef4c342SEd Tanous                 "Location", "/redfish/v1/AccountService/Accounts/" + username);
165004ae99ecSEd Tanous             },
1651002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1652002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "CreateUser", username,
1653002d39b4SEd Tanous             allGroupsList, *roleId, *enabled);
16541e1e598dSJonathan Doman         });
16551ef4c342SEd Tanous }
1656b9b2e0b2SEd Tanous 
16571ef4c342SEd Tanous inline void
16584c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
16596c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16604c7d4d33SEd Tanous                       const std::string& /*accountName*/)
16611ef4c342SEd Tanous {
16624c7d4d33SEd Tanous 
16633ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
166445ca1b86SEd Tanous     {
166545ca1b86SEd Tanous         return;
166645ca1b86SEd Tanous     }
16674c7d4d33SEd Tanous     asyncResp->res.addHeader(
16684c7d4d33SEd Tanous         boost::beast::http::field::link,
16694c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
16704c7d4d33SEd Tanous }
16714c7d4d33SEd Tanous inline void
16724c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
16734c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16744c7d4d33SEd Tanous                      const std::string& accountName)
16754c7d4d33SEd Tanous {
16764c7d4d33SEd Tanous     handleAccountHead(app, req, asyncResp, accountName);
16771ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1678031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1679d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1680031514fbSJunLin Chen     return;
1681031514fbSJunLin Chen 
16821ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1683031514fbSJunLin Chen     if (req.session == nullptr)
1684031514fbSJunLin Chen     {
1685031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1686031514fbSJunLin Chen         return;
1687031514fbSJunLin Chen     }
16886c51eab1SEd Tanous     if (req.session->username != accountName)
1689b9b2e0b2SEd Tanous     {
16906c51eab1SEd Tanous         // At this point we've determined that the user is trying to
16911ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
16921ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
16931ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
16946c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
16956c51eab1SEd Tanous             redfish::getUserPrivileges(req.userRole);
16961ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
16971ef4c342SEd Tanous                                                          "ConfigureManager"};
16986c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
16996c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1700900f9497SJoseph Reynolds         {
1701900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
1702900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1703900f9497SJoseph Reynolds             return;
1704900f9497SJoseph Reynolds         }
1705900f9497SJoseph Reynolds     }
1706900f9497SJoseph Reynolds 
1707b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
17081ef4c342SEd Tanous         [asyncResp,
17091ef4c342SEd Tanous          accountName](const boost::system::error_code ec,
1710711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1711b9b2e0b2SEd Tanous         if (ec)
1712b9b2e0b2SEd Tanous         {
1713f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1714b9b2e0b2SEd Tanous             return;
1715b9b2e0b2SEd Tanous         }
1716711ac7a9SEd Tanous         const auto userIt = std::find_if(
1717b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
1718b477fd44SP Dheeraj Srujan Kumar             [accountName](
1719b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
1720002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
172155f79e6fSEd Tanous             return accountName == user.first.filename();
1722b477fd44SP Dheeraj Srujan Kumar             });
1723b9b2e0b2SEd Tanous 
172484e12cb7SAppaRao Puli         if (userIt == users.end())
1725b9b2e0b2SEd Tanous         {
1726002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1727002d39b4SEd Tanous                                        accountName);
172884e12cb7SAppaRao Puli             return;
172984e12cb7SAppaRao Puli         }
17304e68c45bSAyushi Smriti 
17311476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
17321476687dSEd Tanous             "#ManagerAccount.v1_4_0.ManagerAccount";
17331476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
17341476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
17351476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
17361476687dSEd Tanous         asyncResp->res.jsonValue["AccountTypes"] = {"Redfish"};
17374e68c45bSAyushi Smriti 
173884e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
173965b0dc32SEd Tanous         {
1740002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
174165b0dc32SEd Tanous             {
174265b0dc32SEd Tanous                 for (const auto& property : interface.second)
174365b0dc32SEd Tanous                 {
174465b0dc32SEd Tanous                     if (property.first == "UserEnabled")
174565b0dc32SEd Tanous                     {
174665b0dc32SEd Tanous                         const bool* userEnabled =
1747abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
174865b0dc32SEd Tanous                         if (userEnabled == nullptr)
174965b0dc32SEd Tanous                         {
1750002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
175184e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
175284e12cb7SAppaRao Puli                             return;
175365b0dc32SEd Tanous                         }
1754002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
175565b0dc32SEd Tanous                     }
1756002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
175765b0dc32SEd Tanous                     {
175865b0dc32SEd Tanous                         const bool* userLocked =
1759abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
176065b0dc32SEd Tanous                         if (userLocked == nullptr)
176165b0dc32SEd Tanous                         {
176284e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
176384e12cb7SAppaRao Puli                                                 "ailedAttempt "
176484e12cb7SAppaRao Puli                                                 "wasn't a bool";
176584e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
176684e12cb7SAppaRao Puli                             return;
176765b0dc32SEd Tanous                         }
1768002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
1769002d39b4SEd Tanous                         asyncResp->res
1770002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
17713bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
177265b0dc32SEd Tanous                     }
177384e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
177484e12cb7SAppaRao Puli                     {
177554fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1776002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
177754fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
177884e12cb7SAppaRao Puli                         {
1779002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
178084e12cb7SAppaRao Puli                                                 "string";
178184e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
178284e12cb7SAppaRao Puli                             return;
178384e12cb7SAppaRao Puli                         }
17841ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
178554fc587aSNagaraju Goruganti                         if (role.empty())
178684e12cb7SAppaRao Puli                         {
178784e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
178884e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
178984e12cb7SAppaRao Puli                             return;
179084e12cb7SAppaRao Puli                         }
179154fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
179284e12cb7SAppaRao Puli 
17931476687dSEd Tanous                         nlohmann::json& roleEntry =
1794002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
17951476687dSEd Tanous                         roleEntry["@odata.id"] =
1796002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
179784e12cb7SAppaRao Puli                     }
1798002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
17993bf4e632SJoseph Reynolds                     {
18003bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
18013bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
18023bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
18033bf4e632SJoseph Reynolds                         {
18040fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
18050fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
18063bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
18073bf4e632SJoseph Reynolds                             return;
18083bf4e632SJoseph Reynolds                         }
1809002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
18103bf4e632SJoseph Reynolds                             *userPasswordExpired;
18113bf4e632SJoseph Reynolds                     }
181265b0dc32SEd Tanous                 }
181365b0dc32SEd Tanous             }
181465b0dc32SEd Tanous         }
181565b0dc32SEd Tanous 
1816b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
181784e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
1818b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
1819b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
1820b9b2e0b2SEd Tanous         },
18211ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1822b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18231ef4c342SEd Tanous }
1824a840879dSEd Tanous 
18251ef4c342SEd Tanous inline void
18261ef4c342SEd Tanous     handleAccounttDelete(App& app, const crow::Request& req,
18276c51eab1SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18281ef4c342SEd Tanous                          const std::string& username)
18291ef4c342SEd Tanous {
18303ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
183145ca1b86SEd Tanous     {
183245ca1b86SEd Tanous         return;
183345ca1b86SEd Tanous     }
18341ef4c342SEd Tanous 
18351ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1836031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1837d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1838031514fbSJunLin Chen     return;
1839031514fbSJunLin Chen 
18401ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
18411ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
18421ef4c342SEd Tanous     tempObjPath /= username;
18431ef4c342SEd Tanous     const std::string userPath(tempObjPath);
18441ef4c342SEd Tanous 
18451ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
18461ef4c342SEd Tanous         [asyncResp, username](const boost::system::error_code ec) {
18471ef4c342SEd Tanous         if (ec)
18481ef4c342SEd Tanous         {
1849d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
18501ef4c342SEd Tanous                                        username);
18511ef4c342SEd Tanous             return;
18521ef4c342SEd Tanous         }
18531ef4c342SEd Tanous 
18541ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
18551ef4c342SEd Tanous         },
18561ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
18571ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
18581ef4c342SEd Tanous }
18591ef4c342SEd Tanous 
18601ef4c342SEd Tanous inline void
18611ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
18621ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18631ef4c342SEd Tanous                        const std::string& username)
18641ef4c342SEd Tanous {
18651ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18661ef4c342SEd Tanous     {
18671ef4c342SEd Tanous         return;
18681ef4c342SEd Tanous     }
18691ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
18701ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
1871d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
18721ef4c342SEd Tanous     return;
18731ef4c342SEd Tanous 
18741ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1875a24526dcSEd Tanous     std::optional<std::string> newUserName;
1876a24526dcSEd Tanous     std::optional<std::string> password;
1877a24526dcSEd Tanous     std::optional<bool> enabled;
1878a24526dcSEd Tanous     std::optional<std::string> roleId;
187924c8542dSRatan Gupta     std::optional<bool> locked;
1880e9cc5172SEd Tanous 
1881031514fbSJunLin Chen     if (req.session == nullptr)
1882031514fbSJunLin Chen     {
1883031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1884031514fbSJunLin Chen         return;
1885031514fbSJunLin Chen     }
1886031514fbSJunLin Chen 
1887e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
1888e9cc5172SEd Tanous         redfish::getUserPrivileges(req.userRole);
1889e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
1890e9cc5172SEd Tanous     bool userHasConfigureUsers =
1891e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
1892e9cc5172SEd Tanous     if (userHasConfigureUsers)
1893e9cc5172SEd Tanous     {
1894e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
18951ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "UserName",
18961ef4c342SEd Tanous                                       newUserName, "Password", password,
18971ef4c342SEd Tanous                                       "RoleId", roleId, "Enabled", enabled,
18981ef4c342SEd Tanous                                       "Locked", locked))
1899a840879dSEd Tanous         {
1900a840879dSEd Tanous             return;
1901a840879dSEd Tanous         }
1902e9cc5172SEd Tanous     }
1903e9cc5172SEd Tanous     else
1904900f9497SJoseph Reynolds     {
1905e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
1906e9cc5172SEd Tanous         if (username != req.session->username)
1907900f9497SJoseph Reynolds         {
1908900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1909900f9497SJoseph Reynolds             return;
1910900f9497SJoseph Reynolds         }
1911031514fbSJunLin Chen 
1912e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
19131ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
19141ef4c342SEd Tanous                                       password))
1915e9cc5172SEd Tanous         {
1916e9cc5172SEd Tanous             return;
1917e9cc5172SEd Tanous         }
1918900f9497SJoseph Reynolds     }
1919900f9497SJoseph Reynolds 
192066b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
19216c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
19226c51eab1SEd Tanous     // updating user properties other then username. If username
19236c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
19246c51eab1SEd Tanous     // user rename request.
192566b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
1926a840879dSEd Tanous     {
19271ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
19281ef4c342SEd Tanous                              locked);
192984e12cb7SAppaRao Puli         return;
193084e12cb7SAppaRao Puli     }
193184e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
19326c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
19331ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
193459d494eeSPatrick Williams          locked](const boost::system::error_code ec, sdbusplus::message_t& m) {
193584e12cb7SAppaRao Puli         if (ec)
193684e12cb7SAppaRao Puli         {
1937002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
1938002d39b4SEd Tanous                                     username);
1939a840879dSEd Tanous             return;
1940a840879dSEd Tanous         }
1941a840879dSEd Tanous 
1942002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
1943002d39b4SEd Tanous                              locked);
194484e12cb7SAppaRao Puli         },
19451ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
194684e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
194784e12cb7SAppaRao Puli         *newUserName);
19481ef4c342SEd Tanous }
19491ef4c342SEd Tanous 
19501ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
19511ef4c342SEd Tanous {
19521ef4c342SEd Tanous 
19531ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
19544c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
19554c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
19564c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
19574c7d4d33SEd Tanous 
19584c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
19591ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
19601ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
19611ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
19621ef4c342SEd Tanous 
19631ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
19641ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
19651ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
19661ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
19671ef4c342SEd Tanous 
19681ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
19694c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
19704c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
19714c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
19724c7d4d33SEd Tanous 
19734c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
19741ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
19751ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
19761ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
19771ef4c342SEd Tanous 
19781ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
19791ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
19801ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
19811ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
19821ef4c342SEd Tanous 
19831ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
19844c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
19854c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
19864c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
19874c7d4d33SEd Tanous 
19884c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
19891ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
19901ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
19911ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
19921ef4c342SEd Tanous 
19931ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
19941ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
19951ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
19961ef4c342SEd Tanous         // yet
19971ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
19981ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
19991ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
200084e12cb7SAppaRao Puli 
20016c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2002ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
20036c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
20041ef4c342SEd Tanous             std::bind_front(handleAccounttDelete, std::ref(app)));
200506e086d9SEd Tanous }
200688d16c9aSLewanczyk, Dawid 
200788d16c9aSLewanczyk, Dawid } // namespace redfish
2008