xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 2e8c4bda9c4b2809ca76bb227f818592515a3e4a)
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* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
576973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
586973a582SRatan Gupta 
5954fc587aSNagaraju Goruganti struct LDAPRoleMapData
6054fc587aSNagaraju Goruganti {
6154fc587aSNagaraju Goruganti     std::string groupName;
6254fc587aSNagaraju Goruganti     std::string privilege;
6354fc587aSNagaraju Goruganti };
6454fc587aSNagaraju Goruganti 
656973a582SRatan Gupta struct LDAPConfigData
666973a582SRatan Gupta {
676973a582SRatan Gupta     std::string uri{};
686973a582SRatan Gupta     std::string bindDN{};
696973a582SRatan Gupta     std::string baseDN{};
706973a582SRatan Gupta     std::string searchScope{};
716973a582SRatan Gupta     std::string serverType{};
726973a582SRatan Gupta     bool serviceEnabled = false;
736973a582SRatan Gupta     std::string userNameAttribute{};
746973a582SRatan Gupta     std::string groupAttribute{};
7554fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
766973a582SRatan Gupta };
776973a582SRatan Gupta 
7854fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7984e12cb7SAppaRao Puli {
8084e12cb7SAppaRao Puli     if (role == "priv-admin")
8184e12cb7SAppaRao Puli     {
8284e12cb7SAppaRao Puli         return "Administrator";
8384e12cb7SAppaRao Puli     }
843174e4dfSEd Tanous     if (role == "priv-user")
8584e12cb7SAppaRao Puli     {
86c80fee55SAppaRao Puli         return "ReadOnly";
8784e12cb7SAppaRao Puli     }
883174e4dfSEd Tanous     if (role == "priv-operator")
8984e12cb7SAppaRao Puli     {
9084e12cb7SAppaRao Puli         return "Operator";
9184e12cb7SAppaRao Puli     }
9284e12cb7SAppaRao Puli     return "";
9384e12cb7SAppaRao Puli }
9454fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9584e12cb7SAppaRao Puli {
9684e12cb7SAppaRao Puli     if (role == "Administrator")
9784e12cb7SAppaRao Puli     {
9884e12cb7SAppaRao Puli         return "priv-admin";
9984e12cb7SAppaRao Puli     }
1003174e4dfSEd Tanous     if (role == "ReadOnly")
10184e12cb7SAppaRao Puli     {
10284e12cb7SAppaRao Puli         return "priv-user";
10384e12cb7SAppaRao Puli     }
1043174e4dfSEd Tanous     if (role == "Operator")
10584e12cb7SAppaRao Puli     {
10684e12cb7SAppaRao Puli         return "priv-operator";
10784e12cb7SAppaRao Puli     }
10884e12cb7SAppaRao Puli     return "";
10984e12cb7SAppaRao Puli }
110b9b2e0b2SEd Tanous 
111c7229815SAbhishek Patel /**
112c7229815SAbhishek Patel  * @brief Maps user group names retrieved from D-Bus object to
113c7229815SAbhishek Patel  * Account Types.
114c7229815SAbhishek Patel  *
115c7229815SAbhishek Patel  * @param[in] userGroups List of User groups
116c7229815SAbhishek Patel  * @param[out] res AccountTypes populated
117c7229815SAbhishek Patel  *
118c7229815SAbhishek Patel  * @return true in case of success, false if UserGroups contains
119c7229815SAbhishek Patel  * invalid group name(s).
120c7229815SAbhishek Patel  */
121c7229815SAbhishek Patel inline bool translateUserGroup(const std::vector<std::string>& userGroups,
122c7229815SAbhishek Patel                                crow::Response& res)
123c7229815SAbhishek Patel {
124c7229815SAbhishek Patel     std::vector<std::string> accountTypes;
125c7229815SAbhishek Patel     for (const auto& userGroup : userGroups)
126c7229815SAbhishek Patel     {
127c7229815SAbhishek Patel         if (userGroup == "redfish")
128c7229815SAbhishek Patel         {
129c7229815SAbhishek Patel             accountTypes.emplace_back("Redfish");
130c7229815SAbhishek Patel             accountTypes.emplace_back("WebUI");
131c7229815SAbhishek Patel         }
132c7229815SAbhishek Patel         else if (userGroup == "ipmi")
133c7229815SAbhishek Patel         {
134c7229815SAbhishek Patel             accountTypes.emplace_back("IPMI");
135c7229815SAbhishek Patel         }
136c7229815SAbhishek Patel         else if (userGroup == "ssh")
137c7229815SAbhishek Patel         {
138c7229815SAbhishek Patel             accountTypes.emplace_back("ManagerConsole");
139c7229815SAbhishek Patel         }
1403e72c202SNinad Palsule         else if (userGroup == "hostconsole")
1413e72c202SNinad Palsule         {
1423e72c202SNinad Palsule             // The hostconsole group controls who can access the host console
1433e72c202SNinad Palsule             // port via ssh and websocket.
1443e72c202SNinad Palsule             accountTypes.emplace_back("HostConsole");
1453e72c202SNinad Palsule         }
146c7229815SAbhishek Patel         else if (userGroup == "web")
147c7229815SAbhishek Patel         {
148c7229815SAbhishek Patel             // 'web' is one of the valid groups in the UserGroups property of
149c7229815SAbhishek Patel             // the user account in the D-Bus object. This group is currently not
150c7229815SAbhishek Patel             // doing anything, and is considered to be equivalent to 'redfish'.
151c7229815SAbhishek Patel             // 'redfish' user group is mapped to 'Redfish'and 'WebUI'
152c7229815SAbhishek Patel             // AccountTypes, so do nothing here...
153c7229815SAbhishek Patel         }
154c7229815SAbhishek Patel         else
155c7229815SAbhishek Patel         {
156c7229815SAbhishek Patel             // Invalid user group name. Caller throws an excption.
157c7229815SAbhishek Patel             return false;
158c7229815SAbhishek Patel         }
159c7229815SAbhishek Patel     }
160c7229815SAbhishek Patel 
161c7229815SAbhishek Patel     res.jsonValue["AccountTypes"] = std::move(accountTypes);
162c7229815SAbhishek Patel     return true;
163c7229815SAbhishek Patel }
164c7229815SAbhishek Patel 
16558345856SAbhishek Patel /**
16658345856SAbhishek Patel  * @brief Builds User Groups from the Account Types
16758345856SAbhishek Patel  *
16858345856SAbhishek Patel  * @param[in] asyncResp Async Response
16958345856SAbhishek Patel  * @param[in] accountTypes List of Account Types
17058345856SAbhishek Patel  * @param[out] userGroups List of User Groups mapped from Account Types
17158345856SAbhishek Patel  *
17258345856SAbhishek Patel  * @return true if Account Types mapped to User Groups, false otherwise.
17358345856SAbhishek Patel  */
17458345856SAbhishek Patel inline bool
17558345856SAbhishek Patel     getUserGroupFromAccountType(crow::Response& res,
17658345856SAbhishek Patel                                 const std::vector<std::string>& accountTypes,
17758345856SAbhishek Patel                                 std::vector<std::string>& userGroups)
17858345856SAbhishek Patel {
17958345856SAbhishek Patel     // Need both Redfish and WebUI Account Types to map to 'redfish' User Group
18058345856SAbhishek Patel     bool redfishType = false;
18158345856SAbhishek Patel     bool webUIType = false;
18258345856SAbhishek Patel 
18358345856SAbhishek Patel     for (const auto& accountType : accountTypes)
18458345856SAbhishek Patel     {
18558345856SAbhishek Patel         if (accountType == "Redfish")
18658345856SAbhishek Patel         {
18758345856SAbhishek Patel             redfishType = true;
18858345856SAbhishek Patel         }
18958345856SAbhishek Patel         else if (accountType == "WebUI")
19058345856SAbhishek Patel         {
19158345856SAbhishek Patel             webUIType = true;
19258345856SAbhishek Patel         }
19358345856SAbhishek Patel         else if (accountType == "IPMI")
19458345856SAbhishek Patel         {
19558345856SAbhishek Patel             userGroups.emplace_back("ipmi");
19658345856SAbhishek Patel         }
19758345856SAbhishek Patel         else if (accountType == "HostConsole")
19858345856SAbhishek Patel         {
19958345856SAbhishek Patel             userGroups.emplace_back("hostconsole");
20058345856SAbhishek Patel         }
20158345856SAbhishek Patel         else if (accountType == "ManagerConsole")
20258345856SAbhishek Patel         {
20358345856SAbhishek Patel             userGroups.emplace_back("ssh");
20458345856SAbhishek Patel         }
20558345856SAbhishek Patel         else
20658345856SAbhishek Patel         {
20758345856SAbhishek Patel             // Invalid Account Type
20858345856SAbhishek Patel             messages::propertyValueNotInList(res, "AccountTypes", accountType);
20958345856SAbhishek Patel             return false;
21058345856SAbhishek Patel         }
21158345856SAbhishek Patel     }
21258345856SAbhishek Patel 
21358345856SAbhishek Patel     // Both  Redfish and WebUI Account Types are needed to PATCH
21458345856SAbhishek Patel     if (redfishType ^ webUIType)
21558345856SAbhishek Patel     {
21658345856SAbhishek Patel         BMCWEB_LOG_ERROR
21758345856SAbhishek Patel             << "Missing Redfish or WebUI Account Type to set redfish User Group";
21858345856SAbhishek Patel         messages::strictAccountTypes(res, "AccountTypes");
21958345856SAbhishek Patel         return false;
22058345856SAbhishek Patel     }
22158345856SAbhishek Patel 
22258345856SAbhishek Patel     if (redfishType && webUIType)
22358345856SAbhishek Patel     {
22458345856SAbhishek Patel         userGroups.emplace_back("redfish");
22558345856SAbhishek Patel     }
22658345856SAbhishek Patel 
22758345856SAbhishek Patel     return true;
22858345856SAbhishek Patel }
22958345856SAbhishek Patel 
23058345856SAbhishek Patel /**
23158345856SAbhishek Patel  * @brief Sets UserGroups property of the user based on the Account Types
23258345856SAbhishek Patel  *
23358345856SAbhishek Patel  * @param[in] accountTypes List of User Account Types
23458345856SAbhishek Patel  * @param[in] asyncResp Async Response
23558345856SAbhishek Patel  * @param[in] dbusObjectPath D-Bus Object Path
23658345856SAbhishek Patel  * @param[in] userSelf true if User is updating OWN Account Types
23758345856SAbhishek Patel  */
23858345856SAbhishek Patel inline void
23958345856SAbhishek Patel     patchAccountTypes(const std::vector<std::string>& accountTypes,
24058345856SAbhishek Patel                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24158345856SAbhishek Patel                       const std::string& dbusObjectPath, bool userSelf)
24258345856SAbhishek Patel {
24358345856SAbhishek Patel     // Check if User is disabling own Redfish Account Type
24458345856SAbhishek Patel     if (userSelf &&
24558345856SAbhishek Patel         (accountTypes.cend() ==
24658345856SAbhishek Patel          std::find(accountTypes.cbegin(), accountTypes.cend(), "Redfish")))
24758345856SAbhishek Patel     {
24858345856SAbhishek Patel         BMCWEB_LOG_ERROR
24958345856SAbhishek Patel             << "User disabling OWN Redfish Account Type is not allowed";
25058345856SAbhishek Patel         messages::strictAccountTypes(asyncResp->res, "AccountTypes");
25158345856SAbhishek Patel         return;
25258345856SAbhishek Patel     }
25358345856SAbhishek Patel 
25458345856SAbhishek Patel     std::vector<std::string> updatedUserGroups;
25558345856SAbhishek Patel     if (!getUserGroupFromAccountType(asyncResp->res, accountTypes,
25658345856SAbhishek Patel                                      updatedUserGroups))
25758345856SAbhishek Patel     {
25858345856SAbhishek Patel         // Problem in mapping Account Types to User Groups, Error already
25958345856SAbhishek Patel         // logged.
26058345856SAbhishek Patel         return;
26158345856SAbhishek Patel     }
26258345856SAbhishek Patel 
26358345856SAbhishek Patel     crow::connections::systemBus->async_method_call(
26458345856SAbhishek Patel         [asyncResp](const boost::system::error_code ec) {
26558345856SAbhishek Patel         if (ec)
26658345856SAbhishek Patel         {
26758345856SAbhishek Patel             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
26858345856SAbhishek Patel             messages::internalError(asyncResp->res);
26958345856SAbhishek Patel             return;
27058345856SAbhishek Patel         }
27158345856SAbhishek Patel         messages::success(asyncResp->res);
27258345856SAbhishek Patel         },
27358345856SAbhishek Patel         "xyz.openbmc_project.User.Manager", dbusObjectPath,
27458345856SAbhishek Patel         "org.freedesktop.DBus.Properties", "Set",
27558345856SAbhishek Patel         "xyz.openbmc_project.User.Attributes", "UserGroups",
27658345856SAbhishek Patel         dbus::utility::DbusVariantType{updatedUserGroups});
27758345856SAbhishek Patel }
27858345856SAbhishek Patel 
2798d1b46d7Szhanghch05 inline void userErrorMessageHandler(
2808d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2818d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
28266b5ca76Sjayaprakash Mutyala {
28366b5ca76Sjayaprakash Mutyala     if (e == nullptr)
28466b5ca76Sjayaprakash Mutyala     {
28566b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
28666b5ca76Sjayaprakash Mutyala         return;
28766b5ca76Sjayaprakash Mutyala     }
28866b5ca76Sjayaprakash Mutyala 
289055806b3SManojkiran Eda     const char* errorMessage = e->name;
29066b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
29166b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
29266b5ca76Sjayaprakash Mutyala     {
293d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
29466b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
29566b5ca76Sjayaprakash Mutyala     }
29666b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
29766b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
29866b5ca76Sjayaprakash Mutyala     {
299d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
30066b5ca76Sjayaprakash Mutyala     }
301d4d25793SEd Tanous     else if ((strcmp(errorMessage,
302d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
303d4d25793SEd Tanous               0) ||
3040fda0f12SGeorge Liu              (strcmp(
3050fda0f12SGeorge Liu                   errorMessage,
3060fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
3070fda0f12SGeorge Liu               0))
30866b5ca76Sjayaprakash Mutyala     {
30966b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
31066b5ca76Sjayaprakash Mutyala     }
31166b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
31266b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
31366b5ca76Sjayaprakash Mutyala     {
31466b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
31566b5ca76Sjayaprakash Mutyala     }
31666b5ca76Sjayaprakash Mutyala     else
31766b5ca76Sjayaprakash Mutyala     {
31866b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
31966b5ca76Sjayaprakash Mutyala     }
32066b5ca76Sjayaprakash Mutyala }
32166b5ca76Sjayaprakash Mutyala 
32281ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
323ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
324ab828d7cSRatan Gupta                                 const std::string& ldapType)
3256973a582SRatan Gupta {
32689492a15SPatrick Williams     std::string service = (ldapType == "LDAP") ? "LDAPService"
32789492a15SPatrick Williams                                                : "ActiveDirectoryService";
32854fc587aSNagaraju Goruganti 
3291476687dSEd Tanous     nlohmann::json& ldap = jsonResponse[ldapType];
33054fc587aSNagaraju Goruganti 
3311476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
3321476687dSEd Tanous     ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri});
3330ec8b83dSEd Tanous     ldap["Authentication"]["AuthenticationType"] =
3340ec8b83dSEd Tanous         account_service::AuthenticationTypes::UsernameAndPassword;
3351476687dSEd Tanous     ldap["Authentication"]["Username"] = confData.bindDN;
3361476687dSEd Tanous     ldap["Authentication"]["Password"] = nullptr;
3371476687dSEd Tanous 
3381476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] =
3391476687dSEd Tanous         nlohmann::json::array({confData.baseDN});
3401476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] =
3411476687dSEd Tanous         confData.userNameAttribute;
3421476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] =
3431476687dSEd Tanous         confData.groupAttribute;
3441476687dSEd Tanous 
3451476687dSEd Tanous     nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"];
34654fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
3479eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
34854fc587aSNagaraju Goruganti     {
34954fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
35054fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
351613dabeaSEd Tanous 
352613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
353613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
354329f0348SJorge Cisneros         remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
355329f0348SJorge Cisneros         roleMapArray.emplace_back(std::move(remoteGroup));
35654fc587aSNagaraju Goruganti     }
3576973a582SRatan Gupta }
3586973a582SRatan Gupta 
3596973a582SRatan Gupta /**
36006785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
36106785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
36206785244SRatan Gupta  *
36306785244SRatan Gupta  */
36423a21a1cSEd Tanous inline void handleRoleMapPatch(
3658d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36606785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
367f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
36806785244SRatan Gupta {
36906785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
37006785244SRatan Gupta     {
371f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
37206785244SRatan Gupta 
37306785244SRatan Gupta         if (thisJson.is_null())
37406785244SRatan Gupta         {
37506785244SRatan Gupta             // delete the existing object
37606785244SRatan Gupta             if (index < roleMapObjData.size())
37706785244SRatan Gupta             {
37806785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
37906785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
3805e7e2dc5SEd Tanous                      index](const boost::system::error_code& ec) {
38106785244SRatan Gupta                     if (ec)
38206785244SRatan Gupta                     {
38306785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
38406785244SRatan Gupta                         messages::internalError(asyncResp->res);
38506785244SRatan Gupta                         return;
38606785244SRatan Gupta                     }
38789492a15SPatrick Williams                     asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"]
38889492a15SPatrick Williams                                             [index] = nullptr;
38906785244SRatan Gupta                     },
39006785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
39106785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
39206785244SRatan Gupta             }
39306785244SRatan Gupta             else
39406785244SRatan Gupta             {
39506785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
396*2e8c4bdaSEd Tanous                 messages::propertyValueTypeError(asyncResp->res, thisJson,
397*2e8c4bdaSEd Tanous                                                  "RemoteRoleMapping/" +
398*2e8c4bdaSEd Tanous                                                      std::to_string(index));
39906785244SRatan Gupta                 return;
40006785244SRatan Gupta             }
40106785244SRatan Gupta         }
40206785244SRatan Gupta         else if (thisJson.empty())
40306785244SRatan Gupta         {
40406785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
40506785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
40606785244SRatan Gupta         }
40706785244SRatan Gupta         else
40806785244SRatan Gupta         {
40906785244SRatan Gupta             // update/create the object
41006785244SRatan Gupta             std::optional<std::string> remoteGroup;
41106785244SRatan Gupta             std::optional<std::string> localRole;
41206785244SRatan Gupta 
413f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
414f23b7296SEd Tanous             // readJson is structured
415f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
416f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
417f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
418f23b7296SEd Tanous                                      localRole))
41906785244SRatan Gupta             {
42006785244SRatan Gupta                 continue;
42106785244SRatan Gupta             }
42206785244SRatan Gupta 
42306785244SRatan Gupta             // Update existing RoleMapping Object
42406785244SRatan Gupta             if (index < roleMapObjData.size())
42506785244SRatan Gupta             {
42606785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
42706785244SRatan Gupta                 // If "RemoteGroup" info is provided
42806785244SRatan Gupta                 if (remoteGroup)
42906785244SRatan Gupta                 {
43006785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
43106785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
43225e055a3SRavi Teja                          remoteGroup](const boost::system::error_code& ec,
43325e055a3SRavi Teja                                       const sdbusplus::message::message& msg) {
43406785244SRatan Gupta                         if (ec)
43506785244SRatan Gupta                         {
43625e055a3SRavi Teja                             const sd_bus_error* dbusError = msg.get_error();
43725e055a3SRavi Teja                             if ((dbusError != nullptr) &&
43825e055a3SRavi Teja                                 (dbusError->name ==
43925e055a3SRavi Teja                                  std::string_view(
44025e055a3SRavi Teja                                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
44125e055a3SRavi Teja                             {
44225e055a3SRavi Teja                                 BMCWEB_LOG_WARNING << "DBUS response error: "
44325e055a3SRavi Teja                                                    << ec;
44425e055a3SRavi Teja                                 messages::propertyValueIncorrect(asyncResp->res,
44525e055a3SRavi Teja                                                                  "RemoteGroup",
44625e055a3SRavi Teja                                                                  *remoteGroup);
44725e055a3SRavi Teja                                 return;
44825e055a3SRavi Teja                             }
44906785244SRatan Gupta                             messages::internalError(asyncResp->res);
45006785244SRatan Gupta                             return;
45106785244SRatan Gupta                         }
45206785244SRatan Gupta                         asyncResp->res
453002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
454002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
45506785244SRatan Gupta                         },
45606785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
45706785244SRatan Gupta                         propertyInterface, "Set",
45806785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
45906785244SRatan Gupta                         "GroupName",
460168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
461168e20c1SEd Tanous                             std::move(*remoteGroup)));
46206785244SRatan Gupta                 }
46306785244SRatan Gupta 
46406785244SRatan Gupta                 // If "LocalRole" info is provided
46506785244SRatan Gupta                 if (localRole)
46606785244SRatan Gupta                 {
46706785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
46806785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
46925e055a3SRavi Teja                          localRole](const boost::system::error_code& ec,
47025e055a3SRavi Teja                                     const sdbusplus::message::message& msg) {
47106785244SRatan Gupta                         if (ec)
47206785244SRatan Gupta                         {
47325e055a3SRavi Teja                             const sd_bus_error* dbusError = msg.get_error();
47425e055a3SRavi Teja                             if ((dbusError != nullptr) &&
47525e055a3SRavi Teja                                 (dbusError->name ==
47625e055a3SRavi Teja                                  std::string_view(
47725e055a3SRavi Teja                                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
47825e055a3SRavi Teja                             {
47925e055a3SRavi Teja                                 BMCWEB_LOG_WARNING << "DBUS response error: "
48025e055a3SRavi Teja                                                    << ec;
48125e055a3SRavi Teja                                 messages::propertyValueIncorrect(
48225e055a3SRavi Teja                                     asyncResp->res, "LocalRole", *localRole);
48325e055a3SRavi Teja                                 return;
48425e055a3SRavi Teja                             }
48506785244SRatan Gupta                             messages::internalError(asyncResp->res);
48606785244SRatan Gupta                             return;
48706785244SRatan Gupta                         }
48806785244SRatan Gupta                         asyncResp->res
489002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
490002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
49106785244SRatan Gupta                         },
49206785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
49306785244SRatan Gupta                         propertyInterface, "Set",
49406785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
49506785244SRatan Gupta                         "Privilege",
496168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
49706785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
49806785244SRatan Gupta                 }
49906785244SRatan Gupta             }
50006785244SRatan Gupta             // Create a new RoleMapping Object.
50106785244SRatan Gupta             else
50206785244SRatan Gupta             {
50306785244SRatan Gupta                 BMCWEB_LOG_DEBUG
50406785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
50589492a15SPatrick Williams                 std::string pathString = "RemoteRoleMapping/" +
50689492a15SPatrick Williams                                          std::to_string(index);
50706785244SRatan Gupta 
50806785244SRatan Gupta                 if (!localRole)
50906785244SRatan Gupta                 {
51006785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
51106785244SRatan Gupta                                               pathString + "/LocalRole");
51206785244SRatan Gupta                     continue;
51306785244SRatan Gupta                 }
51406785244SRatan Gupta                 if (!remoteGroup)
51506785244SRatan Gupta                 {
51606785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
51706785244SRatan Gupta                                               pathString + "/RemoteGroup");
51806785244SRatan Gupta                     continue;
51906785244SRatan Gupta                 }
52006785244SRatan Gupta 
52106785244SRatan Gupta                 std::string dbusObjectPath;
52206785244SRatan Gupta                 if (serverType == "ActiveDirectory")
52306785244SRatan Gupta                 {
5242c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
52506785244SRatan Gupta                 }
52606785244SRatan Gupta                 else if (serverType == "LDAP")
52706785244SRatan Gupta                 {
52823a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
52906785244SRatan Gupta                 }
53006785244SRatan Gupta 
53106785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
53206785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
53306785244SRatan Gupta 
53406785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
535271584abSEd Tanous                     [asyncResp, serverType, localRole,
5365e7e2dc5SEd Tanous                      remoteGroup](const boost::system::error_code& ec) {
53706785244SRatan Gupta                     if (ec)
53806785244SRatan Gupta                     {
53906785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
54006785244SRatan Gupta                         messages::internalError(asyncResp->res);
54106785244SRatan Gupta                         return;
54206785244SRatan Gupta                     }
54306785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
54406785244SRatan Gupta                         asyncResp->res
54506785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
5461476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
5471476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
5481476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
549b2ba3072SPatrick Williams                     remoteRoleJson.emplace_back(std::move(roleMapEntry));
55006785244SRatan Gupta                     },
55106785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
5523174e4dfSEd Tanous                     "Create", *remoteGroup,
55306785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
55406785244SRatan Gupta             }
55506785244SRatan Gupta         }
55606785244SRatan Gupta     }
55706785244SRatan Gupta }
55806785244SRatan Gupta 
55906785244SRatan Gupta /**
5606973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
5616973a582SRatan Gupta  * into JSON
5626973a582SRatan Gupta  */
5636973a582SRatan Gupta template <typename CallbackFunc>
5646973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
5656973a582SRatan Gupta                               CallbackFunc&& callback)
5666973a582SRatan Gupta {
5672b73119cSGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
5682b73119cSGeorge Liu         ldapEnableInterface, ldapConfigInterface};
56954fc587aSNagaraju Goruganti 
5702b73119cSGeorge Liu     dbus::utility::getDbusObject(
5712b73119cSGeorge Liu         ldapConfigObjectName, interfaces,
5722b73119cSGeorge Liu         [callback, ldapType](const boost::system::error_code& ec,
573b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
57454fc587aSNagaraju Goruganti         if (ec || resp.empty())
57554fc587aSNagaraju Goruganti         {
5760fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
577002d39b4SEd Tanous                 << "DBUS response error during getting of service name: " << ec;
57823a21a1cSEd Tanous             LDAPConfigData empty{};
57923a21a1cSEd Tanous             callback(false, empty, ldapType);
58054fc587aSNagaraju Goruganti             return;
58154fc587aSNagaraju Goruganti         }
58254fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
58354fc587aSNagaraju Goruganti         crow::connections::systemBus->async_method_call(
584002d39b4SEd Tanous             [callback,
5855e7e2dc5SEd Tanous              ldapType](const boost::system::error_code& errorCode,
586711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
5876973a582SRatan Gupta             LDAPConfigData confData{};
58881ce609eSEd Tanous             if (errorCode)
5896973a582SRatan Gupta             {
590ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
591002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode;
5926973a582SRatan Gupta                 return;
5936973a582SRatan Gupta             }
594ab828d7cSRatan Gupta 
595ab828d7cSRatan Gupta             std::string ldapDbusType;
59654fc587aSNagaraju Goruganti             std::string searchString;
59754fc587aSNagaraju Goruganti 
598ab828d7cSRatan Gupta             if (ldapType == "LDAP")
599ab828d7cSRatan Gupta             {
6000fda0f12SGeorge Liu                 ldapDbusType =
6010fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
60254fc587aSNagaraju Goruganti                 searchString = "openldap";
603ab828d7cSRatan Gupta             }
604ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
605ab828d7cSRatan Gupta             {
60654fc587aSNagaraju Goruganti                 ldapDbusType =
6070fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
60854fc587aSNagaraju Goruganti                 searchString = "active_directory";
609ab828d7cSRatan Gupta             }
610ab828d7cSRatan Gupta             else
611ab828d7cSRatan Gupta             {
612002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type="
613ab828d7cSRatan Gupta                                  << ldapType;
614ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
615ab828d7cSRatan Gupta                 return;
616ab828d7cSRatan Gupta             }
617ab828d7cSRatan Gupta 
618ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
619ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
620ab828d7cSRatan Gupta 
6216973a582SRatan Gupta             for (const auto& object : ldapObjects)
6226973a582SRatan Gupta             {
62354fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
62454fc587aSNagaraju Goruganti                 // given type
625002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
6266973a582SRatan Gupta                 {
627ab828d7cSRatan Gupta                     continue;
628ab828d7cSRatan Gupta                 }
629ab828d7cSRatan Gupta 
6306973a582SRatan Gupta                 for (const auto& interface : object.second)
6316973a582SRatan Gupta                 {
6326973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
6336973a582SRatan Gupta                     {
6346973a582SRatan Gupta                         // rest of the properties are string.
6356973a582SRatan Gupta                         for (const auto& property : interface.second)
6366973a582SRatan Gupta                         {
6376973a582SRatan Gupta                             if (property.first == "Enabled")
6386973a582SRatan Gupta                             {
6396973a582SRatan Gupta                                 const bool* value =
6406973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
6416973a582SRatan Gupta                                 if (value == nullptr)
6426973a582SRatan Gupta                                 {
6436973a582SRatan Gupta                                     continue;
6446973a582SRatan Gupta                                 }
6456973a582SRatan Gupta                                 confData.serviceEnabled = *value;
6466973a582SRatan Gupta                                 break;
6476973a582SRatan Gupta                             }
6486973a582SRatan Gupta                         }
6496973a582SRatan Gupta                     }
6506973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
6516973a582SRatan Gupta                     {
6526973a582SRatan Gupta                         for (const auto& property : interface.second)
6536973a582SRatan Gupta                         {
654271584abSEd Tanous                             const std::string* strValue =
655002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
656271584abSEd Tanous                             if (strValue == nullptr)
6576973a582SRatan Gupta                             {
6586973a582SRatan Gupta                                 continue;
6596973a582SRatan Gupta                             }
6606973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
6616973a582SRatan Gupta                             {
662271584abSEd Tanous                                 confData.uri = *strValue;
6636973a582SRatan Gupta                             }
6646973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
6656973a582SRatan Gupta                             {
666271584abSEd Tanous                                 confData.bindDN = *strValue;
6676973a582SRatan Gupta                             }
6686973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
6696973a582SRatan Gupta                             {
670271584abSEd Tanous                                 confData.baseDN = *strValue;
6716973a582SRatan Gupta                             }
672002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
6736973a582SRatan Gupta                             {
674271584abSEd Tanous                                 confData.searchScope = *strValue;
6756973a582SRatan Gupta                             }
676002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
6776973a582SRatan Gupta                             {
678271584abSEd Tanous                                 confData.groupAttribute = *strValue;
6796973a582SRatan Gupta                             }
680002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
6816973a582SRatan Gupta                             {
682271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
6836973a582SRatan Gupta                             }
68454fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
685ab828d7cSRatan Gupta                             {
686271584abSEd Tanous                                 confData.serverType = *strValue;
68754fc587aSNagaraju Goruganti                             }
68854fc587aSNagaraju Goruganti                         }
68954fc587aSNagaraju Goruganti                     }
690002d39b4SEd Tanous                     else if (interface.first ==
6910fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
69254fc587aSNagaraju Goruganti                     {
69354fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
69454fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
69554fc587aSNagaraju Goruganti                         {
696271584abSEd Tanous                             const std::string* strValue =
697002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
69854fc587aSNagaraju Goruganti 
699271584abSEd Tanous                             if (strValue == nullptr)
70054fc587aSNagaraju Goruganti                             {
70154fc587aSNagaraju Goruganti                                 continue;
70254fc587aSNagaraju Goruganti                             }
70354fc587aSNagaraju Goruganti 
70454fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
70554fc587aSNagaraju Goruganti                             {
706271584abSEd Tanous                                 roleMapData.groupName = *strValue;
70754fc587aSNagaraju Goruganti                             }
70854fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
70954fc587aSNagaraju Goruganti                             {
710271584abSEd Tanous                                 roleMapData.privilege = *strValue;
71154fc587aSNagaraju Goruganti                             }
71254fc587aSNagaraju Goruganti                         }
71354fc587aSNagaraju Goruganti 
714002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
715002d39b4SEd Tanous                                                             roleMapData);
71654fc587aSNagaraju Goruganti                     }
71754fc587aSNagaraju Goruganti                 }
71854fc587aSNagaraju Goruganti             }
719ab828d7cSRatan Gupta             callback(true, confData, ldapType);
72054fc587aSNagaraju Goruganti             },
721002d39b4SEd Tanous             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
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(
8068a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
80725e055a3SRavi Teja          serviceAddressList](const boost::system::error_code& ec,
80825e055a3SRavi Teja                              sdbusplus::message::message& msg) {
8098a07d286SRatan Gupta         if (ec)
8108a07d286SRatan Gupta         {
81125e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
81225e055a3SRavi Teja             if ((dbusError != nullptr) &&
81325e055a3SRavi Teja                 (dbusError->name ==
81425e055a3SRavi Teja                  std::string_view(
81525e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
81625e055a3SRavi Teja             {
81725e055a3SRavi Teja                 BMCWEB_LOG_WARNING
818c61704abSGunnar Mills                     << "Error Occurred in updating the service address";
81925e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
82025e055a3SRavi Teja                                                  "ServiceAddresses",
82125e055a3SRavi Teja                                                  serviceAddressList.front());
82225e055a3SRavi Teja                 return;
82325e055a3SRavi Teja             }
8248a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8258a07d286SRatan Gupta             return;
8268a07d286SRatan Gupta         }
8278a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
8288a07d286SRatan Gupta             serviceAddressList.front()};
829002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
8308a07d286SRatan Gupta             modifiedserviceAddressList;
8318a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
8328a07d286SRatan Gupta         {
833002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
8348a07d286SRatan Gupta                                             serviceAddressList.front());
8358a07d286SRatan Gupta         }
8368a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
8378a07d286SRatan Gupta         },
8388a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8398a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
840168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
8418a07d286SRatan Gupta }
8428a07d286SRatan Gupta /**
8438a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
8448a07d286SRatan Gupta           json response with the new value.
8458a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
8468a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8478a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8488a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8498a07d286SRatan Gupta  */
8508a07d286SRatan Gupta 
8514f48d5f6SEd Tanous inline void
8524f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
8538d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8548a07d286SRatan Gupta                         const std::string& ldapServerElementName,
8558a07d286SRatan Gupta                         const std::string& ldapConfigObject)
8568a07d286SRatan Gupta {
8578a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8588a07d286SRatan Gupta         [asyncResp, username,
8595e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
8608a07d286SRatan Gupta         if (ec)
8618a07d286SRatan Gupta         {
8626c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
8638a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8648a07d286SRatan Gupta             return;
8658a07d286SRatan Gupta         }
86689492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
86789492a15SPatrick Williams                                 ["Username"] = username;
8688a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
8698a07d286SRatan Gupta         },
8708a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
871168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
872168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
8738a07d286SRatan Gupta }
8748a07d286SRatan Gupta 
8758a07d286SRatan Gupta /**
8768a07d286SRatan Gupta  * @brief updates the LDAP password
8778a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
8788a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8798a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8808a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
8818a07d286SRatan Gupta  */
8828a07d286SRatan Gupta 
8834f48d5f6SEd Tanous inline void
8844f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
8858d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8868a07d286SRatan Gupta                         const std::string& ldapServerElementName,
8878a07d286SRatan Gupta                         const std::string& ldapConfigObject)
8888a07d286SRatan Gupta {
8898a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8908a07d286SRatan Gupta         [asyncResp, password,
8915e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
8928a07d286SRatan Gupta         if (ec)
8938a07d286SRatan Gupta         {
8946c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
8958a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8968a07d286SRatan Gupta             return;
8978a07d286SRatan Gupta         }
89889492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
89989492a15SPatrick Williams                                 ["Password"] = "";
9008a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
9018a07d286SRatan Gupta         },
9028a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
9038a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
904168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
9058a07d286SRatan Gupta }
9068a07d286SRatan Gupta 
9078a07d286SRatan Gupta /**
9088a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
9098a07d286SRatan Gupta           json response with the new value.
9108a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
9118a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9128a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9138a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9148a07d286SRatan Gupta  */
9158a07d286SRatan Gupta 
9164f48d5f6SEd Tanous inline void
9174f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
9188d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9198a07d286SRatan Gupta                       const std::string& ldapServerElementName,
9208a07d286SRatan Gupta                       const std::string& ldapConfigObject)
9218a07d286SRatan Gupta {
9228a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
9238a07d286SRatan Gupta         [asyncResp, baseDNList,
92425e055a3SRavi Teja          ldapServerElementName](const boost::system::error_code& ec,
92525e055a3SRavi Teja                                 const sdbusplus::message::message& msg) {
9268a07d286SRatan Gupta         if (ec)
9278a07d286SRatan Gupta         {
9286c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
92925e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
93025e055a3SRavi Teja             if ((dbusError != nullptr) &&
93125e055a3SRavi Teja                 (dbusError->name ==
93225e055a3SRavi Teja                  std::string_view(
93325e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
93425e055a3SRavi Teja             {
93525e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
93625e055a3SRavi Teja                                                  "BaseDistinguishedNames",
93725e055a3SRavi Teja                                                  baseDNList.front());
93825e055a3SRavi Teja                 return;
93925e055a3SRavi Teja             }
9408a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9418a07d286SRatan Gupta             return;
9428a07d286SRatan Gupta         }
943002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
9448a07d286SRatan Gupta         auto& searchSettingsJson =
9458a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
9466c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
9476c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
9488a07d286SRatan Gupta         if (baseDNList.size() > 1)
9498a07d286SRatan Gupta         {
950002d39b4SEd Tanous             messages::propertyValueModified(
951002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
9528a07d286SRatan Gupta         }
9538a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
9548a07d286SRatan Gupta         },
9558a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
9568a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
957168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
9588a07d286SRatan Gupta }
9598a07d286SRatan Gupta /**
9608a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
9618a07d286SRatan Gupta           json response with the new value.
9628a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
9638a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9648a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9658a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9668a07d286SRatan Gupta  */
9678a07d286SRatan Gupta 
9684f48d5f6SEd Tanous inline void
9694f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
9708d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9718a07d286SRatan Gupta                             const std::string& ldapServerElementName,
9728a07d286SRatan Gupta                             const std::string& ldapConfigObject)
9738a07d286SRatan Gupta {
9748a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
9758a07d286SRatan Gupta         [asyncResp, userNameAttribute,
9765e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
9778a07d286SRatan Gupta         if (ec)
9788a07d286SRatan Gupta         {
979c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
9808a07d286SRatan Gupta                                 "username attribute";
9818a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9828a07d286SRatan Gupta             return;
9838a07d286SRatan Gupta         }
984002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
9858a07d286SRatan Gupta         auto& searchSettingsJson =
9868a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
9878a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
9888a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
9898a07d286SRatan Gupta         },
9908a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
9918a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
992168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
9938a07d286SRatan Gupta }
9948a07d286SRatan Gupta /**
9958a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
9968a07d286SRatan Gupta           json response with the new value.
9978a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
9988a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9998a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
10008a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
10018a07d286SRatan Gupta  */
10028a07d286SRatan Gupta 
10034f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
10048d1b46d7Szhanghch05     const std::string& groupsAttribute,
10058d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10068a07d286SRatan Gupta     const std::string& ldapServerElementName,
10078a07d286SRatan Gupta     const std::string& ldapConfigObject)
10088a07d286SRatan Gupta {
10098a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
10108a07d286SRatan Gupta         [asyncResp, groupsAttribute,
10115e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
10128a07d286SRatan Gupta         if (ec)
10138a07d286SRatan Gupta         {
1014c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
10158a07d286SRatan Gupta                                 "groupname attribute";
10168a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10178a07d286SRatan Gupta             return;
10188a07d286SRatan Gupta         }
1019002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
10208a07d286SRatan Gupta         auto& searchSettingsJson =
10218a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
10228a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
10238a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
10248a07d286SRatan Gupta         },
10258a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
10268a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
1027168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
10288a07d286SRatan Gupta }
10298a07d286SRatan Gupta /**
10308a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
10318a07d286SRatan Gupta           json response with the new value.
10328a07d286SRatan Gupta  * @param input JSON data.
10338a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
10348a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
10358a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
10368a07d286SRatan Gupta  */
10378a07d286SRatan Gupta 
10384f48d5f6SEd Tanous inline void handleServiceEnablePatch(
10396c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10408a07d286SRatan Gupta     const std::string& ldapServerElementName,
10418a07d286SRatan Gupta     const std::string& ldapConfigObject)
10428a07d286SRatan Gupta {
10438a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
10448a07d286SRatan Gupta         [asyncResp, serviceEnabled,
10455e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
10468a07d286SRatan Gupta         if (ec)
10478a07d286SRatan Gupta         {
1048002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
10498a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10508a07d286SRatan Gupta             return;
10518a07d286SRatan Gupta         }
10526c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
10538a07d286SRatan Gupta             serviceEnabled;
10546c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
10558a07d286SRatan Gupta         },
10568a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
1057168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
1058168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
10598a07d286SRatan Gupta }
10608a07d286SRatan Gupta 
10614f48d5f6SEd Tanous inline void
10624f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
10638d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
106478158631SZbigniew Kurzynski {
106578158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
106678158631SZbigniew Kurzynski     std::optional<bool> cookie;
106778158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
106878158631SZbigniew Kurzynski     std::optional<bool> xToken;
1069501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
107078158631SZbigniew Kurzynski 
107178158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
107278158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
1073501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
107478158631SZbigniew Kurzynski     {
107578158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
107678158631SZbigniew Kurzynski         return;
107778158631SZbigniew Kurzynski     }
107878158631SZbigniew Kurzynski 
107978158631SZbigniew Kurzynski     // Make a copy of methods configuration
108052cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
108152cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
108278158631SZbigniew Kurzynski 
108378158631SZbigniew Kurzynski     if (basicAuth)
108478158631SZbigniew Kurzynski     {
1085f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
1086f16f6263SAlan Kuo         messages::actionNotSupported(
10870fda0f12SGeorge Liu             asyncResp->res,
10880fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
1089f16f6263SAlan Kuo         return;
1090f16f6263SAlan Kuo #endif
109178158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
109278158631SZbigniew Kurzynski     }
109378158631SZbigniew Kurzynski 
109478158631SZbigniew Kurzynski     if (cookie)
109578158631SZbigniew Kurzynski     {
1096f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
10970fda0f12SGeorge Liu         messages::actionNotSupported(
10980fda0f12SGeorge Liu             asyncResp->res,
10990fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
1100f16f6263SAlan Kuo         return;
1101f16f6263SAlan Kuo #endif
110278158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
110378158631SZbigniew Kurzynski     }
110478158631SZbigniew Kurzynski 
110578158631SZbigniew Kurzynski     if (sessionToken)
110678158631SZbigniew Kurzynski     {
1107f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
1108f16f6263SAlan Kuo         messages::actionNotSupported(
11090fda0f12SGeorge Liu             asyncResp->res,
11100fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
1111f16f6263SAlan Kuo         return;
1112f16f6263SAlan Kuo #endif
111378158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
111478158631SZbigniew Kurzynski     }
111578158631SZbigniew Kurzynski 
111678158631SZbigniew Kurzynski     if (xToken)
111778158631SZbigniew Kurzynski     {
1118f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
11190fda0f12SGeorge Liu         messages::actionNotSupported(
11200fda0f12SGeorge Liu             asyncResp->res,
11210fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
1122f16f6263SAlan Kuo         return;
1123f16f6263SAlan Kuo #endif
112478158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
112578158631SZbigniew Kurzynski     }
112678158631SZbigniew Kurzynski 
1127501f1e58SZbigniew Kurzynski     if (tls)
1128501f1e58SZbigniew Kurzynski     {
1129f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
11300fda0f12SGeorge Liu         messages::actionNotSupported(
11310fda0f12SGeorge Liu             asyncResp->res,
11320fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
1133f16f6263SAlan Kuo         return;
1134f16f6263SAlan Kuo #endif
1135501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
1136501f1e58SZbigniew Kurzynski     }
1137501f1e58SZbigniew Kurzynski 
113878158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
1139501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
1140501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
114178158631SZbigniew Kurzynski     {
114278158631SZbigniew Kurzynski         // Do not allow user to disable everything
114378158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
114478158631SZbigniew Kurzynski                                      "of disabling all available methods");
114578158631SZbigniew Kurzynski         return;
114678158631SZbigniew Kurzynski     }
114778158631SZbigniew Kurzynski 
114852cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
114952cc112dSEd Tanous         authMethodsConfig);
115078158631SZbigniew Kurzynski     // Save configuration immediately
115152cc112dSEd Tanous     persistent_data::getConfig().writeData();
115278158631SZbigniew Kurzynski 
115378158631SZbigniew Kurzynski     messages::success(asyncResp->res);
115478158631SZbigniew Kurzynski }
115578158631SZbigniew Kurzynski 
11568a07d286SRatan Gupta /**
11578a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
11588a07d286SRatan Gupta  *        value and create the LDAP config object.
11598a07d286SRatan Gupta  * @param input JSON data
11608a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
11618a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
11628a07d286SRatan Gupta  */
11638a07d286SRatan Gupta 
11646c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
11658d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
11668a07d286SRatan Gupta                             const std::string& serverType)
11678a07d286SRatan Gupta {
1168eb2bbe56SRatan Gupta     std::string dbusObjectPath;
1169eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
1170eb2bbe56SRatan Gupta     {
11712c70f800SEd Tanous         dbusObjectPath = adConfigObject;
1172eb2bbe56SRatan Gupta     }
1173eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
1174eb2bbe56SRatan Gupta     {
117523a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
1176eb2bbe56SRatan Gupta     }
1177cb13a392SEd Tanous     else
1178cb13a392SEd Tanous     {
1179cb13a392SEd Tanous         return;
1180cb13a392SEd Tanous     }
1181eb2bbe56SRatan Gupta 
11828a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
11838a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
11848a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
11858a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
11868a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
11878a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
11888a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
11898a07d286SRatan Gupta     std::optional<std::string> userName;
11908a07d286SRatan Gupta     std::optional<std::string> password;
119106785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
11928a07d286SRatan Gupta 
11938a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
11948a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
11958a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
119606785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
119706785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
11988a07d286SRatan Gupta     {
11998a07d286SRatan Gupta         return;
12008a07d286SRatan Gupta     }
12018a07d286SRatan Gupta 
12028a07d286SRatan Gupta     if (authentication)
12038a07d286SRatan Gupta     {
12048a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
12058a07d286SRatan Gupta                                     password);
12068a07d286SRatan Gupta     }
12078a07d286SRatan Gupta     if (ldapService)
12088a07d286SRatan Gupta     {
12098a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
12108a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
12118a07d286SRatan Gupta     }
12128a07d286SRatan Gupta     if (serviceAddressList)
12138a07d286SRatan Gupta     {
121426f6976fSEd Tanous         if (serviceAddressList->empty())
12158a07d286SRatan Gupta         {
1216e2616cc5SEd Tanous             messages::propertyValueNotInList(
1217e2616cc5SEd Tanous                 asyncResp->res, *serviceAddressList, "ServiceAddress");
12188a07d286SRatan Gupta             return;
12198a07d286SRatan Gupta         }
12208a07d286SRatan Gupta     }
12218a07d286SRatan Gupta     if (baseDNList)
12228a07d286SRatan Gupta     {
122326f6976fSEd Tanous         if (baseDNList->empty())
12248a07d286SRatan Gupta         {
1225e2616cc5SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *baseDNList,
12268a07d286SRatan Gupta                                              "BaseDistinguishedNames");
12278a07d286SRatan Gupta             return;
12288a07d286SRatan Gupta         }
12298a07d286SRatan Gupta     }
12308a07d286SRatan Gupta 
12318a07d286SRatan Gupta     // nothing to update, then return
12328a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
123306785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
123406785244SRatan Gupta         !remoteRoleMapData)
12358a07d286SRatan Gupta     {
12368a07d286SRatan Gupta         return;
12378a07d286SRatan Gupta     }
12388a07d286SRatan Gupta 
12398a07d286SRatan Gupta     // Get the existing resource first then keep modifying
12408a07d286SRatan Gupta     // whenever any property gets updated.
1241002d39b4SEd Tanous     getLDAPConfigData(
1242002d39b4SEd Tanous         serverType,
1243002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1244002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1245002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
124623a21a1cSEd Tanous                             const std::string& serverT) {
12478a07d286SRatan Gupta         if (!success)
12488a07d286SRatan Gupta         {
12498a07d286SRatan Gupta             messages::internalError(asyncResp->res);
12508a07d286SRatan Gupta             return;
12518a07d286SRatan Gupta         }
12526c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
12538a07d286SRatan Gupta         if (confData.serviceEnabled)
12548a07d286SRatan Gupta         {
12558a07d286SRatan Gupta             // Disable the service first and update the rest of
12568a07d286SRatan Gupta             // the properties.
12576c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
12588a07d286SRatan Gupta         }
12598a07d286SRatan Gupta 
12608a07d286SRatan Gupta         if (serviceAddressList)
12618a07d286SRatan Gupta         {
12626c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
12636c51eab1SEd Tanous                                       dbusObjectPath);
12648a07d286SRatan Gupta         }
12658a07d286SRatan Gupta         if (userName)
12668a07d286SRatan Gupta         {
12676c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
12688a07d286SRatan Gupta         }
12698a07d286SRatan Gupta         if (password)
12708a07d286SRatan Gupta         {
12716c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
12728a07d286SRatan Gupta         }
12738a07d286SRatan Gupta 
12748a07d286SRatan Gupta         if (baseDNList)
12758a07d286SRatan Gupta         {
12766c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
12778a07d286SRatan Gupta         }
12788a07d286SRatan Gupta         if (userNameAttribute)
12798a07d286SRatan Gupta         {
12806c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
12816c51eab1SEd Tanous                                     dbusObjectPath);
12828a07d286SRatan Gupta         }
12838a07d286SRatan Gupta         if (groupsAttribute)
12848a07d286SRatan Gupta         {
12856c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
12866c51eab1SEd Tanous                                      dbusObjectPath);
12878a07d286SRatan Gupta         }
12888a07d286SRatan Gupta         if (serviceEnabled)
12898a07d286SRatan Gupta         {
12908a07d286SRatan Gupta             // if user has given the value as true then enable
12918a07d286SRatan Gupta             // the service. if user has given false then no-op
12928a07d286SRatan Gupta             // as service is already stopped.
12938a07d286SRatan Gupta             if (*serviceEnabled)
12948a07d286SRatan Gupta             {
12956c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
12966c51eab1SEd Tanous                                          dbusObjectPath);
12978a07d286SRatan Gupta             }
12988a07d286SRatan Gupta         }
12998a07d286SRatan Gupta         else
13008a07d286SRatan Gupta         {
13018a07d286SRatan Gupta             // if user has not given the service enabled value
13028a07d286SRatan Gupta             // then revert it to the same state as it was
13038a07d286SRatan Gupta             // before.
13048a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
130523a21a1cSEd Tanous                                      serverT, dbusObjectPath);
13068a07d286SRatan Gupta         }
130706785244SRatan Gupta 
130806785244SRatan Gupta         if (remoteRoleMapData)
130906785244SRatan Gupta         {
13106c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
13116c51eab1SEd Tanous                                *remoteRoleMapData);
131206785244SRatan Gupta         }
13138a07d286SRatan Gupta         });
13148a07d286SRatan Gupta }
1315d4b5443fSEd Tanous 
131658345856SAbhishek Patel inline void updateUserProperties(
131758345856SAbhishek Patel     std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& username,
1318618c14b4SEd Tanous     const std::optional<std::string>& password,
1319618c14b4SEd Tanous     const std::optional<bool>& enabled,
132058345856SAbhishek Patel     const std::optional<std::string>& roleId, const std::optional<bool>& locked,
132158345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes, bool userSelf)
13221abe55efSEd Tanous {
1323b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1324b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1325b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
13266c51eab1SEd Tanous 
13276c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1328618c14b4SEd Tanous         dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
132958345856SAbhishek Patel                          locked, accountTypes(std::move(accountTypes)),
133058345856SAbhishek Patel                          userSelf, asyncResp{std::move(asyncResp)}](int rc) {
1331e662eae8SEd Tanous             if (rc <= 0)
13326c51eab1SEd Tanous             {
1333d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
13346c51eab1SEd Tanous                                            username);
13356c51eab1SEd Tanous                 return;
13366c51eab1SEd Tanous             }
13376c51eab1SEd Tanous 
13386c51eab1SEd Tanous             if (password)
13396c51eab1SEd Tanous             {
13406c51eab1SEd Tanous                 int retval = pamUpdatePassword(username, *password);
13416c51eab1SEd Tanous 
13426c51eab1SEd Tanous                 if (retval == PAM_USER_UNKNOWN)
13436c51eab1SEd Tanous                 {
1344d8a5d5d8SJiaqing Zhao                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
13456c51eab1SEd Tanous                                                username);
13466c51eab1SEd Tanous                 }
13476c51eab1SEd Tanous                 else if (retval == PAM_AUTHTOK_ERR)
13486c51eab1SEd Tanous                 {
13496c51eab1SEd Tanous                     // If password is invalid
1350618c14b4SEd Tanous                     messages::propertyValueFormatError(asyncResp->res,
1351618c14b4SEd Tanous                                                        *password, "Password");
13526c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
13536c51eab1SEd Tanous                 }
13546c51eab1SEd Tanous                 else if (retval != PAM_SUCCESS)
13556c51eab1SEd Tanous                 {
13566c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
13576c51eab1SEd Tanous                     return;
13586c51eab1SEd Tanous                 }
1359e7b1b62bSEd Tanous                 else
1360e7b1b62bSEd Tanous                 {
1361e7b1b62bSEd Tanous                     messages::success(asyncResp->res);
1362e7b1b62bSEd Tanous                 }
13636c51eab1SEd Tanous             }
13646c51eab1SEd Tanous 
13656c51eab1SEd Tanous             if (enabled)
13666c51eab1SEd Tanous             {
13676c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
13685e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
13696c51eab1SEd Tanous                 if (ec)
13706c51eab1SEd Tanous                 {
13716c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
13726c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
13736c51eab1SEd Tanous                     return;
13746c51eab1SEd Tanous                 }
13756c51eab1SEd Tanous                 messages::success(asyncResp->res);
13766c51eab1SEd Tanous                 return;
13776c51eab1SEd Tanous                     },
1378e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
13796c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
13806c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserEnabled",
1381168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*enabled});
13826c51eab1SEd Tanous             }
13836c51eab1SEd Tanous 
13846c51eab1SEd Tanous             if (roleId)
13856c51eab1SEd Tanous             {
13866c51eab1SEd Tanous                 std::string priv = getPrivilegeFromRoleId(*roleId);
13876c51eab1SEd Tanous                 if (priv.empty())
13886c51eab1SEd Tanous                 {
1389e2616cc5SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, true,
1390e2616cc5SEd Tanous                                                      "Locked");
13916c51eab1SEd Tanous                     return;
13926c51eab1SEd Tanous                 }
13936c51eab1SEd Tanous 
13946c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
13955e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
13966c51eab1SEd Tanous                 if (ec)
13976c51eab1SEd Tanous                 {
13986c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
13996c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
14006c51eab1SEd Tanous                     return;
14016c51eab1SEd Tanous                 }
14026c51eab1SEd Tanous                 messages::success(asyncResp->res);
14036c51eab1SEd Tanous                     },
1404e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
14056c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
14066c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1407168e20c1SEd Tanous                     dbus::utility::DbusVariantType{priv});
14086c51eab1SEd Tanous             }
14096c51eab1SEd Tanous 
14106c51eab1SEd Tanous             if (locked)
14116c51eab1SEd Tanous             {
14126c51eab1SEd Tanous                 // admin can unlock the account which is locked by
14136c51eab1SEd Tanous                 // successive authentication failures but admin should
14146c51eab1SEd Tanous                 // not be allowed to lock an account.
14156c51eab1SEd Tanous                 if (*locked)
14166c51eab1SEd Tanous                 {
14176c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, "true",
14186c51eab1SEd Tanous                                                      "Locked");
14196c51eab1SEd Tanous                     return;
14206c51eab1SEd Tanous                 }
14216c51eab1SEd Tanous 
14226c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
14235e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
14246c51eab1SEd Tanous                 if (ec)
14256c51eab1SEd Tanous                 {
14266c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
14276c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
14286c51eab1SEd Tanous                     return;
14296c51eab1SEd Tanous                 }
14306c51eab1SEd Tanous                 messages::success(asyncResp->res);
14316c51eab1SEd Tanous                 return;
14326c51eab1SEd Tanous                     },
1433e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
14346c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
14356c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes",
1436168e20c1SEd Tanous                     "UserLockedForFailedAttempt",
1437168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*locked});
14386c51eab1SEd Tanous             }
143958345856SAbhishek Patel 
144058345856SAbhishek Patel             if (accountTypes)
144158345856SAbhishek Patel             {
144258345856SAbhishek Patel                 patchAccountTypes(*accountTypes, asyncResp, dbusObjectPath,
144358345856SAbhishek Patel                                   userSelf);
144458345856SAbhishek Patel             }
14456c51eab1SEd Tanous         });
14466c51eab1SEd Tanous }
14476c51eab1SEd Tanous 
14484c7d4d33SEd Tanous inline void handleAccountServiceHead(
14494c7d4d33SEd Tanous     App& app, const crow::Request& req,
14501ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14516c51eab1SEd Tanous {
14523ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
145345ca1b86SEd Tanous     {
145445ca1b86SEd Tanous         return;
145545ca1b86SEd Tanous     }
14564c7d4d33SEd Tanous     asyncResp->res.addHeader(
14574c7d4d33SEd Tanous         boost::beast::http::field::link,
14584c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
14594c7d4d33SEd Tanous }
14604c7d4d33SEd Tanous 
14614c7d4d33SEd Tanous inline void
14624c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
14634c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14644c7d4d33SEd Tanous {
1465afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1466afd369c6SJiaqing Zhao     {
1467afd369c6SJiaqing Zhao         return;
1468afd369c6SJiaqing Zhao     }
14693e72c202SNinad Palsule 
14703e72c202SNinad Palsule     if (req.session == nullptr)
14713e72c202SNinad Palsule     {
14723e72c202SNinad Palsule         messages::internalError(asyncResp->res);
14733e72c202SNinad Palsule         return;
14743e72c202SNinad Palsule     }
14753e72c202SNinad Palsule 
1476afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1477afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1478afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1479afd369c6SJiaqing Zhao 
148052cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
14811ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
148278158631SZbigniew Kurzynski 
14831476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
14841476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
14851476687dSEd Tanous     json["@odata.type"] = "#AccountService."
14861476687dSEd Tanous                           "v1_10_0.AccountService";
14871476687dSEd Tanous     json["Id"] = "AccountService";
14881476687dSEd Tanous     json["Name"] = "Account Service";
14891476687dSEd Tanous     json["Description"] = "Account Service";
14901476687dSEd Tanous     json["ServiceEnabled"] = true;
14911476687dSEd Tanous     json["MaxPasswordLength"] = 20;
14921ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
14931476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
14941476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
14955b5574acSEd Tanous         "#OpenBMCAccountService.v1_0_0.AccountService";
14961476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
14971476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
14981476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
14991476687dSEd Tanous         authMethodsConfig.basic;
15001476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
15011476687dSEd Tanous         authMethodsConfig.sessionToken;
15021ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
15031ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
15041ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
15051476687dSEd Tanous 
15061ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
15071ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
15081ef4c342SEd Tanous     // permissions ConfigureManager
150972048780SAbhishek Patel     Privileges effectiveUserPrivileges =
15103e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
151172048780SAbhishek Patel 
151272048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
151372048780SAbhishek Patel                                          effectiveUserPrivileges))
151472048780SAbhishek Patel     {
15151ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
15161476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
151772048780SAbhishek Patel     }
1518d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1519d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1520d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
15215e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
15221ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
15233d958bbcSAppaRao Puli         if (ec)
15243d958bbcSAppaRao Puli         {
15253d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
15263d958bbcSAppaRao Puli             return;
15273d958bbcSAppaRao Puli         }
1528d1bde9e5SKrzysztof Grobelny 
15293d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
15303d958bbcSAppaRao Puli                          << "properties for AccountService";
1531d1bde9e5SKrzysztof Grobelny 
1532d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1533d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1534d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1535d1bde9e5SKrzysztof Grobelny 
1536d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1537d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1538d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1539d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1540d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1541d1bde9e5SKrzysztof Grobelny 
1542d1bde9e5SKrzysztof Grobelny         if (!success)
15433d958bbcSAppaRao Puli         {
1544d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1545d1bde9e5SKrzysztof Grobelny             return;
15463d958bbcSAppaRao Puli         }
1547d1bde9e5SKrzysztof Grobelny 
1548d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
15493d958bbcSAppaRao Puli         {
1550d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
15513d958bbcSAppaRao Puli         }
1552d1bde9e5SKrzysztof Grobelny 
1553d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
15543d958bbcSAppaRao Puli         {
1555d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1556d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1557d1bde9e5SKrzysztof Grobelny         }
1558d1bde9e5SKrzysztof Grobelny 
1559d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
15603d958bbcSAppaRao Puli         {
1561002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1562d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
15633d958bbcSAppaRao Puli         }
1564d1bde9e5SKrzysztof Grobelny         });
15656973a582SRatan Gupta 
156602cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1567ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1568cb13a392SEd Tanous         if (!success)
1569cb13a392SEd Tanous         {
1570cb13a392SEd Tanous             return;
1571cb13a392SEd Tanous         }
1572002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1573ab828d7cSRatan Gupta     };
1574ab828d7cSRatan Gupta 
1575ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1576ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
15771ef4c342SEd Tanous }
15786973a582SRatan Gupta 
15791ef4c342SEd Tanous inline void handleAccountServicePatch(
15801ef4c342SEd Tanous     App& app, const crow::Request& req,
15811ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15821ef4c342SEd Tanous {
15833ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
158445ca1b86SEd Tanous     {
158545ca1b86SEd Tanous         return;
158645ca1b86SEd Tanous     }
1587f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1588f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1589ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1590f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1591f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1592f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1593f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1594f5ffd806SEd Tanous 
159515ed6780SWilly Tu     if (!json_util::readJsonPatch(
15961ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
15971ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
15981ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
15991ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1600f5ffd806SEd Tanous             oemObject))
1601f5ffd806SEd Tanous     {
1602f5ffd806SEd Tanous         return;
1603f5ffd806SEd Tanous     }
1604f5ffd806SEd Tanous 
1605f5ffd806SEd Tanous     if (minPasswordLength)
1606f5ffd806SEd Tanous     {
1607ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
16085e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1609ef73ad0dSPaul Fertser             if (ec)
1610ef73ad0dSPaul Fertser             {
1611ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1612ef73ad0dSPaul Fertser                 return;
1613ef73ad0dSPaul Fertser             }
1614ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1615ef73ad0dSPaul Fertser             },
16161ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1617ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
16181ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1619ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1620f5ffd806SEd Tanous     }
1621f5ffd806SEd Tanous 
1622f5ffd806SEd Tanous     if (maxPasswordLength)
1623f5ffd806SEd Tanous     {
16241ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1625f5ffd806SEd Tanous     }
1626f5ffd806SEd Tanous 
1627f5ffd806SEd Tanous     if (ldapObject)
1628f5ffd806SEd Tanous     {
1629f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1630f5ffd806SEd Tanous     }
1631f5ffd806SEd Tanous 
1632f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
16331ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1634f5ffd806SEd Tanous                                          oemOpenBMCObject))
1635f5ffd806SEd Tanous     {
1636f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1637f5ffd806SEd Tanous             oemOpenBMCObject &&
1638f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1639f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1640f5ffd806SEd Tanous         {
1641f5ffd806SEd Tanous             if (authMethodsObject)
1642f5ffd806SEd Tanous             {
16431ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1644f5ffd806SEd Tanous             }
1645f5ffd806SEd Tanous         }
1646f5ffd806SEd Tanous     }
1647f5ffd806SEd Tanous 
1648f5ffd806SEd Tanous     if (activeDirectoryObject)
1649f5ffd806SEd Tanous     {
16501ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1651f5ffd806SEd Tanous     }
1652f5ffd806SEd Tanous 
1653f5ffd806SEd Tanous     if (unlockTimeout)
1654f5ffd806SEd Tanous     {
1655f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
16565e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1657f5ffd806SEd Tanous             if (ec)
1658f5ffd806SEd Tanous             {
1659f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1660f5ffd806SEd Tanous                 return;
1661f5ffd806SEd Tanous             }
1662f5ffd806SEd Tanous             messages::success(asyncResp->res);
1663f5ffd806SEd Tanous             },
16641ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1665f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
16661ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1667168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1668f5ffd806SEd Tanous     }
1669f5ffd806SEd Tanous     if (lockoutThreshold)
1670f5ffd806SEd Tanous     {
1671f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
16725e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1673f5ffd806SEd Tanous             if (ec)
1674f5ffd806SEd Tanous             {
1675f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1676f5ffd806SEd Tanous                 return;
1677f5ffd806SEd Tanous             }
1678f5ffd806SEd Tanous             messages::success(asyncResp->res);
1679f5ffd806SEd Tanous             },
16801ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1681f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1682f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1683f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1684168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1685f5ffd806SEd Tanous     }
16861ef4c342SEd Tanous }
1687f5ffd806SEd Tanous 
16884c7d4d33SEd Tanous inline void handleAccountCollectionHead(
16891ef4c342SEd Tanous     App& app, const crow::Request& req,
16901ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16911ef4c342SEd Tanous {
16923ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
169345ca1b86SEd Tanous     {
169445ca1b86SEd Tanous         return;
169545ca1b86SEd Tanous     }
16964c7d4d33SEd Tanous     asyncResp->res.addHeader(
16974c7d4d33SEd Tanous         boost::beast::http::field::link,
16984c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
16994c7d4d33SEd Tanous }
17004c7d4d33SEd Tanous 
17014c7d4d33SEd Tanous inline void handleAccountCollectionGet(
17024c7d4d33SEd Tanous     App& app, const crow::Request& req,
17034c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
17044c7d4d33SEd Tanous {
1705afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1706afd369c6SJiaqing Zhao     {
1707afd369c6SJiaqing Zhao         return;
1708afd369c6SJiaqing Zhao     }
17093e72c202SNinad Palsule 
17103e72c202SNinad Palsule     if (req.session == nullptr)
17113e72c202SNinad Palsule     {
17123e72c202SNinad Palsule         messages::internalError(asyncResp->res);
17133e72c202SNinad Palsule         return;
17143e72c202SNinad Palsule     }
17153e72c202SNinad Palsule 
1716afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1717afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1718afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
17191476687dSEd Tanous 
17201476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
17211476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
17221ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
17231476687dSEd Tanous                                               "ManagerAccountCollection";
17241476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
17251476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
17260f74e643SEd Tanous 
17276c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
17283e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
17296c51eab1SEd Tanous 
1730f5e29f33SJunLin Chen     std::string thisUser;
1731f5e29f33SJunLin Chen     if (req.session)
1732f5e29f33SJunLin Chen     {
1733f5e29f33SJunLin Chen         thisUser = req.session->username;
1734f5e29f33SJunLin Chen     }
1735b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
1736cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
17375e7e2dc5SEd Tanous             const boost::system::error_code& ec,
1738711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1739b9b2e0b2SEd Tanous         if (ec)
1740b9b2e0b2SEd Tanous         {
1741f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1742b9b2e0b2SEd Tanous             return;
1743b9b2e0b2SEd Tanous         }
1744b9b2e0b2SEd Tanous 
1745cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1746002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1747cef1ddfbSEd Tanous 
1748cef1ddfbSEd Tanous         bool userCanSeeSelf =
1749002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1750cef1ddfbSEd Tanous 
1751002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1752b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1753b9b2e0b2SEd Tanous 
17549eb808c1SEd Tanous         for (const auto& userpath : users)
1755b9b2e0b2SEd Tanous         {
17562dfd18efSEd Tanous             std::string user = userpath.first.filename();
17572dfd18efSEd Tanous             if (user.empty())
1758b9b2e0b2SEd Tanous             {
17592dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
17602dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
17612dfd18efSEd Tanous 
17622dfd18efSEd Tanous                 return;
1763b9b2e0b2SEd Tanous             }
1764f365910cSGunnar Mills 
1765f365910cSGunnar Mills             // As clarified by Redfish here:
1766f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
17676c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
17686c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
17696c51eab1SEd Tanous             // accounts.
17701ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1771f365910cSGunnar Mills             {
17721476687dSEd Tanous                 nlohmann::json::object_t member;
177389492a15SPatrick Williams                 member["@odata.id"] = "/redfish/v1/AccountService/Accounts/" +
177489492a15SPatrick Williams                                       user;
1775b2ba3072SPatrick Williams                 memberArray.emplace_back(std::move(member));
1776b9b2e0b2SEd Tanous             }
1777f365910cSGunnar Mills         }
17781ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1779b9b2e0b2SEd Tanous         },
17801ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1781b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
17821ef4c342SEd Tanous }
17836c51eab1SEd Tanous 
178497e90da3SNinad Palsule inline void processAfterCreateUser(
178597e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
178697e90da3SNinad Palsule     const std::string& username, const std::string& password,
178797e90da3SNinad Palsule     const boost::system::error_code& ec, sdbusplus::message_t& m)
178897e90da3SNinad Palsule {
178997e90da3SNinad Palsule     if (ec)
179097e90da3SNinad Palsule     {
179197e90da3SNinad Palsule         userErrorMessageHandler(m.get_error(), asyncResp, username, "");
179297e90da3SNinad Palsule         return;
179397e90da3SNinad Palsule     }
179497e90da3SNinad Palsule 
179597e90da3SNinad Palsule     if (pamUpdatePassword(username, password) != PAM_SUCCESS)
179697e90da3SNinad Palsule     {
179797e90da3SNinad Palsule         // At this point we have a user that's been
179897e90da3SNinad Palsule         // created, but the password set
179997e90da3SNinad Palsule         // failed.Something is wrong, so delete the user
180097e90da3SNinad Palsule         // that we've already created
180197e90da3SNinad Palsule         sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
180297e90da3SNinad Palsule         tempObjPath /= username;
180397e90da3SNinad Palsule         const std::string userPath(tempObjPath);
180497e90da3SNinad Palsule 
180597e90da3SNinad Palsule         crow::connections::systemBus->async_method_call(
180697e90da3SNinad Palsule             [asyncResp, password](const boost::system::error_code& ec3) {
180797e90da3SNinad Palsule             if (ec3)
180897e90da3SNinad Palsule             {
180997e90da3SNinad Palsule                 messages::internalError(asyncResp->res);
181097e90da3SNinad Palsule                 return;
181197e90da3SNinad Palsule             }
181297e90da3SNinad Palsule 
181397e90da3SNinad Palsule             // If password is invalid
181497e90da3SNinad Palsule             messages::propertyValueFormatError(asyncResp->res, password,
181597e90da3SNinad Palsule                                                "Password");
181697e90da3SNinad Palsule             },
181797e90da3SNinad Palsule             "xyz.openbmc_project.User.Manager", userPath,
181897e90da3SNinad Palsule             "xyz.openbmc_project.Object.Delete", "Delete");
181997e90da3SNinad Palsule 
182097e90da3SNinad Palsule         BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
182197e90da3SNinad Palsule         return;
182297e90da3SNinad Palsule     }
182397e90da3SNinad Palsule 
182497e90da3SNinad Palsule     messages::created(asyncResp->res);
182597e90da3SNinad Palsule     asyncResp->res.addHeader("Location",
182697e90da3SNinad Palsule                              "/redfish/v1/AccountService/Accounts/" + username);
182797e90da3SNinad Palsule }
182897e90da3SNinad Palsule 
182997e90da3SNinad Palsule inline void processAfterGetAllGroups(
183097e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
183197e90da3SNinad Palsule     const std::string& username, const std::string& password,
183297e90da3SNinad Palsule     const std::optional<std::string>& roleId, std::optional<bool> enabled,
18339ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes,
183497e90da3SNinad Palsule     const std::vector<std::string>& allGroupsList)
183597e90da3SNinad Palsule {
18363e72c202SNinad Palsule     std::vector<std::string> userGroups;
18379ba73934SNinad Palsule     std::vector<std::string> accountTypeUserGroups;
18389ba73934SNinad Palsule 
18399ba73934SNinad Palsule     // If user specified account types then convert them to unix user groups
18409ba73934SNinad Palsule     if (accountTypes)
18419ba73934SNinad Palsule     {
18429ba73934SNinad Palsule         if (!getUserGroupFromAccountType(asyncResp->res, *accountTypes,
18439ba73934SNinad Palsule                                          accountTypeUserGroups))
18449ba73934SNinad Palsule         {
18459ba73934SNinad Palsule             // Problem in mapping Account Types to User Groups, Error already
18469ba73934SNinad Palsule             // logged.
18479ba73934SNinad Palsule             return;
18489ba73934SNinad Palsule         }
18499ba73934SNinad Palsule     }
18509ba73934SNinad Palsule 
18513e72c202SNinad Palsule     for (const auto& grp : allGroupsList)
18523e72c202SNinad Palsule     {
18539ba73934SNinad Palsule         // If user specified the account type then only accept groups which are
18549ba73934SNinad Palsule         // in the account types group list.
18559ba73934SNinad Palsule         if (!accountTypeUserGroups.empty())
18569ba73934SNinad Palsule         {
18579ba73934SNinad Palsule             bool found = false;
18589ba73934SNinad Palsule             for (const auto& grp1 : accountTypeUserGroups)
18599ba73934SNinad Palsule             {
18609ba73934SNinad Palsule                 if (grp == grp1)
18619ba73934SNinad Palsule                 {
18629ba73934SNinad Palsule                     found = true;
18639ba73934SNinad Palsule                     break;
18649ba73934SNinad Palsule                 }
18659ba73934SNinad Palsule             }
18669ba73934SNinad Palsule             if (!found)
18679ba73934SNinad Palsule             {
18689ba73934SNinad Palsule                 continue;
18699ba73934SNinad Palsule             }
18709ba73934SNinad Palsule         }
18719ba73934SNinad Palsule 
18723e72c202SNinad Palsule         // Console access is provided to the user who is a member of
18733e72c202SNinad Palsule         // hostconsole group and has a administrator role. So, set
18743e72c202SNinad Palsule         // hostconsole group only for the administrator.
18759ba73934SNinad Palsule         if ((grp == "hostconsole") && (roleId != "priv-admin"))
18763e72c202SNinad Palsule         {
18779ba73934SNinad Palsule             if (!accountTypeUserGroups.empty())
18789ba73934SNinad Palsule             {
18799ba73934SNinad Palsule                 BMCWEB_LOG_ERROR
18809ba73934SNinad Palsule                     << "Only administrator can get HostConsole access";
18819ba73934SNinad Palsule                 asyncResp->res.result(boost::beast::http::status::bad_request);
18829ba73934SNinad Palsule                 return;
18839ba73934SNinad Palsule             }
18849ba73934SNinad Palsule             continue;
18859ba73934SNinad Palsule         }
18863e72c202SNinad Palsule         userGroups.emplace_back(grp);
18873e72c202SNinad Palsule     }
18889ba73934SNinad Palsule 
18899ba73934SNinad Palsule     // Make sure user specified groups are valid. This is internal error because
18909ba73934SNinad Palsule     // it some inconsistencies between user manager and bmcweb.
18919ba73934SNinad Palsule     if (!accountTypeUserGroups.empty() &&
18929ba73934SNinad Palsule         accountTypeUserGroups.size() != userGroups.size())
18939ba73934SNinad Palsule     {
18949ba73934SNinad Palsule         messages::internalError(asyncResp->res);
18959ba73934SNinad Palsule         return;
18963e72c202SNinad Palsule     }
18973e72c202SNinad Palsule 
189897e90da3SNinad Palsule     crow::connections::systemBus->async_method_call(
189997e90da3SNinad Palsule         [asyncResp, username, password](const boost::system::error_code& ec2,
190097e90da3SNinad Palsule                                         sdbusplus::message_t& m) {
190197e90da3SNinad Palsule         processAfterCreateUser(asyncResp, username, password, ec2, m);
190297e90da3SNinad Palsule         },
190397e90da3SNinad Palsule         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
19043e72c202SNinad Palsule         "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
19053e72c202SNinad Palsule         *roleId, *enabled);
190697e90da3SNinad Palsule }
190797e90da3SNinad Palsule 
19081ef4c342SEd Tanous inline void handleAccountCollectionPost(
19091ef4c342SEd Tanous     App& app, const crow::Request& req,
19101ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
19111ef4c342SEd Tanous {
19123ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
191345ca1b86SEd Tanous     {
191445ca1b86SEd Tanous         return;
191545ca1b86SEd Tanous     }
19169712f8acSEd Tanous     std::string username;
19179712f8acSEd Tanous     std::string password;
1918a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1919a24526dcSEd Tanous     std::optional<bool> enabled = true;
19209ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes;
19219ba73934SNinad Palsule     if (!json_util::readJsonPatch(
19229ba73934SNinad Palsule             req, asyncResp->res, "UserName", username, "Password", password,
19239ba73934SNinad Palsule             "RoleId", roleId, "Enabled", enabled, "AccountTypes", accountTypes))
192404ae99ecSEd Tanous     {
192504ae99ecSEd Tanous         return;
192604ae99ecSEd Tanous     }
192704ae99ecSEd Tanous 
192854fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
192984e12cb7SAppaRao Puli     if (priv.empty())
193004ae99ecSEd Tanous     {
19311ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
193204ae99ecSEd Tanous         return;
193304ae99ecSEd Tanous     }
19349712f8acSEd Tanous     roleId = priv;
193504ae99ecSEd Tanous 
1936599c71d8SAyushi Smriti     // Reading AllGroups property
19371e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
19381ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
19391ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
19401ef4c342SEd Tanous         "AllGroups",
19419ba73934SNinad Palsule         [asyncResp, username, password{std::move(password)}, roleId, enabled,
19429ba73934SNinad Palsule          accountTypes](const boost::system::error_code& ec,
19431e1e598dSJonathan Doman                        const std::vector<std::string>& allGroupsList) {
1944599c71d8SAyushi Smriti         if (ec)
1945599c71d8SAyushi Smriti         {
1946599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1947599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1948599c71d8SAyushi Smriti             return;
1949599c71d8SAyushi Smriti         }
1950599c71d8SAyushi Smriti 
19511e1e598dSJonathan Doman         if (allGroupsList.empty())
1952599c71d8SAyushi Smriti         {
1953599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1954599c71d8SAyushi Smriti             return;
1955599c71d8SAyushi Smriti         }
1956599c71d8SAyushi Smriti 
195797e90da3SNinad Palsule         processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
19589ba73934SNinad Palsule                                  accountTypes, allGroupsList);
19591e1e598dSJonathan Doman         });
19601ef4c342SEd Tanous }
1961b9b2e0b2SEd Tanous 
19621ef4c342SEd Tanous inline void
19634c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
19646c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19654c7d4d33SEd Tanous                       const std::string& /*accountName*/)
19661ef4c342SEd Tanous {
19673ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
196845ca1b86SEd Tanous     {
196945ca1b86SEd Tanous         return;
197045ca1b86SEd Tanous     }
19714c7d4d33SEd Tanous     asyncResp->res.addHeader(
19724c7d4d33SEd Tanous         boost::beast::http::field::link,
19734c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
19744c7d4d33SEd Tanous }
1975afd369c6SJiaqing Zhao 
19764c7d4d33SEd Tanous inline void
19774c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
19784c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19794c7d4d33SEd Tanous                      const std::string& accountName)
19804c7d4d33SEd Tanous {
1981afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1982afd369c6SJiaqing Zhao     {
1983afd369c6SJiaqing Zhao         return;
1984afd369c6SJiaqing Zhao     }
1985afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1986afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1987afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1988afd369c6SJiaqing Zhao 
19891ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1990031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1991d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1992031514fbSJunLin Chen     return;
19931ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1994afd369c6SJiaqing Zhao 
1995031514fbSJunLin Chen     if (req.session == nullptr)
1996031514fbSJunLin Chen     {
1997031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1998031514fbSJunLin Chen         return;
1999031514fbSJunLin Chen     }
20006c51eab1SEd Tanous     if (req.session->username != accountName)
2001b9b2e0b2SEd Tanous     {
20026c51eab1SEd Tanous         // At this point we've determined that the user is trying to
20031ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
20041ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
20051ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
20066c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
20073e72c202SNinad Palsule             redfish::getUserPrivileges(*req.session);
20081ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
20091ef4c342SEd Tanous                                                          "ConfigureManager"};
20106c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
20116c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
2012900f9497SJoseph Reynolds         {
2013900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
2014900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
2015900f9497SJoseph Reynolds             return;
2016900f9497SJoseph Reynolds         }
2017900f9497SJoseph Reynolds     }
2018900f9497SJoseph Reynolds 
2019b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
20201ef4c342SEd Tanous         [asyncResp,
20215e7e2dc5SEd Tanous          accountName](const boost::system::error_code& ec,
2022711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
2023b9b2e0b2SEd Tanous         if (ec)
2024b9b2e0b2SEd Tanous         {
2025f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2026b9b2e0b2SEd Tanous             return;
2027b9b2e0b2SEd Tanous         }
2028711ac7a9SEd Tanous         const auto userIt = std::find_if(
2029b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
2030b477fd44SP Dheeraj Srujan Kumar             [accountName](
2031b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
2032002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
203355f79e6fSEd Tanous             return accountName == user.first.filename();
2034b477fd44SP Dheeraj Srujan Kumar             });
2035b9b2e0b2SEd Tanous 
203684e12cb7SAppaRao Puli         if (userIt == users.end())
2037b9b2e0b2SEd Tanous         {
2038002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
2039002d39b4SEd Tanous                                        accountName);
204084e12cb7SAppaRao Puli             return;
204184e12cb7SAppaRao Puli         }
20424e68c45bSAyushi Smriti 
20431476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
204458345856SAbhishek Patel             "#ManagerAccount.v1_7_0.ManagerAccount";
20451476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
20461476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
20471476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
204858345856SAbhishek Patel         asyncResp->res.jsonValue["StrictAccountTypes"] = true;
20494e68c45bSAyushi Smriti 
205084e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
205165b0dc32SEd Tanous         {
2052002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
205365b0dc32SEd Tanous             {
205465b0dc32SEd Tanous                 for (const auto& property : interface.second)
205565b0dc32SEd Tanous                 {
205665b0dc32SEd Tanous                     if (property.first == "UserEnabled")
205765b0dc32SEd Tanous                     {
205865b0dc32SEd Tanous                         const bool* userEnabled =
2059abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
206065b0dc32SEd Tanous                         if (userEnabled == nullptr)
206165b0dc32SEd Tanous                         {
2062002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
206384e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
206484e12cb7SAppaRao Puli                             return;
206565b0dc32SEd Tanous                         }
2066002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
206765b0dc32SEd Tanous                     }
2068002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
206965b0dc32SEd Tanous                     {
207065b0dc32SEd Tanous                         const bool* userLocked =
2071abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
207265b0dc32SEd Tanous                         if (userLocked == nullptr)
207365b0dc32SEd Tanous                         {
207484e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
207584e12cb7SAppaRao Puli                                                 "ailedAttempt "
207684e12cb7SAppaRao Puli                                                 "wasn't a bool";
207784e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
207884e12cb7SAppaRao Puli                             return;
207965b0dc32SEd Tanous                         }
2080002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
2081002d39b4SEd Tanous                         asyncResp->res
2082002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
20833bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
208465b0dc32SEd Tanous                     }
208584e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
208684e12cb7SAppaRao Puli                     {
208754fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
2088002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
208954fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
209084e12cb7SAppaRao Puli                         {
2091002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
209284e12cb7SAppaRao Puli                                                 "string";
209384e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
209484e12cb7SAppaRao Puli                             return;
209584e12cb7SAppaRao Puli                         }
20961ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
209754fc587aSNagaraju Goruganti                         if (role.empty())
209884e12cb7SAppaRao Puli                         {
209984e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
210084e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
210184e12cb7SAppaRao Puli                             return;
210284e12cb7SAppaRao Puli                         }
210354fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
210484e12cb7SAppaRao Puli 
21051476687dSEd Tanous                         nlohmann::json& roleEntry =
2106002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
21071476687dSEd Tanous                         roleEntry["@odata.id"] =
2108002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
210984e12cb7SAppaRao Puli                     }
2110002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
21113bf4e632SJoseph Reynolds                     {
21123bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
21133bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
21143bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
21153bf4e632SJoseph Reynolds                         {
21160fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
21170fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
21183bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
21193bf4e632SJoseph Reynolds                             return;
21203bf4e632SJoseph Reynolds                         }
2121002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
21223bf4e632SJoseph Reynolds                             *userPasswordExpired;
21233bf4e632SJoseph Reynolds                     }
2124c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
2125c7229815SAbhishek Patel                     {
2126c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
2127c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
2128c7229815SAbhishek Patel                                 &property.second);
2129c7229815SAbhishek Patel                         if (userGroups == nullptr)
2130c7229815SAbhishek Patel                         {
2131c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR
2132c7229815SAbhishek Patel                                 << "userGroups wasn't a string vector";
2133c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2134c7229815SAbhishek Patel                             return;
2135c7229815SAbhishek Patel                         }
2136c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
2137c7229815SAbhishek Patel                         {
2138c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR << "userGroups mapping failed";
2139c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2140c7229815SAbhishek Patel                             return;
2141c7229815SAbhishek Patel                         }
2142c7229815SAbhishek Patel                     }
214365b0dc32SEd Tanous                 }
214465b0dc32SEd Tanous             }
214565b0dc32SEd Tanous         }
214665b0dc32SEd Tanous 
2147b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
214884e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
2149b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
2150b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
2151b9b2e0b2SEd Tanous         },
21521ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
2153b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
21541ef4c342SEd Tanous }
2155a840879dSEd Tanous 
21561ef4c342SEd Tanous inline void
215720fc307fSGunnar Mills     handleAccountDelete(App& app, const crow::Request& req,
21586c51eab1SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21591ef4c342SEd Tanous                         const std::string& username)
21601ef4c342SEd Tanous {
21613ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
216245ca1b86SEd Tanous     {
216345ca1b86SEd Tanous         return;
216445ca1b86SEd Tanous     }
21651ef4c342SEd Tanous 
21661ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
2167031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
2168d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
2169031514fbSJunLin Chen     return;
2170031514fbSJunLin Chen 
21711ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
21721ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
21731ef4c342SEd Tanous     tempObjPath /= username;
21741ef4c342SEd Tanous     const std::string userPath(tempObjPath);
21751ef4c342SEd Tanous 
21761ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
21775e7e2dc5SEd Tanous         [asyncResp, username](const boost::system::error_code& ec) {
21781ef4c342SEd Tanous         if (ec)
21791ef4c342SEd Tanous         {
2180d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
21811ef4c342SEd Tanous                                        username);
21821ef4c342SEd Tanous             return;
21831ef4c342SEd Tanous         }
21841ef4c342SEd Tanous 
21851ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
21861ef4c342SEd Tanous         },
21871ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
21881ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
21891ef4c342SEd Tanous }
21901ef4c342SEd Tanous 
21911ef4c342SEd Tanous inline void
21921ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
21931ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21941ef4c342SEd Tanous                        const std::string& username)
21951ef4c342SEd Tanous {
21961ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
21971ef4c342SEd Tanous     {
21981ef4c342SEd Tanous         return;
21991ef4c342SEd Tanous     }
22001ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
22011ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
2202d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
22031ef4c342SEd Tanous     return;
22041ef4c342SEd Tanous 
22051ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
2206a24526dcSEd Tanous     std::optional<std::string> newUserName;
2207a24526dcSEd Tanous     std::optional<std::string> password;
2208a24526dcSEd Tanous     std::optional<bool> enabled;
2209a24526dcSEd Tanous     std::optional<std::string> roleId;
221024c8542dSRatan Gupta     std::optional<bool> locked;
221158345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes;
221258345856SAbhishek Patel 
221358345856SAbhishek Patel     bool userSelf = (username == req.session->username);
2214e9cc5172SEd Tanous 
2215031514fbSJunLin Chen     if (req.session == nullptr)
2216031514fbSJunLin Chen     {
2217031514fbSJunLin Chen         messages::internalError(asyncResp->res);
2218031514fbSJunLin Chen         return;
2219031514fbSJunLin Chen     }
2220031514fbSJunLin Chen 
2221e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
22223e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
2223e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
2224e9cc5172SEd Tanous     bool userHasConfigureUsers =
2225e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
2226e9cc5172SEd Tanous     if (userHasConfigureUsers)
2227e9cc5172SEd Tanous     {
2228e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
222958345856SAbhishek Patel         if (!json_util::readJsonPatch(
223058345856SAbhishek Patel                 req, asyncResp->res, "UserName", newUserName, "Password",
223158345856SAbhishek Patel                 password, "RoleId", roleId, "Enabled", enabled, "Locked",
223258345856SAbhishek Patel                 locked, "AccountTypes", accountTypes))
2233a840879dSEd Tanous         {
2234a840879dSEd Tanous             return;
2235a840879dSEd Tanous         }
2236e9cc5172SEd Tanous     }
2237e9cc5172SEd Tanous     else
2238900f9497SJoseph Reynolds     {
2239e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
224058345856SAbhishek Patel         if (!userSelf)
2241900f9497SJoseph Reynolds         {
2242900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
2243900f9497SJoseph Reynolds             return;
2244900f9497SJoseph Reynolds         }
2245031514fbSJunLin Chen 
2246e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
22471ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
22481ef4c342SEd Tanous                                       password))
2249e9cc5172SEd Tanous         {
2250e9cc5172SEd Tanous             return;
2251e9cc5172SEd Tanous         }
2252900f9497SJoseph Reynolds     }
2253900f9497SJoseph Reynolds 
225466b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
22556c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
22566c51eab1SEd Tanous     // updating user properties other then username. If username
22576c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
22586c51eab1SEd Tanous     // user rename request.
225966b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
2260a840879dSEd Tanous     {
22611ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
226258345856SAbhishek Patel                              locked, accountTypes, userSelf);
226384e12cb7SAppaRao Puli         return;
226484e12cb7SAppaRao Puli     }
226584e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
22666c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
22671ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
226858345856SAbhishek Patel          locked, userSelf, accountTypes(std::move(accountTypes))](
226958345856SAbhishek Patel             const boost::system::error_code ec, sdbusplus::message_t& m) {
227084e12cb7SAppaRao Puli         if (ec)
227184e12cb7SAppaRao Puli         {
2272002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2273002d39b4SEd Tanous                                     username);
2274a840879dSEd Tanous             return;
2275a840879dSEd Tanous         }
2276a840879dSEd Tanous 
2277002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
227858345856SAbhishek Patel                              locked, accountTypes, userSelf);
227984e12cb7SAppaRao Puli         },
22801ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
228184e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
228284e12cb7SAppaRao Puli         *newUserName);
22831ef4c342SEd Tanous }
22841ef4c342SEd Tanous 
22851ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
22861ef4c342SEd Tanous {
22871ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22884c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
22894c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
22904c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
22914c7d4d33SEd Tanous 
22924c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22931ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
22941ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
22951ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
22961ef4c342SEd Tanous 
22971ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22981ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
22991ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
23001ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
23011ef4c342SEd Tanous 
23021ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
23034c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
23044c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
23054c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
23064c7d4d33SEd Tanous 
23074c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
23081ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
23091ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
23101ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
23111ef4c342SEd Tanous 
23121ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
23131ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
23141ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
23151ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
23161ef4c342SEd Tanous 
23171ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23184c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
23194c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
23204c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
23214c7d4d33SEd Tanous 
23224c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23231ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
23241ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
23251ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
23261ef4c342SEd Tanous 
23271ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23281ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
23291ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
23301ef4c342SEd Tanous         // yet
23311ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
23321ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
23331ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
233484e12cb7SAppaRao Puli 
23356c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2336ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
23376c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
233820fc307fSGunnar Mills             std::bind_front(handleAccountDelete, std::ref(app)));
233906e086d9SEd Tanous }
234088d16c9aSLewanczyk, Dawid 
234188d16c9aSLewanczyk, Dawid } // namespace redfish
2342