xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 0ec8b83db7e8629c721c0e632de702bf1018f58f)
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 
18*0ec8b83dSEd Tanous #include "generated/enums/account_service.hpp"
19*0ec8b83dSEd Tanous #include "registries/privilege_registry.hpp"
20*0ec8b83dSEd Tanous 
217e860f15SJohn Edward Broadbent #include <app.hpp>
2224c8542dSRatan Gupta #include <dbus_utility.hpp>
2365b0dc32SEd Tanous #include <error_messages.hpp>
24b9b2e0b2SEd Tanous #include <openbmc_dbus_rest.hpp>
2552cc112dSEd Tanous #include <persistent_data.hpp>
2645ca1b86SEd Tanous #include <query.hpp>
271e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
28d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
29d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
30a840879dSEd Tanous #include <utils/json_utils.hpp>
311214b7e7SGunnar Mills 
32c7229815SAbhishek Patel #include <optional>
33c7229815SAbhishek Patel #include <string>
34c7229815SAbhishek Patel #include <vector>
35c7229815SAbhishek Patel 
361abe55efSEd Tanous namespace redfish
371abe55efSEd Tanous {
3888d16c9aSLewanczyk, Dawid 
3923a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
406973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
412c70f800SEd Tanous constexpr const char* adConfigObject =
42ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
43ab828d7cSRatan Gupta 
44b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
456973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
466973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
476973a582SRatan Gupta constexpr const char* ldapConfigInterface =
486973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
496973a582SRatan Gupta constexpr const char* ldapCreateInterface =
506973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
516973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
5206785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
5306785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
546973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
556973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
566973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
576973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
586973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
596973a582SRatan Gupta 
6054fc587aSNagaraju Goruganti struct LDAPRoleMapData
6154fc587aSNagaraju Goruganti {
6254fc587aSNagaraju Goruganti     std::string groupName;
6354fc587aSNagaraju Goruganti     std::string privilege;
6454fc587aSNagaraju Goruganti };
6554fc587aSNagaraju Goruganti 
666973a582SRatan Gupta struct LDAPConfigData
676973a582SRatan Gupta {
686973a582SRatan Gupta     std::string uri{};
696973a582SRatan Gupta     std::string bindDN{};
706973a582SRatan Gupta     std::string baseDN{};
716973a582SRatan Gupta     std::string searchScope{};
726973a582SRatan Gupta     std::string serverType{};
736973a582SRatan Gupta     bool serviceEnabled = false;
746973a582SRatan Gupta     std::string userNameAttribute{};
756973a582SRatan Gupta     std::string groupAttribute{};
7654fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
776973a582SRatan Gupta };
786973a582SRatan Gupta 
7954fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
8084e12cb7SAppaRao Puli {
8184e12cb7SAppaRao Puli     if (role == "priv-admin")
8284e12cb7SAppaRao Puli     {
8384e12cb7SAppaRao Puli         return "Administrator";
8484e12cb7SAppaRao Puli     }
853174e4dfSEd Tanous     if (role == "priv-user")
8684e12cb7SAppaRao Puli     {
87c80fee55SAppaRao Puli         return "ReadOnly";
8884e12cb7SAppaRao Puli     }
893174e4dfSEd Tanous     if (role == "priv-operator")
9084e12cb7SAppaRao Puli     {
9184e12cb7SAppaRao Puli         return "Operator";
9284e12cb7SAppaRao Puli     }
9384e12cb7SAppaRao Puli     return "";
9484e12cb7SAppaRao Puli }
9554fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9684e12cb7SAppaRao Puli {
9784e12cb7SAppaRao Puli     if (role == "Administrator")
9884e12cb7SAppaRao Puli     {
9984e12cb7SAppaRao Puli         return "priv-admin";
10084e12cb7SAppaRao Puli     }
1013174e4dfSEd Tanous     if (role == "ReadOnly")
10284e12cb7SAppaRao Puli     {
10384e12cb7SAppaRao Puli         return "priv-user";
10484e12cb7SAppaRao Puli     }
1053174e4dfSEd Tanous     if (role == "Operator")
10684e12cb7SAppaRao Puli     {
10784e12cb7SAppaRao Puli         return "priv-operator";
10884e12cb7SAppaRao Puli     }
10984e12cb7SAppaRao Puli     return "";
11084e12cb7SAppaRao Puli }
111b9b2e0b2SEd Tanous 
112c7229815SAbhishek Patel /**
113c7229815SAbhishek Patel  * @brief Maps user group names retrieved from D-Bus object to
114c7229815SAbhishek Patel  * Account Types.
115c7229815SAbhishek Patel  *
116c7229815SAbhishek Patel  * @param[in] userGroups List of User groups
117c7229815SAbhishek Patel  * @param[out] res AccountTypes populated
118c7229815SAbhishek Patel  *
119c7229815SAbhishek Patel  * @return true in case of success, false if UserGroups contains
120c7229815SAbhishek Patel  * invalid group name(s).
121c7229815SAbhishek Patel  */
122c7229815SAbhishek Patel inline bool translateUserGroup(const std::vector<std::string>& userGroups,
123c7229815SAbhishek Patel                                crow::Response& res)
124c7229815SAbhishek Patel {
125c7229815SAbhishek Patel     std::vector<std::string> accountTypes;
126c7229815SAbhishek Patel     for (const auto& userGroup : userGroups)
127c7229815SAbhishek Patel     {
128c7229815SAbhishek Patel         if (userGroup == "redfish")
129c7229815SAbhishek Patel         {
130c7229815SAbhishek Patel             accountTypes.emplace_back("Redfish");
131c7229815SAbhishek Patel             accountTypes.emplace_back("WebUI");
132c7229815SAbhishek Patel         }
133c7229815SAbhishek Patel         else if (userGroup == "ipmi")
134c7229815SAbhishek Patel         {
135c7229815SAbhishek Patel             accountTypes.emplace_back("IPMI");
136c7229815SAbhishek Patel         }
137c7229815SAbhishek Patel         else if (userGroup == "ssh")
138c7229815SAbhishek Patel         {
139c7229815SAbhishek Patel             accountTypes.emplace_back("HostConsole");
140c7229815SAbhishek Patel             accountTypes.emplace_back("ManagerConsole");
141c7229815SAbhishek Patel         }
142c7229815SAbhishek Patel         else if (userGroup == "web")
143c7229815SAbhishek Patel         {
144c7229815SAbhishek Patel             // 'web' is one of the valid groups in the UserGroups property of
145c7229815SAbhishek Patel             // the user account in the D-Bus object. This group is currently not
146c7229815SAbhishek Patel             // doing anything, and is considered to be equivalent to 'redfish'.
147c7229815SAbhishek Patel             // 'redfish' user group is mapped to 'Redfish'and 'WebUI'
148c7229815SAbhishek Patel             // AccountTypes, so do nothing here...
149c7229815SAbhishek Patel         }
150c7229815SAbhishek Patel         else
151c7229815SAbhishek Patel         {
152c7229815SAbhishek Patel             // Invalid user group name. Caller throws an excption.
153c7229815SAbhishek Patel             return false;
154c7229815SAbhishek Patel         }
155c7229815SAbhishek Patel     }
156c7229815SAbhishek Patel 
157c7229815SAbhishek Patel     res.jsonValue["AccountTypes"] = std::move(accountTypes);
158c7229815SAbhishek Patel     return true;
159c7229815SAbhishek Patel }
160c7229815SAbhishek Patel 
1618d1b46d7Szhanghch05 inline void userErrorMessageHandler(
1628d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1638d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
16466b5ca76Sjayaprakash Mutyala {
16566b5ca76Sjayaprakash Mutyala     if (e == nullptr)
16666b5ca76Sjayaprakash Mutyala     {
16766b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
16866b5ca76Sjayaprakash Mutyala         return;
16966b5ca76Sjayaprakash Mutyala     }
17066b5ca76Sjayaprakash Mutyala 
171055806b3SManojkiran Eda     const char* errorMessage = e->name;
17266b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
17366b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
17466b5ca76Sjayaprakash Mutyala     {
175d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
17666b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
17766b5ca76Sjayaprakash Mutyala     }
17866b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
17966b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
18066b5ca76Sjayaprakash Mutyala     {
181d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
18266b5ca76Sjayaprakash Mutyala     }
183d4d25793SEd Tanous     else if ((strcmp(errorMessage,
184d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
185d4d25793SEd Tanous               0) ||
1860fda0f12SGeorge Liu              (strcmp(
1870fda0f12SGeorge Liu                   errorMessage,
1880fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
1890fda0f12SGeorge Liu               0))
19066b5ca76Sjayaprakash Mutyala     {
19166b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
19266b5ca76Sjayaprakash Mutyala     }
19366b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
19466b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
19566b5ca76Sjayaprakash Mutyala     {
19666b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
19766b5ca76Sjayaprakash Mutyala     }
19866b5ca76Sjayaprakash Mutyala     else
19966b5ca76Sjayaprakash Mutyala     {
20066b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
20166b5ca76Sjayaprakash Mutyala     }
20266b5ca76Sjayaprakash Mutyala }
20366b5ca76Sjayaprakash Mutyala 
20481ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
205ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
206ab828d7cSRatan Gupta                                 const std::string& ldapType)
2076973a582SRatan Gupta {
208ab828d7cSRatan Gupta     std::string service =
209ab828d7cSRatan Gupta         (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService";
21054fc587aSNagaraju Goruganti 
2111476687dSEd Tanous     nlohmann::json& ldap = jsonResponse[ldapType];
21254fc587aSNagaraju Goruganti 
2131476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
2141476687dSEd Tanous     ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri});
215*0ec8b83dSEd Tanous     ldap["Authentication"]["AuthenticationType"] =
216*0ec8b83dSEd Tanous         account_service::AuthenticationTypes::UsernameAndPassword;
2171476687dSEd Tanous     ldap["Authentication"]["Username"] = confData.bindDN;
2181476687dSEd Tanous     ldap["Authentication"]["Password"] = nullptr;
2191476687dSEd Tanous 
2201476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] =
2211476687dSEd Tanous         nlohmann::json::array({confData.baseDN});
2221476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] =
2231476687dSEd Tanous         confData.userNameAttribute;
2241476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] =
2251476687dSEd Tanous         confData.groupAttribute;
2261476687dSEd Tanous 
2271476687dSEd Tanous     nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"];
22854fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
2299eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
23054fc587aSNagaraju Goruganti     {
23154fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
23254fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
233613dabeaSEd Tanous 
234613dabeaSEd Tanous         nlohmann::json::array_t remoteGroupArray;
235613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
236613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
237613dabeaSEd Tanous         remoteGroupArray.emplace_back(std::move(remoteGroup));
238613dabeaSEd Tanous         roleMapArray.emplace_back(std::move(remoteGroupArray));
239613dabeaSEd Tanous 
240613dabeaSEd Tanous         nlohmann::json::array_t localRoleArray;
241613dabeaSEd Tanous         nlohmann::json::object_t localRole;
242613dabeaSEd Tanous         localRole["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
243613dabeaSEd Tanous         localRoleArray.emplace_back(std::move(localRole));
244613dabeaSEd Tanous         roleMapArray.emplace_back(std::move(localRoleArray));
24554fc587aSNagaraju Goruganti     }
2466973a582SRatan Gupta }
2476973a582SRatan Gupta 
2486973a582SRatan Gupta /**
24906785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
25006785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
25106785244SRatan Gupta  *
25206785244SRatan Gupta  */
25323a21a1cSEd Tanous inline void handleRoleMapPatch(
2548d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25506785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
256f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
25706785244SRatan Gupta {
25806785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
25906785244SRatan Gupta     {
260f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
26106785244SRatan Gupta 
26206785244SRatan Gupta         if (thisJson.is_null())
26306785244SRatan Gupta         {
26406785244SRatan Gupta             // delete the existing object
26506785244SRatan Gupta             if (index < roleMapObjData.size())
26606785244SRatan Gupta             {
26706785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
26806785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
26906785244SRatan Gupta                      index](const boost::system::error_code ec) {
27006785244SRatan Gupta                     if (ec)
27106785244SRatan Gupta                     {
27206785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
27306785244SRatan Gupta                         messages::internalError(asyncResp->res);
27406785244SRatan Gupta                         return;
27506785244SRatan Gupta                     }
27606785244SRatan Gupta                     asyncResp->res
27706785244SRatan Gupta                         .jsonValue[serverType]["RemoteRoleMapping"][index] =
27806785244SRatan Gupta                         nullptr;
27906785244SRatan Gupta                     },
28006785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
28106785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
28206785244SRatan Gupta             }
28306785244SRatan Gupta             else
28406785244SRatan Gupta             {
28506785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
28606785244SRatan Gupta                 messages::propertyValueTypeError(
28771f52d96SEd Tanous                     asyncResp->res,
28871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
28971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
29006785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
29106785244SRatan Gupta                 return;
29206785244SRatan Gupta             }
29306785244SRatan Gupta         }
29406785244SRatan Gupta         else if (thisJson.empty())
29506785244SRatan Gupta         {
29606785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
29706785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
29806785244SRatan Gupta         }
29906785244SRatan Gupta         else
30006785244SRatan Gupta         {
30106785244SRatan Gupta             // update/create the object
30206785244SRatan Gupta             std::optional<std::string> remoteGroup;
30306785244SRatan Gupta             std::optional<std::string> localRole;
30406785244SRatan Gupta 
305f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
306f23b7296SEd Tanous             // readJson is structured
307f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
308f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
309f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
310f23b7296SEd Tanous                                      localRole))
31106785244SRatan Gupta             {
31206785244SRatan Gupta                 continue;
31306785244SRatan Gupta             }
31406785244SRatan Gupta 
31506785244SRatan Gupta             // Update existing RoleMapping Object
31606785244SRatan Gupta             if (index < roleMapObjData.size())
31706785244SRatan Gupta             {
31806785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
31906785244SRatan Gupta                 // If "RemoteGroup" info is provided
32006785244SRatan Gupta                 if (remoteGroup)
32106785244SRatan Gupta                 {
32206785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
32306785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
32406785244SRatan Gupta                          remoteGroup](const boost::system::error_code ec) {
32506785244SRatan Gupta                         if (ec)
32606785244SRatan Gupta                         {
327002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
32806785244SRatan Gupta                             messages::internalError(asyncResp->res);
32906785244SRatan Gupta                             return;
33006785244SRatan Gupta                         }
33106785244SRatan Gupta                         asyncResp->res
332002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
333002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
33406785244SRatan Gupta                         },
33506785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
33606785244SRatan Gupta                         propertyInterface, "Set",
33706785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
33806785244SRatan Gupta                         "GroupName",
339168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
340168e20c1SEd Tanous                             std::move(*remoteGroup)));
34106785244SRatan Gupta                 }
34206785244SRatan Gupta 
34306785244SRatan Gupta                 // If "LocalRole" info is provided
34406785244SRatan Gupta                 if (localRole)
34506785244SRatan Gupta                 {
34606785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
34706785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
34806785244SRatan Gupta                          localRole](const boost::system::error_code ec) {
34906785244SRatan Gupta                         if (ec)
35006785244SRatan Gupta                         {
351002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
35206785244SRatan Gupta                             messages::internalError(asyncResp->res);
35306785244SRatan Gupta                             return;
35406785244SRatan Gupta                         }
35506785244SRatan Gupta                         asyncResp->res
356002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
357002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
35806785244SRatan Gupta                         },
35906785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
36006785244SRatan Gupta                         propertyInterface, "Set",
36106785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
36206785244SRatan Gupta                         "Privilege",
363168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
36406785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
36506785244SRatan Gupta                 }
36606785244SRatan Gupta             }
36706785244SRatan Gupta             // Create a new RoleMapping Object.
36806785244SRatan Gupta             else
36906785244SRatan Gupta             {
37006785244SRatan Gupta                 BMCWEB_LOG_DEBUG
37106785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
37206785244SRatan Gupta                 std::string pathString =
37306785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index);
37406785244SRatan Gupta 
37506785244SRatan Gupta                 if (!localRole)
37606785244SRatan Gupta                 {
37706785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
37806785244SRatan Gupta                                               pathString + "/LocalRole");
37906785244SRatan Gupta                     continue;
38006785244SRatan Gupta                 }
38106785244SRatan Gupta                 if (!remoteGroup)
38206785244SRatan Gupta                 {
38306785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
38406785244SRatan Gupta                                               pathString + "/RemoteGroup");
38506785244SRatan Gupta                     continue;
38606785244SRatan Gupta                 }
38706785244SRatan Gupta 
38806785244SRatan Gupta                 std::string dbusObjectPath;
38906785244SRatan Gupta                 if (serverType == "ActiveDirectory")
39006785244SRatan Gupta                 {
3912c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
39206785244SRatan Gupta                 }
39306785244SRatan Gupta                 else if (serverType == "LDAP")
39406785244SRatan Gupta                 {
39523a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
39606785244SRatan Gupta                 }
39706785244SRatan Gupta 
39806785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
39906785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
40006785244SRatan Gupta 
40106785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
402271584abSEd Tanous                     [asyncResp, serverType, localRole,
40306785244SRatan Gupta                      remoteGroup](const boost::system::error_code ec) {
40406785244SRatan Gupta                     if (ec)
40506785244SRatan Gupta                     {
40606785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
40706785244SRatan Gupta                         messages::internalError(asyncResp->res);
40806785244SRatan Gupta                         return;
40906785244SRatan Gupta                     }
41006785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
41106785244SRatan Gupta                         asyncResp->res
41206785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
4131476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
4141476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
4151476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
4161476687dSEd Tanous                     remoteRoleJson.push_back(std::move(roleMapEntry));
41706785244SRatan Gupta                     },
41806785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
4193174e4dfSEd Tanous                     "Create", *remoteGroup,
42006785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
42106785244SRatan Gupta             }
42206785244SRatan Gupta         }
42306785244SRatan Gupta     }
42406785244SRatan Gupta }
42506785244SRatan Gupta 
42606785244SRatan Gupta /**
4276973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
4286973a582SRatan Gupta  * into JSON
4296973a582SRatan Gupta  */
4306973a582SRatan Gupta template <typename CallbackFunc>
4316973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
4326973a582SRatan Gupta                               CallbackFunc&& callback)
4336973a582SRatan Gupta {
43454fc587aSNagaraju Goruganti 
43554fc587aSNagaraju Goruganti     const std::array<const char*, 2> interfaces = {ldapEnableInterface,
43654fc587aSNagaraju Goruganti                                                    ldapConfigInterface};
43754fc587aSNagaraju Goruganti 
43854fc587aSNagaraju Goruganti     crow::connections::systemBus->async_method_call(
43954fc587aSNagaraju Goruganti         [callback, ldapType](const boost::system::error_code ec,
440b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
44154fc587aSNagaraju Goruganti         if (ec || resp.empty())
44254fc587aSNagaraju Goruganti         {
4430fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
444002d39b4SEd Tanous                 << "DBUS response error during getting of service name: " << ec;
44523a21a1cSEd Tanous             LDAPConfigData empty{};
44623a21a1cSEd Tanous             callback(false, empty, ldapType);
44754fc587aSNagaraju Goruganti             return;
44854fc587aSNagaraju Goruganti         }
44954fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
45054fc587aSNagaraju Goruganti         crow::connections::systemBus->async_method_call(
451002d39b4SEd Tanous             [callback,
452002d39b4SEd Tanous              ldapType](const boost::system::error_code errorCode,
453711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
4546973a582SRatan Gupta             LDAPConfigData confData{};
45581ce609eSEd Tanous             if (errorCode)
4566973a582SRatan Gupta             {
457ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
458002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode;
4596973a582SRatan Gupta                 return;
4606973a582SRatan Gupta             }
461ab828d7cSRatan Gupta 
462ab828d7cSRatan Gupta             std::string ldapDbusType;
46354fc587aSNagaraju Goruganti             std::string searchString;
46454fc587aSNagaraju Goruganti 
465ab828d7cSRatan Gupta             if (ldapType == "LDAP")
466ab828d7cSRatan Gupta             {
4670fda0f12SGeorge Liu                 ldapDbusType =
4680fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
46954fc587aSNagaraju Goruganti                 searchString = "openldap";
470ab828d7cSRatan Gupta             }
471ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
472ab828d7cSRatan Gupta             {
47354fc587aSNagaraju Goruganti                 ldapDbusType =
4740fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
47554fc587aSNagaraju Goruganti                 searchString = "active_directory";
476ab828d7cSRatan Gupta             }
477ab828d7cSRatan Gupta             else
478ab828d7cSRatan Gupta             {
479002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type="
480ab828d7cSRatan Gupta                                  << ldapType;
481ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
482ab828d7cSRatan Gupta                 return;
483ab828d7cSRatan Gupta             }
484ab828d7cSRatan Gupta 
485ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
486ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
487ab828d7cSRatan Gupta 
4886973a582SRatan Gupta             for (const auto& object : ldapObjects)
4896973a582SRatan Gupta             {
49054fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
49154fc587aSNagaraju Goruganti                 // given type
492002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
4936973a582SRatan Gupta                 {
494ab828d7cSRatan Gupta                     continue;
495ab828d7cSRatan Gupta                 }
496ab828d7cSRatan Gupta 
4976973a582SRatan Gupta                 for (const auto& interface : object.second)
4986973a582SRatan Gupta                 {
4996973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
5006973a582SRatan Gupta                     {
5016973a582SRatan Gupta                         // rest of the properties are string.
5026973a582SRatan Gupta                         for (const auto& property : interface.second)
5036973a582SRatan Gupta                         {
5046973a582SRatan Gupta                             if (property.first == "Enabled")
5056973a582SRatan Gupta                             {
5066973a582SRatan Gupta                                 const bool* value =
5076973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
5086973a582SRatan Gupta                                 if (value == nullptr)
5096973a582SRatan Gupta                                 {
5106973a582SRatan Gupta                                     continue;
5116973a582SRatan Gupta                                 }
5126973a582SRatan Gupta                                 confData.serviceEnabled = *value;
5136973a582SRatan Gupta                                 break;
5146973a582SRatan Gupta                             }
5156973a582SRatan Gupta                         }
5166973a582SRatan Gupta                     }
5176973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
5186973a582SRatan Gupta                     {
5196973a582SRatan Gupta 
5206973a582SRatan Gupta                         for (const auto& property : interface.second)
5216973a582SRatan Gupta                         {
522271584abSEd Tanous                             const std::string* strValue =
523002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
524271584abSEd Tanous                             if (strValue == nullptr)
5256973a582SRatan Gupta                             {
5266973a582SRatan Gupta                                 continue;
5276973a582SRatan Gupta                             }
5286973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
5296973a582SRatan Gupta                             {
530271584abSEd Tanous                                 confData.uri = *strValue;
5316973a582SRatan Gupta                             }
5326973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
5336973a582SRatan Gupta                             {
534271584abSEd Tanous                                 confData.bindDN = *strValue;
5356973a582SRatan Gupta                             }
5366973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
5376973a582SRatan Gupta                             {
538271584abSEd Tanous                                 confData.baseDN = *strValue;
5396973a582SRatan Gupta                             }
540002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
5416973a582SRatan Gupta                             {
542271584abSEd Tanous                                 confData.searchScope = *strValue;
5436973a582SRatan Gupta                             }
544002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
5456973a582SRatan Gupta                             {
546271584abSEd Tanous                                 confData.groupAttribute = *strValue;
5476973a582SRatan Gupta                             }
548002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
5496973a582SRatan Gupta                             {
550271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
5516973a582SRatan Gupta                             }
55254fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
553ab828d7cSRatan Gupta                             {
554271584abSEd Tanous                                 confData.serverType = *strValue;
55554fc587aSNagaraju Goruganti                             }
55654fc587aSNagaraju Goruganti                         }
55754fc587aSNagaraju Goruganti                     }
558002d39b4SEd Tanous                     else if (interface.first ==
5590fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
56054fc587aSNagaraju Goruganti                     {
56154fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
56254fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
56354fc587aSNagaraju Goruganti                         {
564271584abSEd Tanous                             const std::string* strValue =
565002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
56654fc587aSNagaraju Goruganti 
567271584abSEd Tanous                             if (strValue == nullptr)
56854fc587aSNagaraju Goruganti                             {
56954fc587aSNagaraju Goruganti                                 continue;
57054fc587aSNagaraju Goruganti                             }
57154fc587aSNagaraju Goruganti 
57254fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
57354fc587aSNagaraju Goruganti                             {
574271584abSEd Tanous                                 roleMapData.groupName = *strValue;
57554fc587aSNagaraju Goruganti                             }
57654fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
57754fc587aSNagaraju Goruganti                             {
578271584abSEd Tanous                                 roleMapData.privilege = *strValue;
57954fc587aSNagaraju Goruganti                             }
58054fc587aSNagaraju Goruganti                         }
58154fc587aSNagaraju Goruganti 
582002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
583002d39b4SEd Tanous                                                             roleMapData);
58454fc587aSNagaraju Goruganti                     }
58554fc587aSNagaraju Goruganti                 }
58654fc587aSNagaraju Goruganti             }
587ab828d7cSRatan Gupta             callback(true, confData, ldapType);
58854fc587aSNagaraju Goruganti             },
589002d39b4SEd Tanous             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
59054fc587aSNagaraju Goruganti         },
59154fc587aSNagaraju Goruganti         mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
59223a21a1cSEd Tanous         ldapConfigObjectName, interfaces);
5936973a582SRatan Gupta }
5946973a582SRatan Gupta 
5958a07d286SRatan Gupta /**
5968a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
5978a07d286SRatan Gupta  * @param input JSON data
5988a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5998a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
6008a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
6018a07d286SRatan Gupta  */
6024f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
6036c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6046c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
6058a07d286SRatan Gupta {
6068a07d286SRatan Gupta     std::optional<std::string> authType;
6078a07d286SRatan Gupta 
6088a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
6098a07d286SRatan Gupta                              authType, "Username", username, "Password",
6108a07d286SRatan Gupta                              password))
6118a07d286SRatan Gupta     {
6128a07d286SRatan Gupta         return;
6138a07d286SRatan Gupta     }
6148a07d286SRatan Gupta     if (!authType)
6158a07d286SRatan Gupta     {
6168a07d286SRatan Gupta         return;
6178a07d286SRatan Gupta     }
6188a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
6198a07d286SRatan Gupta     {
6208a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
6218a07d286SRatan Gupta                                          "AuthenticationType");
6228a07d286SRatan Gupta         return;
6238a07d286SRatan Gupta     }
6248a07d286SRatan Gupta }
6258a07d286SRatan Gupta /**
6268a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
6278a07d286SRatan Gupta  * @param input JSON data
6288a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6298a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
6308a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
6318a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
6328a07d286SRatan Gupta  */
6338a07d286SRatan Gupta 
6344f48d5f6SEd Tanous inline void
6354f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
6368d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6378a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
6388a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
6398a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
6408a07d286SRatan Gupta {
6418a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
6428a07d286SRatan Gupta 
6438a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
6448a07d286SRatan Gupta                              searchSettings))
6458a07d286SRatan Gupta     {
6468a07d286SRatan Gupta         return;
6478a07d286SRatan Gupta     }
6488a07d286SRatan Gupta     if (!searchSettings)
6498a07d286SRatan Gupta     {
6508a07d286SRatan Gupta         return;
6518a07d286SRatan Gupta     }
6528a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
6538a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
6548a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
6558a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
6568a07d286SRatan Gupta     {
6578a07d286SRatan Gupta         return;
6588a07d286SRatan Gupta     }
6598a07d286SRatan Gupta }
6608a07d286SRatan Gupta /**
6618a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6628a07d286SRatan Gupta           json response with the new value.
6638a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6648a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6658a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6668a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6678a07d286SRatan Gupta  */
6688a07d286SRatan Gupta 
6694f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6708a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6718d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6728a07d286SRatan Gupta     const std::string& ldapServerElementName,
6738a07d286SRatan Gupta     const std::string& ldapConfigObject)
6748a07d286SRatan Gupta {
6758a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6768a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
6778a07d286SRatan Gupta          serviceAddressList](const boost::system::error_code ec) {
6788a07d286SRatan Gupta         if (ec)
6798a07d286SRatan Gupta         {
6808a07d286SRatan Gupta             BMCWEB_LOG_DEBUG
681c61704abSGunnar Mills                 << "Error Occurred in updating the service address";
6828a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6838a07d286SRatan Gupta             return;
6848a07d286SRatan Gupta         }
6858a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
6868a07d286SRatan Gupta             serviceAddressList.front()};
687002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
6888a07d286SRatan Gupta             modifiedserviceAddressList;
6898a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
6908a07d286SRatan Gupta         {
691002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
6928a07d286SRatan Gupta                                             serviceAddressList.front());
6938a07d286SRatan Gupta         }
6948a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
6958a07d286SRatan Gupta         },
6968a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6978a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
698168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
6998a07d286SRatan Gupta }
7008a07d286SRatan Gupta /**
7018a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
7028a07d286SRatan Gupta           json response with the new value.
7038a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
7048a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7058a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7068a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7078a07d286SRatan Gupta  */
7088a07d286SRatan Gupta 
7094f48d5f6SEd Tanous inline void
7104f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
7118d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7128a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7138a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7148a07d286SRatan Gupta {
7158a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7168a07d286SRatan Gupta         [asyncResp, username,
7178a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7188a07d286SRatan Gupta         if (ec)
7198a07d286SRatan Gupta         {
7206c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
7218a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7228a07d286SRatan Gupta             return;
7238a07d286SRatan Gupta         }
724002d39b4SEd Tanous         asyncResp->res
725002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Username"] =
726002d39b4SEd Tanous             username;
7278a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
7288a07d286SRatan Gupta         },
7298a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
730168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
731168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
7328a07d286SRatan Gupta }
7338a07d286SRatan Gupta 
7348a07d286SRatan Gupta /**
7358a07d286SRatan Gupta  * @brief updates the LDAP password
7368a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
7378a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7388a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7398a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
7408a07d286SRatan Gupta  */
7418a07d286SRatan Gupta 
7424f48d5f6SEd Tanous inline void
7434f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
7448d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7458a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7468a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7478a07d286SRatan Gupta {
7488a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7498a07d286SRatan Gupta         [asyncResp, password,
7508a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7518a07d286SRatan Gupta         if (ec)
7528a07d286SRatan Gupta         {
7536c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
7548a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7558a07d286SRatan Gupta             return;
7568a07d286SRatan Gupta         }
757002d39b4SEd Tanous         asyncResp->res
758002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Password"] =
759002d39b4SEd Tanous             "";
7608a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
7618a07d286SRatan Gupta         },
7628a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7638a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
764168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
7658a07d286SRatan Gupta }
7668a07d286SRatan Gupta 
7678a07d286SRatan Gupta /**
7688a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7698a07d286SRatan Gupta           json response with the new value.
7708a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7718a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7728a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7738a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7748a07d286SRatan Gupta  */
7758a07d286SRatan Gupta 
7764f48d5f6SEd Tanous inline void
7774f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7788d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7798a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7808a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7818a07d286SRatan Gupta {
7828a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7838a07d286SRatan Gupta         [asyncResp, baseDNList,
7848a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7858a07d286SRatan Gupta         if (ec)
7868a07d286SRatan Gupta         {
7876c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
7888a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7898a07d286SRatan Gupta             return;
7908a07d286SRatan Gupta         }
791002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7928a07d286SRatan Gupta         auto& searchSettingsJson =
7938a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7946c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
7956c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
7968a07d286SRatan Gupta         if (baseDNList.size() > 1)
7978a07d286SRatan Gupta         {
798002d39b4SEd Tanous             messages::propertyValueModified(
799002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
8008a07d286SRatan Gupta         }
8018a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
8028a07d286SRatan Gupta         },
8038a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8048a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
805168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
8068a07d286SRatan Gupta }
8078a07d286SRatan Gupta /**
8088a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
8098a07d286SRatan Gupta           json response with the new value.
8108a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
8118a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8128a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8138a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8148a07d286SRatan Gupta  */
8158a07d286SRatan Gupta 
8164f48d5f6SEd Tanous inline void
8174f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
8188d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8198a07d286SRatan Gupta                             const std::string& ldapServerElementName,
8208a07d286SRatan Gupta                             const std::string& ldapConfigObject)
8218a07d286SRatan Gupta {
8228a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8238a07d286SRatan Gupta         [asyncResp, userNameAttribute,
8248a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8258a07d286SRatan Gupta         if (ec)
8268a07d286SRatan Gupta         {
827c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8288a07d286SRatan Gupta                                 "username attribute";
8298a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8308a07d286SRatan Gupta             return;
8318a07d286SRatan Gupta         }
832002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8338a07d286SRatan Gupta         auto& searchSettingsJson =
8348a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8358a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
8368a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
8378a07d286SRatan Gupta         },
8388a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8398a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
840168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
8418a07d286SRatan Gupta }
8428a07d286SRatan Gupta /**
8438a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
8448a07d286SRatan Gupta           json response with the new value.
8458a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
8468a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8478a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8488a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8498a07d286SRatan Gupta  */
8508a07d286SRatan Gupta 
8514f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
8528d1b46d7Szhanghch05     const std::string& groupsAttribute,
8538d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8548a07d286SRatan Gupta     const std::string& ldapServerElementName,
8558a07d286SRatan Gupta     const std::string& ldapConfigObject)
8568a07d286SRatan Gupta {
8578a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8588a07d286SRatan Gupta         [asyncResp, groupsAttribute,
8598a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8608a07d286SRatan Gupta         if (ec)
8618a07d286SRatan Gupta         {
862c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8638a07d286SRatan Gupta                                 "groupname attribute";
8648a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8658a07d286SRatan Gupta             return;
8668a07d286SRatan Gupta         }
867002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8688a07d286SRatan Gupta         auto& searchSettingsJson =
8698a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8708a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8718a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8728a07d286SRatan Gupta         },
8738a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8748a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
875168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
8768a07d286SRatan Gupta }
8778a07d286SRatan Gupta /**
8788a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
8798a07d286SRatan Gupta           json response with the new value.
8808a07d286SRatan Gupta  * @param input JSON data.
8818a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8828a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8838a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8848a07d286SRatan Gupta  */
8858a07d286SRatan Gupta 
8864f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8876c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8888a07d286SRatan Gupta     const std::string& ldapServerElementName,
8898a07d286SRatan Gupta     const std::string& ldapConfigObject)
8908a07d286SRatan Gupta {
8918a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8928a07d286SRatan Gupta         [asyncResp, serviceEnabled,
8938a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8948a07d286SRatan Gupta         if (ec)
8958a07d286SRatan Gupta         {
896002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
8978a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8988a07d286SRatan Gupta             return;
8998a07d286SRatan Gupta         }
9006c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
9018a07d286SRatan Gupta             serviceEnabled;
9026c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
9038a07d286SRatan Gupta         },
9048a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
905168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
906168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
9078a07d286SRatan Gupta }
9088a07d286SRatan Gupta 
9094f48d5f6SEd Tanous inline void
9104f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
9118d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
91278158631SZbigniew Kurzynski {
91378158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
91478158631SZbigniew Kurzynski     std::optional<bool> cookie;
91578158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
91678158631SZbigniew Kurzynski     std::optional<bool> xToken;
917501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
91878158631SZbigniew Kurzynski 
91978158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
92078158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
921501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
92278158631SZbigniew Kurzynski     {
92378158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
92478158631SZbigniew Kurzynski         return;
92578158631SZbigniew Kurzynski     }
92678158631SZbigniew Kurzynski 
92778158631SZbigniew Kurzynski     // Make a copy of methods configuration
92852cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
92952cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
93078158631SZbigniew Kurzynski 
93178158631SZbigniew Kurzynski     if (basicAuth)
93278158631SZbigniew Kurzynski     {
933f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
934f16f6263SAlan Kuo         messages::actionNotSupported(
9350fda0f12SGeorge Liu             asyncResp->res,
9360fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
937f16f6263SAlan Kuo         return;
938f16f6263SAlan Kuo #endif
93978158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
94078158631SZbigniew Kurzynski     }
94178158631SZbigniew Kurzynski 
94278158631SZbigniew Kurzynski     if (cookie)
94378158631SZbigniew Kurzynski     {
944f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
9450fda0f12SGeorge Liu         messages::actionNotSupported(
9460fda0f12SGeorge Liu             asyncResp->res,
9470fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
948f16f6263SAlan Kuo         return;
949f16f6263SAlan Kuo #endif
95078158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
95178158631SZbigniew Kurzynski     }
95278158631SZbigniew Kurzynski 
95378158631SZbigniew Kurzynski     if (sessionToken)
95478158631SZbigniew Kurzynski     {
955f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
956f16f6263SAlan Kuo         messages::actionNotSupported(
9570fda0f12SGeorge Liu             asyncResp->res,
9580fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
959f16f6263SAlan Kuo         return;
960f16f6263SAlan Kuo #endif
96178158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
96278158631SZbigniew Kurzynski     }
96378158631SZbigniew Kurzynski 
96478158631SZbigniew Kurzynski     if (xToken)
96578158631SZbigniew Kurzynski     {
966f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
9670fda0f12SGeorge Liu         messages::actionNotSupported(
9680fda0f12SGeorge Liu             asyncResp->res,
9690fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
970f16f6263SAlan Kuo         return;
971f16f6263SAlan Kuo #endif
97278158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
97378158631SZbigniew Kurzynski     }
97478158631SZbigniew Kurzynski 
975501f1e58SZbigniew Kurzynski     if (tls)
976501f1e58SZbigniew Kurzynski     {
977f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
9780fda0f12SGeorge Liu         messages::actionNotSupported(
9790fda0f12SGeorge Liu             asyncResp->res,
9800fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
981f16f6263SAlan Kuo         return;
982f16f6263SAlan Kuo #endif
983501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
984501f1e58SZbigniew Kurzynski     }
985501f1e58SZbigniew Kurzynski 
98678158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
987501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
988501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
98978158631SZbigniew Kurzynski     {
99078158631SZbigniew Kurzynski         // Do not allow user to disable everything
99178158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
99278158631SZbigniew Kurzynski                                      "of disabling all available methods");
99378158631SZbigniew Kurzynski         return;
99478158631SZbigniew Kurzynski     }
99578158631SZbigniew Kurzynski 
99652cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
99752cc112dSEd Tanous         authMethodsConfig);
99878158631SZbigniew Kurzynski     // Save configuration immediately
99952cc112dSEd Tanous     persistent_data::getConfig().writeData();
100078158631SZbigniew Kurzynski 
100178158631SZbigniew Kurzynski     messages::success(asyncResp->res);
100278158631SZbigniew Kurzynski }
100378158631SZbigniew Kurzynski 
10048a07d286SRatan Gupta /**
10058a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
10068a07d286SRatan Gupta  *        value and create the LDAP config object.
10078a07d286SRatan Gupta  * @param input JSON data
10088a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
10098a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
10108a07d286SRatan Gupta  */
10118a07d286SRatan Gupta 
10126c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
10138d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10148a07d286SRatan Gupta                             const std::string& serverType)
10158a07d286SRatan Gupta {
1016eb2bbe56SRatan Gupta     std::string dbusObjectPath;
1017eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
1018eb2bbe56SRatan Gupta     {
10192c70f800SEd Tanous         dbusObjectPath = adConfigObject;
1020eb2bbe56SRatan Gupta     }
1021eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
1022eb2bbe56SRatan Gupta     {
102323a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
1024eb2bbe56SRatan Gupta     }
1025cb13a392SEd Tanous     else
1026cb13a392SEd Tanous     {
1027cb13a392SEd Tanous         return;
1028cb13a392SEd Tanous     }
1029eb2bbe56SRatan Gupta 
10308a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
10318a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
10328a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
10338a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
10348a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
10358a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
10368a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
10378a07d286SRatan Gupta     std::optional<std::string> userName;
10388a07d286SRatan Gupta     std::optional<std::string> password;
103906785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
10408a07d286SRatan Gupta 
10418a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
10428a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
10438a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
104406785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
104506785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
10468a07d286SRatan Gupta     {
10478a07d286SRatan Gupta         return;
10488a07d286SRatan Gupta     }
10498a07d286SRatan Gupta 
10508a07d286SRatan Gupta     if (authentication)
10518a07d286SRatan Gupta     {
10528a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
10538a07d286SRatan Gupta                                     password);
10548a07d286SRatan Gupta     }
10558a07d286SRatan Gupta     if (ldapService)
10568a07d286SRatan Gupta     {
10578a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10588a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
10598a07d286SRatan Gupta     }
10608a07d286SRatan Gupta     if (serviceAddressList)
10618a07d286SRatan Gupta     {
106226f6976fSEd Tanous         if (serviceAddressList->empty())
10638a07d286SRatan Gupta         {
10648a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10658a07d286SRatan Gupta                                              "ServiceAddress");
10668a07d286SRatan Gupta             return;
10678a07d286SRatan Gupta         }
10688a07d286SRatan Gupta     }
10698a07d286SRatan Gupta     if (baseDNList)
10708a07d286SRatan Gupta     {
107126f6976fSEd Tanous         if (baseDNList->empty())
10728a07d286SRatan Gupta         {
10738a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10748a07d286SRatan Gupta                                              "BaseDistinguishedNames");
10758a07d286SRatan Gupta             return;
10768a07d286SRatan Gupta         }
10778a07d286SRatan Gupta     }
10788a07d286SRatan Gupta 
10798a07d286SRatan Gupta     // nothing to update, then return
10808a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
108106785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
108206785244SRatan Gupta         !remoteRoleMapData)
10838a07d286SRatan Gupta     {
10848a07d286SRatan Gupta         return;
10858a07d286SRatan Gupta     }
10868a07d286SRatan Gupta 
10878a07d286SRatan Gupta     // Get the existing resource first then keep modifying
10888a07d286SRatan Gupta     // whenever any property gets updated.
1089002d39b4SEd Tanous     getLDAPConfigData(
1090002d39b4SEd Tanous         serverType,
1091002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1092002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1093002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
109423a21a1cSEd Tanous                             const std::string& serverT) {
10958a07d286SRatan Gupta         if (!success)
10968a07d286SRatan Gupta         {
10978a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10988a07d286SRatan Gupta             return;
10998a07d286SRatan Gupta         }
11006c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
11018a07d286SRatan Gupta         if (confData.serviceEnabled)
11028a07d286SRatan Gupta         {
11038a07d286SRatan Gupta             // Disable the service first and update the rest of
11048a07d286SRatan Gupta             // the properties.
11056c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
11068a07d286SRatan Gupta         }
11078a07d286SRatan Gupta 
11088a07d286SRatan Gupta         if (serviceAddressList)
11098a07d286SRatan Gupta         {
11106c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
11116c51eab1SEd Tanous                                       dbusObjectPath);
11128a07d286SRatan Gupta         }
11138a07d286SRatan Gupta         if (userName)
11148a07d286SRatan Gupta         {
11156c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
11168a07d286SRatan Gupta         }
11178a07d286SRatan Gupta         if (password)
11188a07d286SRatan Gupta         {
11196c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
11208a07d286SRatan Gupta         }
11218a07d286SRatan Gupta 
11228a07d286SRatan Gupta         if (baseDNList)
11238a07d286SRatan Gupta         {
11246c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
11258a07d286SRatan Gupta         }
11268a07d286SRatan Gupta         if (userNameAttribute)
11278a07d286SRatan Gupta         {
11286c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
11296c51eab1SEd Tanous                                     dbusObjectPath);
11308a07d286SRatan Gupta         }
11318a07d286SRatan Gupta         if (groupsAttribute)
11328a07d286SRatan Gupta         {
11336c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
11346c51eab1SEd Tanous                                      dbusObjectPath);
11358a07d286SRatan Gupta         }
11368a07d286SRatan Gupta         if (serviceEnabled)
11378a07d286SRatan Gupta         {
11388a07d286SRatan Gupta             // if user has given the value as true then enable
11398a07d286SRatan Gupta             // the service. if user has given false then no-op
11408a07d286SRatan Gupta             // as service is already stopped.
11418a07d286SRatan Gupta             if (*serviceEnabled)
11428a07d286SRatan Gupta             {
11436c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
11446c51eab1SEd Tanous                                          dbusObjectPath);
11458a07d286SRatan Gupta             }
11468a07d286SRatan Gupta         }
11478a07d286SRatan Gupta         else
11488a07d286SRatan Gupta         {
11498a07d286SRatan Gupta             // if user has not given the service enabled value
11508a07d286SRatan Gupta             // then revert it to the same state as it was
11518a07d286SRatan Gupta             // before.
11528a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
115323a21a1cSEd Tanous                                      serverT, dbusObjectPath);
11548a07d286SRatan Gupta         }
115506785244SRatan Gupta 
115606785244SRatan Gupta         if (remoteRoleMapData)
115706785244SRatan Gupta         {
11586c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
11596c51eab1SEd Tanous                                *remoteRoleMapData);
116006785244SRatan Gupta         }
11618a07d286SRatan Gupta         });
11628a07d286SRatan Gupta }
1163d4b5443fSEd Tanous 
11646c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
11656c51eab1SEd Tanous                                  const std::string& username,
11666c51eab1SEd Tanous                                  std::optional<std::string> password,
11676c51eab1SEd Tanous                                  std::optional<bool> enabled,
11686c51eab1SEd Tanous                                  std::optional<std::string> roleId,
11696c51eab1SEd Tanous                                  std::optional<bool> locked)
11701abe55efSEd Tanous {
1171b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1172b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1173b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
11746c51eab1SEd Tanous 
11756c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
11766c51eab1SEd Tanous         dbusObjectPath,
11771106333aSEd Tanous         [dbusObjectPath, username, password(std::move(password)),
11781106333aSEd Tanous          roleId(std::move(roleId)), enabled, locked,
11791106333aSEd Tanous          asyncResp{std::move(asyncResp)}](int rc) {
1180e662eae8SEd Tanous         if (rc <= 0)
11816c51eab1SEd Tanous         {
1182d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11836c51eab1SEd Tanous                                        username);
11846c51eab1SEd Tanous             return;
11856c51eab1SEd Tanous         }
11866c51eab1SEd Tanous 
11876c51eab1SEd Tanous         if (password)
11886c51eab1SEd Tanous         {
11896c51eab1SEd Tanous             int retval = pamUpdatePassword(username, *password);
11906c51eab1SEd Tanous 
11916c51eab1SEd Tanous             if (retval == PAM_USER_UNKNOWN)
11926c51eab1SEd Tanous             {
1193d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11946c51eab1SEd Tanous                                            username);
11956c51eab1SEd Tanous             }
11966c51eab1SEd Tanous             else if (retval == PAM_AUTHTOK_ERR)
11976c51eab1SEd Tanous             {
11986c51eab1SEd Tanous                 // If password is invalid
1199002d39b4SEd Tanous                 messages::propertyValueFormatError(asyncResp->res, *password,
1200002d39b4SEd Tanous                                                    "Password");
12016c51eab1SEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
12026c51eab1SEd Tanous             }
12036c51eab1SEd Tanous             else if (retval != PAM_SUCCESS)
12046c51eab1SEd Tanous             {
12056c51eab1SEd Tanous                 messages::internalError(asyncResp->res);
12066c51eab1SEd Tanous                 return;
12076c51eab1SEd Tanous             }
1208e7b1b62bSEd Tanous             else
1209e7b1b62bSEd Tanous             {
1210e7b1b62bSEd Tanous                 messages::success(asyncResp->res);
1211e7b1b62bSEd Tanous             }
12126c51eab1SEd Tanous         }
12136c51eab1SEd Tanous 
12146c51eab1SEd Tanous         if (enabled)
12156c51eab1SEd Tanous         {
12166c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
12176c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
12186c51eab1SEd Tanous                 if (ec)
12196c51eab1SEd Tanous                 {
12206c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12216c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12226c51eab1SEd Tanous                     return;
12236c51eab1SEd Tanous                 }
12246c51eab1SEd Tanous                 messages::success(asyncResp->res);
12256c51eab1SEd Tanous                 return;
12266c51eab1SEd Tanous                 },
1227e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12286c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12296c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserEnabled",
1230168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*enabled});
12316c51eab1SEd Tanous         }
12326c51eab1SEd Tanous 
12336c51eab1SEd Tanous         if (roleId)
12346c51eab1SEd Tanous         {
12356c51eab1SEd Tanous             std::string priv = getPrivilegeFromRoleId(*roleId);
12366c51eab1SEd Tanous             if (priv.empty())
12376c51eab1SEd Tanous             {
12386c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, *roleId,
12396c51eab1SEd Tanous                                                  "RoleId");
12406c51eab1SEd Tanous                 return;
12416c51eab1SEd Tanous             }
12426c51eab1SEd Tanous 
12436c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
12446c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
12456c51eab1SEd Tanous                 if (ec)
12466c51eab1SEd Tanous                 {
12476c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12486c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12496c51eab1SEd Tanous                     return;
12506c51eab1SEd Tanous                 }
12516c51eab1SEd Tanous                 messages::success(asyncResp->res);
12526c51eab1SEd Tanous                 },
1253e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12546c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12556c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1256168e20c1SEd Tanous                 dbus::utility::DbusVariantType{priv});
12576c51eab1SEd Tanous         }
12586c51eab1SEd Tanous 
12596c51eab1SEd Tanous         if (locked)
12606c51eab1SEd Tanous         {
12616c51eab1SEd Tanous             // admin can unlock the account which is locked by
12626c51eab1SEd Tanous             // successive authentication failures but admin should
12636c51eab1SEd Tanous             // not be allowed to lock an account.
12646c51eab1SEd Tanous             if (*locked)
12656c51eab1SEd Tanous             {
12666c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, "true",
12676c51eab1SEd Tanous                                                  "Locked");
12686c51eab1SEd Tanous                 return;
12696c51eab1SEd Tanous             }
12706c51eab1SEd Tanous 
12716c51eab1SEd Tanous             crow::connections::systemBus->async_method_call(
12726c51eab1SEd Tanous                 [asyncResp](const boost::system::error_code ec) {
12736c51eab1SEd Tanous                 if (ec)
12746c51eab1SEd Tanous                 {
12756c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12766c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12776c51eab1SEd Tanous                     return;
12786c51eab1SEd Tanous                 }
12796c51eab1SEd Tanous                 messages::success(asyncResp->res);
12806c51eab1SEd Tanous                 return;
12816c51eab1SEd Tanous                 },
1282e05aec50SEd Tanous                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
12836c51eab1SEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
12846c51eab1SEd Tanous                 "xyz.openbmc_project.User.Attributes",
1285168e20c1SEd Tanous                 "UserLockedForFailedAttempt",
1286168e20c1SEd Tanous                 dbus::utility::DbusVariantType{*locked});
12876c51eab1SEd Tanous         }
12886c51eab1SEd Tanous         });
12896c51eab1SEd Tanous }
12906c51eab1SEd Tanous 
12914c7d4d33SEd Tanous inline void handleAccountServiceHead(
12924c7d4d33SEd Tanous     App& app, const crow::Request& req,
12931ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12946c51eab1SEd Tanous {
12954c7d4d33SEd Tanous 
12963ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
129745ca1b86SEd Tanous     {
129845ca1b86SEd Tanous         return;
129945ca1b86SEd Tanous     }
13004c7d4d33SEd Tanous     asyncResp->res.addHeader(
13014c7d4d33SEd Tanous         boost::beast::http::field::link,
13024c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
13034c7d4d33SEd Tanous }
13044c7d4d33SEd Tanous 
13054c7d4d33SEd Tanous inline void
13064c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
13074c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13084c7d4d33SEd Tanous {
13094c7d4d33SEd Tanous     handleAccountServiceHead(app, req, asyncResp);
131052cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
13111ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
131278158631SZbigniew Kurzynski 
13131476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
13141476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
13151476687dSEd Tanous     json["@odata.type"] = "#AccountService."
13161476687dSEd Tanous                           "v1_10_0.AccountService";
13171476687dSEd Tanous     json["Id"] = "AccountService";
13181476687dSEd Tanous     json["Name"] = "Account Service";
13191476687dSEd Tanous     json["Description"] = "Account Service";
13201476687dSEd Tanous     json["ServiceEnabled"] = true;
13211476687dSEd Tanous     json["MaxPasswordLength"] = 20;
13221ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
13231476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
13241476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
13251476687dSEd Tanous         "#OemAccountService.v1_0_0.AccountService";
13261476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
13271476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
13281476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
13291476687dSEd Tanous         authMethodsConfig.basic;
13301476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
13311476687dSEd Tanous         authMethodsConfig.sessionToken;
13321ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
13331ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
13341ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
13351476687dSEd Tanous 
13361ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
13371ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
13381ef4c342SEd Tanous     // permissions ConfigureManager
133972048780SAbhishek Patel     Privileges effectiveUserPrivileges =
134072048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
134172048780SAbhishek Patel 
134272048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
134372048780SAbhishek Patel                                          effectiveUserPrivileges))
134472048780SAbhishek Patel     {
13451ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
13461476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
134772048780SAbhishek Patel     }
1348d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1349d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1350d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
1351002d39b4SEd Tanous         [asyncResp](const boost::system::error_code ec,
13521ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
13533d958bbcSAppaRao Puli         if (ec)
13543d958bbcSAppaRao Puli         {
13553d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
13563d958bbcSAppaRao Puli             return;
13573d958bbcSAppaRao Puli         }
1358d1bde9e5SKrzysztof Grobelny 
13593d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
13603d958bbcSAppaRao Puli                          << "properties for AccountService";
1361d1bde9e5SKrzysztof Grobelny 
1362d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1363d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1364d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1365d1bde9e5SKrzysztof Grobelny 
1366d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1367d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1368d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1369d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1370d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1371d1bde9e5SKrzysztof Grobelny 
1372d1bde9e5SKrzysztof Grobelny         if (!success)
13733d958bbcSAppaRao Puli         {
1374d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1375d1bde9e5SKrzysztof Grobelny             return;
13763d958bbcSAppaRao Puli         }
1377d1bde9e5SKrzysztof Grobelny 
1378d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
13793d958bbcSAppaRao Puli         {
1380d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
13813d958bbcSAppaRao Puli         }
1382d1bde9e5SKrzysztof Grobelny 
1383d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
13843d958bbcSAppaRao Puli         {
1385d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1386d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1387d1bde9e5SKrzysztof Grobelny         }
1388d1bde9e5SKrzysztof Grobelny 
1389d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
13903d958bbcSAppaRao Puli         {
1391002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1392d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
13933d958bbcSAppaRao Puli         }
1394d1bde9e5SKrzysztof Grobelny         });
13956973a582SRatan Gupta 
139602cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1397ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1398cb13a392SEd Tanous         if (!success)
1399cb13a392SEd Tanous         {
1400cb13a392SEd Tanous             return;
1401cb13a392SEd Tanous         }
1402002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1403ab828d7cSRatan Gupta     };
1404ab828d7cSRatan Gupta 
1405ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1406ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
14071ef4c342SEd Tanous }
14086973a582SRatan Gupta 
14091ef4c342SEd Tanous inline void handleAccountServicePatch(
14101ef4c342SEd Tanous     App& app, const crow::Request& req,
14111ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14121ef4c342SEd Tanous {
14133ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
141445ca1b86SEd Tanous     {
141545ca1b86SEd Tanous         return;
141645ca1b86SEd Tanous     }
1417f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1418f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1419ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1420f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1421f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1422f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1423f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1424f5ffd806SEd Tanous 
142515ed6780SWilly Tu     if (!json_util::readJsonPatch(
14261ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
14271ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
14281ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
14291ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1430f5ffd806SEd Tanous             oemObject))
1431f5ffd806SEd Tanous     {
1432f5ffd806SEd Tanous         return;
1433f5ffd806SEd Tanous     }
1434f5ffd806SEd Tanous 
1435f5ffd806SEd Tanous     if (minPasswordLength)
1436f5ffd806SEd Tanous     {
1437ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
1438ef73ad0dSPaul Fertser             [asyncResp](const boost::system::error_code ec) {
1439ef73ad0dSPaul Fertser             if (ec)
1440ef73ad0dSPaul Fertser             {
1441ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1442ef73ad0dSPaul Fertser                 return;
1443ef73ad0dSPaul Fertser             }
1444ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1445ef73ad0dSPaul Fertser             },
14461ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1447ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
14481ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1449ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1450f5ffd806SEd Tanous     }
1451f5ffd806SEd Tanous 
1452f5ffd806SEd Tanous     if (maxPasswordLength)
1453f5ffd806SEd Tanous     {
14541ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1455f5ffd806SEd Tanous     }
1456f5ffd806SEd Tanous 
1457f5ffd806SEd Tanous     if (ldapObject)
1458f5ffd806SEd Tanous     {
1459f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1460f5ffd806SEd Tanous     }
1461f5ffd806SEd Tanous 
1462f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
14631ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1464f5ffd806SEd Tanous                                          oemOpenBMCObject))
1465f5ffd806SEd Tanous     {
1466f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1467f5ffd806SEd Tanous             oemOpenBMCObject &&
1468f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1469f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1470f5ffd806SEd Tanous         {
1471f5ffd806SEd Tanous             if (authMethodsObject)
1472f5ffd806SEd Tanous             {
14731ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1474f5ffd806SEd Tanous             }
1475f5ffd806SEd Tanous         }
1476f5ffd806SEd Tanous     }
1477f5ffd806SEd Tanous 
1478f5ffd806SEd Tanous     if (activeDirectoryObject)
1479f5ffd806SEd Tanous     {
14801ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1481f5ffd806SEd Tanous     }
1482f5ffd806SEd Tanous 
1483f5ffd806SEd Tanous     if (unlockTimeout)
1484f5ffd806SEd Tanous     {
1485f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1486f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1487f5ffd806SEd Tanous             if (ec)
1488f5ffd806SEd Tanous             {
1489f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1490f5ffd806SEd Tanous                 return;
1491f5ffd806SEd Tanous             }
1492f5ffd806SEd Tanous             messages::success(asyncResp->res);
1493f5ffd806SEd Tanous             },
14941ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1495f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
14961ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1497168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1498f5ffd806SEd Tanous     }
1499f5ffd806SEd Tanous     if (lockoutThreshold)
1500f5ffd806SEd Tanous     {
1501f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1502f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1503f5ffd806SEd Tanous             if (ec)
1504f5ffd806SEd Tanous             {
1505f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1506f5ffd806SEd Tanous                 return;
1507f5ffd806SEd Tanous             }
1508f5ffd806SEd Tanous             messages::success(asyncResp->res);
1509f5ffd806SEd Tanous             },
15101ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1511f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1512f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1513f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1514168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1515f5ffd806SEd Tanous     }
15161ef4c342SEd Tanous }
1517f5ffd806SEd Tanous 
15184c7d4d33SEd Tanous inline void handleAccountCollectionHead(
15191ef4c342SEd Tanous     App& app, const crow::Request& req,
15201ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15211ef4c342SEd Tanous {
15224c7d4d33SEd Tanous 
15233ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
152445ca1b86SEd Tanous     {
152545ca1b86SEd Tanous         return;
152645ca1b86SEd Tanous     }
15274c7d4d33SEd Tanous     asyncResp->res.addHeader(
15284c7d4d33SEd Tanous         boost::beast::http::field::link,
15294c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
15304c7d4d33SEd Tanous }
15314c7d4d33SEd Tanous 
15324c7d4d33SEd Tanous inline void handleAccountCollectionGet(
15334c7d4d33SEd Tanous     App& app, const crow::Request& req,
15344c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15354c7d4d33SEd Tanous {
15364c7d4d33SEd Tanous     handleAccountCollectionHead(app, req, asyncResp);
15371476687dSEd Tanous 
15381476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
15391476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
15401ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
15411476687dSEd Tanous                                               "ManagerAccountCollection";
15421476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
15431476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
15440f74e643SEd Tanous 
15456c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
15466c51eab1SEd Tanous         redfish::getUserPrivileges(req.userRole);
15476c51eab1SEd Tanous 
1548f5e29f33SJunLin Chen     std::string thisUser;
1549f5e29f33SJunLin Chen     if (req.session)
1550f5e29f33SJunLin Chen     {
1551f5e29f33SJunLin Chen         thisUser = req.session->username;
1552f5e29f33SJunLin Chen     }
1553b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
1554cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
1555cef1ddfbSEd Tanous             const boost::system::error_code ec,
1556711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1557b9b2e0b2SEd Tanous         if (ec)
1558b9b2e0b2SEd Tanous         {
1559f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1560b9b2e0b2SEd Tanous             return;
1561b9b2e0b2SEd Tanous         }
1562b9b2e0b2SEd Tanous 
1563cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1564002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1565cef1ddfbSEd Tanous 
1566cef1ddfbSEd Tanous         bool userCanSeeSelf =
1567002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1568cef1ddfbSEd Tanous 
1569002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1570b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1571b9b2e0b2SEd Tanous 
15729eb808c1SEd Tanous         for (const auto& userpath : users)
1573b9b2e0b2SEd Tanous         {
15742dfd18efSEd Tanous             std::string user = userpath.first.filename();
15752dfd18efSEd Tanous             if (user.empty())
1576b9b2e0b2SEd Tanous             {
15772dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
15782dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
15792dfd18efSEd Tanous 
15802dfd18efSEd Tanous                 return;
1581b9b2e0b2SEd Tanous             }
1582f365910cSGunnar Mills 
1583f365910cSGunnar Mills             // As clarified by Redfish here:
1584f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
15856c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
15866c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
15876c51eab1SEd Tanous             // accounts.
15881ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1589f365910cSGunnar Mills             {
15901476687dSEd Tanous                 nlohmann::json::object_t member;
15911476687dSEd Tanous                 member["@odata.id"] =
1592002d39b4SEd Tanous                     "/redfish/v1/AccountService/Accounts/" + user;
15931476687dSEd Tanous                 memberArray.push_back(std::move(member));
1594b9b2e0b2SEd Tanous             }
1595f365910cSGunnar Mills         }
15961ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1597b9b2e0b2SEd Tanous         },
15981ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1599b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16001ef4c342SEd Tanous }
16016c51eab1SEd Tanous 
16021ef4c342SEd Tanous inline void handleAccountCollectionPost(
16031ef4c342SEd Tanous     App& app, const crow::Request& req,
16041ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16051ef4c342SEd Tanous {
16063ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
160745ca1b86SEd Tanous     {
160845ca1b86SEd Tanous         return;
160945ca1b86SEd Tanous     }
16109712f8acSEd Tanous     std::string username;
16119712f8acSEd Tanous     std::string password;
1612a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1613a24526dcSEd Tanous     std::optional<bool> enabled = true;
16141ef4c342SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
16151ef4c342SEd Tanous                                   "Password", password, "RoleId", roleId,
16161ef4c342SEd Tanous                                   "Enabled", enabled))
161704ae99ecSEd Tanous     {
161804ae99ecSEd Tanous         return;
161904ae99ecSEd Tanous     }
162004ae99ecSEd Tanous 
162154fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
162284e12cb7SAppaRao Puli     if (priv.empty())
162304ae99ecSEd Tanous     {
16241ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
162504ae99ecSEd Tanous         return;
162604ae99ecSEd Tanous     }
16279712f8acSEd Tanous     roleId = priv;
162804ae99ecSEd Tanous 
1629599c71d8SAyushi Smriti     // Reading AllGroups property
16301e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
16311ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
16321ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
16331ef4c342SEd Tanous         "AllGroups",
1634599c71d8SAyushi Smriti         [asyncResp, username, password{std::move(password)}, roleId,
1635168e20c1SEd Tanous          enabled](const boost::system::error_code ec,
16361e1e598dSJonathan Doman                   const std::vector<std::string>& allGroupsList) {
1637599c71d8SAyushi Smriti         if (ec)
1638599c71d8SAyushi Smriti         {
1639599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1640599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1641599c71d8SAyushi Smriti             return;
1642599c71d8SAyushi Smriti         }
1643599c71d8SAyushi Smriti 
16441e1e598dSJonathan Doman         if (allGroupsList.empty())
1645599c71d8SAyushi Smriti         {
1646599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1647599c71d8SAyushi Smriti             return;
1648599c71d8SAyushi Smriti         }
1649599c71d8SAyushi Smriti 
165004ae99ecSEd Tanous         crow::connections::systemBus->async_method_call(
16511ef4c342SEd Tanous             [asyncResp, username, password](const boost::system::error_code ec2,
165259d494eeSPatrick Williams                                             sdbusplus::message_t& m) {
165323a21a1cSEd Tanous             if (ec2)
165404ae99ecSEd Tanous             {
16551ef4c342SEd Tanous                 userErrorMessageHandler(m.get_error(), asyncResp, username, "");
165604ae99ecSEd Tanous                 return;
165704ae99ecSEd Tanous             }
165804ae99ecSEd Tanous 
1659002d39b4SEd Tanous             if (pamUpdatePassword(username, password) != PAM_SUCCESS)
166004ae99ecSEd Tanous             {
16616c51eab1SEd Tanous                 // At this point we have a user that's been
16626c51eab1SEd Tanous                 // created, but the password set
16636c51eab1SEd Tanous                 // failed.Something is wrong, so delete the user
16646c51eab1SEd Tanous                 // that we've already created
16651ef4c342SEd Tanous                 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1666b477fd44SP Dheeraj Srujan Kumar                 tempObjPath /= username;
1667b477fd44SP Dheeraj Srujan Kumar                 const std::string userPath(tempObjPath);
1668b477fd44SP Dheeraj Srujan Kumar 
166904ae99ecSEd Tanous                 crow::connections::systemBus->async_method_call(
16701ef4c342SEd Tanous                     [asyncResp, password](const boost::system::error_code ec3) {
167123a21a1cSEd Tanous                     if (ec3)
167204ae99ecSEd Tanous                     {
1673002d39b4SEd Tanous                         messages::internalError(asyncResp->res);
167404ae99ecSEd Tanous                         return;
167504ae99ecSEd Tanous                     }
167604ae99ecSEd Tanous 
16770d4197e0Sanil kumar appana                     // If password is invalid
16781ef4c342SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, password,
16791ef4c342SEd Tanous                                                        "Password");
168004ae99ecSEd Tanous                     },
1681002d39b4SEd Tanous                     "xyz.openbmc_project.User.Manager", userPath,
1682002d39b4SEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
168304ae99ecSEd Tanous 
168404ae99ecSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
168504ae99ecSEd Tanous                 return;
168604ae99ecSEd Tanous             }
168704ae99ecSEd Tanous 
1688f12894f8SJason M. Bills             messages::created(asyncResp->res);
168904ae99ecSEd Tanous             asyncResp->res.addHeader(
16901ef4c342SEd Tanous                 "Location", "/redfish/v1/AccountService/Accounts/" + username);
169104ae99ecSEd Tanous             },
1692002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1693002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "CreateUser", username,
1694002d39b4SEd Tanous             allGroupsList, *roleId, *enabled);
16951e1e598dSJonathan Doman         });
16961ef4c342SEd Tanous }
1697b9b2e0b2SEd Tanous 
16981ef4c342SEd Tanous inline void
16994c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
17006c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17014c7d4d33SEd Tanous                       const std::string& /*accountName*/)
17021ef4c342SEd Tanous {
17034c7d4d33SEd Tanous 
17043ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
170545ca1b86SEd Tanous     {
170645ca1b86SEd Tanous         return;
170745ca1b86SEd Tanous     }
17084c7d4d33SEd Tanous     asyncResp->res.addHeader(
17094c7d4d33SEd Tanous         boost::beast::http::field::link,
17104c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
17114c7d4d33SEd Tanous }
17124c7d4d33SEd Tanous inline void
17134c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
17144c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17154c7d4d33SEd Tanous                      const std::string& accountName)
17164c7d4d33SEd Tanous {
17174c7d4d33SEd Tanous     handleAccountHead(app, req, asyncResp, accountName);
17181ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1719031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1720d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1721031514fbSJunLin Chen     return;
1722031514fbSJunLin Chen 
17231ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1724031514fbSJunLin Chen     if (req.session == nullptr)
1725031514fbSJunLin Chen     {
1726031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1727031514fbSJunLin Chen         return;
1728031514fbSJunLin Chen     }
17296c51eab1SEd Tanous     if (req.session->username != accountName)
1730b9b2e0b2SEd Tanous     {
17316c51eab1SEd Tanous         // At this point we've determined that the user is trying to
17321ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
17331ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
17341ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
17356c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
17366c51eab1SEd Tanous             redfish::getUserPrivileges(req.userRole);
17371ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
17381ef4c342SEd Tanous                                                          "ConfigureManager"};
17396c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
17406c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1741900f9497SJoseph Reynolds         {
1742900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
1743900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1744900f9497SJoseph Reynolds             return;
1745900f9497SJoseph Reynolds         }
1746900f9497SJoseph Reynolds     }
1747900f9497SJoseph Reynolds 
1748b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
17491ef4c342SEd Tanous         [asyncResp,
17501ef4c342SEd Tanous          accountName](const boost::system::error_code ec,
1751711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1752b9b2e0b2SEd Tanous         if (ec)
1753b9b2e0b2SEd Tanous         {
1754f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1755b9b2e0b2SEd Tanous             return;
1756b9b2e0b2SEd Tanous         }
1757711ac7a9SEd Tanous         const auto userIt = std::find_if(
1758b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
1759b477fd44SP Dheeraj Srujan Kumar             [accountName](
1760b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
1761002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
176255f79e6fSEd Tanous             return accountName == user.first.filename();
1763b477fd44SP Dheeraj Srujan Kumar             });
1764b9b2e0b2SEd Tanous 
176584e12cb7SAppaRao Puli         if (userIt == users.end())
1766b9b2e0b2SEd Tanous         {
1767002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1768002d39b4SEd Tanous                                        accountName);
176984e12cb7SAppaRao Puli             return;
177084e12cb7SAppaRao Puli         }
17714e68c45bSAyushi Smriti 
17721476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
17731476687dSEd Tanous             "#ManagerAccount.v1_4_0.ManagerAccount";
17741476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
17751476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
17761476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
17774e68c45bSAyushi Smriti 
177884e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
177965b0dc32SEd Tanous         {
1780002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
178165b0dc32SEd Tanous             {
178265b0dc32SEd Tanous                 for (const auto& property : interface.second)
178365b0dc32SEd Tanous                 {
178465b0dc32SEd Tanous                     if (property.first == "UserEnabled")
178565b0dc32SEd Tanous                     {
178665b0dc32SEd Tanous                         const bool* userEnabled =
1787abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
178865b0dc32SEd Tanous                         if (userEnabled == nullptr)
178965b0dc32SEd Tanous                         {
1790002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
179184e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
179284e12cb7SAppaRao Puli                             return;
179365b0dc32SEd Tanous                         }
1794002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
179565b0dc32SEd Tanous                     }
1796002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
179765b0dc32SEd Tanous                     {
179865b0dc32SEd Tanous                         const bool* userLocked =
1799abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
180065b0dc32SEd Tanous                         if (userLocked == nullptr)
180165b0dc32SEd Tanous                         {
180284e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
180384e12cb7SAppaRao Puli                                                 "ailedAttempt "
180484e12cb7SAppaRao Puli                                                 "wasn't a bool";
180584e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
180684e12cb7SAppaRao Puli                             return;
180765b0dc32SEd Tanous                         }
1808002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
1809002d39b4SEd Tanous                         asyncResp->res
1810002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
18113bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
181265b0dc32SEd Tanous                     }
181384e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
181484e12cb7SAppaRao Puli                     {
181554fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1816002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
181754fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
181884e12cb7SAppaRao Puli                         {
1819002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
182084e12cb7SAppaRao Puli                                                 "string";
182184e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
182284e12cb7SAppaRao Puli                             return;
182384e12cb7SAppaRao Puli                         }
18241ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
182554fc587aSNagaraju Goruganti                         if (role.empty())
182684e12cb7SAppaRao Puli                         {
182784e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
182884e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
182984e12cb7SAppaRao Puli                             return;
183084e12cb7SAppaRao Puli                         }
183154fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
183284e12cb7SAppaRao Puli 
18331476687dSEd Tanous                         nlohmann::json& roleEntry =
1834002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
18351476687dSEd Tanous                         roleEntry["@odata.id"] =
1836002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
183784e12cb7SAppaRao Puli                     }
1838002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
18393bf4e632SJoseph Reynolds                     {
18403bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
18413bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
18423bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
18433bf4e632SJoseph Reynolds                         {
18440fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
18450fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
18463bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
18473bf4e632SJoseph Reynolds                             return;
18483bf4e632SJoseph Reynolds                         }
1849002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
18503bf4e632SJoseph Reynolds                             *userPasswordExpired;
18513bf4e632SJoseph Reynolds                     }
1852c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
1853c7229815SAbhishek Patel                     {
1854c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
1855c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
1856c7229815SAbhishek Patel                                 &property.second);
1857c7229815SAbhishek Patel                         if (userGroups == nullptr)
1858c7229815SAbhishek Patel                         {
1859c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR
1860c7229815SAbhishek Patel                                 << "userGroups wasn't a string vector";
1861c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1862c7229815SAbhishek Patel                             return;
1863c7229815SAbhishek Patel                         }
1864c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
1865c7229815SAbhishek Patel                         {
1866c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR << "userGroups mapping failed";
1867c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1868c7229815SAbhishek Patel                             return;
1869c7229815SAbhishek Patel                         }
1870c7229815SAbhishek Patel                     }
187165b0dc32SEd Tanous                 }
187265b0dc32SEd Tanous             }
187365b0dc32SEd Tanous         }
187465b0dc32SEd Tanous 
1875b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
187684e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
1877b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
1878b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
1879b9b2e0b2SEd Tanous         },
18801ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1881b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18821ef4c342SEd Tanous }
1883a840879dSEd Tanous 
18841ef4c342SEd Tanous inline void
18851ef4c342SEd Tanous     handleAccounttDelete(App& app, const crow::Request& req,
18866c51eab1SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18871ef4c342SEd Tanous                          const std::string& username)
18881ef4c342SEd Tanous {
18893ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
189045ca1b86SEd Tanous     {
189145ca1b86SEd Tanous         return;
189245ca1b86SEd Tanous     }
18931ef4c342SEd Tanous 
18941ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1895031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1896d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1897031514fbSJunLin Chen     return;
1898031514fbSJunLin Chen 
18991ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
19001ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
19011ef4c342SEd Tanous     tempObjPath /= username;
19021ef4c342SEd Tanous     const std::string userPath(tempObjPath);
19031ef4c342SEd Tanous 
19041ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
19051ef4c342SEd Tanous         [asyncResp, username](const boost::system::error_code ec) {
19061ef4c342SEd Tanous         if (ec)
19071ef4c342SEd Tanous         {
1908d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
19091ef4c342SEd Tanous                                        username);
19101ef4c342SEd Tanous             return;
19111ef4c342SEd Tanous         }
19121ef4c342SEd Tanous 
19131ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
19141ef4c342SEd Tanous         },
19151ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
19161ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
19171ef4c342SEd Tanous }
19181ef4c342SEd Tanous 
19191ef4c342SEd Tanous inline void
19201ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
19211ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19221ef4c342SEd Tanous                        const std::string& username)
19231ef4c342SEd Tanous {
19241ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19251ef4c342SEd Tanous     {
19261ef4c342SEd Tanous         return;
19271ef4c342SEd Tanous     }
19281ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
19291ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
1930d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
19311ef4c342SEd Tanous     return;
19321ef4c342SEd Tanous 
19331ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1934a24526dcSEd Tanous     std::optional<std::string> newUserName;
1935a24526dcSEd Tanous     std::optional<std::string> password;
1936a24526dcSEd Tanous     std::optional<bool> enabled;
1937a24526dcSEd Tanous     std::optional<std::string> roleId;
193824c8542dSRatan Gupta     std::optional<bool> locked;
1939e9cc5172SEd Tanous 
1940031514fbSJunLin Chen     if (req.session == nullptr)
1941031514fbSJunLin Chen     {
1942031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1943031514fbSJunLin Chen         return;
1944031514fbSJunLin Chen     }
1945031514fbSJunLin Chen 
1946e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
1947e9cc5172SEd Tanous         redfish::getUserPrivileges(req.userRole);
1948e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
1949e9cc5172SEd Tanous     bool userHasConfigureUsers =
1950e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
1951e9cc5172SEd Tanous     if (userHasConfigureUsers)
1952e9cc5172SEd Tanous     {
1953e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
19541ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "UserName",
19551ef4c342SEd Tanous                                       newUserName, "Password", password,
19561ef4c342SEd Tanous                                       "RoleId", roleId, "Enabled", enabled,
19571ef4c342SEd Tanous                                       "Locked", locked))
1958a840879dSEd Tanous         {
1959a840879dSEd Tanous             return;
1960a840879dSEd Tanous         }
1961e9cc5172SEd Tanous     }
1962e9cc5172SEd Tanous     else
1963900f9497SJoseph Reynolds     {
1964e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
1965e9cc5172SEd Tanous         if (username != req.session->username)
1966900f9497SJoseph Reynolds         {
1967900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1968900f9497SJoseph Reynolds             return;
1969900f9497SJoseph Reynolds         }
1970031514fbSJunLin Chen 
1971e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
19721ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
19731ef4c342SEd Tanous                                       password))
1974e9cc5172SEd Tanous         {
1975e9cc5172SEd Tanous             return;
1976e9cc5172SEd Tanous         }
1977900f9497SJoseph Reynolds     }
1978900f9497SJoseph Reynolds 
197966b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
19806c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
19816c51eab1SEd Tanous     // updating user properties other then username. If username
19826c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
19836c51eab1SEd Tanous     // user rename request.
198466b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
1985a840879dSEd Tanous     {
19861ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
19871ef4c342SEd Tanous                              locked);
198884e12cb7SAppaRao Puli         return;
198984e12cb7SAppaRao Puli     }
199084e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
19916c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
19921ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
199359d494eeSPatrick Williams          locked](const boost::system::error_code ec, sdbusplus::message_t& m) {
199484e12cb7SAppaRao Puli         if (ec)
199584e12cb7SAppaRao Puli         {
1996002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
1997002d39b4SEd Tanous                                     username);
1998a840879dSEd Tanous             return;
1999a840879dSEd Tanous         }
2000a840879dSEd Tanous 
2001002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
2002002d39b4SEd Tanous                              locked);
200384e12cb7SAppaRao Puli         },
20041ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
200584e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
200684e12cb7SAppaRao Puli         *newUserName);
20071ef4c342SEd Tanous }
20081ef4c342SEd Tanous 
20091ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
20101ef4c342SEd Tanous {
20111ef4c342SEd Tanous 
20121ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20134c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
20144c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20154c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
20164c7d4d33SEd Tanous 
20174c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20181ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
20191ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20201ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
20211ef4c342SEd Tanous 
20221ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20231ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
20241ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20251ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
20261ef4c342SEd Tanous 
20271ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20284c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
20294c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20304c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
20314c7d4d33SEd Tanous 
20324c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20331ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
20341ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20351ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
20361ef4c342SEd Tanous 
20371ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20381ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
20391ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
20401ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
20411ef4c342SEd Tanous 
20421ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20434c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
20444c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20454c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
20464c7d4d33SEd Tanous 
20474c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20481ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
20491ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20501ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
20511ef4c342SEd Tanous 
20521ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20531ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
20541ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
20551ef4c342SEd Tanous         // yet
20561ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
20571ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20581ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
205984e12cb7SAppaRao Puli 
20606c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2061ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
20626c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
20631ef4c342SEd Tanous             std::bind_front(handleAccounttDelete, std::ref(app)));
206406e086d9SEd Tanous }
206588d16c9aSLewanczyk, Dawid 
206688d16c9aSLewanczyk, Dawid } // namespace redfish
2067