xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision d8a5d5d8e07f6dec97ed0cbc84052656165f6172)
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     {
126*d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
12766b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
12866b5ca76Sjayaprakash Mutyala     }
12966b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
13066b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
13166b5ca76Sjayaprakash Mutyala     {
132*d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
13366b5ca76Sjayaprakash Mutyala     }
134d4d25793SEd Tanous     else if ((strcmp(errorMessage,
135d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
136d4d25793SEd Tanous               0) ||
1370fda0f12SGeorge Liu              (strcmp(
1380fda0f12SGeorge Liu                   errorMessage,
1390fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
1400fda0f12SGeorge Liu               0))
14166b5ca76Sjayaprakash Mutyala     {
14266b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
14366b5ca76Sjayaprakash Mutyala     }
14466b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
14566b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
14666b5ca76Sjayaprakash Mutyala     {
14766b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
14866b5ca76Sjayaprakash Mutyala     }
14966b5ca76Sjayaprakash Mutyala     else
15066b5ca76Sjayaprakash Mutyala     {
15166b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
15266b5ca76Sjayaprakash Mutyala     }
15366b5ca76Sjayaprakash Mutyala }
15466b5ca76Sjayaprakash Mutyala 
15581ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
156ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
157ab828d7cSRatan Gupta                                 const std::string& ldapType)
1586973a582SRatan Gupta {
159ab828d7cSRatan Gupta     std::string service =
160ab828d7cSRatan Gupta         (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService";
16154fc587aSNagaraju Goruganti 
1621476687dSEd Tanous     nlohmann::json& ldap = jsonResponse[ldapType];
16354fc587aSNagaraju Goruganti 
1641476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
1651476687dSEd Tanous     ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri});
1661476687dSEd Tanous     ldap["Authentication"]["AuthenticationType"] = "UsernameAndPassword";
1671476687dSEd Tanous     ldap["Authentication"]["Username"] = confData.bindDN;
1681476687dSEd Tanous     ldap["Authentication"]["Password"] = nullptr;
1691476687dSEd Tanous 
1701476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] =
1711476687dSEd Tanous         nlohmann::json::array({confData.baseDN});
1721476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] =
1731476687dSEd Tanous         confData.userNameAttribute;
1741476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] =
1751476687dSEd Tanous         confData.groupAttribute;
1761476687dSEd Tanous 
1771476687dSEd Tanous     nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"];
17854fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
1799eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
18054fc587aSNagaraju Goruganti     {
18154fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
18254fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
18354fc587aSNagaraju Goruganti         roleMapArray.push_back(
18454fc587aSNagaraju Goruganti             {nlohmann::json::array({"RemoteGroup", obj.second.groupName}),
18554fc587aSNagaraju Goruganti              nlohmann::json::array(
18654fc587aSNagaraju Goruganti                  {"LocalRole", getRoleIdFromPrivilege(obj.second.privilege)})});
18754fc587aSNagaraju Goruganti     }
1886973a582SRatan Gupta }
1896973a582SRatan Gupta 
1906973a582SRatan Gupta /**
19106785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
19206785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
19306785244SRatan Gupta  *
19406785244SRatan Gupta  */
19523a21a1cSEd Tanous inline void handleRoleMapPatch(
1968d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19706785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
198f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
19906785244SRatan Gupta {
20006785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
20106785244SRatan Gupta     {
202f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
20306785244SRatan Gupta 
20406785244SRatan Gupta         if (thisJson.is_null())
20506785244SRatan Gupta         {
20606785244SRatan Gupta             // delete the existing object
20706785244SRatan Gupta             if (index < roleMapObjData.size())
20806785244SRatan Gupta             {
20906785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
21006785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
21106785244SRatan Gupta                      index](const boost::system::error_code ec) {
21206785244SRatan Gupta                     if (ec)
21306785244SRatan Gupta                     {
21406785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
21506785244SRatan Gupta                         messages::internalError(asyncResp->res);
21606785244SRatan Gupta                         return;
21706785244SRatan Gupta                     }
21806785244SRatan Gupta                     asyncResp->res
21906785244SRatan Gupta                         .jsonValue[serverType]["RemoteRoleMapping"][index] =
22006785244SRatan Gupta                         nullptr;
22106785244SRatan Gupta                     },
22206785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
22306785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
22406785244SRatan Gupta             }
22506785244SRatan Gupta             else
22606785244SRatan Gupta             {
22706785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
22806785244SRatan Gupta                 messages::propertyValueTypeError(
22971f52d96SEd Tanous                     asyncResp->res,
23071f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
23171f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
23206785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
23306785244SRatan Gupta                 return;
23406785244SRatan Gupta             }
23506785244SRatan Gupta         }
23606785244SRatan Gupta         else if (thisJson.empty())
23706785244SRatan Gupta         {
23806785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
23906785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
24006785244SRatan Gupta         }
24106785244SRatan Gupta         else
24206785244SRatan Gupta         {
24306785244SRatan Gupta             // update/create the object
24406785244SRatan Gupta             std::optional<std::string> remoteGroup;
24506785244SRatan Gupta             std::optional<std::string> localRole;
24606785244SRatan Gupta 
247f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
248f23b7296SEd Tanous             // readJson is structured
249f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
250f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
251f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
252f23b7296SEd Tanous                                      localRole))
25306785244SRatan Gupta             {
25406785244SRatan Gupta                 continue;
25506785244SRatan Gupta             }
25606785244SRatan Gupta 
25706785244SRatan Gupta             // Update existing RoleMapping Object
25806785244SRatan Gupta             if (index < roleMapObjData.size())
25906785244SRatan Gupta             {
26006785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
26106785244SRatan Gupta                 // If "RemoteGroup" info is provided
26206785244SRatan Gupta                 if (remoteGroup)
26306785244SRatan Gupta                 {
26406785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
26506785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
26606785244SRatan Gupta                          remoteGroup](const boost::system::error_code ec) {
26706785244SRatan Gupta                         if (ec)
26806785244SRatan Gupta                         {
269002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
27006785244SRatan Gupta                             messages::internalError(asyncResp->res);
27106785244SRatan Gupta                             return;
27206785244SRatan Gupta                         }
27306785244SRatan Gupta                         asyncResp->res
274002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
275002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
27606785244SRatan Gupta                         },
27706785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
27806785244SRatan Gupta                         propertyInterface, "Set",
27906785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
28006785244SRatan Gupta                         "GroupName",
281168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
282168e20c1SEd Tanous                             std::move(*remoteGroup)));
28306785244SRatan Gupta                 }
28406785244SRatan Gupta 
28506785244SRatan Gupta                 // If "LocalRole" info is provided
28606785244SRatan Gupta                 if (localRole)
28706785244SRatan Gupta                 {
28806785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
28906785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
29006785244SRatan Gupta                          localRole](const boost::system::error_code ec) {
29106785244SRatan Gupta                         if (ec)
29206785244SRatan Gupta                         {
293002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
29406785244SRatan Gupta                             messages::internalError(asyncResp->res);
29506785244SRatan Gupta                             return;
29606785244SRatan Gupta                         }
29706785244SRatan Gupta                         asyncResp->res
298002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
299002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
30006785244SRatan Gupta                         },
30106785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
30206785244SRatan Gupta                         propertyInterface, "Set",
30306785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
30406785244SRatan Gupta                         "Privilege",
305168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
30606785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
30706785244SRatan Gupta                 }
30806785244SRatan Gupta             }
30906785244SRatan Gupta             // Create a new RoleMapping Object.
31006785244SRatan Gupta             else
31106785244SRatan Gupta             {
31206785244SRatan Gupta                 BMCWEB_LOG_DEBUG
31306785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
31406785244SRatan Gupta                 std::string pathString =
31506785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index);
31606785244SRatan Gupta 
31706785244SRatan Gupta                 if (!localRole)
31806785244SRatan Gupta                 {
31906785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
32006785244SRatan Gupta                                               pathString + "/LocalRole");
32106785244SRatan Gupta                     continue;
32206785244SRatan Gupta                 }
32306785244SRatan Gupta                 if (!remoteGroup)
32406785244SRatan Gupta                 {
32506785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
32606785244SRatan Gupta                                               pathString + "/RemoteGroup");
32706785244SRatan Gupta                     continue;
32806785244SRatan Gupta                 }
32906785244SRatan Gupta 
33006785244SRatan Gupta                 std::string dbusObjectPath;
33106785244SRatan Gupta                 if (serverType == "ActiveDirectory")
33206785244SRatan Gupta                 {
3332c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
33406785244SRatan Gupta                 }
33506785244SRatan Gupta                 else if (serverType == "LDAP")
33606785244SRatan Gupta                 {
33723a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
33806785244SRatan Gupta                 }
33906785244SRatan Gupta 
34006785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
34106785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
34206785244SRatan Gupta 
34306785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
344271584abSEd Tanous                     [asyncResp, serverType, localRole,
34506785244SRatan Gupta                      remoteGroup](const boost::system::error_code ec) {
34606785244SRatan Gupta                     if (ec)
34706785244SRatan Gupta                     {
34806785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
34906785244SRatan Gupta                         messages::internalError(asyncResp->res);
35006785244SRatan Gupta                         return;
35106785244SRatan Gupta                     }
35206785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
35306785244SRatan Gupta                         asyncResp->res
35406785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
3551476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
3561476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
3571476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
3581476687dSEd Tanous                     remoteRoleJson.push_back(std::move(roleMapEntry));
35906785244SRatan Gupta                     },
36006785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
3613174e4dfSEd Tanous                     "Create", *remoteGroup,
36206785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
36306785244SRatan Gupta             }
36406785244SRatan Gupta         }
36506785244SRatan Gupta     }
36606785244SRatan Gupta }
36706785244SRatan Gupta 
36806785244SRatan Gupta /**
3696973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
3706973a582SRatan Gupta  * into JSON
3716973a582SRatan Gupta  */
3726973a582SRatan Gupta template <typename CallbackFunc>
3736973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
3746973a582SRatan Gupta                               CallbackFunc&& callback)
3756973a582SRatan Gupta {
37654fc587aSNagaraju Goruganti 
37754fc587aSNagaraju Goruganti     const std::array<const char*, 2> interfaces = {ldapEnableInterface,
37854fc587aSNagaraju Goruganti                                                    ldapConfigInterface};
37954fc587aSNagaraju Goruganti 
38054fc587aSNagaraju Goruganti     crow::connections::systemBus->async_method_call(
38154fc587aSNagaraju Goruganti         [callback, ldapType](const boost::system::error_code ec,
382b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
38354fc587aSNagaraju Goruganti         if (ec || resp.empty())
38454fc587aSNagaraju Goruganti         {
3850fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
386002d39b4SEd Tanous                 << "DBUS response error during getting of service name: " << ec;
38723a21a1cSEd Tanous             LDAPConfigData empty{};
38823a21a1cSEd Tanous             callback(false, empty, ldapType);
38954fc587aSNagaraju Goruganti             return;
39054fc587aSNagaraju Goruganti         }
39154fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
39254fc587aSNagaraju Goruganti         crow::connections::systemBus->async_method_call(
393002d39b4SEd Tanous             [callback,
394002d39b4SEd Tanous              ldapType](const boost::system::error_code errorCode,
395711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
3966973a582SRatan Gupta             LDAPConfigData confData{};
39781ce609eSEd Tanous             if (errorCode)
3986973a582SRatan Gupta             {
399ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
400002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode;
4016973a582SRatan Gupta                 return;
4026973a582SRatan Gupta             }
403ab828d7cSRatan Gupta 
404ab828d7cSRatan Gupta             std::string ldapDbusType;
40554fc587aSNagaraju Goruganti             std::string searchString;
40654fc587aSNagaraju Goruganti 
407ab828d7cSRatan Gupta             if (ldapType == "LDAP")
408ab828d7cSRatan Gupta             {
4090fda0f12SGeorge Liu                 ldapDbusType =
4100fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
41154fc587aSNagaraju Goruganti                 searchString = "openldap";
412ab828d7cSRatan Gupta             }
413ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
414ab828d7cSRatan Gupta             {
41554fc587aSNagaraju Goruganti                 ldapDbusType =
4160fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
41754fc587aSNagaraju Goruganti                 searchString = "active_directory";
418ab828d7cSRatan Gupta             }
419ab828d7cSRatan Gupta             else
420ab828d7cSRatan Gupta             {
421002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type="
422ab828d7cSRatan Gupta                                  << ldapType;
423ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
424ab828d7cSRatan Gupta                 return;
425ab828d7cSRatan Gupta             }
426ab828d7cSRatan Gupta 
427ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
428ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
429ab828d7cSRatan Gupta 
4306973a582SRatan Gupta             for (const auto& object : ldapObjects)
4316973a582SRatan Gupta             {
43254fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
43354fc587aSNagaraju Goruganti                 // given type
434002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
4356973a582SRatan Gupta                 {
436ab828d7cSRatan Gupta                     continue;
437ab828d7cSRatan Gupta                 }
438ab828d7cSRatan Gupta 
4396973a582SRatan Gupta                 for (const auto& interface : object.second)
4406973a582SRatan Gupta                 {
4416973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
4426973a582SRatan Gupta                     {
4436973a582SRatan Gupta                         // rest of the properties are string.
4446973a582SRatan Gupta                         for (const auto& property : interface.second)
4456973a582SRatan Gupta                         {
4466973a582SRatan Gupta                             if (property.first == "Enabled")
4476973a582SRatan Gupta                             {
4486973a582SRatan Gupta                                 const bool* value =
4496973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
4506973a582SRatan Gupta                                 if (value == nullptr)
4516973a582SRatan Gupta                                 {
4526973a582SRatan Gupta                                     continue;
4536973a582SRatan Gupta                                 }
4546973a582SRatan Gupta                                 confData.serviceEnabled = *value;
4556973a582SRatan Gupta                                 break;
4566973a582SRatan Gupta                             }
4576973a582SRatan Gupta                         }
4586973a582SRatan Gupta                     }
4596973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
4606973a582SRatan Gupta                     {
4616973a582SRatan Gupta 
4626973a582SRatan Gupta                         for (const auto& property : interface.second)
4636973a582SRatan Gupta                         {
464271584abSEd Tanous                             const std::string* strValue =
465002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
466271584abSEd Tanous                             if (strValue == nullptr)
4676973a582SRatan Gupta                             {
4686973a582SRatan Gupta                                 continue;
4696973a582SRatan Gupta                             }
4706973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
4716973a582SRatan Gupta                             {
472271584abSEd Tanous                                 confData.uri = *strValue;
4736973a582SRatan Gupta                             }
4746973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
4756973a582SRatan Gupta                             {
476271584abSEd Tanous                                 confData.bindDN = *strValue;
4776973a582SRatan Gupta                             }
4786973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
4796973a582SRatan Gupta                             {
480271584abSEd Tanous                                 confData.baseDN = *strValue;
4816973a582SRatan Gupta                             }
482002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
4836973a582SRatan Gupta                             {
484271584abSEd Tanous                                 confData.searchScope = *strValue;
4856973a582SRatan Gupta                             }
486002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
4876973a582SRatan Gupta                             {
488271584abSEd Tanous                                 confData.groupAttribute = *strValue;
4896973a582SRatan Gupta                             }
490002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
4916973a582SRatan Gupta                             {
492271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
4936973a582SRatan Gupta                             }
49454fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
495ab828d7cSRatan Gupta                             {
496271584abSEd Tanous                                 confData.serverType = *strValue;
49754fc587aSNagaraju Goruganti                             }
49854fc587aSNagaraju Goruganti                         }
49954fc587aSNagaraju Goruganti                     }
500002d39b4SEd Tanous                     else if (interface.first ==
5010fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
50254fc587aSNagaraju Goruganti                     {
50354fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
50454fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
50554fc587aSNagaraju Goruganti                         {
506271584abSEd Tanous                             const std::string* strValue =
507002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
50854fc587aSNagaraju Goruganti 
509271584abSEd Tanous                             if (strValue == nullptr)
51054fc587aSNagaraju Goruganti                             {
51154fc587aSNagaraju Goruganti                                 continue;
51254fc587aSNagaraju Goruganti                             }
51354fc587aSNagaraju Goruganti 
51454fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
51554fc587aSNagaraju Goruganti                             {
516271584abSEd Tanous                                 roleMapData.groupName = *strValue;
51754fc587aSNagaraju Goruganti                             }
51854fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
51954fc587aSNagaraju Goruganti                             {
520271584abSEd Tanous                                 roleMapData.privilege = *strValue;
52154fc587aSNagaraju Goruganti                             }
52254fc587aSNagaraju Goruganti                         }
52354fc587aSNagaraju Goruganti 
524002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
525002d39b4SEd Tanous                                                             roleMapData);
52654fc587aSNagaraju Goruganti                     }
52754fc587aSNagaraju Goruganti                 }
52854fc587aSNagaraju Goruganti             }
529ab828d7cSRatan Gupta             callback(true, confData, ldapType);
53054fc587aSNagaraju Goruganti             },
531002d39b4SEd Tanous             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
53254fc587aSNagaraju Goruganti         },
53354fc587aSNagaraju Goruganti         mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
53423a21a1cSEd Tanous         ldapConfigObjectName, interfaces);
5356973a582SRatan Gupta }
5366973a582SRatan Gupta 
5378a07d286SRatan Gupta /**
5388a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
5398a07d286SRatan Gupta  * @param input JSON data
5408a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5418a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
5428a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
5438a07d286SRatan Gupta  */
5444f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
5456c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5466c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
5478a07d286SRatan Gupta {
5488a07d286SRatan Gupta     std::optional<std::string> authType;
5498a07d286SRatan Gupta 
5508a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
5518a07d286SRatan Gupta                              authType, "Username", username, "Password",
5528a07d286SRatan Gupta                              password))
5538a07d286SRatan Gupta     {
5548a07d286SRatan Gupta         return;
5558a07d286SRatan Gupta     }
5568a07d286SRatan Gupta     if (!authType)
5578a07d286SRatan Gupta     {
5588a07d286SRatan Gupta         return;
5598a07d286SRatan Gupta     }
5608a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
5618a07d286SRatan Gupta     {
5628a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
5638a07d286SRatan Gupta                                          "AuthenticationType");
5648a07d286SRatan Gupta         return;
5658a07d286SRatan Gupta     }
5668a07d286SRatan Gupta }
5678a07d286SRatan Gupta /**
5688a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
5698a07d286SRatan Gupta  * @param input JSON data
5708a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5718a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
5728a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
5738a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
5748a07d286SRatan Gupta  */
5758a07d286SRatan Gupta 
5764f48d5f6SEd Tanous inline void
5774f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
5788d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5798a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
5808a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
5818a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
5828a07d286SRatan Gupta {
5838a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
5848a07d286SRatan Gupta 
5858a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
5868a07d286SRatan Gupta                              searchSettings))
5878a07d286SRatan Gupta     {
5888a07d286SRatan Gupta         return;
5898a07d286SRatan Gupta     }
5908a07d286SRatan Gupta     if (!searchSettings)
5918a07d286SRatan Gupta     {
5928a07d286SRatan Gupta         return;
5938a07d286SRatan Gupta     }
5948a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
5958a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
5968a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
5978a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
5988a07d286SRatan Gupta     {
5998a07d286SRatan Gupta         return;
6008a07d286SRatan Gupta     }
6018a07d286SRatan Gupta }
6028a07d286SRatan Gupta /**
6038a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6048a07d286SRatan Gupta           json response with the new value.
6058a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6068a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6078a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6088a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6098a07d286SRatan Gupta  */
6108a07d286SRatan Gupta 
6114f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6128a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6138d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6148a07d286SRatan Gupta     const std::string& ldapServerElementName,
6158a07d286SRatan Gupta     const std::string& ldapConfigObject)
6168a07d286SRatan Gupta {
6178a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6188a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
6198a07d286SRatan Gupta          serviceAddressList](const boost::system::error_code ec) {
6208a07d286SRatan Gupta         if (ec)
6218a07d286SRatan Gupta         {
6228a07d286SRatan Gupta             BMCWEB_LOG_DEBUG
623c61704abSGunnar Mills                 << "Error Occurred in updating the service address";
6248a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6258a07d286SRatan Gupta             return;
6268a07d286SRatan Gupta         }
6278a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
6288a07d286SRatan Gupta             serviceAddressList.front()};
629002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
6308a07d286SRatan Gupta             modifiedserviceAddressList;
6318a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
6328a07d286SRatan Gupta         {
633002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
6348a07d286SRatan Gupta                                             serviceAddressList.front());
6358a07d286SRatan Gupta         }
6368a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
6378a07d286SRatan Gupta         },
6388a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6398a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
640168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
6418a07d286SRatan Gupta }
6428a07d286SRatan Gupta /**
6438a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
6448a07d286SRatan Gupta           json response with the new value.
6458a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
6468a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6478a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6488a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6498a07d286SRatan Gupta  */
6508a07d286SRatan Gupta 
6514f48d5f6SEd Tanous inline void
6524f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
6538d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6548a07d286SRatan Gupta                         const std::string& ldapServerElementName,
6558a07d286SRatan Gupta                         const std::string& ldapConfigObject)
6568a07d286SRatan Gupta {
6578a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6588a07d286SRatan Gupta         [asyncResp, username,
6598a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
6608a07d286SRatan Gupta         if (ec)
6618a07d286SRatan Gupta         {
6626c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
6638a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6648a07d286SRatan Gupta             return;
6658a07d286SRatan Gupta         }
666002d39b4SEd Tanous         asyncResp->res
667002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Username"] =
668002d39b4SEd Tanous             username;
6698a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
6708a07d286SRatan Gupta         },
6718a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
672168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
673168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
6748a07d286SRatan Gupta }
6758a07d286SRatan Gupta 
6768a07d286SRatan Gupta /**
6778a07d286SRatan Gupta  * @brief updates the LDAP password
6788a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
6798a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6808a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6818a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
6828a07d286SRatan Gupta  */
6838a07d286SRatan Gupta 
6844f48d5f6SEd Tanous inline void
6854f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
6868d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6878a07d286SRatan Gupta                         const std::string& ldapServerElementName,
6888a07d286SRatan Gupta                         const std::string& ldapConfigObject)
6898a07d286SRatan Gupta {
6908a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6918a07d286SRatan Gupta         [asyncResp, password,
6928a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
6938a07d286SRatan Gupta         if (ec)
6948a07d286SRatan Gupta         {
6956c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
6968a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6978a07d286SRatan Gupta             return;
6988a07d286SRatan Gupta         }
699002d39b4SEd Tanous         asyncResp->res
700002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Password"] =
701002d39b4SEd Tanous             "";
7028a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
7038a07d286SRatan Gupta         },
7048a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7058a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
706168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
7078a07d286SRatan Gupta }
7088a07d286SRatan Gupta 
7098a07d286SRatan Gupta /**
7108a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7118a07d286SRatan Gupta           json response with the new value.
7128a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7138a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7148a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7158a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7168a07d286SRatan Gupta  */
7178a07d286SRatan Gupta 
7184f48d5f6SEd Tanous inline void
7194f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7208d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7218a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7228a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7238a07d286SRatan Gupta {
7248a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7258a07d286SRatan Gupta         [asyncResp, baseDNList,
7268a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7278a07d286SRatan Gupta         if (ec)
7288a07d286SRatan Gupta         {
7296c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
7308a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7318a07d286SRatan Gupta             return;
7328a07d286SRatan Gupta         }
733002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7348a07d286SRatan Gupta         auto& searchSettingsJson =
7358a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7366c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
7376c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
7388a07d286SRatan Gupta         if (baseDNList.size() > 1)
7398a07d286SRatan Gupta         {
740002d39b4SEd Tanous             messages::propertyValueModified(
741002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
7428a07d286SRatan Gupta         }
7438a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
7448a07d286SRatan Gupta         },
7458a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7468a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
747168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
7488a07d286SRatan Gupta }
7498a07d286SRatan Gupta /**
7508a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
7518a07d286SRatan Gupta           json response with the new value.
7528a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
7538a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7548a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7558a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7568a07d286SRatan Gupta  */
7578a07d286SRatan Gupta 
7584f48d5f6SEd Tanous inline void
7594f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
7608d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7618a07d286SRatan Gupta                             const std::string& ldapServerElementName,
7628a07d286SRatan Gupta                             const std::string& ldapConfigObject)
7638a07d286SRatan Gupta {
7648a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7658a07d286SRatan Gupta         [asyncResp, userNameAttribute,
7668a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7678a07d286SRatan Gupta         if (ec)
7688a07d286SRatan Gupta         {
769c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
7708a07d286SRatan Gupta                                 "username attribute";
7718a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7728a07d286SRatan Gupta             return;
7738a07d286SRatan Gupta         }
774002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7758a07d286SRatan Gupta         auto& searchSettingsJson =
7768a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7778a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
7788a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
7798a07d286SRatan Gupta         },
7808a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7818a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
782168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
7838a07d286SRatan Gupta }
7848a07d286SRatan Gupta /**
7858a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
7868a07d286SRatan Gupta           json response with the new value.
7878a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
7888a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7898a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7908a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7918a07d286SRatan Gupta  */
7928a07d286SRatan Gupta 
7934f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
7948d1b46d7Szhanghch05     const std::string& groupsAttribute,
7958d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7968a07d286SRatan Gupta     const std::string& ldapServerElementName,
7978a07d286SRatan Gupta     const std::string& ldapConfigObject)
7988a07d286SRatan Gupta {
7998a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8008a07d286SRatan Gupta         [asyncResp, groupsAttribute,
8018a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8028a07d286SRatan Gupta         if (ec)
8038a07d286SRatan Gupta         {
804c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8058a07d286SRatan Gupta                                 "groupname attribute";
8068a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8078a07d286SRatan Gupta             return;
8088a07d286SRatan Gupta         }
809002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8108a07d286SRatan Gupta         auto& searchSettingsJson =
8118a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8128a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8138a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8148a07d286SRatan Gupta         },
8158a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8168a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
817168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
8188a07d286SRatan Gupta }
8198a07d286SRatan Gupta /**
8208a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
8218a07d286SRatan Gupta           json response with the new value.
8228a07d286SRatan Gupta  * @param input JSON data.
8238a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8248a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8258a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8268a07d286SRatan Gupta  */
8278a07d286SRatan Gupta 
8284f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8296c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8308a07d286SRatan Gupta     const std::string& ldapServerElementName,
8318a07d286SRatan Gupta     const std::string& ldapConfigObject)
8328a07d286SRatan Gupta {
8338a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8348a07d286SRatan Gupta         [asyncResp, serviceEnabled,
8358a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8368a07d286SRatan Gupta         if (ec)
8378a07d286SRatan Gupta         {
838002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
8398a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8408a07d286SRatan Gupta             return;
8418a07d286SRatan Gupta         }
8426c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
8438a07d286SRatan Gupta             serviceEnabled;
8446c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
8458a07d286SRatan Gupta         },
8468a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
847168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
848168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
8498a07d286SRatan Gupta }
8508a07d286SRatan Gupta 
8514f48d5f6SEd Tanous inline void
8524f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
8538d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
85478158631SZbigniew Kurzynski {
85578158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
85678158631SZbigniew Kurzynski     std::optional<bool> cookie;
85778158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
85878158631SZbigniew Kurzynski     std::optional<bool> xToken;
859501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
86078158631SZbigniew Kurzynski 
86178158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
86278158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
863501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
86478158631SZbigniew Kurzynski     {
86578158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
86678158631SZbigniew Kurzynski         return;
86778158631SZbigniew Kurzynski     }
86878158631SZbigniew Kurzynski 
86978158631SZbigniew Kurzynski     // Make a copy of methods configuration
87052cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
87152cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
87278158631SZbigniew Kurzynski 
87378158631SZbigniew Kurzynski     if (basicAuth)
87478158631SZbigniew Kurzynski     {
875f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
876f16f6263SAlan Kuo         messages::actionNotSupported(
8770fda0f12SGeorge Liu             asyncResp->res,
8780fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
879f16f6263SAlan Kuo         return;
880f16f6263SAlan Kuo #endif
88178158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
88278158631SZbigniew Kurzynski     }
88378158631SZbigniew Kurzynski 
88478158631SZbigniew Kurzynski     if (cookie)
88578158631SZbigniew Kurzynski     {
886f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
8870fda0f12SGeorge Liu         messages::actionNotSupported(
8880fda0f12SGeorge Liu             asyncResp->res,
8890fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
890f16f6263SAlan Kuo         return;
891f16f6263SAlan Kuo #endif
89278158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
89378158631SZbigniew Kurzynski     }
89478158631SZbigniew Kurzynski 
89578158631SZbigniew Kurzynski     if (sessionToken)
89678158631SZbigniew Kurzynski     {
897f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
898f16f6263SAlan Kuo         messages::actionNotSupported(
8990fda0f12SGeorge Liu             asyncResp->res,
9000fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
901f16f6263SAlan Kuo         return;
902f16f6263SAlan Kuo #endif
90378158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
90478158631SZbigniew Kurzynski     }
90578158631SZbigniew Kurzynski 
90678158631SZbigniew Kurzynski     if (xToken)
90778158631SZbigniew Kurzynski     {
908f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
9090fda0f12SGeorge Liu         messages::actionNotSupported(
9100fda0f12SGeorge Liu             asyncResp->res,
9110fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
912f16f6263SAlan Kuo         return;
913f16f6263SAlan Kuo #endif
91478158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
91578158631SZbigniew Kurzynski     }
91678158631SZbigniew Kurzynski 
917501f1e58SZbigniew Kurzynski     if (tls)
918501f1e58SZbigniew Kurzynski     {
919f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
9200fda0f12SGeorge Liu         messages::actionNotSupported(
9210fda0f12SGeorge Liu             asyncResp->res,
9220fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
923f16f6263SAlan Kuo         return;
924f16f6263SAlan Kuo #endif
925501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
926501f1e58SZbigniew Kurzynski     }
927501f1e58SZbigniew Kurzynski 
92878158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
929501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
930501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
93178158631SZbigniew Kurzynski     {
93278158631SZbigniew Kurzynski         // Do not allow user to disable everything
93378158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
93478158631SZbigniew Kurzynski                                      "of disabling all available methods");
93578158631SZbigniew Kurzynski         return;
93678158631SZbigniew Kurzynski     }
93778158631SZbigniew Kurzynski 
93852cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
93952cc112dSEd Tanous         authMethodsConfig);
94078158631SZbigniew Kurzynski     // Save configuration immediately
94152cc112dSEd Tanous     persistent_data::getConfig().writeData();
94278158631SZbigniew Kurzynski 
94378158631SZbigniew Kurzynski     messages::success(asyncResp->res);
94478158631SZbigniew Kurzynski }
94578158631SZbigniew Kurzynski 
9468a07d286SRatan Gupta /**
9478a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
9488a07d286SRatan Gupta  *        value and create the LDAP config object.
9498a07d286SRatan Gupta  * @param input JSON data
9508a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9518a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
9528a07d286SRatan Gupta  */
9538a07d286SRatan Gupta 
9546c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
9558d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9568a07d286SRatan Gupta                             const std::string& serverType)
9578a07d286SRatan Gupta {
958eb2bbe56SRatan Gupta     std::string dbusObjectPath;
959eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
960eb2bbe56SRatan Gupta     {
9612c70f800SEd Tanous         dbusObjectPath = adConfigObject;
962eb2bbe56SRatan Gupta     }
963eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
964eb2bbe56SRatan Gupta     {
96523a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
966eb2bbe56SRatan Gupta     }
967cb13a392SEd Tanous     else
968cb13a392SEd Tanous     {
969cb13a392SEd Tanous         return;
970cb13a392SEd Tanous     }
971eb2bbe56SRatan Gupta 
9728a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
9738a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
9748a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
9758a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
9768a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
9778a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
9788a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
9798a07d286SRatan Gupta     std::optional<std::string> userName;
9808a07d286SRatan Gupta     std::optional<std::string> password;
98106785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
9828a07d286SRatan Gupta 
9838a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
9848a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
9858a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
98606785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
98706785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
9888a07d286SRatan Gupta     {
9898a07d286SRatan Gupta         return;
9908a07d286SRatan Gupta     }
9918a07d286SRatan Gupta 
9928a07d286SRatan Gupta     if (authentication)
9938a07d286SRatan Gupta     {
9948a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
9958a07d286SRatan Gupta                                     password);
9968a07d286SRatan Gupta     }
9978a07d286SRatan Gupta     if (ldapService)
9988a07d286SRatan Gupta     {
9998a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10008a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
10018a07d286SRatan Gupta     }
10028a07d286SRatan Gupta     if (serviceAddressList)
10038a07d286SRatan Gupta     {
100426f6976fSEd Tanous         if (serviceAddressList->empty())
10058a07d286SRatan Gupta         {
10068a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10078a07d286SRatan Gupta                                              "ServiceAddress");
10088a07d286SRatan Gupta             return;
10098a07d286SRatan Gupta         }
10108a07d286SRatan Gupta     }
10118a07d286SRatan Gupta     if (baseDNList)
10128a07d286SRatan Gupta     {
101326f6976fSEd Tanous         if (baseDNList->empty())
10148a07d286SRatan Gupta         {
10158a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10168a07d286SRatan Gupta                                              "BaseDistinguishedNames");
10178a07d286SRatan Gupta             return;
10188a07d286SRatan Gupta         }
10198a07d286SRatan Gupta     }
10208a07d286SRatan Gupta 
10218a07d286SRatan Gupta     // nothing to update, then return
10228a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
102306785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
102406785244SRatan Gupta         !remoteRoleMapData)
10258a07d286SRatan Gupta     {
10268a07d286SRatan Gupta         return;
10278a07d286SRatan Gupta     }
10288a07d286SRatan Gupta 
10298a07d286SRatan Gupta     // Get the existing resource first then keep modifying
10308a07d286SRatan Gupta     // whenever any property gets updated.
1031002d39b4SEd Tanous     getLDAPConfigData(
1032002d39b4SEd Tanous         serverType,
1033002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1034002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1035002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
103623a21a1cSEd Tanous                             const std::string& serverT) {
10378a07d286SRatan Gupta         if (!success)
10388a07d286SRatan Gupta         {
10398a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10408a07d286SRatan Gupta             return;
10418a07d286SRatan Gupta         }
10426c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
10438a07d286SRatan Gupta         if (confData.serviceEnabled)
10448a07d286SRatan Gupta         {
10458a07d286SRatan Gupta             // Disable the service first and update the rest of
10468a07d286SRatan Gupta             // the properties.
10476c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
10488a07d286SRatan Gupta         }
10498a07d286SRatan Gupta 
10508a07d286SRatan Gupta         if (serviceAddressList)
10518a07d286SRatan Gupta         {
10526c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
10536c51eab1SEd Tanous                                       dbusObjectPath);
10548a07d286SRatan Gupta         }
10558a07d286SRatan Gupta         if (userName)
10568a07d286SRatan Gupta         {
10576c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
10588a07d286SRatan Gupta         }
10598a07d286SRatan Gupta         if (password)
10608a07d286SRatan Gupta         {
10616c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
10628a07d286SRatan Gupta         }
10638a07d286SRatan Gupta 
10648a07d286SRatan Gupta         if (baseDNList)
10658a07d286SRatan Gupta         {
10666c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
10678a07d286SRatan Gupta         }
10688a07d286SRatan Gupta         if (userNameAttribute)
10698a07d286SRatan Gupta         {
10706c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
10716c51eab1SEd Tanous                                     dbusObjectPath);
10728a07d286SRatan Gupta         }
10738a07d286SRatan Gupta         if (groupsAttribute)
10748a07d286SRatan Gupta         {
10756c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
10766c51eab1SEd Tanous                                      dbusObjectPath);
10778a07d286SRatan Gupta         }
10788a07d286SRatan Gupta         if (serviceEnabled)
10798a07d286SRatan Gupta         {
10808a07d286SRatan Gupta             // if user has given the value as true then enable
10818a07d286SRatan Gupta             // the service. if user has given false then no-op
10828a07d286SRatan Gupta             // as service is already stopped.
10838a07d286SRatan Gupta             if (*serviceEnabled)
10848a07d286SRatan Gupta             {
10856c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
10866c51eab1SEd Tanous                                          dbusObjectPath);
10878a07d286SRatan Gupta             }
10888a07d286SRatan Gupta         }
10898a07d286SRatan Gupta         else
10908a07d286SRatan Gupta         {
10918a07d286SRatan Gupta             // if user has not given the service enabled value
10928a07d286SRatan Gupta             // then revert it to the same state as it was
10938a07d286SRatan Gupta             // before.
10948a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
109523a21a1cSEd Tanous                                      serverT, dbusObjectPath);
10968a07d286SRatan Gupta         }
109706785244SRatan Gupta 
109806785244SRatan Gupta         if (remoteRoleMapData)
109906785244SRatan Gupta         {
11006c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
11016c51eab1SEd Tanous                                *remoteRoleMapData);
110206785244SRatan Gupta         }
11038a07d286SRatan Gupta         });
11048a07d286SRatan Gupta }
1105d4b5443fSEd Tanous 
11066c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
11076c51eab1SEd Tanous                                  const std::string& username,
11086c51eab1SEd Tanous                                  std::optional<std::string> password,
11096c51eab1SEd Tanous                                  std::optional<bool> enabled,
11106c51eab1SEd Tanous                                  std::optional<std::string> roleId,
11116c51eab1SEd Tanous                                  std::optional<bool> locked)
11121abe55efSEd Tanous {
1113b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1114b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1115b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
11166c51eab1SEd Tanous 
11176c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
11186c51eab1SEd Tanous         dbusObjectPath,
11191106333aSEd Tanous         [dbusObjectPath, username, password(std::move(password)),
11201106333aSEd Tanous          roleId(std::move(roleId)), enabled, locked,
11211106333aSEd Tanous          asyncResp{std::move(asyncResp)}](int rc) {
1122e662eae8SEd Tanous         if (rc <= 0)
11236c51eab1SEd Tanous         {
1124*d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11256c51eab1SEd Tanous                                        username);
11266c51eab1SEd Tanous             return;
11276c51eab1SEd Tanous         }
11286c51eab1SEd Tanous 
11296c51eab1SEd Tanous         if (password)
11306c51eab1SEd Tanous         {
11316c51eab1SEd Tanous             int retval = pamUpdatePassword(username, *password);
11326c51eab1SEd Tanous 
11336c51eab1SEd Tanous             if (retval == PAM_USER_UNKNOWN)
11346c51eab1SEd Tanous             {
1135*d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11366c51eab1SEd Tanous                                            username);
11376c51eab1SEd Tanous             }
11386c51eab1SEd Tanous             else if (retval == PAM_AUTHTOK_ERR)
11396c51eab1SEd Tanous             {
11406c51eab1SEd Tanous                 // If password is invalid
1141002d39b4SEd Tanous                 messages::propertyValueFormatError(asyncResp->res, *password,
1142002d39b4SEd Tanous                                                    "Password");
11436c51eab1SEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
11446c51eab1SEd Tanous             }
11456c51eab1SEd Tanous             else if (retval != PAM_SUCCESS)
11466c51eab1SEd Tanous             {
11476c51eab1SEd Tanous                 messages::internalError(asyncResp->res);
11486c51eab1SEd Tanous                 return;
11496c51eab1SEd Tanous             }
1150e7b1b62bSEd Tanous             else
1151e7b1b62bSEd Tanous             {
1152e7b1b62bSEd Tanous                 messages::success(asyncResp->res);
1153e7b1b62bSEd Tanous             }
11546c51eab1SEd Tanous         }
11556c51eab1SEd Tanous 
11566c51eab1SEd Tanous         if (enabled)
11576c51eab1SEd Tanous         {
11586c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
11596c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
11606c51eab1SEd Tanous                 if (ec)
11616c51eab1SEd Tanous                 {
11626c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
11636c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11646c51eab1SEd Tanous                     return;
11656c51eab1SEd Tanous                 }
11666c51eab1SEd Tanous                 messages::success(asyncResp->res);
11676c51eab1SEd Tanous                 return;
11686c51eab1SEd Tanous                 },
1169e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
11706c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
11716c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserEnabled",
1172168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*enabled});
11736c51eab1SEd Tanous         }
11746c51eab1SEd Tanous 
11756c51eab1SEd Tanous         if (roleId)
11766c51eab1SEd Tanous         {
11776c51eab1SEd Tanous             std::string priv = getPrivilegeFromRoleId(*roleId);
11786c51eab1SEd Tanous             if (priv.empty())
11796c51eab1SEd Tanous             {
11806c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, *roleId,
11816c51eab1SEd Tanous                                                  "RoleId");
11826c51eab1SEd Tanous                 return;
11836c51eab1SEd Tanous             }
11846c51eab1SEd Tanous             if (priv == "priv-noaccess")
11856c51eab1SEd Tanous             {
11866c51eab1SEd Tanous                 priv = "";
11876c51eab1SEd Tanous             }
11886c51eab1SEd Tanous 
11896c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
11906c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
11916c51eab1SEd Tanous                 if (ec)
11926c51eab1SEd Tanous                 {
11936c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
11946c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11956c51eab1SEd Tanous                     return;
11966c51eab1SEd Tanous                 }
11976c51eab1SEd Tanous                 messages::success(asyncResp->res);
11986c51eab1SEd Tanous                 },
1199e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12006c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12016c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1202168e20c1SEd Tanous                 dbus::utility::DbusVariantType{priv});
12036c51eab1SEd Tanous         }
12046c51eab1SEd Tanous 
12056c51eab1SEd Tanous         if (locked)
12066c51eab1SEd Tanous         {
12076c51eab1SEd Tanous             // admin can unlock the account which is locked by
12086c51eab1SEd Tanous             // successive authentication failures but admin should
12096c51eab1SEd Tanous             // not be allowed to lock an account.
12106c51eab1SEd Tanous             if (*locked)
12116c51eab1SEd Tanous             {
12126c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, "true",
12136c51eab1SEd Tanous                                                  "Locked");
12146c51eab1SEd Tanous                 return;
12156c51eab1SEd Tanous             }
12166c51eab1SEd Tanous 
12176c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
12186c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
12196c51eab1SEd Tanous                 if (ec)
12206c51eab1SEd Tanous                 {
12216c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12226c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12236c51eab1SEd Tanous                     return;
12246c51eab1SEd Tanous                 }
12256c51eab1SEd Tanous                 messages::success(asyncResp->res);
12266c51eab1SEd Tanous                 return;
12276c51eab1SEd Tanous                 },
1228e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12296c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12306c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes",
1231168e20c1SEd Tanous                 "UserLockedForFailedAttempt",
1232168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*locked});
12336c51eab1SEd Tanous         }
12346c51eab1SEd Tanous         });
12356c51eab1SEd Tanous }
12366c51eab1SEd Tanous 
12374c7d4d33SEd Tanous inline void handleAccountServiceHead(
12384c7d4d33SEd Tanous     App& app, const crow::Request& req,
12391ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12406c51eab1SEd Tanous {
12414c7d4d33SEd Tanous 
12423ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
124345ca1b86SEd Tanous     {
124445ca1b86SEd Tanous         return;
124545ca1b86SEd Tanous     }
12464c7d4d33SEd Tanous     asyncResp->res.addHeader(
12474c7d4d33SEd Tanous         boost::beast::http::field::link,
12484c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
12494c7d4d33SEd Tanous }
12504c7d4d33SEd Tanous 
12514c7d4d33SEd Tanous inline void
12524c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
12534c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12544c7d4d33SEd Tanous {
12554c7d4d33SEd Tanous     handleAccountServiceHead(app, req, asyncResp);
125652cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
12571ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
125878158631SZbigniew Kurzynski 
12591476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
12601476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
12611476687dSEd Tanous     json["@odata.type"] = "#AccountService."
12621476687dSEd Tanous                           "v1_10_0.AccountService";
12631476687dSEd Tanous     json["Id"] = "AccountService";
12641476687dSEd Tanous     json["Name"] = "Account Service";
12651476687dSEd Tanous     json["Description"] = "Account Service";
12661476687dSEd Tanous     json["ServiceEnabled"] = true;
12671476687dSEd Tanous     json["MaxPasswordLength"] = 20;
12681ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/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;
12781ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
12791ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
12801ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
12811476687dSEd Tanous 
12821ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
12831ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
12841ef4c342SEd Tanous     // permissions ConfigureManager
128572048780SAbhishek Patel     Privileges effectiveUserPrivileges =
128672048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
128772048780SAbhishek Patel 
128872048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
128972048780SAbhishek Patel                                          effectiveUserPrivileges))
129072048780SAbhishek Patel     {
12911ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
12921476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
129372048780SAbhishek Patel     }
12943d958bbcSAppaRao Puli     crow::connections::systemBus->async_method_call(
1295002d39b4SEd Tanous         [asyncResp](const boost::system::error_code ec,
12961ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
12973d958bbcSAppaRao Puli         if (ec)
12983d958bbcSAppaRao Puli         {
12993d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
13003d958bbcSAppaRao Puli             return;
13013d958bbcSAppaRao Puli         }
13023d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
13033d958bbcSAppaRao Puli                          << "properties for AccountService";
1304002d39b4SEd Tanous         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
1305168e20c1SEd Tanous                  property : propertiesList)
13063d958bbcSAppaRao Puli         {
13073d958bbcSAppaRao Puli             if (property.first == "MinPasswordLength")
13083d958bbcSAppaRao Puli             {
13091ef4c342SEd Tanous                 const uint8_t* value = std::get_if<uint8_t>(&property.second);
13103d958bbcSAppaRao Puli                 if (value != nullptr)
13113d958bbcSAppaRao Puli                 {
1312002d39b4SEd Tanous                     asyncResp->res.jsonValue["MinPasswordLength"] = *value;
13133d958bbcSAppaRao Puli                 }
13143d958bbcSAppaRao Puli             }
13153d958bbcSAppaRao Puli             if (property.first == "AccountUnlockTimeout")
13163d958bbcSAppaRao Puli             {
13171ef4c342SEd Tanous                 const uint32_t* value = std::get_if<uint32_t>(&property.second);
13183d958bbcSAppaRao Puli                 if (value != nullptr)
13193d958bbcSAppaRao Puli                 {
13201ef4c342SEd Tanous                     asyncResp->res.jsonValue["AccountLockoutDuration"] = *value;
13213d958bbcSAppaRao Puli                 }
13223d958bbcSAppaRao Puli             }
13233d958bbcSAppaRao Puli             if (property.first == "MaxLoginAttemptBeforeLockout")
13243d958bbcSAppaRao Puli             {
13251ef4c342SEd Tanous                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
13263d958bbcSAppaRao Puli                 if (value != nullptr)
13273d958bbcSAppaRao Puli                 {
1328002d39b4SEd Tanous                     asyncResp->res.jsonValue["AccountLockoutThreshold"] =
13296c51eab1SEd Tanous                         *value;
13303d958bbcSAppaRao Puli                 }
13313d958bbcSAppaRao Puli             }
13323d958bbcSAppaRao Puli         }
13333d958bbcSAppaRao Puli         },
13341ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
13353d958bbcSAppaRao Puli         "org.freedesktop.DBus.Properties", "GetAll",
13363d958bbcSAppaRao Puli         "xyz.openbmc_project.User.AccountPolicy");
13376973a582SRatan Gupta 
133802cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1339ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1340cb13a392SEd Tanous         if (!success)
1341cb13a392SEd Tanous         {
1342cb13a392SEd Tanous             return;
1343cb13a392SEd Tanous         }
1344002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1345ab828d7cSRatan Gupta     };
1346ab828d7cSRatan Gupta 
1347ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1348ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
13491ef4c342SEd Tanous }
13506973a582SRatan Gupta 
13511ef4c342SEd Tanous inline void handleAccountServicePatch(
13521ef4c342SEd Tanous     App& app, const crow::Request& req,
13531ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13541ef4c342SEd Tanous {
13553ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
135645ca1b86SEd Tanous     {
135745ca1b86SEd Tanous         return;
135845ca1b86SEd Tanous     }
1359f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1360f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1361ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1362f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1363f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1364f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1365f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1366f5ffd806SEd Tanous 
136715ed6780SWilly Tu     if (!json_util::readJsonPatch(
13681ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
13691ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
13701ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
13711ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1372f5ffd806SEd Tanous             oemObject))
1373f5ffd806SEd Tanous     {
1374f5ffd806SEd Tanous         return;
1375f5ffd806SEd Tanous     }
1376f5ffd806SEd Tanous 
1377f5ffd806SEd Tanous     if (minPasswordLength)
1378f5ffd806SEd Tanous     {
1379ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
1380ef73ad0dSPaul Fertser             [asyncResp](const boost::system::error_code ec) {
1381ef73ad0dSPaul Fertser             if (ec)
1382ef73ad0dSPaul Fertser             {
1383ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1384ef73ad0dSPaul Fertser                 return;
1385ef73ad0dSPaul Fertser             }
1386ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1387ef73ad0dSPaul Fertser             },
13881ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1389ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
13901ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1391ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1392f5ffd806SEd Tanous     }
1393f5ffd806SEd Tanous 
1394f5ffd806SEd Tanous     if (maxPasswordLength)
1395f5ffd806SEd Tanous     {
13961ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1397f5ffd806SEd Tanous     }
1398f5ffd806SEd Tanous 
1399f5ffd806SEd Tanous     if (ldapObject)
1400f5ffd806SEd Tanous     {
1401f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1402f5ffd806SEd Tanous     }
1403f5ffd806SEd Tanous 
1404f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
14051ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1406f5ffd806SEd Tanous                                          oemOpenBMCObject))
1407f5ffd806SEd Tanous     {
1408f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1409f5ffd806SEd Tanous             oemOpenBMCObject &&
1410f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1411f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1412f5ffd806SEd Tanous         {
1413f5ffd806SEd Tanous             if (authMethodsObject)
1414f5ffd806SEd Tanous             {
14151ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1416f5ffd806SEd Tanous             }
1417f5ffd806SEd Tanous         }
1418f5ffd806SEd Tanous     }
1419f5ffd806SEd Tanous 
1420f5ffd806SEd Tanous     if (activeDirectoryObject)
1421f5ffd806SEd Tanous     {
14221ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1423f5ffd806SEd Tanous     }
1424f5ffd806SEd Tanous 
1425f5ffd806SEd Tanous     if (unlockTimeout)
1426f5ffd806SEd Tanous     {
1427f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1428f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1429f5ffd806SEd Tanous             if (ec)
1430f5ffd806SEd Tanous             {
1431f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1432f5ffd806SEd Tanous                 return;
1433f5ffd806SEd Tanous             }
1434f5ffd806SEd Tanous             messages::success(asyncResp->res);
1435f5ffd806SEd Tanous             },
14361ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1437f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
14381ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1439168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1440f5ffd806SEd Tanous     }
1441f5ffd806SEd Tanous     if (lockoutThreshold)
1442f5ffd806SEd Tanous     {
1443f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1444f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1445f5ffd806SEd Tanous             if (ec)
1446f5ffd806SEd Tanous             {
1447f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1448f5ffd806SEd Tanous                 return;
1449f5ffd806SEd Tanous             }
1450f5ffd806SEd Tanous             messages::success(asyncResp->res);
1451f5ffd806SEd Tanous             },
14521ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1453f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1454f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1455f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1456168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1457f5ffd806SEd Tanous     }
14581ef4c342SEd Tanous }
1459f5ffd806SEd Tanous 
14604c7d4d33SEd Tanous inline void handleAccountCollectionHead(
14611ef4c342SEd Tanous     App& app, const crow::Request& req,
14621ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14631ef4c342SEd Tanous {
14644c7d4d33SEd Tanous 
14653ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
146645ca1b86SEd Tanous     {
146745ca1b86SEd Tanous         return;
146845ca1b86SEd Tanous     }
14694c7d4d33SEd Tanous     asyncResp->res.addHeader(
14704c7d4d33SEd Tanous         boost::beast::http::field::link,
14714c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
14724c7d4d33SEd Tanous }
14734c7d4d33SEd Tanous 
14744c7d4d33SEd Tanous inline void handleAccountCollectionGet(
14754c7d4d33SEd Tanous     App& app, const crow::Request& req,
14764c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14774c7d4d33SEd Tanous {
14784c7d4d33SEd Tanous     handleAccountCollectionHead(app, req, asyncResp);
14791476687dSEd Tanous 
14801476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
14811476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
14821ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
14831476687dSEd Tanous                                               "ManagerAccountCollection";
14841476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
14851476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
14860f74e643SEd Tanous 
14876c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
14886c51eab1SEd Tanous         redfish::getUserPrivileges(req.userRole);
14896c51eab1SEd Tanous 
1490f5e29f33SJunLin Chen     std::string thisUser;
1491f5e29f33SJunLin Chen     if (req.session)
1492f5e29f33SJunLin Chen     {
1493f5e29f33SJunLin Chen         thisUser = req.session->username;
1494f5e29f33SJunLin Chen     }
1495b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
1496cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
1497cef1ddfbSEd Tanous             const boost::system::error_code ec,
1498711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1499b9b2e0b2SEd Tanous         if (ec)
1500b9b2e0b2SEd Tanous         {
1501f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1502b9b2e0b2SEd Tanous             return;
1503b9b2e0b2SEd Tanous         }
1504b9b2e0b2SEd Tanous 
1505cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1506002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1507cef1ddfbSEd Tanous 
1508cef1ddfbSEd Tanous         bool userCanSeeSelf =
1509002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1510cef1ddfbSEd Tanous 
1511002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1512b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1513b9b2e0b2SEd Tanous 
15149eb808c1SEd Tanous         for (const auto& userpath : users)
1515b9b2e0b2SEd Tanous         {
15162dfd18efSEd Tanous             std::string user = userpath.first.filename();
15172dfd18efSEd Tanous             if (user.empty())
1518b9b2e0b2SEd Tanous             {
15192dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
15202dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
15212dfd18efSEd Tanous 
15222dfd18efSEd Tanous                 return;
1523b9b2e0b2SEd Tanous             }
1524f365910cSGunnar Mills 
1525f365910cSGunnar Mills             // As clarified by Redfish here:
1526f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
15276c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
15286c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
15296c51eab1SEd Tanous             // accounts.
15301ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1531f365910cSGunnar Mills             {
15321476687dSEd Tanous                 nlohmann::json::object_t member;
15331476687dSEd Tanous                 member["@odata.id"] =
1534002d39b4SEd Tanous                     "/redfish/v1/AccountService/Accounts/" + user;
15351476687dSEd Tanous                 memberArray.push_back(std::move(member));
1536b9b2e0b2SEd Tanous             }
1537f365910cSGunnar Mills         }
15381ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1539b9b2e0b2SEd Tanous         },
15401ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1541b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15421ef4c342SEd Tanous }
15436c51eab1SEd Tanous 
15441ef4c342SEd Tanous inline void handleAccountCollectionPost(
15451ef4c342SEd Tanous     App& app, const crow::Request& req,
15461ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15471ef4c342SEd Tanous {
15483ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
154945ca1b86SEd Tanous     {
155045ca1b86SEd Tanous         return;
155145ca1b86SEd Tanous     }
15529712f8acSEd Tanous     std::string username;
15539712f8acSEd Tanous     std::string password;
1554a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1555a24526dcSEd Tanous     std::optional<bool> enabled = true;
15561ef4c342SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
15571ef4c342SEd Tanous                                   "Password", password, "RoleId", roleId,
15581ef4c342SEd Tanous                                   "Enabled", enabled))
155904ae99ecSEd Tanous     {
156004ae99ecSEd Tanous         return;
156104ae99ecSEd Tanous     }
156204ae99ecSEd Tanous 
156354fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
156484e12cb7SAppaRao Puli     if (priv.empty())
156504ae99ecSEd Tanous     {
15661ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
156704ae99ecSEd Tanous         return;
156804ae99ecSEd Tanous     }
156996200606Sjayaprakash Mutyala     // TODO: Following override will be reverted once support in
15706c51eab1SEd Tanous     // phosphor-user-manager is added. In order to avoid dependency
15716c51eab1SEd Tanous     // issues, this is added in bmcweb, which will removed, once
157296200606Sjayaprakash Mutyala     // phosphor-user-manager supports priv-noaccess.
157396200606Sjayaprakash Mutyala     if (priv == "priv-noaccess")
157496200606Sjayaprakash Mutyala     {
157596200606Sjayaprakash Mutyala         roleId = "";
157696200606Sjayaprakash Mutyala     }
157796200606Sjayaprakash Mutyala     else
157896200606Sjayaprakash Mutyala     {
15799712f8acSEd Tanous         roleId = priv;
158096200606Sjayaprakash Mutyala     }
158104ae99ecSEd Tanous 
1582599c71d8SAyushi Smriti     // Reading AllGroups property
15831e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
15841ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
15851ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
15861ef4c342SEd Tanous         "AllGroups",
1587599c71d8SAyushi Smriti         [asyncResp, username, password{std::move(password)}, roleId,
1588168e20c1SEd Tanous          enabled](const boost::system::error_code ec,
15891e1e598dSJonathan Doman                   const std::vector<std::string>& allGroupsList) {
1590599c71d8SAyushi Smriti         if (ec)
1591599c71d8SAyushi Smriti         {
1592599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1593599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1594599c71d8SAyushi Smriti             return;
1595599c71d8SAyushi Smriti         }
1596599c71d8SAyushi Smriti 
15971e1e598dSJonathan Doman         if (allGroupsList.empty())
1598599c71d8SAyushi Smriti         {
1599599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1600599c71d8SAyushi Smriti             return;
1601599c71d8SAyushi Smriti         }
1602599c71d8SAyushi Smriti 
160304ae99ecSEd Tanous         crow::connections::systemBus->async_method_call(
16041ef4c342SEd Tanous             [asyncResp, username, password](const boost::system::error_code ec2,
160559d494eeSPatrick Williams                                             sdbusplus::message_t& m) {
160623a21a1cSEd Tanous             if (ec2)
160704ae99ecSEd Tanous             {
16081ef4c342SEd Tanous                 userErrorMessageHandler(m.get_error(), asyncResp, username, "");
160904ae99ecSEd Tanous                 return;
161004ae99ecSEd Tanous             }
161104ae99ecSEd Tanous 
1612002d39b4SEd Tanous             if (pamUpdatePassword(username, password) != PAM_SUCCESS)
161304ae99ecSEd Tanous             {
16146c51eab1SEd Tanous                 // At this point we have a user that's been
16156c51eab1SEd Tanous                 // created, but the password set
16166c51eab1SEd Tanous                 // failed.Something is wrong, so delete the user
16176c51eab1SEd Tanous                 // that we've already created
16181ef4c342SEd Tanous                 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1619b477fd44SP Dheeraj Srujan Kumar                 tempObjPath /= username;
1620b477fd44SP Dheeraj Srujan Kumar                 const std::string userPath(tempObjPath);
1621b477fd44SP Dheeraj Srujan Kumar 
162204ae99ecSEd Tanous                 crow::connections::systemBus->async_method_call(
16231ef4c342SEd Tanous                     [asyncResp, password](const boost::system::error_code ec3) {
162423a21a1cSEd Tanous                     if (ec3)
162504ae99ecSEd Tanous                     {
1626002d39b4SEd Tanous                         messages::internalError(asyncResp->res);
162704ae99ecSEd Tanous                         return;
162804ae99ecSEd Tanous                     }
162904ae99ecSEd Tanous 
16300d4197e0Sanil kumar appana                     // If password is invalid
16311ef4c342SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, password,
16321ef4c342SEd Tanous                                                        "Password");
163304ae99ecSEd Tanous                     },
1634002d39b4SEd Tanous                     "xyz.openbmc_project.User.Manager", userPath,
1635002d39b4SEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
163604ae99ecSEd Tanous 
163704ae99ecSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
163804ae99ecSEd Tanous                 return;
163904ae99ecSEd Tanous             }
164004ae99ecSEd Tanous 
1641f12894f8SJason M. Bills             messages::created(asyncResp->res);
164204ae99ecSEd Tanous             asyncResp->res.addHeader(
16431ef4c342SEd Tanous                 "Location", "/redfish/v1/AccountService/Accounts/" + username);
164404ae99ecSEd Tanous             },
1645002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1646002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "CreateUser", username,
1647002d39b4SEd Tanous             allGroupsList, *roleId, *enabled);
16481e1e598dSJonathan Doman         });
16491ef4c342SEd Tanous }
1650b9b2e0b2SEd Tanous 
16511ef4c342SEd Tanous inline void
16524c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
16536c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16544c7d4d33SEd Tanous                       const std::string& /*accountName*/)
16551ef4c342SEd Tanous {
16564c7d4d33SEd Tanous 
16573ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
165845ca1b86SEd Tanous     {
165945ca1b86SEd Tanous         return;
166045ca1b86SEd Tanous     }
16614c7d4d33SEd Tanous     asyncResp->res.addHeader(
16624c7d4d33SEd Tanous         boost::beast::http::field::link,
16634c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
16644c7d4d33SEd Tanous }
16654c7d4d33SEd Tanous inline void
16664c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
16674c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16684c7d4d33SEd Tanous                      const std::string& accountName)
16694c7d4d33SEd Tanous {
16704c7d4d33SEd Tanous     handleAccountHead(app, req, asyncResp, accountName);
16711ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1672031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1673*d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1674031514fbSJunLin Chen     return;
1675031514fbSJunLin Chen 
16761ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1677031514fbSJunLin Chen     if (req.session == nullptr)
1678031514fbSJunLin Chen     {
1679031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1680031514fbSJunLin Chen         return;
1681031514fbSJunLin Chen     }
16826c51eab1SEd Tanous     if (req.session->username != accountName)
1683b9b2e0b2SEd Tanous     {
16846c51eab1SEd Tanous         // At this point we've determined that the user is trying to
16851ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
16861ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
16871ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
16886c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
16896c51eab1SEd Tanous             redfish::getUserPrivileges(req.userRole);
16901ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
16911ef4c342SEd Tanous                                                          "ConfigureManager"};
16926c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
16936c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1694900f9497SJoseph Reynolds         {
1695900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
1696900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1697900f9497SJoseph Reynolds             return;
1698900f9497SJoseph Reynolds         }
1699900f9497SJoseph Reynolds     }
1700900f9497SJoseph Reynolds 
1701b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
17021ef4c342SEd Tanous         [asyncResp,
17031ef4c342SEd Tanous          accountName](const boost::system::error_code ec,
1704711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1705b9b2e0b2SEd Tanous         if (ec)
1706b9b2e0b2SEd Tanous         {
1707f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1708b9b2e0b2SEd Tanous             return;
1709b9b2e0b2SEd Tanous         }
1710711ac7a9SEd Tanous         const auto userIt = std::find_if(
1711b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
1712b477fd44SP Dheeraj Srujan Kumar             [accountName](
1713b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
1714002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
171555f79e6fSEd Tanous             return accountName == user.first.filename();
1716b477fd44SP Dheeraj Srujan Kumar             });
1717b9b2e0b2SEd Tanous 
171884e12cb7SAppaRao Puli         if (userIt == users.end())
1719b9b2e0b2SEd Tanous         {
1720002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1721002d39b4SEd Tanous                                        accountName);
172284e12cb7SAppaRao Puli             return;
172384e12cb7SAppaRao Puli         }
17244e68c45bSAyushi Smriti 
17251476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
17261476687dSEd Tanous             "#ManagerAccount.v1_4_0.ManagerAccount";
17271476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
17281476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
17291476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
17301476687dSEd Tanous         asyncResp->res.jsonValue["AccountTypes"] = {"Redfish"};
17314e68c45bSAyushi Smriti 
173284e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
173365b0dc32SEd Tanous         {
1734002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
173565b0dc32SEd Tanous             {
173665b0dc32SEd Tanous                 for (const auto& property : interface.second)
173765b0dc32SEd Tanous                 {
173865b0dc32SEd Tanous                     if (property.first == "UserEnabled")
173965b0dc32SEd Tanous                     {
174065b0dc32SEd Tanous                         const bool* userEnabled =
1741abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
174265b0dc32SEd Tanous                         if (userEnabled == nullptr)
174365b0dc32SEd Tanous                         {
1744002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
174584e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
174684e12cb7SAppaRao Puli                             return;
174765b0dc32SEd Tanous                         }
1748002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
174965b0dc32SEd Tanous                     }
1750002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
175165b0dc32SEd Tanous                     {
175265b0dc32SEd Tanous                         const bool* userLocked =
1753abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
175465b0dc32SEd Tanous                         if (userLocked == nullptr)
175565b0dc32SEd Tanous                         {
175684e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
175784e12cb7SAppaRao Puli                                                 "ailedAttempt "
175884e12cb7SAppaRao Puli                                                 "wasn't a bool";
175984e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
176084e12cb7SAppaRao Puli                             return;
176165b0dc32SEd Tanous                         }
1762002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
1763002d39b4SEd Tanous                         asyncResp->res
1764002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
17653bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
176665b0dc32SEd Tanous                     }
176784e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
176884e12cb7SAppaRao Puli                     {
176954fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1770002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
177154fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
177284e12cb7SAppaRao Puli                         {
1773002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
177484e12cb7SAppaRao Puli                                                 "string";
177584e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
177684e12cb7SAppaRao Puli                             return;
177784e12cb7SAppaRao Puli                         }
17781ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
177954fc587aSNagaraju Goruganti                         if (role.empty())
178084e12cb7SAppaRao Puli                         {
178184e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
178284e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
178384e12cb7SAppaRao Puli                             return;
178484e12cb7SAppaRao Puli                         }
178554fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
178684e12cb7SAppaRao Puli 
17871476687dSEd Tanous                         nlohmann::json& roleEntry =
1788002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
17891476687dSEd Tanous                         roleEntry["@odata.id"] =
1790002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
179184e12cb7SAppaRao Puli                     }
1792002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
17933bf4e632SJoseph Reynolds                     {
17943bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
17953bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
17963bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
17973bf4e632SJoseph Reynolds                         {
17980fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
17990fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
18003bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
18013bf4e632SJoseph Reynolds                             return;
18023bf4e632SJoseph Reynolds                         }
1803002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
18043bf4e632SJoseph Reynolds                             *userPasswordExpired;
18053bf4e632SJoseph Reynolds                     }
180665b0dc32SEd Tanous                 }
180765b0dc32SEd Tanous             }
180865b0dc32SEd Tanous         }
180965b0dc32SEd Tanous 
1810b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
181184e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
1812b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
1813b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
1814b9b2e0b2SEd Tanous         },
18151ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1816b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18171ef4c342SEd Tanous }
1818a840879dSEd Tanous 
18191ef4c342SEd Tanous inline void
18201ef4c342SEd Tanous     handleAccounttDelete(App& app, const crow::Request& req,
18216c51eab1SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18221ef4c342SEd Tanous                          const std::string& username)
18231ef4c342SEd Tanous {
18243ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
182545ca1b86SEd Tanous     {
182645ca1b86SEd Tanous         return;
182745ca1b86SEd Tanous     }
18281ef4c342SEd Tanous 
18291ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1830031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1831*d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1832031514fbSJunLin Chen     return;
1833031514fbSJunLin Chen 
18341ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
18351ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
18361ef4c342SEd Tanous     tempObjPath /= username;
18371ef4c342SEd Tanous     const std::string userPath(tempObjPath);
18381ef4c342SEd Tanous 
18391ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
18401ef4c342SEd Tanous         [asyncResp, username](const boost::system::error_code ec) {
18411ef4c342SEd Tanous         if (ec)
18421ef4c342SEd Tanous         {
1843*d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
18441ef4c342SEd Tanous                                        username);
18451ef4c342SEd Tanous             return;
18461ef4c342SEd Tanous         }
18471ef4c342SEd Tanous 
18481ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
18491ef4c342SEd Tanous         },
18501ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
18511ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
18521ef4c342SEd Tanous }
18531ef4c342SEd Tanous 
18541ef4c342SEd Tanous inline void
18551ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
18561ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18571ef4c342SEd Tanous                        const std::string& username)
18581ef4c342SEd Tanous {
18591ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18601ef4c342SEd Tanous     {
18611ef4c342SEd Tanous         return;
18621ef4c342SEd Tanous     }
18631ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
18641ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
1865*d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
18661ef4c342SEd Tanous     return;
18671ef4c342SEd Tanous 
18681ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1869a24526dcSEd Tanous     std::optional<std::string> newUserName;
1870a24526dcSEd Tanous     std::optional<std::string> password;
1871a24526dcSEd Tanous     std::optional<bool> enabled;
1872a24526dcSEd Tanous     std::optional<std::string> roleId;
187324c8542dSRatan Gupta     std::optional<bool> locked;
1874e9cc5172SEd Tanous 
1875031514fbSJunLin Chen     if (req.session == nullptr)
1876031514fbSJunLin Chen     {
1877031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1878031514fbSJunLin Chen         return;
1879031514fbSJunLin Chen     }
1880031514fbSJunLin Chen 
1881e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
1882e9cc5172SEd Tanous         redfish::getUserPrivileges(req.userRole);
1883e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
1884e9cc5172SEd Tanous     bool userHasConfigureUsers =
1885e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
1886e9cc5172SEd Tanous     if (userHasConfigureUsers)
1887e9cc5172SEd Tanous     {
1888e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
18891ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "UserName",
18901ef4c342SEd Tanous                                       newUserName, "Password", password,
18911ef4c342SEd Tanous                                       "RoleId", roleId, "Enabled", enabled,
18921ef4c342SEd Tanous                                       "Locked", locked))
1893a840879dSEd Tanous         {
1894a840879dSEd Tanous             return;
1895a840879dSEd Tanous         }
1896e9cc5172SEd Tanous     }
1897e9cc5172SEd Tanous     else
1898900f9497SJoseph Reynolds     {
1899e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
1900e9cc5172SEd Tanous         if (username != req.session->username)
1901900f9497SJoseph Reynolds         {
1902900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1903900f9497SJoseph Reynolds             return;
1904900f9497SJoseph Reynolds         }
1905031514fbSJunLin Chen 
1906e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
19071ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
19081ef4c342SEd Tanous                                       password))
1909e9cc5172SEd Tanous         {
1910e9cc5172SEd Tanous             return;
1911e9cc5172SEd Tanous         }
1912900f9497SJoseph Reynolds     }
1913900f9497SJoseph Reynolds 
191466b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
19156c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
19166c51eab1SEd Tanous     // updating user properties other then username. If username
19176c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
19186c51eab1SEd Tanous     // user rename request.
191966b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
1920a840879dSEd Tanous     {
19211ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
19221ef4c342SEd Tanous                              locked);
192384e12cb7SAppaRao Puli         return;
192484e12cb7SAppaRao Puli     }
192584e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
19266c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
19271ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
192859d494eeSPatrick Williams          locked](const boost::system::error_code ec, sdbusplus::message_t& m) {
192984e12cb7SAppaRao Puli         if (ec)
193084e12cb7SAppaRao Puli         {
1931002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
1932002d39b4SEd Tanous                                     username);
1933a840879dSEd Tanous             return;
1934a840879dSEd Tanous         }
1935a840879dSEd Tanous 
1936002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
1937002d39b4SEd Tanous                              locked);
193884e12cb7SAppaRao Puli         },
19391ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
194084e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
194184e12cb7SAppaRao Puli         *newUserName);
19421ef4c342SEd Tanous }
19431ef4c342SEd Tanous 
19441ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
19451ef4c342SEd Tanous {
19461ef4c342SEd Tanous 
19471ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
19484c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
19494c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
19504c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
19514c7d4d33SEd Tanous 
19524c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
19531ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
19541ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
19551ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
19561ef4c342SEd Tanous 
19571ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
19581ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
19591ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
19601ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
19611ef4c342SEd Tanous 
19621ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
19634c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
19644c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
19654c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
19664c7d4d33SEd Tanous 
19674c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
19681ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
19691ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
19701ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
19711ef4c342SEd Tanous 
19721ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
19731ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
19741ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
19751ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
19761ef4c342SEd Tanous 
19771ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
19784c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
19794c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
19804c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
19814c7d4d33SEd Tanous 
19824c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
19831ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
19841ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
19851ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
19861ef4c342SEd Tanous 
19871ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
19881ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
19891ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
19901ef4c342SEd Tanous         // yet
19911ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
19921ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
19931ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
199484e12cb7SAppaRao Puli 
19956c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
1996ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
19976c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
19981ef4c342SEd Tanous             std::bind_front(handleAccounttDelete, std::ref(app)));
199906e086d9SEd Tanous }
200088d16c9aSLewanczyk, Dawid 
200188d16c9aSLewanczyk, Dawid } // namespace redfish
2002