xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 7a54389415ea264ffdd9605002ee6f61bd6a995f)
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>
34c7229815SAbhishek Patel #include <string>
352b73119cSGeorge Liu #include <string_view>
36c7229815SAbhishek Patel #include <vector>
37c7229815SAbhishek Patel 
381abe55efSEd Tanous namespace redfish
391abe55efSEd Tanous {
4088d16c9aSLewanczyk, Dawid 
4123a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
426973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
432c70f800SEd Tanous constexpr const char* adConfigObject =
44ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
45ab828d7cSRatan Gupta 
46b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
476973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
486973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
496973a582SRatan Gupta constexpr const char* ldapConfigInterface =
506973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
516973a582SRatan Gupta constexpr const char* ldapCreateInterface =
526973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
536973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
5406785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
5506785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
566973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
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         {
155c7229815SAbhishek Patel             // Invalid user group name. Caller throws an excption.
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     {
21558345856SAbhishek Patel         BMCWEB_LOG_ERROR
21658345856SAbhishek Patel             << "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     {
24758345856SAbhishek Patel         BMCWEB_LOG_ERROR
24858345856SAbhishek Patel             << "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 
26258345856SAbhishek Patel     crow::connections::systemBus->async_method_call(
26358345856SAbhishek Patel         [asyncResp](const boost::system::error_code ec) {
26458345856SAbhishek Patel         if (ec)
26558345856SAbhishek Patel         {
26658345856SAbhishek Patel             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
26758345856SAbhishek Patel             messages::internalError(asyncResp->res);
26858345856SAbhishek Patel             return;
26958345856SAbhishek Patel         }
27058345856SAbhishek Patel         messages::success(asyncResp->res);
27158345856SAbhishek Patel         },
27258345856SAbhishek Patel         "xyz.openbmc_project.User.Manager", dbusObjectPath,
27358345856SAbhishek Patel         "org.freedesktop.DBus.Properties", "Set",
27458345856SAbhishek Patel         "xyz.openbmc_project.User.Attributes", "UserGroups",
27558345856SAbhishek Patel         dbus::utility::DbusVariantType{updatedUserGroups});
27658345856SAbhishek Patel }
27758345856SAbhishek Patel 
2788d1b46d7Szhanghch05 inline void userErrorMessageHandler(
2798d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2808d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
28166b5ca76Sjayaprakash Mutyala {
28266b5ca76Sjayaprakash Mutyala     if (e == nullptr)
28366b5ca76Sjayaprakash Mutyala     {
28466b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
28566b5ca76Sjayaprakash Mutyala         return;
28666b5ca76Sjayaprakash Mutyala     }
28766b5ca76Sjayaprakash Mutyala 
288055806b3SManojkiran Eda     const char* errorMessage = e->name;
28966b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
29066b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
29166b5ca76Sjayaprakash Mutyala     {
292d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
29366b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
29466b5ca76Sjayaprakash Mutyala     }
29566b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
29666b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
29766b5ca76Sjayaprakash Mutyala     {
298d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
29966b5ca76Sjayaprakash Mutyala     }
300d4d25793SEd Tanous     else if ((strcmp(errorMessage,
301d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
302d4d25793SEd Tanous               0) ||
3030fda0f12SGeorge Liu              (strcmp(
3040fda0f12SGeorge Liu                   errorMessage,
3050fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
3060fda0f12SGeorge Liu               0))
30766b5ca76Sjayaprakash Mutyala     {
30866b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
30966b5ca76Sjayaprakash Mutyala     }
31066b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
31166b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
31266b5ca76Sjayaprakash Mutyala     {
31366b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
31466b5ca76Sjayaprakash Mutyala     }
31566b5ca76Sjayaprakash Mutyala     else
31666b5ca76Sjayaprakash Mutyala     {
31766b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
31866b5ca76Sjayaprakash Mutyala     }
31966b5ca76Sjayaprakash Mutyala }
32066b5ca76Sjayaprakash Mutyala 
32181ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
322ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
323ab828d7cSRatan Gupta                                 const std::string& ldapType)
3246973a582SRatan Gupta {
32589492a15SPatrick Williams     std::string service = (ldapType == "LDAP") ? "LDAPService"
32689492a15SPatrick Williams                                                : "ActiveDirectoryService";
32754fc587aSNagaraju Goruganti 
3281476687dSEd Tanous     nlohmann::json& ldap = jsonResponse[ldapType];
32954fc587aSNagaraju Goruganti 
3301476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
3311476687dSEd Tanous     ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri});
3320ec8b83dSEd Tanous     ldap["Authentication"]["AuthenticationType"] =
3330ec8b83dSEd Tanous         account_service::AuthenticationTypes::UsernameAndPassword;
3341476687dSEd Tanous     ldap["Authentication"]["Username"] = confData.bindDN;
3351476687dSEd Tanous     ldap["Authentication"]["Password"] = nullptr;
3361476687dSEd Tanous 
3371476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] =
3381476687dSEd Tanous         nlohmann::json::array({confData.baseDN});
3391476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] =
3401476687dSEd Tanous         confData.userNameAttribute;
3411476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] =
3421476687dSEd Tanous         confData.groupAttribute;
3431476687dSEd Tanous 
3441476687dSEd Tanous     nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"];
34554fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
3469eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
34754fc587aSNagaraju Goruganti     {
34854fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
34954fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
350613dabeaSEd Tanous 
351613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
352613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
353329f0348SJorge Cisneros         remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
354329f0348SJorge Cisneros         roleMapArray.emplace_back(std::move(remoteGroup));
35554fc587aSNagaraju Goruganti     }
3566973a582SRatan Gupta }
3576973a582SRatan Gupta 
3586973a582SRatan Gupta /**
35906785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
36006785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
36106785244SRatan Gupta  *
36206785244SRatan Gupta  */
36323a21a1cSEd Tanous inline void handleRoleMapPatch(
3648d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36506785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
366f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
36706785244SRatan Gupta {
36806785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
36906785244SRatan Gupta     {
370f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
37106785244SRatan Gupta 
37206785244SRatan Gupta         if (thisJson.is_null())
37306785244SRatan Gupta         {
37406785244SRatan Gupta             // delete the existing object
37506785244SRatan Gupta             if (index < roleMapObjData.size())
37606785244SRatan Gupta             {
37706785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
37806785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
3795e7e2dc5SEd Tanous                      index](const boost::system::error_code& ec) {
38006785244SRatan Gupta                     if (ec)
38106785244SRatan Gupta                     {
38206785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
38306785244SRatan Gupta                         messages::internalError(asyncResp->res);
38406785244SRatan Gupta                         return;
38506785244SRatan Gupta                     }
38689492a15SPatrick Williams                     asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"]
38789492a15SPatrick Williams                                             [index] = nullptr;
38806785244SRatan Gupta                     },
38906785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
39006785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
39106785244SRatan Gupta             }
39206785244SRatan Gupta             else
39306785244SRatan Gupta             {
39406785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
3952e8c4bdaSEd Tanous                 messages::propertyValueTypeError(asyncResp->res, thisJson,
3962e8c4bdaSEd Tanous                                                  "RemoteRoleMapping/" +
3972e8c4bdaSEd Tanous                                                      std::to_string(index));
39806785244SRatan Gupta                 return;
39906785244SRatan Gupta             }
40006785244SRatan Gupta         }
40106785244SRatan Gupta         else if (thisJson.empty())
40206785244SRatan Gupta         {
40306785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
40406785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
40506785244SRatan Gupta         }
40606785244SRatan Gupta         else
40706785244SRatan Gupta         {
40806785244SRatan Gupta             // update/create the object
40906785244SRatan Gupta             std::optional<std::string> remoteGroup;
41006785244SRatan Gupta             std::optional<std::string> localRole;
41106785244SRatan Gupta 
412f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
413f23b7296SEd Tanous             // readJson is structured
414f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
415f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
416f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
417f23b7296SEd Tanous                                      localRole))
41806785244SRatan Gupta             {
41906785244SRatan Gupta                 continue;
42006785244SRatan Gupta             }
42106785244SRatan Gupta 
42206785244SRatan Gupta             // Update existing RoleMapping Object
42306785244SRatan Gupta             if (index < roleMapObjData.size())
42406785244SRatan Gupta             {
42506785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
42606785244SRatan Gupta                 // If "RemoteGroup" info is provided
42706785244SRatan Gupta                 if (remoteGroup)
42806785244SRatan Gupta                 {
42906785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
43006785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
43125e055a3SRavi Teja                          remoteGroup](const boost::system::error_code& ec,
432*7a543894SPatrick Williams                                       const sdbusplus::message_t& msg) {
43306785244SRatan Gupta                         if (ec)
43406785244SRatan Gupta                         {
43525e055a3SRavi Teja                             const sd_bus_error* dbusError = msg.get_error();
43625e055a3SRavi Teja                             if ((dbusError != nullptr) &&
43725e055a3SRavi Teja                                 (dbusError->name ==
43825e055a3SRavi Teja                                  std::string_view(
43925e055a3SRavi Teja                                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
44025e055a3SRavi Teja                             {
44125e055a3SRavi Teja                                 BMCWEB_LOG_WARNING << "DBUS response error: "
44225e055a3SRavi Teja                                                    << ec;
44325e055a3SRavi Teja                                 messages::propertyValueIncorrect(asyncResp->res,
44425e055a3SRavi Teja                                                                  "RemoteGroup",
44525e055a3SRavi Teja                                                                  *remoteGroup);
44625e055a3SRavi Teja                                 return;
44725e055a3SRavi Teja                             }
44806785244SRatan Gupta                             messages::internalError(asyncResp->res);
44906785244SRatan Gupta                             return;
45006785244SRatan Gupta                         }
45106785244SRatan Gupta                         asyncResp->res
452002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
453002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
45406785244SRatan Gupta                         },
45506785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
45606785244SRatan Gupta                         propertyInterface, "Set",
45706785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
45806785244SRatan Gupta                         "GroupName",
459168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
460168e20c1SEd Tanous                             std::move(*remoteGroup)));
46106785244SRatan Gupta                 }
46206785244SRatan Gupta 
46306785244SRatan Gupta                 // If "LocalRole" info is provided
46406785244SRatan Gupta                 if (localRole)
46506785244SRatan Gupta                 {
46606785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
46706785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
46825e055a3SRavi Teja                          localRole](const boost::system::error_code& ec,
469*7a543894SPatrick 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                             {
47825e055a3SRavi Teja                                 BMCWEB_LOG_WARNING << "DBUS response error: "
47925e055a3SRavi Teja                                                    << 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;
49006785244SRatan Gupta                         },
49106785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
49206785244SRatan Gupta                         propertyInterface, "Set",
49306785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
49406785244SRatan Gupta                         "Privilege",
495168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
49606785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
49706785244SRatan Gupta                 }
49806785244SRatan Gupta             }
49906785244SRatan Gupta             // Create a new RoleMapping Object.
50006785244SRatan Gupta             else
50106785244SRatan Gupta             {
50206785244SRatan Gupta                 BMCWEB_LOG_DEBUG
50306785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
50489492a15SPatrick Williams                 std::string pathString = "RemoteRoleMapping/" +
50589492a15SPatrick Williams                                          std::to_string(index);
50606785244SRatan Gupta 
50706785244SRatan Gupta                 if (!localRole)
50806785244SRatan Gupta                 {
50906785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
51006785244SRatan Gupta                                               pathString + "/LocalRole");
51106785244SRatan Gupta                     continue;
51206785244SRatan Gupta                 }
51306785244SRatan Gupta                 if (!remoteGroup)
51406785244SRatan Gupta                 {
51506785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
51606785244SRatan Gupta                                               pathString + "/RemoteGroup");
51706785244SRatan Gupta                     continue;
51806785244SRatan Gupta                 }
51906785244SRatan Gupta 
52006785244SRatan Gupta                 std::string dbusObjectPath;
52106785244SRatan Gupta                 if (serverType == "ActiveDirectory")
52206785244SRatan Gupta                 {
5232c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
52406785244SRatan Gupta                 }
52506785244SRatan Gupta                 else if (serverType == "LDAP")
52606785244SRatan Gupta                 {
52723a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
52806785244SRatan Gupta                 }
52906785244SRatan Gupta 
53006785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
53106785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
53206785244SRatan Gupta 
53306785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
534271584abSEd Tanous                     [asyncResp, serverType, localRole,
5355e7e2dc5SEd Tanous                      remoteGroup](const boost::system::error_code& ec) {
53606785244SRatan Gupta                     if (ec)
53706785244SRatan Gupta                     {
53806785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
53906785244SRatan Gupta                         messages::internalError(asyncResp->res);
54006785244SRatan Gupta                         return;
54106785244SRatan Gupta                     }
54206785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
54306785244SRatan Gupta                         asyncResp->res
54406785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
5451476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
5461476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
5471476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
548b2ba3072SPatrick Williams                     remoteRoleJson.emplace_back(std::move(roleMapEntry));
54906785244SRatan Gupta                     },
55006785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
5513174e4dfSEd Tanous                     "Create", *remoteGroup,
55206785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
55306785244SRatan Gupta             }
55406785244SRatan Gupta         }
55506785244SRatan Gupta     }
55606785244SRatan Gupta }
55706785244SRatan Gupta 
55806785244SRatan Gupta /**
5596973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
5606973a582SRatan Gupta  * into JSON
5616973a582SRatan Gupta  */
5626973a582SRatan Gupta template <typename CallbackFunc>
5636973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
5646973a582SRatan Gupta                               CallbackFunc&& callback)
5656973a582SRatan Gupta {
5662b73119cSGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
5672b73119cSGeorge Liu         ldapEnableInterface, ldapConfigInterface};
56854fc587aSNagaraju Goruganti 
5692b73119cSGeorge Liu     dbus::utility::getDbusObject(
5702b73119cSGeorge Liu         ldapConfigObjectName, interfaces,
5712b73119cSGeorge Liu         [callback, ldapType](const boost::system::error_code& ec,
572b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
57354fc587aSNagaraju Goruganti         if (ec || resp.empty())
57454fc587aSNagaraju Goruganti         {
5750fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
576002d39b4SEd Tanous                 << "DBUS response error during getting of service name: " << ec;
57723a21a1cSEd Tanous             LDAPConfigData empty{};
57823a21a1cSEd Tanous             callback(false, empty, ldapType);
57954fc587aSNagaraju Goruganti             return;
58054fc587aSNagaraju Goruganti         }
58154fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
5825eb468daSGeorge Liu         sdbusplus::message::object_path path(ldapRootObject);
5835eb468daSGeorge Liu         dbus::utility::getManagedObjects(
5845eb468daSGeorge Liu             service, path,
585002d39b4SEd Tanous             [callback,
5865e7e2dc5SEd Tanous              ldapType](const boost::system::error_code& errorCode,
587711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
5886973a582SRatan Gupta             LDAPConfigData confData{};
58981ce609eSEd Tanous             if (errorCode)
5906973a582SRatan Gupta             {
591ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
592002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode;
5936973a582SRatan Gupta                 return;
5946973a582SRatan Gupta             }
595ab828d7cSRatan Gupta 
596ab828d7cSRatan Gupta             std::string ldapDbusType;
59754fc587aSNagaraju Goruganti             std::string searchString;
59854fc587aSNagaraju Goruganti 
599ab828d7cSRatan Gupta             if (ldapType == "LDAP")
600ab828d7cSRatan Gupta             {
6010fda0f12SGeorge Liu                 ldapDbusType =
6020fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
60354fc587aSNagaraju Goruganti                 searchString = "openldap";
604ab828d7cSRatan Gupta             }
605ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
606ab828d7cSRatan Gupta             {
60754fc587aSNagaraju Goruganti                 ldapDbusType =
6080fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
60954fc587aSNagaraju Goruganti                 searchString = "active_directory";
610ab828d7cSRatan Gupta             }
611ab828d7cSRatan Gupta             else
612ab828d7cSRatan Gupta             {
613002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type="
614ab828d7cSRatan Gupta                                  << ldapType;
615ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
616ab828d7cSRatan Gupta                 return;
617ab828d7cSRatan Gupta             }
618ab828d7cSRatan Gupta 
619ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
620ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
621ab828d7cSRatan Gupta 
6226973a582SRatan Gupta             for (const auto& object : ldapObjects)
6236973a582SRatan Gupta             {
62454fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
62554fc587aSNagaraju Goruganti                 // given type
626002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
6276973a582SRatan Gupta                 {
628ab828d7cSRatan Gupta                     continue;
629ab828d7cSRatan Gupta                 }
630ab828d7cSRatan Gupta 
6316973a582SRatan Gupta                 for (const auto& interface : object.second)
6326973a582SRatan Gupta                 {
6336973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
6346973a582SRatan Gupta                     {
6356973a582SRatan Gupta                         // rest of the properties are string.
6366973a582SRatan Gupta                         for (const auto& property : interface.second)
6376973a582SRatan Gupta                         {
6386973a582SRatan Gupta                             if (property.first == "Enabled")
6396973a582SRatan Gupta                             {
6406973a582SRatan Gupta                                 const bool* value =
6416973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
6426973a582SRatan Gupta                                 if (value == nullptr)
6436973a582SRatan Gupta                                 {
6446973a582SRatan Gupta                                     continue;
6456973a582SRatan Gupta                                 }
6466973a582SRatan Gupta                                 confData.serviceEnabled = *value;
6476973a582SRatan Gupta                                 break;
6486973a582SRatan Gupta                             }
6496973a582SRatan Gupta                         }
6506973a582SRatan Gupta                     }
6516973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
6526973a582SRatan Gupta                     {
6536973a582SRatan Gupta                         for (const auto& property : interface.second)
6546973a582SRatan Gupta                         {
655271584abSEd Tanous                             const std::string* strValue =
656002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
657271584abSEd Tanous                             if (strValue == nullptr)
6586973a582SRatan Gupta                             {
6596973a582SRatan Gupta                                 continue;
6606973a582SRatan Gupta                             }
6616973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
6626973a582SRatan Gupta                             {
663271584abSEd Tanous                                 confData.uri = *strValue;
6646973a582SRatan Gupta                             }
6656973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
6666973a582SRatan Gupta                             {
667271584abSEd Tanous                                 confData.bindDN = *strValue;
6686973a582SRatan Gupta                             }
6696973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
6706973a582SRatan Gupta                             {
671271584abSEd Tanous                                 confData.baseDN = *strValue;
6726973a582SRatan Gupta                             }
673002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
6746973a582SRatan Gupta                             {
675271584abSEd Tanous                                 confData.searchScope = *strValue;
6766973a582SRatan Gupta                             }
677002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
6786973a582SRatan Gupta                             {
679271584abSEd Tanous                                 confData.groupAttribute = *strValue;
6806973a582SRatan Gupta                             }
681002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
6826973a582SRatan Gupta                             {
683271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
6846973a582SRatan Gupta                             }
68554fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
686ab828d7cSRatan Gupta                             {
687271584abSEd Tanous                                 confData.serverType = *strValue;
68854fc587aSNagaraju Goruganti                             }
68954fc587aSNagaraju Goruganti                         }
69054fc587aSNagaraju Goruganti                     }
691002d39b4SEd Tanous                     else if (interface.first ==
6920fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
69354fc587aSNagaraju Goruganti                     {
69454fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
69554fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
69654fc587aSNagaraju Goruganti                         {
697271584abSEd Tanous                             const std::string* strValue =
698002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
69954fc587aSNagaraju Goruganti 
700271584abSEd Tanous                             if (strValue == nullptr)
70154fc587aSNagaraju Goruganti                             {
70254fc587aSNagaraju Goruganti                                 continue;
70354fc587aSNagaraju Goruganti                             }
70454fc587aSNagaraju Goruganti 
70554fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
70654fc587aSNagaraju Goruganti                             {
707271584abSEd Tanous                                 roleMapData.groupName = *strValue;
70854fc587aSNagaraju Goruganti                             }
70954fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
71054fc587aSNagaraju Goruganti                             {
711271584abSEd Tanous                                 roleMapData.privilege = *strValue;
71254fc587aSNagaraju Goruganti                             }
71354fc587aSNagaraju Goruganti                         }
71454fc587aSNagaraju Goruganti 
715002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
716002d39b4SEd Tanous                                                             roleMapData);
71754fc587aSNagaraju Goruganti                     }
71854fc587aSNagaraju Goruganti                 }
71954fc587aSNagaraju Goruganti             }
720ab828d7cSRatan Gupta             callback(true, confData, ldapType);
7215eb468daSGeorge Liu             });
7222b73119cSGeorge Liu         });
7236973a582SRatan Gupta }
7246973a582SRatan Gupta 
7258a07d286SRatan Gupta /**
7268a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
7278a07d286SRatan Gupta  * @param input JSON data
7288a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7298a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
7308a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
7318a07d286SRatan Gupta  */
7324f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
7336c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7346c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
7358a07d286SRatan Gupta {
7368a07d286SRatan Gupta     std::optional<std::string> authType;
7378a07d286SRatan Gupta 
7388a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
7398a07d286SRatan Gupta                              authType, "Username", username, "Password",
7408a07d286SRatan Gupta                              password))
7418a07d286SRatan Gupta     {
7428a07d286SRatan Gupta         return;
7438a07d286SRatan Gupta     }
7448a07d286SRatan Gupta     if (!authType)
7458a07d286SRatan Gupta     {
7468a07d286SRatan Gupta         return;
7478a07d286SRatan Gupta     }
7488a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
7498a07d286SRatan Gupta     {
7508a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
7518a07d286SRatan Gupta                                          "AuthenticationType");
7528a07d286SRatan Gupta         return;
7538a07d286SRatan Gupta     }
7548a07d286SRatan Gupta }
7558a07d286SRatan Gupta /**
7568a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
7578a07d286SRatan Gupta  * @param input JSON data
7588a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7598a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
7608a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
7618a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
7628a07d286SRatan Gupta  */
7638a07d286SRatan Gupta 
7644f48d5f6SEd Tanous inline void
7654f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
7668d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7678a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
7688a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
7698a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
7708a07d286SRatan Gupta {
7718a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
7728a07d286SRatan Gupta 
7738a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
7748a07d286SRatan Gupta                              searchSettings))
7758a07d286SRatan Gupta     {
7768a07d286SRatan Gupta         return;
7778a07d286SRatan Gupta     }
7788a07d286SRatan Gupta     if (!searchSettings)
7798a07d286SRatan Gupta     {
7808a07d286SRatan Gupta         return;
7818a07d286SRatan Gupta     }
7828a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
7838a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
7848a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
7858a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
7868a07d286SRatan Gupta     {
7878a07d286SRatan Gupta         return;
7888a07d286SRatan Gupta     }
7898a07d286SRatan Gupta }
7908a07d286SRatan Gupta /**
7918a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
7928a07d286SRatan Gupta           json response with the new value.
7938a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
7948a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7958a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7968a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7978a07d286SRatan Gupta  */
7988a07d286SRatan Gupta 
7994f48d5f6SEd Tanous inline void handleServiceAddressPatch(
8008a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
8018d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8028a07d286SRatan Gupta     const std::string& ldapServerElementName,
8038a07d286SRatan Gupta     const std::string& ldapConfigObject)
8048a07d286SRatan Gupta {
8058a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
806*7a543894SPatrick Williams         [asyncResp, ldapServerElementName, serviceAddressList](
807*7a543894SPatrick Williams             const boost::system::error_code& ec, sdbusplus::message_t& msg) {
8088a07d286SRatan Gupta         if (ec)
8098a07d286SRatan Gupta         {
81025e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
81125e055a3SRavi Teja             if ((dbusError != nullptr) &&
81225e055a3SRavi Teja                 (dbusError->name ==
81325e055a3SRavi Teja                  std::string_view(
81425e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
81525e055a3SRavi Teja             {
81625e055a3SRavi Teja                 BMCWEB_LOG_WARNING
817c61704abSGunnar Mills                     << "Error Occurred in updating the service address";
81825e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
81925e055a3SRavi Teja                                                  "ServiceAddresses",
82025e055a3SRavi Teja                                                  serviceAddressList.front());
82125e055a3SRavi Teja                 return;
82225e055a3SRavi Teja             }
8238a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8248a07d286SRatan Gupta             return;
8258a07d286SRatan Gupta         }
8268a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
8278a07d286SRatan Gupta             serviceAddressList.front()};
828002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
8298a07d286SRatan Gupta             modifiedserviceAddressList;
8308a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
8318a07d286SRatan Gupta         {
832002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
8338a07d286SRatan Gupta                                             serviceAddressList.front());
8348a07d286SRatan Gupta         }
8358a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
8368a07d286SRatan Gupta         },
8378a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8388a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
839168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
8408a07d286SRatan Gupta }
8418a07d286SRatan Gupta /**
8428a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
8438a07d286SRatan Gupta           json response with the new value.
8448a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
8458a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8468a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8478a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8488a07d286SRatan Gupta  */
8498a07d286SRatan Gupta 
8504f48d5f6SEd Tanous inline void
8514f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
8528d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8538a07d286SRatan Gupta                         const std::string& ldapServerElementName,
8548a07d286SRatan Gupta                         const std::string& ldapConfigObject)
8558a07d286SRatan Gupta {
8568a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8578a07d286SRatan Gupta         [asyncResp, username,
8585e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
8598a07d286SRatan Gupta         if (ec)
8608a07d286SRatan Gupta         {
8616c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
8628a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8638a07d286SRatan Gupta             return;
8648a07d286SRatan Gupta         }
86589492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
86689492a15SPatrick Williams                                 ["Username"] = username;
8678a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
8688a07d286SRatan Gupta         },
8698a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
870168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
871168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
8728a07d286SRatan Gupta }
8738a07d286SRatan Gupta 
8748a07d286SRatan Gupta /**
8758a07d286SRatan Gupta  * @brief updates the LDAP password
8768a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
8778a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8788a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8798a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
8808a07d286SRatan Gupta  */
8818a07d286SRatan Gupta 
8824f48d5f6SEd Tanous inline void
8834f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
8848d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8858a07d286SRatan Gupta                         const std::string& ldapServerElementName,
8868a07d286SRatan Gupta                         const std::string& ldapConfigObject)
8878a07d286SRatan Gupta {
8888a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8898a07d286SRatan Gupta         [asyncResp, password,
8905e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
8918a07d286SRatan Gupta         if (ec)
8928a07d286SRatan Gupta         {
8936c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
8948a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8958a07d286SRatan Gupta             return;
8968a07d286SRatan Gupta         }
89789492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
89889492a15SPatrick Williams                                 ["Password"] = "";
8998a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
9008a07d286SRatan Gupta         },
9018a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
9028a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
903168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
9048a07d286SRatan Gupta }
9058a07d286SRatan Gupta 
9068a07d286SRatan Gupta /**
9078a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
9088a07d286SRatan Gupta           json response with the new value.
9098a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
9108a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9118a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9128a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9138a07d286SRatan Gupta  */
9148a07d286SRatan Gupta 
9154f48d5f6SEd Tanous inline void
9164f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
9178d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9188a07d286SRatan Gupta                       const std::string& ldapServerElementName,
9198a07d286SRatan Gupta                       const std::string& ldapConfigObject)
9208a07d286SRatan Gupta {
9218a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
9228a07d286SRatan Gupta         [asyncResp, baseDNList,
92325e055a3SRavi Teja          ldapServerElementName](const boost::system::error_code& ec,
924*7a543894SPatrick Williams                                 const sdbusplus::message_t& msg) {
9258a07d286SRatan Gupta         if (ec)
9268a07d286SRatan Gupta         {
9276c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
92825e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
92925e055a3SRavi Teja             if ((dbusError != nullptr) &&
93025e055a3SRavi Teja                 (dbusError->name ==
93125e055a3SRavi Teja                  std::string_view(
93225e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
93325e055a3SRavi Teja             {
93425e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
93525e055a3SRavi Teja                                                  "BaseDistinguishedNames",
93625e055a3SRavi Teja                                                  baseDNList.front());
93725e055a3SRavi Teja                 return;
93825e055a3SRavi Teja             }
9398a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9408a07d286SRatan Gupta             return;
9418a07d286SRatan Gupta         }
942002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
9438a07d286SRatan Gupta         auto& searchSettingsJson =
9448a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
9456c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
9466c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
9478a07d286SRatan Gupta         if (baseDNList.size() > 1)
9488a07d286SRatan Gupta         {
949002d39b4SEd Tanous             messages::propertyValueModified(
950002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
9518a07d286SRatan Gupta         }
9528a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
9538a07d286SRatan Gupta         },
9548a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
9558a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
956168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
9578a07d286SRatan Gupta }
9588a07d286SRatan Gupta /**
9598a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
9608a07d286SRatan Gupta           json response with the new value.
9618a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
9628a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9638a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9648a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9658a07d286SRatan Gupta  */
9668a07d286SRatan Gupta 
9674f48d5f6SEd Tanous inline void
9684f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
9698d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9708a07d286SRatan Gupta                             const std::string& ldapServerElementName,
9718a07d286SRatan Gupta                             const std::string& ldapConfigObject)
9728a07d286SRatan Gupta {
9738a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
9748a07d286SRatan Gupta         [asyncResp, userNameAttribute,
9755e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
9768a07d286SRatan Gupta         if (ec)
9778a07d286SRatan Gupta         {
978c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
9798a07d286SRatan Gupta                                 "username attribute";
9808a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9818a07d286SRatan Gupta             return;
9828a07d286SRatan Gupta         }
983002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
9848a07d286SRatan Gupta         auto& searchSettingsJson =
9858a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
9868a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
9878a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
9888a07d286SRatan Gupta         },
9898a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
9908a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
991168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
9928a07d286SRatan Gupta }
9938a07d286SRatan Gupta /**
9948a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
9958a07d286SRatan Gupta           json response with the new value.
9968a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
9978a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9988a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9998a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
10008a07d286SRatan Gupta  */
10018a07d286SRatan Gupta 
10024f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
10038d1b46d7Szhanghch05     const std::string& groupsAttribute,
10048d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10058a07d286SRatan Gupta     const std::string& ldapServerElementName,
10068a07d286SRatan Gupta     const std::string& ldapConfigObject)
10078a07d286SRatan Gupta {
10088a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
10098a07d286SRatan Gupta         [asyncResp, groupsAttribute,
10105e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
10118a07d286SRatan Gupta         if (ec)
10128a07d286SRatan Gupta         {
1013c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
10148a07d286SRatan Gupta                                 "groupname attribute";
10158a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10168a07d286SRatan Gupta             return;
10178a07d286SRatan Gupta         }
1018002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
10198a07d286SRatan Gupta         auto& searchSettingsJson =
10208a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
10218a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
10228a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
10238a07d286SRatan Gupta         },
10248a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
10258a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
1026168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
10278a07d286SRatan Gupta }
10288a07d286SRatan Gupta /**
10298a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
10308a07d286SRatan Gupta           json response with the new value.
10318a07d286SRatan Gupta  * @param input JSON data.
10328a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
10338a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
10348a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
10358a07d286SRatan Gupta  */
10368a07d286SRatan Gupta 
10374f48d5f6SEd Tanous inline void handleServiceEnablePatch(
10386c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10398a07d286SRatan Gupta     const std::string& ldapServerElementName,
10408a07d286SRatan Gupta     const std::string& ldapConfigObject)
10418a07d286SRatan Gupta {
10428a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
10438a07d286SRatan Gupta         [asyncResp, serviceEnabled,
10445e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
10458a07d286SRatan Gupta         if (ec)
10468a07d286SRatan Gupta         {
1047002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
10488a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10498a07d286SRatan Gupta             return;
10508a07d286SRatan Gupta         }
10516c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
10528a07d286SRatan Gupta             serviceEnabled;
10536c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
10548a07d286SRatan Gupta         },
10558a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
1056168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
1057168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
10588a07d286SRatan Gupta }
10598a07d286SRatan Gupta 
10604f48d5f6SEd Tanous inline void
10614f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
10628d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
106378158631SZbigniew Kurzynski {
106478158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
106578158631SZbigniew Kurzynski     std::optional<bool> cookie;
106678158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
106778158631SZbigniew Kurzynski     std::optional<bool> xToken;
1068501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
106978158631SZbigniew Kurzynski 
107078158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
107178158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
1072501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
107378158631SZbigniew Kurzynski     {
107478158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
107578158631SZbigniew Kurzynski         return;
107678158631SZbigniew Kurzynski     }
107778158631SZbigniew Kurzynski 
107878158631SZbigniew Kurzynski     // Make a copy of methods configuration
107952cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
108052cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
108178158631SZbigniew Kurzynski 
108278158631SZbigniew Kurzynski     if (basicAuth)
108378158631SZbigniew Kurzynski     {
1084f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
1085f16f6263SAlan Kuo         messages::actionNotSupported(
10860fda0f12SGeorge Liu             asyncResp->res,
10870fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
1088f16f6263SAlan Kuo         return;
1089f16f6263SAlan Kuo #endif
109078158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
109178158631SZbigniew Kurzynski     }
109278158631SZbigniew Kurzynski 
109378158631SZbigniew Kurzynski     if (cookie)
109478158631SZbigniew Kurzynski     {
1095f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
10960fda0f12SGeorge Liu         messages::actionNotSupported(
10970fda0f12SGeorge Liu             asyncResp->res,
10980fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
1099f16f6263SAlan Kuo         return;
1100f16f6263SAlan Kuo #endif
110178158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
110278158631SZbigniew Kurzynski     }
110378158631SZbigniew Kurzynski 
110478158631SZbigniew Kurzynski     if (sessionToken)
110578158631SZbigniew Kurzynski     {
1106f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
1107f16f6263SAlan Kuo         messages::actionNotSupported(
11080fda0f12SGeorge Liu             asyncResp->res,
11090fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
1110f16f6263SAlan Kuo         return;
1111f16f6263SAlan Kuo #endif
111278158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
111378158631SZbigniew Kurzynski     }
111478158631SZbigniew Kurzynski 
111578158631SZbigniew Kurzynski     if (xToken)
111678158631SZbigniew Kurzynski     {
1117f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
11180fda0f12SGeorge Liu         messages::actionNotSupported(
11190fda0f12SGeorge Liu             asyncResp->res,
11200fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
1121f16f6263SAlan Kuo         return;
1122f16f6263SAlan Kuo #endif
112378158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
112478158631SZbigniew Kurzynski     }
112578158631SZbigniew Kurzynski 
1126501f1e58SZbigniew Kurzynski     if (tls)
1127501f1e58SZbigniew Kurzynski     {
1128f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
11290fda0f12SGeorge Liu         messages::actionNotSupported(
11300fda0f12SGeorge Liu             asyncResp->res,
11310fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
1132f16f6263SAlan Kuo         return;
1133f16f6263SAlan Kuo #endif
1134501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
1135501f1e58SZbigniew Kurzynski     }
1136501f1e58SZbigniew Kurzynski 
113778158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
1138501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
1139501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
114078158631SZbigniew Kurzynski     {
114178158631SZbigniew Kurzynski         // Do not allow user to disable everything
114278158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
114378158631SZbigniew Kurzynski                                      "of disabling all available methods");
114478158631SZbigniew Kurzynski         return;
114578158631SZbigniew Kurzynski     }
114678158631SZbigniew Kurzynski 
114752cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
114852cc112dSEd Tanous         authMethodsConfig);
114978158631SZbigniew Kurzynski     // Save configuration immediately
115052cc112dSEd Tanous     persistent_data::getConfig().writeData();
115178158631SZbigniew Kurzynski 
115278158631SZbigniew Kurzynski     messages::success(asyncResp->res);
115378158631SZbigniew Kurzynski }
115478158631SZbigniew Kurzynski 
11558a07d286SRatan Gupta /**
11568a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
11578a07d286SRatan Gupta  *        value and create the LDAP config object.
11588a07d286SRatan Gupta  * @param input JSON data
11598a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
11608a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
11618a07d286SRatan Gupta  */
11628a07d286SRatan Gupta 
11636c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
11648d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
11658a07d286SRatan Gupta                             const std::string& serverType)
11668a07d286SRatan Gupta {
1167eb2bbe56SRatan Gupta     std::string dbusObjectPath;
1168eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
1169eb2bbe56SRatan Gupta     {
11702c70f800SEd Tanous         dbusObjectPath = adConfigObject;
1171eb2bbe56SRatan Gupta     }
1172eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
1173eb2bbe56SRatan Gupta     {
117423a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
1175eb2bbe56SRatan Gupta     }
1176cb13a392SEd Tanous     else
1177cb13a392SEd Tanous     {
1178cb13a392SEd Tanous         return;
1179cb13a392SEd Tanous     }
1180eb2bbe56SRatan Gupta 
11818a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
11828a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
11838a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
11848a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
11858a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
11868a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
11878a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
11888a07d286SRatan Gupta     std::optional<std::string> userName;
11898a07d286SRatan Gupta     std::optional<std::string> password;
119006785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
11918a07d286SRatan Gupta 
11928a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
11938a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
11948a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
119506785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
119606785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
11978a07d286SRatan Gupta     {
11988a07d286SRatan Gupta         return;
11998a07d286SRatan Gupta     }
12008a07d286SRatan Gupta 
12018a07d286SRatan Gupta     if (authentication)
12028a07d286SRatan Gupta     {
12038a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
12048a07d286SRatan Gupta                                     password);
12058a07d286SRatan Gupta     }
12068a07d286SRatan Gupta     if (ldapService)
12078a07d286SRatan Gupta     {
12088a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
12098a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
12108a07d286SRatan Gupta     }
12118a07d286SRatan Gupta     if (serviceAddressList)
12128a07d286SRatan Gupta     {
121326f6976fSEd Tanous         if (serviceAddressList->empty())
12148a07d286SRatan Gupta         {
1215e2616cc5SEd Tanous             messages::propertyValueNotInList(
1216e2616cc5SEd Tanous                 asyncResp->res, *serviceAddressList, "ServiceAddress");
12178a07d286SRatan Gupta             return;
12188a07d286SRatan Gupta         }
12198a07d286SRatan Gupta     }
12208a07d286SRatan Gupta     if (baseDNList)
12218a07d286SRatan Gupta     {
122226f6976fSEd Tanous         if (baseDNList->empty())
12238a07d286SRatan Gupta         {
1224e2616cc5SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *baseDNList,
12258a07d286SRatan Gupta                                              "BaseDistinguishedNames");
12268a07d286SRatan Gupta             return;
12278a07d286SRatan Gupta         }
12288a07d286SRatan Gupta     }
12298a07d286SRatan Gupta 
12308a07d286SRatan Gupta     // nothing to update, then return
12318a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
123206785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
123306785244SRatan Gupta         !remoteRoleMapData)
12348a07d286SRatan Gupta     {
12358a07d286SRatan Gupta         return;
12368a07d286SRatan Gupta     }
12378a07d286SRatan Gupta 
12388a07d286SRatan Gupta     // Get the existing resource first then keep modifying
12398a07d286SRatan Gupta     // whenever any property gets updated.
1240002d39b4SEd Tanous     getLDAPConfigData(
1241002d39b4SEd Tanous         serverType,
1242002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1243002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1244002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
124523a21a1cSEd Tanous                             const std::string& serverT) {
12468a07d286SRatan Gupta         if (!success)
12478a07d286SRatan Gupta         {
12488a07d286SRatan Gupta             messages::internalError(asyncResp->res);
12498a07d286SRatan Gupta             return;
12508a07d286SRatan Gupta         }
12516c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
12528a07d286SRatan Gupta         if (confData.serviceEnabled)
12538a07d286SRatan Gupta         {
12548a07d286SRatan Gupta             // Disable the service first and update the rest of
12558a07d286SRatan Gupta             // the properties.
12566c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
12578a07d286SRatan Gupta         }
12588a07d286SRatan Gupta 
12598a07d286SRatan Gupta         if (serviceAddressList)
12608a07d286SRatan Gupta         {
12616c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
12626c51eab1SEd Tanous                                       dbusObjectPath);
12638a07d286SRatan Gupta         }
12648a07d286SRatan Gupta         if (userName)
12658a07d286SRatan Gupta         {
12666c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
12678a07d286SRatan Gupta         }
12688a07d286SRatan Gupta         if (password)
12698a07d286SRatan Gupta         {
12706c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
12718a07d286SRatan Gupta         }
12728a07d286SRatan Gupta 
12738a07d286SRatan Gupta         if (baseDNList)
12748a07d286SRatan Gupta         {
12756c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
12768a07d286SRatan Gupta         }
12778a07d286SRatan Gupta         if (userNameAttribute)
12788a07d286SRatan Gupta         {
12796c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
12806c51eab1SEd Tanous                                     dbusObjectPath);
12818a07d286SRatan Gupta         }
12828a07d286SRatan Gupta         if (groupsAttribute)
12838a07d286SRatan Gupta         {
12846c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
12856c51eab1SEd Tanous                                      dbusObjectPath);
12868a07d286SRatan Gupta         }
12878a07d286SRatan Gupta         if (serviceEnabled)
12888a07d286SRatan Gupta         {
12898a07d286SRatan Gupta             // if user has given the value as true then enable
12908a07d286SRatan Gupta             // the service. if user has given false then no-op
12918a07d286SRatan Gupta             // as service is already stopped.
12928a07d286SRatan Gupta             if (*serviceEnabled)
12938a07d286SRatan Gupta             {
12946c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
12956c51eab1SEd Tanous                                          dbusObjectPath);
12968a07d286SRatan Gupta             }
12978a07d286SRatan Gupta         }
12988a07d286SRatan Gupta         else
12998a07d286SRatan Gupta         {
13008a07d286SRatan Gupta             // if user has not given the service enabled value
13018a07d286SRatan Gupta             // then revert it to the same state as it was
13028a07d286SRatan Gupta             // before.
13038a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
130423a21a1cSEd Tanous                                      serverT, dbusObjectPath);
13058a07d286SRatan Gupta         }
130606785244SRatan Gupta 
130706785244SRatan Gupta         if (remoteRoleMapData)
130806785244SRatan Gupta         {
13096c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
13106c51eab1SEd Tanous                                *remoteRoleMapData);
131106785244SRatan Gupta         }
13128a07d286SRatan Gupta         });
13138a07d286SRatan Gupta }
1314d4b5443fSEd Tanous 
131558345856SAbhishek Patel inline void updateUserProperties(
131658345856SAbhishek Patel     std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& username,
1317618c14b4SEd Tanous     const std::optional<std::string>& password,
1318618c14b4SEd Tanous     const std::optional<bool>& enabled,
131958345856SAbhishek Patel     const std::optional<std::string>& roleId, const std::optional<bool>& locked,
132058345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes, bool userSelf)
13211abe55efSEd Tanous {
1322b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1323b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1324b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
13256c51eab1SEd Tanous 
13266c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1327618c14b4SEd Tanous         dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
132858345856SAbhishek Patel                          locked, accountTypes(std::move(accountTypes)),
132958345856SAbhishek Patel                          userSelf, asyncResp{std::move(asyncResp)}](int rc) {
1330e662eae8SEd Tanous             if (rc <= 0)
13316c51eab1SEd Tanous             {
1332d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
13336c51eab1SEd Tanous                                            username);
13346c51eab1SEd Tanous                 return;
13356c51eab1SEd Tanous             }
13366c51eab1SEd Tanous 
13376c51eab1SEd Tanous             if (password)
13386c51eab1SEd Tanous             {
13396c51eab1SEd Tanous                 int retval = pamUpdatePassword(username, *password);
13406c51eab1SEd Tanous 
13416c51eab1SEd Tanous                 if (retval == PAM_USER_UNKNOWN)
13426c51eab1SEd Tanous                 {
1343d8a5d5d8SJiaqing Zhao                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
13446c51eab1SEd Tanous                                                username);
13456c51eab1SEd Tanous                 }
13466c51eab1SEd Tanous                 else if (retval == PAM_AUTHTOK_ERR)
13476c51eab1SEd Tanous                 {
13486c51eab1SEd Tanous                     // If password is invalid
1349618c14b4SEd Tanous                     messages::propertyValueFormatError(asyncResp->res,
1350618c14b4SEd Tanous                                                        *password, "Password");
13516c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
13526c51eab1SEd Tanous                 }
13536c51eab1SEd Tanous                 else if (retval != PAM_SUCCESS)
13546c51eab1SEd Tanous                 {
13556c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
13566c51eab1SEd Tanous                     return;
13576c51eab1SEd Tanous                 }
1358e7b1b62bSEd Tanous                 else
1359e7b1b62bSEd Tanous                 {
1360e7b1b62bSEd Tanous                     messages::success(asyncResp->res);
1361e7b1b62bSEd Tanous                 }
13626c51eab1SEd Tanous             }
13636c51eab1SEd Tanous 
13646c51eab1SEd Tanous             if (enabled)
13656c51eab1SEd Tanous             {
13666c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
13675e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
13686c51eab1SEd Tanous                 if (ec)
13696c51eab1SEd Tanous                 {
13706c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
13716c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
13726c51eab1SEd Tanous                     return;
13736c51eab1SEd Tanous                 }
13746c51eab1SEd Tanous                 messages::success(asyncResp->res);
13756c51eab1SEd Tanous                 return;
13766c51eab1SEd Tanous                     },
1377e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
13786c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
13796c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserEnabled",
1380168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*enabled});
13816c51eab1SEd Tanous             }
13826c51eab1SEd Tanous 
13836c51eab1SEd Tanous             if (roleId)
13846c51eab1SEd Tanous             {
13856c51eab1SEd Tanous                 std::string priv = getPrivilegeFromRoleId(*roleId);
13866c51eab1SEd Tanous                 if (priv.empty())
13876c51eab1SEd Tanous                 {
1388e2616cc5SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, true,
1389e2616cc5SEd Tanous                                                      "Locked");
13906c51eab1SEd Tanous                     return;
13916c51eab1SEd Tanous                 }
13926c51eab1SEd Tanous 
13936c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
13945e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
13956c51eab1SEd Tanous                 if (ec)
13966c51eab1SEd Tanous                 {
13976c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
13986c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
13996c51eab1SEd Tanous                     return;
14006c51eab1SEd Tanous                 }
14016c51eab1SEd Tanous                 messages::success(asyncResp->res);
14026c51eab1SEd Tanous                     },
1403e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
14046c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
14056c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1406168e20c1SEd Tanous                     dbus::utility::DbusVariantType{priv});
14076c51eab1SEd Tanous             }
14086c51eab1SEd Tanous 
14096c51eab1SEd Tanous             if (locked)
14106c51eab1SEd Tanous             {
14116c51eab1SEd Tanous                 // admin can unlock the account which is locked by
14126c51eab1SEd Tanous                 // successive authentication failures but admin should
14136c51eab1SEd Tanous                 // not be allowed to lock an account.
14146c51eab1SEd Tanous                 if (*locked)
14156c51eab1SEd Tanous                 {
14166c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, "true",
14176c51eab1SEd Tanous                                                      "Locked");
14186c51eab1SEd Tanous                     return;
14196c51eab1SEd Tanous                 }
14206c51eab1SEd Tanous 
14216c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
14225e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
14236c51eab1SEd Tanous                 if (ec)
14246c51eab1SEd Tanous                 {
14256c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
14266c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
14276c51eab1SEd Tanous                     return;
14286c51eab1SEd Tanous                 }
14296c51eab1SEd Tanous                 messages::success(asyncResp->res);
14306c51eab1SEd Tanous                 return;
14316c51eab1SEd Tanous                     },
1432e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
14336c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
14346c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes",
1435168e20c1SEd Tanous                     "UserLockedForFailedAttempt",
1436168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*locked});
14376c51eab1SEd Tanous             }
143858345856SAbhishek Patel 
143958345856SAbhishek Patel             if (accountTypes)
144058345856SAbhishek Patel             {
144158345856SAbhishek Patel                 patchAccountTypes(*accountTypes, asyncResp, dbusObjectPath,
144258345856SAbhishek Patel                                   userSelf);
144358345856SAbhishek Patel             }
14446c51eab1SEd Tanous         });
14456c51eab1SEd Tanous }
14466c51eab1SEd Tanous 
14474c7d4d33SEd Tanous inline void handleAccountServiceHead(
14484c7d4d33SEd Tanous     App& app, const crow::Request& req,
14491ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14506c51eab1SEd Tanous {
14513ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
145245ca1b86SEd Tanous     {
145345ca1b86SEd Tanous         return;
145445ca1b86SEd Tanous     }
14554c7d4d33SEd Tanous     asyncResp->res.addHeader(
14564c7d4d33SEd Tanous         boost::beast::http::field::link,
14574c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
14584c7d4d33SEd Tanous }
14594c7d4d33SEd Tanous 
14604c7d4d33SEd Tanous inline void
14614c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
14624c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14634c7d4d33SEd Tanous {
1464afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1465afd369c6SJiaqing Zhao     {
1466afd369c6SJiaqing Zhao         return;
1467afd369c6SJiaqing Zhao     }
14683e72c202SNinad Palsule 
14693e72c202SNinad Palsule     if (req.session == nullptr)
14703e72c202SNinad Palsule     {
14713e72c202SNinad Palsule         messages::internalError(asyncResp->res);
14723e72c202SNinad Palsule         return;
14733e72c202SNinad Palsule     }
14743e72c202SNinad Palsule 
1475afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1476afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1477afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1478afd369c6SJiaqing Zhao 
147952cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
14801ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
148178158631SZbigniew Kurzynski 
14821476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
14831476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
14841476687dSEd Tanous     json["@odata.type"] = "#AccountService."
14851476687dSEd Tanous                           "v1_10_0.AccountService";
14861476687dSEd Tanous     json["Id"] = "AccountService";
14871476687dSEd Tanous     json["Name"] = "Account Service";
14881476687dSEd Tanous     json["Description"] = "Account Service";
14891476687dSEd Tanous     json["ServiceEnabled"] = true;
14901476687dSEd Tanous     json["MaxPasswordLength"] = 20;
14911ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
14921476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
14931476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
14945b5574acSEd Tanous         "#OpenBMCAccountService.v1_0_0.AccountService";
14951476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
14961476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
14971476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
14981476687dSEd Tanous         authMethodsConfig.basic;
14991476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
15001476687dSEd Tanous         authMethodsConfig.sessionToken;
15011ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
15021ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
15031ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
15041476687dSEd Tanous 
15051ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
15061ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
15071ef4c342SEd Tanous     // permissions ConfigureManager
150872048780SAbhishek Patel     Privileges effectiveUserPrivileges =
15093e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
151072048780SAbhishek Patel 
151172048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
151272048780SAbhishek Patel                                          effectiveUserPrivileges))
151372048780SAbhishek Patel     {
15141ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
15151476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
151672048780SAbhishek Patel     }
1517d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1518d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1519d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
15205e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
15211ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
15223d958bbcSAppaRao Puli         if (ec)
15233d958bbcSAppaRao Puli         {
15243d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
15253d958bbcSAppaRao Puli             return;
15263d958bbcSAppaRao Puli         }
1527d1bde9e5SKrzysztof Grobelny 
15283d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
15293d958bbcSAppaRao Puli                          << "properties for AccountService";
1530d1bde9e5SKrzysztof Grobelny 
1531d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1532d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1533d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1534d1bde9e5SKrzysztof Grobelny 
1535d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1536d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1537d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1538d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1539d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1540d1bde9e5SKrzysztof Grobelny 
1541d1bde9e5SKrzysztof Grobelny         if (!success)
15423d958bbcSAppaRao Puli         {
1543d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1544d1bde9e5SKrzysztof Grobelny             return;
15453d958bbcSAppaRao Puli         }
1546d1bde9e5SKrzysztof Grobelny 
1547d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
15483d958bbcSAppaRao Puli         {
1549d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
15503d958bbcSAppaRao Puli         }
1551d1bde9e5SKrzysztof Grobelny 
1552d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
15533d958bbcSAppaRao Puli         {
1554d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1555d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1556d1bde9e5SKrzysztof Grobelny         }
1557d1bde9e5SKrzysztof Grobelny 
1558d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
15593d958bbcSAppaRao Puli         {
1560002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1561d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
15623d958bbcSAppaRao Puli         }
1563d1bde9e5SKrzysztof Grobelny         });
15646973a582SRatan Gupta 
156502cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1566ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1567cb13a392SEd Tanous         if (!success)
1568cb13a392SEd Tanous         {
1569cb13a392SEd Tanous             return;
1570cb13a392SEd Tanous         }
1571002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1572ab828d7cSRatan Gupta     };
1573ab828d7cSRatan Gupta 
1574ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1575ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
15761ef4c342SEd Tanous }
15776973a582SRatan Gupta 
15781ef4c342SEd Tanous inline void handleAccountServicePatch(
15791ef4c342SEd Tanous     App& app, const crow::Request& req,
15801ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15811ef4c342SEd Tanous {
15823ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
158345ca1b86SEd Tanous     {
158445ca1b86SEd Tanous         return;
158545ca1b86SEd Tanous     }
1586f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1587f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1588ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1589f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1590f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1591f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1592f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1593f5ffd806SEd Tanous 
159415ed6780SWilly Tu     if (!json_util::readJsonPatch(
15951ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
15961ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
15971ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
15981ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1599f5ffd806SEd Tanous             oemObject))
1600f5ffd806SEd Tanous     {
1601f5ffd806SEd Tanous         return;
1602f5ffd806SEd Tanous     }
1603f5ffd806SEd Tanous 
1604f5ffd806SEd Tanous     if (minPasswordLength)
1605f5ffd806SEd Tanous     {
1606ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
16075e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1608ef73ad0dSPaul Fertser             if (ec)
1609ef73ad0dSPaul Fertser             {
1610ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1611ef73ad0dSPaul Fertser                 return;
1612ef73ad0dSPaul Fertser             }
1613ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1614ef73ad0dSPaul Fertser             },
16151ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1616ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
16171ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1618ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1619f5ffd806SEd Tanous     }
1620f5ffd806SEd Tanous 
1621f5ffd806SEd Tanous     if (maxPasswordLength)
1622f5ffd806SEd Tanous     {
16231ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1624f5ffd806SEd Tanous     }
1625f5ffd806SEd Tanous 
1626f5ffd806SEd Tanous     if (ldapObject)
1627f5ffd806SEd Tanous     {
1628f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1629f5ffd806SEd Tanous     }
1630f5ffd806SEd Tanous 
1631f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
16321ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1633f5ffd806SEd Tanous                                          oemOpenBMCObject))
1634f5ffd806SEd Tanous     {
1635f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1636f5ffd806SEd Tanous             oemOpenBMCObject &&
1637f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1638f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1639f5ffd806SEd Tanous         {
1640f5ffd806SEd Tanous             if (authMethodsObject)
1641f5ffd806SEd Tanous             {
16421ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1643f5ffd806SEd Tanous             }
1644f5ffd806SEd Tanous         }
1645f5ffd806SEd Tanous     }
1646f5ffd806SEd Tanous 
1647f5ffd806SEd Tanous     if (activeDirectoryObject)
1648f5ffd806SEd Tanous     {
16491ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1650f5ffd806SEd Tanous     }
1651f5ffd806SEd Tanous 
1652f5ffd806SEd Tanous     if (unlockTimeout)
1653f5ffd806SEd Tanous     {
1654f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
16555e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1656f5ffd806SEd Tanous             if (ec)
1657f5ffd806SEd Tanous             {
1658f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1659f5ffd806SEd Tanous                 return;
1660f5ffd806SEd Tanous             }
1661f5ffd806SEd Tanous             messages::success(asyncResp->res);
1662f5ffd806SEd Tanous             },
16631ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1664f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
16651ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1666168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1667f5ffd806SEd Tanous     }
1668f5ffd806SEd Tanous     if (lockoutThreshold)
1669f5ffd806SEd Tanous     {
1670f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
16715e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1672f5ffd806SEd Tanous             if (ec)
1673f5ffd806SEd Tanous             {
1674f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1675f5ffd806SEd Tanous                 return;
1676f5ffd806SEd Tanous             }
1677f5ffd806SEd Tanous             messages::success(asyncResp->res);
1678f5ffd806SEd Tanous             },
16791ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1680f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1681f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1682f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1683168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1684f5ffd806SEd Tanous     }
16851ef4c342SEd Tanous }
1686f5ffd806SEd Tanous 
16874c7d4d33SEd Tanous inline void handleAccountCollectionHead(
16881ef4c342SEd Tanous     App& app, const crow::Request& req,
16891ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16901ef4c342SEd Tanous {
16913ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
169245ca1b86SEd Tanous     {
169345ca1b86SEd Tanous         return;
169445ca1b86SEd Tanous     }
16954c7d4d33SEd Tanous     asyncResp->res.addHeader(
16964c7d4d33SEd Tanous         boost::beast::http::field::link,
16974c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
16984c7d4d33SEd Tanous }
16994c7d4d33SEd Tanous 
17004c7d4d33SEd Tanous inline void handleAccountCollectionGet(
17014c7d4d33SEd Tanous     App& app, const crow::Request& req,
17024c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
17034c7d4d33SEd Tanous {
1704afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1705afd369c6SJiaqing Zhao     {
1706afd369c6SJiaqing Zhao         return;
1707afd369c6SJiaqing Zhao     }
17083e72c202SNinad Palsule 
17093e72c202SNinad Palsule     if (req.session == nullptr)
17103e72c202SNinad Palsule     {
17113e72c202SNinad Palsule         messages::internalError(asyncResp->res);
17123e72c202SNinad Palsule         return;
17133e72c202SNinad Palsule     }
17143e72c202SNinad Palsule 
1715afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1716afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1717afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
17181476687dSEd Tanous 
17191476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
17201476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
17211ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
17221476687dSEd Tanous                                               "ManagerAccountCollection";
17231476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
17241476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
17250f74e643SEd Tanous 
17266c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
17273e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
17286c51eab1SEd Tanous 
1729f5e29f33SJunLin Chen     std::string thisUser;
1730f5e29f33SJunLin Chen     if (req.session)
1731f5e29f33SJunLin Chen     {
1732f5e29f33SJunLin Chen         thisUser = req.session->username;
1733f5e29f33SJunLin Chen     }
17345eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
17355eb468daSGeorge Liu     dbus::utility::getManagedObjects(
17365eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
1737cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
17385e7e2dc5SEd Tanous             const boost::system::error_code& ec,
1739711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1740b9b2e0b2SEd Tanous         if (ec)
1741b9b2e0b2SEd Tanous         {
1742f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1743b9b2e0b2SEd Tanous             return;
1744b9b2e0b2SEd Tanous         }
1745b9b2e0b2SEd Tanous 
1746cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1747002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1748cef1ddfbSEd Tanous 
1749cef1ddfbSEd Tanous         bool userCanSeeSelf =
1750002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1751cef1ddfbSEd Tanous 
1752002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1753b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1754b9b2e0b2SEd Tanous 
17559eb808c1SEd Tanous         for (const auto& userpath : users)
1756b9b2e0b2SEd Tanous         {
17572dfd18efSEd Tanous             std::string user = userpath.first.filename();
17582dfd18efSEd Tanous             if (user.empty())
1759b9b2e0b2SEd Tanous             {
17602dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
17612dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
17622dfd18efSEd Tanous 
17632dfd18efSEd Tanous                 return;
1764b9b2e0b2SEd Tanous             }
1765f365910cSGunnar Mills 
1766f365910cSGunnar Mills             // As clarified by Redfish here:
1767f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
17686c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
17696c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
17706c51eab1SEd Tanous             // accounts.
17711ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1772f365910cSGunnar Mills             {
17731476687dSEd Tanous                 nlohmann::json::object_t member;
177489492a15SPatrick Williams                 member["@odata.id"] = "/redfish/v1/AccountService/Accounts/" +
177589492a15SPatrick Williams                                       user;
1776b2ba3072SPatrick Williams                 memberArray.emplace_back(std::move(member));
1777b9b2e0b2SEd Tanous             }
1778f365910cSGunnar Mills         }
17791ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
17805eb468daSGeorge Liu         });
17811ef4c342SEd Tanous }
17826c51eab1SEd Tanous 
178397e90da3SNinad Palsule inline void processAfterCreateUser(
178497e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
178597e90da3SNinad Palsule     const std::string& username, const std::string& password,
178697e90da3SNinad Palsule     const boost::system::error_code& ec, sdbusplus::message_t& m)
178797e90da3SNinad Palsule {
178897e90da3SNinad Palsule     if (ec)
178997e90da3SNinad Palsule     {
179097e90da3SNinad Palsule         userErrorMessageHandler(m.get_error(), asyncResp, username, "");
179197e90da3SNinad Palsule         return;
179297e90da3SNinad Palsule     }
179397e90da3SNinad Palsule 
179497e90da3SNinad Palsule     if (pamUpdatePassword(username, password) != PAM_SUCCESS)
179597e90da3SNinad Palsule     {
179697e90da3SNinad Palsule         // At this point we have a user that's been
179797e90da3SNinad Palsule         // created, but the password set
179897e90da3SNinad Palsule         // failed.Something is wrong, so delete the user
179997e90da3SNinad Palsule         // that we've already created
180097e90da3SNinad Palsule         sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
180197e90da3SNinad Palsule         tempObjPath /= username;
180297e90da3SNinad Palsule         const std::string userPath(tempObjPath);
180397e90da3SNinad Palsule 
180497e90da3SNinad Palsule         crow::connections::systemBus->async_method_call(
180597e90da3SNinad Palsule             [asyncResp, password](const boost::system::error_code& ec3) {
180697e90da3SNinad Palsule             if (ec3)
180797e90da3SNinad Palsule             {
180897e90da3SNinad Palsule                 messages::internalError(asyncResp->res);
180997e90da3SNinad Palsule                 return;
181097e90da3SNinad Palsule             }
181197e90da3SNinad Palsule 
181297e90da3SNinad Palsule             // If password is invalid
181397e90da3SNinad Palsule             messages::propertyValueFormatError(asyncResp->res, password,
181497e90da3SNinad Palsule                                                "Password");
181597e90da3SNinad Palsule             },
181697e90da3SNinad Palsule             "xyz.openbmc_project.User.Manager", userPath,
181797e90da3SNinad Palsule             "xyz.openbmc_project.Object.Delete", "Delete");
181897e90da3SNinad Palsule 
181997e90da3SNinad Palsule         BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
182097e90da3SNinad Palsule         return;
182197e90da3SNinad Palsule     }
182297e90da3SNinad Palsule 
182397e90da3SNinad Palsule     messages::created(asyncResp->res);
182497e90da3SNinad Palsule     asyncResp->res.addHeader("Location",
182597e90da3SNinad Palsule                              "/redfish/v1/AccountService/Accounts/" + username);
182697e90da3SNinad Palsule }
182797e90da3SNinad Palsule 
182897e90da3SNinad Palsule inline void processAfterGetAllGroups(
182997e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
183097e90da3SNinad Palsule     const std::string& username, const std::string& password,
183197e90da3SNinad Palsule     const std::optional<std::string>& roleId, std::optional<bool> enabled,
18329ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes,
183397e90da3SNinad Palsule     const std::vector<std::string>& allGroupsList)
183497e90da3SNinad Palsule {
18353e72c202SNinad Palsule     std::vector<std::string> userGroups;
18369ba73934SNinad Palsule     std::vector<std::string> accountTypeUserGroups;
18379ba73934SNinad Palsule 
18389ba73934SNinad Palsule     // If user specified account types then convert them to unix user groups
18399ba73934SNinad Palsule     if (accountTypes)
18409ba73934SNinad Palsule     {
18419ba73934SNinad Palsule         if (!getUserGroupFromAccountType(asyncResp->res, *accountTypes,
18429ba73934SNinad Palsule                                          accountTypeUserGroups))
18439ba73934SNinad Palsule         {
18449ba73934SNinad Palsule             // Problem in mapping Account Types to User Groups, Error already
18459ba73934SNinad Palsule             // logged.
18469ba73934SNinad Palsule             return;
18479ba73934SNinad Palsule         }
18489ba73934SNinad Palsule     }
18499ba73934SNinad Palsule 
18503e72c202SNinad Palsule     for (const auto& grp : allGroupsList)
18513e72c202SNinad Palsule     {
18529ba73934SNinad Palsule         // If user specified the account type then only accept groups which are
18539ba73934SNinad Palsule         // in the account types group list.
18549ba73934SNinad Palsule         if (!accountTypeUserGroups.empty())
18559ba73934SNinad Palsule         {
18569ba73934SNinad Palsule             bool found = false;
18579ba73934SNinad Palsule             for (const auto& grp1 : accountTypeUserGroups)
18589ba73934SNinad Palsule             {
18599ba73934SNinad Palsule                 if (grp == grp1)
18609ba73934SNinad Palsule                 {
18619ba73934SNinad Palsule                     found = true;
18629ba73934SNinad Palsule                     break;
18639ba73934SNinad Palsule                 }
18649ba73934SNinad Palsule             }
18659ba73934SNinad Palsule             if (!found)
18669ba73934SNinad Palsule             {
18679ba73934SNinad Palsule                 continue;
18689ba73934SNinad Palsule             }
18699ba73934SNinad Palsule         }
18709ba73934SNinad Palsule 
18713e72c202SNinad Palsule         // Console access is provided to the user who is a member of
18723e72c202SNinad Palsule         // hostconsole group and has a administrator role. So, set
18733e72c202SNinad Palsule         // hostconsole group only for the administrator.
18749ba73934SNinad Palsule         if ((grp == "hostconsole") && (roleId != "priv-admin"))
18753e72c202SNinad Palsule         {
18769ba73934SNinad Palsule             if (!accountTypeUserGroups.empty())
18779ba73934SNinad Palsule             {
18789ba73934SNinad Palsule                 BMCWEB_LOG_ERROR
18799ba73934SNinad Palsule                     << "Only administrator can get HostConsole access";
18809ba73934SNinad Palsule                 asyncResp->res.result(boost::beast::http::status::bad_request);
18819ba73934SNinad Palsule                 return;
18829ba73934SNinad Palsule             }
18839ba73934SNinad Palsule             continue;
18849ba73934SNinad Palsule         }
18853e72c202SNinad Palsule         userGroups.emplace_back(grp);
18863e72c202SNinad Palsule     }
18879ba73934SNinad Palsule 
18889ba73934SNinad Palsule     // Make sure user specified groups are valid. This is internal error because
18899ba73934SNinad Palsule     // it some inconsistencies between user manager and bmcweb.
18909ba73934SNinad Palsule     if (!accountTypeUserGroups.empty() &&
18919ba73934SNinad Palsule         accountTypeUserGroups.size() != userGroups.size())
18929ba73934SNinad Palsule     {
18939ba73934SNinad Palsule         messages::internalError(asyncResp->res);
18949ba73934SNinad Palsule         return;
18953e72c202SNinad Palsule     }
18963e72c202SNinad Palsule 
189797e90da3SNinad Palsule     crow::connections::systemBus->async_method_call(
189897e90da3SNinad Palsule         [asyncResp, username, password](const boost::system::error_code& ec2,
189997e90da3SNinad Palsule                                         sdbusplus::message_t& m) {
190097e90da3SNinad Palsule         processAfterCreateUser(asyncResp, username, password, ec2, m);
190197e90da3SNinad Palsule         },
190297e90da3SNinad Palsule         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
19033e72c202SNinad Palsule         "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
19043e72c202SNinad Palsule         *roleId, *enabled);
190597e90da3SNinad Palsule }
190697e90da3SNinad Palsule 
19071ef4c342SEd Tanous inline void handleAccountCollectionPost(
19081ef4c342SEd Tanous     App& app, const crow::Request& req,
19091ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
19101ef4c342SEd Tanous {
19113ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
191245ca1b86SEd Tanous     {
191345ca1b86SEd Tanous         return;
191445ca1b86SEd Tanous     }
19159712f8acSEd Tanous     std::string username;
19169712f8acSEd Tanous     std::string password;
1917a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1918a24526dcSEd Tanous     std::optional<bool> enabled = true;
19199ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes;
19209ba73934SNinad Palsule     if (!json_util::readJsonPatch(
19219ba73934SNinad Palsule             req, asyncResp->res, "UserName", username, "Password", password,
19229ba73934SNinad Palsule             "RoleId", roleId, "Enabled", enabled, "AccountTypes", accountTypes))
192304ae99ecSEd Tanous     {
192404ae99ecSEd Tanous         return;
192504ae99ecSEd Tanous     }
192604ae99ecSEd Tanous 
192754fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
192884e12cb7SAppaRao Puli     if (priv.empty())
192904ae99ecSEd Tanous     {
19301ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
193104ae99ecSEd Tanous         return;
193204ae99ecSEd Tanous     }
19339712f8acSEd Tanous     roleId = priv;
193404ae99ecSEd Tanous 
1935599c71d8SAyushi Smriti     // Reading AllGroups property
19361e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
19371ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
19381ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
19391ef4c342SEd Tanous         "AllGroups",
19409ba73934SNinad Palsule         [asyncResp, username, password{std::move(password)}, roleId, enabled,
19419ba73934SNinad Palsule          accountTypes](const boost::system::error_code& ec,
19421e1e598dSJonathan Doman                        const std::vector<std::string>& allGroupsList) {
1943599c71d8SAyushi Smriti         if (ec)
1944599c71d8SAyushi Smriti         {
1945599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1946599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1947599c71d8SAyushi Smriti             return;
1948599c71d8SAyushi Smriti         }
1949599c71d8SAyushi Smriti 
19501e1e598dSJonathan Doman         if (allGroupsList.empty())
1951599c71d8SAyushi Smriti         {
1952599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1953599c71d8SAyushi Smriti             return;
1954599c71d8SAyushi Smriti         }
1955599c71d8SAyushi Smriti 
195697e90da3SNinad Palsule         processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
19579ba73934SNinad Palsule                                  accountTypes, allGroupsList);
19581e1e598dSJonathan Doman         });
19591ef4c342SEd Tanous }
1960b9b2e0b2SEd Tanous 
19611ef4c342SEd Tanous inline void
19624c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
19636c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19644c7d4d33SEd Tanous                       const std::string& /*accountName*/)
19651ef4c342SEd Tanous {
19663ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
196745ca1b86SEd Tanous     {
196845ca1b86SEd Tanous         return;
196945ca1b86SEd Tanous     }
19704c7d4d33SEd Tanous     asyncResp->res.addHeader(
19714c7d4d33SEd Tanous         boost::beast::http::field::link,
19724c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
19734c7d4d33SEd Tanous }
1974afd369c6SJiaqing Zhao 
19754c7d4d33SEd Tanous inline void
19764c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
19774c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19784c7d4d33SEd Tanous                      const std::string& accountName)
19794c7d4d33SEd Tanous {
1980afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1981afd369c6SJiaqing Zhao     {
1982afd369c6SJiaqing Zhao         return;
1983afd369c6SJiaqing Zhao     }
1984afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1985afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1986afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1987afd369c6SJiaqing Zhao 
19881ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1989031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1990d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1991031514fbSJunLin Chen     return;
19921ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1993afd369c6SJiaqing Zhao 
1994031514fbSJunLin Chen     if (req.session == nullptr)
1995031514fbSJunLin Chen     {
1996031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1997031514fbSJunLin Chen         return;
1998031514fbSJunLin Chen     }
19996c51eab1SEd Tanous     if (req.session->username != accountName)
2000b9b2e0b2SEd Tanous     {
20016c51eab1SEd Tanous         // At this point we've determined that the user is trying to
20021ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
20031ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
20041ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
20056c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
20063e72c202SNinad Palsule             redfish::getUserPrivileges(*req.session);
20071ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
20081ef4c342SEd Tanous                                                          "ConfigureManager"};
20096c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
20106c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
2011900f9497SJoseph Reynolds         {
2012900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
2013900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
2014900f9497SJoseph Reynolds             return;
2015900f9497SJoseph Reynolds         }
2016900f9497SJoseph Reynolds     }
2017900f9497SJoseph Reynolds 
20185eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
20195eb468daSGeorge Liu     dbus::utility::getManagedObjects(
20205eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
20211ef4c342SEd Tanous         [asyncResp,
20225e7e2dc5SEd Tanous          accountName](const boost::system::error_code& ec,
2023711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
2024b9b2e0b2SEd Tanous         if (ec)
2025b9b2e0b2SEd Tanous         {
2026f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2027b9b2e0b2SEd Tanous             return;
2028b9b2e0b2SEd Tanous         }
2029711ac7a9SEd Tanous         const auto userIt = std::find_if(
2030b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
2031b477fd44SP Dheeraj Srujan Kumar             [accountName](
2032b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
2033002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
203455f79e6fSEd Tanous             return accountName == user.first.filename();
2035b477fd44SP Dheeraj Srujan Kumar             });
2036b9b2e0b2SEd Tanous 
203784e12cb7SAppaRao Puli         if (userIt == users.end())
2038b9b2e0b2SEd Tanous         {
2039002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
2040002d39b4SEd Tanous                                        accountName);
204184e12cb7SAppaRao Puli             return;
204284e12cb7SAppaRao Puli         }
20434e68c45bSAyushi Smriti 
20441476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
204558345856SAbhishek Patel             "#ManagerAccount.v1_7_0.ManagerAccount";
20461476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
20471476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
20481476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
204958345856SAbhishek Patel         asyncResp->res.jsonValue["StrictAccountTypes"] = true;
20504e68c45bSAyushi Smriti 
205184e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
205265b0dc32SEd Tanous         {
2053002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
205465b0dc32SEd Tanous             {
205565b0dc32SEd Tanous                 for (const auto& property : interface.second)
205665b0dc32SEd Tanous                 {
205765b0dc32SEd Tanous                     if (property.first == "UserEnabled")
205865b0dc32SEd Tanous                     {
205965b0dc32SEd Tanous                         const bool* userEnabled =
2060abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
206165b0dc32SEd Tanous                         if (userEnabled == nullptr)
206265b0dc32SEd Tanous                         {
2063002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
206484e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
206584e12cb7SAppaRao Puli                             return;
206665b0dc32SEd Tanous                         }
2067002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
206865b0dc32SEd Tanous                     }
2069002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
207065b0dc32SEd Tanous                     {
207165b0dc32SEd Tanous                         const bool* userLocked =
2072abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
207365b0dc32SEd Tanous                         if (userLocked == nullptr)
207465b0dc32SEd Tanous                         {
207584e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
207684e12cb7SAppaRao Puli                                                 "ailedAttempt "
207784e12cb7SAppaRao Puli                                                 "wasn't a bool";
207884e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
207984e12cb7SAppaRao Puli                             return;
208065b0dc32SEd Tanous                         }
2081002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
2082002d39b4SEd Tanous                         asyncResp->res
2083002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
20843bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
208565b0dc32SEd Tanous                     }
208684e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
208784e12cb7SAppaRao Puli                     {
208854fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
2089002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
209054fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
209184e12cb7SAppaRao Puli                         {
2092002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
209384e12cb7SAppaRao Puli                                                 "string";
209484e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
209584e12cb7SAppaRao Puli                             return;
209684e12cb7SAppaRao Puli                         }
20971ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
209854fc587aSNagaraju Goruganti                         if (role.empty())
209984e12cb7SAppaRao Puli                         {
210084e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
210184e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
210284e12cb7SAppaRao Puli                             return;
210384e12cb7SAppaRao Puli                         }
210454fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
210584e12cb7SAppaRao Puli 
21061476687dSEd Tanous                         nlohmann::json& roleEntry =
2107002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
21081476687dSEd Tanous                         roleEntry["@odata.id"] =
2109002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
211084e12cb7SAppaRao Puli                     }
2111002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
21123bf4e632SJoseph Reynolds                     {
21133bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
21143bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
21153bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
21163bf4e632SJoseph Reynolds                         {
21170fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
21180fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
21193bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
21203bf4e632SJoseph Reynolds                             return;
21213bf4e632SJoseph Reynolds                         }
2122002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
21233bf4e632SJoseph Reynolds                             *userPasswordExpired;
21243bf4e632SJoseph Reynolds                     }
2125c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
2126c7229815SAbhishek Patel                     {
2127c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
2128c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
2129c7229815SAbhishek Patel                                 &property.second);
2130c7229815SAbhishek Patel                         if (userGroups == nullptr)
2131c7229815SAbhishek Patel                         {
2132c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR
2133c7229815SAbhishek Patel                                 << "userGroups wasn't a string vector";
2134c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2135c7229815SAbhishek Patel                             return;
2136c7229815SAbhishek Patel                         }
2137c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
2138c7229815SAbhishek Patel                         {
2139c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR << "userGroups mapping failed";
2140c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2141c7229815SAbhishek Patel                             return;
2142c7229815SAbhishek Patel                         }
2143c7229815SAbhishek Patel                     }
214465b0dc32SEd Tanous                 }
214565b0dc32SEd Tanous             }
214665b0dc32SEd Tanous         }
214765b0dc32SEd Tanous 
2148b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
214984e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
2150b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
2151b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
21525eb468daSGeorge Liu         });
21531ef4c342SEd Tanous }
2154a840879dSEd Tanous 
21551ef4c342SEd Tanous inline void
215620fc307fSGunnar Mills     handleAccountDelete(App& app, const crow::Request& req,
21576c51eab1SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21581ef4c342SEd Tanous                         const std::string& username)
21591ef4c342SEd Tanous {
21603ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
216145ca1b86SEd Tanous     {
216245ca1b86SEd Tanous         return;
216345ca1b86SEd Tanous     }
21641ef4c342SEd Tanous 
21651ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
2166031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
2167d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
2168031514fbSJunLin Chen     return;
2169031514fbSJunLin Chen 
21701ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
21711ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
21721ef4c342SEd Tanous     tempObjPath /= username;
21731ef4c342SEd Tanous     const std::string userPath(tempObjPath);
21741ef4c342SEd Tanous 
21751ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
21765e7e2dc5SEd Tanous         [asyncResp, username](const boost::system::error_code& ec) {
21771ef4c342SEd Tanous         if (ec)
21781ef4c342SEd Tanous         {
2179d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
21801ef4c342SEd Tanous                                        username);
21811ef4c342SEd Tanous             return;
21821ef4c342SEd Tanous         }
21831ef4c342SEd Tanous 
21841ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
21851ef4c342SEd Tanous         },
21861ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
21871ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
21881ef4c342SEd Tanous }
21891ef4c342SEd Tanous 
21901ef4c342SEd Tanous inline void
21911ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
21921ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21931ef4c342SEd Tanous                        const std::string& username)
21941ef4c342SEd Tanous {
21951ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
21961ef4c342SEd Tanous     {
21971ef4c342SEd Tanous         return;
21981ef4c342SEd Tanous     }
21991ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
22001ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
2201d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
22021ef4c342SEd Tanous     return;
22031ef4c342SEd Tanous 
22041ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
2205a24526dcSEd Tanous     std::optional<std::string> newUserName;
2206a24526dcSEd Tanous     std::optional<std::string> password;
2207a24526dcSEd Tanous     std::optional<bool> enabled;
2208a24526dcSEd Tanous     std::optional<std::string> roleId;
220924c8542dSRatan Gupta     std::optional<bool> locked;
221058345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes;
221158345856SAbhishek Patel 
221258345856SAbhishek Patel     bool userSelf = (username == req.session->username);
2213e9cc5172SEd Tanous 
2214031514fbSJunLin Chen     if (req.session == nullptr)
2215031514fbSJunLin Chen     {
2216031514fbSJunLin Chen         messages::internalError(asyncResp->res);
2217031514fbSJunLin Chen         return;
2218031514fbSJunLin Chen     }
2219031514fbSJunLin Chen 
2220e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
22213e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
2222e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
2223e9cc5172SEd Tanous     bool userHasConfigureUsers =
2224e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
2225e9cc5172SEd Tanous     if (userHasConfigureUsers)
2226e9cc5172SEd Tanous     {
2227e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
222858345856SAbhishek Patel         if (!json_util::readJsonPatch(
222958345856SAbhishek Patel                 req, asyncResp->res, "UserName", newUserName, "Password",
223058345856SAbhishek Patel                 password, "RoleId", roleId, "Enabled", enabled, "Locked",
223158345856SAbhishek Patel                 locked, "AccountTypes", accountTypes))
2232a840879dSEd Tanous         {
2233a840879dSEd Tanous             return;
2234a840879dSEd Tanous         }
2235e9cc5172SEd Tanous     }
2236e9cc5172SEd Tanous     else
2237900f9497SJoseph Reynolds     {
2238e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
223958345856SAbhishek Patel         if (!userSelf)
2240900f9497SJoseph Reynolds         {
2241900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
2242900f9497SJoseph Reynolds             return;
2243900f9497SJoseph Reynolds         }
2244031514fbSJunLin Chen 
2245e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
22461ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
22471ef4c342SEd Tanous                                       password))
2248e9cc5172SEd Tanous         {
2249e9cc5172SEd Tanous             return;
2250e9cc5172SEd Tanous         }
2251900f9497SJoseph Reynolds     }
2252900f9497SJoseph Reynolds 
225366b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
22546c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
22556c51eab1SEd Tanous     // updating user properties other then username. If username
22566c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
22576c51eab1SEd Tanous     // user rename request.
225866b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
2259a840879dSEd Tanous     {
22601ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
226158345856SAbhishek Patel                              locked, accountTypes, userSelf);
226284e12cb7SAppaRao Puli         return;
226384e12cb7SAppaRao Puli     }
226484e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
22656c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
22661ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
226758345856SAbhishek Patel          locked, userSelf, accountTypes(std::move(accountTypes))](
226858345856SAbhishek Patel             const boost::system::error_code ec, sdbusplus::message_t& m) {
226984e12cb7SAppaRao Puli         if (ec)
227084e12cb7SAppaRao Puli         {
2271002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2272002d39b4SEd Tanous                                     username);
2273a840879dSEd Tanous             return;
2274a840879dSEd Tanous         }
2275a840879dSEd Tanous 
2276002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
227758345856SAbhishek Patel                              locked, accountTypes, userSelf);
227884e12cb7SAppaRao Puli         },
22791ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
228084e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
228184e12cb7SAppaRao Puli         *newUserName);
22821ef4c342SEd Tanous }
22831ef4c342SEd Tanous 
22841ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
22851ef4c342SEd Tanous {
22861ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22874c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
22884c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
22894c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
22904c7d4d33SEd Tanous 
22914c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22921ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
22931ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
22941ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
22951ef4c342SEd Tanous 
22961ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22971ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
22981ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
22991ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
23001ef4c342SEd Tanous 
23011ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
23024c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
23034c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
23044c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
23054c7d4d33SEd Tanous 
23064c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
23071ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
23081ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
23091ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
23101ef4c342SEd Tanous 
23111ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
23121ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
23131ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
23141ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
23151ef4c342SEd Tanous 
23161ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23174c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
23184c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
23194c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
23204c7d4d33SEd Tanous 
23214c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23221ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
23231ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
23241ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
23251ef4c342SEd Tanous 
23261ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23271ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
23281ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
23291ef4c342SEd Tanous         // yet
23301ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
23311ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
23321ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
233384e12cb7SAppaRao Puli 
23346c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2335ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
23366c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
233720fc307fSGunnar Mills             std::bind_front(handleAccountDelete, std::ref(app)));
233806e086d9SEd Tanous }
233988d16c9aSLewanczyk, Dawid 
234088d16c9aSLewanczyk, Dawid } // namespace redfish
2341