xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 25b54dba775b31021a3a4677eb79e9771bcb97f7)
188d16c9aSLewanczyk, Dawid /*
288d16c9aSLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
388d16c9aSLewanczyk, Dawid //
488d16c9aSLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
588d16c9aSLewanczyk, Dawid // you may not use this file except in compliance with the License.
688d16c9aSLewanczyk, Dawid // You may obtain a copy of the License at
788d16c9aSLewanczyk, Dawid //
888d16c9aSLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
988d16c9aSLewanczyk, Dawid //
1088d16c9aSLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1188d16c9aSLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1288d16c9aSLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388d16c9aSLewanczyk, Dawid // See the License for the specific language governing permissions and
1488d16c9aSLewanczyk, Dawid // limitations under the License.
1588d16c9aSLewanczyk, Dawid */
1688d16c9aSLewanczyk, Dawid #pragma once
1788d16c9aSLewanczyk, Dawid 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_utility.hpp"
203ccb3adbSEd Tanous #include "error_messages.hpp"
210ec8b83dSEd Tanous #include "generated/enums/account_service.hpp"
223ccb3adbSEd Tanous #include "openbmc_dbus_rest.hpp"
233ccb3adbSEd Tanous #include "persistent_data.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
250ec8b83dSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
280ec8b83dSEd Tanous 
291e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
30d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
311214b7e7SGunnar Mills 
322b73119cSGeorge Liu #include <array>
33c7229815SAbhishek Patel #include <optional>
343544d2a7SEd Tanous #include <ranges>
35c7229815SAbhishek Patel #include <string>
362b73119cSGeorge Liu #include <string_view>
37c7229815SAbhishek Patel #include <vector>
38c7229815SAbhishek Patel 
391abe55efSEd Tanous namespace redfish
401abe55efSEd Tanous {
4188d16c9aSLewanczyk, Dawid 
4223a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
436973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
442c70f800SEd Tanous constexpr const char* adConfigObject =
45ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
46ab828d7cSRatan Gupta 
47b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
486973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
496973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
506973a582SRatan Gupta constexpr const char* ldapConfigInterface =
516973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
526973a582SRatan Gupta constexpr const char* ldapCreateInterface =
536973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
546973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
5506785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
5606785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
576973a582SRatan Gupta 
5854fc587aSNagaraju Goruganti struct LDAPRoleMapData
5954fc587aSNagaraju Goruganti {
6054fc587aSNagaraju Goruganti     std::string groupName;
6154fc587aSNagaraju Goruganti     std::string privilege;
6254fc587aSNagaraju Goruganti };
6354fc587aSNagaraju Goruganti 
646973a582SRatan Gupta struct LDAPConfigData
656973a582SRatan Gupta {
6647f2934cSEd Tanous     std::string uri;
6747f2934cSEd Tanous     std::string bindDN;
6847f2934cSEd Tanous     std::string baseDN;
6947f2934cSEd Tanous     std::string searchScope;
7047f2934cSEd Tanous     std::string serverType;
716973a582SRatan Gupta     bool serviceEnabled = false;
7247f2934cSEd Tanous     std::string userNameAttribute;
7347f2934cSEd Tanous     std::string groupAttribute;
7454fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
756973a582SRatan Gupta };
766973a582SRatan Gupta 
7754fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7884e12cb7SAppaRao Puli {
7984e12cb7SAppaRao Puli     if (role == "priv-admin")
8084e12cb7SAppaRao Puli     {
8184e12cb7SAppaRao Puli         return "Administrator";
8284e12cb7SAppaRao Puli     }
833174e4dfSEd Tanous     if (role == "priv-user")
8484e12cb7SAppaRao Puli     {
85c80fee55SAppaRao Puli         return "ReadOnly";
8684e12cb7SAppaRao Puli     }
873174e4dfSEd Tanous     if (role == "priv-operator")
8884e12cb7SAppaRao Puli     {
8984e12cb7SAppaRao Puli         return "Operator";
9084e12cb7SAppaRao Puli     }
9184e12cb7SAppaRao Puli     return "";
9284e12cb7SAppaRao Puli }
9354fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9484e12cb7SAppaRao Puli {
9584e12cb7SAppaRao Puli     if (role == "Administrator")
9684e12cb7SAppaRao Puli     {
9784e12cb7SAppaRao Puli         return "priv-admin";
9884e12cb7SAppaRao Puli     }
993174e4dfSEd Tanous     if (role == "ReadOnly")
10084e12cb7SAppaRao Puli     {
10184e12cb7SAppaRao Puli         return "priv-user";
10284e12cb7SAppaRao Puli     }
1033174e4dfSEd Tanous     if (role == "Operator")
10484e12cb7SAppaRao Puli     {
10584e12cb7SAppaRao Puli         return "priv-operator";
10684e12cb7SAppaRao Puli     }
10784e12cb7SAppaRao Puli     return "";
10884e12cb7SAppaRao Puli }
109b9b2e0b2SEd Tanous 
110c7229815SAbhishek Patel /**
111c7229815SAbhishek Patel  * @brief Maps user group names retrieved from D-Bus object to
112c7229815SAbhishek Patel  * Account Types.
113c7229815SAbhishek Patel  *
114c7229815SAbhishek Patel  * @param[in] userGroups List of User groups
115c7229815SAbhishek Patel  * @param[out] res AccountTypes populated
116c7229815SAbhishek Patel  *
117c7229815SAbhishek Patel  * @return true in case of success, false if UserGroups contains
118c7229815SAbhishek Patel  * invalid group name(s).
119c7229815SAbhishek Patel  */
120c7229815SAbhishek Patel inline bool translateUserGroup(const std::vector<std::string>& userGroups,
121c7229815SAbhishek Patel                                crow::Response& res)
122c7229815SAbhishek Patel {
123c7229815SAbhishek Patel     std::vector<std::string> accountTypes;
124c7229815SAbhishek Patel     for (const auto& userGroup : userGroups)
125c7229815SAbhishek Patel     {
126c7229815SAbhishek Patel         if (userGroup == "redfish")
127c7229815SAbhishek Patel         {
128c7229815SAbhishek Patel             accountTypes.emplace_back("Redfish");
129c7229815SAbhishek Patel             accountTypes.emplace_back("WebUI");
130c7229815SAbhishek Patel         }
131c7229815SAbhishek Patel         else if (userGroup == "ipmi")
132c7229815SAbhishek Patel         {
133c7229815SAbhishek Patel             accountTypes.emplace_back("IPMI");
134c7229815SAbhishek Patel         }
135c7229815SAbhishek Patel         else if (userGroup == "ssh")
136c7229815SAbhishek Patel         {
137c7229815SAbhishek Patel             accountTypes.emplace_back("ManagerConsole");
138c7229815SAbhishek Patel         }
1393e72c202SNinad Palsule         else if (userGroup == "hostconsole")
1403e72c202SNinad Palsule         {
1413e72c202SNinad Palsule             // The hostconsole group controls who can access the host console
1423e72c202SNinad Palsule             // port via ssh and websocket.
1433e72c202SNinad Palsule             accountTypes.emplace_back("HostConsole");
1443e72c202SNinad Palsule         }
145c7229815SAbhishek Patel         else if (userGroup == "web")
146c7229815SAbhishek Patel         {
147c7229815SAbhishek Patel             // 'web' is one of the valid groups in the UserGroups property of
148c7229815SAbhishek Patel             // the user account in the D-Bus object. This group is currently not
149c7229815SAbhishek Patel             // doing anything, and is considered to be equivalent to 'redfish'.
150c7229815SAbhishek Patel             // 'redfish' user group is mapped to 'Redfish'and 'WebUI'
151c7229815SAbhishek Patel             // AccountTypes, so do nothing here...
152c7229815SAbhishek Patel         }
153c7229815SAbhishek Patel         else
154c7229815SAbhishek Patel         {
1558ece0e45SEd Tanous             // Invalid user group name. Caller throws an exception.
156c7229815SAbhishek Patel             return false;
157c7229815SAbhishek Patel         }
158c7229815SAbhishek Patel     }
159c7229815SAbhishek Patel 
160c7229815SAbhishek Patel     res.jsonValue["AccountTypes"] = std::move(accountTypes);
161c7229815SAbhishek Patel     return true;
162c7229815SAbhishek Patel }
163c7229815SAbhishek Patel 
16458345856SAbhishek Patel /**
16558345856SAbhishek Patel  * @brief Builds User Groups from the Account Types
16658345856SAbhishek Patel  *
16758345856SAbhishek Patel  * @param[in] asyncResp Async Response
16858345856SAbhishek Patel  * @param[in] accountTypes List of Account Types
16958345856SAbhishek Patel  * @param[out] userGroups List of User Groups mapped from Account Types
17058345856SAbhishek Patel  *
17158345856SAbhishek Patel  * @return true if Account Types mapped to User Groups, false otherwise.
17258345856SAbhishek Patel  */
17358345856SAbhishek Patel inline bool
17458345856SAbhishek Patel     getUserGroupFromAccountType(crow::Response& res,
17558345856SAbhishek Patel                                 const std::vector<std::string>& accountTypes,
17658345856SAbhishek Patel                                 std::vector<std::string>& userGroups)
17758345856SAbhishek Patel {
17858345856SAbhishek Patel     // Need both Redfish and WebUI Account Types to map to 'redfish' User Group
17958345856SAbhishek Patel     bool redfishType = false;
18058345856SAbhishek Patel     bool webUIType = false;
18158345856SAbhishek Patel 
18258345856SAbhishek Patel     for (const auto& accountType : accountTypes)
18358345856SAbhishek Patel     {
18458345856SAbhishek Patel         if (accountType == "Redfish")
18558345856SAbhishek Patel         {
18658345856SAbhishek Patel             redfishType = true;
18758345856SAbhishek Patel         }
18858345856SAbhishek Patel         else if (accountType == "WebUI")
18958345856SAbhishek Patel         {
19058345856SAbhishek Patel             webUIType = true;
19158345856SAbhishek Patel         }
19258345856SAbhishek Patel         else if (accountType == "IPMI")
19358345856SAbhishek Patel         {
19458345856SAbhishek Patel             userGroups.emplace_back("ipmi");
19558345856SAbhishek Patel         }
19658345856SAbhishek Patel         else if (accountType == "HostConsole")
19758345856SAbhishek Patel         {
19858345856SAbhishek Patel             userGroups.emplace_back("hostconsole");
19958345856SAbhishek Patel         }
20058345856SAbhishek Patel         else if (accountType == "ManagerConsole")
20158345856SAbhishek Patel         {
20258345856SAbhishek Patel             userGroups.emplace_back("ssh");
20358345856SAbhishek Patel         }
20458345856SAbhishek Patel         else
20558345856SAbhishek Patel         {
20658345856SAbhishek Patel             // Invalid Account Type
20758345856SAbhishek Patel             messages::propertyValueNotInList(res, "AccountTypes", accountType);
20858345856SAbhishek Patel             return false;
20958345856SAbhishek Patel         }
21058345856SAbhishek Patel     }
21158345856SAbhishek Patel 
21258345856SAbhishek Patel     // Both  Redfish and WebUI Account Types are needed to PATCH
21358345856SAbhishek Patel     if (redfishType ^ webUIType)
21458345856SAbhishek Patel     {
21562598e31SEd Tanous         BMCWEB_LOG_ERROR(
21662598e31SEd Tanous             "Missing Redfish or WebUI Account Type to set redfish User Group");
21758345856SAbhishek Patel         messages::strictAccountTypes(res, "AccountTypes");
21858345856SAbhishek Patel         return false;
21958345856SAbhishek Patel     }
22058345856SAbhishek Patel 
22158345856SAbhishek Patel     if (redfishType && webUIType)
22258345856SAbhishek Patel     {
22358345856SAbhishek Patel         userGroups.emplace_back("redfish");
22458345856SAbhishek Patel     }
22558345856SAbhishek Patel 
22658345856SAbhishek Patel     return true;
22758345856SAbhishek Patel }
22858345856SAbhishek Patel 
22958345856SAbhishek Patel /**
23058345856SAbhishek Patel  * @brief Sets UserGroups property of the user based on the Account Types
23158345856SAbhishek Patel  *
23258345856SAbhishek Patel  * @param[in] accountTypes List of User Account Types
23358345856SAbhishek Patel  * @param[in] asyncResp Async Response
23458345856SAbhishek Patel  * @param[in] dbusObjectPath D-Bus Object Path
23558345856SAbhishek Patel  * @param[in] userSelf true if User is updating OWN Account Types
23658345856SAbhishek Patel  */
23758345856SAbhishek Patel inline void
23858345856SAbhishek Patel     patchAccountTypes(const std::vector<std::string>& accountTypes,
23958345856SAbhishek Patel                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24058345856SAbhishek Patel                       const std::string& dbusObjectPath, bool userSelf)
24158345856SAbhishek Patel {
24258345856SAbhishek Patel     // Check if User is disabling own Redfish Account Type
24358345856SAbhishek Patel     if (userSelf &&
24458345856SAbhishek Patel         (accountTypes.cend() ==
24558345856SAbhishek Patel          std::find(accountTypes.cbegin(), accountTypes.cend(), "Redfish")))
24658345856SAbhishek Patel     {
24762598e31SEd Tanous         BMCWEB_LOG_ERROR(
24862598e31SEd Tanous             "User disabling OWN Redfish Account Type is not allowed");
24958345856SAbhishek Patel         messages::strictAccountTypes(asyncResp->res, "AccountTypes");
25058345856SAbhishek Patel         return;
25158345856SAbhishek Patel     }
25258345856SAbhishek Patel 
25358345856SAbhishek Patel     std::vector<std::string> updatedUserGroups;
25458345856SAbhishek Patel     if (!getUserGroupFromAccountType(asyncResp->res, accountTypes,
25558345856SAbhishek Patel                                      updatedUserGroups))
25658345856SAbhishek Patel     {
25758345856SAbhishek Patel         // Problem in mapping Account Types to User Groups, Error already
25858345856SAbhishek Patel         // logged.
25958345856SAbhishek Patel         return;
26058345856SAbhishek Patel     }
261d02aad39SEd Tanous     setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
262d02aad39SEd Tanous                     dbusObjectPath, "xyz.openbmc_project.User.Attributes",
263d02aad39SEd Tanous                     "UserGroups", "AccountTypes", updatedUserGroups);
26458345856SAbhishek Patel }
26558345856SAbhishek Patel 
2668d1b46d7Szhanghch05 inline void userErrorMessageHandler(
2678d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2688d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
26966b5ca76Sjayaprakash Mutyala {
27066b5ca76Sjayaprakash Mutyala     if (e == nullptr)
27166b5ca76Sjayaprakash Mutyala     {
27266b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
27366b5ca76Sjayaprakash Mutyala         return;
27466b5ca76Sjayaprakash Mutyala     }
27566b5ca76Sjayaprakash Mutyala 
276055806b3SManojkiran Eda     const char* errorMessage = e->name;
27766b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
27866b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
27966b5ca76Sjayaprakash Mutyala     {
280d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
28166b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
28266b5ca76Sjayaprakash Mutyala     }
28366b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
28466b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
28566b5ca76Sjayaprakash Mutyala     {
286d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
28766b5ca76Sjayaprakash Mutyala     }
288d4d25793SEd Tanous     else if ((strcmp(errorMessage,
289d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
290d4d25793SEd Tanous               0) ||
2910fda0f12SGeorge Liu              (strcmp(
2920fda0f12SGeorge Liu                   errorMessage,
2930fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
2940fda0f12SGeorge Liu               0))
29566b5ca76Sjayaprakash Mutyala     {
29666b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
29766b5ca76Sjayaprakash Mutyala     }
29866b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
29966b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
30066b5ca76Sjayaprakash Mutyala     {
30166b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
30266b5ca76Sjayaprakash Mutyala     }
30366b5ca76Sjayaprakash Mutyala     else
30466b5ca76Sjayaprakash Mutyala     {
305b8ad583fSGunnar Mills         BMCWEB_LOG_ERROR("DBUS response error {}", errorMessage);
30666b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
30766b5ca76Sjayaprakash Mutyala     }
30866b5ca76Sjayaprakash Mutyala }
30966b5ca76Sjayaprakash Mutyala 
31081ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
311ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
312ab828d7cSRatan Gupta                                 const std::string& ldapType)
3136973a582SRatan Gupta {
31449cc263fSEd Tanous     nlohmann::json::object_t ldap;
3151476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
31649cc263fSEd Tanous     nlohmann::json::array_t serviceAddresses;
31749cc263fSEd Tanous     serviceAddresses.emplace_back(confData.uri);
31849cc263fSEd Tanous     ldap["ServiceAddresses"] = std::move(serviceAddresses);
31949cc263fSEd Tanous 
32049cc263fSEd Tanous     nlohmann::json::object_t authentication;
32149cc263fSEd Tanous     authentication["AuthenticationType"] =
3220ec8b83dSEd Tanous         account_service::AuthenticationTypes::UsernameAndPassword;
32349cc263fSEd Tanous     authentication["Username"] = confData.bindDN;
32449cc263fSEd Tanous     authentication["Password"] = nullptr;
32549cc263fSEd Tanous     ldap["Authentication"] = std::move(authentication);
3261476687dSEd Tanous 
32749cc263fSEd Tanous     nlohmann::json::object_t ldapService;
32849cc263fSEd Tanous     nlohmann::json::object_t searchSettings;
32949cc263fSEd Tanous     nlohmann::json::array_t baseDistinguishedNames;
33049cc263fSEd Tanous     baseDistinguishedNames.emplace_back(confData.baseDN);
3311476687dSEd Tanous 
33249cc263fSEd Tanous     searchSettings["BaseDistinguishedNames"] =
33349cc263fSEd Tanous         std::move(baseDistinguishedNames);
33449cc263fSEd Tanous     searchSettings["UsernameAttribute"] = confData.userNameAttribute;
33549cc263fSEd Tanous     searchSettings["GroupsAttribute"] = confData.groupAttribute;
33649cc263fSEd Tanous     ldapService["SearchSettings"] = std::move(searchSettings);
33749cc263fSEd Tanous     ldap["LDAPService"] = std::move(ldapService);
33849cc263fSEd Tanous 
33949cc263fSEd Tanous     nlohmann::json::array_t roleMapArray;
3409eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
34154fc587aSNagaraju Goruganti     {
34262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Pushing the data groupName={}", obj.second.groupName);
343613dabeaSEd Tanous 
344613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
345613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
346329f0348SJorge Cisneros         remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
347329f0348SJorge Cisneros         roleMapArray.emplace_back(std::move(remoteGroup));
34854fc587aSNagaraju Goruganti     }
34949cc263fSEd Tanous 
35049cc263fSEd Tanous     ldap["RemoteRoleMapping"] = std::move(roleMapArray);
35149cc263fSEd Tanous 
35249cc263fSEd Tanous     jsonResponse[ldapType].update(ldap);
3536973a582SRatan Gupta }
3546973a582SRatan Gupta 
3556973a582SRatan Gupta /**
35606785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
35706785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
35806785244SRatan Gupta  *
35906785244SRatan Gupta  */
36023a21a1cSEd Tanous inline void handleRoleMapPatch(
3618d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36206785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
363c1019828SEd Tanous     const std::string& serverType,
364c1019828SEd Tanous     std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input)
36506785244SRatan Gupta {
36606785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
36706785244SRatan Gupta     {
368c1019828SEd Tanous         std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson =
369c1019828SEd Tanous             input[index];
370c1019828SEd Tanous         nlohmann::json::object_t* obj =
371c1019828SEd Tanous             std::get_if<nlohmann::json::object_t>(&thisJson);
372c1019828SEd Tanous         if (obj == nullptr)
37306785244SRatan Gupta         {
37406785244SRatan Gupta             // delete the existing object
37506785244SRatan Gupta             if (index < roleMapObjData.size())
37606785244SRatan Gupta             {
37706785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
37806785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
3795e7e2dc5SEd Tanous                      index](const boost::system::error_code& ec) {
38006785244SRatan Gupta                     if (ec)
38106785244SRatan Gupta                     {
38262598e31SEd Tanous                         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
38306785244SRatan Gupta                         messages::internalError(asyncResp->res);
38406785244SRatan Gupta                         return;
38506785244SRatan Gupta                     }
38689492a15SPatrick Williams                     asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"]
38789492a15SPatrick Williams                                             [index] = nullptr;
38806785244SRatan Gupta                 },
38906785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
39006785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
39106785244SRatan Gupta             }
39206785244SRatan Gupta             else
39306785244SRatan Gupta             {
39462598e31SEd Tanous                 BMCWEB_LOG_ERROR("Can't delete the object");
395c1019828SEd Tanous                 messages::propertyValueTypeError(asyncResp->res, "null",
3962e8c4bdaSEd Tanous                                                  "RemoteRoleMapping/" +
3972e8c4bdaSEd Tanous                                                      std::to_string(index));
39806785244SRatan Gupta                 return;
39906785244SRatan Gupta             }
40006785244SRatan Gupta         }
401c1019828SEd Tanous         else if (obj->empty())
40206785244SRatan Gupta         {
40306785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
40406785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
40506785244SRatan Gupta         }
40606785244SRatan Gupta         else
40706785244SRatan Gupta         {
40806785244SRatan Gupta             // update/create the object
40906785244SRatan Gupta             std::optional<std::string> remoteGroup;
41006785244SRatan Gupta             std::optional<std::string> localRole;
41106785244SRatan Gupta 
412c1019828SEd Tanous             if (!json_util::readJsonObject(*obj, asyncResp->res, "RemoteGroup",
413c1019828SEd Tanous                                            remoteGroup, "LocalRole", localRole))
41406785244SRatan Gupta             {
41506785244SRatan Gupta                 continue;
41606785244SRatan Gupta             }
41706785244SRatan Gupta 
41806785244SRatan Gupta             // Update existing RoleMapping Object
41906785244SRatan Gupta             if (index < roleMapObjData.size())
42006785244SRatan Gupta             {
42162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Update Role Map Object");
42206785244SRatan Gupta                 // If "RemoteGroup" info is provided
42306785244SRatan Gupta                 if (remoteGroup)
42406785244SRatan Gupta                 {
425d02aad39SEd Tanous                     setDbusProperty(
426d02aad39SEd Tanous                         asyncResp, ldapDbusService, roleMapObjData[index].first,
4279ae226faSGeorge Liu                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
428d02aad39SEd Tanous                         "GroupName",
429d02aad39SEd Tanous                         std::format("RemoteRoleMapping/{}/RemoteGroup", index),
43025e055a3SRavi Teja                         *remoteGroup);
43106785244SRatan Gupta                 }
43206785244SRatan Gupta 
43306785244SRatan Gupta                 // If "LocalRole" info is provided
43406785244SRatan Gupta                 if (localRole)
43506785244SRatan Gupta                 {
436d02aad39SEd Tanous                     setDbusProperty(
437d02aad39SEd Tanous                         asyncResp, ldapDbusService, roleMapObjData[index].first,
4389ae226faSGeorge Liu                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
439d02aad39SEd Tanous                         "Privilege",
440d02aad39SEd Tanous                         std::format("RemoteRoleMapping/{}/LocalRole", index),
441d02aad39SEd Tanous                         *localRole);
44206785244SRatan Gupta                 }
44306785244SRatan Gupta             }
44406785244SRatan Gupta             // Create a new RoleMapping Object.
44506785244SRatan Gupta             else
44606785244SRatan Gupta             {
44762598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
44862598e31SEd Tanous                     "setRoleMappingProperties: Creating new Object");
44989492a15SPatrick Williams                 std::string pathString = "RemoteRoleMapping/" +
45089492a15SPatrick Williams                                          std::to_string(index);
45106785244SRatan Gupta 
45206785244SRatan Gupta                 if (!localRole)
45306785244SRatan Gupta                 {
45406785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
45506785244SRatan Gupta                                               pathString + "/LocalRole");
45606785244SRatan Gupta                     continue;
45706785244SRatan Gupta                 }
45806785244SRatan Gupta                 if (!remoteGroup)
45906785244SRatan Gupta                 {
46006785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
46106785244SRatan Gupta                                               pathString + "/RemoteGroup");
46206785244SRatan Gupta                     continue;
46306785244SRatan Gupta                 }
46406785244SRatan Gupta 
46506785244SRatan Gupta                 std::string dbusObjectPath;
46606785244SRatan Gupta                 if (serverType == "ActiveDirectory")
46706785244SRatan Gupta                 {
4682c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
46906785244SRatan Gupta                 }
47006785244SRatan Gupta                 else if (serverType == "LDAP")
47106785244SRatan Gupta                 {
47223a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
47306785244SRatan Gupta                 }
47406785244SRatan Gupta 
47562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Remote Group={},LocalRole={}", *remoteGroup,
47662598e31SEd Tanous                                  *localRole);
47706785244SRatan Gupta 
47806785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
479271584abSEd Tanous                     [asyncResp, serverType, localRole,
4805e7e2dc5SEd Tanous                      remoteGroup](const boost::system::error_code& ec) {
48106785244SRatan Gupta                     if (ec)
48206785244SRatan Gupta                     {
48362598e31SEd Tanous                         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
48406785244SRatan Gupta                         messages::internalError(asyncResp->res);
48506785244SRatan Gupta                         return;
48606785244SRatan Gupta                     }
48706785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
48806785244SRatan Gupta                         asyncResp->res
48906785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
4901476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
4911476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
4921476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
493b2ba3072SPatrick Williams                     remoteRoleJson.emplace_back(std::move(roleMapEntry));
49406785244SRatan Gupta                 },
49506785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
4963174e4dfSEd Tanous                     "Create", *remoteGroup,
49706785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
49806785244SRatan Gupta             }
49906785244SRatan Gupta         }
50006785244SRatan Gupta     }
50106785244SRatan Gupta }
50206785244SRatan Gupta 
50306785244SRatan Gupta /**
5046973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
5056973a582SRatan Gupta  * into JSON
5066973a582SRatan Gupta  */
5076973a582SRatan Gupta template <typename CallbackFunc>
5086973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
5096973a582SRatan Gupta                               CallbackFunc&& callback)
5106973a582SRatan Gupta {
5112b73119cSGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
5122b73119cSGeorge Liu         ldapEnableInterface, ldapConfigInterface};
51354fc587aSNagaraju Goruganti 
5142b73119cSGeorge Liu     dbus::utility::getDbusObject(
5152b73119cSGeorge Liu         ldapConfigObjectName, interfaces,
5168cb2c024SEd Tanous         [callback = std::forward<CallbackFunc>(callback),
517c1019828SEd Tanous          ldapType](const boost::system::error_code& ec,
518c1019828SEd Tanous                    const dbus::utility::MapperGetObject& resp) mutable {
51954fc587aSNagaraju Goruganti         if (ec || resp.empty())
52054fc587aSNagaraju Goruganti         {
521bf2ddedeSCarson Labrado             BMCWEB_LOG_WARNING(
52262598e31SEd Tanous                 "DBUS response error during getting of service name: {}", ec);
52323a21a1cSEd Tanous             LDAPConfigData empty{};
52423a21a1cSEd Tanous             callback(false, empty, ldapType);
52554fc587aSNagaraju Goruganti             return;
52654fc587aSNagaraju Goruganti         }
52754fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
5285eb468daSGeorge Liu         sdbusplus::message::object_path path(ldapRootObject);
5295eb468daSGeorge Liu         dbus::utility::getManagedObjects(
5305eb468daSGeorge Liu             service, path,
531c1019828SEd Tanous             [callback, ldapType](
532c1019828SEd Tanous                 const boost::system::error_code& ec2,
533c1019828SEd Tanous                 const dbus::utility::ManagedObjectType& ldapObjects) mutable {
5346973a582SRatan Gupta             LDAPConfigData confData{};
5358b24275dSEd Tanous             if (ec2)
5366973a582SRatan Gupta             {
537ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
538bf2ddedeSCarson Labrado                 BMCWEB_LOG_WARNING("D-Bus responses error: {}", ec2);
5396973a582SRatan Gupta                 return;
5406973a582SRatan Gupta             }
541ab828d7cSRatan Gupta 
542ab828d7cSRatan Gupta             std::string ldapDbusType;
54354fc587aSNagaraju Goruganti             std::string searchString;
54454fc587aSNagaraju Goruganti 
545ab828d7cSRatan Gupta             if (ldapType == "LDAP")
546ab828d7cSRatan Gupta             {
5470fda0f12SGeorge Liu                 ldapDbusType =
5480fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
54954fc587aSNagaraju Goruganti                 searchString = "openldap";
550ab828d7cSRatan Gupta             }
551ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
552ab828d7cSRatan Gupta             {
55354fc587aSNagaraju Goruganti                 ldapDbusType =
5540fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
55554fc587aSNagaraju Goruganti                 searchString = "active_directory";
556ab828d7cSRatan Gupta             }
557ab828d7cSRatan Gupta             else
558ab828d7cSRatan Gupta             {
55962598e31SEd Tanous                 BMCWEB_LOG_ERROR("Can't get the DbusType for the given type={}",
56062598e31SEd Tanous                                  ldapType);
561ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
562ab828d7cSRatan Gupta                 return;
563ab828d7cSRatan Gupta             }
564ab828d7cSRatan Gupta 
565ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
566ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
567ab828d7cSRatan Gupta 
5686973a582SRatan Gupta             for (const auto& object : ldapObjects)
5696973a582SRatan Gupta             {
57054fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
57154fc587aSNagaraju Goruganti                 // given type
572002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
5736973a582SRatan Gupta                 {
574ab828d7cSRatan Gupta                     continue;
575ab828d7cSRatan Gupta                 }
576ab828d7cSRatan Gupta 
5776973a582SRatan Gupta                 for (const auto& interface : object.second)
5786973a582SRatan Gupta                 {
5796973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
5806973a582SRatan Gupta                     {
5816973a582SRatan Gupta                         // rest of the properties are string.
5826973a582SRatan Gupta                         for (const auto& property : interface.second)
5836973a582SRatan Gupta                         {
5846973a582SRatan Gupta                             if (property.first == "Enabled")
5856973a582SRatan Gupta                             {
5866973a582SRatan Gupta                                 const bool* value =
5876973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
5886973a582SRatan Gupta                                 if (value == nullptr)
5896973a582SRatan Gupta                                 {
5906973a582SRatan Gupta                                     continue;
5916973a582SRatan Gupta                                 }
5926973a582SRatan Gupta                                 confData.serviceEnabled = *value;
5936973a582SRatan Gupta                                 break;
5946973a582SRatan Gupta                             }
5956973a582SRatan Gupta                         }
5966973a582SRatan Gupta                     }
5976973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
5986973a582SRatan Gupta                     {
5996973a582SRatan Gupta                         for (const auto& property : interface.second)
6006973a582SRatan Gupta                         {
601271584abSEd Tanous                             const std::string* strValue =
602002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
603271584abSEd Tanous                             if (strValue == nullptr)
6046973a582SRatan Gupta                             {
6056973a582SRatan Gupta                                 continue;
6066973a582SRatan Gupta                             }
6076973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
6086973a582SRatan Gupta                             {
609271584abSEd Tanous                                 confData.uri = *strValue;
6106973a582SRatan Gupta                             }
6116973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
6126973a582SRatan Gupta                             {
613271584abSEd Tanous                                 confData.bindDN = *strValue;
6146973a582SRatan Gupta                             }
6156973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
6166973a582SRatan Gupta                             {
617271584abSEd Tanous                                 confData.baseDN = *strValue;
6186973a582SRatan Gupta                             }
619002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
6206973a582SRatan Gupta                             {
621271584abSEd Tanous                                 confData.searchScope = *strValue;
6226973a582SRatan Gupta                             }
623002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
6246973a582SRatan Gupta                             {
625271584abSEd Tanous                                 confData.groupAttribute = *strValue;
6266973a582SRatan Gupta                             }
627002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
6286973a582SRatan Gupta                             {
629271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
6306973a582SRatan Gupta                             }
63154fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
632ab828d7cSRatan Gupta                             {
633271584abSEd Tanous                                 confData.serverType = *strValue;
63454fc587aSNagaraju Goruganti                             }
63554fc587aSNagaraju Goruganti                         }
63654fc587aSNagaraju Goruganti                     }
637002d39b4SEd Tanous                     else if (interface.first ==
6380fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
63954fc587aSNagaraju Goruganti                     {
64054fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
64154fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
64254fc587aSNagaraju Goruganti                         {
643271584abSEd Tanous                             const std::string* strValue =
644002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
64554fc587aSNagaraju Goruganti 
646271584abSEd Tanous                             if (strValue == nullptr)
64754fc587aSNagaraju Goruganti                             {
64854fc587aSNagaraju Goruganti                                 continue;
64954fc587aSNagaraju Goruganti                             }
65054fc587aSNagaraju Goruganti 
65154fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
65254fc587aSNagaraju Goruganti                             {
653271584abSEd Tanous                                 roleMapData.groupName = *strValue;
65454fc587aSNagaraju Goruganti                             }
65554fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
65654fc587aSNagaraju Goruganti                             {
657271584abSEd Tanous                                 roleMapData.privilege = *strValue;
65854fc587aSNagaraju Goruganti                             }
65954fc587aSNagaraju Goruganti                         }
66054fc587aSNagaraju Goruganti 
661002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
662002d39b4SEd Tanous                                                             roleMapData);
66354fc587aSNagaraju Goruganti                     }
66454fc587aSNagaraju Goruganti                 }
66554fc587aSNagaraju Goruganti             }
666ab828d7cSRatan Gupta             callback(true, confData, ldapType);
6675eb468daSGeorge Liu         });
6682b73119cSGeorge Liu     });
6696973a582SRatan Gupta }
6706973a582SRatan Gupta 
6718a07d286SRatan Gupta /**
6728a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6738a07d286SRatan Gupta           json response with the new value.
6748a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6758a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6768a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6778a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6788a07d286SRatan Gupta  */
6798a07d286SRatan Gupta 
6804f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6818a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6828d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6838a07d286SRatan Gupta     const std::string& ldapServerElementName,
6848a07d286SRatan Gupta     const std::string& ldapConfigObject)
6858a07d286SRatan Gupta {
686d02aad39SEd Tanous     setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
687d02aad39SEd Tanous                     ldapConfigInterface, "LDAPServerURI",
688d02aad39SEd Tanous                     ldapServerElementName + "/ServiceAddress",
68925e055a3SRavi Teja                     serviceAddressList.front());
6908a07d286SRatan Gupta }
6918a07d286SRatan Gupta /**
6928a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
6938a07d286SRatan Gupta           json response with the new value.
6948a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
6958a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6968a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6978a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6988a07d286SRatan Gupta  */
6998a07d286SRatan Gupta 
7004f48d5f6SEd Tanous inline void
7014f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
7028d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7038a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7048a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7058a07d286SRatan Gupta {
706d02aad39SEd Tanous     setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
707d02aad39SEd Tanous                     ldapConfigInterface, "LDAPBindDN",
708d02aad39SEd Tanous                     ldapServerElementName + "/Authentication/Username",
709d02aad39SEd Tanous                     username);
7108a07d286SRatan Gupta }
7118a07d286SRatan Gupta 
7128a07d286SRatan Gupta /**
7138a07d286SRatan Gupta  * @brief updates the LDAP password
7148a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
7158a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7168a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7178a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
7188a07d286SRatan Gupta  */
7198a07d286SRatan Gupta 
7204f48d5f6SEd Tanous inline void
7214f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
7228d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7238a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7248a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7258a07d286SRatan Gupta {
726d02aad39SEd Tanous     setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
727d02aad39SEd Tanous                     ldapConfigInterface, "LDAPBindDNPassword",
728d02aad39SEd Tanous                     ldapServerElementName + "/Authentication/Password",
729d02aad39SEd Tanous                     password);
7308a07d286SRatan Gupta }
7318a07d286SRatan Gupta 
7328a07d286SRatan Gupta /**
7338a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7348a07d286SRatan Gupta           json response with the new value.
7358a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7368a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7378a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7388a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7398a07d286SRatan Gupta  */
7408a07d286SRatan Gupta 
7414f48d5f6SEd Tanous inline void
7424f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7438d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7448a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7458a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7468a07d286SRatan Gupta {
747d02aad39SEd Tanous     setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
748d02aad39SEd Tanous                     ldapConfigInterface, "LDAPBaseDN",
749d02aad39SEd Tanous                     ldapServerElementName +
750d02aad39SEd Tanous                         "/LDAPService/SearchSettings/BaseDistinguishedNames",
75125e055a3SRavi Teja                     baseDNList.front());
7528a07d286SRatan Gupta }
7538a07d286SRatan Gupta /**
7548a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
7558a07d286SRatan Gupta           json response with the new value.
7568a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
7578a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7588a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7598a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7608a07d286SRatan Gupta  */
7618a07d286SRatan Gupta 
7624f48d5f6SEd Tanous inline void
7634f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
7648d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7658a07d286SRatan Gupta                             const std::string& ldapServerElementName,
7668a07d286SRatan Gupta                             const std::string& ldapConfigObject)
7678a07d286SRatan Gupta {
768d02aad39SEd Tanous     setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
769d02aad39SEd Tanous                     ldapConfigInterface, "UserNameAttribute",
770d02aad39SEd Tanous                     ldapServerElementName +
771d02aad39SEd Tanous                         "LDAPService/SearchSettings/UsernameAttribute",
772d02aad39SEd Tanous                     userNameAttribute);
7738a07d286SRatan Gupta }
7748a07d286SRatan Gupta /**
7758a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
7768a07d286SRatan Gupta           json response with the new value.
7778a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
7788a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7798a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7808a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7818a07d286SRatan Gupta  */
7828a07d286SRatan Gupta 
7834f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
7848d1b46d7Szhanghch05     const std::string& groupsAttribute,
7858d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7868a07d286SRatan Gupta     const std::string& ldapServerElementName,
7878a07d286SRatan Gupta     const std::string& ldapConfigObject)
7888a07d286SRatan Gupta {
789d02aad39SEd Tanous     setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
790d02aad39SEd Tanous                     ldapConfigInterface, "GroupNameAttribute",
791d02aad39SEd Tanous                     ldapServerElementName +
792d02aad39SEd Tanous                         "/LDAPService/SearchSettings/GroupsAttribute",
793d02aad39SEd Tanous                     groupsAttribute);
7948a07d286SRatan Gupta }
7958a07d286SRatan Gupta /**
7968a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
7978a07d286SRatan Gupta           json response with the new value.
7988a07d286SRatan Gupta  * @param input JSON data.
7998a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8008a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8018a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8028a07d286SRatan Gupta  */
8038a07d286SRatan Gupta 
8044f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8056c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8068a07d286SRatan Gupta     const std::string& ldapServerElementName,
8078a07d286SRatan Gupta     const std::string& ldapConfigObject)
8088a07d286SRatan Gupta {
809d02aad39SEd Tanous     setDbusProperty(asyncResp, ldapDbusService, ldapConfigObject,
810d02aad39SEd Tanous                     ldapEnableInterface, "Enabled",
811d02aad39SEd Tanous                     ldapServerElementName + "/ServiceEnabled", serviceEnabled);
8128a07d286SRatan Gupta }
8138a07d286SRatan Gupta 
814c1019828SEd Tanous struct AuthMethods
81578158631SZbigniew Kurzynski {
81678158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
81778158631SZbigniew Kurzynski     std::optional<bool> cookie;
81878158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
81978158631SZbigniew Kurzynski     std::optional<bool> xToken;
820501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
821c1019828SEd Tanous };
82278158631SZbigniew Kurzynski 
823c1019828SEd Tanous inline void
824c1019828SEd Tanous     handleAuthMethodsPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
825c1019828SEd Tanous                            const AuthMethods& auth)
82678158631SZbigniew Kurzynski {
827c1019828SEd Tanous     persistent_data::AuthConfigMethods& authMethodsConfig =
82852cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
82978158631SZbigniew Kurzynski 
830c1019828SEd Tanous     if (auth.basicAuth)
83178158631SZbigniew Kurzynski     {
832*25b54dbaSEd Tanous         if constexpr (!BMCWEB_BASIC_AUTH)
833*25b54dbaSEd Tanous         {
834f16f6263SAlan Kuo             messages::actionNotSupported(
8350fda0f12SGeorge Liu                 asyncResp->res,
8360fda0f12SGeorge Liu                 "Setting BasicAuth when basic-auth feature is disabled");
837f16f6263SAlan Kuo             return;
838*25b54dbaSEd Tanous         }
839*25b54dbaSEd Tanous 
840c1019828SEd Tanous         authMethodsConfig.basic = *auth.basicAuth;
84178158631SZbigniew Kurzynski     }
84278158631SZbigniew Kurzynski 
843c1019828SEd Tanous     if (auth.cookie)
84478158631SZbigniew Kurzynski     {
845*25b54dbaSEd Tanous         if constexpr (!BMCWEB_COOKIE_AUTH)
846*25b54dbaSEd Tanous         {
8470fda0f12SGeorge Liu             messages::actionNotSupported(
8480fda0f12SGeorge Liu                 asyncResp->res,
8490fda0f12SGeorge Liu                 "Setting Cookie when cookie-auth feature is disabled");
850f16f6263SAlan Kuo             return;
851*25b54dbaSEd Tanous         }
852c1019828SEd Tanous         authMethodsConfig.cookie = *auth.cookie;
85378158631SZbigniew Kurzynski     }
85478158631SZbigniew Kurzynski 
855c1019828SEd Tanous     if (auth.sessionToken)
85678158631SZbigniew Kurzynski     {
857*25b54dbaSEd Tanous         if constexpr (!BMCWEB_SESSION_AUTH)
858*25b54dbaSEd Tanous         {
859f16f6263SAlan Kuo             messages::actionNotSupported(
8600fda0f12SGeorge Liu                 asyncResp->res,
8610fda0f12SGeorge Liu                 "Setting SessionToken when session-auth feature is disabled");
862f16f6263SAlan Kuo             return;
863*25b54dbaSEd Tanous         }
864c1019828SEd Tanous         authMethodsConfig.sessionToken = *auth.sessionToken;
86578158631SZbigniew Kurzynski     }
86678158631SZbigniew Kurzynski 
867c1019828SEd Tanous     if (auth.xToken)
86878158631SZbigniew Kurzynski     {
869*25b54dbaSEd Tanous         if constexpr (!BMCWEB_XTOKEN_AUTH)
870*25b54dbaSEd Tanous         {
8710fda0f12SGeorge Liu             messages::actionNotSupported(
8720fda0f12SGeorge Liu                 asyncResp->res,
8730fda0f12SGeorge Liu                 "Setting XToken when xtoken-auth feature is disabled");
874f16f6263SAlan Kuo             return;
875*25b54dbaSEd Tanous         }
876c1019828SEd Tanous         authMethodsConfig.xtoken = *auth.xToken;
87778158631SZbigniew Kurzynski     }
87878158631SZbigniew Kurzynski 
879c1019828SEd Tanous     if (auth.tls)
880501f1e58SZbigniew Kurzynski     {
881*25b54dbaSEd Tanous         if constexpr (!BMCWEB_MUTUAL_TLS_AUTH)
882*25b54dbaSEd Tanous         {
8830fda0f12SGeorge Liu             messages::actionNotSupported(
8840fda0f12SGeorge Liu                 asyncResp->res,
8850fda0f12SGeorge Liu                 "Setting TLS when mutual-tls-auth feature is disabled");
886f16f6263SAlan Kuo             return;
887*25b54dbaSEd Tanous         }
888c1019828SEd Tanous         authMethodsConfig.tls = *auth.tls;
889501f1e58SZbigniew Kurzynski     }
890501f1e58SZbigniew Kurzynski 
89178158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
892501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
893501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
89478158631SZbigniew Kurzynski     {
89578158631SZbigniew Kurzynski         // Do not allow user to disable everything
89678158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
89778158631SZbigniew Kurzynski                                      "of disabling all available methods");
89878158631SZbigniew Kurzynski         return;
89978158631SZbigniew Kurzynski     }
90078158631SZbigniew Kurzynski 
90152cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
90252cc112dSEd Tanous         authMethodsConfig);
90378158631SZbigniew Kurzynski     // Save configuration immediately
90452cc112dSEd Tanous     persistent_data::getConfig().writeData();
90578158631SZbigniew Kurzynski 
90678158631SZbigniew Kurzynski     messages::success(asyncResp->res);
90778158631SZbigniew Kurzynski }
90878158631SZbigniew Kurzynski 
9098a07d286SRatan Gupta /**
9108a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
9118a07d286SRatan Gupta  *        value and create the LDAP config object.
9128a07d286SRatan Gupta  * @param input JSON data
9138a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9148a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
9158a07d286SRatan Gupta  */
9168a07d286SRatan Gupta 
91710cb44f3SEd Tanous struct LdapPatchParams
91810cb44f3SEd Tanous {
91910cb44f3SEd Tanous     std::optional<std::string> authType;
92010cb44f3SEd Tanous     std::optional<std::vector<std::string>> serviceAddressList;
92110cb44f3SEd Tanous     std::optional<bool> serviceEnabled;
92210cb44f3SEd Tanous     std::optional<std::vector<std::string>> baseDNList;
92310cb44f3SEd Tanous     std::optional<std::string> userNameAttribute;
92410cb44f3SEd Tanous     std::optional<std::string> groupsAttribute;
92510cb44f3SEd Tanous     std::optional<std::string> userName;
92610cb44f3SEd Tanous     std::optional<std::string> password;
92710cb44f3SEd Tanous     std::optional<
92810cb44f3SEd Tanous         std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
92910cb44f3SEd Tanous         remoteRoleMapData;
93010cb44f3SEd Tanous };
93110cb44f3SEd Tanous 
93210cb44f3SEd Tanous inline void handleLDAPPatch(LdapPatchParams&& input,
9338d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9348a07d286SRatan Gupta                             const std::string& serverType)
9358a07d286SRatan Gupta {
936eb2bbe56SRatan Gupta     std::string dbusObjectPath;
937eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
938eb2bbe56SRatan Gupta     {
9392c70f800SEd Tanous         dbusObjectPath = adConfigObject;
940eb2bbe56SRatan Gupta     }
941eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
942eb2bbe56SRatan Gupta     {
94323a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
944eb2bbe56SRatan Gupta     }
945cb13a392SEd Tanous     else
946cb13a392SEd Tanous     {
94710cb44f3SEd Tanous         BMCWEB_LOG_ERROR("serverType wasn't AD or LDAP but was {}????",
94810cb44f3SEd Tanous                          serverType);
949cb13a392SEd Tanous         return;
950cb13a392SEd Tanous     }
951eb2bbe56SRatan Gupta 
95210cb44f3SEd Tanous     if (input.authType && *input.authType != "UsernameAndPassword")
9538a07d286SRatan Gupta     {
95410cb44f3SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *input.authType,
955c1019828SEd Tanous                                          "AuthenticationType");
956c1019828SEd Tanous         return;
9578a07d286SRatan Gupta     }
958c1019828SEd Tanous 
95910cb44f3SEd Tanous     if (input.serviceAddressList)
9608a07d286SRatan Gupta     {
96110cb44f3SEd Tanous         if (input.serviceAddressList->empty())
9628a07d286SRatan Gupta         {
963e2616cc5SEd Tanous             messages::propertyValueNotInList(
96410cb44f3SEd Tanous                 asyncResp->res, *input.serviceAddressList, "ServiceAddress");
9658a07d286SRatan Gupta             return;
9668a07d286SRatan Gupta         }
9678a07d286SRatan Gupta     }
96810cb44f3SEd Tanous     if (input.baseDNList)
9698a07d286SRatan Gupta     {
97010cb44f3SEd Tanous         if (input.baseDNList->empty())
9718a07d286SRatan Gupta         {
97210cb44f3SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *input.baseDNList,
9738a07d286SRatan Gupta                                              "BaseDistinguishedNames");
9748a07d286SRatan Gupta             return;
9758a07d286SRatan Gupta         }
9768a07d286SRatan Gupta     }
9778a07d286SRatan Gupta 
9788a07d286SRatan Gupta     // nothing to update, then return
97910cb44f3SEd Tanous     if (!input.userName && !input.password && !input.serviceAddressList &&
98010cb44f3SEd Tanous         !input.baseDNList && !input.userNameAttribute &&
98110cb44f3SEd Tanous         !input.groupsAttribute && !input.serviceEnabled &&
98210cb44f3SEd Tanous         !input.remoteRoleMapData)
9838a07d286SRatan Gupta     {
9848a07d286SRatan Gupta         return;
9858a07d286SRatan Gupta     }
9868a07d286SRatan Gupta 
9878a07d286SRatan Gupta     // Get the existing resource first then keep modifying
9888a07d286SRatan Gupta     // whenever any property gets updated.
98910cb44f3SEd Tanous     getLDAPConfigData(serverType,
99010cb44f3SEd Tanous                       [asyncResp, input = std::move(input),
99110cb44f3SEd Tanous                        dbusObjectPath = std::move(dbusObjectPath)](
99210cb44f3SEd Tanous                           bool success, const LDAPConfigData& confData,
993c1019828SEd Tanous                           const std::string& serverT) mutable {
9948a07d286SRatan Gupta         if (!success)
9958a07d286SRatan Gupta         {
9968a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9978a07d286SRatan Gupta             return;
9988a07d286SRatan Gupta         }
9996c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
10008a07d286SRatan Gupta         if (confData.serviceEnabled)
10018a07d286SRatan Gupta         {
10028a07d286SRatan Gupta             // Disable the service first and update the rest of
10038a07d286SRatan Gupta             // the properties.
10046c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
10058a07d286SRatan Gupta         }
10068a07d286SRatan Gupta 
100710cb44f3SEd Tanous         if (input.serviceAddressList)
10088a07d286SRatan Gupta         {
100910cb44f3SEd Tanous             handleServiceAddressPatch(*input.serviceAddressList, asyncResp,
101010cb44f3SEd Tanous                                       serverT, dbusObjectPath);
101110cb44f3SEd Tanous         }
101210cb44f3SEd Tanous         if (input.userName)
101310cb44f3SEd Tanous         {
101410cb44f3SEd Tanous             handleUserNamePatch(*input.userName, asyncResp, serverT,
10156c51eab1SEd Tanous                                 dbusObjectPath);
10168a07d286SRatan Gupta         }
101710cb44f3SEd Tanous         if (input.password)
10188a07d286SRatan Gupta         {
101910cb44f3SEd Tanous             handlePasswordPatch(*input.password, asyncResp, serverT,
102010cb44f3SEd Tanous                                 dbusObjectPath);
10218a07d286SRatan Gupta         }
10228a07d286SRatan Gupta 
102310cb44f3SEd Tanous         if (input.baseDNList)
10248a07d286SRatan Gupta         {
102510cb44f3SEd Tanous             handleBaseDNPatch(*input.baseDNList, asyncResp, serverT,
10266c51eab1SEd Tanous                               dbusObjectPath);
10278a07d286SRatan Gupta         }
102810cb44f3SEd Tanous         if (input.userNameAttribute)
10298a07d286SRatan Gupta         {
103010cb44f3SEd Tanous             handleUserNameAttrPatch(*input.userNameAttribute, asyncResp,
103110cb44f3SEd Tanous                                     serverT, dbusObjectPath);
103210cb44f3SEd Tanous         }
103310cb44f3SEd Tanous         if (input.groupsAttribute)
103410cb44f3SEd Tanous         {
103510cb44f3SEd Tanous             handleGroupNameAttrPatch(*input.groupsAttribute, asyncResp, serverT,
10366c51eab1SEd Tanous                                      dbusObjectPath);
10378a07d286SRatan Gupta         }
103810cb44f3SEd Tanous         if (input.serviceEnabled)
10398a07d286SRatan Gupta         {
10408a07d286SRatan Gupta             // if user has given the value as true then enable
10418a07d286SRatan Gupta             // the service. if user has given false then no-op
10428a07d286SRatan Gupta             // as service is already stopped.
104310cb44f3SEd Tanous             if (*input.serviceEnabled)
10448a07d286SRatan Gupta             {
104510cb44f3SEd Tanous                 handleServiceEnablePatch(*input.serviceEnabled, asyncResp,
104610cb44f3SEd Tanous                                          serverT, dbusObjectPath);
10478a07d286SRatan Gupta             }
10488a07d286SRatan Gupta         }
10498a07d286SRatan Gupta         else
10508a07d286SRatan Gupta         {
10518a07d286SRatan Gupta             // if user has not given the service enabled value
10528a07d286SRatan Gupta             // then revert it to the same state as it was
10538a07d286SRatan Gupta             // before.
10548a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
105523a21a1cSEd Tanous                                      serverT, dbusObjectPath);
10568a07d286SRatan Gupta         }
105706785244SRatan Gupta 
105810cb44f3SEd Tanous         if (input.remoteRoleMapData)
105906785244SRatan Gupta         {
10606c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
106110cb44f3SEd Tanous                                *input.remoteRoleMapData);
106206785244SRatan Gupta         }
10638a07d286SRatan Gupta     });
10648a07d286SRatan Gupta }
1065d4b5443fSEd Tanous 
106658345856SAbhishek Patel inline void updateUserProperties(
106758345856SAbhishek Patel     std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& username,
1068618c14b4SEd Tanous     const std::optional<std::string>& password,
1069618c14b4SEd Tanous     const std::optional<bool>& enabled,
107058345856SAbhishek Patel     const std::optional<std::string>& roleId, const std::optional<bool>& locked,
107158345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes, bool userSelf)
10721abe55efSEd Tanous {
1073b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1074b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1075b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
10766c51eab1SEd Tanous 
10776c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1078618c14b4SEd Tanous         dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
107958345856SAbhishek Patel                          locked, accountTypes(std::move(accountTypes)),
108058345856SAbhishek Patel                          userSelf, asyncResp{std::move(asyncResp)}](int rc) {
1081e662eae8SEd Tanous         if (rc <= 0)
10826c51eab1SEd Tanous         {
1083d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
10846c51eab1SEd Tanous                                        username);
10856c51eab1SEd Tanous             return;
10866c51eab1SEd Tanous         }
10876c51eab1SEd Tanous 
10886c51eab1SEd Tanous         if (password)
10896c51eab1SEd Tanous         {
10906c51eab1SEd Tanous             int retval = pamUpdatePassword(username, *password);
10916c51eab1SEd Tanous 
10926c51eab1SEd Tanous             if (retval == PAM_USER_UNKNOWN)
10936c51eab1SEd Tanous             {
1094d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
10956c51eab1SEd Tanous                                            username);
10966c51eab1SEd Tanous             }
10976c51eab1SEd Tanous             else if (retval == PAM_AUTHTOK_ERR)
10986c51eab1SEd Tanous             {
10996c51eab1SEd Tanous                 // If password is invalid
11009bd80831SJason M. Bills                 messages::propertyValueFormatError(asyncResp->res, nullptr,
11019bd80831SJason M. Bills                                                    "Password");
110262598e31SEd Tanous                 BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
11036c51eab1SEd Tanous             }
11046c51eab1SEd Tanous             else if (retval != PAM_SUCCESS)
11056c51eab1SEd Tanous             {
11066c51eab1SEd Tanous                 messages::internalError(asyncResp->res);
11076c51eab1SEd Tanous                 return;
11086c51eab1SEd Tanous             }
1109e7b1b62bSEd Tanous             else
1110e7b1b62bSEd Tanous             {
1111e7b1b62bSEd Tanous                 messages::success(asyncResp->res);
1112e7b1b62bSEd Tanous             }
11136c51eab1SEd Tanous         }
11146c51eab1SEd Tanous 
11156c51eab1SEd Tanous         if (enabled)
11166c51eab1SEd Tanous         {
1117d02aad39SEd Tanous             setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
1118d02aad39SEd Tanous                             dbusObjectPath,
1119d02aad39SEd Tanous                             "xyz.openbmc_project.User.Attributes",
1120d02aad39SEd Tanous                             "UserEnabled", "Enabled", *enabled);
11216c51eab1SEd Tanous         }
11226c51eab1SEd Tanous 
11236c51eab1SEd Tanous         if (roleId)
11246c51eab1SEd Tanous         {
11256c51eab1SEd Tanous             std::string priv = getPrivilegeFromRoleId(*roleId);
11266c51eab1SEd Tanous             if (priv.empty())
11276c51eab1SEd Tanous             {
1128e2616cc5SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, true,
1129e2616cc5SEd Tanous                                                  "Locked");
11306c51eab1SEd Tanous                 return;
11316c51eab1SEd Tanous             }
1132d02aad39SEd Tanous             setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
1133d02aad39SEd Tanous                             dbusObjectPath,
1134d02aad39SEd Tanous                             "xyz.openbmc_project.User.Attributes",
1135d02aad39SEd Tanous                             "UserPrivilege", "RoleId", priv);
11366c51eab1SEd Tanous         }
11376c51eab1SEd Tanous 
11386c51eab1SEd Tanous         if (locked)
11396c51eab1SEd Tanous         {
11406c51eab1SEd Tanous             // admin can unlock the account which is locked by
11416c51eab1SEd Tanous             // successive authentication failures but admin should
11426c51eab1SEd Tanous             // not be allowed to lock an account.
11436c51eab1SEd Tanous             if (*locked)
11446c51eab1SEd Tanous             {
11456c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, "true",
11466c51eab1SEd Tanous                                                  "Locked");
11476c51eab1SEd Tanous                 return;
11486c51eab1SEd Tanous             }
1149d02aad39SEd Tanous             setDbusProperty(asyncResp, "xyz.openbmc_project.User.Manager",
1150d02aad39SEd Tanous                             dbusObjectPath,
11519ae226faSGeorge Liu                             "xyz.openbmc_project.User.Attributes",
1152d02aad39SEd Tanous                             "UserLockedForFailedAttempt", "Locked", *locked);
11536c51eab1SEd Tanous         }
115458345856SAbhishek Patel 
115558345856SAbhishek Patel         if (accountTypes)
115658345856SAbhishek Patel         {
115758345856SAbhishek Patel             patchAccountTypes(*accountTypes, asyncResp, dbusObjectPath,
115858345856SAbhishek Patel                               userSelf);
115958345856SAbhishek Patel         }
11606c51eab1SEd Tanous     });
11616c51eab1SEd Tanous }
11626c51eab1SEd Tanous 
11634c7d4d33SEd Tanous inline void handleAccountServiceHead(
11644c7d4d33SEd Tanous     App& app, const crow::Request& req,
11651ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
11666c51eab1SEd Tanous {
11673ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
116845ca1b86SEd Tanous     {
116945ca1b86SEd Tanous         return;
117045ca1b86SEd Tanous     }
11714c7d4d33SEd Tanous     asyncResp->res.addHeader(
11724c7d4d33SEd Tanous         boost::beast::http::field::link,
11734c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
11744c7d4d33SEd Tanous }
11754c7d4d33SEd Tanous 
11764c7d4d33SEd Tanous inline void
11774c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
11784c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
11794c7d4d33SEd Tanous {
1180afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1181afd369c6SJiaqing Zhao     {
1182afd369c6SJiaqing Zhao         return;
1183afd369c6SJiaqing Zhao     }
11843e72c202SNinad Palsule 
11853e72c202SNinad Palsule     if (req.session == nullptr)
11863e72c202SNinad Palsule     {
11873e72c202SNinad Palsule         messages::internalError(asyncResp->res);
11883e72c202SNinad Palsule         return;
11893e72c202SNinad Palsule     }
11903e72c202SNinad Palsule 
1191c1019828SEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
1192c1019828SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
1193c1019828SEd Tanous 
1194afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1195afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1196afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1197afd369c6SJiaqing Zhao 
11981476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
11991476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
1200482a69e7SRavi Teja     json["@odata.type"] = "#AccountService.v1_15_0.AccountService";
12011476687dSEd Tanous     json["Id"] = "AccountService";
12021476687dSEd Tanous     json["Name"] = "Account Service";
12031476687dSEd Tanous     json["Description"] = "Account Service";
12041476687dSEd Tanous     json["ServiceEnabled"] = true;
12051476687dSEd Tanous     json["MaxPasswordLength"] = 20;
12061ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
12071476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
1208482a69e7SRavi Teja     json["HTTPBasicAuth"] = authMethodsConfig.basic
1209482a69e7SRavi Teja                                 ? account_service::BasicAuthState::Enabled
1210482a69e7SRavi Teja                                 : account_service::BasicAuthState::Disabled;
1211482a69e7SRavi Teja 
1212482a69e7SRavi Teja     nlohmann::json::array_t allowed;
1213482a69e7SRavi Teja     allowed.emplace_back(account_service::BasicAuthState::Enabled);
1214482a69e7SRavi Teja     allowed.emplace_back(account_service::BasicAuthState::Disabled);
1215482a69e7SRavi Teja     json["HTTPBasicAuth@AllowableValues"] = std::move(allowed);
1216482a69e7SRavi Teja 
12171476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
12185b5574acSEd Tanous         "#OpenBMCAccountService.v1_0_0.AccountService";
12191476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
12201476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
12211476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
12221476687dSEd Tanous         authMethodsConfig.basic;
12231476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
12241476687dSEd Tanous         authMethodsConfig.sessionToken;
12251ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
12261ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
12271ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
12281476687dSEd Tanous 
12291ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
12301ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
12311ef4c342SEd Tanous     // permissions ConfigureManager
123272048780SAbhishek Patel     Privileges effectiveUserPrivileges =
12333e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
123472048780SAbhishek Patel 
123572048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
123672048780SAbhishek Patel                                          effectiveUserPrivileges))
123772048780SAbhishek Patel     {
12381ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
12391476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
124072048780SAbhishek Patel     }
1241d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1242d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1243d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
12445e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
12451ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
12463d958bbcSAppaRao Puli         if (ec)
12473d958bbcSAppaRao Puli         {
12483d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
12493d958bbcSAppaRao Puli             return;
12503d958bbcSAppaRao Puli         }
1251d1bde9e5SKrzysztof Grobelny 
125262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Got {}properties for AccountService",
125362598e31SEd Tanous                          propertiesList.size());
1254d1bde9e5SKrzysztof Grobelny 
1255d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1256d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1257d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1258d1bde9e5SKrzysztof Grobelny 
1259d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1260d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1261d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1262d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1263d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1264d1bde9e5SKrzysztof Grobelny 
1265d1bde9e5SKrzysztof Grobelny         if (!success)
12663d958bbcSAppaRao Puli         {
1267d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1268d1bde9e5SKrzysztof Grobelny             return;
12693d958bbcSAppaRao Puli         }
1270d1bde9e5SKrzysztof Grobelny 
1271d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
12723d958bbcSAppaRao Puli         {
1273d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
12743d958bbcSAppaRao Puli         }
1275d1bde9e5SKrzysztof Grobelny 
1276d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
12773d958bbcSAppaRao Puli         {
1278d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1279d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1280d1bde9e5SKrzysztof Grobelny         }
1281d1bde9e5SKrzysztof Grobelny 
1282d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
12833d958bbcSAppaRao Puli         {
1284002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1285d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
12863d958bbcSAppaRao Puli         }
1287d1bde9e5SKrzysztof Grobelny     });
12886973a582SRatan Gupta 
128902cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1290ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1291cb13a392SEd Tanous         if (!success)
1292cb13a392SEd Tanous         {
1293cb13a392SEd Tanous             return;
1294cb13a392SEd Tanous         }
1295002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1296ab828d7cSRatan Gupta     };
1297ab828d7cSRatan Gupta 
1298ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1299ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
13001ef4c342SEd Tanous }
13016973a582SRatan Gupta 
13021ef4c342SEd Tanous inline void handleAccountServicePatch(
13031ef4c342SEd Tanous     App& app, const crow::Request& req,
13041ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13051ef4c342SEd Tanous {
13063ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
130745ca1b86SEd Tanous     {
130845ca1b86SEd Tanous         return;
130945ca1b86SEd Tanous     }
1310f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1311f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1312ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1313f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
131410cb44f3SEd Tanous     LdapPatchParams ldapObject;
131510cb44f3SEd Tanous     LdapPatchParams activeDirectoryObject;
1316c1019828SEd Tanous     AuthMethods auth;
1317482a69e7SRavi Teja     std::optional<std::string> httpBasicAuth;
1318c1019828SEd Tanous     // clang-format off
131915ed6780SWilly Tu     if (!json_util::readJsonPatch(
1320c1019828SEd Tanous             req, asyncResp->res,
1321c1019828SEd Tanous             "AccountLockoutDuration", unlockTimeout,
1322c1019828SEd Tanous             "AccountLockoutThreshold", lockoutThreshold,
132310cb44f3SEd Tanous             "ActiveDirectory/Authentication/AuthenticationType", activeDirectoryObject.authType,
132410cb44f3SEd Tanous             "ActiveDirectory/Authentication/Password", activeDirectoryObject.password,
132510cb44f3SEd Tanous             "ActiveDirectory/Authentication/Username", activeDirectoryObject.userName,
132610cb44f3SEd Tanous             "ActiveDirectory/LDAPService/SearchSettings/BaseDistinguishedNames", activeDirectoryObject.baseDNList,
132710cb44f3SEd Tanous             "ActiveDirectory/LDAPService/SearchSettings/GroupsAttribute", activeDirectoryObject.groupsAttribute,
132810cb44f3SEd Tanous             "ActiveDirectory/LDAPService/SearchSettings/UsernameAttribute", activeDirectoryObject.userNameAttribute,
132910cb44f3SEd Tanous             "ActiveDirectory/RemoteRoleMapping", activeDirectoryObject.remoteRoleMapData,
133010cb44f3SEd Tanous             "ActiveDirectory/ServiceAddresses", activeDirectoryObject.serviceAddressList,
133110cb44f3SEd Tanous             "ActiveDirectory/ServiceEnabled", activeDirectoryObject.serviceEnabled,
133210cb44f3SEd Tanous             "LDAP/Authentication/AuthenticationType", ldapObject.authType,
133310cb44f3SEd Tanous             "LDAP/Authentication/Password", ldapObject.password,
133410cb44f3SEd Tanous             "LDAP/Authentication/Username", ldapObject.userName,
133510cb44f3SEd Tanous             "LDAP/LDAPService/SearchSettings/BaseDistinguishedNames", ldapObject.baseDNList,
133610cb44f3SEd Tanous             "LDAP/LDAPService/SearchSettings/GroupsAttribute", ldapObject.groupsAttribute,
133710cb44f3SEd Tanous             "LDAP/LDAPService/SearchSettings/UsernameAttribute", ldapObject.userNameAttribute,
133810cb44f3SEd Tanous             "LDAP/RemoteRoleMapping", ldapObject.remoteRoleMapData,
133910cb44f3SEd Tanous             "LDAP/ServiceAddresses", ldapObject.serviceAddressList,
134010cb44f3SEd Tanous             "LDAP/ServiceEnabled", ldapObject.serviceEnabled,
1341c1019828SEd Tanous             "MaxPasswordLength", maxPasswordLength,
1342c1019828SEd Tanous             "MinPasswordLength", minPasswordLength,
1343c1019828SEd Tanous             "Oem/OpenBMC/AuthMethods/BasicAuth", auth.basicAuth,
1344c1019828SEd Tanous             "Oem/OpenBMC/AuthMethods/Cookie", auth.cookie,
1345c1019828SEd Tanous             "Oem/OpenBMC/AuthMethods/SessionToken", auth.sessionToken,
134610cb44f3SEd Tanous             "Oem/OpenBMC/AuthMethods/TLS", auth.tls,
1347482a69e7SRavi Teja             "Oem/OpenBMC/AuthMethods/XToken", auth.xToken,
1348482a69e7SRavi Teja             "HTTPBasicAuth", httpBasicAuth))
1349f5ffd806SEd Tanous     {
1350f5ffd806SEd Tanous         return;
1351f5ffd806SEd Tanous     }
1352c1019828SEd Tanous     // clang-format on
1353f5ffd806SEd Tanous 
1354482a69e7SRavi Teja     if (httpBasicAuth)
1355482a69e7SRavi Teja     {
1356482a69e7SRavi Teja         if (*httpBasicAuth == "Enabled")
1357482a69e7SRavi Teja         {
1358482a69e7SRavi Teja             auth.basicAuth = true;
1359482a69e7SRavi Teja         }
1360482a69e7SRavi Teja         else if (*httpBasicAuth == "Disabled")
1361482a69e7SRavi Teja         {
1362482a69e7SRavi Teja             auth.basicAuth = false;
1363482a69e7SRavi Teja         }
1364482a69e7SRavi Teja         else
1365482a69e7SRavi Teja         {
1366482a69e7SRavi Teja             messages::propertyValueNotInList(asyncResp->res, "HttpBasicAuth",
1367482a69e7SRavi Teja                                              *httpBasicAuth);
1368482a69e7SRavi Teja         }
1369482a69e7SRavi Teja     }
1370482a69e7SRavi Teja 
1371f5ffd806SEd Tanous     if (minPasswordLength)
1372f5ffd806SEd Tanous     {
1373d02aad39SEd Tanous         setDbusProperty(
1374d02aad39SEd Tanous             asyncResp, "xyz.openbmc_project.User.Manager",
1375d02aad39SEd Tanous             sdbusplus::message::object_path("/xyz/openbmc_project/user"),
13769ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1377d02aad39SEd Tanous             "MinPasswordLength", *minPasswordLength);
1378f5ffd806SEd Tanous     }
1379f5ffd806SEd Tanous 
1380f5ffd806SEd Tanous     if (maxPasswordLength)
1381f5ffd806SEd Tanous     {
13821ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1383f5ffd806SEd Tanous     }
1384f5ffd806SEd Tanous 
138510cb44f3SEd Tanous     handleLDAPPatch(std::move(activeDirectoryObject), asyncResp,
138610cb44f3SEd Tanous                     "ActiveDirectory");
138710cb44f3SEd Tanous     handleLDAPPatch(std::move(ldapObject), asyncResp, "LDAP");
1388f5ffd806SEd Tanous 
1389c1019828SEd Tanous     handleAuthMethodsPatch(asyncResp, auth);
1390f5ffd806SEd Tanous 
1391f5ffd806SEd Tanous     if (unlockTimeout)
1392f5ffd806SEd Tanous     {
1393d02aad39SEd Tanous         setDbusProperty(
1394d02aad39SEd Tanous             asyncResp, "xyz.openbmc_project.User.Manager",
1395d02aad39SEd Tanous             sdbusplus::message::object_path("/xyz/openbmc_project/user"),
13969ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1397d02aad39SEd Tanous             "AccountLockoutDuration", *unlockTimeout);
1398f5ffd806SEd Tanous     }
1399f5ffd806SEd Tanous     if (lockoutThreshold)
1400f5ffd806SEd Tanous     {
1401d02aad39SEd Tanous         setDbusProperty(
1402d02aad39SEd Tanous             asyncResp, "xyz.openbmc_project.User.Manager",
1403d02aad39SEd Tanous             sdbusplus::message::object_path("/xyz/openbmc_project/user"),
14049ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy",
1405d02aad39SEd Tanous             "MaxLoginAttemptBeforeLockout", "AccountLockoutThreshold",
1406d02aad39SEd Tanous             *lockoutThreshold);
1407f5ffd806SEd Tanous     }
14081ef4c342SEd Tanous }
1409f5ffd806SEd Tanous 
14104c7d4d33SEd Tanous inline void handleAccountCollectionHead(
14111ef4c342SEd Tanous     App& app, const crow::Request& req,
14121ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14131ef4c342SEd Tanous {
14143ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
141545ca1b86SEd Tanous     {
141645ca1b86SEd Tanous         return;
141745ca1b86SEd Tanous     }
14184c7d4d33SEd Tanous     asyncResp->res.addHeader(
14194c7d4d33SEd Tanous         boost::beast::http::field::link,
14204c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
14214c7d4d33SEd Tanous }
14224c7d4d33SEd Tanous 
14234c7d4d33SEd Tanous inline void handleAccountCollectionGet(
14244c7d4d33SEd Tanous     App& app, const crow::Request& req,
14254c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14264c7d4d33SEd Tanous {
1427afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1428afd369c6SJiaqing Zhao     {
1429afd369c6SJiaqing Zhao         return;
1430afd369c6SJiaqing Zhao     }
14313e72c202SNinad Palsule 
14323e72c202SNinad Palsule     if (req.session == nullptr)
14333e72c202SNinad Palsule     {
14343e72c202SNinad Palsule         messages::internalError(asyncResp->res);
14353e72c202SNinad Palsule         return;
14363e72c202SNinad Palsule     }
14373e72c202SNinad Palsule 
1438afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1439afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1440afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
14411476687dSEd Tanous 
14421476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
14431476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
14441ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
14451476687dSEd Tanous                                               "ManagerAccountCollection";
14461476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
14471476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
14480f74e643SEd Tanous 
14496c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
14503e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
14516c51eab1SEd Tanous 
1452f5e29f33SJunLin Chen     std::string thisUser;
1453f5e29f33SJunLin Chen     if (req.session)
1454f5e29f33SJunLin Chen     {
1455f5e29f33SJunLin Chen         thisUser = req.session->username;
1456f5e29f33SJunLin Chen     }
14575eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
14585eb468daSGeorge Liu     dbus::utility::getManagedObjects(
14595eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
1460cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
14615e7e2dc5SEd Tanous             const boost::system::error_code& ec,
1462711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1463b9b2e0b2SEd Tanous         if (ec)
1464b9b2e0b2SEd Tanous         {
1465f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1466b9b2e0b2SEd Tanous             return;
1467b9b2e0b2SEd Tanous         }
1468b9b2e0b2SEd Tanous 
1469cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1470002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1471cef1ddfbSEd Tanous 
1472cef1ddfbSEd Tanous         bool userCanSeeSelf =
1473002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1474cef1ddfbSEd Tanous 
1475002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1476b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1477b9b2e0b2SEd Tanous 
14789eb808c1SEd Tanous         for (const auto& userpath : users)
1479b9b2e0b2SEd Tanous         {
14802dfd18efSEd Tanous             std::string user = userpath.first.filename();
14812dfd18efSEd Tanous             if (user.empty())
1482b9b2e0b2SEd Tanous             {
14832dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
148462598e31SEd Tanous                 BMCWEB_LOG_ERROR("Invalid firmware ID");
14852dfd18efSEd Tanous 
14862dfd18efSEd Tanous                 return;
1487b9b2e0b2SEd Tanous             }
1488f365910cSGunnar Mills 
1489f365910cSGunnar Mills             // As clarified by Redfish here:
1490f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
14916c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
14926c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
14936c51eab1SEd Tanous             // accounts.
14941ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1495f365910cSGunnar Mills             {
14961476687dSEd Tanous                 nlohmann::json::object_t member;
14973b32780dSEd Tanous                 member["@odata.id"] = boost::urls::format(
14983b32780dSEd Tanous                     "/redfish/v1/AccountService/Accounts/{}", user);
1499b2ba3072SPatrick Williams                 memberArray.emplace_back(std::move(member));
1500b9b2e0b2SEd Tanous             }
1501f365910cSGunnar Mills         }
15021ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
15035eb468daSGeorge Liu     });
15041ef4c342SEd Tanous }
15056c51eab1SEd Tanous 
150697e90da3SNinad Palsule inline void processAfterCreateUser(
150797e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
150897e90da3SNinad Palsule     const std::string& username, const std::string& password,
150997e90da3SNinad Palsule     const boost::system::error_code& ec, sdbusplus::message_t& m)
151097e90da3SNinad Palsule {
151197e90da3SNinad Palsule     if (ec)
151297e90da3SNinad Palsule     {
151397e90da3SNinad Palsule         userErrorMessageHandler(m.get_error(), asyncResp, username, "");
151497e90da3SNinad Palsule         return;
151597e90da3SNinad Palsule     }
151697e90da3SNinad Palsule 
151797e90da3SNinad Palsule     if (pamUpdatePassword(username, password) != PAM_SUCCESS)
151897e90da3SNinad Palsule     {
151997e90da3SNinad Palsule         // At this point we have a user that's been
152097e90da3SNinad Palsule         // created, but the password set
152197e90da3SNinad Palsule         // failed.Something is wrong, so delete the user
152297e90da3SNinad Palsule         // that we've already created
152397e90da3SNinad Palsule         sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
152497e90da3SNinad Palsule         tempObjPath /= username;
152597e90da3SNinad Palsule         const std::string userPath(tempObjPath);
152697e90da3SNinad Palsule 
152797e90da3SNinad Palsule         crow::connections::systemBus->async_method_call(
152897e90da3SNinad Palsule             [asyncResp, password](const boost::system::error_code& ec3) {
152997e90da3SNinad Palsule             if (ec3)
153097e90da3SNinad Palsule             {
153197e90da3SNinad Palsule                 messages::internalError(asyncResp->res);
153297e90da3SNinad Palsule                 return;
153397e90da3SNinad Palsule             }
153497e90da3SNinad Palsule 
153597e90da3SNinad Palsule             // If password is invalid
15369bd80831SJason M. Bills             messages::propertyValueFormatError(asyncResp->res, nullptr,
153797e90da3SNinad Palsule                                                "Password");
153897e90da3SNinad Palsule         },
153997e90da3SNinad Palsule             "xyz.openbmc_project.User.Manager", userPath,
154097e90da3SNinad Palsule             "xyz.openbmc_project.Object.Delete", "Delete");
154197e90da3SNinad Palsule 
154262598e31SEd Tanous         BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
154397e90da3SNinad Palsule         return;
154497e90da3SNinad Palsule     }
154597e90da3SNinad Palsule 
154697e90da3SNinad Palsule     messages::created(asyncResp->res);
154797e90da3SNinad Palsule     asyncResp->res.addHeader("Location",
154897e90da3SNinad Palsule                              "/redfish/v1/AccountService/Accounts/" + username);
154997e90da3SNinad Palsule }
155097e90da3SNinad Palsule 
155197e90da3SNinad Palsule inline void processAfterGetAllGroups(
155297e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
155397e90da3SNinad Palsule     const std::string& username, const std::string& password,
1554e01d0c36SEd Tanous     const std::string& roleId, bool enabled,
15559ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes,
155697e90da3SNinad Palsule     const std::vector<std::string>& allGroupsList)
155797e90da3SNinad Palsule {
15583e72c202SNinad Palsule     std::vector<std::string> userGroups;
15599ba73934SNinad Palsule     std::vector<std::string> accountTypeUserGroups;
15609ba73934SNinad Palsule 
15619ba73934SNinad Palsule     // If user specified account types then convert them to unix user groups
15629ba73934SNinad Palsule     if (accountTypes)
15639ba73934SNinad Palsule     {
15649ba73934SNinad Palsule         if (!getUserGroupFromAccountType(asyncResp->res, *accountTypes,
15659ba73934SNinad Palsule                                          accountTypeUserGroups))
15669ba73934SNinad Palsule         {
15679ba73934SNinad Palsule             // Problem in mapping Account Types to User Groups, Error already
15689ba73934SNinad Palsule             // logged.
15699ba73934SNinad Palsule             return;
15709ba73934SNinad Palsule         }
15719ba73934SNinad Palsule     }
15729ba73934SNinad Palsule 
15733e72c202SNinad Palsule     for (const auto& grp : allGroupsList)
15743e72c202SNinad Palsule     {
15759ba73934SNinad Palsule         // If user specified the account type then only accept groups which are
15769ba73934SNinad Palsule         // in the account types group list.
15779ba73934SNinad Palsule         if (!accountTypeUserGroups.empty())
15789ba73934SNinad Palsule         {
15799ba73934SNinad Palsule             bool found = false;
15809ba73934SNinad Palsule             for (const auto& grp1 : accountTypeUserGroups)
15819ba73934SNinad Palsule             {
15829ba73934SNinad Palsule                 if (grp == grp1)
15839ba73934SNinad Palsule                 {
15849ba73934SNinad Palsule                     found = true;
15859ba73934SNinad Palsule                     break;
15869ba73934SNinad Palsule                 }
15879ba73934SNinad Palsule             }
15889ba73934SNinad Palsule             if (!found)
15899ba73934SNinad Palsule             {
15909ba73934SNinad Palsule                 continue;
15919ba73934SNinad Palsule             }
15929ba73934SNinad Palsule         }
15939ba73934SNinad Palsule 
15943e72c202SNinad Palsule         // Console access is provided to the user who is a member of
15953e72c202SNinad Palsule         // hostconsole group and has a administrator role. So, set
15963e72c202SNinad Palsule         // hostconsole group only for the administrator.
15979ba73934SNinad Palsule         if ((grp == "hostconsole") && (roleId != "priv-admin"))
15983e72c202SNinad Palsule         {
15999ba73934SNinad Palsule             if (!accountTypeUserGroups.empty())
16009ba73934SNinad Palsule             {
160162598e31SEd Tanous                 BMCWEB_LOG_ERROR(
160262598e31SEd Tanous                     "Only administrator can get HostConsole access");
16039ba73934SNinad Palsule                 asyncResp->res.result(boost::beast::http::status::bad_request);
16049ba73934SNinad Palsule                 return;
16059ba73934SNinad Palsule             }
16069ba73934SNinad Palsule             continue;
16079ba73934SNinad Palsule         }
16083e72c202SNinad Palsule         userGroups.emplace_back(grp);
16093e72c202SNinad Palsule     }
16109ba73934SNinad Palsule 
16119ba73934SNinad Palsule     // Make sure user specified groups are valid. This is internal error because
16129ba73934SNinad Palsule     // it some inconsistencies between user manager and bmcweb.
16139ba73934SNinad Palsule     if (!accountTypeUserGroups.empty() &&
16149ba73934SNinad Palsule         accountTypeUserGroups.size() != userGroups.size())
16159ba73934SNinad Palsule     {
16169ba73934SNinad Palsule         messages::internalError(asyncResp->res);
16179ba73934SNinad Palsule         return;
16183e72c202SNinad Palsule     }
161997e90da3SNinad Palsule     crow::connections::systemBus->async_method_call(
162097e90da3SNinad Palsule         [asyncResp, username, password](const boost::system::error_code& ec2,
162197e90da3SNinad Palsule                                         sdbusplus::message_t& m) {
162297e90da3SNinad Palsule         processAfterCreateUser(asyncResp, username, password, ec2, m);
162397e90da3SNinad Palsule     },
162497e90da3SNinad Palsule         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
16253e72c202SNinad Palsule         "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
1626e01d0c36SEd Tanous         roleId, enabled);
162797e90da3SNinad Palsule }
162897e90da3SNinad Palsule 
16291ef4c342SEd Tanous inline void handleAccountCollectionPost(
16301ef4c342SEd Tanous     App& app, const crow::Request& req,
16311ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16321ef4c342SEd Tanous {
16333ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
163445ca1b86SEd Tanous     {
163545ca1b86SEd Tanous         return;
163645ca1b86SEd Tanous     }
16379712f8acSEd Tanous     std::string username;
16389712f8acSEd Tanous     std::string password;
1639e01d0c36SEd Tanous     std::optional<std::string> roleIdJson;
1640e01d0c36SEd Tanous     std::optional<bool> enabledJson;
16419ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes;
1642e01d0c36SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
1643e01d0c36SEd Tanous                                   "Password", password, "RoleId", roleIdJson,
1644e01d0c36SEd Tanous                                   "Enabled", enabledJson, "AccountTypes",
1645e01d0c36SEd Tanous                                   accountTypes))
164604ae99ecSEd Tanous     {
164704ae99ecSEd Tanous         return;
164804ae99ecSEd Tanous     }
164904ae99ecSEd Tanous 
1650e01d0c36SEd Tanous     std::string roleId = roleIdJson.value_or("User");
1651e01d0c36SEd Tanous     std::string priv = getPrivilegeFromRoleId(roleId);
165284e12cb7SAppaRao Puli     if (priv.empty())
165304ae99ecSEd Tanous     {
1654e01d0c36SEd Tanous         messages::propertyValueNotInList(asyncResp->res, roleId, "RoleId");
165504ae99ecSEd Tanous         return;
165604ae99ecSEd Tanous     }
16579712f8acSEd Tanous     roleId = priv;
165804ae99ecSEd Tanous 
1659e01d0c36SEd Tanous     bool enabled = enabledJson.value_or(true);
1660e01d0c36SEd Tanous 
1661599c71d8SAyushi Smriti     // Reading AllGroups property
16621e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
16631ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
16641ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
16651ef4c342SEd Tanous         "AllGroups",
16669ba73934SNinad Palsule         [asyncResp, username, password{std::move(password)}, roleId, enabled,
16679ba73934SNinad Palsule          accountTypes](const boost::system::error_code& ec,
16681e1e598dSJonathan Doman                        const std::vector<std::string>& allGroupsList) {
1669599c71d8SAyushi Smriti         if (ec)
1670599c71d8SAyushi Smriti         {
167162598e31SEd Tanous             BMCWEB_LOG_DEBUG("ERROR with async_method_call");
1672599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1673599c71d8SAyushi Smriti             return;
1674599c71d8SAyushi Smriti         }
1675599c71d8SAyushi Smriti 
16761e1e598dSJonathan Doman         if (allGroupsList.empty())
1677599c71d8SAyushi Smriti         {
1678599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1679599c71d8SAyushi Smriti             return;
1680599c71d8SAyushi Smriti         }
1681599c71d8SAyushi Smriti 
168297e90da3SNinad Palsule         processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
16839ba73934SNinad Palsule                                  accountTypes, allGroupsList);
16841e1e598dSJonathan Doman     });
16851ef4c342SEd Tanous }
1686b9b2e0b2SEd Tanous 
16871ef4c342SEd Tanous inline void
16884c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
16896c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16904c7d4d33SEd Tanous                       const std::string& /*accountName*/)
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/ManagerAccount/ManagerAccount.json>; rel=describedby");
16994c7d4d33SEd Tanous }
1700afd369c6SJiaqing Zhao 
17014c7d4d33SEd Tanous inline void
17024c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
17034c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17044c7d4d33SEd Tanous                      const std::string& accountName)
17054c7d4d33SEd Tanous {
1706afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1707afd369c6SJiaqing Zhao     {
1708afd369c6SJiaqing Zhao         return;
1709afd369c6SJiaqing Zhao     }
1710afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1711afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1712afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1713afd369c6SJiaqing Zhao 
1714*25b54dbaSEd Tanous     if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
1715*25b54dbaSEd Tanous     {
1716031514fbSJunLin Chen         // If authentication is disabled, there are no user accounts
1717*25b54dbaSEd Tanous         messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1718*25b54dbaSEd Tanous                                    accountName);
1719031514fbSJunLin Chen         return;
1720*25b54dbaSEd Tanous     }
1721afd369c6SJiaqing Zhao 
1722031514fbSJunLin Chen     if (req.session == nullptr)
1723031514fbSJunLin Chen     {
1724031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1725031514fbSJunLin Chen         return;
1726031514fbSJunLin Chen     }
17276c51eab1SEd Tanous     if (req.session->username != accountName)
1728b9b2e0b2SEd Tanous     {
17296c51eab1SEd Tanous         // At this point we've determined that the user is trying to
17301ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
17311ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
17321ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
17336c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
17343e72c202SNinad Palsule             redfish::getUserPrivileges(*req.session);
17351ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
17361ef4c342SEd Tanous                                                          "ConfigureManager"};
17376c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
17386c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1739900f9497SJoseph Reynolds         {
174062598e31SEd Tanous             BMCWEB_LOG_DEBUG("GET Account denied access");
1741900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1742900f9497SJoseph Reynolds             return;
1743900f9497SJoseph Reynolds         }
1744900f9497SJoseph Reynolds     }
1745900f9497SJoseph Reynolds 
17465eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
17475eb468daSGeorge Liu     dbus::utility::getManagedObjects(
17485eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
17491ef4c342SEd Tanous         [asyncResp,
17505e7e2dc5SEd Tanous          accountName](const boost::system::error_code& ec,
1751711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1752b9b2e0b2SEd Tanous         if (ec)
1753b9b2e0b2SEd Tanous         {
1754f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1755b9b2e0b2SEd Tanous             return;
1756b9b2e0b2SEd Tanous         }
17573544d2a7SEd Tanous         const auto userIt = std::ranges::find_if(
175880f79a40SMichael Shen             users,
175980f79a40SMichael Shen             [accountName](
1760b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
176180f79a40SMichael Shen                                 dbus::utility::DBusInterfacesMap>& user) {
176255f79e6fSEd Tanous             return accountName == user.first.filename();
1763b477fd44SP Dheeraj Srujan Kumar         });
1764b9b2e0b2SEd Tanous 
176584e12cb7SAppaRao Puli         if (userIt == users.end())
1766b9b2e0b2SEd Tanous         {
1767002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1768002d39b4SEd Tanous                                        accountName);
176984e12cb7SAppaRao Puli             return;
177084e12cb7SAppaRao Puli         }
17714e68c45bSAyushi Smriti 
17721476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
177358345856SAbhishek Patel             "#ManagerAccount.v1_7_0.ManagerAccount";
17741476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
17751476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
17761476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
177758345856SAbhishek Patel         asyncResp->res.jsonValue["StrictAccountTypes"] = true;
17784e68c45bSAyushi Smriti 
177984e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
178065b0dc32SEd Tanous         {
1781002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
178265b0dc32SEd Tanous             {
178365b0dc32SEd Tanous                 for (const auto& property : interface.second)
178465b0dc32SEd Tanous                 {
178565b0dc32SEd Tanous                     if (property.first == "UserEnabled")
178665b0dc32SEd Tanous                     {
178765b0dc32SEd Tanous                         const bool* userEnabled =
1788abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
178965b0dc32SEd Tanous                         if (userEnabled == nullptr)
179065b0dc32SEd Tanous                         {
179162598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserEnabled wasn't a bool");
179284e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
179384e12cb7SAppaRao Puli                             return;
179465b0dc32SEd Tanous                         }
1795002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
179665b0dc32SEd Tanous                     }
1797002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
179865b0dc32SEd Tanous                     {
179965b0dc32SEd Tanous                         const bool* userLocked =
1800abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
180165b0dc32SEd Tanous                         if (userLocked == nullptr)
180265b0dc32SEd Tanous                         {
180362598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserLockedForF"
180484e12cb7SAppaRao Puli                                              "ailedAttempt "
180562598e31SEd Tanous                                              "wasn't a bool");
180684e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
180784e12cb7SAppaRao Puli                             return;
180865b0dc32SEd Tanous                         }
1809002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
1810002d39b4SEd Tanous                         asyncResp->res
1811002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
18123bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
181365b0dc32SEd Tanous                     }
181484e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
181584e12cb7SAppaRao Puli                     {
181654fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1817002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
181854fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
181984e12cb7SAppaRao Puli                         {
182062598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserPrivilege wasn't a "
182162598e31SEd Tanous                                              "string");
182284e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
182384e12cb7SAppaRao Puli                             return;
182484e12cb7SAppaRao Puli                         }
18251ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
182654fc587aSNagaraju Goruganti                         if (role.empty())
182784e12cb7SAppaRao Puli                         {
182862598e31SEd Tanous                             BMCWEB_LOG_ERROR("Invalid user role");
182984e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
183084e12cb7SAppaRao Puli                             return;
183184e12cb7SAppaRao Puli                         }
183254fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
183384e12cb7SAppaRao Puli 
18341476687dSEd Tanous                         nlohmann::json& roleEntry =
1835002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
18363b32780dSEd Tanous                         roleEntry["@odata.id"] = boost::urls::format(
18373b32780dSEd Tanous                             "/redfish/v1/AccountService/Roles/{}", role);
183884e12cb7SAppaRao Puli                     }
1839002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
18403bf4e632SJoseph Reynolds                     {
18413bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
18423bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
18433bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
18443bf4e632SJoseph Reynolds                         {
184562598e31SEd Tanous                             BMCWEB_LOG_ERROR(
184662598e31SEd Tanous                                 "UserPasswordExpired wasn't a bool");
18473bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
18483bf4e632SJoseph Reynolds                             return;
18493bf4e632SJoseph Reynolds                         }
1850002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
18513bf4e632SJoseph Reynolds                             *userPasswordExpired;
18523bf4e632SJoseph Reynolds                     }
1853c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
1854c7229815SAbhishek Patel                     {
1855c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
1856c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
1857c7229815SAbhishek Patel                                 &property.second);
1858c7229815SAbhishek Patel                         if (userGroups == nullptr)
1859c7229815SAbhishek Patel                         {
186062598e31SEd Tanous                             BMCWEB_LOG_ERROR(
186162598e31SEd Tanous                                 "userGroups wasn't a string vector");
1862c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1863c7229815SAbhishek Patel                             return;
1864c7229815SAbhishek Patel                         }
1865c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
1866c7229815SAbhishek Patel                         {
186762598e31SEd Tanous                             BMCWEB_LOG_ERROR("userGroups mapping failed");
1868c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1869c7229815SAbhishek Patel                             return;
1870c7229815SAbhishek Patel                         }
1871c7229815SAbhishek Patel                     }
187265b0dc32SEd Tanous                 }
187365b0dc32SEd Tanous             }
187465b0dc32SEd Tanous         }
187565b0dc32SEd Tanous 
18763b32780dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
18773b32780dSEd Tanous             "/redfish/v1/AccountService/Accounts/{}", accountName);
1878b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
1879b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
18805eb468daSGeorge Liu     });
18811ef4c342SEd Tanous }
1882a840879dSEd Tanous 
18831ef4c342SEd Tanous inline void
188420fc307fSGunnar Mills     handleAccountDelete(App& app, const crow::Request& req,
18856c51eab1SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18861ef4c342SEd Tanous                         const std::string& username)
18871ef4c342SEd Tanous {
18883ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
188945ca1b86SEd Tanous     {
189045ca1b86SEd Tanous         return;
189145ca1b86SEd Tanous     }
18921ef4c342SEd Tanous 
1893*25b54dbaSEd Tanous     if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
1894*25b54dbaSEd Tanous     {
1895031514fbSJunLin Chen         // If authentication is disabled, there are no user accounts
1896d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1897031514fbSJunLin Chen         return;
1898*25b54dbaSEd Tanous     }
18991ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
19001ef4c342SEd Tanous     tempObjPath /= username;
19011ef4c342SEd Tanous     const std::string userPath(tempObjPath);
19021ef4c342SEd Tanous 
19031ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
19045e7e2dc5SEd Tanous         [asyncResp, username](const boost::system::error_code& ec) {
19051ef4c342SEd Tanous         if (ec)
19061ef4c342SEd Tanous         {
1907d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
19081ef4c342SEd Tanous                                        username);
19091ef4c342SEd Tanous             return;
19101ef4c342SEd Tanous         }
19111ef4c342SEd Tanous 
19121ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
19131ef4c342SEd Tanous     },
19141ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
19151ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
19161ef4c342SEd Tanous }
19171ef4c342SEd Tanous 
19181ef4c342SEd Tanous inline void
19191ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
19201ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19211ef4c342SEd Tanous                        const std::string& username)
19221ef4c342SEd Tanous {
19231ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19241ef4c342SEd Tanous     {
19251ef4c342SEd Tanous         return;
19261ef4c342SEd Tanous     }
1927*25b54dbaSEd Tanous     if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
1928*25b54dbaSEd Tanous     {
19291ef4c342SEd Tanous         // If authentication is disabled, there are no user accounts
1930d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
19311ef4c342SEd Tanous         return;
1932*25b54dbaSEd Tanous     }
1933a24526dcSEd Tanous     std::optional<std::string> newUserName;
1934a24526dcSEd Tanous     std::optional<std::string> password;
1935a24526dcSEd Tanous     std::optional<bool> enabled;
1936a24526dcSEd Tanous     std::optional<std::string> roleId;
193724c8542dSRatan Gupta     std::optional<bool> locked;
193858345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes;
193958345856SAbhishek Patel 
1940031514fbSJunLin Chen     if (req.session == nullptr)
1941031514fbSJunLin Chen     {
1942031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1943031514fbSJunLin Chen         return;
1944031514fbSJunLin Chen     }
1945031514fbSJunLin Chen 
19462b9c1dfeSEd Tanous     bool userSelf = (username == req.session->username);
19472b9c1dfeSEd Tanous 
1948e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
19493e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
1950e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
1951e9cc5172SEd Tanous     bool userHasConfigureUsers =
1952e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
1953e9cc5172SEd Tanous     if (userHasConfigureUsers)
1954e9cc5172SEd Tanous     {
1955e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
195658345856SAbhishek Patel         if (!json_util::readJsonPatch(
195758345856SAbhishek Patel                 req, asyncResp->res, "UserName", newUserName, "Password",
195858345856SAbhishek Patel                 password, "RoleId", roleId, "Enabled", enabled, "Locked",
195958345856SAbhishek Patel                 locked, "AccountTypes", accountTypes))
1960a840879dSEd Tanous         {
1961a840879dSEd Tanous             return;
1962a840879dSEd Tanous         }
1963e9cc5172SEd Tanous     }
1964e9cc5172SEd Tanous     else
1965900f9497SJoseph Reynolds     {
1966e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
196758345856SAbhishek Patel         if (!userSelf)
1968900f9497SJoseph Reynolds         {
1969900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1970900f9497SJoseph Reynolds             return;
1971900f9497SJoseph Reynolds         }
1972031514fbSJunLin Chen 
1973e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
19741ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
19751ef4c342SEd Tanous                                       password))
1976e9cc5172SEd Tanous         {
1977e9cc5172SEd Tanous             return;
1978e9cc5172SEd Tanous         }
1979900f9497SJoseph Reynolds     }
1980900f9497SJoseph Reynolds 
198166b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
19826c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
19836c51eab1SEd Tanous     // updating user properties other then username. If username
19846c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
19856c51eab1SEd Tanous     // user rename request.
198666b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
1987a840879dSEd Tanous     {
19881ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
198958345856SAbhishek Patel                              locked, accountTypes, userSelf);
199084e12cb7SAppaRao Puli         return;
199184e12cb7SAppaRao Puli     }
199284e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
19936c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
19941ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
199558345856SAbhishek Patel          locked, userSelf, accountTypes(std::move(accountTypes))](
1996e81de512SEd Tanous             const boost::system::error_code& ec, sdbusplus::message_t& m) {
199784e12cb7SAppaRao Puli         if (ec)
199884e12cb7SAppaRao Puli         {
1999002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2000002d39b4SEd Tanous                                     username);
2001a840879dSEd Tanous             return;
2002a840879dSEd Tanous         }
2003a840879dSEd Tanous 
2004002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
200558345856SAbhishek Patel                              locked, accountTypes, userSelf);
200684e12cb7SAppaRao Puli     },
20071ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
200884e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
200984e12cb7SAppaRao Puli         *newUserName);
20101ef4c342SEd Tanous }
20111ef4c342SEd Tanous 
20121ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
20131ef4c342SEd Tanous {
20141ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20154c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
20164c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20174c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
20184c7d4d33SEd Tanous 
20194c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20201ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
20211ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20221ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
20231ef4c342SEd Tanous 
20241ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20251ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
20261ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20271ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
20281ef4c342SEd Tanous 
20291ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20304c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
20314c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20324c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
20334c7d4d33SEd Tanous 
20344c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20351ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
20361ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20371ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
20381ef4c342SEd Tanous 
20391ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20401ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
20411ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
20421ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
20431ef4c342SEd Tanous 
20441ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20454c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
20464c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20474c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
20484c7d4d33SEd Tanous 
20494c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20501ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
20511ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20521ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
20531ef4c342SEd Tanous 
20541ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20551ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
20561ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
20571ef4c342SEd Tanous         // yet
20581ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
20591ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20601ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
206184e12cb7SAppaRao Puli 
20626c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2063ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
20646c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
206520fc307fSGunnar Mills             std::bind_front(handleAccountDelete, std::ref(app)));
206606e086d9SEd Tanous }
206788d16c9aSLewanczyk, Dawid 
206888d16c9aSLewanczyk, Dawid } // namespace redfish
2069