xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 329f0348f10ee9a0b51ae7b416862fcbea05f19f)
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 
180ec8b83dSEd Tanous #include "generated/enums/account_service.hpp"
190ec8b83dSEd Tanous #include "registries/privilege_registry.hpp"
200ec8b83dSEd 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});
2150ec8b83dSEd Tanous     ldap["Authentication"]["AuthenticationType"] =
2160ec8b83dSEd 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::object_t remoteGroup;
235613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
236*329f0348SJorge Cisneros         remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
237*329f0348SJorge Cisneros         roleMapArray.emplace_back(std::move(remoteGroup));
23854fc587aSNagaraju Goruganti     }
2396973a582SRatan Gupta }
2406973a582SRatan Gupta 
2416973a582SRatan Gupta /**
24206785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
24306785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
24406785244SRatan Gupta  *
24506785244SRatan Gupta  */
24623a21a1cSEd Tanous inline void handleRoleMapPatch(
2478d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24806785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
249f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
25006785244SRatan Gupta {
25106785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
25206785244SRatan Gupta     {
253f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
25406785244SRatan Gupta 
25506785244SRatan Gupta         if (thisJson.is_null())
25606785244SRatan Gupta         {
25706785244SRatan Gupta             // delete the existing object
25806785244SRatan Gupta             if (index < roleMapObjData.size())
25906785244SRatan Gupta             {
26006785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
26106785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
26206785244SRatan Gupta                      index](const boost::system::error_code ec) {
26306785244SRatan Gupta                     if (ec)
26406785244SRatan Gupta                     {
26506785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
26606785244SRatan Gupta                         messages::internalError(asyncResp->res);
26706785244SRatan Gupta                         return;
26806785244SRatan Gupta                     }
26906785244SRatan Gupta                     asyncResp->res
27006785244SRatan Gupta                         .jsonValue[serverType]["RemoteRoleMapping"][index] =
27106785244SRatan Gupta                         nullptr;
27206785244SRatan Gupta                     },
27306785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
27406785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
27506785244SRatan Gupta             }
27606785244SRatan Gupta             else
27706785244SRatan Gupta             {
27806785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
27906785244SRatan Gupta                 messages::propertyValueTypeError(
28071f52d96SEd Tanous                     asyncResp->res,
28171f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
28271f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
28306785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
28406785244SRatan Gupta                 return;
28506785244SRatan Gupta             }
28606785244SRatan Gupta         }
28706785244SRatan Gupta         else if (thisJson.empty())
28806785244SRatan Gupta         {
28906785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
29006785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
29106785244SRatan Gupta         }
29206785244SRatan Gupta         else
29306785244SRatan Gupta         {
29406785244SRatan Gupta             // update/create the object
29506785244SRatan Gupta             std::optional<std::string> remoteGroup;
29606785244SRatan Gupta             std::optional<std::string> localRole;
29706785244SRatan Gupta 
298f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
299f23b7296SEd Tanous             // readJson is structured
300f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
301f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
302f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
303f23b7296SEd Tanous                                      localRole))
30406785244SRatan Gupta             {
30506785244SRatan Gupta                 continue;
30606785244SRatan Gupta             }
30706785244SRatan Gupta 
30806785244SRatan Gupta             // Update existing RoleMapping Object
30906785244SRatan Gupta             if (index < roleMapObjData.size())
31006785244SRatan Gupta             {
31106785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
31206785244SRatan Gupta                 // If "RemoteGroup" info is provided
31306785244SRatan Gupta                 if (remoteGroup)
31406785244SRatan Gupta                 {
31506785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
31606785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
31706785244SRatan Gupta                          remoteGroup](const boost::system::error_code ec) {
31806785244SRatan Gupta                         if (ec)
31906785244SRatan Gupta                         {
320002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
32106785244SRatan Gupta                             messages::internalError(asyncResp->res);
32206785244SRatan Gupta                             return;
32306785244SRatan Gupta                         }
32406785244SRatan Gupta                         asyncResp->res
325002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
326002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
32706785244SRatan Gupta                         },
32806785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
32906785244SRatan Gupta                         propertyInterface, "Set",
33006785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
33106785244SRatan Gupta                         "GroupName",
332168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
333168e20c1SEd Tanous                             std::move(*remoteGroup)));
33406785244SRatan Gupta                 }
33506785244SRatan Gupta 
33606785244SRatan Gupta                 // If "LocalRole" info is provided
33706785244SRatan Gupta                 if (localRole)
33806785244SRatan Gupta                 {
33906785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
34006785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
34106785244SRatan Gupta                          localRole](const boost::system::error_code ec) {
34206785244SRatan Gupta                         if (ec)
34306785244SRatan Gupta                         {
344002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
34506785244SRatan Gupta                             messages::internalError(asyncResp->res);
34606785244SRatan Gupta                             return;
34706785244SRatan Gupta                         }
34806785244SRatan Gupta                         asyncResp->res
349002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
350002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
35106785244SRatan Gupta                         },
35206785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
35306785244SRatan Gupta                         propertyInterface, "Set",
35406785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
35506785244SRatan Gupta                         "Privilege",
356168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
35706785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
35806785244SRatan Gupta                 }
35906785244SRatan Gupta             }
36006785244SRatan Gupta             // Create a new RoleMapping Object.
36106785244SRatan Gupta             else
36206785244SRatan Gupta             {
36306785244SRatan Gupta                 BMCWEB_LOG_DEBUG
36406785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
36506785244SRatan Gupta                 std::string pathString =
36606785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index);
36706785244SRatan Gupta 
36806785244SRatan Gupta                 if (!localRole)
36906785244SRatan Gupta                 {
37006785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
37106785244SRatan Gupta                                               pathString + "/LocalRole");
37206785244SRatan Gupta                     continue;
37306785244SRatan Gupta                 }
37406785244SRatan Gupta                 if (!remoteGroup)
37506785244SRatan Gupta                 {
37606785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
37706785244SRatan Gupta                                               pathString + "/RemoteGroup");
37806785244SRatan Gupta                     continue;
37906785244SRatan Gupta                 }
38006785244SRatan Gupta 
38106785244SRatan Gupta                 std::string dbusObjectPath;
38206785244SRatan Gupta                 if (serverType == "ActiveDirectory")
38306785244SRatan Gupta                 {
3842c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
38506785244SRatan Gupta                 }
38606785244SRatan Gupta                 else if (serverType == "LDAP")
38706785244SRatan Gupta                 {
38823a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
38906785244SRatan Gupta                 }
39006785244SRatan Gupta 
39106785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
39206785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
39306785244SRatan Gupta 
39406785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
395271584abSEd Tanous                     [asyncResp, serverType, localRole,
39606785244SRatan Gupta                      remoteGroup](const boost::system::error_code ec) {
39706785244SRatan Gupta                     if (ec)
39806785244SRatan Gupta                     {
39906785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
40006785244SRatan Gupta                         messages::internalError(asyncResp->res);
40106785244SRatan Gupta                         return;
40206785244SRatan Gupta                     }
40306785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
40406785244SRatan Gupta                         asyncResp->res
40506785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
4061476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
4071476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
4081476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
4091476687dSEd Tanous                     remoteRoleJson.push_back(std::move(roleMapEntry));
41006785244SRatan Gupta                     },
41106785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
4123174e4dfSEd Tanous                     "Create", *remoteGroup,
41306785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
41406785244SRatan Gupta             }
41506785244SRatan Gupta         }
41606785244SRatan Gupta     }
41706785244SRatan Gupta }
41806785244SRatan Gupta 
41906785244SRatan Gupta /**
4206973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
4216973a582SRatan Gupta  * into JSON
4226973a582SRatan Gupta  */
4236973a582SRatan Gupta template <typename CallbackFunc>
4246973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
4256973a582SRatan Gupta                               CallbackFunc&& callback)
4266973a582SRatan Gupta {
42754fc587aSNagaraju Goruganti 
42854fc587aSNagaraju Goruganti     const std::array<const char*, 2> interfaces = {ldapEnableInterface,
42954fc587aSNagaraju Goruganti                                                    ldapConfigInterface};
43054fc587aSNagaraju Goruganti 
43154fc587aSNagaraju Goruganti     crow::connections::systemBus->async_method_call(
43254fc587aSNagaraju Goruganti         [callback, ldapType](const boost::system::error_code ec,
433b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
43454fc587aSNagaraju Goruganti         if (ec || resp.empty())
43554fc587aSNagaraju Goruganti         {
4360fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
437002d39b4SEd Tanous                 << "DBUS response error during getting of service name: " << ec;
43823a21a1cSEd Tanous             LDAPConfigData empty{};
43923a21a1cSEd Tanous             callback(false, empty, ldapType);
44054fc587aSNagaraju Goruganti             return;
44154fc587aSNagaraju Goruganti         }
44254fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
44354fc587aSNagaraju Goruganti         crow::connections::systemBus->async_method_call(
444002d39b4SEd Tanous             [callback,
445002d39b4SEd Tanous              ldapType](const boost::system::error_code errorCode,
446711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
4476973a582SRatan Gupta             LDAPConfigData confData{};
44881ce609eSEd Tanous             if (errorCode)
4496973a582SRatan Gupta             {
450ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
451002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode;
4526973a582SRatan Gupta                 return;
4536973a582SRatan Gupta             }
454ab828d7cSRatan Gupta 
455ab828d7cSRatan Gupta             std::string ldapDbusType;
45654fc587aSNagaraju Goruganti             std::string searchString;
45754fc587aSNagaraju Goruganti 
458ab828d7cSRatan Gupta             if (ldapType == "LDAP")
459ab828d7cSRatan Gupta             {
4600fda0f12SGeorge Liu                 ldapDbusType =
4610fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
46254fc587aSNagaraju Goruganti                 searchString = "openldap";
463ab828d7cSRatan Gupta             }
464ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
465ab828d7cSRatan Gupta             {
46654fc587aSNagaraju Goruganti                 ldapDbusType =
4670fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
46854fc587aSNagaraju Goruganti                 searchString = "active_directory";
469ab828d7cSRatan Gupta             }
470ab828d7cSRatan Gupta             else
471ab828d7cSRatan Gupta             {
472002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type="
473ab828d7cSRatan Gupta                                  << ldapType;
474ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
475ab828d7cSRatan Gupta                 return;
476ab828d7cSRatan Gupta             }
477ab828d7cSRatan Gupta 
478ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
479ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
480ab828d7cSRatan Gupta 
4816973a582SRatan Gupta             for (const auto& object : ldapObjects)
4826973a582SRatan Gupta             {
48354fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
48454fc587aSNagaraju Goruganti                 // given type
485002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
4866973a582SRatan Gupta                 {
487ab828d7cSRatan Gupta                     continue;
488ab828d7cSRatan Gupta                 }
489ab828d7cSRatan Gupta 
4906973a582SRatan Gupta                 for (const auto& interface : object.second)
4916973a582SRatan Gupta                 {
4926973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
4936973a582SRatan Gupta                     {
4946973a582SRatan Gupta                         // rest of the properties are string.
4956973a582SRatan Gupta                         for (const auto& property : interface.second)
4966973a582SRatan Gupta                         {
4976973a582SRatan Gupta                             if (property.first == "Enabled")
4986973a582SRatan Gupta                             {
4996973a582SRatan Gupta                                 const bool* value =
5006973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
5016973a582SRatan Gupta                                 if (value == nullptr)
5026973a582SRatan Gupta                                 {
5036973a582SRatan Gupta                                     continue;
5046973a582SRatan Gupta                                 }
5056973a582SRatan Gupta                                 confData.serviceEnabled = *value;
5066973a582SRatan Gupta                                 break;
5076973a582SRatan Gupta                             }
5086973a582SRatan Gupta                         }
5096973a582SRatan Gupta                     }
5106973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
5116973a582SRatan Gupta                     {
5126973a582SRatan Gupta 
5136973a582SRatan Gupta                         for (const auto& property : interface.second)
5146973a582SRatan Gupta                         {
515271584abSEd Tanous                             const std::string* strValue =
516002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
517271584abSEd Tanous                             if (strValue == nullptr)
5186973a582SRatan Gupta                             {
5196973a582SRatan Gupta                                 continue;
5206973a582SRatan Gupta                             }
5216973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
5226973a582SRatan Gupta                             {
523271584abSEd Tanous                                 confData.uri = *strValue;
5246973a582SRatan Gupta                             }
5256973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
5266973a582SRatan Gupta                             {
527271584abSEd Tanous                                 confData.bindDN = *strValue;
5286973a582SRatan Gupta                             }
5296973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
5306973a582SRatan Gupta                             {
531271584abSEd Tanous                                 confData.baseDN = *strValue;
5326973a582SRatan Gupta                             }
533002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
5346973a582SRatan Gupta                             {
535271584abSEd Tanous                                 confData.searchScope = *strValue;
5366973a582SRatan Gupta                             }
537002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
5386973a582SRatan Gupta                             {
539271584abSEd Tanous                                 confData.groupAttribute = *strValue;
5406973a582SRatan Gupta                             }
541002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
5426973a582SRatan Gupta                             {
543271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
5446973a582SRatan Gupta                             }
54554fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
546ab828d7cSRatan Gupta                             {
547271584abSEd Tanous                                 confData.serverType = *strValue;
54854fc587aSNagaraju Goruganti                             }
54954fc587aSNagaraju Goruganti                         }
55054fc587aSNagaraju Goruganti                     }
551002d39b4SEd Tanous                     else if (interface.first ==
5520fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
55354fc587aSNagaraju Goruganti                     {
55454fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
55554fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
55654fc587aSNagaraju Goruganti                         {
557271584abSEd Tanous                             const std::string* strValue =
558002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
55954fc587aSNagaraju Goruganti 
560271584abSEd Tanous                             if (strValue == nullptr)
56154fc587aSNagaraju Goruganti                             {
56254fc587aSNagaraju Goruganti                                 continue;
56354fc587aSNagaraju Goruganti                             }
56454fc587aSNagaraju Goruganti 
56554fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
56654fc587aSNagaraju Goruganti                             {
567271584abSEd Tanous                                 roleMapData.groupName = *strValue;
56854fc587aSNagaraju Goruganti                             }
56954fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
57054fc587aSNagaraju Goruganti                             {
571271584abSEd Tanous                                 roleMapData.privilege = *strValue;
57254fc587aSNagaraju Goruganti                             }
57354fc587aSNagaraju Goruganti                         }
57454fc587aSNagaraju Goruganti 
575002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
576002d39b4SEd Tanous                                                             roleMapData);
57754fc587aSNagaraju Goruganti                     }
57854fc587aSNagaraju Goruganti                 }
57954fc587aSNagaraju Goruganti             }
580ab828d7cSRatan Gupta             callback(true, confData, ldapType);
58154fc587aSNagaraju Goruganti             },
582002d39b4SEd Tanous             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
58354fc587aSNagaraju Goruganti         },
58454fc587aSNagaraju Goruganti         mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
58523a21a1cSEd Tanous         ldapConfigObjectName, interfaces);
5866973a582SRatan Gupta }
5876973a582SRatan Gupta 
5888a07d286SRatan Gupta /**
5898a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
5908a07d286SRatan Gupta  * @param input JSON data
5918a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5928a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
5938a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
5948a07d286SRatan Gupta  */
5954f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
5966c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5976c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
5988a07d286SRatan Gupta {
5998a07d286SRatan Gupta     std::optional<std::string> authType;
6008a07d286SRatan Gupta 
6018a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
6028a07d286SRatan Gupta                              authType, "Username", username, "Password",
6038a07d286SRatan Gupta                              password))
6048a07d286SRatan Gupta     {
6058a07d286SRatan Gupta         return;
6068a07d286SRatan Gupta     }
6078a07d286SRatan Gupta     if (!authType)
6088a07d286SRatan Gupta     {
6098a07d286SRatan Gupta         return;
6108a07d286SRatan Gupta     }
6118a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
6128a07d286SRatan Gupta     {
6138a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
6148a07d286SRatan Gupta                                          "AuthenticationType");
6158a07d286SRatan Gupta         return;
6168a07d286SRatan Gupta     }
6178a07d286SRatan Gupta }
6188a07d286SRatan Gupta /**
6198a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
6208a07d286SRatan Gupta  * @param input JSON data
6218a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6228a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
6238a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
6248a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
6258a07d286SRatan Gupta  */
6268a07d286SRatan Gupta 
6274f48d5f6SEd Tanous inline void
6284f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
6298d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6308a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
6318a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
6328a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
6338a07d286SRatan Gupta {
6348a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
6358a07d286SRatan Gupta 
6368a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
6378a07d286SRatan Gupta                              searchSettings))
6388a07d286SRatan Gupta     {
6398a07d286SRatan Gupta         return;
6408a07d286SRatan Gupta     }
6418a07d286SRatan Gupta     if (!searchSettings)
6428a07d286SRatan Gupta     {
6438a07d286SRatan Gupta         return;
6448a07d286SRatan Gupta     }
6458a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
6468a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
6478a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
6488a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
6498a07d286SRatan Gupta     {
6508a07d286SRatan Gupta         return;
6518a07d286SRatan Gupta     }
6528a07d286SRatan Gupta }
6538a07d286SRatan Gupta /**
6548a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6558a07d286SRatan Gupta           json response with the new value.
6568a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6578a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6588a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6598a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6608a07d286SRatan Gupta  */
6618a07d286SRatan Gupta 
6624f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6638a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6648d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6658a07d286SRatan Gupta     const std::string& ldapServerElementName,
6668a07d286SRatan Gupta     const std::string& ldapConfigObject)
6678a07d286SRatan Gupta {
6688a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6698a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
6708a07d286SRatan Gupta          serviceAddressList](const boost::system::error_code ec) {
6718a07d286SRatan Gupta         if (ec)
6728a07d286SRatan Gupta         {
6738a07d286SRatan Gupta             BMCWEB_LOG_DEBUG
674c61704abSGunnar Mills                 << "Error Occurred in updating the service address";
6758a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6768a07d286SRatan Gupta             return;
6778a07d286SRatan Gupta         }
6788a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
6798a07d286SRatan Gupta             serviceAddressList.front()};
680002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
6818a07d286SRatan Gupta             modifiedserviceAddressList;
6828a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
6838a07d286SRatan Gupta         {
684002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
6858a07d286SRatan Gupta                                             serviceAddressList.front());
6868a07d286SRatan Gupta         }
6878a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
6888a07d286SRatan Gupta         },
6898a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6908a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
691168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
6928a07d286SRatan Gupta }
6938a07d286SRatan Gupta /**
6948a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
6958a07d286SRatan Gupta           json response with the new value.
6968a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
6978a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6988a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6998a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7008a07d286SRatan Gupta  */
7018a07d286SRatan Gupta 
7024f48d5f6SEd Tanous inline void
7034f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
7048d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7058a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7068a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7078a07d286SRatan Gupta {
7088a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7098a07d286SRatan Gupta         [asyncResp, username,
7108a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7118a07d286SRatan Gupta         if (ec)
7128a07d286SRatan Gupta         {
7136c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
7148a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7158a07d286SRatan Gupta             return;
7168a07d286SRatan Gupta         }
717002d39b4SEd Tanous         asyncResp->res
718002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Username"] =
719002d39b4SEd Tanous             username;
7208a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
7218a07d286SRatan Gupta         },
7228a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
723168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
724168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
7258a07d286SRatan Gupta }
7268a07d286SRatan Gupta 
7278a07d286SRatan Gupta /**
7288a07d286SRatan Gupta  * @brief updates the LDAP password
7298a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
7308a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7318a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7328a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
7338a07d286SRatan Gupta  */
7348a07d286SRatan Gupta 
7354f48d5f6SEd Tanous inline void
7364f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
7378d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7388a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7398a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7408a07d286SRatan Gupta {
7418a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7428a07d286SRatan Gupta         [asyncResp, password,
7438a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7448a07d286SRatan Gupta         if (ec)
7458a07d286SRatan Gupta         {
7466c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
7478a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7488a07d286SRatan Gupta             return;
7498a07d286SRatan Gupta         }
750002d39b4SEd Tanous         asyncResp->res
751002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Password"] =
752002d39b4SEd Tanous             "";
7538a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
7548a07d286SRatan Gupta         },
7558a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7568a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
757168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
7588a07d286SRatan Gupta }
7598a07d286SRatan Gupta 
7608a07d286SRatan Gupta /**
7618a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7628a07d286SRatan Gupta           json response with the new value.
7638a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7648a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7658a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7668a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7678a07d286SRatan Gupta  */
7688a07d286SRatan Gupta 
7694f48d5f6SEd Tanous inline void
7704f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7718d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7728a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7738a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7748a07d286SRatan Gupta {
7758a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7768a07d286SRatan Gupta         [asyncResp, baseDNList,
7778a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7788a07d286SRatan Gupta         if (ec)
7798a07d286SRatan Gupta         {
7806c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
7818a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7828a07d286SRatan Gupta             return;
7838a07d286SRatan Gupta         }
784002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7858a07d286SRatan Gupta         auto& searchSettingsJson =
7868a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7876c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
7886c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
7898a07d286SRatan Gupta         if (baseDNList.size() > 1)
7908a07d286SRatan Gupta         {
791002d39b4SEd Tanous             messages::propertyValueModified(
792002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
7938a07d286SRatan Gupta         }
7948a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
7958a07d286SRatan Gupta         },
7968a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7978a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
798168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
7998a07d286SRatan Gupta }
8008a07d286SRatan Gupta /**
8018a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
8028a07d286SRatan Gupta           json response with the new value.
8038a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
8048a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8058a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8068a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8078a07d286SRatan Gupta  */
8088a07d286SRatan Gupta 
8094f48d5f6SEd Tanous inline void
8104f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
8118d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8128a07d286SRatan Gupta                             const std::string& ldapServerElementName,
8138a07d286SRatan Gupta                             const std::string& ldapConfigObject)
8148a07d286SRatan Gupta {
8158a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8168a07d286SRatan Gupta         [asyncResp, userNameAttribute,
8178a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8188a07d286SRatan Gupta         if (ec)
8198a07d286SRatan Gupta         {
820c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8218a07d286SRatan Gupta                                 "username attribute";
8228a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8238a07d286SRatan Gupta             return;
8248a07d286SRatan Gupta         }
825002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8268a07d286SRatan Gupta         auto& searchSettingsJson =
8278a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8288a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
8298a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
8308a07d286SRatan Gupta         },
8318a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8328a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
833168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
8348a07d286SRatan Gupta }
8358a07d286SRatan Gupta /**
8368a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
8378a07d286SRatan Gupta           json response with the new value.
8388a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
8398a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8408a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8418a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8428a07d286SRatan Gupta  */
8438a07d286SRatan Gupta 
8444f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
8458d1b46d7Szhanghch05     const std::string& groupsAttribute,
8468d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8478a07d286SRatan Gupta     const std::string& ldapServerElementName,
8488a07d286SRatan Gupta     const std::string& ldapConfigObject)
8498a07d286SRatan Gupta {
8508a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8518a07d286SRatan Gupta         [asyncResp, groupsAttribute,
8528a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8538a07d286SRatan Gupta         if (ec)
8548a07d286SRatan Gupta         {
855c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8568a07d286SRatan Gupta                                 "groupname attribute";
8578a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8588a07d286SRatan Gupta             return;
8598a07d286SRatan Gupta         }
860002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8618a07d286SRatan Gupta         auto& searchSettingsJson =
8628a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8638a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8648a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8658a07d286SRatan Gupta         },
8668a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8678a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
868168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
8698a07d286SRatan Gupta }
8708a07d286SRatan Gupta /**
8718a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
8728a07d286SRatan Gupta           json response with the new value.
8738a07d286SRatan Gupta  * @param input JSON data.
8748a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8758a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8768a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8778a07d286SRatan Gupta  */
8788a07d286SRatan Gupta 
8794f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8806c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8818a07d286SRatan Gupta     const std::string& ldapServerElementName,
8828a07d286SRatan Gupta     const std::string& ldapConfigObject)
8838a07d286SRatan Gupta {
8848a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8858a07d286SRatan Gupta         [asyncResp, serviceEnabled,
8868a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8878a07d286SRatan Gupta         if (ec)
8888a07d286SRatan Gupta         {
889002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
8908a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8918a07d286SRatan Gupta             return;
8928a07d286SRatan Gupta         }
8936c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
8948a07d286SRatan Gupta             serviceEnabled;
8956c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
8968a07d286SRatan Gupta         },
8978a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
898168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
899168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
9008a07d286SRatan Gupta }
9018a07d286SRatan Gupta 
9024f48d5f6SEd Tanous inline void
9034f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
9048d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
90578158631SZbigniew Kurzynski {
90678158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
90778158631SZbigniew Kurzynski     std::optional<bool> cookie;
90878158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
90978158631SZbigniew Kurzynski     std::optional<bool> xToken;
910501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
91178158631SZbigniew Kurzynski 
91278158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
91378158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
914501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
91578158631SZbigniew Kurzynski     {
91678158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
91778158631SZbigniew Kurzynski         return;
91878158631SZbigniew Kurzynski     }
91978158631SZbigniew Kurzynski 
92078158631SZbigniew Kurzynski     // Make a copy of methods configuration
92152cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
92252cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
92378158631SZbigniew Kurzynski 
92478158631SZbigniew Kurzynski     if (basicAuth)
92578158631SZbigniew Kurzynski     {
926f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
927f16f6263SAlan Kuo         messages::actionNotSupported(
9280fda0f12SGeorge Liu             asyncResp->res,
9290fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
930f16f6263SAlan Kuo         return;
931f16f6263SAlan Kuo #endif
93278158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
93378158631SZbigniew Kurzynski     }
93478158631SZbigniew Kurzynski 
93578158631SZbigniew Kurzynski     if (cookie)
93678158631SZbigniew Kurzynski     {
937f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
9380fda0f12SGeorge Liu         messages::actionNotSupported(
9390fda0f12SGeorge Liu             asyncResp->res,
9400fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
941f16f6263SAlan Kuo         return;
942f16f6263SAlan Kuo #endif
94378158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
94478158631SZbigniew Kurzynski     }
94578158631SZbigniew Kurzynski 
94678158631SZbigniew Kurzynski     if (sessionToken)
94778158631SZbigniew Kurzynski     {
948f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
949f16f6263SAlan Kuo         messages::actionNotSupported(
9500fda0f12SGeorge Liu             asyncResp->res,
9510fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
952f16f6263SAlan Kuo         return;
953f16f6263SAlan Kuo #endif
95478158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
95578158631SZbigniew Kurzynski     }
95678158631SZbigniew Kurzynski 
95778158631SZbigniew Kurzynski     if (xToken)
95878158631SZbigniew Kurzynski     {
959f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
9600fda0f12SGeorge Liu         messages::actionNotSupported(
9610fda0f12SGeorge Liu             asyncResp->res,
9620fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
963f16f6263SAlan Kuo         return;
964f16f6263SAlan Kuo #endif
96578158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
96678158631SZbigniew Kurzynski     }
96778158631SZbigniew Kurzynski 
968501f1e58SZbigniew Kurzynski     if (tls)
969501f1e58SZbigniew Kurzynski     {
970f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
9710fda0f12SGeorge Liu         messages::actionNotSupported(
9720fda0f12SGeorge Liu             asyncResp->res,
9730fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
974f16f6263SAlan Kuo         return;
975f16f6263SAlan Kuo #endif
976501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
977501f1e58SZbigniew Kurzynski     }
978501f1e58SZbigniew Kurzynski 
97978158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
980501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
981501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
98278158631SZbigniew Kurzynski     {
98378158631SZbigniew Kurzynski         // Do not allow user to disable everything
98478158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
98578158631SZbigniew Kurzynski                                      "of disabling all available methods");
98678158631SZbigniew Kurzynski         return;
98778158631SZbigniew Kurzynski     }
98878158631SZbigniew Kurzynski 
98952cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
99052cc112dSEd Tanous         authMethodsConfig);
99178158631SZbigniew Kurzynski     // Save configuration immediately
99252cc112dSEd Tanous     persistent_data::getConfig().writeData();
99378158631SZbigniew Kurzynski 
99478158631SZbigniew Kurzynski     messages::success(asyncResp->res);
99578158631SZbigniew Kurzynski }
99678158631SZbigniew Kurzynski 
9978a07d286SRatan Gupta /**
9988a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
9998a07d286SRatan Gupta  *        value and create the LDAP config object.
10008a07d286SRatan Gupta  * @param input JSON data
10018a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
10028a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
10038a07d286SRatan Gupta  */
10048a07d286SRatan Gupta 
10056c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
10068d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10078a07d286SRatan Gupta                             const std::string& serverType)
10088a07d286SRatan Gupta {
1009eb2bbe56SRatan Gupta     std::string dbusObjectPath;
1010eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
1011eb2bbe56SRatan Gupta     {
10122c70f800SEd Tanous         dbusObjectPath = adConfigObject;
1013eb2bbe56SRatan Gupta     }
1014eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
1015eb2bbe56SRatan Gupta     {
101623a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
1017eb2bbe56SRatan Gupta     }
1018cb13a392SEd Tanous     else
1019cb13a392SEd Tanous     {
1020cb13a392SEd Tanous         return;
1021cb13a392SEd Tanous     }
1022eb2bbe56SRatan Gupta 
10238a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
10248a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
10258a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
10268a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
10278a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
10288a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
10298a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
10308a07d286SRatan Gupta     std::optional<std::string> userName;
10318a07d286SRatan Gupta     std::optional<std::string> password;
103206785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
10338a07d286SRatan Gupta 
10348a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
10358a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
10368a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
103706785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
103806785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
10398a07d286SRatan Gupta     {
10408a07d286SRatan Gupta         return;
10418a07d286SRatan Gupta     }
10428a07d286SRatan Gupta 
10438a07d286SRatan Gupta     if (authentication)
10448a07d286SRatan Gupta     {
10458a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
10468a07d286SRatan Gupta                                     password);
10478a07d286SRatan Gupta     }
10488a07d286SRatan Gupta     if (ldapService)
10498a07d286SRatan Gupta     {
10508a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10518a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
10528a07d286SRatan Gupta     }
10538a07d286SRatan Gupta     if (serviceAddressList)
10548a07d286SRatan Gupta     {
105526f6976fSEd Tanous         if (serviceAddressList->empty())
10568a07d286SRatan Gupta         {
10578a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10588a07d286SRatan Gupta                                              "ServiceAddress");
10598a07d286SRatan Gupta             return;
10608a07d286SRatan Gupta         }
10618a07d286SRatan Gupta     }
10628a07d286SRatan Gupta     if (baseDNList)
10638a07d286SRatan Gupta     {
106426f6976fSEd Tanous         if (baseDNList->empty())
10658a07d286SRatan Gupta         {
10668a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10678a07d286SRatan Gupta                                              "BaseDistinguishedNames");
10688a07d286SRatan Gupta             return;
10698a07d286SRatan Gupta         }
10708a07d286SRatan Gupta     }
10718a07d286SRatan Gupta 
10728a07d286SRatan Gupta     // nothing to update, then return
10738a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
107406785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
107506785244SRatan Gupta         !remoteRoleMapData)
10768a07d286SRatan Gupta     {
10778a07d286SRatan Gupta         return;
10788a07d286SRatan Gupta     }
10798a07d286SRatan Gupta 
10808a07d286SRatan Gupta     // Get the existing resource first then keep modifying
10818a07d286SRatan Gupta     // whenever any property gets updated.
1082002d39b4SEd Tanous     getLDAPConfigData(
1083002d39b4SEd Tanous         serverType,
1084002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1085002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1086002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
108723a21a1cSEd Tanous                             const std::string& serverT) {
10888a07d286SRatan Gupta         if (!success)
10898a07d286SRatan Gupta         {
10908a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10918a07d286SRatan Gupta             return;
10928a07d286SRatan Gupta         }
10936c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
10948a07d286SRatan Gupta         if (confData.serviceEnabled)
10958a07d286SRatan Gupta         {
10968a07d286SRatan Gupta             // Disable the service first and update the rest of
10978a07d286SRatan Gupta             // the properties.
10986c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
10998a07d286SRatan Gupta         }
11008a07d286SRatan Gupta 
11018a07d286SRatan Gupta         if (serviceAddressList)
11028a07d286SRatan Gupta         {
11036c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
11046c51eab1SEd Tanous                                       dbusObjectPath);
11058a07d286SRatan Gupta         }
11068a07d286SRatan Gupta         if (userName)
11078a07d286SRatan Gupta         {
11086c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
11098a07d286SRatan Gupta         }
11108a07d286SRatan Gupta         if (password)
11118a07d286SRatan Gupta         {
11126c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
11138a07d286SRatan Gupta         }
11148a07d286SRatan Gupta 
11158a07d286SRatan Gupta         if (baseDNList)
11168a07d286SRatan Gupta         {
11176c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
11188a07d286SRatan Gupta         }
11198a07d286SRatan Gupta         if (userNameAttribute)
11208a07d286SRatan Gupta         {
11216c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
11226c51eab1SEd Tanous                                     dbusObjectPath);
11238a07d286SRatan Gupta         }
11248a07d286SRatan Gupta         if (groupsAttribute)
11258a07d286SRatan Gupta         {
11266c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
11276c51eab1SEd Tanous                                      dbusObjectPath);
11288a07d286SRatan Gupta         }
11298a07d286SRatan Gupta         if (serviceEnabled)
11308a07d286SRatan Gupta         {
11318a07d286SRatan Gupta             // if user has given the value as true then enable
11328a07d286SRatan Gupta             // the service. if user has given false then no-op
11338a07d286SRatan Gupta             // as service is already stopped.
11348a07d286SRatan Gupta             if (*serviceEnabled)
11358a07d286SRatan Gupta             {
11366c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
11376c51eab1SEd Tanous                                          dbusObjectPath);
11388a07d286SRatan Gupta             }
11398a07d286SRatan Gupta         }
11408a07d286SRatan Gupta         else
11418a07d286SRatan Gupta         {
11428a07d286SRatan Gupta             // if user has not given the service enabled value
11438a07d286SRatan Gupta             // then revert it to the same state as it was
11448a07d286SRatan Gupta             // before.
11458a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
114623a21a1cSEd Tanous                                      serverT, dbusObjectPath);
11478a07d286SRatan Gupta         }
114806785244SRatan Gupta 
114906785244SRatan Gupta         if (remoteRoleMapData)
115006785244SRatan Gupta         {
11516c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
11526c51eab1SEd Tanous                                *remoteRoleMapData);
115306785244SRatan Gupta         }
11548a07d286SRatan Gupta         });
11558a07d286SRatan Gupta }
1156d4b5443fSEd Tanous 
11576c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
11586c51eab1SEd Tanous                                  const std::string& username,
1159618c14b4SEd Tanous                                  const std::optional<std::string>& password,
1160618c14b4SEd Tanous                                  const std::optional<bool>& enabled,
1161618c14b4SEd Tanous                                  const std::optional<std::string>& roleId,
1162618c14b4SEd Tanous                                  const std::optional<bool>& locked)
11631abe55efSEd Tanous {
1164b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1165b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1166b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
11676c51eab1SEd Tanous 
11686c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1169618c14b4SEd Tanous         dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
1170618c14b4SEd Tanous                          locked, asyncResp{std::move(asyncResp)}](int rc) {
1171e662eae8SEd Tanous             if (rc <= 0)
11726c51eab1SEd Tanous             {
1173d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11746c51eab1SEd Tanous                                            username);
11756c51eab1SEd Tanous                 return;
11766c51eab1SEd Tanous             }
11776c51eab1SEd Tanous 
11786c51eab1SEd Tanous             if (password)
11796c51eab1SEd Tanous             {
11806c51eab1SEd Tanous                 int retval = pamUpdatePassword(username, *password);
11816c51eab1SEd Tanous 
11826c51eab1SEd Tanous                 if (retval == PAM_USER_UNKNOWN)
11836c51eab1SEd Tanous                 {
1184d8a5d5d8SJiaqing Zhao                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11856c51eab1SEd Tanous                                                username);
11866c51eab1SEd Tanous                 }
11876c51eab1SEd Tanous                 else if (retval == PAM_AUTHTOK_ERR)
11886c51eab1SEd Tanous                 {
11896c51eab1SEd Tanous                     // If password is invalid
1190618c14b4SEd Tanous                     messages::propertyValueFormatError(asyncResp->res,
1191618c14b4SEd Tanous                                                        *password, "Password");
11926c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
11936c51eab1SEd Tanous                 }
11946c51eab1SEd Tanous                 else if (retval != PAM_SUCCESS)
11956c51eab1SEd Tanous                 {
11966c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11976c51eab1SEd Tanous                     return;
11986c51eab1SEd Tanous                 }
1199e7b1b62bSEd Tanous                 else
1200e7b1b62bSEd Tanous                 {
1201e7b1b62bSEd Tanous                     messages::success(asyncResp->res);
1202e7b1b62bSEd Tanous                 }
12036c51eab1SEd Tanous             }
12046c51eab1SEd Tanous 
12056c51eab1SEd Tanous             if (enabled)
12066c51eab1SEd Tanous             {
12076c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12086c51eab1SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
12096c51eab1SEd Tanous                 if (ec)
12106c51eab1SEd Tanous                 {
12116c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12126c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12136c51eab1SEd Tanous                     return;
12146c51eab1SEd Tanous                 }
12156c51eab1SEd Tanous                 messages::success(asyncResp->res);
12166c51eab1SEd Tanous                 return;
12176c51eab1SEd Tanous                     },
1218e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12196c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12206c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserEnabled",
1221168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*enabled});
12226c51eab1SEd Tanous             }
12236c51eab1SEd Tanous 
12246c51eab1SEd Tanous             if (roleId)
12256c51eab1SEd Tanous             {
12266c51eab1SEd Tanous                 std::string priv = getPrivilegeFromRoleId(*roleId);
12276c51eab1SEd Tanous                 if (priv.empty())
12286c51eab1SEd Tanous                 {
12296c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, *roleId,
12306c51eab1SEd Tanous                                                      "RoleId");
12316c51eab1SEd Tanous                     return;
12326c51eab1SEd Tanous                 }
12336c51eab1SEd Tanous 
12346c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12356c51eab1SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
12366c51eab1SEd Tanous                 if (ec)
12376c51eab1SEd Tanous                 {
12386c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12396c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12406c51eab1SEd Tanous                     return;
12416c51eab1SEd Tanous                 }
12426c51eab1SEd Tanous                 messages::success(asyncResp->res);
12436c51eab1SEd Tanous                     },
1244e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12456c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12466c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1247168e20c1SEd Tanous                     dbus::utility::DbusVariantType{priv});
12486c51eab1SEd Tanous             }
12496c51eab1SEd Tanous 
12506c51eab1SEd Tanous             if (locked)
12516c51eab1SEd Tanous             {
12526c51eab1SEd Tanous                 // admin can unlock the account which is locked by
12536c51eab1SEd Tanous                 // successive authentication failures but admin should
12546c51eab1SEd Tanous                 // not be allowed to lock an account.
12556c51eab1SEd Tanous                 if (*locked)
12566c51eab1SEd Tanous                 {
12576c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, "true",
12586c51eab1SEd Tanous                                                      "Locked");
12596c51eab1SEd Tanous                     return;
12606c51eab1SEd Tanous                 }
12616c51eab1SEd Tanous 
12626c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12636c51eab1SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
12646c51eab1SEd Tanous                 if (ec)
12656c51eab1SEd Tanous                 {
12666c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12676c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12686c51eab1SEd Tanous                     return;
12696c51eab1SEd Tanous                 }
12706c51eab1SEd Tanous                 messages::success(asyncResp->res);
12716c51eab1SEd Tanous                 return;
12726c51eab1SEd Tanous                     },
1273e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12746c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12756c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes",
1276168e20c1SEd Tanous                     "UserLockedForFailedAttempt",
1277168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*locked});
12786c51eab1SEd Tanous             }
12796c51eab1SEd Tanous         });
12806c51eab1SEd Tanous }
12816c51eab1SEd Tanous 
12824c7d4d33SEd Tanous inline void handleAccountServiceHead(
12834c7d4d33SEd Tanous     App& app, const crow::Request& req,
12841ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12856c51eab1SEd Tanous {
12864c7d4d33SEd Tanous 
12873ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
128845ca1b86SEd Tanous     {
128945ca1b86SEd Tanous         return;
129045ca1b86SEd Tanous     }
12914c7d4d33SEd Tanous     asyncResp->res.addHeader(
12924c7d4d33SEd Tanous         boost::beast::http::field::link,
12934c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
12944c7d4d33SEd Tanous }
12954c7d4d33SEd Tanous 
12964c7d4d33SEd Tanous inline void
12974c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
12984c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12994c7d4d33SEd Tanous {
13004c7d4d33SEd Tanous     handleAccountServiceHead(app, req, asyncResp);
130152cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
13021ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
130378158631SZbigniew Kurzynski 
13041476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
13051476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
13061476687dSEd Tanous     json["@odata.type"] = "#AccountService."
13071476687dSEd Tanous                           "v1_10_0.AccountService";
13081476687dSEd Tanous     json["Id"] = "AccountService";
13091476687dSEd Tanous     json["Name"] = "Account Service";
13101476687dSEd Tanous     json["Description"] = "Account Service";
13111476687dSEd Tanous     json["ServiceEnabled"] = true;
13121476687dSEd Tanous     json["MaxPasswordLength"] = 20;
13131ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
13141476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
13151476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
13161476687dSEd Tanous         "#OemAccountService.v1_0_0.AccountService";
13171476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
13181476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
13191476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
13201476687dSEd Tanous         authMethodsConfig.basic;
13211476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
13221476687dSEd Tanous         authMethodsConfig.sessionToken;
13231ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
13241ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
13251ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
13261476687dSEd Tanous 
13271ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
13281ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
13291ef4c342SEd Tanous     // permissions ConfigureManager
133072048780SAbhishek Patel     Privileges effectiveUserPrivileges =
133172048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
133272048780SAbhishek Patel 
133372048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
133472048780SAbhishek Patel                                          effectiveUserPrivileges))
133572048780SAbhishek Patel     {
13361ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
13371476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
133872048780SAbhishek Patel     }
1339d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1340d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1341d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
1342002d39b4SEd Tanous         [asyncResp](const boost::system::error_code ec,
13431ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
13443d958bbcSAppaRao Puli         if (ec)
13453d958bbcSAppaRao Puli         {
13463d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
13473d958bbcSAppaRao Puli             return;
13483d958bbcSAppaRao Puli         }
1349d1bde9e5SKrzysztof Grobelny 
13503d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
13513d958bbcSAppaRao Puli                          << "properties for AccountService";
1352d1bde9e5SKrzysztof Grobelny 
1353d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1354d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1355d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1356d1bde9e5SKrzysztof Grobelny 
1357d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1358d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1359d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1360d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1361d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1362d1bde9e5SKrzysztof Grobelny 
1363d1bde9e5SKrzysztof Grobelny         if (!success)
13643d958bbcSAppaRao Puli         {
1365d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1366d1bde9e5SKrzysztof Grobelny             return;
13673d958bbcSAppaRao Puli         }
1368d1bde9e5SKrzysztof Grobelny 
1369d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
13703d958bbcSAppaRao Puli         {
1371d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
13723d958bbcSAppaRao Puli         }
1373d1bde9e5SKrzysztof Grobelny 
1374d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
13753d958bbcSAppaRao Puli         {
1376d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1377d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1378d1bde9e5SKrzysztof Grobelny         }
1379d1bde9e5SKrzysztof Grobelny 
1380d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
13813d958bbcSAppaRao Puli         {
1382002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1383d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
13843d958bbcSAppaRao Puli         }
1385d1bde9e5SKrzysztof Grobelny         });
13866973a582SRatan Gupta 
138702cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1388ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1389cb13a392SEd Tanous         if (!success)
1390cb13a392SEd Tanous         {
1391cb13a392SEd Tanous             return;
1392cb13a392SEd Tanous         }
1393002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1394ab828d7cSRatan Gupta     };
1395ab828d7cSRatan Gupta 
1396ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1397ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
13981ef4c342SEd Tanous }
13996973a582SRatan Gupta 
14001ef4c342SEd Tanous inline void handleAccountServicePatch(
14011ef4c342SEd Tanous     App& app, const crow::Request& req,
14021ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14031ef4c342SEd Tanous {
14043ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
140545ca1b86SEd Tanous     {
140645ca1b86SEd Tanous         return;
140745ca1b86SEd Tanous     }
1408f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1409f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1410ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1411f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1412f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1413f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1414f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1415f5ffd806SEd Tanous 
141615ed6780SWilly Tu     if (!json_util::readJsonPatch(
14171ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
14181ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
14191ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
14201ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1421f5ffd806SEd Tanous             oemObject))
1422f5ffd806SEd Tanous     {
1423f5ffd806SEd Tanous         return;
1424f5ffd806SEd Tanous     }
1425f5ffd806SEd Tanous 
1426f5ffd806SEd Tanous     if (minPasswordLength)
1427f5ffd806SEd Tanous     {
1428ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
1429ef73ad0dSPaul Fertser             [asyncResp](const boost::system::error_code ec) {
1430ef73ad0dSPaul Fertser             if (ec)
1431ef73ad0dSPaul Fertser             {
1432ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1433ef73ad0dSPaul Fertser                 return;
1434ef73ad0dSPaul Fertser             }
1435ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1436ef73ad0dSPaul Fertser             },
14371ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1438ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
14391ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1440ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1441f5ffd806SEd Tanous     }
1442f5ffd806SEd Tanous 
1443f5ffd806SEd Tanous     if (maxPasswordLength)
1444f5ffd806SEd Tanous     {
14451ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1446f5ffd806SEd Tanous     }
1447f5ffd806SEd Tanous 
1448f5ffd806SEd Tanous     if (ldapObject)
1449f5ffd806SEd Tanous     {
1450f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1451f5ffd806SEd Tanous     }
1452f5ffd806SEd Tanous 
1453f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
14541ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1455f5ffd806SEd Tanous                                          oemOpenBMCObject))
1456f5ffd806SEd Tanous     {
1457f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1458f5ffd806SEd Tanous             oemOpenBMCObject &&
1459f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1460f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1461f5ffd806SEd Tanous         {
1462f5ffd806SEd Tanous             if (authMethodsObject)
1463f5ffd806SEd Tanous             {
14641ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1465f5ffd806SEd Tanous             }
1466f5ffd806SEd Tanous         }
1467f5ffd806SEd Tanous     }
1468f5ffd806SEd Tanous 
1469f5ffd806SEd Tanous     if (activeDirectoryObject)
1470f5ffd806SEd Tanous     {
14711ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1472f5ffd806SEd Tanous     }
1473f5ffd806SEd Tanous 
1474f5ffd806SEd Tanous     if (unlockTimeout)
1475f5ffd806SEd Tanous     {
1476f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1477f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1478f5ffd806SEd Tanous             if (ec)
1479f5ffd806SEd Tanous             {
1480f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1481f5ffd806SEd Tanous                 return;
1482f5ffd806SEd Tanous             }
1483f5ffd806SEd Tanous             messages::success(asyncResp->res);
1484f5ffd806SEd Tanous             },
14851ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1486f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
14871ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1488168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1489f5ffd806SEd Tanous     }
1490f5ffd806SEd Tanous     if (lockoutThreshold)
1491f5ffd806SEd Tanous     {
1492f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1493f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1494f5ffd806SEd Tanous             if (ec)
1495f5ffd806SEd Tanous             {
1496f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1497f5ffd806SEd Tanous                 return;
1498f5ffd806SEd Tanous             }
1499f5ffd806SEd Tanous             messages::success(asyncResp->res);
1500f5ffd806SEd Tanous             },
15011ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1502f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1503f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1504f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1505168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1506f5ffd806SEd Tanous     }
15071ef4c342SEd Tanous }
1508f5ffd806SEd Tanous 
15094c7d4d33SEd Tanous inline void handleAccountCollectionHead(
15101ef4c342SEd Tanous     App& app, const crow::Request& req,
15111ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15121ef4c342SEd Tanous {
15134c7d4d33SEd Tanous 
15143ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
151545ca1b86SEd Tanous     {
151645ca1b86SEd Tanous         return;
151745ca1b86SEd Tanous     }
15184c7d4d33SEd Tanous     asyncResp->res.addHeader(
15194c7d4d33SEd Tanous         boost::beast::http::field::link,
15204c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
15214c7d4d33SEd Tanous }
15224c7d4d33SEd Tanous 
15234c7d4d33SEd Tanous inline void handleAccountCollectionGet(
15244c7d4d33SEd Tanous     App& app, const crow::Request& req,
15254c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15264c7d4d33SEd Tanous {
15274c7d4d33SEd Tanous     handleAccountCollectionHead(app, req, asyncResp);
15281476687dSEd Tanous 
15291476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
15301476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
15311ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
15321476687dSEd Tanous                                               "ManagerAccountCollection";
15331476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
15341476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
15350f74e643SEd Tanous 
15366c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
15376c51eab1SEd Tanous         redfish::getUserPrivileges(req.userRole);
15386c51eab1SEd Tanous 
1539f5e29f33SJunLin Chen     std::string thisUser;
1540f5e29f33SJunLin Chen     if (req.session)
1541f5e29f33SJunLin Chen     {
1542f5e29f33SJunLin Chen         thisUser = req.session->username;
1543f5e29f33SJunLin Chen     }
1544b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
1545cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
1546cef1ddfbSEd Tanous             const boost::system::error_code ec,
1547711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1548b9b2e0b2SEd Tanous         if (ec)
1549b9b2e0b2SEd Tanous         {
1550f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1551b9b2e0b2SEd Tanous             return;
1552b9b2e0b2SEd Tanous         }
1553b9b2e0b2SEd Tanous 
1554cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1555002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1556cef1ddfbSEd Tanous 
1557cef1ddfbSEd Tanous         bool userCanSeeSelf =
1558002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1559cef1ddfbSEd Tanous 
1560002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1561b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1562b9b2e0b2SEd Tanous 
15639eb808c1SEd Tanous         for (const auto& userpath : users)
1564b9b2e0b2SEd Tanous         {
15652dfd18efSEd Tanous             std::string user = userpath.first.filename();
15662dfd18efSEd Tanous             if (user.empty())
1567b9b2e0b2SEd Tanous             {
15682dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
15692dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
15702dfd18efSEd Tanous 
15712dfd18efSEd Tanous                 return;
1572b9b2e0b2SEd Tanous             }
1573f365910cSGunnar Mills 
1574f365910cSGunnar Mills             // As clarified by Redfish here:
1575f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
15766c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
15776c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
15786c51eab1SEd Tanous             // accounts.
15791ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1580f365910cSGunnar Mills             {
15811476687dSEd Tanous                 nlohmann::json::object_t member;
15821476687dSEd Tanous                 member["@odata.id"] =
1583002d39b4SEd Tanous                     "/redfish/v1/AccountService/Accounts/" + user;
15841476687dSEd Tanous                 memberArray.push_back(std::move(member));
1585b9b2e0b2SEd Tanous             }
1586f365910cSGunnar Mills         }
15871ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1588b9b2e0b2SEd Tanous         },
15891ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1590b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15911ef4c342SEd Tanous }
15926c51eab1SEd Tanous 
15931ef4c342SEd Tanous inline void handleAccountCollectionPost(
15941ef4c342SEd Tanous     App& app, const crow::Request& req,
15951ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15961ef4c342SEd Tanous {
15973ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
159845ca1b86SEd Tanous     {
159945ca1b86SEd Tanous         return;
160045ca1b86SEd Tanous     }
16019712f8acSEd Tanous     std::string username;
16029712f8acSEd Tanous     std::string password;
1603a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1604a24526dcSEd Tanous     std::optional<bool> enabled = true;
16051ef4c342SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
16061ef4c342SEd Tanous                                   "Password", password, "RoleId", roleId,
16071ef4c342SEd Tanous                                   "Enabled", enabled))
160804ae99ecSEd Tanous     {
160904ae99ecSEd Tanous         return;
161004ae99ecSEd Tanous     }
161104ae99ecSEd Tanous 
161254fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
161384e12cb7SAppaRao Puli     if (priv.empty())
161404ae99ecSEd Tanous     {
16151ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
161604ae99ecSEd Tanous         return;
161704ae99ecSEd Tanous     }
16189712f8acSEd Tanous     roleId = priv;
161904ae99ecSEd Tanous 
1620599c71d8SAyushi Smriti     // Reading AllGroups property
16211e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
16221ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
16231ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
16241ef4c342SEd Tanous         "AllGroups",
1625599c71d8SAyushi Smriti         [asyncResp, username, password{std::move(password)}, roleId,
1626168e20c1SEd Tanous          enabled](const boost::system::error_code ec,
16271e1e598dSJonathan Doman                   const std::vector<std::string>& allGroupsList) {
1628599c71d8SAyushi Smriti         if (ec)
1629599c71d8SAyushi Smriti         {
1630599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1631599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1632599c71d8SAyushi Smriti             return;
1633599c71d8SAyushi Smriti         }
1634599c71d8SAyushi Smriti 
16351e1e598dSJonathan Doman         if (allGroupsList.empty())
1636599c71d8SAyushi Smriti         {
1637599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1638599c71d8SAyushi Smriti             return;
1639599c71d8SAyushi Smriti         }
1640599c71d8SAyushi Smriti 
164104ae99ecSEd Tanous         crow::connections::systemBus->async_method_call(
16421ef4c342SEd Tanous             [asyncResp, username, password](const boost::system::error_code ec2,
164359d494eeSPatrick Williams                                             sdbusplus::message_t& m) {
164423a21a1cSEd Tanous             if (ec2)
164504ae99ecSEd Tanous             {
16461ef4c342SEd Tanous                 userErrorMessageHandler(m.get_error(), asyncResp, username, "");
164704ae99ecSEd Tanous                 return;
164804ae99ecSEd Tanous             }
164904ae99ecSEd Tanous 
1650002d39b4SEd Tanous             if (pamUpdatePassword(username, password) != PAM_SUCCESS)
165104ae99ecSEd Tanous             {
16526c51eab1SEd Tanous                 // At this point we have a user that's been
16536c51eab1SEd Tanous                 // created, but the password set
16546c51eab1SEd Tanous                 // failed.Something is wrong, so delete the user
16556c51eab1SEd Tanous                 // that we've already created
16561ef4c342SEd Tanous                 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1657b477fd44SP Dheeraj Srujan Kumar                 tempObjPath /= username;
1658b477fd44SP Dheeraj Srujan Kumar                 const std::string userPath(tempObjPath);
1659b477fd44SP Dheeraj Srujan Kumar 
166004ae99ecSEd Tanous                 crow::connections::systemBus->async_method_call(
16611ef4c342SEd Tanous                     [asyncResp, password](const boost::system::error_code ec3) {
166223a21a1cSEd Tanous                     if (ec3)
166304ae99ecSEd Tanous                     {
1664002d39b4SEd Tanous                         messages::internalError(asyncResp->res);
166504ae99ecSEd Tanous                         return;
166604ae99ecSEd Tanous                     }
166704ae99ecSEd Tanous 
16680d4197e0Sanil kumar appana                     // If password is invalid
16691ef4c342SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, password,
16701ef4c342SEd Tanous                                                        "Password");
167104ae99ecSEd Tanous                     },
1672002d39b4SEd Tanous                     "xyz.openbmc_project.User.Manager", userPath,
1673002d39b4SEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
167404ae99ecSEd Tanous 
167504ae99ecSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
167604ae99ecSEd Tanous                 return;
167704ae99ecSEd Tanous             }
167804ae99ecSEd Tanous 
1679f12894f8SJason M. Bills             messages::created(asyncResp->res);
168004ae99ecSEd Tanous             asyncResp->res.addHeader(
16811ef4c342SEd Tanous                 "Location", "/redfish/v1/AccountService/Accounts/" + username);
168204ae99ecSEd Tanous             },
1683002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1684002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "CreateUser", username,
1685002d39b4SEd Tanous             allGroupsList, *roleId, *enabled);
16861e1e598dSJonathan Doman         });
16871ef4c342SEd Tanous }
1688b9b2e0b2SEd Tanous 
16891ef4c342SEd Tanous inline void
16904c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
16916c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16924c7d4d33SEd Tanous                       const std::string& /*accountName*/)
16931ef4c342SEd Tanous {
16944c7d4d33SEd Tanous 
16953ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
169645ca1b86SEd Tanous     {
169745ca1b86SEd Tanous         return;
169845ca1b86SEd Tanous     }
16994c7d4d33SEd Tanous     asyncResp->res.addHeader(
17004c7d4d33SEd Tanous         boost::beast::http::field::link,
17014c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
17024c7d4d33SEd Tanous }
17034c7d4d33SEd Tanous inline void
17044c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
17054c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17064c7d4d33SEd Tanous                      const std::string& accountName)
17074c7d4d33SEd Tanous {
17084c7d4d33SEd Tanous     handleAccountHead(app, req, asyncResp, accountName);
17091ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1710031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1711d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1712031514fbSJunLin Chen     return;
1713031514fbSJunLin Chen 
17141ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1715031514fbSJunLin Chen     if (req.session == nullptr)
1716031514fbSJunLin Chen     {
1717031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1718031514fbSJunLin Chen         return;
1719031514fbSJunLin Chen     }
17206c51eab1SEd Tanous     if (req.session->username != accountName)
1721b9b2e0b2SEd Tanous     {
17226c51eab1SEd Tanous         // At this point we've determined that the user is trying to
17231ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
17241ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
17251ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
17266c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
17276c51eab1SEd Tanous             redfish::getUserPrivileges(req.userRole);
17281ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
17291ef4c342SEd Tanous                                                          "ConfigureManager"};
17306c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
17316c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1732900f9497SJoseph Reynolds         {
1733900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
1734900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1735900f9497SJoseph Reynolds             return;
1736900f9497SJoseph Reynolds         }
1737900f9497SJoseph Reynolds     }
1738900f9497SJoseph Reynolds 
1739b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
17401ef4c342SEd Tanous         [asyncResp,
17411ef4c342SEd Tanous          accountName](const boost::system::error_code ec,
1742711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1743b9b2e0b2SEd Tanous         if (ec)
1744b9b2e0b2SEd Tanous         {
1745f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1746b9b2e0b2SEd Tanous             return;
1747b9b2e0b2SEd Tanous         }
1748711ac7a9SEd Tanous         const auto userIt = std::find_if(
1749b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
1750b477fd44SP Dheeraj Srujan Kumar             [accountName](
1751b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
1752002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
175355f79e6fSEd Tanous             return accountName == user.first.filename();
1754b477fd44SP Dheeraj Srujan Kumar             });
1755b9b2e0b2SEd Tanous 
175684e12cb7SAppaRao Puli         if (userIt == users.end())
1757b9b2e0b2SEd Tanous         {
1758002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1759002d39b4SEd Tanous                                        accountName);
176084e12cb7SAppaRao Puli             return;
176184e12cb7SAppaRao Puli         }
17624e68c45bSAyushi Smriti 
17631476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
17641476687dSEd Tanous             "#ManagerAccount.v1_4_0.ManagerAccount";
17651476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
17661476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
17671476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
17684e68c45bSAyushi Smriti 
176984e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
177065b0dc32SEd Tanous         {
1771002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
177265b0dc32SEd Tanous             {
177365b0dc32SEd Tanous                 for (const auto& property : interface.second)
177465b0dc32SEd Tanous                 {
177565b0dc32SEd Tanous                     if (property.first == "UserEnabled")
177665b0dc32SEd Tanous                     {
177765b0dc32SEd Tanous                         const bool* userEnabled =
1778abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
177965b0dc32SEd Tanous                         if (userEnabled == nullptr)
178065b0dc32SEd Tanous                         {
1781002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
178284e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
178384e12cb7SAppaRao Puli                             return;
178465b0dc32SEd Tanous                         }
1785002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
178665b0dc32SEd Tanous                     }
1787002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
178865b0dc32SEd Tanous                     {
178965b0dc32SEd Tanous                         const bool* userLocked =
1790abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
179165b0dc32SEd Tanous                         if (userLocked == nullptr)
179265b0dc32SEd Tanous                         {
179384e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
179484e12cb7SAppaRao Puli                                                 "ailedAttempt "
179584e12cb7SAppaRao Puli                                                 "wasn't a bool";
179684e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
179784e12cb7SAppaRao Puli                             return;
179865b0dc32SEd Tanous                         }
1799002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
1800002d39b4SEd Tanous                         asyncResp->res
1801002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
18023bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
180365b0dc32SEd Tanous                     }
180484e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
180584e12cb7SAppaRao Puli                     {
180654fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1807002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
180854fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
180984e12cb7SAppaRao Puli                         {
1810002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
181184e12cb7SAppaRao Puli                                                 "string";
181284e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
181384e12cb7SAppaRao Puli                             return;
181484e12cb7SAppaRao Puli                         }
18151ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
181654fc587aSNagaraju Goruganti                         if (role.empty())
181784e12cb7SAppaRao Puli                         {
181884e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
181984e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
182084e12cb7SAppaRao Puli                             return;
182184e12cb7SAppaRao Puli                         }
182254fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
182384e12cb7SAppaRao Puli 
18241476687dSEd Tanous                         nlohmann::json& roleEntry =
1825002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
18261476687dSEd Tanous                         roleEntry["@odata.id"] =
1827002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
182884e12cb7SAppaRao Puli                     }
1829002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
18303bf4e632SJoseph Reynolds                     {
18313bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
18323bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
18333bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
18343bf4e632SJoseph Reynolds                         {
18350fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
18360fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
18373bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
18383bf4e632SJoseph Reynolds                             return;
18393bf4e632SJoseph Reynolds                         }
1840002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
18413bf4e632SJoseph Reynolds                             *userPasswordExpired;
18423bf4e632SJoseph Reynolds                     }
1843c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
1844c7229815SAbhishek Patel                     {
1845c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
1846c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
1847c7229815SAbhishek Patel                                 &property.second);
1848c7229815SAbhishek Patel                         if (userGroups == nullptr)
1849c7229815SAbhishek Patel                         {
1850c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR
1851c7229815SAbhishek Patel                                 << "userGroups wasn't a string vector";
1852c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1853c7229815SAbhishek Patel                             return;
1854c7229815SAbhishek Patel                         }
1855c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
1856c7229815SAbhishek Patel                         {
1857c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR << "userGroups mapping failed";
1858c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1859c7229815SAbhishek Patel                             return;
1860c7229815SAbhishek Patel                         }
1861c7229815SAbhishek Patel                     }
186265b0dc32SEd Tanous                 }
186365b0dc32SEd Tanous             }
186465b0dc32SEd Tanous         }
186565b0dc32SEd Tanous 
1866b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
186784e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
1868b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
1869b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
1870b9b2e0b2SEd Tanous         },
18711ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1872b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18731ef4c342SEd Tanous }
1874a840879dSEd Tanous 
18751ef4c342SEd Tanous inline void
18761ef4c342SEd Tanous     handleAccounttDelete(App& app, const crow::Request& req,
18776c51eab1SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18781ef4c342SEd Tanous                          const std::string& username)
18791ef4c342SEd Tanous {
18803ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
188145ca1b86SEd Tanous     {
188245ca1b86SEd Tanous         return;
188345ca1b86SEd Tanous     }
18841ef4c342SEd Tanous 
18851ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1886031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1887d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1888031514fbSJunLin Chen     return;
1889031514fbSJunLin Chen 
18901ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
18911ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
18921ef4c342SEd Tanous     tempObjPath /= username;
18931ef4c342SEd Tanous     const std::string userPath(tempObjPath);
18941ef4c342SEd Tanous 
18951ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
18961ef4c342SEd Tanous         [asyncResp, username](const boost::system::error_code ec) {
18971ef4c342SEd Tanous         if (ec)
18981ef4c342SEd Tanous         {
1899d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
19001ef4c342SEd Tanous                                        username);
19011ef4c342SEd Tanous             return;
19021ef4c342SEd Tanous         }
19031ef4c342SEd Tanous 
19041ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
19051ef4c342SEd Tanous         },
19061ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
19071ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
19081ef4c342SEd Tanous }
19091ef4c342SEd Tanous 
19101ef4c342SEd Tanous inline void
19111ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
19121ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19131ef4c342SEd Tanous                        const std::string& username)
19141ef4c342SEd Tanous {
19151ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19161ef4c342SEd Tanous     {
19171ef4c342SEd Tanous         return;
19181ef4c342SEd Tanous     }
19191ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
19201ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
1921d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
19221ef4c342SEd Tanous     return;
19231ef4c342SEd Tanous 
19241ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1925a24526dcSEd Tanous     std::optional<std::string> newUserName;
1926a24526dcSEd Tanous     std::optional<std::string> password;
1927a24526dcSEd Tanous     std::optional<bool> enabled;
1928a24526dcSEd Tanous     std::optional<std::string> roleId;
192924c8542dSRatan Gupta     std::optional<bool> locked;
1930e9cc5172SEd Tanous 
1931031514fbSJunLin Chen     if (req.session == nullptr)
1932031514fbSJunLin Chen     {
1933031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1934031514fbSJunLin Chen         return;
1935031514fbSJunLin Chen     }
1936031514fbSJunLin Chen 
1937e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
1938e9cc5172SEd Tanous         redfish::getUserPrivileges(req.userRole);
1939e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
1940e9cc5172SEd Tanous     bool userHasConfigureUsers =
1941e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
1942e9cc5172SEd Tanous     if (userHasConfigureUsers)
1943e9cc5172SEd Tanous     {
1944e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
19451ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "UserName",
19461ef4c342SEd Tanous                                       newUserName, "Password", password,
19471ef4c342SEd Tanous                                       "RoleId", roleId, "Enabled", enabled,
19481ef4c342SEd Tanous                                       "Locked", locked))
1949a840879dSEd Tanous         {
1950a840879dSEd Tanous             return;
1951a840879dSEd Tanous         }
1952e9cc5172SEd Tanous     }
1953e9cc5172SEd Tanous     else
1954900f9497SJoseph Reynolds     {
1955e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
1956e9cc5172SEd Tanous         if (username != req.session->username)
1957900f9497SJoseph Reynolds         {
1958900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1959900f9497SJoseph Reynolds             return;
1960900f9497SJoseph Reynolds         }
1961031514fbSJunLin Chen 
1962e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
19631ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
19641ef4c342SEd Tanous                                       password))
1965e9cc5172SEd Tanous         {
1966e9cc5172SEd Tanous             return;
1967e9cc5172SEd Tanous         }
1968900f9497SJoseph Reynolds     }
1969900f9497SJoseph Reynolds 
197066b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
19716c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
19726c51eab1SEd Tanous     // updating user properties other then username. If username
19736c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
19746c51eab1SEd Tanous     // user rename request.
197566b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
1976a840879dSEd Tanous     {
19771ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
19781ef4c342SEd Tanous                              locked);
197984e12cb7SAppaRao Puli         return;
198084e12cb7SAppaRao Puli     }
198184e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
19826c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
19831ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
198459d494eeSPatrick Williams          locked](const boost::system::error_code ec, sdbusplus::message_t& m) {
198584e12cb7SAppaRao Puli         if (ec)
198684e12cb7SAppaRao Puli         {
1987002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
1988002d39b4SEd Tanous                                     username);
1989a840879dSEd Tanous             return;
1990a840879dSEd Tanous         }
1991a840879dSEd Tanous 
1992002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
1993002d39b4SEd Tanous                              locked);
199484e12cb7SAppaRao Puli         },
19951ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
199684e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
199784e12cb7SAppaRao Puli         *newUserName);
19981ef4c342SEd Tanous }
19991ef4c342SEd Tanous 
20001ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
20011ef4c342SEd Tanous {
20021ef4c342SEd Tanous 
20031ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20044c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
20054c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20064c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
20074c7d4d33SEd Tanous 
20084c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20091ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
20101ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20111ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
20121ef4c342SEd Tanous 
20131ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20141ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
20151ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20161ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
20171ef4c342SEd Tanous 
20181ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20194c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
20204c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20214c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
20224c7d4d33SEd Tanous 
20234c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20241ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
20251ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20261ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
20271ef4c342SEd Tanous 
20281ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20291ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
20301ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
20311ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
20321ef4c342SEd Tanous 
20331ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20344c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
20354c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20364c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
20374c7d4d33SEd Tanous 
20384c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20391ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
20401ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20411ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
20421ef4c342SEd Tanous 
20431ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20441ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
20451ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
20461ef4c342SEd Tanous         // yet
20471ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
20481ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20491ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
205084e12cb7SAppaRao Puli 
20516c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2052ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
20536c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
20541ef4c342SEd Tanous             std::bind_front(handleAccounttDelete, std::ref(app)));
205506e086d9SEd Tanous }
205688d16c9aSLewanczyk, Dawid 
205788d16c9aSLewanczyk, Dawid } // namespace redfish
2058