xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 8ece0e457ee994e54c23ee7393fbce831d81a954)
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 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_utility.hpp"
203ccb3adbSEd Tanous #include "error_messages.hpp"
210ec8b83dSEd Tanous #include "generated/enums/account_service.hpp"
223ccb3adbSEd Tanous #include "openbmc_dbus_rest.hpp"
233ccb3adbSEd Tanous #include "persistent_data.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
250ec8b83dSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
280ec8b83dSEd Tanous 
291e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
30d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
311214b7e7SGunnar Mills 
322b73119cSGeorge Liu #include <array>
33c7229815SAbhishek Patel #include <optional>
343544d2a7SEd Tanous #include <ranges>
35c7229815SAbhishek Patel #include <string>
362b73119cSGeorge Liu #include <string_view>
37c7229815SAbhishek Patel #include <vector>
38c7229815SAbhishek Patel 
391abe55efSEd Tanous namespace redfish
401abe55efSEd Tanous {
4188d16c9aSLewanczyk, Dawid 
4223a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
436973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
442c70f800SEd Tanous constexpr const char* adConfigObject =
45ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
46ab828d7cSRatan Gupta 
47b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
486973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
496973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
506973a582SRatan Gupta constexpr const char* ldapConfigInterface =
516973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
526973a582SRatan Gupta constexpr const char* ldapCreateInterface =
536973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
546973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
5506785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
5606785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
576973a582SRatan Gupta 
5854fc587aSNagaraju Goruganti struct LDAPRoleMapData
5954fc587aSNagaraju Goruganti {
6054fc587aSNagaraju Goruganti     std::string groupName;
6154fc587aSNagaraju Goruganti     std::string privilege;
6254fc587aSNagaraju Goruganti };
6354fc587aSNagaraju Goruganti 
646973a582SRatan Gupta struct LDAPConfigData
656973a582SRatan Gupta {
666973a582SRatan Gupta     std::string uri{};
676973a582SRatan Gupta     std::string bindDN{};
686973a582SRatan Gupta     std::string baseDN{};
696973a582SRatan Gupta     std::string searchScope{};
706973a582SRatan Gupta     std::string serverType{};
716973a582SRatan Gupta     bool serviceEnabled = false;
726973a582SRatan Gupta     std::string userNameAttribute{};
736973a582SRatan Gupta     std::string groupAttribute{};
7454fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
756973a582SRatan Gupta };
766973a582SRatan Gupta 
7754fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7884e12cb7SAppaRao Puli {
7984e12cb7SAppaRao Puli     if (role == "priv-admin")
8084e12cb7SAppaRao Puli     {
8184e12cb7SAppaRao Puli         return "Administrator";
8284e12cb7SAppaRao Puli     }
833174e4dfSEd Tanous     if (role == "priv-user")
8484e12cb7SAppaRao Puli     {
85c80fee55SAppaRao Puli         return "ReadOnly";
8684e12cb7SAppaRao Puli     }
873174e4dfSEd Tanous     if (role == "priv-operator")
8884e12cb7SAppaRao Puli     {
8984e12cb7SAppaRao Puli         return "Operator";
9084e12cb7SAppaRao Puli     }
9184e12cb7SAppaRao Puli     return "";
9284e12cb7SAppaRao Puli }
9354fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9484e12cb7SAppaRao Puli {
9584e12cb7SAppaRao Puli     if (role == "Administrator")
9684e12cb7SAppaRao Puli     {
9784e12cb7SAppaRao Puli         return "priv-admin";
9884e12cb7SAppaRao Puli     }
993174e4dfSEd Tanous     if (role == "ReadOnly")
10084e12cb7SAppaRao Puli     {
10184e12cb7SAppaRao Puli         return "priv-user";
10284e12cb7SAppaRao Puli     }
1033174e4dfSEd Tanous     if (role == "Operator")
10484e12cb7SAppaRao Puli     {
10584e12cb7SAppaRao Puli         return "priv-operator";
10684e12cb7SAppaRao Puli     }
10784e12cb7SAppaRao Puli     return "";
10884e12cb7SAppaRao Puli }
109b9b2e0b2SEd Tanous 
110c7229815SAbhishek Patel /**
111c7229815SAbhishek Patel  * @brief Maps user group names retrieved from D-Bus object to
112c7229815SAbhishek Patel  * Account Types.
113c7229815SAbhishek Patel  *
114c7229815SAbhishek Patel  * @param[in] userGroups List of User groups
115c7229815SAbhishek Patel  * @param[out] res AccountTypes populated
116c7229815SAbhishek Patel  *
117c7229815SAbhishek Patel  * @return true in case of success, false if UserGroups contains
118c7229815SAbhishek Patel  * invalid group name(s).
119c7229815SAbhishek Patel  */
120c7229815SAbhishek Patel inline bool translateUserGroup(const std::vector<std::string>& userGroups,
121c7229815SAbhishek Patel                                crow::Response& res)
122c7229815SAbhishek Patel {
123c7229815SAbhishek Patel     std::vector<std::string> accountTypes;
124c7229815SAbhishek Patel     for (const auto& userGroup : userGroups)
125c7229815SAbhishek Patel     {
126c7229815SAbhishek Patel         if (userGroup == "redfish")
127c7229815SAbhishek Patel         {
128c7229815SAbhishek Patel             accountTypes.emplace_back("Redfish");
129c7229815SAbhishek Patel             accountTypes.emplace_back("WebUI");
130c7229815SAbhishek Patel         }
131c7229815SAbhishek Patel         else if (userGroup == "ipmi")
132c7229815SAbhishek Patel         {
133c7229815SAbhishek Patel             accountTypes.emplace_back("IPMI");
134c7229815SAbhishek Patel         }
135c7229815SAbhishek Patel         else if (userGroup == "ssh")
136c7229815SAbhishek Patel         {
137c7229815SAbhishek Patel             accountTypes.emplace_back("ManagerConsole");
138c7229815SAbhishek Patel         }
1393e72c202SNinad Palsule         else if (userGroup == "hostconsole")
1403e72c202SNinad Palsule         {
1413e72c202SNinad Palsule             // The hostconsole group controls who can access the host console
1423e72c202SNinad Palsule             // port via ssh and websocket.
1433e72c202SNinad Palsule             accountTypes.emplace_back("HostConsole");
1443e72c202SNinad Palsule         }
145c7229815SAbhishek Patel         else if (userGroup == "web")
146c7229815SAbhishek Patel         {
147c7229815SAbhishek Patel             // 'web' is one of the valid groups in the UserGroups property of
148c7229815SAbhishek Patel             // the user account in the D-Bus object. This group is currently not
149c7229815SAbhishek Patel             // doing anything, and is considered to be equivalent to 'redfish'.
150c7229815SAbhishek Patel             // 'redfish' user group is mapped to 'Redfish'and 'WebUI'
151c7229815SAbhishek Patel             // AccountTypes, so do nothing here...
152c7229815SAbhishek Patel         }
153c7229815SAbhishek Patel         else
154c7229815SAbhishek Patel         {
155*8ece0e45SEd Tanous             // Invalid user group name. Caller throws an exception.
156c7229815SAbhishek Patel             return false;
157c7229815SAbhishek Patel         }
158c7229815SAbhishek Patel     }
159c7229815SAbhishek Patel 
160c7229815SAbhishek Patel     res.jsonValue["AccountTypes"] = std::move(accountTypes);
161c7229815SAbhishek Patel     return true;
162c7229815SAbhishek Patel }
163c7229815SAbhishek Patel 
16458345856SAbhishek Patel /**
16558345856SAbhishek Patel  * @brief Builds User Groups from the Account Types
16658345856SAbhishek Patel  *
16758345856SAbhishek Patel  * @param[in] asyncResp Async Response
16858345856SAbhishek Patel  * @param[in] accountTypes List of Account Types
16958345856SAbhishek Patel  * @param[out] userGroups List of User Groups mapped from Account Types
17058345856SAbhishek Patel  *
17158345856SAbhishek Patel  * @return true if Account Types mapped to User Groups, false otherwise.
17258345856SAbhishek Patel  */
17358345856SAbhishek Patel inline bool
17458345856SAbhishek Patel     getUserGroupFromAccountType(crow::Response& res,
17558345856SAbhishek Patel                                 const std::vector<std::string>& accountTypes,
17658345856SAbhishek Patel                                 std::vector<std::string>& userGroups)
17758345856SAbhishek Patel {
17858345856SAbhishek Patel     // Need both Redfish and WebUI Account Types to map to 'redfish' User Group
17958345856SAbhishek Patel     bool redfishType = false;
18058345856SAbhishek Patel     bool webUIType = false;
18158345856SAbhishek Patel 
18258345856SAbhishek Patel     for (const auto& accountType : accountTypes)
18358345856SAbhishek Patel     {
18458345856SAbhishek Patel         if (accountType == "Redfish")
18558345856SAbhishek Patel         {
18658345856SAbhishek Patel             redfishType = true;
18758345856SAbhishek Patel         }
18858345856SAbhishek Patel         else if (accountType == "WebUI")
18958345856SAbhishek Patel         {
19058345856SAbhishek Patel             webUIType = true;
19158345856SAbhishek Patel         }
19258345856SAbhishek Patel         else if (accountType == "IPMI")
19358345856SAbhishek Patel         {
19458345856SAbhishek Patel             userGroups.emplace_back("ipmi");
19558345856SAbhishek Patel         }
19658345856SAbhishek Patel         else if (accountType == "HostConsole")
19758345856SAbhishek Patel         {
19858345856SAbhishek Patel             userGroups.emplace_back("hostconsole");
19958345856SAbhishek Patel         }
20058345856SAbhishek Patel         else if (accountType == "ManagerConsole")
20158345856SAbhishek Patel         {
20258345856SAbhishek Patel             userGroups.emplace_back("ssh");
20358345856SAbhishek Patel         }
20458345856SAbhishek Patel         else
20558345856SAbhishek Patel         {
20658345856SAbhishek Patel             // Invalid Account Type
20758345856SAbhishek Patel             messages::propertyValueNotInList(res, "AccountTypes", accountType);
20858345856SAbhishek Patel             return false;
20958345856SAbhishek Patel         }
21058345856SAbhishek Patel     }
21158345856SAbhishek Patel 
21258345856SAbhishek Patel     // Both  Redfish and WebUI Account Types are needed to PATCH
21358345856SAbhishek Patel     if (redfishType ^ webUIType)
21458345856SAbhishek Patel     {
21562598e31SEd Tanous         BMCWEB_LOG_ERROR(
21662598e31SEd Tanous             "Missing Redfish or WebUI Account Type to set redfish User Group");
21758345856SAbhishek Patel         messages::strictAccountTypes(res, "AccountTypes");
21858345856SAbhishek Patel         return false;
21958345856SAbhishek Patel     }
22058345856SAbhishek Patel 
22158345856SAbhishek Patel     if (redfishType && webUIType)
22258345856SAbhishek Patel     {
22358345856SAbhishek Patel         userGroups.emplace_back("redfish");
22458345856SAbhishek Patel     }
22558345856SAbhishek Patel 
22658345856SAbhishek Patel     return true;
22758345856SAbhishek Patel }
22858345856SAbhishek Patel 
22958345856SAbhishek Patel /**
23058345856SAbhishek Patel  * @brief Sets UserGroups property of the user based on the Account Types
23158345856SAbhishek Patel  *
23258345856SAbhishek Patel  * @param[in] accountTypes List of User Account Types
23358345856SAbhishek Patel  * @param[in] asyncResp Async Response
23458345856SAbhishek Patel  * @param[in] dbusObjectPath D-Bus Object Path
23558345856SAbhishek Patel  * @param[in] userSelf true if User is updating OWN Account Types
23658345856SAbhishek Patel  */
23758345856SAbhishek Patel inline void
23858345856SAbhishek Patel     patchAccountTypes(const std::vector<std::string>& accountTypes,
23958345856SAbhishek Patel                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24058345856SAbhishek Patel                       const std::string& dbusObjectPath, bool userSelf)
24158345856SAbhishek Patel {
24258345856SAbhishek Patel     // Check if User is disabling own Redfish Account Type
24358345856SAbhishek Patel     if (userSelf &&
24458345856SAbhishek Patel         (accountTypes.cend() ==
24558345856SAbhishek Patel          std::find(accountTypes.cbegin(), accountTypes.cend(), "Redfish")))
24658345856SAbhishek Patel     {
24762598e31SEd Tanous         BMCWEB_LOG_ERROR(
24862598e31SEd Tanous             "User disabling OWN Redfish Account Type is not allowed");
24958345856SAbhishek Patel         messages::strictAccountTypes(asyncResp->res, "AccountTypes");
25058345856SAbhishek Patel         return;
25158345856SAbhishek Patel     }
25258345856SAbhishek Patel 
25358345856SAbhishek Patel     std::vector<std::string> updatedUserGroups;
25458345856SAbhishek Patel     if (!getUserGroupFromAccountType(asyncResp->res, accountTypes,
25558345856SAbhishek Patel                                      updatedUserGroups))
25658345856SAbhishek Patel     {
25758345856SAbhishek Patel         // Problem in mapping Account Types to User Groups, Error already
25858345856SAbhishek Patel         // logged.
25958345856SAbhishek Patel         return;
26058345856SAbhishek Patel     }
26158345856SAbhishek Patel 
2629ae226faSGeorge Liu     sdbusplus::asio::setProperty(
2639ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
2649ae226faSGeorge Liu         dbusObjectPath, "xyz.openbmc_project.User.Attributes", "UserGroups",
2659ae226faSGeorge Liu         updatedUserGroups, [asyncResp](const boost::system::error_code& ec) {
26658345856SAbhishek Patel         if (ec)
26758345856SAbhishek Patel         {
26862598e31SEd Tanous             BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
26958345856SAbhishek Patel             messages::internalError(asyncResp->res);
27058345856SAbhishek Patel             return;
27158345856SAbhishek Patel         }
27258345856SAbhishek Patel         messages::success(asyncResp->res);
2739ae226faSGeorge Liu     });
27458345856SAbhishek Patel }
27558345856SAbhishek Patel 
2768d1b46d7Szhanghch05 inline void userErrorMessageHandler(
2778d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2788d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
27966b5ca76Sjayaprakash Mutyala {
28066b5ca76Sjayaprakash Mutyala     if (e == nullptr)
28166b5ca76Sjayaprakash Mutyala     {
28266b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
28366b5ca76Sjayaprakash Mutyala         return;
28466b5ca76Sjayaprakash Mutyala     }
28566b5ca76Sjayaprakash Mutyala 
286055806b3SManojkiran Eda     const char* errorMessage = e->name;
28766b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
28866b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
28966b5ca76Sjayaprakash Mutyala     {
290d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
29166b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
29266b5ca76Sjayaprakash Mutyala     }
29366b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
29466b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
29566b5ca76Sjayaprakash Mutyala     {
296d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
29766b5ca76Sjayaprakash Mutyala     }
298d4d25793SEd Tanous     else if ((strcmp(errorMessage,
299d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
300d4d25793SEd Tanous               0) ||
3010fda0f12SGeorge Liu              (strcmp(
3020fda0f12SGeorge Liu                   errorMessage,
3030fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
3040fda0f12SGeorge Liu               0))
30566b5ca76Sjayaprakash Mutyala     {
30666b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
30766b5ca76Sjayaprakash Mutyala     }
30866b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
30966b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
31066b5ca76Sjayaprakash Mutyala     {
31166b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
31266b5ca76Sjayaprakash Mutyala     }
31366b5ca76Sjayaprakash Mutyala     else
31466b5ca76Sjayaprakash Mutyala     {
315b8ad583fSGunnar Mills         BMCWEB_LOG_ERROR("DBUS response error {}", errorMessage);
31666b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
31766b5ca76Sjayaprakash Mutyala     }
31866b5ca76Sjayaprakash Mutyala }
31966b5ca76Sjayaprakash Mutyala 
32081ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
321ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
322ab828d7cSRatan Gupta                                 const std::string& ldapType)
3236973a582SRatan Gupta {
32489492a15SPatrick Williams     std::string service = (ldapType == "LDAP") ? "LDAPService"
32589492a15SPatrick Williams                                                : "ActiveDirectoryService";
32654fc587aSNagaraju Goruganti 
3271476687dSEd Tanous     nlohmann::json& ldap = jsonResponse[ldapType];
32854fc587aSNagaraju Goruganti 
3291476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
3301476687dSEd Tanous     ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri});
3310ec8b83dSEd Tanous     ldap["Authentication"]["AuthenticationType"] =
3320ec8b83dSEd Tanous         account_service::AuthenticationTypes::UsernameAndPassword;
3331476687dSEd Tanous     ldap["Authentication"]["Username"] = confData.bindDN;
3341476687dSEd Tanous     ldap["Authentication"]["Password"] = nullptr;
3351476687dSEd Tanous 
3361476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] =
3371476687dSEd Tanous         nlohmann::json::array({confData.baseDN});
3381476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] =
3391476687dSEd Tanous         confData.userNameAttribute;
3401476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] =
3411476687dSEd Tanous         confData.groupAttribute;
3421476687dSEd Tanous 
3431476687dSEd Tanous     nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"];
34454fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
3459eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
34654fc587aSNagaraju Goruganti     {
34762598e31SEd Tanous         BMCWEB_LOG_DEBUG("Pushing the data groupName={}", obj.second.groupName);
348613dabeaSEd Tanous 
349613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
350613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
351329f0348SJorge Cisneros         remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
352329f0348SJorge Cisneros         roleMapArray.emplace_back(std::move(remoteGroup));
35354fc587aSNagaraju Goruganti     }
3546973a582SRatan Gupta }
3556973a582SRatan Gupta 
3566973a582SRatan Gupta /**
35706785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
35806785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
35906785244SRatan Gupta  *
36006785244SRatan Gupta  */
36123a21a1cSEd Tanous inline void handleRoleMapPatch(
3628d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36306785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
364f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
36506785244SRatan Gupta {
36606785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
36706785244SRatan Gupta     {
368f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
36906785244SRatan Gupta 
37006785244SRatan Gupta         if (thisJson.is_null())
37106785244SRatan Gupta         {
37206785244SRatan Gupta             // delete the existing object
37306785244SRatan Gupta             if (index < roleMapObjData.size())
37406785244SRatan Gupta             {
37506785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
37606785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
3775e7e2dc5SEd Tanous                      index](const boost::system::error_code& ec) {
37806785244SRatan Gupta                     if (ec)
37906785244SRatan Gupta                     {
38062598e31SEd Tanous                         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
38106785244SRatan Gupta                         messages::internalError(asyncResp->res);
38206785244SRatan Gupta                         return;
38306785244SRatan Gupta                     }
38489492a15SPatrick Williams                     asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"]
38589492a15SPatrick Williams                                             [index] = nullptr;
38606785244SRatan Gupta                 },
38706785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
38806785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
38906785244SRatan Gupta             }
39006785244SRatan Gupta             else
39106785244SRatan Gupta             {
39262598e31SEd Tanous                 BMCWEB_LOG_ERROR("Can't delete the object");
3932e8c4bdaSEd Tanous                 messages::propertyValueTypeError(asyncResp->res, thisJson,
3942e8c4bdaSEd Tanous                                                  "RemoteRoleMapping/" +
3952e8c4bdaSEd Tanous                                                      std::to_string(index));
39606785244SRatan Gupta                 return;
39706785244SRatan Gupta             }
39806785244SRatan Gupta         }
39906785244SRatan Gupta         else if (thisJson.empty())
40006785244SRatan Gupta         {
40106785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
40206785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
40306785244SRatan Gupta         }
40406785244SRatan Gupta         else
40506785244SRatan Gupta         {
40606785244SRatan Gupta             // update/create the object
40706785244SRatan Gupta             std::optional<std::string> remoteGroup;
40806785244SRatan Gupta             std::optional<std::string> localRole;
40906785244SRatan Gupta 
410f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
411f23b7296SEd Tanous             // readJson is structured
412f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
413f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
414f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
415f23b7296SEd Tanous                                      localRole))
41606785244SRatan Gupta             {
41706785244SRatan Gupta                 continue;
41806785244SRatan Gupta             }
41906785244SRatan Gupta 
42006785244SRatan Gupta             // Update existing RoleMapping Object
42106785244SRatan Gupta             if (index < roleMapObjData.size())
42206785244SRatan Gupta             {
42362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Update Role Map Object");
42406785244SRatan Gupta                 // If "RemoteGroup" info is provided
42506785244SRatan Gupta                 if (remoteGroup)
42606785244SRatan Gupta                 {
4279ae226faSGeorge Liu                     sdbusplus::asio::setProperty(
4289ae226faSGeorge Liu                         *crow::connections::systemBus, ldapDbusService,
4299ae226faSGeorge Liu                         roleMapObjData[index].first,
4309ae226faSGeorge Liu                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
4319ae226faSGeorge Liu                         "GroupName", *remoteGroup,
43206785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
43325e055a3SRavi Teja                          remoteGroup](const boost::system::error_code& ec,
4347a543894SPatrick Williams                                       const sdbusplus::message_t& msg) {
43506785244SRatan Gupta                         if (ec)
43606785244SRatan Gupta                         {
43725e055a3SRavi Teja                             const sd_bus_error* dbusError = msg.get_error();
43825e055a3SRavi Teja                             if ((dbusError != nullptr) &&
43925e055a3SRavi Teja                                 (dbusError->name ==
44025e055a3SRavi Teja                                  std::string_view(
44125e055a3SRavi Teja                                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
44225e055a3SRavi Teja                             {
44362598e31SEd Tanous                                 BMCWEB_LOG_WARNING("DBUS response error: {}",
44462598e31SEd Tanous                                                    ec);
44525e055a3SRavi Teja                                 messages::propertyValueIncorrect(asyncResp->res,
44625e055a3SRavi Teja                                                                  "RemoteGroup",
44725e055a3SRavi Teja                                                                  *remoteGroup);
44825e055a3SRavi Teja                                 return;
44925e055a3SRavi Teja                             }
45006785244SRatan Gupta                             messages::internalError(asyncResp->res);
45106785244SRatan Gupta                             return;
45206785244SRatan Gupta                         }
45306785244SRatan Gupta                         asyncResp->res
454002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
455002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
4569ae226faSGeorge Liu                     });
45706785244SRatan Gupta                 }
45806785244SRatan Gupta 
45906785244SRatan Gupta                 // If "LocalRole" info is provided
46006785244SRatan Gupta                 if (localRole)
46106785244SRatan Gupta                 {
4629ae226faSGeorge Liu                     sdbusplus::asio::setProperty(
4639ae226faSGeorge Liu                         *crow::connections::systemBus, ldapDbusService,
4649ae226faSGeorge Liu                         roleMapObjData[index].first,
4659ae226faSGeorge Liu                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
4669ae226faSGeorge Liu                         "Privilege", *localRole,
46706785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
46825e055a3SRavi Teja                          localRole](const boost::system::error_code& ec,
4697a543894SPatrick Williams                                     const sdbusplus::message_t& msg) {
47006785244SRatan Gupta                         if (ec)
47106785244SRatan Gupta                         {
47225e055a3SRavi Teja                             const sd_bus_error* dbusError = msg.get_error();
47325e055a3SRavi Teja                             if ((dbusError != nullptr) &&
47425e055a3SRavi Teja                                 (dbusError->name ==
47525e055a3SRavi Teja                                  std::string_view(
47625e055a3SRavi Teja                                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
47725e055a3SRavi Teja                             {
47862598e31SEd Tanous                                 BMCWEB_LOG_WARNING("DBUS response error: {}",
47962598e31SEd Tanous                                                    ec);
48025e055a3SRavi Teja                                 messages::propertyValueIncorrect(
48125e055a3SRavi Teja                                     asyncResp->res, "LocalRole", *localRole);
48225e055a3SRavi Teja                                 return;
48325e055a3SRavi Teja                             }
48406785244SRatan Gupta                             messages::internalError(asyncResp->res);
48506785244SRatan Gupta                             return;
48606785244SRatan Gupta                         }
48706785244SRatan Gupta                         asyncResp->res
488002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
489002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
4909ae226faSGeorge Liu                     });
49106785244SRatan Gupta                 }
49206785244SRatan Gupta             }
49306785244SRatan Gupta             // Create a new RoleMapping Object.
49406785244SRatan Gupta             else
49506785244SRatan Gupta             {
49662598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
49762598e31SEd Tanous                     "setRoleMappingProperties: Creating new Object");
49889492a15SPatrick Williams                 std::string pathString = "RemoteRoleMapping/" +
49989492a15SPatrick Williams                                          std::to_string(index);
50006785244SRatan Gupta 
50106785244SRatan Gupta                 if (!localRole)
50206785244SRatan Gupta                 {
50306785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
50406785244SRatan Gupta                                               pathString + "/LocalRole");
50506785244SRatan Gupta                     continue;
50606785244SRatan Gupta                 }
50706785244SRatan Gupta                 if (!remoteGroup)
50806785244SRatan Gupta                 {
50906785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
51006785244SRatan Gupta                                               pathString + "/RemoteGroup");
51106785244SRatan Gupta                     continue;
51206785244SRatan Gupta                 }
51306785244SRatan Gupta 
51406785244SRatan Gupta                 std::string dbusObjectPath;
51506785244SRatan Gupta                 if (serverType == "ActiveDirectory")
51606785244SRatan Gupta                 {
5172c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
51806785244SRatan Gupta                 }
51906785244SRatan Gupta                 else if (serverType == "LDAP")
52006785244SRatan Gupta                 {
52123a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
52206785244SRatan Gupta                 }
52306785244SRatan Gupta 
52462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Remote Group={},LocalRole={}", *remoteGroup,
52562598e31SEd Tanous                                  *localRole);
52606785244SRatan Gupta 
52706785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
528271584abSEd Tanous                     [asyncResp, serverType, localRole,
5295e7e2dc5SEd Tanous                      remoteGroup](const boost::system::error_code& ec) {
53006785244SRatan Gupta                     if (ec)
53106785244SRatan Gupta                     {
53262598e31SEd Tanous                         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
53306785244SRatan Gupta                         messages::internalError(asyncResp->res);
53406785244SRatan Gupta                         return;
53506785244SRatan Gupta                     }
53606785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
53706785244SRatan Gupta                         asyncResp->res
53806785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
5391476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
5401476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
5411476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
542b2ba3072SPatrick Williams                     remoteRoleJson.emplace_back(std::move(roleMapEntry));
54306785244SRatan Gupta                 },
54406785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
5453174e4dfSEd Tanous                     "Create", *remoteGroup,
54606785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
54706785244SRatan Gupta             }
54806785244SRatan Gupta         }
54906785244SRatan Gupta     }
55006785244SRatan Gupta }
55106785244SRatan Gupta 
55206785244SRatan Gupta /**
5536973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
5546973a582SRatan Gupta  * into JSON
5556973a582SRatan Gupta  */
5566973a582SRatan Gupta template <typename CallbackFunc>
5576973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
5586973a582SRatan Gupta                               CallbackFunc&& callback)
5596973a582SRatan Gupta {
5602b73119cSGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
5612b73119cSGeorge Liu         ldapEnableInterface, ldapConfigInterface};
56254fc587aSNagaraju Goruganti 
5632b73119cSGeorge Liu     dbus::utility::getDbusObject(
5642b73119cSGeorge Liu         ldapConfigObjectName, interfaces,
5652b73119cSGeorge Liu         [callback, ldapType](const boost::system::error_code& ec,
566b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
56754fc587aSNagaraju Goruganti         if (ec || resp.empty())
56854fc587aSNagaraju Goruganti         {
569bf2ddedeSCarson Labrado             BMCWEB_LOG_WARNING(
57062598e31SEd Tanous                 "DBUS response error during getting of service name: {}", ec);
57123a21a1cSEd Tanous             LDAPConfigData empty{};
57223a21a1cSEd Tanous             callback(false, empty, ldapType);
57354fc587aSNagaraju Goruganti             return;
57454fc587aSNagaraju Goruganti         }
57554fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
5765eb468daSGeorge Liu         sdbusplus::message::object_path path(ldapRootObject);
5775eb468daSGeorge Liu         dbus::utility::getManagedObjects(
5785eb468daSGeorge Liu             service, path,
579002d39b4SEd Tanous             [callback,
5808b24275dSEd Tanous              ldapType](const boost::system::error_code& ec2,
581711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
5826973a582SRatan Gupta             LDAPConfigData confData{};
5838b24275dSEd Tanous             if (ec2)
5846973a582SRatan Gupta             {
585ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
586bf2ddedeSCarson Labrado                 BMCWEB_LOG_WARNING("D-Bus responses error: {}", ec2);
5876973a582SRatan Gupta                 return;
5886973a582SRatan Gupta             }
589ab828d7cSRatan Gupta 
590ab828d7cSRatan Gupta             std::string ldapDbusType;
59154fc587aSNagaraju Goruganti             std::string searchString;
59254fc587aSNagaraju Goruganti 
593ab828d7cSRatan Gupta             if (ldapType == "LDAP")
594ab828d7cSRatan Gupta             {
5950fda0f12SGeorge Liu                 ldapDbusType =
5960fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
59754fc587aSNagaraju Goruganti                 searchString = "openldap";
598ab828d7cSRatan Gupta             }
599ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
600ab828d7cSRatan Gupta             {
60154fc587aSNagaraju Goruganti                 ldapDbusType =
6020fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
60354fc587aSNagaraju Goruganti                 searchString = "active_directory";
604ab828d7cSRatan Gupta             }
605ab828d7cSRatan Gupta             else
606ab828d7cSRatan Gupta             {
60762598e31SEd Tanous                 BMCWEB_LOG_ERROR("Can't get the DbusType for the given type={}",
60862598e31SEd Tanous                                  ldapType);
609ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
610ab828d7cSRatan Gupta                 return;
611ab828d7cSRatan Gupta             }
612ab828d7cSRatan Gupta 
613ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
614ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
615ab828d7cSRatan Gupta 
6166973a582SRatan Gupta             for (const auto& object : ldapObjects)
6176973a582SRatan Gupta             {
61854fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
61954fc587aSNagaraju Goruganti                 // given type
620002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
6216973a582SRatan Gupta                 {
622ab828d7cSRatan Gupta                     continue;
623ab828d7cSRatan Gupta                 }
624ab828d7cSRatan Gupta 
6256973a582SRatan Gupta                 for (const auto& interface : object.second)
6266973a582SRatan Gupta                 {
6276973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
6286973a582SRatan Gupta                     {
6296973a582SRatan Gupta                         // rest of the properties are string.
6306973a582SRatan Gupta                         for (const auto& property : interface.second)
6316973a582SRatan Gupta                         {
6326973a582SRatan Gupta                             if (property.first == "Enabled")
6336973a582SRatan Gupta                             {
6346973a582SRatan Gupta                                 const bool* value =
6356973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
6366973a582SRatan Gupta                                 if (value == nullptr)
6376973a582SRatan Gupta                                 {
6386973a582SRatan Gupta                                     continue;
6396973a582SRatan Gupta                                 }
6406973a582SRatan Gupta                                 confData.serviceEnabled = *value;
6416973a582SRatan Gupta                                 break;
6426973a582SRatan Gupta                             }
6436973a582SRatan Gupta                         }
6446973a582SRatan Gupta                     }
6456973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
6466973a582SRatan Gupta                     {
6476973a582SRatan Gupta                         for (const auto& property : interface.second)
6486973a582SRatan Gupta                         {
649271584abSEd Tanous                             const std::string* strValue =
650002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
651271584abSEd Tanous                             if (strValue == nullptr)
6526973a582SRatan Gupta                             {
6536973a582SRatan Gupta                                 continue;
6546973a582SRatan Gupta                             }
6556973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
6566973a582SRatan Gupta                             {
657271584abSEd Tanous                                 confData.uri = *strValue;
6586973a582SRatan Gupta                             }
6596973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
6606973a582SRatan Gupta                             {
661271584abSEd Tanous                                 confData.bindDN = *strValue;
6626973a582SRatan Gupta                             }
6636973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
6646973a582SRatan Gupta                             {
665271584abSEd Tanous                                 confData.baseDN = *strValue;
6666973a582SRatan Gupta                             }
667002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
6686973a582SRatan Gupta                             {
669271584abSEd Tanous                                 confData.searchScope = *strValue;
6706973a582SRatan Gupta                             }
671002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
6726973a582SRatan Gupta                             {
673271584abSEd Tanous                                 confData.groupAttribute = *strValue;
6746973a582SRatan Gupta                             }
675002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
6766973a582SRatan Gupta                             {
677271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
6786973a582SRatan Gupta                             }
67954fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
680ab828d7cSRatan Gupta                             {
681271584abSEd Tanous                                 confData.serverType = *strValue;
68254fc587aSNagaraju Goruganti                             }
68354fc587aSNagaraju Goruganti                         }
68454fc587aSNagaraju Goruganti                     }
685002d39b4SEd Tanous                     else if (interface.first ==
6860fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
68754fc587aSNagaraju Goruganti                     {
68854fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
68954fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
69054fc587aSNagaraju Goruganti                         {
691271584abSEd Tanous                             const std::string* strValue =
692002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
69354fc587aSNagaraju Goruganti 
694271584abSEd Tanous                             if (strValue == nullptr)
69554fc587aSNagaraju Goruganti                             {
69654fc587aSNagaraju Goruganti                                 continue;
69754fc587aSNagaraju Goruganti                             }
69854fc587aSNagaraju Goruganti 
69954fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
70054fc587aSNagaraju Goruganti                             {
701271584abSEd Tanous                                 roleMapData.groupName = *strValue;
70254fc587aSNagaraju Goruganti                             }
70354fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
70454fc587aSNagaraju Goruganti                             {
705271584abSEd Tanous                                 roleMapData.privilege = *strValue;
70654fc587aSNagaraju Goruganti                             }
70754fc587aSNagaraju Goruganti                         }
70854fc587aSNagaraju Goruganti 
709002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
710002d39b4SEd Tanous                                                             roleMapData);
71154fc587aSNagaraju Goruganti                     }
71254fc587aSNagaraju Goruganti                 }
71354fc587aSNagaraju Goruganti             }
714ab828d7cSRatan Gupta             callback(true, confData, ldapType);
7155eb468daSGeorge Liu         });
7162b73119cSGeorge Liu     });
7176973a582SRatan Gupta }
7186973a582SRatan Gupta 
7198a07d286SRatan Gupta /**
7208a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
7218a07d286SRatan Gupta  * @param input JSON data
7228a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7238a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
7248a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
7258a07d286SRatan Gupta  */
7264f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
7276c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7286c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
7298a07d286SRatan Gupta {
7308a07d286SRatan Gupta     std::optional<std::string> authType;
7318a07d286SRatan Gupta 
7328a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
7338a07d286SRatan Gupta                              authType, "Username", username, "Password",
7348a07d286SRatan Gupta                              password))
7358a07d286SRatan Gupta     {
7368a07d286SRatan Gupta         return;
7378a07d286SRatan Gupta     }
7388a07d286SRatan Gupta     if (!authType)
7398a07d286SRatan Gupta     {
7408a07d286SRatan Gupta         return;
7418a07d286SRatan Gupta     }
7428a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
7438a07d286SRatan Gupta     {
7448a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
7458a07d286SRatan Gupta                                          "AuthenticationType");
7468a07d286SRatan Gupta         return;
7478a07d286SRatan Gupta     }
7488a07d286SRatan Gupta }
7498a07d286SRatan Gupta /**
7508a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
7518a07d286SRatan Gupta  * @param input JSON data
7528a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7538a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
7548a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
7558a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
7568a07d286SRatan Gupta  */
7578a07d286SRatan Gupta 
7584f48d5f6SEd Tanous inline void
7594f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
7608d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7618a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
7628a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
7638a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
7648a07d286SRatan Gupta {
7658a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
7668a07d286SRatan Gupta 
7678a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
7688a07d286SRatan Gupta                              searchSettings))
7698a07d286SRatan Gupta     {
7708a07d286SRatan Gupta         return;
7718a07d286SRatan Gupta     }
7728a07d286SRatan Gupta     if (!searchSettings)
7738a07d286SRatan Gupta     {
7748a07d286SRatan Gupta         return;
7758a07d286SRatan Gupta     }
7768a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
7778a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
7788a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
7798a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
7808a07d286SRatan Gupta     {
7818a07d286SRatan Gupta         return;
7828a07d286SRatan Gupta     }
7838a07d286SRatan Gupta }
7848a07d286SRatan Gupta /**
7858a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
7868a07d286SRatan Gupta           json response with the new value.
7878a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
7888a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7898a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7908a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7918a07d286SRatan Gupta  */
7928a07d286SRatan Gupta 
7934f48d5f6SEd Tanous inline void handleServiceAddressPatch(
7948a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
7958d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7968a07d286SRatan Gupta     const std::string& ldapServerElementName,
7978a07d286SRatan Gupta     const std::string& ldapConfigObject)
7988a07d286SRatan Gupta {
7999ae226faSGeorge Liu     sdbusplus::asio::setProperty(
8009ae226faSGeorge Liu         *crow::connections::systemBus, ldapDbusService, ldapConfigObject,
8019ae226faSGeorge Liu         ldapConfigInterface, "LDAPServerURI", serviceAddressList.front(),
8027a543894SPatrick Williams         [asyncResp, ldapServerElementName, serviceAddressList](
8037a543894SPatrick Williams             const boost::system::error_code& ec, sdbusplus::message_t& msg) {
8048a07d286SRatan Gupta         if (ec)
8058a07d286SRatan Gupta         {
80625e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
80725e055a3SRavi Teja             if ((dbusError != nullptr) &&
80825e055a3SRavi Teja                 (dbusError->name ==
80925e055a3SRavi Teja                  std::string_view(
81025e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
81125e055a3SRavi Teja             {
81262598e31SEd Tanous                 BMCWEB_LOG_WARNING(
81362598e31SEd Tanous                     "Error Occurred in updating the service address");
81425e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
81525e055a3SRavi Teja                                                  "ServiceAddresses",
81625e055a3SRavi Teja                                                  serviceAddressList.front());
81725e055a3SRavi Teja                 return;
81825e055a3SRavi Teja             }
8198a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8208a07d286SRatan Gupta             return;
8218a07d286SRatan Gupta         }
8228a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
8238a07d286SRatan Gupta             serviceAddressList.front()};
824002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
8258a07d286SRatan Gupta             modifiedserviceAddressList;
8268a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
8278a07d286SRatan Gupta         {
828002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
8298a07d286SRatan Gupta                                             serviceAddressList.front());
8308a07d286SRatan Gupta         }
83162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the service address");
8329ae226faSGeorge Liu     });
8338a07d286SRatan Gupta }
8348a07d286SRatan Gupta /**
8358a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
8368a07d286SRatan Gupta           json response with the new value.
8378a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
8388a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8398a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8408a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8418a07d286SRatan Gupta  */
8428a07d286SRatan Gupta 
8434f48d5f6SEd Tanous inline void
8444f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
8458d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8468a07d286SRatan Gupta                         const std::string& ldapServerElementName,
8478a07d286SRatan Gupta                         const std::string& ldapConfigObject)
8488a07d286SRatan Gupta {
8499ae226faSGeorge Liu     sdbusplus::asio::setProperty(*crow::connections::systemBus, ldapDbusService,
8509ae226faSGeorge Liu                                  ldapConfigObject, ldapConfigInterface,
8519ae226faSGeorge Liu                                  "LDAPBindDN", username,
8529ae226faSGeorge Liu                                  [asyncResp, username, ldapServerElementName](
8539ae226faSGeorge Liu                                      const boost::system::error_code& ec) {
8548a07d286SRatan Gupta         if (ec)
8558a07d286SRatan Gupta         {
85662598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error occurred in updating the username");
8578a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8588a07d286SRatan Gupta             return;
8598a07d286SRatan Gupta         }
86089492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
86189492a15SPatrick Williams                                 ["Username"] = username;
86262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the username");
8639ae226faSGeorge Liu     });
8648a07d286SRatan Gupta }
8658a07d286SRatan Gupta 
8668a07d286SRatan Gupta /**
8678a07d286SRatan Gupta  * @brief updates the LDAP password
8688a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
8698a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8708a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8718a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
8728a07d286SRatan Gupta  */
8738a07d286SRatan Gupta 
8744f48d5f6SEd Tanous inline void
8754f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
8768d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8778a07d286SRatan Gupta                         const std::string& ldapServerElementName,
8788a07d286SRatan Gupta                         const std::string& ldapConfigObject)
8798a07d286SRatan Gupta {
8809ae226faSGeorge Liu     sdbusplus::asio::setProperty(*crow::connections::systemBus, ldapDbusService,
8819ae226faSGeorge Liu                                  ldapConfigObject, ldapConfigInterface,
8829ae226faSGeorge Liu                                  "LDAPBindDNPassword", password,
8839ae226faSGeorge Liu                                  [asyncResp, password, ldapServerElementName](
8849ae226faSGeorge Liu                                      const boost::system::error_code& ec) {
8858a07d286SRatan Gupta         if (ec)
8868a07d286SRatan Gupta         {
88762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error occurred in updating the password");
8888a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8898a07d286SRatan Gupta             return;
8908a07d286SRatan Gupta         }
89189492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
89289492a15SPatrick Williams                                 ["Password"] = "";
89362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the password");
8949ae226faSGeorge Liu     });
8958a07d286SRatan Gupta }
8968a07d286SRatan Gupta 
8978a07d286SRatan Gupta /**
8988a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
8998a07d286SRatan Gupta           json response with the new value.
9008a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
9018a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9028a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9038a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9048a07d286SRatan Gupta  */
9058a07d286SRatan Gupta 
9064f48d5f6SEd Tanous inline void
9074f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
9088d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9098a07d286SRatan Gupta                       const std::string& ldapServerElementName,
9108a07d286SRatan Gupta                       const std::string& ldapConfigObject)
9118a07d286SRatan Gupta {
9129ae226faSGeorge Liu     sdbusplus::asio::setProperty(*crow::connections::systemBus, ldapDbusService,
9139ae226faSGeorge Liu                                  ldapConfigObject, ldapConfigInterface,
9149ae226faSGeorge Liu                                  "LDAPBaseDN", baseDNList.front(),
9159ae226faSGeorge Liu                                  [asyncResp, baseDNList, ldapServerElementName](
9169ae226faSGeorge Liu                                      const boost::system::error_code& ec,
9177a543894SPatrick Williams                                      const sdbusplus::message_t& msg) {
9188a07d286SRatan Gupta         if (ec)
9198a07d286SRatan Gupta         {
92062598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error Occurred in Updating the base DN");
92125e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
92225e055a3SRavi Teja             if ((dbusError != nullptr) &&
92325e055a3SRavi Teja                 (dbusError->name ==
92425e055a3SRavi Teja                  std::string_view(
92525e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
92625e055a3SRavi Teja             {
92725e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
92825e055a3SRavi Teja                                                  "BaseDistinguishedNames",
92925e055a3SRavi Teja                                                  baseDNList.front());
93025e055a3SRavi Teja                 return;
93125e055a3SRavi Teja             }
9328a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9338a07d286SRatan Gupta             return;
9348a07d286SRatan Gupta         }
935002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
9368a07d286SRatan Gupta         auto& searchSettingsJson =
9378a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
9386c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
9396c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
9408a07d286SRatan Gupta         if (baseDNList.size() > 1)
9418a07d286SRatan Gupta         {
942002d39b4SEd Tanous             messages::propertyValueModified(
943002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
9448a07d286SRatan Gupta         }
94562598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the base DN");
9469ae226faSGeorge Liu     });
9478a07d286SRatan Gupta }
9488a07d286SRatan Gupta /**
9498a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
9508a07d286SRatan Gupta           json response with the new value.
9518a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
9528a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9538a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9548a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9558a07d286SRatan Gupta  */
9568a07d286SRatan Gupta 
9574f48d5f6SEd Tanous inline void
9584f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
9598d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9608a07d286SRatan Gupta                             const std::string& ldapServerElementName,
9618a07d286SRatan Gupta                             const std::string& ldapConfigObject)
9628a07d286SRatan Gupta {
9639ae226faSGeorge Liu     sdbusplus::asio::setProperty(
9649ae226faSGeorge Liu         *crow::connections::systemBus, ldapDbusService, ldapConfigObject,
9659ae226faSGeorge Liu         ldapConfigInterface, "UserNameAttribute", userNameAttribute,
9668a07d286SRatan Gupta         [asyncResp, userNameAttribute,
9675e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
9688a07d286SRatan Gupta         if (ec)
9698a07d286SRatan Gupta         {
97062598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error Occurred in Updating the "
97162598e31SEd Tanous                              "username attribute");
9728a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9738a07d286SRatan Gupta             return;
9748a07d286SRatan Gupta         }
975002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
9768a07d286SRatan Gupta         auto& searchSettingsJson =
9778a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
9788a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
97962598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the user name attr.");
9809ae226faSGeorge Liu     });
9818a07d286SRatan Gupta }
9828a07d286SRatan Gupta /**
9838a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
9848a07d286SRatan Gupta           json response with the new value.
9858a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
9868a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9878a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9888a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9898a07d286SRatan Gupta  */
9908a07d286SRatan Gupta 
9914f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
9928d1b46d7Szhanghch05     const std::string& groupsAttribute,
9938d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9948a07d286SRatan Gupta     const std::string& ldapServerElementName,
9958a07d286SRatan Gupta     const std::string& ldapConfigObject)
9968a07d286SRatan Gupta {
9979ae226faSGeorge Liu     sdbusplus::asio::setProperty(
9989ae226faSGeorge Liu         *crow::connections::systemBus, ldapDbusService, ldapConfigObject,
9999ae226faSGeorge Liu         ldapConfigInterface, "GroupNameAttribute", groupsAttribute,
10008a07d286SRatan Gupta         [asyncResp, groupsAttribute,
10015e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
10028a07d286SRatan Gupta         if (ec)
10038a07d286SRatan Gupta         {
100462598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error Occurred in Updating the "
100562598e31SEd Tanous                              "groupname attribute");
10068a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10078a07d286SRatan Gupta             return;
10088a07d286SRatan Gupta         }
1009002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
10108a07d286SRatan Gupta         auto& searchSettingsJson =
10118a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
10128a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
101362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the groupname attr");
10149ae226faSGeorge Liu     });
10158a07d286SRatan Gupta }
10168a07d286SRatan Gupta /**
10178a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
10188a07d286SRatan Gupta           json response with the new value.
10198a07d286SRatan Gupta  * @param input JSON data.
10208a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
10218a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
10228a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
10238a07d286SRatan Gupta  */
10248a07d286SRatan Gupta 
10254f48d5f6SEd Tanous inline void handleServiceEnablePatch(
10266c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10278a07d286SRatan Gupta     const std::string& ldapServerElementName,
10288a07d286SRatan Gupta     const std::string& ldapConfigObject)
10298a07d286SRatan Gupta {
10309ae226faSGeorge Liu     sdbusplus::asio::setProperty(
10319ae226faSGeorge Liu         *crow::connections::systemBus, ldapDbusService, ldapConfigObject,
10329ae226faSGeorge Liu         ldapEnableInterface, "Enabled", serviceEnabled,
10338a07d286SRatan Gupta         [asyncResp, serviceEnabled,
10345e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
10358a07d286SRatan Gupta         if (ec)
10368a07d286SRatan Gupta         {
103762598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error Occurred in Updating the service enable");
10388a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10398a07d286SRatan Gupta             return;
10408a07d286SRatan Gupta         }
10416c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
10428a07d286SRatan Gupta             serviceEnabled;
104362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated Service enable = {}", serviceEnabled);
10449ae226faSGeorge Liu     });
10458a07d286SRatan Gupta }
10468a07d286SRatan Gupta 
10474f48d5f6SEd Tanous inline void
10484f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
10498d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
105078158631SZbigniew Kurzynski {
105178158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
105278158631SZbigniew Kurzynski     std::optional<bool> cookie;
105378158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
105478158631SZbigniew Kurzynski     std::optional<bool> xToken;
1055501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
105678158631SZbigniew Kurzynski 
105778158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
105878158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
1059501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
106078158631SZbigniew Kurzynski     {
106162598e31SEd Tanous         BMCWEB_LOG_ERROR("Cannot read values from AuthMethod tag");
106278158631SZbigniew Kurzynski         return;
106378158631SZbigniew Kurzynski     }
106478158631SZbigniew Kurzynski 
106578158631SZbigniew Kurzynski     // Make a copy of methods configuration
106652cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
106752cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
106878158631SZbigniew Kurzynski 
106978158631SZbigniew Kurzynski     if (basicAuth)
107078158631SZbigniew Kurzynski     {
1071f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
1072f16f6263SAlan Kuo         messages::actionNotSupported(
10730fda0f12SGeorge Liu             asyncResp->res,
10740fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
1075f16f6263SAlan Kuo         return;
1076f16f6263SAlan Kuo #endif
107778158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
107878158631SZbigniew Kurzynski     }
107978158631SZbigniew Kurzynski 
108078158631SZbigniew Kurzynski     if (cookie)
108178158631SZbigniew Kurzynski     {
1082f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
10830fda0f12SGeorge Liu         messages::actionNotSupported(
10840fda0f12SGeorge Liu             asyncResp->res,
10850fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
1086f16f6263SAlan Kuo         return;
1087f16f6263SAlan Kuo #endif
108878158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
108978158631SZbigniew Kurzynski     }
109078158631SZbigniew Kurzynski 
109178158631SZbigniew Kurzynski     if (sessionToken)
109278158631SZbigniew Kurzynski     {
1093f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
1094f16f6263SAlan Kuo         messages::actionNotSupported(
10950fda0f12SGeorge Liu             asyncResp->res,
10960fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
1097f16f6263SAlan Kuo         return;
1098f16f6263SAlan Kuo #endif
109978158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
110078158631SZbigniew Kurzynski     }
110178158631SZbigniew Kurzynski 
110278158631SZbigniew Kurzynski     if (xToken)
110378158631SZbigniew Kurzynski     {
1104f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
11050fda0f12SGeorge Liu         messages::actionNotSupported(
11060fda0f12SGeorge Liu             asyncResp->res,
11070fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
1108f16f6263SAlan Kuo         return;
1109f16f6263SAlan Kuo #endif
111078158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
111178158631SZbigniew Kurzynski     }
111278158631SZbigniew Kurzynski 
1113501f1e58SZbigniew Kurzynski     if (tls)
1114501f1e58SZbigniew Kurzynski     {
1115f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
11160fda0f12SGeorge Liu         messages::actionNotSupported(
11170fda0f12SGeorge Liu             asyncResp->res,
11180fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
1119f16f6263SAlan Kuo         return;
1120f16f6263SAlan Kuo #endif
1121501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
1122501f1e58SZbigniew Kurzynski     }
1123501f1e58SZbigniew Kurzynski 
112478158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
1125501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
1126501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
112778158631SZbigniew Kurzynski     {
112878158631SZbigniew Kurzynski         // Do not allow user to disable everything
112978158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
113078158631SZbigniew Kurzynski                                      "of disabling all available methods");
113178158631SZbigniew Kurzynski         return;
113278158631SZbigniew Kurzynski     }
113378158631SZbigniew Kurzynski 
113452cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
113552cc112dSEd Tanous         authMethodsConfig);
113678158631SZbigniew Kurzynski     // Save configuration immediately
113752cc112dSEd Tanous     persistent_data::getConfig().writeData();
113878158631SZbigniew Kurzynski 
113978158631SZbigniew Kurzynski     messages::success(asyncResp->res);
114078158631SZbigniew Kurzynski }
114178158631SZbigniew Kurzynski 
11428a07d286SRatan Gupta /**
11438a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
11448a07d286SRatan Gupta  *        value and create the LDAP config object.
11458a07d286SRatan Gupta  * @param input JSON data
11468a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
11478a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
11488a07d286SRatan Gupta  */
11498a07d286SRatan Gupta 
11506c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
11518d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
11528a07d286SRatan Gupta                             const std::string& serverType)
11538a07d286SRatan Gupta {
1154eb2bbe56SRatan Gupta     std::string dbusObjectPath;
1155eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
1156eb2bbe56SRatan Gupta     {
11572c70f800SEd Tanous         dbusObjectPath = adConfigObject;
1158eb2bbe56SRatan Gupta     }
1159eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
1160eb2bbe56SRatan Gupta     {
116123a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
1162eb2bbe56SRatan Gupta     }
1163cb13a392SEd Tanous     else
1164cb13a392SEd Tanous     {
1165cb13a392SEd Tanous         return;
1166cb13a392SEd Tanous     }
1167eb2bbe56SRatan Gupta 
11688a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
11698a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
11708a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
11718a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
11728a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
11738a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
11748a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
11758a07d286SRatan Gupta     std::optional<std::string> userName;
11768a07d286SRatan Gupta     std::optional<std::string> password;
117706785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
11788a07d286SRatan Gupta 
11798a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
11808a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
11818a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
118206785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
118306785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
11848a07d286SRatan Gupta     {
11858a07d286SRatan Gupta         return;
11868a07d286SRatan Gupta     }
11878a07d286SRatan Gupta 
11888a07d286SRatan Gupta     if (authentication)
11898a07d286SRatan Gupta     {
11908a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
11918a07d286SRatan Gupta                                     password);
11928a07d286SRatan Gupta     }
11938a07d286SRatan Gupta     if (ldapService)
11948a07d286SRatan Gupta     {
11958a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
11968a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
11978a07d286SRatan Gupta     }
11988a07d286SRatan Gupta     if (serviceAddressList)
11998a07d286SRatan Gupta     {
120026f6976fSEd Tanous         if (serviceAddressList->empty())
12018a07d286SRatan Gupta         {
1202e2616cc5SEd Tanous             messages::propertyValueNotInList(
1203e2616cc5SEd Tanous                 asyncResp->res, *serviceAddressList, "ServiceAddress");
12048a07d286SRatan Gupta             return;
12058a07d286SRatan Gupta         }
12068a07d286SRatan Gupta     }
12078a07d286SRatan Gupta     if (baseDNList)
12088a07d286SRatan Gupta     {
120926f6976fSEd Tanous         if (baseDNList->empty())
12108a07d286SRatan Gupta         {
1211e2616cc5SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *baseDNList,
12128a07d286SRatan Gupta                                              "BaseDistinguishedNames");
12138a07d286SRatan Gupta             return;
12148a07d286SRatan Gupta         }
12158a07d286SRatan Gupta     }
12168a07d286SRatan Gupta 
12178a07d286SRatan Gupta     // nothing to update, then return
12188a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
121906785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
122006785244SRatan Gupta         !remoteRoleMapData)
12218a07d286SRatan Gupta     {
12228a07d286SRatan Gupta         return;
12238a07d286SRatan Gupta     }
12248a07d286SRatan Gupta 
12258a07d286SRatan Gupta     // Get the existing resource first then keep modifying
12268a07d286SRatan Gupta     // whenever any property gets updated.
1227002d39b4SEd Tanous     getLDAPConfigData(
1228002d39b4SEd Tanous         serverType,
1229002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1230002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1231002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
123223a21a1cSEd Tanous                             const std::string& serverT) {
12338a07d286SRatan Gupta         if (!success)
12348a07d286SRatan Gupta         {
12358a07d286SRatan Gupta             messages::internalError(asyncResp->res);
12368a07d286SRatan Gupta             return;
12378a07d286SRatan Gupta         }
12386c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
12398a07d286SRatan Gupta         if (confData.serviceEnabled)
12408a07d286SRatan Gupta         {
12418a07d286SRatan Gupta             // Disable the service first and update the rest of
12428a07d286SRatan Gupta             // the properties.
12436c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
12448a07d286SRatan Gupta         }
12458a07d286SRatan Gupta 
12468a07d286SRatan Gupta         if (serviceAddressList)
12478a07d286SRatan Gupta         {
12486c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
12496c51eab1SEd Tanous                                       dbusObjectPath);
12508a07d286SRatan Gupta         }
12518a07d286SRatan Gupta         if (userName)
12528a07d286SRatan Gupta         {
12536c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
12548a07d286SRatan Gupta         }
12558a07d286SRatan Gupta         if (password)
12568a07d286SRatan Gupta         {
12576c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
12588a07d286SRatan Gupta         }
12598a07d286SRatan Gupta 
12608a07d286SRatan Gupta         if (baseDNList)
12618a07d286SRatan Gupta         {
12626c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
12638a07d286SRatan Gupta         }
12648a07d286SRatan Gupta         if (userNameAttribute)
12658a07d286SRatan Gupta         {
12666c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
12676c51eab1SEd Tanous                                     dbusObjectPath);
12688a07d286SRatan Gupta         }
12698a07d286SRatan Gupta         if (groupsAttribute)
12708a07d286SRatan Gupta         {
12716c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
12726c51eab1SEd Tanous                                      dbusObjectPath);
12738a07d286SRatan Gupta         }
12748a07d286SRatan Gupta         if (serviceEnabled)
12758a07d286SRatan Gupta         {
12768a07d286SRatan Gupta             // if user has given the value as true then enable
12778a07d286SRatan Gupta             // the service. if user has given false then no-op
12788a07d286SRatan Gupta             // as service is already stopped.
12798a07d286SRatan Gupta             if (*serviceEnabled)
12808a07d286SRatan Gupta             {
12816c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
12826c51eab1SEd Tanous                                          dbusObjectPath);
12838a07d286SRatan Gupta             }
12848a07d286SRatan Gupta         }
12858a07d286SRatan Gupta         else
12868a07d286SRatan Gupta         {
12878a07d286SRatan Gupta             // if user has not given the service enabled value
12888a07d286SRatan Gupta             // then revert it to the same state as it was
12898a07d286SRatan Gupta             // before.
12908a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
129123a21a1cSEd Tanous                                      serverT, dbusObjectPath);
12928a07d286SRatan Gupta         }
129306785244SRatan Gupta 
129406785244SRatan Gupta         if (remoteRoleMapData)
129506785244SRatan Gupta         {
12966c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
12976c51eab1SEd Tanous                                *remoteRoleMapData);
129806785244SRatan Gupta         }
12998a07d286SRatan Gupta     });
13008a07d286SRatan Gupta }
1301d4b5443fSEd Tanous 
130258345856SAbhishek Patel inline void updateUserProperties(
130358345856SAbhishek Patel     std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& username,
1304618c14b4SEd Tanous     const std::optional<std::string>& password,
1305618c14b4SEd Tanous     const std::optional<bool>& enabled,
130658345856SAbhishek Patel     const std::optional<std::string>& roleId, const std::optional<bool>& locked,
130758345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes, bool userSelf)
13081abe55efSEd Tanous {
1309b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1310b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1311b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
13126c51eab1SEd Tanous 
13136c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1314618c14b4SEd Tanous         dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
131558345856SAbhishek Patel                          locked, accountTypes(std::move(accountTypes)),
131658345856SAbhishek Patel                          userSelf, asyncResp{std::move(asyncResp)}](int rc) {
1317e662eae8SEd Tanous         if (rc <= 0)
13186c51eab1SEd Tanous         {
1319d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
13206c51eab1SEd Tanous                                        username);
13216c51eab1SEd Tanous             return;
13226c51eab1SEd Tanous         }
13236c51eab1SEd Tanous 
13246c51eab1SEd Tanous         if (password)
13256c51eab1SEd Tanous         {
13266c51eab1SEd Tanous             int retval = pamUpdatePassword(username, *password);
13276c51eab1SEd Tanous 
13286c51eab1SEd Tanous             if (retval == PAM_USER_UNKNOWN)
13296c51eab1SEd Tanous             {
1330d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
13316c51eab1SEd Tanous                                            username);
13326c51eab1SEd Tanous             }
13336c51eab1SEd Tanous             else if (retval == PAM_AUTHTOK_ERR)
13346c51eab1SEd Tanous             {
13356c51eab1SEd Tanous                 // If password is invalid
13369bd80831SJason M. Bills                 messages::propertyValueFormatError(asyncResp->res, nullptr,
13379bd80831SJason M. Bills                                                    "Password");
133862598e31SEd Tanous                 BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
13396c51eab1SEd Tanous             }
13406c51eab1SEd Tanous             else if (retval != PAM_SUCCESS)
13416c51eab1SEd Tanous             {
13426c51eab1SEd Tanous                 messages::internalError(asyncResp->res);
13436c51eab1SEd Tanous                 return;
13446c51eab1SEd Tanous             }
1345e7b1b62bSEd Tanous             else
1346e7b1b62bSEd Tanous             {
1347e7b1b62bSEd Tanous                 messages::success(asyncResp->res);
1348e7b1b62bSEd Tanous             }
13496c51eab1SEd Tanous         }
13506c51eab1SEd Tanous 
13516c51eab1SEd Tanous         if (enabled)
13526c51eab1SEd Tanous         {
13539ae226faSGeorge Liu             sdbusplus::asio::setProperty(
13549ae226faSGeorge Liu                 *crow::connections::systemBus,
13559ae226faSGeorge Liu                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
13565a39f77aSPatrick Williams                 "xyz.openbmc_project.User.Attributes", "UserEnabled", *enabled,
13575a39f77aSPatrick Williams                 [asyncResp](const boost::system::error_code& ec) {
13586c51eab1SEd Tanous                 if (ec)
13596c51eab1SEd Tanous                 {
136062598e31SEd Tanous                     BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
13616c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
13626c51eab1SEd Tanous                     return;
13636c51eab1SEd Tanous                 }
13646c51eab1SEd Tanous                 messages::success(asyncResp->res);
13659ae226faSGeorge Liu             });
13666c51eab1SEd Tanous         }
13676c51eab1SEd Tanous 
13686c51eab1SEd Tanous         if (roleId)
13696c51eab1SEd Tanous         {
13706c51eab1SEd Tanous             std::string priv = getPrivilegeFromRoleId(*roleId);
13716c51eab1SEd Tanous             if (priv.empty())
13726c51eab1SEd Tanous             {
1373e2616cc5SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, true,
1374e2616cc5SEd Tanous                                                  "Locked");
13756c51eab1SEd Tanous                 return;
13766c51eab1SEd Tanous             }
13776c51eab1SEd Tanous 
13789ae226faSGeorge Liu             sdbusplus::asio::setProperty(
13799ae226faSGeorge Liu                 *crow::connections::systemBus,
13809ae226faSGeorge Liu                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
13815a39f77aSPatrick Williams                 "xyz.openbmc_project.User.Attributes", "UserPrivilege", priv,
13825a39f77aSPatrick Williams                 [asyncResp](const boost::system::error_code& ec) {
13836c51eab1SEd Tanous                 if (ec)
13846c51eab1SEd Tanous                 {
138562598e31SEd Tanous                     BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
13866c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
13876c51eab1SEd Tanous                     return;
13886c51eab1SEd Tanous                 }
13896c51eab1SEd Tanous                 messages::success(asyncResp->res);
13909ae226faSGeorge Liu             });
13916c51eab1SEd Tanous         }
13926c51eab1SEd Tanous 
13936c51eab1SEd Tanous         if (locked)
13946c51eab1SEd Tanous         {
13956c51eab1SEd Tanous             // admin can unlock the account which is locked by
13966c51eab1SEd Tanous             // successive authentication failures but admin should
13976c51eab1SEd Tanous             // not be allowed to lock an account.
13986c51eab1SEd Tanous             if (*locked)
13996c51eab1SEd Tanous             {
14006c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, "true",
14016c51eab1SEd Tanous                                                  "Locked");
14026c51eab1SEd Tanous                 return;
14036c51eab1SEd Tanous             }
14046c51eab1SEd Tanous 
14059ae226faSGeorge Liu             sdbusplus::asio::setProperty(
14069ae226faSGeorge Liu                 *crow::connections::systemBus,
14079ae226faSGeorge Liu                 "xyz.openbmc_project.User.Manager", dbusObjectPath,
14089ae226faSGeorge Liu                 "xyz.openbmc_project.User.Attributes",
14099ae226faSGeorge Liu                 "UserLockedForFailedAttempt", *locked,
14105e7e2dc5SEd Tanous                 [asyncResp](const boost::system::error_code& ec) {
14116c51eab1SEd Tanous                 if (ec)
14126c51eab1SEd Tanous                 {
141362598e31SEd Tanous                     BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
14146c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
14156c51eab1SEd Tanous                     return;
14166c51eab1SEd Tanous                 }
14176c51eab1SEd Tanous                 messages::success(asyncResp->res);
14189ae226faSGeorge Liu             });
14196c51eab1SEd Tanous         }
142058345856SAbhishek Patel 
142158345856SAbhishek Patel         if (accountTypes)
142258345856SAbhishek Patel         {
142358345856SAbhishek Patel             patchAccountTypes(*accountTypes, asyncResp, dbusObjectPath,
142458345856SAbhishek Patel                               userSelf);
142558345856SAbhishek Patel         }
14266c51eab1SEd Tanous     });
14276c51eab1SEd Tanous }
14286c51eab1SEd Tanous 
14294c7d4d33SEd Tanous inline void handleAccountServiceHead(
14304c7d4d33SEd Tanous     App& app, const crow::Request& req,
14311ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14326c51eab1SEd Tanous {
14333ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
143445ca1b86SEd Tanous     {
143545ca1b86SEd Tanous         return;
143645ca1b86SEd Tanous     }
14374c7d4d33SEd Tanous     asyncResp->res.addHeader(
14384c7d4d33SEd Tanous         boost::beast::http::field::link,
14394c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
14404c7d4d33SEd Tanous }
14414c7d4d33SEd Tanous 
14424c7d4d33SEd Tanous inline void
14434c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
14444c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14454c7d4d33SEd Tanous {
1446afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1447afd369c6SJiaqing Zhao     {
1448afd369c6SJiaqing Zhao         return;
1449afd369c6SJiaqing Zhao     }
14503e72c202SNinad Palsule 
14513e72c202SNinad Palsule     if (req.session == nullptr)
14523e72c202SNinad Palsule     {
14533e72c202SNinad Palsule         messages::internalError(asyncResp->res);
14543e72c202SNinad Palsule         return;
14553e72c202SNinad Palsule     }
14563e72c202SNinad Palsule 
1457afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1458afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1459afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1460afd369c6SJiaqing Zhao 
146152cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
14621ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
146378158631SZbigniew Kurzynski 
14641476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
14651476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
14661476687dSEd Tanous     json["@odata.type"] = "#AccountService."
14671476687dSEd Tanous                           "v1_10_0.AccountService";
14681476687dSEd Tanous     json["Id"] = "AccountService";
14691476687dSEd Tanous     json["Name"] = "Account Service";
14701476687dSEd Tanous     json["Description"] = "Account Service";
14711476687dSEd Tanous     json["ServiceEnabled"] = true;
14721476687dSEd Tanous     json["MaxPasswordLength"] = 20;
14731ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
14741476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
14751476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
14765b5574acSEd Tanous         "#OpenBMCAccountService.v1_0_0.AccountService";
14771476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
14781476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
14791476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
14801476687dSEd Tanous         authMethodsConfig.basic;
14811476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
14821476687dSEd Tanous         authMethodsConfig.sessionToken;
14831ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
14841ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
14851ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
14861476687dSEd Tanous 
14871ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
14881ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
14891ef4c342SEd Tanous     // permissions ConfigureManager
149072048780SAbhishek Patel     Privileges effectiveUserPrivileges =
14913e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
149272048780SAbhishek Patel 
149372048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
149472048780SAbhishek Patel                                          effectiveUserPrivileges))
149572048780SAbhishek Patel     {
14961ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
14971476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
149872048780SAbhishek Patel     }
1499d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1500d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1501d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
15025e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
15031ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
15043d958bbcSAppaRao Puli         if (ec)
15053d958bbcSAppaRao Puli         {
15063d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
15073d958bbcSAppaRao Puli             return;
15083d958bbcSAppaRao Puli         }
1509d1bde9e5SKrzysztof Grobelny 
151062598e31SEd Tanous         BMCWEB_LOG_DEBUG("Got {}properties for AccountService",
151162598e31SEd Tanous                          propertiesList.size());
1512d1bde9e5SKrzysztof Grobelny 
1513d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1514d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1515d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1516d1bde9e5SKrzysztof Grobelny 
1517d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1518d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1519d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1520d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1521d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1522d1bde9e5SKrzysztof Grobelny 
1523d1bde9e5SKrzysztof Grobelny         if (!success)
15243d958bbcSAppaRao Puli         {
1525d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1526d1bde9e5SKrzysztof Grobelny             return;
15273d958bbcSAppaRao Puli         }
1528d1bde9e5SKrzysztof Grobelny 
1529d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
15303d958bbcSAppaRao Puli         {
1531d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
15323d958bbcSAppaRao Puli         }
1533d1bde9e5SKrzysztof Grobelny 
1534d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
15353d958bbcSAppaRao Puli         {
1536d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1537d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1538d1bde9e5SKrzysztof Grobelny         }
1539d1bde9e5SKrzysztof Grobelny 
1540d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
15413d958bbcSAppaRao Puli         {
1542002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1543d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
15443d958bbcSAppaRao Puli         }
1545d1bde9e5SKrzysztof Grobelny     });
15466973a582SRatan Gupta 
154702cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1548ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1549cb13a392SEd Tanous         if (!success)
1550cb13a392SEd Tanous         {
1551cb13a392SEd Tanous             return;
1552cb13a392SEd Tanous         }
1553002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1554ab828d7cSRatan Gupta     };
1555ab828d7cSRatan Gupta 
1556ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1557ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
15581ef4c342SEd Tanous }
15596973a582SRatan Gupta 
15601ef4c342SEd Tanous inline void handleAccountServicePatch(
15611ef4c342SEd Tanous     App& app, const crow::Request& req,
15621ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15631ef4c342SEd Tanous {
15643ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
156545ca1b86SEd Tanous     {
156645ca1b86SEd Tanous         return;
156745ca1b86SEd Tanous     }
1568f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1569f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1570ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1571f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1572f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1573f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1574f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1575f5ffd806SEd Tanous 
157615ed6780SWilly Tu     if (!json_util::readJsonPatch(
15771ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
15781ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
15791ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
15801ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1581f5ffd806SEd Tanous             oemObject))
1582f5ffd806SEd Tanous     {
1583f5ffd806SEd Tanous         return;
1584f5ffd806SEd Tanous     }
1585f5ffd806SEd Tanous 
1586f5ffd806SEd Tanous     if (minPasswordLength)
1587f5ffd806SEd Tanous     {
15889ae226faSGeorge Liu         sdbusplus::asio::setProperty(
15899ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
15909ae226faSGeorge Liu             "/xyz/openbmc_project/user",
15919ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
15929ae226faSGeorge Liu             *minPasswordLength,
15935e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1594ef73ad0dSPaul Fertser             if (ec)
1595ef73ad0dSPaul Fertser             {
1596ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1597ef73ad0dSPaul Fertser                 return;
1598ef73ad0dSPaul Fertser             }
1599ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
16009ae226faSGeorge Liu         });
1601f5ffd806SEd Tanous     }
1602f5ffd806SEd Tanous 
1603f5ffd806SEd Tanous     if (maxPasswordLength)
1604f5ffd806SEd Tanous     {
16051ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1606f5ffd806SEd Tanous     }
1607f5ffd806SEd Tanous 
1608f5ffd806SEd Tanous     if (ldapObject)
1609f5ffd806SEd Tanous     {
1610f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1611f5ffd806SEd Tanous     }
1612f5ffd806SEd Tanous 
1613f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
16141ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1615f5ffd806SEd Tanous                                          oemOpenBMCObject))
1616f5ffd806SEd Tanous     {
1617f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1618f5ffd806SEd Tanous             oemOpenBMCObject &&
1619f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1620f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1621f5ffd806SEd Tanous         {
1622f5ffd806SEd Tanous             if (authMethodsObject)
1623f5ffd806SEd Tanous             {
16241ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1625f5ffd806SEd Tanous             }
1626f5ffd806SEd Tanous         }
1627f5ffd806SEd Tanous     }
1628f5ffd806SEd Tanous 
1629f5ffd806SEd Tanous     if (activeDirectoryObject)
1630f5ffd806SEd Tanous     {
16311ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1632f5ffd806SEd Tanous     }
1633f5ffd806SEd Tanous 
1634f5ffd806SEd Tanous     if (unlockTimeout)
1635f5ffd806SEd Tanous     {
16369ae226faSGeorge Liu         sdbusplus::asio::setProperty(
16379ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
16389ae226faSGeorge Liu             "/xyz/openbmc_project/user",
16399ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
16409ae226faSGeorge Liu             *unlockTimeout, [asyncResp](const boost::system::error_code& ec) {
1641f5ffd806SEd Tanous             if (ec)
1642f5ffd806SEd Tanous             {
1643f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1644f5ffd806SEd Tanous                 return;
1645f5ffd806SEd Tanous             }
1646f5ffd806SEd Tanous             messages::success(asyncResp->res);
16479ae226faSGeorge Liu         });
1648f5ffd806SEd Tanous     }
1649f5ffd806SEd Tanous     if (lockoutThreshold)
1650f5ffd806SEd Tanous     {
16519ae226faSGeorge Liu         sdbusplus::asio::setProperty(
16529ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
16539ae226faSGeorge Liu             "/xyz/openbmc_project/user",
16549ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy",
16559ae226faSGeorge Liu             "MaxLoginAttemptBeforeLockout", *lockoutThreshold,
16565e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1657f5ffd806SEd Tanous             if (ec)
1658f5ffd806SEd Tanous             {
1659f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1660f5ffd806SEd Tanous                 return;
1661f5ffd806SEd Tanous             }
1662f5ffd806SEd Tanous             messages::success(asyncResp->res);
16639ae226faSGeorge Liu         });
1664f5ffd806SEd Tanous     }
16651ef4c342SEd Tanous }
1666f5ffd806SEd Tanous 
16674c7d4d33SEd Tanous inline void handleAccountCollectionHead(
16681ef4c342SEd Tanous     App& app, const crow::Request& req,
16691ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16701ef4c342SEd Tanous {
16713ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
167245ca1b86SEd Tanous     {
167345ca1b86SEd Tanous         return;
167445ca1b86SEd Tanous     }
16754c7d4d33SEd Tanous     asyncResp->res.addHeader(
16764c7d4d33SEd Tanous         boost::beast::http::field::link,
16774c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
16784c7d4d33SEd Tanous }
16794c7d4d33SEd Tanous 
16804c7d4d33SEd Tanous inline void handleAccountCollectionGet(
16814c7d4d33SEd Tanous     App& app, const crow::Request& req,
16824c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16834c7d4d33SEd Tanous {
1684afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1685afd369c6SJiaqing Zhao     {
1686afd369c6SJiaqing Zhao         return;
1687afd369c6SJiaqing Zhao     }
16883e72c202SNinad Palsule 
16893e72c202SNinad Palsule     if (req.session == nullptr)
16903e72c202SNinad Palsule     {
16913e72c202SNinad Palsule         messages::internalError(asyncResp->res);
16923e72c202SNinad Palsule         return;
16933e72c202SNinad Palsule     }
16943e72c202SNinad Palsule 
1695afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1696afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1697afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
16981476687dSEd Tanous 
16991476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
17001476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
17011ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
17021476687dSEd Tanous                                               "ManagerAccountCollection";
17031476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
17041476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
17050f74e643SEd Tanous 
17066c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
17073e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
17086c51eab1SEd Tanous 
1709f5e29f33SJunLin Chen     std::string thisUser;
1710f5e29f33SJunLin Chen     if (req.session)
1711f5e29f33SJunLin Chen     {
1712f5e29f33SJunLin Chen         thisUser = req.session->username;
1713f5e29f33SJunLin Chen     }
17145eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
17155eb468daSGeorge Liu     dbus::utility::getManagedObjects(
17165eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
1717cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
17185e7e2dc5SEd Tanous             const boost::system::error_code& ec,
1719711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1720b9b2e0b2SEd Tanous         if (ec)
1721b9b2e0b2SEd Tanous         {
1722f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1723b9b2e0b2SEd Tanous             return;
1724b9b2e0b2SEd Tanous         }
1725b9b2e0b2SEd Tanous 
1726cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1727002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1728cef1ddfbSEd Tanous 
1729cef1ddfbSEd Tanous         bool userCanSeeSelf =
1730002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1731cef1ddfbSEd Tanous 
1732002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1733b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1734b9b2e0b2SEd Tanous 
17359eb808c1SEd Tanous         for (const auto& userpath : users)
1736b9b2e0b2SEd Tanous         {
17372dfd18efSEd Tanous             std::string user = userpath.first.filename();
17382dfd18efSEd Tanous             if (user.empty())
1739b9b2e0b2SEd Tanous             {
17402dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
174162598e31SEd Tanous                 BMCWEB_LOG_ERROR("Invalid firmware ID");
17422dfd18efSEd Tanous 
17432dfd18efSEd Tanous                 return;
1744b9b2e0b2SEd Tanous             }
1745f365910cSGunnar Mills 
1746f365910cSGunnar Mills             // As clarified by Redfish here:
1747f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
17486c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
17496c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
17506c51eab1SEd Tanous             // accounts.
17511ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1752f365910cSGunnar Mills             {
17531476687dSEd Tanous                 nlohmann::json::object_t member;
17543b32780dSEd Tanous                 member["@odata.id"] = boost::urls::format(
17553b32780dSEd Tanous                     "/redfish/v1/AccountService/Accounts/{}", user);
1756b2ba3072SPatrick Williams                 memberArray.emplace_back(std::move(member));
1757b9b2e0b2SEd Tanous             }
1758f365910cSGunnar Mills         }
17591ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
17605eb468daSGeorge Liu     });
17611ef4c342SEd Tanous }
17626c51eab1SEd Tanous 
176397e90da3SNinad Palsule inline void processAfterCreateUser(
176497e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
176597e90da3SNinad Palsule     const std::string& username, const std::string& password,
176697e90da3SNinad Palsule     const boost::system::error_code& ec, sdbusplus::message_t& m)
176797e90da3SNinad Palsule {
176897e90da3SNinad Palsule     if (ec)
176997e90da3SNinad Palsule     {
177097e90da3SNinad Palsule         userErrorMessageHandler(m.get_error(), asyncResp, username, "");
177197e90da3SNinad Palsule         return;
177297e90da3SNinad Palsule     }
177397e90da3SNinad Palsule 
177497e90da3SNinad Palsule     if (pamUpdatePassword(username, password) != PAM_SUCCESS)
177597e90da3SNinad Palsule     {
177697e90da3SNinad Palsule         // At this point we have a user that's been
177797e90da3SNinad Palsule         // created, but the password set
177897e90da3SNinad Palsule         // failed.Something is wrong, so delete the user
177997e90da3SNinad Palsule         // that we've already created
178097e90da3SNinad Palsule         sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
178197e90da3SNinad Palsule         tempObjPath /= username;
178297e90da3SNinad Palsule         const std::string userPath(tempObjPath);
178397e90da3SNinad Palsule 
178497e90da3SNinad Palsule         crow::connections::systemBus->async_method_call(
178597e90da3SNinad Palsule             [asyncResp, password](const boost::system::error_code& ec3) {
178697e90da3SNinad Palsule             if (ec3)
178797e90da3SNinad Palsule             {
178897e90da3SNinad Palsule                 messages::internalError(asyncResp->res);
178997e90da3SNinad Palsule                 return;
179097e90da3SNinad Palsule             }
179197e90da3SNinad Palsule 
179297e90da3SNinad Palsule             // If password is invalid
17939bd80831SJason M. Bills             messages::propertyValueFormatError(asyncResp->res, nullptr,
179497e90da3SNinad Palsule                                                "Password");
179597e90da3SNinad Palsule         },
179697e90da3SNinad Palsule             "xyz.openbmc_project.User.Manager", userPath,
179797e90da3SNinad Palsule             "xyz.openbmc_project.Object.Delete", "Delete");
179897e90da3SNinad Palsule 
179962598e31SEd Tanous         BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
180097e90da3SNinad Palsule         return;
180197e90da3SNinad Palsule     }
180297e90da3SNinad Palsule 
180397e90da3SNinad Palsule     messages::created(asyncResp->res);
180497e90da3SNinad Palsule     asyncResp->res.addHeader("Location",
180597e90da3SNinad Palsule                              "/redfish/v1/AccountService/Accounts/" + username);
180697e90da3SNinad Palsule }
180797e90da3SNinad Palsule 
180897e90da3SNinad Palsule inline void processAfterGetAllGroups(
180997e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
181097e90da3SNinad Palsule     const std::string& username, const std::string& password,
1811e01d0c36SEd Tanous     const std::string& roleId, bool enabled,
18129ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes,
181397e90da3SNinad Palsule     const std::vector<std::string>& allGroupsList)
181497e90da3SNinad Palsule {
18153e72c202SNinad Palsule     std::vector<std::string> userGroups;
18169ba73934SNinad Palsule     std::vector<std::string> accountTypeUserGroups;
18179ba73934SNinad Palsule 
18189ba73934SNinad Palsule     // If user specified account types then convert them to unix user groups
18199ba73934SNinad Palsule     if (accountTypes)
18209ba73934SNinad Palsule     {
18219ba73934SNinad Palsule         if (!getUserGroupFromAccountType(asyncResp->res, *accountTypes,
18229ba73934SNinad Palsule                                          accountTypeUserGroups))
18239ba73934SNinad Palsule         {
18249ba73934SNinad Palsule             // Problem in mapping Account Types to User Groups, Error already
18259ba73934SNinad Palsule             // logged.
18269ba73934SNinad Palsule             return;
18279ba73934SNinad Palsule         }
18289ba73934SNinad Palsule     }
18299ba73934SNinad Palsule 
18303e72c202SNinad Palsule     for (const auto& grp : allGroupsList)
18313e72c202SNinad Palsule     {
18329ba73934SNinad Palsule         // If user specified the account type then only accept groups which are
18339ba73934SNinad Palsule         // in the account types group list.
18349ba73934SNinad Palsule         if (!accountTypeUserGroups.empty())
18359ba73934SNinad Palsule         {
18369ba73934SNinad Palsule             bool found = false;
18379ba73934SNinad Palsule             for (const auto& grp1 : accountTypeUserGroups)
18389ba73934SNinad Palsule             {
18399ba73934SNinad Palsule                 if (grp == grp1)
18409ba73934SNinad Palsule                 {
18419ba73934SNinad Palsule                     found = true;
18429ba73934SNinad Palsule                     break;
18439ba73934SNinad Palsule                 }
18449ba73934SNinad Palsule             }
18459ba73934SNinad Palsule             if (!found)
18469ba73934SNinad Palsule             {
18479ba73934SNinad Palsule                 continue;
18489ba73934SNinad Palsule             }
18499ba73934SNinad Palsule         }
18509ba73934SNinad Palsule 
18513e72c202SNinad Palsule         // Console access is provided to the user who is a member of
18523e72c202SNinad Palsule         // hostconsole group and has a administrator role. So, set
18533e72c202SNinad Palsule         // hostconsole group only for the administrator.
18549ba73934SNinad Palsule         if ((grp == "hostconsole") && (roleId != "priv-admin"))
18553e72c202SNinad Palsule         {
18569ba73934SNinad Palsule             if (!accountTypeUserGroups.empty())
18579ba73934SNinad Palsule             {
185862598e31SEd Tanous                 BMCWEB_LOG_ERROR(
185962598e31SEd Tanous                     "Only administrator can get HostConsole access");
18609ba73934SNinad Palsule                 asyncResp->res.result(boost::beast::http::status::bad_request);
18619ba73934SNinad Palsule                 return;
18629ba73934SNinad Palsule             }
18639ba73934SNinad Palsule             continue;
18649ba73934SNinad Palsule         }
18653e72c202SNinad Palsule         userGroups.emplace_back(grp);
18663e72c202SNinad Palsule     }
18679ba73934SNinad Palsule 
18689ba73934SNinad Palsule     // Make sure user specified groups are valid. This is internal error because
18699ba73934SNinad Palsule     // it some inconsistencies between user manager and bmcweb.
18709ba73934SNinad Palsule     if (!accountTypeUserGroups.empty() &&
18719ba73934SNinad Palsule         accountTypeUserGroups.size() != userGroups.size())
18729ba73934SNinad Palsule     {
18739ba73934SNinad Palsule         messages::internalError(asyncResp->res);
18749ba73934SNinad Palsule         return;
18753e72c202SNinad Palsule     }
187697e90da3SNinad Palsule     crow::connections::systemBus->async_method_call(
187797e90da3SNinad Palsule         [asyncResp, username, password](const boost::system::error_code& ec2,
187897e90da3SNinad Palsule                                         sdbusplus::message_t& m) {
187997e90da3SNinad Palsule         processAfterCreateUser(asyncResp, username, password, ec2, m);
188097e90da3SNinad Palsule     },
188197e90da3SNinad Palsule         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
18823e72c202SNinad Palsule         "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
1883e01d0c36SEd Tanous         roleId, enabled);
188497e90da3SNinad Palsule }
188597e90da3SNinad Palsule 
18861ef4c342SEd Tanous inline void handleAccountCollectionPost(
18871ef4c342SEd Tanous     App& app, const crow::Request& req,
18881ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
18891ef4c342SEd Tanous {
18903ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
189145ca1b86SEd Tanous     {
189245ca1b86SEd Tanous         return;
189345ca1b86SEd Tanous     }
18949712f8acSEd Tanous     std::string username;
18959712f8acSEd Tanous     std::string password;
1896e01d0c36SEd Tanous     std::optional<std::string> roleIdJson;
1897e01d0c36SEd Tanous     std::optional<bool> enabledJson;
18989ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes;
1899e01d0c36SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
1900e01d0c36SEd Tanous                                   "Password", password, "RoleId", roleIdJson,
1901e01d0c36SEd Tanous                                   "Enabled", enabledJson, "AccountTypes",
1902e01d0c36SEd Tanous                                   accountTypes))
190304ae99ecSEd Tanous     {
190404ae99ecSEd Tanous         return;
190504ae99ecSEd Tanous     }
190604ae99ecSEd Tanous 
1907e01d0c36SEd Tanous     std::string roleId = roleIdJson.value_or("User");
1908e01d0c36SEd Tanous     std::string priv = getPrivilegeFromRoleId(roleId);
190984e12cb7SAppaRao Puli     if (priv.empty())
191004ae99ecSEd Tanous     {
1911e01d0c36SEd Tanous         messages::propertyValueNotInList(asyncResp->res, roleId, "RoleId");
191204ae99ecSEd Tanous         return;
191304ae99ecSEd Tanous     }
19149712f8acSEd Tanous     roleId = priv;
191504ae99ecSEd Tanous 
1916e01d0c36SEd Tanous     bool enabled = enabledJson.value_or(true);
1917e01d0c36SEd Tanous 
1918599c71d8SAyushi Smriti     // Reading AllGroups property
19191e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
19201ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
19211ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
19221ef4c342SEd Tanous         "AllGroups",
19239ba73934SNinad Palsule         [asyncResp, username, password{std::move(password)}, roleId, enabled,
19249ba73934SNinad Palsule          accountTypes](const boost::system::error_code& ec,
19251e1e598dSJonathan Doman                        const std::vector<std::string>& allGroupsList) {
1926599c71d8SAyushi Smriti         if (ec)
1927599c71d8SAyushi Smriti         {
192862598e31SEd Tanous             BMCWEB_LOG_DEBUG("ERROR with async_method_call");
1929599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1930599c71d8SAyushi Smriti             return;
1931599c71d8SAyushi Smriti         }
1932599c71d8SAyushi Smriti 
19331e1e598dSJonathan Doman         if (allGroupsList.empty())
1934599c71d8SAyushi Smriti         {
1935599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1936599c71d8SAyushi Smriti             return;
1937599c71d8SAyushi Smriti         }
1938599c71d8SAyushi Smriti 
193997e90da3SNinad Palsule         processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
19409ba73934SNinad Palsule                                  accountTypes, allGroupsList);
19411e1e598dSJonathan Doman     });
19421ef4c342SEd Tanous }
1943b9b2e0b2SEd Tanous 
19441ef4c342SEd Tanous inline void
19454c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
19466c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19474c7d4d33SEd Tanous                       const std::string& /*accountName*/)
19481ef4c342SEd Tanous {
19493ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
195045ca1b86SEd Tanous     {
195145ca1b86SEd Tanous         return;
195245ca1b86SEd Tanous     }
19534c7d4d33SEd Tanous     asyncResp->res.addHeader(
19544c7d4d33SEd Tanous         boost::beast::http::field::link,
19554c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
19564c7d4d33SEd Tanous }
1957afd369c6SJiaqing Zhao 
19584c7d4d33SEd Tanous inline void
19594c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
19604c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19614c7d4d33SEd Tanous                      const std::string& accountName)
19624c7d4d33SEd Tanous {
1963afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1964afd369c6SJiaqing Zhao     {
1965afd369c6SJiaqing Zhao         return;
1966afd369c6SJiaqing Zhao     }
1967afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1968afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1969afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1970afd369c6SJiaqing Zhao 
19711ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1972031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1973d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1974031514fbSJunLin Chen     return;
19751ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1976afd369c6SJiaqing Zhao 
1977031514fbSJunLin Chen     if (req.session == nullptr)
1978031514fbSJunLin Chen     {
1979031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1980031514fbSJunLin Chen         return;
1981031514fbSJunLin Chen     }
19826c51eab1SEd Tanous     if (req.session->username != accountName)
1983b9b2e0b2SEd Tanous     {
19846c51eab1SEd Tanous         // At this point we've determined that the user is trying to
19851ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
19861ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
19871ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
19886c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
19893e72c202SNinad Palsule             redfish::getUserPrivileges(*req.session);
19901ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
19911ef4c342SEd Tanous                                                          "ConfigureManager"};
19926c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
19936c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1994900f9497SJoseph Reynolds         {
199562598e31SEd Tanous             BMCWEB_LOG_DEBUG("GET Account denied access");
1996900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1997900f9497SJoseph Reynolds             return;
1998900f9497SJoseph Reynolds         }
1999900f9497SJoseph Reynolds     }
2000900f9497SJoseph Reynolds 
20015eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
20025eb468daSGeorge Liu     dbus::utility::getManagedObjects(
20035eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
20041ef4c342SEd Tanous         [asyncResp,
20055e7e2dc5SEd Tanous          accountName](const boost::system::error_code& ec,
2006711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
2007b9b2e0b2SEd Tanous         if (ec)
2008b9b2e0b2SEd Tanous         {
2009f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2010b9b2e0b2SEd Tanous             return;
2011b9b2e0b2SEd Tanous         }
20123544d2a7SEd Tanous         const auto userIt = std::ranges::find_if(
201380f79a40SMichael Shen             users,
201480f79a40SMichael Shen             [accountName](
2015b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
201680f79a40SMichael Shen                                 dbus::utility::DBusInterfacesMap>& user) {
201755f79e6fSEd Tanous             return accountName == user.first.filename();
2018b477fd44SP Dheeraj Srujan Kumar         });
2019b9b2e0b2SEd Tanous 
202084e12cb7SAppaRao Puli         if (userIt == users.end())
2021b9b2e0b2SEd Tanous         {
2022002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
2023002d39b4SEd Tanous                                        accountName);
202484e12cb7SAppaRao Puli             return;
202584e12cb7SAppaRao Puli         }
20264e68c45bSAyushi Smriti 
20271476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
202858345856SAbhishek Patel             "#ManagerAccount.v1_7_0.ManagerAccount";
20291476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
20301476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
20311476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
203258345856SAbhishek Patel         asyncResp->res.jsonValue["StrictAccountTypes"] = true;
20334e68c45bSAyushi Smriti 
203484e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
203565b0dc32SEd Tanous         {
2036002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
203765b0dc32SEd Tanous             {
203865b0dc32SEd Tanous                 for (const auto& property : interface.second)
203965b0dc32SEd Tanous                 {
204065b0dc32SEd Tanous                     if (property.first == "UserEnabled")
204165b0dc32SEd Tanous                     {
204265b0dc32SEd Tanous                         const bool* userEnabled =
2043abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
204465b0dc32SEd Tanous                         if (userEnabled == nullptr)
204565b0dc32SEd Tanous                         {
204662598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserEnabled wasn't a bool");
204784e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
204884e12cb7SAppaRao Puli                             return;
204965b0dc32SEd Tanous                         }
2050002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
205165b0dc32SEd Tanous                     }
2052002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
205365b0dc32SEd Tanous                     {
205465b0dc32SEd Tanous                         const bool* userLocked =
2055abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
205665b0dc32SEd Tanous                         if (userLocked == nullptr)
205765b0dc32SEd Tanous                         {
205862598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserLockedForF"
205984e12cb7SAppaRao Puli                                              "ailedAttempt "
206062598e31SEd Tanous                                              "wasn't a bool");
206184e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
206284e12cb7SAppaRao Puli                             return;
206365b0dc32SEd Tanous                         }
2064002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
2065002d39b4SEd Tanous                         asyncResp->res
2066002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
20673bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
206865b0dc32SEd Tanous                     }
206984e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
207084e12cb7SAppaRao Puli                     {
207154fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
2072002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
207354fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
207484e12cb7SAppaRao Puli                         {
207562598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserPrivilege wasn't a "
207662598e31SEd Tanous                                              "string");
207784e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
207884e12cb7SAppaRao Puli                             return;
207984e12cb7SAppaRao Puli                         }
20801ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
208154fc587aSNagaraju Goruganti                         if (role.empty())
208284e12cb7SAppaRao Puli                         {
208362598e31SEd Tanous                             BMCWEB_LOG_ERROR("Invalid user role");
208484e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
208584e12cb7SAppaRao Puli                             return;
208684e12cb7SAppaRao Puli                         }
208754fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
208884e12cb7SAppaRao Puli 
20891476687dSEd Tanous                         nlohmann::json& roleEntry =
2090002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
20913b32780dSEd Tanous                         roleEntry["@odata.id"] = boost::urls::format(
20923b32780dSEd Tanous                             "/redfish/v1/AccountService/Roles/{}", role);
209384e12cb7SAppaRao Puli                     }
2094002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
20953bf4e632SJoseph Reynolds                     {
20963bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
20973bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
20983bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
20993bf4e632SJoseph Reynolds                         {
210062598e31SEd Tanous                             BMCWEB_LOG_ERROR(
210162598e31SEd Tanous                                 "UserPasswordExpired wasn't a bool");
21023bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
21033bf4e632SJoseph Reynolds                             return;
21043bf4e632SJoseph Reynolds                         }
2105002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
21063bf4e632SJoseph Reynolds                             *userPasswordExpired;
21073bf4e632SJoseph Reynolds                     }
2108c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
2109c7229815SAbhishek Patel                     {
2110c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
2111c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
2112c7229815SAbhishek Patel                                 &property.second);
2113c7229815SAbhishek Patel                         if (userGroups == nullptr)
2114c7229815SAbhishek Patel                         {
211562598e31SEd Tanous                             BMCWEB_LOG_ERROR(
211662598e31SEd Tanous                                 "userGroups wasn't a string vector");
2117c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2118c7229815SAbhishek Patel                             return;
2119c7229815SAbhishek Patel                         }
2120c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
2121c7229815SAbhishek Patel                         {
212262598e31SEd Tanous                             BMCWEB_LOG_ERROR("userGroups mapping failed");
2123c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2124c7229815SAbhishek Patel                             return;
2125c7229815SAbhishek Patel                         }
2126c7229815SAbhishek Patel                     }
212765b0dc32SEd Tanous                 }
212865b0dc32SEd Tanous             }
212965b0dc32SEd Tanous         }
213065b0dc32SEd Tanous 
21313b32780dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
21323b32780dSEd Tanous             "/redfish/v1/AccountService/Accounts/{}", accountName);
2133b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
2134b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
21355eb468daSGeorge Liu     });
21361ef4c342SEd Tanous }
2137a840879dSEd Tanous 
21381ef4c342SEd Tanous inline void
213920fc307fSGunnar Mills     handleAccountDelete(App& app, const crow::Request& req,
21406c51eab1SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21411ef4c342SEd Tanous                         const std::string& username)
21421ef4c342SEd Tanous {
21433ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
214445ca1b86SEd Tanous     {
214545ca1b86SEd Tanous         return;
214645ca1b86SEd Tanous     }
21471ef4c342SEd Tanous 
21481ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
2149031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
2150d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
2151031514fbSJunLin Chen     return;
2152031514fbSJunLin Chen 
21531ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
21541ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
21551ef4c342SEd Tanous     tempObjPath /= username;
21561ef4c342SEd Tanous     const std::string userPath(tempObjPath);
21571ef4c342SEd Tanous 
21581ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
21595e7e2dc5SEd Tanous         [asyncResp, username](const boost::system::error_code& ec) {
21601ef4c342SEd Tanous         if (ec)
21611ef4c342SEd Tanous         {
2162d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
21631ef4c342SEd Tanous                                        username);
21641ef4c342SEd Tanous             return;
21651ef4c342SEd Tanous         }
21661ef4c342SEd Tanous 
21671ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
21681ef4c342SEd Tanous     },
21691ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
21701ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
21711ef4c342SEd Tanous }
21721ef4c342SEd Tanous 
21731ef4c342SEd Tanous inline void
21741ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
21751ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21761ef4c342SEd Tanous                        const std::string& username)
21771ef4c342SEd Tanous {
21781ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
21791ef4c342SEd Tanous     {
21801ef4c342SEd Tanous         return;
21811ef4c342SEd Tanous     }
21821ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
21831ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
2184d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
21851ef4c342SEd Tanous     return;
21861ef4c342SEd Tanous 
21871ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
2188a24526dcSEd Tanous     std::optional<std::string> newUserName;
2189a24526dcSEd Tanous     std::optional<std::string> password;
2190a24526dcSEd Tanous     std::optional<bool> enabled;
2191a24526dcSEd Tanous     std::optional<std::string> roleId;
219224c8542dSRatan Gupta     std::optional<bool> locked;
219358345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes;
219458345856SAbhishek Patel 
219558345856SAbhishek Patel     bool userSelf = (username == req.session->username);
2196e9cc5172SEd Tanous 
2197031514fbSJunLin Chen     if (req.session == nullptr)
2198031514fbSJunLin Chen     {
2199031514fbSJunLin Chen         messages::internalError(asyncResp->res);
2200031514fbSJunLin Chen         return;
2201031514fbSJunLin Chen     }
2202031514fbSJunLin Chen 
2203e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
22043e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
2205e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
2206e9cc5172SEd Tanous     bool userHasConfigureUsers =
2207e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
2208e9cc5172SEd Tanous     if (userHasConfigureUsers)
2209e9cc5172SEd Tanous     {
2210e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
221158345856SAbhishek Patel         if (!json_util::readJsonPatch(
221258345856SAbhishek Patel                 req, asyncResp->res, "UserName", newUserName, "Password",
221358345856SAbhishek Patel                 password, "RoleId", roleId, "Enabled", enabled, "Locked",
221458345856SAbhishek Patel                 locked, "AccountTypes", accountTypes))
2215a840879dSEd Tanous         {
2216a840879dSEd Tanous             return;
2217a840879dSEd Tanous         }
2218e9cc5172SEd Tanous     }
2219e9cc5172SEd Tanous     else
2220900f9497SJoseph Reynolds     {
2221e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
222258345856SAbhishek Patel         if (!userSelf)
2223900f9497SJoseph Reynolds         {
2224900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
2225900f9497SJoseph Reynolds             return;
2226900f9497SJoseph Reynolds         }
2227031514fbSJunLin Chen 
2228e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
22291ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
22301ef4c342SEd Tanous                                       password))
2231e9cc5172SEd Tanous         {
2232e9cc5172SEd Tanous             return;
2233e9cc5172SEd Tanous         }
2234900f9497SJoseph Reynolds     }
2235900f9497SJoseph Reynolds 
223666b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
22376c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
22386c51eab1SEd Tanous     // updating user properties other then username. If username
22396c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
22406c51eab1SEd Tanous     // user rename request.
224166b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
2242a840879dSEd Tanous     {
22431ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
224458345856SAbhishek Patel                              locked, accountTypes, userSelf);
224584e12cb7SAppaRao Puli         return;
224684e12cb7SAppaRao Puli     }
224784e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
22486c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
22491ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
225058345856SAbhishek Patel          locked, userSelf, accountTypes(std::move(accountTypes))](
2251e81de512SEd Tanous             const boost::system::error_code& ec, sdbusplus::message_t& m) {
225284e12cb7SAppaRao Puli         if (ec)
225384e12cb7SAppaRao Puli         {
2254002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2255002d39b4SEd Tanous                                     username);
2256a840879dSEd Tanous             return;
2257a840879dSEd Tanous         }
2258a840879dSEd Tanous 
2259002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
226058345856SAbhishek Patel                              locked, accountTypes, userSelf);
226184e12cb7SAppaRao Puli     },
22621ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
226384e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
226484e12cb7SAppaRao Puli         *newUserName);
22651ef4c342SEd Tanous }
22661ef4c342SEd Tanous 
22671ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
22681ef4c342SEd Tanous {
22691ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22704c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
22714c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
22724c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
22734c7d4d33SEd Tanous 
22744c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22751ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
22761ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
22771ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
22781ef4c342SEd Tanous 
22791ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22801ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
22811ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
22821ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
22831ef4c342SEd Tanous 
22841ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
22854c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
22864c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
22874c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
22884c7d4d33SEd Tanous 
22894c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
22901ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
22911ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
22921ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
22931ef4c342SEd Tanous 
22941ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
22951ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
22961ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
22971ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
22981ef4c342SEd Tanous 
22991ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23004c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
23014c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
23024c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
23034c7d4d33SEd Tanous 
23044c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23051ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
23061ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
23071ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
23081ef4c342SEd Tanous 
23091ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23101ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
23111ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
23121ef4c342SEd Tanous         // yet
23131ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
23141ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
23151ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
231684e12cb7SAppaRao Puli 
23176c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2318ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
23196c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
232020fc307fSGunnar Mills             std::bind_front(handleAccountDelete, std::ref(app)));
232106e086d9SEd Tanous }
232288d16c9aSLewanczyk, Dawid 
232388d16c9aSLewanczyk, Dawid } // namespace redfish
2324