xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 002d39b4a7a5ed7166e2acad84e0943c3def9492)
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>
26a840879dSEd Tanous #include <utils/json_utils.hpp>
271214b7e7SGunnar Mills 
281abe55efSEd Tanous namespace redfish
291abe55efSEd Tanous {
3088d16c9aSLewanczyk, Dawid 
3123a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
326973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
332c70f800SEd Tanous constexpr const char* adConfigObject =
34ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
35ab828d7cSRatan Gupta 
36b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
376973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
386973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
396973a582SRatan Gupta constexpr const char* ldapConfigInterface =
406973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
416973a582SRatan Gupta constexpr const char* ldapCreateInterface =
426973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
436973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
4406785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
4506785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
466973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
476973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
486973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
496973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
506973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
516973a582SRatan Gupta 
5254fc587aSNagaraju Goruganti struct LDAPRoleMapData
5354fc587aSNagaraju Goruganti {
5454fc587aSNagaraju Goruganti     std::string groupName;
5554fc587aSNagaraju Goruganti     std::string privilege;
5654fc587aSNagaraju Goruganti };
5754fc587aSNagaraju Goruganti 
586973a582SRatan Gupta struct LDAPConfigData
596973a582SRatan Gupta {
606973a582SRatan Gupta     std::string uri{};
616973a582SRatan Gupta     std::string bindDN{};
626973a582SRatan Gupta     std::string baseDN{};
636973a582SRatan Gupta     std::string searchScope{};
646973a582SRatan Gupta     std::string serverType{};
656973a582SRatan Gupta     bool serviceEnabled = false;
666973a582SRatan Gupta     std::string userNameAttribute{};
676973a582SRatan Gupta     std::string groupAttribute{};
6854fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
696973a582SRatan Gupta };
706973a582SRatan Gupta 
7154fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7284e12cb7SAppaRao Puli {
7384e12cb7SAppaRao Puli     if (role == "priv-admin")
7484e12cb7SAppaRao Puli     {
7584e12cb7SAppaRao Puli         return "Administrator";
7684e12cb7SAppaRao Puli     }
773174e4dfSEd Tanous     if (role == "priv-user")
7884e12cb7SAppaRao Puli     {
79c80fee55SAppaRao Puli         return "ReadOnly";
8084e12cb7SAppaRao Puli     }
813174e4dfSEd Tanous     if (role == "priv-operator")
8284e12cb7SAppaRao Puli     {
8384e12cb7SAppaRao Puli         return "Operator";
8484e12cb7SAppaRao Puli     }
8526f6976fSEd Tanous     if (role.empty() || (role == "priv-noaccess"))
86e9e6d240Sjayaprakash Mutyala     {
87e9e6d240Sjayaprakash Mutyala         return "NoAccess";
88e9e6d240Sjayaprakash Mutyala     }
8984e12cb7SAppaRao Puli     return "";
9084e12cb7SAppaRao Puli }
9154fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9284e12cb7SAppaRao Puli {
9384e12cb7SAppaRao Puli     if (role == "Administrator")
9484e12cb7SAppaRao Puli     {
9584e12cb7SAppaRao Puli         return "priv-admin";
9684e12cb7SAppaRao Puli     }
973174e4dfSEd Tanous     if (role == "ReadOnly")
9884e12cb7SAppaRao Puli     {
9984e12cb7SAppaRao Puli         return "priv-user";
10084e12cb7SAppaRao Puli     }
1013174e4dfSEd Tanous     if (role == "Operator")
10284e12cb7SAppaRao Puli     {
10384e12cb7SAppaRao Puli         return "priv-operator";
10484e12cb7SAppaRao Puli     }
10526f6976fSEd Tanous     if ((role == "NoAccess") || (role.empty()))
106e9e6d240Sjayaprakash Mutyala     {
107e9e6d240Sjayaprakash Mutyala         return "priv-noaccess";
108e9e6d240Sjayaprakash Mutyala     }
10984e12cb7SAppaRao Puli     return "";
11084e12cb7SAppaRao Puli }
111b9b2e0b2SEd Tanous 
1128d1b46d7Szhanghch05 inline void userErrorMessageHandler(
1138d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1148d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
11566b5ca76Sjayaprakash Mutyala {
11666b5ca76Sjayaprakash Mutyala     if (e == nullptr)
11766b5ca76Sjayaprakash Mutyala     {
11866b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
11966b5ca76Sjayaprakash Mutyala         return;
12066b5ca76Sjayaprakash Mutyala     }
12166b5ca76Sjayaprakash Mutyala 
122055806b3SManojkiran Eda     const char* errorMessage = e->name;
12366b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
12466b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
12566b5ca76Sjayaprakash Mutyala     {
12666b5ca76Sjayaprakash Mutyala         messages::resourceAlreadyExists(asyncResp->res,
1278114bd4dSGunnar Mills                                         "#ManagerAccount.v1_4_0.ManagerAccount",
12866b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
12966b5ca76Sjayaprakash Mutyala     }
13066b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
13166b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
13266b5ca76Sjayaprakash Mutyala     {
13366b5ca76Sjayaprakash Mutyala         messages::resourceNotFound(
1348114bd4dSGunnar Mills             asyncResp->res, "#ManagerAccount.v1_4_0.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                         {
271*002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
27206785244SRatan Gupta                             messages::internalError(asyncResp->res);
27306785244SRatan Gupta                             return;
27406785244SRatan Gupta                         }
27506785244SRatan Gupta                         asyncResp->res
276*002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
277*002d39b4SEd 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                         {
295*002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
29606785244SRatan Gupta                             messages::internalError(asyncResp->res);
29706785244SRatan Gupta                             return;
29806785244SRatan Gupta                         }
29906785244SRatan Gupta                         asyncResp->res
300*002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
301*002d39b4SEd 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
388*002d39b4SEd 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(
395*002d39b4SEd Tanous             [callback,
396*002d39b4SEd 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);
402*002d39b4SEd 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             {
423*002d39b4SEd 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
436*002d39b4SEd 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 =
467*002d39b4SEd 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                             }
484*002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
4856973a582SRatan Gupta                             {
486271584abSEd Tanous                                 confData.searchScope = *strValue;
4876973a582SRatan Gupta                             }
488*002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
4896973a582SRatan Gupta                             {
490271584abSEd Tanous                                 confData.groupAttribute = *strValue;
4916973a582SRatan Gupta                             }
492*002d39b4SEd 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                     }
502*002d39b4SEd 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 =
509*002d39b4SEd 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 
526*002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
527*002d39b4SEd Tanous                                                             roleMapData);
52854fc587aSNagaraju Goruganti                     }
52954fc587aSNagaraju Goruganti                 }
53054fc587aSNagaraju Goruganti             }
531ab828d7cSRatan Gupta             callback(true, confData, ldapType);
53254fc587aSNagaraju Goruganti             },
533*002d39b4SEd 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()};
631*002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
6328a07d286SRatan Gupta             modifiedserviceAddressList;
6338a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
6348a07d286SRatan Gupta         {
635*002d39b4SEd 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         }
668*002d39b4SEd Tanous         asyncResp->res
669*002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Username"] =
670*002d39b4SEd 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         }
701*002d39b4SEd Tanous         asyncResp->res
702*002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Password"] =
703*002d39b4SEd 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         }
735*002d39b4SEd 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         {
742*002d39b4SEd Tanous             messages::propertyValueModified(
743*002d39b4SEd 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         }
776*002d39b4SEd 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         }
811*002d39b4SEd 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         {
840*002d39b4SEd 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.
1033*002d39b4SEd Tanous     getLDAPConfigData(
1034*002d39b4SEd Tanous         serverType,
1035*002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1036*002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1037*002d39b4SEd 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         {
1126*002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res,
1127*002d39b4SEd Tanous                                        "#ManagerAccount.v1_4_0.ManagerAccount",
11286c51eab1SEd Tanous                                        username);
11296c51eab1SEd Tanous             return;
11306c51eab1SEd Tanous         }
11316c51eab1SEd Tanous 
11326c51eab1SEd Tanous         if (password)
11336c51eab1SEd Tanous         {
11346c51eab1SEd Tanous             int retval = pamUpdatePassword(username, *password);
11356c51eab1SEd Tanous 
11366c51eab1SEd Tanous             if (retval == PAM_USER_UNKNOWN)
11376c51eab1SEd Tanous             {
11386c51eab1SEd Tanous                 messages::resourceNotFound(
11396c51eab1SEd Tanous                     asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
11406c51eab1SEd Tanous                     username);
11416c51eab1SEd Tanous             }
11426c51eab1SEd Tanous             else if (retval == PAM_AUTHTOK_ERR)
11436c51eab1SEd Tanous             {
11446c51eab1SEd Tanous                 // If password is invalid
1145*002d39b4SEd Tanous                 messages::propertyValueFormatError(asyncResp->res, *password,
1146*002d39b4SEd Tanous                                                    "Password");
11476c51eab1SEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
11486c51eab1SEd Tanous             }
11496c51eab1SEd Tanous             else if (retval != PAM_SUCCESS)
11506c51eab1SEd Tanous             {
11516c51eab1SEd Tanous                 messages::internalError(asyncResp->res);
11526c51eab1SEd Tanous                 return;
11536c51eab1SEd Tanous             }
1154e7b1b62bSEd Tanous             else
1155e7b1b62bSEd Tanous             {
1156e7b1b62bSEd Tanous                 messages::success(asyncResp->res);
1157e7b1b62bSEd Tanous             }
11586c51eab1SEd Tanous         }
11596c51eab1SEd Tanous 
11606c51eab1SEd Tanous         if (enabled)
11616c51eab1SEd Tanous         {
11626c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
11636c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
11646c51eab1SEd Tanous                 if (ec)
11656c51eab1SEd Tanous                 {
11666c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
11676c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11686c51eab1SEd Tanous                     return;
11696c51eab1SEd Tanous                 }
11706c51eab1SEd Tanous                 messages::success(asyncResp->res);
11716c51eab1SEd Tanous                 return;
11726c51eab1SEd Tanous                 },
1173e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
11746c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
11756c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserEnabled",
1176168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*enabled});
11776c51eab1SEd Tanous         }
11786c51eab1SEd Tanous 
11796c51eab1SEd Tanous         if (roleId)
11806c51eab1SEd Tanous         {
11816c51eab1SEd Tanous             std::string priv = getPrivilegeFromRoleId(*roleId);
11826c51eab1SEd Tanous             if (priv.empty())
11836c51eab1SEd Tanous             {
11846c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, *roleId,
11856c51eab1SEd Tanous                                                  "RoleId");
11866c51eab1SEd Tanous                 return;
11876c51eab1SEd Tanous             }
11886c51eab1SEd Tanous             if (priv == "priv-noaccess")
11896c51eab1SEd Tanous             {
11906c51eab1SEd Tanous                 priv = "";
11916c51eab1SEd Tanous             }
11926c51eab1SEd Tanous 
11936c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
11946c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
11956c51eab1SEd Tanous                 if (ec)
11966c51eab1SEd Tanous                 {
11976c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
11986c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11996c51eab1SEd Tanous                     return;
12006c51eab1SEd Tanous                 }
12016c51eab1SEd Tanous                 messages::success(asyncResp->res);
12026c51eab1SEd Tanous                 },
1203e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12046c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12056c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1206168e20c1SEd Tanous                 dbus::utility::DbusVariantType{priv});
12076c51eab1SEd Tanous         }
12086c51eab1SEd Tanous 
12096c51eab1SEd Tanous         if (locked)
12106c51eab1SEd Tanous         {
12116c51eab1SEd Tanous             // admin can unlock the account which is locked by
12126c51eab1SEd Tanous             // successive authentication failures but admin should
12136c51eab1SEd Tanous             // not be allowed to lock an account.
12146c51eab1SEd Tanous             if (*locked)
12156c51eab1SEd Tanous             {
12166c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, "true",
12176c51eab1SEd Tanous                                                  "Locked");
12186c51eab1SEd Tanous                 return;
12196c51eab1SEd Tanous             }
12206c51eab1SEd Tanous 
12216c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
12226c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
12236c51eab1SEd Tanous                 if (ec)
12246c51eab1SEd Tanous                 {
12256c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12266c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12276c51eab1SEd Tanous                     return;
12286c51eab1SEd Tanous                 }
12296c51eab1SEd Tanous                 messages::success(asyncResp->res);
12306c51eab1SEd Tanous                 return;
12316c51eab1SEd Tanous                 },
1232e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12336c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12346c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes",
1235168e20c1SEd Tanous                 "UserLockedForFailedAttempt",
1236168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*locked});
12376c51eab1SEd Tanous         }
12386c51eab1SEd Tanous         });
12396c51eab1SEd Tanous }
12406c51eab1SEd Tanous 
12416c51eab1SEd Tanous inline void requestAccountServiceRoutes(App& app)
12426c51eab1SEd Tanous {
12436c51eab1SEd Tanous 
12446c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
1245ed398213SEd Tanous         .privileges(redfish::privileges::getAccountService)
1246*002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1247*002d39b4SEd Tanous             [&app](
1248*002d39b4SEd Tanous                 const crow::Request& req,
1249*002d39b4SEd Tanous                 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
125045ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
125145ca1b86SEd Tanous                 {
125245ca1b86SEd Tanous                     return;
125345ca1b86SEd Tanous                 }
125452cc112dSEd Tanous                 const persistent_data::AuthConfigMethods& authMethodsConfig =
12556c51eab1SEd Tanous                     persistent_data::SessionStore::getInstance()
12566c51eab1SEd Tanous                         .getAuthMethodsConfig();
125778158631SZbigniew Kurzynski 
12581476687dSEd Tanous                 nlohmann::json& json = asyncResp->res.jsonValue;
12591476687dSEd Tanous                 json["@odata.id"] = "/redfish/v1/AccountService";
12601476687dSEd Tanous                 json["@odata.type"] = "#AccountService."
12611476687dSEd Tanous                                       "v1_10_0.AccountService";
12621476687dSEd Tanous                 json["Id"] = "AccountService";
12631476687dSEd Tanous                 json["Name"] = "Account Service";
12641476687dSEd Tanous                 json["Description"] = "Account Service";
12651476687dSEd Tanous                 json["ServiceEnabled"] = true;
12661476687dSEd Tanous                 json["MaxPasswordLength"] = 20;
12671476687dSEd Tanous                 json["Accounts"]["@odata.id"] =
12681476687dSEd Tanous                     "/redfish/v1/AccountService/Accounts";
12691476687dSEd Tanous                 json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
12701476687dSEd Tanous                 json["Oem"]["OpenBMC"]["@odata.type"] =
12711476687dSEd Tanous                     "#OemAccountService.v1_0_0.AccountService";
12721476687dSEd Tanous                 json["Oem"]["OpenBMC"]["@odata.id"] =
12731476687dSEd Tanous                     "/redfish/v1/AccountService#/Oem/OpenBMC";
12741476687dSEd Tanous                 json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
12751476687dSEd Tanous                     authMethodsConfig.basic;
12761476687dSEd Tanous                 json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
12771476687dSEd Tanous                     authMethodsConfig.sessionToken;
12781476687dSEd Tanous                 json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] =
12791476687dSEd Tanous                     authMethodsConfig.xtoken;
12801476687dSEd Tanous                 json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] =
12811476687dSEd Tanous                     authMethodsConfig.cookie;
12821476687dSEd Tanous                 json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] =
12831476687dSEd Tanous                     authMethodsConfig.tls;
12841476687dSEd Tanous 
1285*002d39b4SEd Tanous                 // /redfish/v1/AccountService/LDAP/Certificates is something
1286*002d39b4SEd Tanous                 // only ConfigureManager can access then only display when the
1287*002d39b4SEd Tanous                 // user has permissions ConfigureManager
128872048780SAbhishek Patel                 Privileges effectiveUserPrivileges =
128972048780SAbhishek Patel                     redfish::getUserPrivileges(req.userRole);
129072048780SAbhishek Patel 
129172048780SAbhishek Patel                 if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
129272048780SAbhishek Patel                                                      effectiveUserPrivileges))
129372048780SAbhishek Patel                 {
1294*002d39b4SEd Tanous                     asyncResp->res
1295*002d39b4SEd Tanous                         .jsonValue["LDAP"]["Certificates"]["@odata.id"] =
12961476687dSEd Tanous                         "/redfish/v1/AccountService/LDAP/Certificates";
129772048780SAbhishek Patel                 }
12983d958bbcSAppaRao Puli                 crow::connections::systemBus->async_method_call(
1299*002d39b4SEd Tanous                     [asyncResp](const boost::system::error_code ec,
1300*002d39b4SEd Tanous                                 const dbus::utility::DBusPropertiesMap&
1301*002d39b4SEd Tanous                                     propertiesList) {
13023d958bbcSAppaRao Puli             if (ec)
13033d958bbcSAppaRao Puli             {
13043d958bbcSAppaRao Puli                 messages::internalError(asyncResp->res);
13053d958bbcSAppaRao Puli                 return;
13063d958bbcSAppaRao Puli             }
13073d958bbcSAppaRao Puli             BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
13083d958bbcSAppaRao Puli                              << "properties for AccountService";
1309*002d39b4SEd Tanous             for (const std::pair<std::string, dbus::utility::DbusVariantType>&
1310168e20c1SEd Tanous                      property : propertiesList)
13113d958bbcSAppaRao Puli             {
13123d958bbcSAppaRao Puli                 if (property.first == "MinPasswordLength")
13133d958bbcSAppaRao Puli                 {
13143d958bbcSAppaRao Puli                     const uint8_t* value =
1315abf2add6SEd Tanous                         std::get_if<uint8_t>(&property.second);
13163d958bbcSAppaRao Puli                     if (value != nullptr)
13173d958bbcSAppaRao Puli                     {
1318*002d39b4SEd Tanous                         asyncResp->res.jsonValue["MinPasswordLength"] = *value;
13193d958bbcSAppaRao Puli                     }
13203d958bbcSAppaRao Puli                 }
13213d958bbcSAppaRao Puli                 if (property.first == "AccountUnlockTimeout")
13223d958bbcSAppaRao Puli                 {
13233d958bbcSAppaRao Puli                     const uint32_t* value =
1324abf2add6SEd Tanous                         std::get_if<uint32_t>(&property.second);
13253d958bbcSAppaRao Puli                     if (value != nullptr)
13263d958bbcSAppaRao Puli                     {
1327*002d39b4SEd Tanous                         asyncResp->res.jsonValue["AccountLockoutDuration"] =
13283d958bbcSAppaRao Puli                             *value;
13293d958bbcSAppaRao Puli                     }
13303d958bbcSAppaRao Puli                 }
13313d958bbcSAppaRao Puli                 if (property.first == "MaxLoginAttemptBeforeLockout")
13323d958bbcSAppaRao Puli                 {
13333d958bbcSAppaRao Puli                     const uint16_t* value =
1334abf2add6SEd Tanous                         std::get_if<uint16_t>(&property.second);
13353d958bbcSAppaRao Puli                     if (value != nullptr)
13363d958bbcSAppaRao Puli                     {
1337*002d39b4SEd Tanous                         asyncResp->res.jsonValue["AccountLockoutThreshold"] =
13386c51eab1SEd Tanous                             *value;
13393d958bbcSAppaRao Puli                     }
13403d958bbcSAppaRao Puli                 }
13413d958bbcSAppaRao Puli             }
13423d958bbcSAppaRao Puli                     },
1343*002d39b4SEd Tanous                     "xyz.openbmc_project.User.Manager",
1344*002d39b4SEd Tanous                     "/xyz/openbmc_project/user",
13453d958bbcSAppaRao Puli                     "org.freedesktop.DBus.Properties", "GetAll",
13463d958bbcSAppaRao Puli                     "xyz.openbmc_project.User.AccountPolicy");
13476973a582SRatan Gupta 
1348*002d39b4SEd Tanous                 auto callback =
1349*002d39b4SEd Tanous                     [asyncResp](bool success, LDAPConfigData& confData,
1350ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1351cb13a392SEd Tanous             if (!success)
1352cb13a392SEd Tanous             {
1353cb13a392SEd Tanous                 return;
1354cb13a392SEd Tanous             }
1355*002d39b4SEd Tanous             parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1356ab828d7cSRatan Gupta                 };
1357ab828d7cSRatan Gupta 
1358ab828d7cSRatan Gupta                 getLDAPConfigData("LDAP", callback);
1359ab828d7cSRatan Gupta                 getLDAPConfigData("ActiveDirectory", callback);
13606c51eab1SEd Tanous             });
13616973a582SRatan Gupta 
1362f5ffd806SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
13631ec43ee3SGunnar Mills         .privileges(redfish::privileges::patchAccountService)
1364f5ffd806SEd Tanous         .methods(boost::beast::http::verb::patch)(
136545ca1b86SEd Tanous             [&app](
136645ca1b86SEd Tanous                 const crow::Request& req,
1367f5ffd806SEd Tanous                 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
136845ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
136945ca1b86SEd Tanous                 {
137045ca1b86SEd Tanous                     return;
137145ca1b86SEd Tanous                 }
1372f5ffd806SEd Tanous                 std::optional<uint32_t> unlockTimeout;
1373f5ffd806SEd Tanous                 std::optional<uint16_t> lockoutThreshold;
1374ef73ad0dSPaul Fertser                 std::optional<uint8_t> minPasswordLength;
1375f5ffd806SEd Tanous                 std::optional<uint16_t> maxPasswordLength;
1376f5ffd806SEd Tanous                 std::optional<nlohmann::json> ldapObject;
1377f5ffd806SEd Tanous                 std::optional<nlohmann::json> activeDirectoryObject;
1378f5ffd806SEd Tanous                 std::optional<nlohmann::json> oemObject;
1379f5ffd806SEd Tanous 
138015ed6780SWilly Tu                 if (!json_util::readJsonPatch(
1381f5ffd806SEd Tanous                         req, asyncResp->res, "AccountLockoutDuration",
1382f5ffd806SEd Tanous                         unlockTimeout, "AccountLockoutThreshold",
1383f5ffd806SEd Tanous                         lockoutThreshold, "MaxPasswordLength",
1384f5ffd806SEd Tanous                         maxPasswordLength, "MinPasswordLength",
1385f5ffd806SEd Tanous                         minPasswordLength, "LDAP", ldapObject,
1386f5ffd806SEd Tanous                         "ActiveDirectory", activeDirectoryObject, "Oem",
1387f5ffd806SEd Tanous                         oemObject))
1388f5ffd806SEd Tanous                 {
1389f5ffd806SEd Tanous                     return;
1390f5ffd806SEd Tanous                 }
1391f5ffd806SEd Tanous 
1392f5ffd806SEd Tanous                 if (minPasswordLength)
1393f5ffd806SEd Tanous                 {
1394ef73ad0dSPaul Fertser                     crow::connections::systemBus->async_method_call(
1395ef73ad0dSPaul Fertser                         [asyncResp](const boost::system::error_code ec) {
1396ef73ad0dSPaul Fertser                 if (ec)
1397ef73ad0dSPaul Fertser                 {
1398ef73ad0dSPaul Fertser                     messages::internalError(asyncResp->res);
1399ef73ad0dSPaul Fertser                     return;
1400ef73ad0dSPaul Fertser                 }
1401ef73ad0dSPaul Fertser                 messages::success(asyncResp->res);
1402ef73ad0dSPaul Fertser                         },
1403ef73ad0dSPaul Fertser                         "xyz.openbmc_project.User.Manager",
1404ef73ad0dSPaul Fertser                         "/xyz/openbmc_project/user",
1405ef73ad0dSPaul Fertser                         "org.freedesktop.DBus.Properties", "Set",
1406ef73ad0dSPaul Fertser                         "xyz.openbmc_project.User.AccountPolicy",
1407ef73ad0dSPaul Fertser                         "MinPasswordLength",
1408ef73ad0dSPaul Fertser                         dbus::utility::DbusVariantType(*minPasswordLength));
1409f5ffd806SEd Tanous                 }
1410f5ffd806SEd Tanous 
1411f5ffd806SEd Tanous                 if (maxPasswordLength)
1412f5ffd806SEd Tanous                 {
1413f5ffd806SEd Tanous                     messages::propertyNotWritable(asyncResp->res,
1414f5ffd806SEd Tanous                                                   "MaxPasswordLength");
1415f5ffd806SEd Tanous                 }
1416f5ffd806SEd Tanous 
1417f5ffd806SEd Tanous                 if (ldapObject)
1418f5ffd806SEd Tanous                 {
1419f5ffd806SEd Tanous                     handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1420f5ffd806SEd Tanous                 }
1421f5ffd806SEd Tanous 
1422f5ffd806SEd Tanous                 if (std::optional<nlohmann::json> oemOpenBMCObject;
1423f5ffd806SEd Tanous                     oemObject &&
1424f5ffd806SEd Tanous                     json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1425f5ffd806SEd Tanous                                         oemOpenBMCObject))
1426f5ffd806SEd Tanous                 {
1427f5ffd806SEd Tanous                     if (std::optional<nlohmann::json> authMethodsObject;
1428f5ffd806SEd Tanous                         oemOpenBMCObject &&
1429f5ffd806SEd Tanous                         json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1430f5ffd806SEd Tanous                                             "AuthMethods", authMethodsObject))
1431f5ffd806SEd Tanous                     {
1432f5ffd806SEd Tanous                         if (authMethodsObject)
1433f5ffd806SEd Tanous                         {
1434f5ffd806SEd Tanous                             handleAuthMethodsPatch(*authMethodsObject,
1435f5ffd806SEd Tanous                                                    asyncResp);
1436f5ffd806SEd Tanous                         }
1437f5ffd806SEd Tanous                     }
1438f5ffd806SEd Tanous                 }
1439f5ffd806SEd Tanous 
1440f5ffd806SEd Tanous                 if (activeDirectoryObject)
1441f5ffd806SEd Tanous                 {
1442f5ffd806SEd Tanous                     handleLDAPPatch(*activeDirectoryObject, asyncResp,
1443f5ffd806SEd Tanous                                     "ActiveDirectory");
1444f5ffd806SEd Tanous                 }
1445f5ffd806SEd Tanous 
1446f5ffd806SEd Tanous                 if (unlockTimeout)
1447f5ffd806SEd Tanous                 {
1448f5ffd806SEd Tanous                     crow::connections::systemBus->async_method_call(
1449f5ffd806SEd Tanous                         [asyncResp](const boost::system::error_code ec) {
1450f5ffd806SEd Tanous                 if (ec)
1451f5ffd806SEd Tanous                 {
1452f5ffd806SEd Tanous                     messages::internalError(asyncResp->res);
1453f5ffd806SEd Tanous                     return;
1454f5ffd806SEd Tanous                 }
1455f5ffd806SEd Tanous                 messages::success(asyncResp->res);
1456f5ffd806SEd Tanous                         },
1457f5ffd806SEd Tanous                         "xyz.openbmc_project.User.Manager",
1458f5ffd806SEd Tanous                         "/xyz/openbmc_project/user",
1459f5ffd806SEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
1460f5ffd806SEd Tanous                         "xyz.openbmc_project.User.AccountPolicy",
1461f5ffd806SEd Tanous                         "AccountUnlockTimeout",
1462168e20c1SEd Tanous                         dbus::utility::DbusVariantType(*unlockTimeout));
1463f5ffd806SEd Tanous                 }
1464f5ffd806SEd Tanous                 if (lockoutThreshold)
1465f5ffd806SEd Tanous                 {
1466f5ffd806SEd Tanous                     crow::connections::systemBus->async_method_call(
1467f5ffd806SEd Tanous                         [asyncResp](const boost::system::error_code ec) {
1468f5ffd806SEd Tanous                 if (ec)
1469f5ffd806SEd Tanous                 {
1470f5ffd806SEd Tanous                     messages::internalError(asyncResp->res);
1471f5ffd806SEd Tanous                     return;
1472f5ffd806SEd Tanous                 }
1473f5ffd806SEd Tanous                 messages::success(asyncResp->res);
1474f5ffd806SEd Tanous                         },
1475f5ffd806SEd Tanous                         "xyz.openbmc_project.User.Manager",
1476f5ffd806SEd Tanous                         "/xyz/openbmc_project/user",
1477f5ffd806SEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
1478f5ffd806SEd Tanous                         "xyz.openbmc_project.User.AccountPolicy",
1479f5ffd806SEd Tanous                         "MaxLoginAttemptBeforeLockout",
1480168e20c1SEd Tanous                         dbus::utility::DbusVariantType(*lockoutThreshold));
1481f5ffd806SEd Tanous                 }
1482f5ffd806SEd Tanous             });
1483f5ffd806SEd Tanous 
14846c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
1485ed398213SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
14866c51eab1SEd Tanous         .methods(boost::beast::http::verb::get)(
148745ca1b86SEd Tanous             [&app](
148845ca1b86SEd Tanous                 const crow::Request& req,
14896c51eab1SEd Tanous                 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
149045ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
149145ca1b86SEd Tanous                 {
149245ca1b86SEd Tanous                     return;
149345ca1b86SEd Tanous                 }
14941476687dSEd Tanous 
14951476687dSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
14961476687dSEd Tanous                     "/redfish/v1/AccountService/Accounts";
14971476687dSEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
14981476687dSEd Tanous                     "#ManagerAccountCollection."
14991476687dSEd Tanous                     "ManagerAccountCollection";
15001476687dSEd Tanous                 asyncResp->res.jsonValue["Name"] = "Accounts Collection";
15011476687dSEd Tanous                 asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
15020f74e643SEd Tanous 
15036c51eab1SEd Tanous                 Privileges effectiveUserPrivileges =
15046c51eab1SEd Tanous                     redfish::getUserPrivileges(req.userRole);
15056c51eab1SEd Tanous 
1506f5e29f33SJunLin Chen                 std::string thisUser;
1507f5e29f33SJunLin Chen                 if (req.session)
1508f5e29f33SJunLin Chen                 {
1509f5e29f33SJunLin Chen                     thisUser = req.session->username;
1510f5e29f33SJunLin Chen                 }
1511b9b2e0b2SEd Tanous                 crow::connections::systemBus->async_method_call(
1512cef1ddfbSEd Tanous                     [asyncResp, thisUser, effectiveUserPrivileges](
1513cef1ddfbSEd Tanous                         const boost::system::error_code ec,
1514711ac7a9SEd Tanous                         const dbus::utility::ManagedObjectType& users) {
1515b9b2e0b2SEd Tanous             if (ec)
1516b9b2e0b2SEd Tanous             {
1517f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1518b9b2e0b2SEd Tanous                 return;
1519b9b2e0b2SEd Tanous             }
1520b9b2e0b2SEd Tanous 
1521cef1ddfbSEd Tanous             bool userCanSeeAllAccounts =
1522*002d39b4SEd Tanous                 effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1523cef1ddfbSEd Tanous 
1524cef1ddfbSEd Tanous             bool userCanSeeSelf =
1525*002d39b4SEd Tanous                 effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1526cef1ddfbSEd Tanous 
1527*002d39b4SEd Tanous             nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1528b9b2e0b2SEd Tanous             memberArray = nlohmann::json::array();
1529b9b2e0b2SEd Tanous 
15309eb808c1SEd Tanous             for (const auto& userpath : users)
1531b9b2e0b2SEd Tanous             {
15322dfd18efSEd Tanous                 std::string user = userpath.first.filename();
15332dfd18efSEd Tanous                 if (user.empty())
1534b9b2e0b2SEd Tanous                 {
15352dfd18efSEd Tanous                     messages::internalError(asyncResp->res);
15362dfd18efSEd Tanous                     BMCWEB_LOG_ERROR << "Invalid firmware ID";
15372dfd18efSEd Tanous 
15382dfd18efSEd Tanous                     return;
1539b9b2e0b2SEd Tanous                 }
1540f365910cSGunnar Mills 
1541f365910cSGunnar Mills                 // As clarified by Redfish here:
1542f365910cSGunnar Mills                 // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
15436c51eab1SEd Tanous                 // Users without ConfigureUsers, only see their own
15446c51eab1SEd Tanous                 // account. Users with ConfigureUsers, see all
15456c51eab1SEd Tanous                 // accounts.
1546cef1ddfbSEd Tanous                 if (userCanSeeAllAccounts ||
1547cef1ddfbSEd Tanous                     (thisUser == user && userCanSeeSelf))
1548f365910cSGunnar Mills                 {
15491476687dSEd Tanous                     nlohmann::json::object_t member;
15501476687dSEd Tanous                     member["@odata.id"] =
1551*002d39b4SEd Tanous                         "/redfish/v1/AccountService/Accounts/" + user;
15521476687dSEd Tanous                     memberArray.push_back(std::move(member));
1553b9b2e0b2SEd Tanous                 }
1554f365910cSGunnar Mills             }
1555f365910cSGunnar Mills             asyncResp->res.jsonValue["Members@odata.count"] =
1556f365910cSGunnar Mills                 memberArray.size();
1557b9b2e0b2SEd Tanous                     },
15586c51eab1SEd Tanous                     "xyz.openbmc_project.User.Manager",
15596c51eab1SEd Tanous                     "/xyz/openbmc_project/user",
1560b9b2e0b2SEd Tanous                     "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15616c51eab1SEd Tanous             });
15626c51eab1SEd Tanous 
15636c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
1564ed398213SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
1565*002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
1566*002d39b4SEd Tanous             [&app](
1567*002d39b4SEd Tanous                 const crow::Request& req,
1568*002d39b4SEd Tanous                 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
156945ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
157045ca1b86SEd Tanous                 {
157145ca1b86SEd Tanous                     return;
157245ca1b86SEd Tanous                 }
15739712f8acSEd Tanous                 std::string username;
15749712f8acSEd Tanous                 std::string password;
1575a24526dcSEd Tanous                 std::optional<std::string> roleId("User");
1576a24526dcSEd Tanous                 std::optional<bool> enabled = true;
1577*002d39b4SEd Tanous                 if (!json_util::readJsonPatch(
1578*002d39b4SEd Tanous                         req, asyncResp->res, "UserName", username, "Password",
1579*002d39b4SEd Tanous                         password, "RoleId", roleId, "Enabled", enabled))
158004ae99ecSEd Tanous                 {
158104ae99ecSEd Tanous                     return;
158204ae99ecSEd Tanous                 }
158304ae99ecSEd Tanous 
158454fc587aSNagaraju Goruganti                 std::string priv = getPrivilegeFromRoleId(*roleId);
158584e12cb7SAppaRao Puli                 if (priv.empty())
158604ae99ecSEd Tanous                 {
15876c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, *roleId,
15886c51eab1SEd Tanous                                                      "RoleId");
158904ae99ecSEd Tanous                     return;
159004ae99ecSEd Tanous                 }
159196200606Sjayaprakash Mutyala                 // TODO: Following override will be reverted once support in
15926c51eab1SEd Tanous                 // phosphor-user-manager is added. In order to avoid dependency
15936c51eab1SEd Tanous                 // issues, this is added in bmcweb, which will removed, once
159496200606Sjayaprakash Mutyala                 // phosphor-user-manager supports priv-noaccess.
159596200606Sjayaprakash Mutyala                 if (priv == "priv-noaccess")
159696200606Sjayaprakash Mutyala                 {
159796200606Sjayaprakash Mutyala                     roleId = "";
159896200606Sjayaprakash Mutyala                 }
159996200606Sjayaprakash Mutyala                 else
160096200606Sjayaprakash Mutyala                 {
16019712f8acSEd Tanous                     roleId = priv;
160296200606Sjayaprakash Mutyala                 }
160304ae99ecSEd Tanous 
1604599c71d8SAyushi Smriti                 // Reading AllGroups property
16051e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<std::vector<std::string>>(
16061e1e598dSJonathan Doman                     *crow::connections::systemBus,
1607*002d39b4SEd Tanous                     "xyz.openbmc_project.User.Manager",
1608*002d39b4SEd Tanous                     "/xyz/openbmc_project/user",
16091e1e598dSJonathan Doman                     "xyz.openbmc_project.User.Manager", "AllGroups",
1610599c71d8SAyushi Smriti                     [asyncResp, username, password{std::move(password)}, roleId,
1611168e20c1SEd Tanous                      enabled](const boost::system::error_code ec,
16121e1e598dSJonathan Doman                               const std::vector<std::string>& allGroupsList) {
1613599c71d8SAyushi Smriti             if (ec)
1614599c71d8SAyushi Smriti             {
1615599c71d8SAyushi Smriti                 BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1616599c71d8SAyushi Smriti                 messages::internalError(asyncResp->res);
1617599c71d8SAyushi Smriti                 return;
1618599c71d8SAyushi Smriti             }
1619599c71d8SAyushi Smriti 
16201e1e598dSJonathan Doman             if (allGroupsList.empty())
1621599c71d8SAyushi Smriti             {
1622599c71d8SAyushi Smriti                 messages::internalError(asyncResp->res);
1623599c71d8SAyushi Smriti                 return;
1624599c71d8SAyushi Smriti             }
1625599c71d8SAyushi Smriti 
162604ae99ecSEd Tanous             crow::connections::systemBus->async_method_call(
1627f23b7296SEd Tanous                 [asyncResp, username,
1628f23b7296SEd Tanous                  password](const boost::system::error_code ec2,
16290d4197e0Sanil kumar appana                            sdbusplus::message::message& m) {
163023a21a1cSEd Tanous                 if (ec2)
163104ae99ecSEd Tanous                 {
1632*002d39b4SEd Tanous                     userErrorMessageHandler(m.get_error(), asyncResp, username,
1633*002d39b4SEd Tanous                                             "");
163404ae99ecSEd Tanous                     return;
163504ae99ecSEd Tanous                 }
163604ae99ecSEd Tanous 
1637*002d39b4SEd Tanous                 if (pamUpdatePassword(username, password) != PAM_SUCCESS)
163804ae99ecSEd Tanous                 {
16396c51eab1SEd Tanous                     // At this point we have a user that's been
16406c51eab1SEd Tanous                     // created, but the password set
16416c51eab1SEd Tanous                     // failed.Something is wrong, so delete the user
16426c51eab1SEd Tanous                     // that we've already created
1643b477fd44SP Dheeraj Srujan Kumar                     sdbusplus::message::object_path tempObjPath(
1644b477fd44SP Dheeraj Srujan Kumar                         rootUserDbusPath);
1645b477fd44SP Dheeraj Srujan Kumar                     tempObjPath /= username;
1646b477fd44SP Dheeraj Srujan Kumar                     const std::string userPath(tempObjPath);
1647b477fd44SP Dheeraj Srujan Kumar 
164804ae99ecSEd Tanous                     crow::connections::systemBus->async_method_call(
1649*002d39b4SEd Tanous                         [asyncResp,
1650*002d39b4SEd Tanous                          password](const boost::system::error_code ec3) {
165123a21a1cSEd Tanous                         if (ec3)
165204ae99ecSEd Tanous                         {
1653*002d39b4SEd Tanous                             messages::internalError(asyncResp->res);
165404ae99ecSEd Tanous                             return;
165504ae99ecSEd Tanous                         }
165604ae99ecSEd Tanous 
16570d4197e0Sanil kumar appana                         // If password is invalid
16580d4197e0Sanil kumar appana                         messages::propertyValueFormatError(
1659*002d39b4SEd Tanous                             asyncResp->res, password, "Password");
166004ae99ecSEd Tanous                         },
1661*002d39b4SEd Tanous                         "xyz.openbmc_project.User.Manager", userPath,
1662*002d39b4SEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
166304ae99ecSEd Tanous 
166404ae99ecSEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
166504ae99ecSEd Tanous                     return;
166604ae99ecSEd Tanous                 }
166704ae99ecSEd Tanous 
1668f12894f8SJason M. Bills                 messages::created(asyncResp->res);
166904ae99ecSEd Tanous                 asyncResp->res.addHeader(
167004ae99ecSEd Tanous                     "Location",
1671*002d39b4SEd Tanous                     "/redfish/v1/AccountService/Accounts/" + username);
167204ae99ecSEd Tanous                 },
1673*002d39b4SEd Tanous                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1674*002d39b4SEd Tanous                 "xyz.openbmc_project.User.Manager", "CreateUser", username,
1675*002d39b4SEd Tanous                 allGroupsList, *roleId, *enabled);
16761e1e598dSJonathan Doman                     });
16776c51eab1SEd Tanous             });
1678b9b2e0b2SEd Tanous 
16796c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
1680ed398213SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
1681*002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1682*002d39b4SEd Tanous             [&app]([[maybe_unused]] const crow::Request& req,
16836c51eab1SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16846c51eab1SEd Tanous                    const std::string& accountName) -> void {
168545ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
168645ca1b86SEd Tanous                 {
168745ca1b86SEd Tanous                     return;
168845ca1b86SEd Tanous                 }
1689a43ea82fSNan Zhou #ifdef BMCWEB_INSECURE_DISABLE_AUTHX
1690031514fbSJunLin Chen                 // If authentication is disabled, there are no user accounts
1691*002d39b4SEd Tanous                 messages::resourceNotFound(
1692*002d39b4SEd Tanous                     asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
1693031514fbSJunLin Chen                     accountName);
1694031514fbSJunLin Chen                 return;
1695031514fbSJunLin Chen 
1696a43ea82fSNan Zhou #endif // BMCWEB_INSECURE_DISABLE_AUTHX
1697031514fbSJunLin Chen                 if (req.session == nullptr)
1698031514fbSJunLin Chen                 {
1699031514fbSJunLin Chen                     messages::internalError(asyncResp->res);
1700031514fbSJunLin Chen                     return;
1701031514fbSJunLin Chen                 }
17026c51eab1SEd Tanous                 if (req.session->username != accountName)
1703b9b2e0b2SEd Tanous                 {
17046c51eab1SEd Tanous                     // At this point we've determined that the user is trying to
1705*002d39b4SEd Tanous                     // modify a user that isn't them.  We need to verify that
1706*002d39b4SEd Tanous                     // they have permissions to modify other users, so re-run
1707*002d39b4SEd Tanous                     // the auth check with the same permissions, minus
1708*002d39b4SEd Tanous                     // ConfigureSelf.
17096c51eab1SEd Tanous                     Privileges effectiveUserPrivileges =
17106c51eab1SEd Tanous                         redfish::getUserPrivileges(req.userRole);
17116c51eab1SEd Tanous                     Privileges requiredPermissionsToChangeNonSelf = {
17124f48d5f6SEd Tanous                         "ConfigureUsers", "ConfigureManager"};
17136c51eab1SEd Tanous                     if (!effectiveUserPrivileges.isSupersetOf(
17146c51eab1SEd Tanous                             requiredPermissionsToChangeNonSelf))
1715900f9497SJoseph Reynolds                     {
1716900f9497SJoseph Reynolds                         BMCWEB_LOG_DEBUG << "GET Account denied access";
1717900f9497SJoseph Reynolds                         messages::insufficientPrivilege(asyncResp->res);
1718900f9497SJoseph Reynolds                         return;
1719900f9497SJoseph Reynolds                     }
1720900f9497SJoseph Reynolds                 }
1721900f9497SJoseph Reynolds 
1722b9b2e0b2SEd Tanous                 crow::connections::systemBus->async_method_call(
1723*002d39b4SEd Tanous                     [asyncResp, accountName](
1724*002d39b4SEd Tanous                         const boost::system::error_code ec,
1725711ac7a9SEd Tanous                         const dbus::utility::ManagedObjectType& users) {
1726b9b2e0b2SEd Tanous             if (ec)
1727b9b2e0b2SEd Tanous             {
1728f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1729b9b2e0b2SEd Tanous                 return;
1730b9b2e0b2SEd Tanous             }
1731711ac7a9SEd Tanous             const auto userIt = std::find_if(
1732b477fd44SP Dheeraj Srujan Kumar                 users.begin(), users.end(),
1733b477fd44SP Dheeraj Srujan Kumar                 [accountName](
1734b477fd44SP Dheeraj Srujan Kumar                     const std::pair<sdbusplus::message::object_path,
1735*002d39b4SEd Tanous                                     dbus::utility::DBusInteracesMap>& user) {
173655f79e6fSEd Tanous                 return accountName == user.first.filename();
1737b477fd44SP Dheeraj Srujan Kumar                 });
1738b9b2e0b2SEd Tanous 
173984e12cb7SAppaRao Puli             if (userIt == users.end())
1740b9b2e0b2SEd Tanous             {
1741*002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1742*002d39b4SEd Tanous                                            accountName);
174384e12cb7SAppaRao Puli                 return;
174484e12cb7SAppaRao Puli             }
17454e68c45bSAyushi Smriti 
17461476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
17471476687dSEd Tanous                 "#ManagerAccount.v1_4_0.ManagerAccount";
17481476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = "User Account";
17491476687dSEd Tanous             asyncResp->res.jsonValue["Description"] = "User Account";
17501476687dSEd Tanous             asyncResp->res.jsonValue["Password"] = nullptr;
17511476687dSEd Tanous             asyncResp->res.jsonValue["AccountTypes"] = {"Redfish"};
17524e68c45bSAyushi Smriti 
175384e12cb7SAppaRao Puli             for (const auto& interface : userIt->second)
175465b0dc32SEd Tanous             {
1755*002d39b4SEd Tanous                 if (interface.first == "xyz.openbmc_project.User.Attributes")
175665b0dc32SEd Tanous                 {
175765b0dc32SEd Tanous                     for (const auto& property : interface.second)
175865b0dc32SEd Tanous                     {
175965b0dc32SEd Tanous                         if (property.first == "UserEnabled")
176065b0dc32SEd Tanous                         {
176165b0dc32SEd Tanous                             const bool* userEnabled =
1762abf2add6SEd Tanous                                 std::get_if<bool>(&property.second);
176365b0dc32SEd Tanous                             if (userEnabled == nullptr)
176465b0dc32SEd Tanous                             {
1765*002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
176684e12cb7SAppaRao Puli                                 messages::internalError(asyncResp->res);
176784e12cb7SAppaRao Puli                                 return;
176865b0dc32SEd Tanous                             }
1769*002d39b4SEd Tanous                             asyncResp->res.jsonValue["Enabled"] = *userEnabled;
177065b0dc32SEd Tanous                         }
1771*002d39b4SEd Tanous                         else if (property.first == "UserLockedForFailedAttempt")
177265b0dc32SEd Tanous                         {
177365b0dc32SEd Tanous                             const bool* userLocked =
1774abf2add6SEd Tanous                                 std::get_if<bool>(&property.second);
177565b0dc32SEd Tanous                             if (userLocked == nullptr)
177665b0dc32SEd Tanous                             {
177784e12cb7SAppaRao Puli                                 BMCWEB_LOG_ERROR << "UserLockedForF"
177884e12cb7SAppaRao Puli                                                     "ailedAttempt "
177984e12cb7SAppaRao Puli                                                     "wasn't a bool";
178084e12cb7SAppaRao Puli                                 messages::internalError(asyncResp->res);
178184e12cb7SAppaRao Puli                                 return;
178265b0dc32SEd Tanous                             }
1783*002d39b4SEd Tanous                             asyncResp->res.jsonValue["Locked"] = *userLocked;
1784*002d39b4SEd Tanous                             asyncResp->res
1785*002d39b4SEd Tanous                                 .jsonValue["Locked@Redfish.AllowableValues"] = {
17863bf4e632SJoseph Reynolds                                 "false"}; // can only unlock accounts
178765b0dc32SEd Tanous                         }
178884e12cb7SAppaRao Puli                         else if (property.first == "UserPrivilege")
178984e12cb7SAppaRao Puli                         {
179054fc587aSNagaraju Goruganti                             const std::string* userPrivPtr =
1791*002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
179254fc587aSNagaraju Goruganti                             if (userPrivPtr == nullptr)
179384e12cb7SAppaRao Puli                             {
1794*002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
179584e12cb7SAppaRao Puli                                                     "string";
179684e12cb7SAppaRao Puli                                 messages::internalError(asyncResp->res);
179784e12cb7SAppaRao Puli                                 return;
179884e12cb7SAppaRao Puli                             }
179954fc587aSNagaraju Goruganti                             std::string role =
180054fc587aSNagaraju Goruganti                                 getRoleIdFromPrivilege(*userPrivPtr);
180154fc587aSNagaraju Goruganti                             if (role.empty())
180284e12cb7SAppaRao Puli                             {
180384e12cb7SAppaRao Puli                                 BMCWEB_LOG_ERROR << "Invalid user role";
180484e12cb7SAppaRao Puli                                 messages::internalError(asyncResp->res);
180584e12cb7SAppaRao Puli                                 return;
180684e12cb7SAppaRao Puli                             }
180754fc587aSNagaraju Goruganti                             asyncResp->res.jsonValue["RoleId"] = role;
180884e12cb7SAppaRao Puli 
18091476687dSEd Tanous                             nlohmann::json& roleEntry =
1810*002d39b4SEd Tanous                                 asyncResp->res.jsonValue["Links"]["Role"];
18111476687dSEd Tanous                             roleEntry["@odata.id"] =
1812*002d39b4SEd Tanous                                 "/redfish/v1/AccountService/Roles/" + role;
181384e12cb7SAppaRao Puli                         }
1814*002d39b4SEd Tanous                         else if (property.first == "UserPasswordExpired")
18153bf4e632SJoseph Reynolds                         {
18163bf4e632SJoseph Reynolds                             const bool* userPasswordExpired =
18173bf4e632SJoseph Reynolds                                 std::get_if<bool>(&property.second);
18183bf4e632SJoseph Reynolds                             if (userPasswordExpired == nullptr)
18193bf4e632SJoseph Reynolds                             {
18200fda0f12SGeorge Liu                                 BMCWEB_LOG_ERROR
18210fda0f12SGeorge Liu                                     << "UserPasswordExpired wasn't a bool";
18223bf4e632SJoseph Reynolds                                 messages::internalError(asyncResp->res);
18233bf4e632SJoseph Reynolds                                 return;
18243bf4e632SJoseph Reynolds                             }
1825*002d39b4SEd Tanous                             asyncResp->res.jsonValue["PasswordChangeRequired"] =
18263bf4e632SJoseph Reynolds                                 *userPasswordExpired;
18273bf4e632SJoseph Reynolds                         }
182865b0dc32SEd Tanous                     }
182965b0dc32SEd Tanous                 }
183065b0dc32SEd Tanous             }
183165b0dc32SEd Tanous 
1832b9b2e0b2SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
183384e12cb7SAppaRao Puli                 "/redfish/v1/AccountService/Accounts/" + accountName;
1834b9b2e0b2SEd Tanous             asyncResp->res.jsonValue["Id"] = accountName;
1835b9b2e0b2SEd Tanous             asyncResp->res.jsonValue["UserName"] = accountName;
1836b9b2e0b2SEd Tanous                     },
1837*002d39b4SEd Tanous                     "xyz.openbmc_project.User.Manager",
1838*002d39b4SEd Tanous                     "/xyz/openbmc_project/user",
1839b9b2e0b2SEd Tanous                     "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18406c51eab1SEd Tanous             });
1841a840879dSEd Tanous 
18426c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
1843ed398213SEd Tanous         // TODO this privilege should be using the generated endpoints, but
1844ed398213SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
1845ed398213SEd Tanous         // yet
18466c51eab1SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
18476c51eab1SEd Tanous         .methods(boost::beast::http::verb::patch)(
184845ca1b86SEd Tanous             [&app](const crow::Request& req,
18496c51eab1SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18506c51eab1SEd Tanous                    const std::string& username) -> void {
185145ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
185245ca1b86SEd Tanous                 {
185345ca1b86SEd Tanous                     return;
185445ca1b86SEd Tanous                 }
1855a43ea82fSNan Zhou #ifdef BMCWEB_INSECURE_DISABLE_AUTHX
1856031514fbSJunLin Chen                 // If authentication is disabled, there are no user accounts
1857031514fbSJunLin Chen                 messages::resourceNotFound(
1858031514fbSJunLin Chen                     asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
1859031514fbSJunLin Chen                     username);
1860031514fbSJunLin Chen                 return;
1861031514fbSJunLin Chen 
1862a43ea82fSNan Zhou #endif // BMCWEB_INSECURE_DISABLE_AUTHX
1863a24526dcSEd Tanous                 std::optional<std::string> newUserName;
1864a24526dcSEd Tanous                 std::optional<std::string> password;
1865a24526dcSEd Tanous                 std::optional<bool> enabled;
1866a24526dcSEd Tanous                 std::optional<std::string> roleId;
186724c8542dSRatan Gupta                 std::optional<bool> locked;
1868e9cc5172SEd Tanous 
1869031514fbSJunLin Chen                 if (req.session == nullptr)
1870031514fbSJunLin Chen                 {
1871031514fbSJunLin Chen                     messages::internalError(asyncResp->res);
1872031514fbSJunLin Chen                     return;
1873031514fbSJunLin Chen                 }
1874031514fbSJunLin Chen 
1875e9cc5172SEd Tanous                 Privileges effectiveUserPrivileges =
1876e9cc5172SEd Tanous                     redfish::getUserPrivileges(req.userRole);
1877e9cc5172SEd Tanous                 Privileges configureUsers = {"ConfigureUsers"};
1878e9cc5172SEd Tanous                 bool userHasConfigureUsers =
1879e9cc5172SEd Tanous                     effectiveUserPrivileges.isSupersetOf(configureUsers);
1880e9cc5172SEd Tanous                 if (userHasConfigureUsers)
1881e9cc5172SEd Tanous                 {
1882e9cc5172SEd Tanous                     // Users with ConfigureUsers can modify for all users
188315ed6780SWilly Tu                     if (!json_util::readJsonPatch(
188415ed6780SWilly Tu                             req, asyncResp->res, "UserName", newUserName,
188515ed6780SWilly Tu                             "Password", password, "RoleId", roleId, "Enabled",
1886e9cc5172SEd Tanous                             enabled, "Locked", locked))
1887a840879dSEd Tanous                     {
1888a840879dSEd Tanous                         return;
1889a840879dSEd Tanous                     }
1890e9cc5172SEd Tanous                 }
1891e9cc5172SEd Tanous                 else
1892900f9497SJoseph Reynolds                 {
1893e9cc5172SEd Tanous                     // ConfigureSelf accounts can only modify their own account
1894e9cc5172SEd Tanous                     if (username != req.session->username)
1895900f9497SJoseph Reynolds                     {
1896900f9497SJoseph Reynolds                         messages::insufficientPrivilege(asyncResp->res);
1897900f9497SJoseph Reynolds                         return;
1898900f9497SJoseph Reynolds                     }
1899031514fbSJunLin Chen 
1900e9cc5172SEd Tanous                     // ConfigureSelf accounts can only modify their password
190115ed6780SWilly Tu                     if (!json_util::readJsonPatch(req, asyncResp->res,
190215ed6780SWilly Tu                                                   "Password", password))
1903e9cc5172SEd Tanous                     {
1904e9cc5172SEd Tanous                         return;
1905e9cc5172SEd Tanous                     }
1906900f9497SJoseph Reynolds                 }
1907900f9497SJoseph Reynolds 
190866b5ca76Sjayaprakash Mutyala                 // if user name is not provided in the patch method or if it
19096c51eab1SEd Tanous                 // matches the user name in the URI, then we are treating it as
19106c51eab1SEd Tanous                 // updating user properties other then username. If username
19116c51eab1SEd Tanous                 // provided doesn't match the URI, then we are treating this as
19126c51eab1SEd Tanous                 // user rename request.
191366b5ca76Sjayaprakash Mutyala                 if (!newUserName || (newUserName.value() == username))
1914a840879dSEd Tanous                 {
19156c51eab1SEd Tanous                     updateUserProperties(asyncResp, username, password, enabled,
19166c51eab1SEd Tanous                                          roleId, locked);
191784e12cb7SAppaRao Puli                     return;
191884e12cb7SAppaRao Puli                 }
191984e12cb7SAppaRao Puli                 crow::connections::systemBus->async_method_call(
19206c51eab1SEd Tanous                     [asyncResp, username, password(std::move(password)),
1921f23b7296SEd Tanous                      roleId(std::move(roleId)), enabled,
192266b5ca76Sjayaprakash Mutyala                      newUser{std::string(*newUserName)},
1923f23b7296SEd Tanous                      locked](const boost::system::error_code ec,
192466b5ca76Sjayaprakash Mutyala                              sdbusplus::message::message& m) {
192584e12cb7SAppaRao Puli             if (ec)
192684e12cb7SAppaRao Puli             {
1927*002d39b4SEd Tanous                 userErrorMessageHandler(m.get_error(), asyncResp, newUser,
1928*002d39b4SEd Tanous                                         username);
1929a840879dSEd Tanous                 return;
1930a840879dSEd Tanous             }
1931a840879dSEd Tanous 
1932*002d39b4SEd Tanous             updateUserProperties(asyncResp, newUser, password, enabled, roleId,
1933*002d39b4SEd Tanous                                  locked);
193484e12cb7SAppaRao Puli                     },
19356c51eab1SEd Tanous                     "xyz.openbmc_project.User.Manager",
19366c51eab1SEd Tanous                     "/xyz/openbmc_project/user",
193784e12cb7SAppaRao Puli                     "xyz.openbmc_project.User.Manager", "RenameUser", username,
193884e12cb7SAppaRao Puli                     *newUserName);
19396c51eab1SEd Tanous             });
194084e12cb7SAppaRao Puli 
19416c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
1942ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
19436c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
194445ca1b86SEd Tanous             [&app](const crow::Request& req,
19456c51eab1SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19466c51eab1SEd Tanous                    const std::string& username) -> void {
194745ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
194845ca1b86SEd Tanous                 {
194945ca1b86SEd Tanous                     return;
195045ca1b86SEd Tanous                 }
1951031514fbSJunLin Chen 
1952a43ea82fSNan Zhou #ifdef BMCWEB_INSECURE_DISABLE_AUTHX
1953031514fbSJunLin Chen                 // If authentication is disabled, there are no user accounts
1954031514fbSJunLin Chen                 messages::resourceNotFound(
1955031514fbSJunLin Chen                     asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
1956031514fbSJunLin Chen                     username);
1957031514fbSJunLin Chen                 return;
1958031514fbSJunLin Chen 
1959a43ea82fSNan Zhou #endif // BMCWEB_INSECURE_DISABLE_AUTHX
1960b477fd44SP Dheeraj Srujan Kumar                 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1961b477fd44SP Dheeraj Srujan Kumar                 tempObjPath /= username;
1962b477fd44SP Dheeraj Srujan Kumar                 const std::string userPath(tempObjPath);
196324c8542dSRatan Gupta 
19646c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
19656c51eab1SEd Tanous                     [asyncResp, username](const boost::system::error_code ec) {
19666c51eab1SEd Tanous             if (ec)
196766b5ca76Sjayaprakash Mutyala             {
196866b5ca76Sjayaprakash Mutyala                 messages::resourceNotFound(
1969*002d39b4SEd Tanous                     asyncResp->res, "#ManagerAccount.v1_4_0.ManagerAccount",
1970f12894f8SJason M. Bills                     username);
197106e086d9SEd Tanous                 return;
197206e086d9SEd Tanous             }
197306e086d9SEd Tanous 
1974f12894f8SJason M. Bills             messages::accountRemoved(asyncResp->res);
197506e086d9SEd Tanous                     },
197606e086d9SEd Tanous                     "xyz.openbmc_project.User.Manager", userPath,
197706e086d9SEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
19786c51eab1SEd Tanous             });
197906e086d9SEd Tanous }
198088d16c9aSLewanczyk, Dawid 
198188d16c9aSLewanczyk, Dawid } // namespace redfish
1982