xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 3ce3688aa391659bd9a17baa703aa9eeeb014e24)
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"
191aa375b8SEd Tanous #include "certificate_service.hpp"
203ccb3adbSEd Tanous #include "dbus_utility.hpp"
213ccb3adbSEd Tanous #include "error_messages.hpp"
220ec8b83dSEd Tanous #include "generated/enums/account_service.hpp"
233ccb3adbSEd Tanous #include "persistent_data.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
250ec8b83dSEd Tanous #include "registries/privilege_registry.hpp"
261aa375b8SEd Tanous #include "utils/collection.hpp"
273ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
283ccb3adbSEd Tanous #include "utils/json_utils.hpp"
290ec8b83dSEd Tanous 
301aa375b8SEd Tanous #include <boost/url/format.hpp>
311aa375b8SEd Tanous #include <boost/url/url.hpp>
321e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
33d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
341214b7e7SGunnar Mills 
352b73119cSGeorge Liu #include <array>
361aa375b8SEd Tanous #include <memory>
37c7229815SAbhishek Patel #include <optional>
383544d2a7SEd Tanous #include <ranges>
39c7229815SAbhishek Patel #include <string>
402b73119cSGeorge Liu #include <string_view>
4120fa6a2cSEd Tanous #include <utility>
42c7229815SAbhishek Patel #include <vector>
43c7229815SAbhishek Patel 
441abe55efSEd Tanous namespace redfish
451abe55efSEd Tanous {
4688d16c9aSLewanczyk, Dawid 
4723a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
486973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
492c70f800SEd Tanous constexpr const char* adConfigObject =
50ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
51ab828d7cSRatan Gupta 
52b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
536973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
546973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
556973a582SRatan Gupta constexpr const char* ldapConfigInterface =
566973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
576973a582SRatan Gupta constexpr const char* ldapCreateInterface =
586973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
596973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
6006785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
6106785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
626973a582SRatan Gupta 
6354fc587aSNagaraju Goruganti struct LDAPRoleMapData
6454fc587aSNagaraju Goruganti {
6554fc587aSNagaraju Goruganti     std::string groupName;
6654fc587aSNagaraju Goruganti     std::string privilege;
6754fc587aSNagaraju Goruganti };
6854fc587aSNagaraju Goruganti 
696973a582SRatan Gupta struct LDAPConfigData
706973a582SRatan Gupta {
7147f2934cSEd Tanous     std::string uri;
7247f2934cSEd Tanous     std::string bindDN;
7347f2934cSEd Tanous     std::string baseDN;
7447f2934cSEd Tanous     std::string searchScope;
7547f2934cSEd Tanous     std::string serverType;
766973a582SRatan Gupta     bool serviceEnabled = false;
7747f2934cSEd Tanous     std::string userNameAttribute;
7847f2934cSEd Tanous     std::string groupAttribute;
7954fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
806973a582SRatan Gupta };
816973a582SRatan Gupta 
8254fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
8384e12cb7SAppaRao Puli {
8484e12cb7SAppaRao Puli     if (role == "priv-admin")
8584e12cb7SAppaRao Puli     {
8684e12cb7SAppaRao Puli         return "Administrator";
8784e12cb7SAppaRao Puli     }
883174e4dfSEd Tanous     if (role == "priv-user")
8984e12cb7SAppaRao Puli     {
90c80fee55SAppaRao Puli         return "ReadOnly";
9184e12cb7SAppaRao Puli     }
923174e4dfSEd Tanous     if (role == "priv-operator")
9384e12cb7SAppaRao Puli     {
9484e12cb7SAppaRao Puli         return "Operator";
9584e12cb7SAppaRao Puli     }
9684e12cb7SAppaRao Puli     return "";
9784e12cb7SAppaRao Puli }
9854fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9984e12cb7SAppaRao Puli {
10084e12cb7SAppaRao Puli     if (role == "Administrator")
10184e12cb7SAppaRao Puli     {
10284e12cb7SAppaRao Puli         return "priv-admin";
10384e12cb7SAppaRao Puli     }
1043174e4dfSEd Tanous     if (role == "ReadOnly")
10584e12cb7SAppaRao Puli     {
10684e12cb7SAppaRao Puli         return "priv-user";
10784e12cb7SAppaRao Puli     }
1083174e4dfSEd Tanous     if (role == "Operator")
10984e12cb7SAppaRao Puli     {
11084e12cb7SAppaRao Puli         return "priv-operator";
11184e12cb7SAppaRao Puli     }
11284e12cb7SAppaRao Puli     return "";
11384e12cb7SAppaRao Puli }
114b9b2e0b2SEd Tanous 
115c7229815SAbhishek Patel /**
116c7229815SAbhishek Patel  * @brief Maps user group names retrieved from D-Bus object to
117c7229815SAbhishek Patel  * Account Types.
118c7229815SAbhishek Patel  *
119c7229815SAbhishek Patel  * @param[in] userGroups List of User groups
120c7229815SAbhishek Patel  * @param[out] res AccountTypes populated
121c7229815SAbhishek Patel  *
122c7229815SAbhishek Patel  * @return true in case of success, false if UserGroups contains
123c7229815SAbhishek Patel  * invalid group name(s).
124c7229815SAbhishek Patel  */
125c7229815SAbhishek Patel inline bool translateUserGroup(const std::vector<std::string>& userGroups,
126c7229815SAbhishek Patel                                crow::Response& res)
127c7229815SAbhishek Patel {
128c7229815SAbhishek Patel     std::vector<std::string> accountTypes;
129c7229815SAbhishek Patel     for (const auto& userGroup : userGroups)
130c7229815SAbhishek Patel     {
131c7229815SAbhishek Patel         if (userGroup == "redfish")
132c7229815SAbhishek Patel         {
133c7229815SAbhishek Patel             accountTypes.emplace_back("Redfish");
134c7229815SAbhishek Patel             accountTypes.emplace_back("WebUI");
135c7229815SAbhishek Patel         }
136c7229815SAbhishek Patel         else if (userGroup == "ipmi")
137c7229815SAbhishek Patel         {
138c7229815SAbhishek Patel             accountTypes.emplace_back("IPMI");
139c7229815SAbhishek Patel         }
140c7229815SAbhishek Patel         else if (userGroup == "ssh")
141c7229815SAbhishek Patel         {
142c7229815SAbhishek Patel             accountTypes.emplace_back("ManagerConsole");
143c7229815SAbhishek Patel         }
1443e72c202SNinad Palsule         else if (userGroup == "hostconsole")
1453e72c202SNinad Palsule         {
1463e72c202SNinad Palsule             // The hostconsole group controls who can access the host console
1473e72c202SNinad Palsule             // port via ssh and websocket.
1483e72c202SNinad Palsule             accountTypes.emplace_back("HostConsole");
1493e72c202SNinad Palsule         }
150c7229815SAbhishek Patel         else if (userGroup == "web")
151c7229815SAbhishek Patel         {
152c7229815SAbhishek Patel             // 'web' is one of the valid groups in the UserGroups property of
153c7229815SAbhishek Patel             // the user account in the D-Bus object. This group is currently not
154c7229815SAbhishek Patel             // doing anything, and is considered to be equivalent to 'redfish'.
155c7229815SAbhishek Patel             // 'redfish' user group is mapped to 'Redfish'and 'WebUI'
156c7229815SAbhishek Patel             // AccountTypes, so do nothing here...
157c7229815SAbhishek Patel         }
158c7229815SAbhishek Patel         else
159c7229815SAbhishek Patel         {
1608ece0e45SEd Tanous             // Invalid user group name. Caller throws an exception.
161c7229815SAbhishek Patel             return false;
162c7229815SAbhishek Patel         }
163c7229815SAbhishek Patel     }
164c7229815SAbhishek Patel 
165c7229815SAbhishek Patel     res.jsonValue["AccountTypes"] = std::move(accountTypes);
166c7229815SAbhishek Patel     return true;
167c7229815SAbhishek Patel }
168c7229815SAbhishek Patel 
16958345856SAbhishek Patel /**
17058345856SAbhishek Patel  * @brief Builds User Groups from the Account Types
17158345856SAbhishek Patel  *
17258345856SAbhishek Patel  * @param[in] asyncResp Async Response
17358345856SAbhishek Patel  * @param[in] accountTypes List of Account Types
17458345856SAbhishek Patel  * @param[out] userGroups List of User Groups mapped from Account Types
17558345856SAbhishek Patel  *
17658345856SAbhishek Patel  * @return true if Account Types mapped to User Groups, false otherwise.
17758345856SAbhishek Patel  */
17858345856SAbhishek Patel inline bool
17958345856SAbhishek Patel     getUserGroupFromAccountType(crow::Response& res,
18058345856SAbhishek Patel                                 const std::vector<std::string>& accountTypes,
18158345856SAbhishek Patel                                 std::vector<std::string>& userGroups)
18258345856SAbhishek Patel {
18358345856SAbhishek Patel     // Need both Redfish and WebUI Account Types to map to 'redfish' User Group
18458345856SAbhishek Patel     bool redfishType = false;
18558345856SAbhishek Patel     bool webUIType = false;
18658345856SAbhishek Patel 
18758345856SAbhishek Patel     for (const auto& accountType : accountTypes)
18858345856SAbhishek Patel     {
18958345856SAbhishek Patel         if (accountType == "Redfish")
19058345856SAbhishek Patel         {
19158345856SAbhishek Patel             redfishType = true;
19258345856SAbhishek Patel         }
19358345856SAbhishek Patel         else if (accountType == "WebUI")
19458345856SAbhishek Patel         {
19558345856SAbhishek Patel             webUIType = true;
19658345856SAbhishek Patel         }
19758345856SAbhishek Patel         else if (accountType == "IPMI")
19858345856SAbhishek Patel         {
19958345856SAbhishek Patel             userGroups.emplace_back("ipmi");
20058345856SAbhishek Patel         }
20158345856SAbhishek Patel         else if (accountType == "HostConsole")
20258345856SAbhishek Patel         {
20358345856SAbhishek Patel             userGroups.emplace_back("hostconsole");
20458345856SAbhishek Patel         }
20558345856SAbhishek Patel         else if (accountType == "ManagerConsole")
20658345856SAbhishek Patel         {
20758345856SAbhishek Patel             userGroups.emplace_back("ssh");
20858345856SAbhishek Patel         }
20958345856SAbhishek Patel         else
21058345856SAbhishek Patel         {
21158345856SAbhishek Patel             // Invalid Account Type
21258345856SAbhishek Patel             messages::propertyValueNotInList(res, "AccountTypes", accountType);
21358345856SAbhishek Patel             return false;
21458345856SAbhishek Patel         }
21558345856SAbhishek Patel     }
21658345856SAbhishek Patel 
21758345856SAbhishek Patel     // Both  Redfish and WebUI Account Types are needed to PATCH
21858345856SAbhishek Patel     if (redfishType ^ webUIType)
21958345856SAbhishek Patel     {
22062598e31SEd Tanous         BMCWEB_LOG_ERROR(
22162598e31SEd Tanous             "Missing Redfish or WebUI Account Type to set redfish User Group");
22258345856SAbhishek Patel         messages::strictAccountTypes(res, "AccountTypes");
22358345856SAbhishek Patel         return false;
22458345856SAbhishek Patel     }
22558345856SAbhishek Patel 
22658345856SAbhishek Patel     if (redfishType && webUIType)
22758345856SAbhishek Patel     {
22858345856SAbhishek Patel         userGroups.emplace_back("redfish");
22958345856SAbhishek Patel     }
23058345856SAbhishek Patel 
23158345856SAbhishek Patel     return true;
23258345856SAbhishek Patel }
23358345856SAbhishek Patel 
23458345856SAbhishek Patel /**
23558345856SAbhishek Patel  * @brief Sets UserGroups property of the user based on the Account Types
23658345856SAbhishek Patel  *
23758345856SAbhishek Patel  * @param[in] accountTypes List of User Account Types
23858345856SAbhishek Patel  * @param[in] asyncResp Async Response
23958345856SAbhishek Patel  * @param[in] dbusObjectPath D-Bus Object Path
24058345856SAbhishek Patel  * @param[in] userSelf true if User is updating OWN Account Types
24158345856SAbhishek Patel  */
24258345856SAbhishek Patel inline void
24358345856SAbhishek Patel     patchAccountTypes(const std::vector<std::string>& accountTypes,
24458345856SAbhishek Patel                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24558345856SAbhishek Patel                       const std::string& dbusObjectPath, bool userSelf)
24658345856SAbhishek Patel {
24758345856SAbhishek Patel     // Check if User is disabling own Redfish Account Type
24858345856SAbhishek Patel     if (userSelf &&
24958345856SAbhishek Patel         (accountTypes.cend() ==
25058345856SAbhishek Patel          std::find(accountTypes.cbegin(), accountTypes.cend(), "Redfish")))
25158345856SAbhishek Patel     {
25262598e31SEd Tanous         BMCWEB_LOG_ERROR(
25362598e31SEd Tanous             "User disabling OWN Redfish Account Type is not allowed");
25458345856SAbhishek Patel         messages::strictAccountTypes(asyncResp->res, "AccountTypes");
25558345856SAbhishek Patel         return;
25658345856SAbhishek Patel     }
25758345856SAbhishek Patel 
25858345856SAbhishek Patel     std::vector<std::string> updatedUserGroups;
25958345856SAbhishek Patel     if (!getUserGroupFromAccountType(asyncResp->res, accountTypes,
26058345856SAbhishek Patel                                      updatedUserGroups))
26158345856SAbhishek Patel     {
26258345856SAbhishek Patel         // Problem in mapping Account Types to User Groups, Error already
26358345856SAbhishek Patel         // logged.
26458345856SAbhishek Patel         return;
26558345856SAbhishek Patel     }
266e93abac6SGinu George     setDbusProperty(asyncResp, "AccountTypes",
267e93abac6SGinu George                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
268e93abac6SGinu George                     "xyz.openbmc_project.User.Attributes", "UserGroups",
269e93abac6SGinu George                     updatedUserGroups);
27058345856SAbhishek Patel }
27158345856SAbhishek Patel 
2728d1b46d7Szhanghch05 inline void userErrorMessageHandler(
2738d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2748d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
27566b5ca76Sjayaprakash Mutyala {
27666b5ca76Sjayaprakash Mutyala     if (e == nullptr)
27766b5ca76Sjayaprakash Mutyala     {
27866b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
27966b5ca76Sjayaprakash Mutyala         return;
28066b5ca76Sjayaprakash Mutyala     }
28166b5ca76Sjayaprakash Mutyala 
282055806b3SManojkiran Eda     const char* errorMessage = e->name;
28366b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
28466b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
28566b5ca76Sjayaprakash Mutyala     {
286d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
28766b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
28866b5ca76Sjayaprakash Mutyala     }
28966b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
29066b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
29166b5ca76Sjayaprakash Mutyala     {
292d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
29366b5ca76Sjayaprakash Mutyala     }
294d4d25793SEd Tanous     else if ((strcmp(errorMessage,
295d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
296d4d25793SEd Tanous               0) ||
2970fda0f12SGeorge Liu              (strcmp(
2980fda0f12SGeorge Liu                   errorMessage,
2990fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
3000fda0f12SGeorge Liu               0))
30166b5ca76Sjayaprakash Mutyala     {
30266b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
30366b5ca76Sjayaprakash Mutyala     }
30466b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
30566b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
30666b5ca76Sjayaprakash Mutyala     {
30766b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
30866b5ca76Sjayaprakash Mutyala     }
30966b5ca76Sjayaprakash Mutyala     else
31066b5ca76Sjayaprakash Mutyala     {
311b8ad583fSGunnar Mills         BMCWEB_LOG_ERROR("DBUS response error {}", errorMessage);
31266b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
31366b5ca76Sjayaprakash Mutyala     }
31466b5ca76Sjayaprakash Mutyala }
31566b5ca76Sjayaprakash Mutyala 
31681ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
317ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
318ab828d7cSRatan Gupta                                 const std::string& ldapType)
3196973a582SRatan Gupta {
32049cc263fSEd Tanous     nlohmann::json::object_t ldap;
3211476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
32249cc263fSEd Tanous     nlohmann::json::array_t serviceAddresses;
32349cc263fSEd Tanous     serviceAddresses.emplace_back(confData.uri);
32449cc263fSEd Tanous     ldap["ServiceAddresses"] = std::move(serviceAddresses);
32549cc263fSEd Tanous 
32649cc263fSEd Tanous     nlohmann::json::object_t authentication;
32749cc263fSEd Tanous     authentication["AuthenticationType"] =
3280ec8b83dSEd Tanous         account_service::AuthenticationTypes::UsernameAndPassword;
32949cc263fSEd Tanous     authentication["Username"] = confData.bindDN;
33049cc263fSEd Tanous     authentication["Password"] = nullptr;
33149cc263fSEd Tanous     ldap["Authentication"] = std::move(authentication);
3321476687dSEd Tanous 
33349cc263fSEd Tanous     nlohmann::json::object_t ldapService;
33449cc263fSEd Tanous     nlohmann::json::object_t searchSettings;
33549cc263fSEd Tanous     nlohmann::json::array_t baseDistinguishedNames;
33649cc263fSEd Tanous     baseDistinguishedNames.emplace_back(confData.baseDN);
3371476687dSEd Tanous 
33849cc263fSEd Tanous     searchSettings["BaseDistinguishedNames"] =
33949cc263fSEd Tanous         std::move(baseDistinguishedNames);
34049cc263fSEd Tanous     searchSettings["UsernameAttribute"] = confData.userNameAttribute;
34149cc263fSEd Tanous     searchSettings["GroupsAttribute"] = confData.groupAttribute;
34249cc263fSEd Tanous     ldapService["SearchSettings"] = std::move(searchSettings);
34349cc263fSEd Tanous     ldap["LDAPService"] = std::move(ldapService);
34449cc263fSEd Tanous 
34549cc263fSEd Tanous     nlohmann::json::array_t roleMapArray;
3469eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
34754fc587aSNagaraju Goruganti     {
34862598e31SEd Tanous         BMCWEB_LOG_DEBUG("Pushing the data groupName={}", obj.second.groupName);
349613dabeaSEd Tanous 
350613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
351613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
352329f0348SJorge Cisneros         remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
353329f0348SJorge Cisneros         roleMapArray.emplace_back(std::move(remoteGroup));
35454fc587aSNagaraju Goruganti     }
35549cc263fSEd Tanous 
35649cc263fSEd Tanous     ldap["RemoteRoleMapping"] = std::move(roleMapArray);
35749cc263fSEd Tanous 
35849cc263fSEd Tanous     jsonResponse[ldapType].update(ldap);
3596973a582SRatan Gupta }
3606973a582SRatan Gupta 
3616973a582SRatan Gupta /**
36206785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
36306785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
36406785244SRatan Gupta  *
36506785244SRatan Gupta  */
36623a21a1cSEd Tanous inline void handleRoleMapPatch(
3678d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36806785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
369c1019828SEd Tanous     const std::string& serverType,
370c1019828SEd Tanous     std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input)
37106785244SRatan Gupta {
37206785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
37306785244SRatan Gupta     {
374c1019828SEd Tanous         std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson =
375c1019828SEd Tanous             input[index];
376c1019828SEd Tanous         nlohmann::json::object_t* obj =
377c1019828SEd Tanous             std::get_if<nlohmann::json::object_t>(&thisJson);
378c1019828SEd Tanous         if (obj == nullptr)
37906785244SRatan Gupta         {
38006785244SRatan Gupta             // delete the existing object
38106785244SRatan Gupta             if (index < roleMapObjData.size())
38206785244SRatan Gupta             {
38306785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
38406785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
3855e7e2dc5SEd Tanous                      index](const boost::system::error_code& ec) {
38606785244SRatan Gupta                     if (ec)
38706785244SRatan Gupta                     {
38862598e31SEd Tanous                         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
38906785244SRatan Gupta                         messages::internalError(asyncResp->res);
39006785244SRatan Gupta                         return;
39106785244SRatan Gupta                     }
39289492a15SPatrick Williams                     asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"]
39389492a15SPatrick Williams                                             [index] = nullptr;
39406785244SRatan Gupta                 },
39506785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
39606785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
39706785244SRatan Gupta             }
39806785244SRatan Gupta             else
39906785244SRatan Gupta             {
40062598e31SEd Tanous                 BMCWEB_LOG_ERROR("Can't delete the object");
401c1019828SEd Tanous                 messages::propertyValueTypeError(asyncResp->res, "null",
4022e8c4bdaSEd Tanous                                                  "RemoteRoleMapping/" +
4032e8c4bdaSEd Tanous                                                      std::to_string(index));
40406785244SRatan Gupta                 return;
40506785244SRatan Gupta             }
40606785244SRatan Gupta         }
407c1019828SEd Tanous         else if (obj->empty())
40806785244SRatan Gupta         {
40906785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
41006785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
41106785244SRatan Gupta         }
41206785244SRatan Gupta         else
41306785244SRatan Gupta         {
41406785244SRatan Gupta             // update/create the object
41506785244SRatan Gupta             std::optional<std::string> remoteGroup;
41606785244SRatan Gupta             std::optional<std::string> localRole;
41706785244SRatan Gupta 
418c1019828SEd Tanous             if (!json_util::readJsonObject(*obj, asyncResp->res, "RemoteGroup",
419c1019828SEd Tanous                                            remoteGroup, "LocalRole", localRole))
42006785244SRatan Gupta             {
42106785244SRatan Gupta                 continue;
42206785244SRatan Gupta             }
42306785244SRatan Gupta 
42406785244SRatan Gupta             // Update existing RoleMapping Object
42506785244SRatan Gupta             if (index < roleMapObjData.size())
42606785244SRatan Gupta             {
42762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Update Role Map Object");
42806785244SRatan Gupta                 // If "RemoteGroup" info is provided
42906785244SRatan Gupta                 if (remoteGroup)
43006785244SRatan Gupta                 {
431d02aad39SEd Tanous                     setDbusProperty(
432e93abac6SGinu George                         asyncResp,
433d02aad39SEd Tanous                         std::format("RemoteRoleMapping/{}/RemoteGroup", index),
434e93abac6SGinu George                         ldapDbusService, roleMapObjData[index].first,
435e93abac6SGinu George                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
436e93abac6SGinu George                         "GroupName", *remoteGroup);
43706785244SRatan Gupta                 }
43806785244SRatan Gupta 
43906785244SRatan Gupta                 // If "LocalRole" info is provided
44006785244SRatan Gupta                 if (localRole)
44106785244SRatan Gupta                 {
442d02aad39SEd Tanous                     setDbusProperty(
443e93abac6SGinu George                         asyncResp,
444d02aad39SEd Tanous                         std::format("RemoteRoleMapping/{}/LocalRole", index),
445e93abac6SGinu George                         ldapDbusService, roleMapObjData[index].first,
446e93abac6SGinu George                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
447e93abac6SGinu George                         "Privilege", *localRole);
44806785244SRatan Gupta                 }
44906785244SRatan Gupta             }
45006785244SRatan Gupta             // Create a new RoleMapping Object.
45106785244SRatan Gupta             else
45206785244SRatan Gupta             {
45362598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
45462598e31SEd Tanous                     "setRoleMappingProperties: Creating new Object");
45589492a15SPatrick Williams                 std::string pathString = "RemoteRoleMapping/" +
45689492a15SPatrick Williams                                          std::to_string(index);
45706785244SRatan Gupta 
45806785244SRatan Gupta                 if (!localRole)
45906785244SRatan Gupta                 {
46006785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
46106785244SRatan Gupta                                               pathString + "/LocalRole");
46206785244SRatan Gupta                     continue;
46306785244SRatan Gupta                 }
46406785244SRatan Gupta                 if (!remoteGroup)
46506785244SRatan Gupta                 {
46606785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
46706785244SRatan Gupta                                               pathString + "/RemoteGroup");
46806785244SRatan Gupta                     continue;
46906785244SRatan Gupta                 }
47006785244SRatan Gupta 
47106785244SRatan Gupta                 std::string dbusObjectPath;
47206785244SRatan Gupta                 if (serverType == "ActiveDirectory")
47306785244SRatan Gupta                 {
4742c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
47506785244SRatan Gupta                 }
47606785244SRatan Gupta                 else if (serverType == "LDAP")
47706785244SRatan Gupta                 {
47823a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
47906785244SRatan Gupta                 }
48006785244SRatan Gupta 
48162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Remote Group={},LocalRole={}", *remoteGroup,
48262598e31SEd Tanous                                  *localRole);
48306785244SRatan Gupta 
48406785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
485271584abSEd Tanous                     [asyncResp, serverType, localRole,
4865e7e2dc5SEd Tanous                      remoteGroup](const boost::system::error_code& ec) {
48706785244SRatan Gupta                     if (ec)
48806785244SRatan Gupta                     {
48962598e31SEd Tanous                         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
49006785244SRatan Gupta                         messages::internalError(asyncResp->res);
49106785244SRatan Gupta                         return;
49206785244SRatan Gupta                     }
49306785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
49406785244SRatan Gupta                         asyncResp->res
49506785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
4961476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
4971476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
4981476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
499b2ba3072SPatrick Williams                     remoteRoleJson.emplace_back(std::move(roleMapEntry));
50006785244SRatan Gupta                 },
50106785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
5023174e4dfSEd Tanous                     "Create", *remoteGroup,
50306785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
50406785244SRatan Gupta             }
50506785244SRatan Gupta         }
50606785244SRatan Gupta     }
50706785244SRatan Gupta }
50806785244SRatan Gupta 
50906785244SRatan Gupta /**
5106973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
5116973a582SRatan Gupta  * into JSON
5126973a582SRatan Gupta  */
5136973a582SRatan Gupta template <typename CallbackFunc>
5146973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
5156973a582SRatan Gupta                               CallbackFunc&& callback)
5166973a582SRatan Gupta {
5172b73119cSGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
5182b73119cSGeorge Liu         ldapEnableInterface, ldapConfigInterface};
51954fc587aSNagaraju Goruganti 
5202b73119cSGeorge Liu     dbus::utility::getDbusObject(
5212b73119cSGeorge Liu         ldapConfigObjectName, interfaces,
5228cb2c024SEd Tanous         [callback = std::forward<CallbackFunc>(callback),
523c1019828SEd Tanous          ldapType](const boost::system::error_code& ec,
524c1019828SEd Tanous                    const dbus::utility::MapperGetObject& resp) mutable {
52554fc587aSNagaraju Goruganti         if (ec || resp.empty())
52654fc587aSNagaraju Goruganti         {
527bf2ddedeSCarson Labrado             BMCWEB_LOG_WARNING(
52862598e31SEd Tanous                 "DBUS response error during getting of service name: {}", ec);
52923a21a1cSEd Tanous             LDAPConfigData empty{};
53023a21a1cSEd Tanous             callback(false, empty, ldapType);
53154fc587aSNagaraju Goruganti             return;
53254fc587aSNagaraju Goruganti         }
53354fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
5345eb468daSGeorge Liu         sdbusplus::message::object_path path(ldapRootObject);
5355eb468daSGeorge Liu         dbus::utility::getManagedObjects(
5365eb468daSGeorge Liu             service, path,
537c1019828SEd Tanous             [callback, ldapType](
538c1019828SEd Tanous                 const boost::system::error_code& ec2,
539c1019828SEd Tanous                 const dbus::utility::ManagedObjectType& ldapObjects) mutable {
5406973a582SRatan Gupta             LDAPConfigData confData{};
5418b24275dSEd Tanous             if (ec2)
5426973a582SRatan Gupta             {
543ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
544bf2ddedeSCarson Labrado                 BMCWEB_LOG_WARNING("D-Bus responses error: {}", ec2);
5456973a582SRatan Gupta                 return;
5466973a582SRatan Gupta             }
547ab828d7cSRatan Gupta 
548ab828d7cSRatan Gupta             std::string ldapDbusType;
54954fc587aSNagaraju Goruganti             std::string searchString;
55054fc587aSNagaraju Goruganti 
551ab828d7cSRatan Gupta             if (ldapType == "LDAP")
552ab828d7cSRatan Gupta             {
5530fda0f12SGeorge Liu                 ldapDbusType =
5540fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
55554fc587aSNagaraju Goruganti                 searchString = "openldap";
556ab828d7cSRatan Gupta             }
557ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
558ab828d7cSRatan Gupta             {
55954fc587aSNagaraju Goruganti                 ldapDbusType =
5600fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
56154fc587aSNagaraju Goruganti                 searchString = "active_directory";
562ab828d7cSRatan Gupta             }
563ab828d7cSRatan Gupta             else
564ab828d7cSRatan Gupta             {
56562598e31SEd Tanous                 BMCWEB_LOG_ERROR("Can't get the DbusType for the given type={}",
56662598e31SEd Tanous                                  ldapType);
567ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
568ab828d7cSRatan Gupta                 return;
569ab828d7cSRatan Gupta             }
570ab828d7cSRatan Gupta 
571ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
572ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
573ab828d7cSRatan Gupta 
5746973a582SRatan Gupta             for (const auto& object : ldapObjects)
5756973a582SRatan Gupta             {
57654fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
57754fc587aSNagaraju Goruganti                 // given type
578002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
5796973a582SRatan Gupta                 {
580ab828d7cSRatan Gupta                     continue;
581ab828d7cSRatan Gupta                 }
582ab828d7cSRatan Gupta 
5836973a582SRatan Gupta                 for (const auto& interface : object.second)
5846973a582SRatan Gupta                 {
5856973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
5866973a582SRatan Gupta                     {
5876973a582SRatan Gupta                         // rest of the properties are string.
5886973a582SRatan Gupta                         for (const auto& property : interface.second)
5896973a582SRatan Gupta                         {
5906973a582SRatan Gupta                             if (property.first == "Enabled")
5916973a582SRatan Gupta                             {
5926973a582SRatan Gupta                                 const bool* value =
5936973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
5946973a582SRatan Gupta                                 if (value == nullptr)
5956973a582SRatan Gupta                                 {
5966973a582SRatan Gupta                                     continue;
5976973a582SRatan Gupta                                 }
5986973a582SRatan Gupta                                 confData.serviceEnabled = *value;
5996973a582SRatan Gupta                                 break;
6006973a582SRatan Gupta                             }
6016973a582SRatan Gupta                         }
6026973a582SRatan Gupta                     }
6036973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
6046973a582SRatan Gupta                     {
6056973a582SRatan Gupta                         for (const auto& property : interface.second)
6066973a582SRatan Gupta                         {
607271584abSEd Tanous                             const std::string* strValue =
608002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
609271584abSEd Tanous                             if (strValue == nullptr)
6106973a582SRatan Gupta                             {
6116973a582SRatan Gupta                                 continue;
6126973a582SRatan Gupta                             }
6136973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
6146973a582SRatan Gupta                             {
615271584abSEd Tanous                                 confData.uri = *strValue;
6166973a582SRatan Gupta                             }
6176973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
6186973a582SRatan Gupta                             {
619271584abSEd Tanous                                 confData.bindDN = *strValue;
6206973a582SRatan Gupta                             }
6216973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
6226973a582SRatan Gupta                             {
623271584abSEd Tanous                                 confData.baseDN = *strValue;
6246973a582SRatan Gupta                             }
625002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
6266973a582SRatan Gupta                             {
627271584abSEd Tanous                                 confData.searchScope = *strValue;
6286973a582SRatan Gupta                             }
629002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
6306973a582SRatan Gupta                             {
631271584abSEd Tanous                                 confData.groupAttribute = *strValue;
6326973a582SRatan Gupta                             }
633002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
6346973a582SRatan Gupta                             {
635271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
6366973a582SRatan Gupta                             }
63754fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
638ab828d7cSRatan Gupta                             {
639271584abSEd Tanous                                 confData.serverType = *strValue;
64054fc587aSNagaraju Goruganti                             }
64154fc587aSNagaraju Goruganti                         }
64254fc587aSNagaraju Goruganti                     }
643002d39b4SEd Tanous                     else if (interface.first ==
6440fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
64554fc587aSNagaraju Goruganti                     {
64654fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
64754fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
64854fc587aSNagaraju Goruganti                         {
649271584abSEd Tanous                             const std::string* strValue =
650002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
65154fc587aSNagaraju Goruganti 
652271584abSEd Tanous                             if (strValue == nullptr)
65354fc587aSNagaraju Goruganti                             {
65454fc587aSNagaraju Goruganti                                 continue;
65554fc587aSNagaraju Goruganti                             }
65654fc587aSNagaraju Goruganti 
65754fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
65854fc587aSNagaraju Goruganti                             {
659271584abSEd Tanous                                 roleMapData.groupName = *strValue;
66054fc587aSNagaraju Goruganti                             }
66154fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
66254fc587aSNagaraju Goruganti                             {
663271584abSEd Tanous                                 roleMapData.privilege = *strValue;
66454fc587aSNagaraju Goruganti                             }
66554fc587aSNagaraju Goruganti                         }
66654fc587aSNagaraju Goruganti 
667002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
668002d39b4SEd Tanous                                                             roleMapData);
66954fc587aSNagaraju Goruganti                     }
67054fc587aSNagaraju Goruganti                 }
67154fc587aSNagaraju Goruganti             }
672ab828d7cSRatan Gupta             callback(true, confData, ldapType);
6735eb468daSGeorge Liu         });
6742b73119cSGeorge Liu     });
6756973a582SRatan Gupta }
6766973a582SRatan Gupta 
6778a07d286SRatan Gupta /**
6788a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6798a07d286SRatan Gupta           json response with the new value.
6808a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6818a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6828a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6838a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6848a07d286SRatan Gupta  */
6858a07d286SRatan Gupta 
6864f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6878a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6888d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6898a07d286SRatan Gupta     const std::string& ldapServerElementName,
6908a07d286SRatan Gupta     const std::string& ldapConfigObject)
6918a07d286SRatan Gupta {
692e93abac6SGinu George     setDbusProperty(asyncResp, ldapServerElementName + "/ServiceAddress",
693e93abac6SGinu George                     ldapDbusService, ldapConfigObject, ldapConfigInterface,
694e93abac6SGinu George                     "LDAPServerURI", serviceAddressList.front());
6958a07d286SRatan Gupta }
6968a07d286SRatan Gupta /**
6978a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
6988a07d286SRatan Gupta           json response with the new value.
6998a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
7008a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7018a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7028a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7038a07d286SRatan Gupta  */
7048a07d286SRatan Gupta 
7054f48d5f6SEd Tanous inline void
7064f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
7078d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7088a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7098a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7108a07d286SRatan Gupta {
711e93abac6SGinu George     setDbusProperty(asyncResp,
712d02aad39SEd Tanous                     ldapServerElementName + "/Authentication/Username",
713e93abac6SGinu George                     ldapDbusService, ldapConfigObject, ldapConfigInterface,
714e93abac6SGinu George                     "LDAPBindDN", username);
7158a07d286SRatan Gupta }
7168a07d286SRatan Gupta 
7178a07d286SRatan Gupta /**
7188a07d286SRatan Gupta  * @brief updates the LDAP password
7198a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
7208a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7218a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7228a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
7238a07d286SRatan Gupta  */
7248a07d286SRatan Gupta 
7254f48d5f6SEd Tanous inline void
7264f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
7278d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7288a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7298a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7308a07d286SRatan Gupta {
731e93abac6SGinu George     setDbusProperty(asyncResp,
732d02aad39SEd Tanous                     ldapServerElementName + "/Authentication/Password",
733e93abac6SGinu George                     ldapDbusService, ldapConfigObject, ldapConfigInterface,
734e93abac6SGinu George                     "LDAPBindDNPassword", password);
7358a07d286SRatan Gupta }
7368a07d286SRatan Gupta 
7378a07d286SRatan Gupta /**
7388a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7398a07d286SRatan Gupta           json response with the new value.
7408a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7418a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7428a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7438a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7448a07d286SRatan Gupta  */
7458a07d286SRatan Gupta 
7464f48d5f6SEd Tanous inline void
7474f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7488d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7498a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7508a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7518a07d286SRatan Gupta {
752e93abac6SGinu George     setDbusProperty(asyncResp,
753d02aad39SEd Tanous                     ldapServerElementName +
754d02aad39SEd Tanous                         "/LDAPService/SearchSettings/BaseDistinguishedNames",
755e93abac6SGinu George                     ldapDbusService, ldapConfigObject, ldapConfigInterface,
756e93abac6SGinu George                     "LDAPBaseDN", baseDNList.front());
7578a07d286SRatan Gupta }
7588a07d286SRatan Gupta /**
7598a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
7608a07d286SRatan Gupta           json response with the new value.
7618a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
7628a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7638a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7648a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7658a07d286SRatan Gupta  */
7668a07d286SRatan Gupta 
7674f48d5f6SEd Tanous inline void
7684f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
7698d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7708a07d286SRatan Gupta                             const std::string& ldapServerElementName,
7718a07d286SRatan Gupta                             const std::string& ldapConfigObject)
7728a07d286SRatan Gupta {
773e93abac6SGinu George     setDbusProperty(asyncResp,
774d02aad39SEd Tanous                     ldapServerElementName +
775d02aad39SEd Tanous                         "LDAPService/SearchSettings/UsernameAttribute",
776e93abac6SGinu George                     ldapDbusService, ldapConfigObject, ldapConfigInterface,
777e93abac6SGinu George                     "UserNameAttribute", userNameAttribute);
7788a07d286SRatan Gupta }
7798a07d286SRatan Gupta /**
7808a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
7818a07d286SRatan Gupta           json response with the new value.
7828a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
7838a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7848a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7858a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7868a07d286SRatan Gupta  */
7878a07d286SRatan Gupta 
7884f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
7898d1b46d7Szhanghch05     const std::string& groupsAttribute,
7908d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7918a07d286SRatan Gupta     const std::string& ldapServerElementName,
7928a07d286SRatan Gupta     const std::string& ldapConfigObject)
7938a07d286SRatan Gupta {
794e93abac6SGinu George     setDbusProperty(asyncResp,
795d02aad39SEd Tanous                     ldapServerElementName +
796d02aad39SEd Tanous                         "/LDAPService/SearchSettings/GroupsAttribute",
797e93abac6SGinu George                     ldapDbusService, ldapConfigObject, ldapConfigInterface,
798e93abac6SGinu George                     "GroupNameAttribute", groupsAttribute);
7998a07d286SRatan Gupta }
8008a07d286SRatan Gupta /**
8018a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
8028a07d286SRatan Gupta           json response with the new value.
8038a07d286SRatan Gupta  * @param input JSON data.
8048a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8058a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8068a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8078a07d286SRatan Gupta  */
8088a07d286SRatan Gupta 
8094f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8106c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8118a07d286SRatan Gupta     const std::string& ldapServerElementName,
8128a07d286SRatan Gupta     const std::string& ldapConfigObject)
8138a07d286SRatan Gupta {
814e93abac6SGinu George     setDbusProperty(asyncResp, ldapServerElementName + "/ServiceEnabled",
815e93abac6SGinu George                     ldapDbusService, ldapConfigObject, ldapEnableInterface,
816e93abac6SGinu George                     "Enabled", serviceEnabled);
8178a07d286SRatan Gupta }
8188a07d286SRatan Gupta 
819c1019828SEd Tanous struct AuthMethods
82078158631SZbigniew Kurzynski {
82178158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
82278158631SZbigniew Kurzynski     std::optional<bool> cookie;
82378158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
82478158631SZbigniew Kurzynski     std::optional<bool> xToken;
825501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
826c1019828SEd Tanous };
82778158631SZbigniew Kurzynski 
828c1019828SEd Tanous inline void
829c1019828SEd Tanous     handleAuthMethodsPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
830c1019828SEd Tanous                            const AuthMethods& auth)
83178158631SZbigniew Kurzynski {
832c1019828SEd Tanous     persistent_data::AuthConfigMethods& authMethodsConfig =
83352cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
83478158631SZbigniew Kurzynski 
835c1019828SEd Tanous     if (auth.basicAuth)
83678158631SZbigniew Kurzynski     {
83725b54dbaSEd Tanous         if constexpr (!BMCWEB_BASIC_AUTH)
83825b54dbaSEd Tanous         {
839f16f6263SAlan Kuo             messages::actionNotSupported(
8400fda0f12SGeorge Liu                 asyncResp->res,
8410fda0f12SGeorge Liu                 "Setting BasicAuth when basic-auth feature is disabled");
842f16f6263SAlan Kuo             return;
84325b54dbaSEd Tanous         }
84425b54dbaSEd Tanous 
845c1019828SEd Tanous         authMethodsConfig.basic = *auth.basicAuth;
84678158631SZbigniew Kurzynski     }
84778158631SZbigniew Kurzynski 
848c1019828SEd Tanous     if (auth.cookie)
84978158631SZbigniew Kurzynski     {
85025b54dbaSEd Tanous         if constexpr (!BMCWEB_COOKIE_AUTH)
85125b54dbaSEd Tanous         {
8520fda0f12SGeorge Liu             messages::actionNotSupported(
8530fda0f12SGeorge Liu                 asyncResp->res,
8540fda0f12SGeorge Liu                 "Setting Cookie when cookie-auth feature is disabled");
855f16f6263SAlan Kuo             return;
85625b54dbaSEd Tanous         }
857c1019828SEd Tanous         authMethodsConfig.cookie = *auth.cookie;
85878158631SZbigniew Kurzynski     }
85978158631SZbigniew Kurzynski 
860c1019828SEd Tanous     if (auth.sessionToken)
86178158631SZbigniew Kurzynski     {
86225b54dbaSEd Tanous         if constexpr (!BMCWEB_SESSION_AUTH)
86325b54dbaSEd Tanous         {
864f16f6263SAlan Kuo             messages::actionNotSupported(
8650fda0f12SGeorge Liu                 asyncResp->res,
8660fda0f12SGeorge Liu                 "Setting SessionToken when session-auth feature is disabled");
867f16f6263SAlan Kuo             return;
86825b54dbaSEd Tanous         }
869c1019828SEd Tanous         authMethodsConfig.sessionToken = *auth.sessionToken;
87078158631SZbigniew Kurzynski     }
87178158631SZbigniew Kurzynski 
872c1019828SEd Tanous     if (auth.xToken)
87378158631SZbigniew Kurzynski     {
87425b54dbaSEd Tanous         if constexpr (!BMCWEB_XTOKEN_AUTH)
87525b54dbaSEd Tanous         {
8760fda0f12SGeorge Liu             messages::actionNotSupported(
8770fda0f12SGeorge Liu                 asyncResp->res,
8780fda0f12SGeorge Liu                 "Setting XToken when xtoken-auth feature is disabled");
879f16f6263SAlan Kuo             return;
88025b54dbaSEd Tanous         }
881c1019828SEd Tanous         authMethodsConfig.xtoken = *auth.xToken;
88278158631SZbigniew Kurzynski     }
88378158631SZbigniew Kurzynski 
884c1019828SEd Tanous     if (auth.tls)
885501f1e58SZbigniew Kurzynski     {
88625b54dbaSEd Tanous         if constexpr (!BMCWEB_MUTUAL_TLS_AUTH)
88725b54dbaSEd Tanous         {
8880fda0f12SGeorge Liu             messages::actionNotSupported(
8890fda0f12SGeorge Liu                 asyncResp->res,
8900fda0f12SGeorge Liu                 "Setting TLS when mutual-tls-auth feature is disabled");
891f16f6263SAlan Kuo             return;
89225b54dbaSEd Tanous         }
893c1019828SEd Tanous         authMethodsConfig.tls = *auth.tls;
894501f1e58SZbigniew Kurzynski     }
895501f1e58SZbigniew Kurzynski 
89678158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
897501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
898501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
89978158631SZbigniew Kurzynski     {
90078158631SZbigniew Kurzynski         // Do not allow user to disable everything
90178158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
90278158631SZbigniew Kurzynski                                      "of disabling all available methods");
90378158631SZbigniew Kurzynski         return;
90478158631SZbigniew Kurzynski     }
90578158631SZbigniew Kurzynski 
90652cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
90752cc112dSEd Tanous         authMethodsConfig);
90878158631SZbigniew Kurzynski     // Save configuration immediately
90952cc112dSEd Tanous     persistent_data::getConfig().writeData();
91078158631SZbigniew Kurzynski 
91178158631SZbigniew Kurzynski     messages::success(asyncResp->res);
91278158631SZbigniew Kurzynski }
91378158631SZbigniew Kurzynski 
9148a07d286SRatan Gupta /**
9158a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
9168a07d286SRatan Gupta  *        value and create the LDAP config object.
9178a07d286SRatan Gupta  * @param input JSON data
9188a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9198a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
9208a07d286SRatan Gupta  */
9218a07d286SRatan Gupta 
92210cb44f3SEd Tanous struct LdapPatchParams
92310cb44f3SEd Tanous {
92410cb44f3SEd Tanous     std::optional<std::string> authType;
92510cb44f3SEd Tanous     std::optional<std::vector<std::string>> serviceAddressList;
92610cb44f3SEd Tanous     std::optional<bool> serviceEnabled;
92710cb44f3SEd Tanous     std::optional<std::vector<std::string>> baseDNList;
92810cb44f3SEd Tanous     std::optional<std::string> userNameAttribute;
92910cb44f3SEd Tanous     std::optional<std::string> groupsAttribute;
93010cb44f3SEd Tanous     std::optional<std::string> userName;
93110cb44f3SEd Tanous     std::optional<std::string> password;
93210cb44f3SEd Tanous     std::optional<
93310cb44f3SEd Tanous         std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
93410cb44f3SEd Tanous         remoteRoleMapData;
93510cb44f3SEd Tanous };
93610cb44f3SEd Tanous 
93710cb44f3SEd Tanous inline void handleLDAPPatch(LdapPatchParams&& input,
9388d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9398a07d286SRatan Gupta                             const std::string& serverType)
9408a07d286SRatan Gupta {
941eb2bbe56SRatan Gupta     std::string dbusObjectPath;
942eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
943eb2bbe56SRatan Gupta     {
9442c70f800SEd Tanous         dbusObjectPath = adConfigObject;
945eb2bbe56SRatan Gupta     }
946eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
947eb2bbe56SRatan Gupta     {
94823a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
949eb2bbe56SRatan Gupta     }
950cb13a392SEd Tanous     else
951cb13a392SEd Tanous     {
95210cb44f3SEd Tanous         BMCWEB_LOG_ERROR("serverType wasn't AD or LDAP but was {}????",
95310cb44f3SEd Tanous                          serverType);
954cb13a392SEd Tanous         return;
955cb13a392SEd Tanous     }
956eb2bbe56SRatan Gupta 
95710cb44f3SEd Tanous     if (input.authType && *input.authType != "UsernameAndPassword")
9588a07d286SRatan Gupta     {
95910cb44f3SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *input.authType,
960c1019828SEd Tanous                                          "AuthenticationType");
961c1019828SEd Tanous         return;
9628a07d286SRatan Gupta     }
963c1019828SEd Tanous 
96410cb44f3SEd Tanous     if (input.serviceAddressList)
9658a07d286SRatan Gupta     {
96610cb44f3SEd Tanous         if (input.serviceAddressList->empty())
9678a07d286SRatan Gupta         {
968e2616cc5SEd Tanous             messages::propertyValueNotInList(
96910cb44f3SEd Tanous                 asyncResp->res, *input.serviceAddressList, "ServiceAddress");
9708a07d286SRatan Gupta             return;
9718a07d286SRatan Gupta         }
9728a07d286SRatan Gupta     }
97310cb44f3SEd Tanous     if (input.baseDNList)
9748a07d286SRatan Gupta     {
97510cb44f3SEd Tanous         if (input.baseDNList->empty())
9768a07d286SRatan Gupta         {
97710cb44f3SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *input.baseDNList,
9788a07d286SRatan Gupta                                              "BaseDistinguishedNames");
9798a07d286SRatan Gupta             return;
9808a07d286SRatan Gupta         }
9818a07d286SRatan Gupta     }
9828a07d286SRatan Gupta 
9838a07d286SRatan Gupta     // nothing to update, then return
98410cb44f3SEd Tanous     if (!input.userName && !input.password && !input.serviceAddressList &&
98510cb44f3SEd Tanous         !input.baseDNList && !input.userNameAttribute &&
98610cb44f3SEd Tanous         !input.groupsAttribute && !input.serviceEnabled &&
98710cb44f3SEd Tanous         !input.remoteRoleMapData)
9888a07d286SRatan Gupta     {
9898a07d286SRatan Gupta         return;
9908a07d286SRatan Gupta     }
9918a07d286SRatan Gupta 
9928a07d286SRatan Gupta     // Get the existing resource first then keep modifying
9938a07d286SRatan Gupta     // whenever any property gets updated.
99410cb44f3SEd Tanous     getLDAPConfigData(serverType,
99510cb44f3SEd Tanous                       [asyncResp, input = std::move(input),
99610cb44f3SEd Tanous                        dbusObjectPath = std::move(dbusObjectPath)](
99710cb44f3SEd Tanous                           bool success, const LDAPConfigData& confData,
998c1019828SEd Tanous                           const std::string& serverT) mutable {
9998a07d286SRatan Gupta         if (!success)
10008a07d286SRatan Gupta         {
10018a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10028a07d286SRatan Gupta             return;
10038a07d286SRatan Gupta         }
10046c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
10058a07d286SRatan Gupta         if (confData.serviceEnabled)
10068a07d286SRatan Gupta         {
10078a07d286SRatan Gupta             // Disable the service first and update the rest of
10088a07d286SRatan Gupta             // the properties.
10096c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
10108a07d286SRatan Gupta         }
10118a07d286SRatan Gupta 
101210cb44f3SEd Tanous         if (input.serviceAddressList)
10138a07d286SRatan Gupta         {
101410cb44f3SEd Tanous             handleServiceAddressPatch(*input.serviceAddressList, asyncResp,
101510cb44f3SEd Tanous                                       serverT, dbusObjectPath);
101610cb44f3SEd Tanous         }
101710cb44f3SEd Tanous         if (input.userName)
101810cb44f3SEd Tanous         {
101910cb44f3SEd Tanous             handleUserNamePatch(*input.userName, asyncResp, serverT,
10206c51eab1SEd Tanous                                 dbusObjectPath);
10218a07d286SRatan Gupta         }
102210cb44f3SEd Tanous         if (input.password)
10238a07d286SRatan Gupta         {
102410cb44f3SEd Tanous             handlePasswordPatch(*input.password, asyncResp, serverT,
102510cb44f3SEd Tanous                                 dbusObjectPath);
10268a07d286SRatan Gupta         }
10278a07d286SRatan Gupta 
102810cb44f3SEd Tanous         if (input.baseDNList)
10298a07d286SRatan Gupta         {
103010cb44f3SEd Tanous             handleBaseDNPatch(*input.baseDNList, asyncResp, serverT,
10316c51eab1SEd Tanous                               dbusObjectPath);
10328a07d286SRatan Gupta         }
103310cb44f3SEd Tanous         if (input.userNameAttribute)
10348a07d286SRatan Gupta         {
103510cb44f3SEd Tanous             handleUserNameAttrPatch(*input.userNameAttribute, asyncResp,
103610cb44f3SEd Tanous                                     serverT, dbusObjectPath);
103710cb44f3SEd Tanous         }
103810cb44f3SEd Tanous         if (input.groupsAttribute)
103910cb44f3SEd Tanous         {
104010cb44f3SEd Tanous             handleGroupNameAttrPatch(*input.groupsAttribute, asyncResp, serverT,
10416c51eab1SEd Tanous                                      dbusObjectPath);
10428a07d286SRatan Gupta         }
104310cb44f3SEd Tanous         if (input.serviceEnabled)
10448a07d286SRatan Gupta         {
10458a07d286SRatan Gupta             // if user has given the value as true then enable
10468a07d286SRatan Gupta             // the service. if user has given false then no-op
10478a07d286SRatan Gupta             // as service is already stopped.
104810cb44f3SEd Tanous             if (*input.serviceEnabled)
10498a07d286SRatan Gupta             {
105010cb44f3SEd Tanous                 handleServiceEnablePatch(*input.serviceEnabled, asyncResp,
105110cb44f3SEd Tanous                                          serverT, dbusObjectPath);
10528a07d286SRatan Gupta             }
10538a07d286SRatan Gupta         }
10548a07d286SRatan Gupta         else
10558a07d286SRatan Gupta         {
10568a07d286SRatan Gupta             // if user has not given the service enabled value
10578a07d286SRatan Gupta             // then revert it to the same state as it was
10588a07d286SRatan Gupta             // before.
10598a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
106023a21a1cSEd Tanous                                      serverT, dbusObjectPath);
10618a07d286SRatan Gupta         }
106206785244SRatan Gupta 
106310cb44f3SEd Tanous         if (input.remoteRoleMapData)
106406785244SRatan Gupta         {
10656c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
106610cb44f3SEd Tanous                                *input.remoteRoleMapData);
106706785244SRatan Gupta         }
10688a07d286SRatan Gupta     });
10698a07d286SRatan Gupta }
1070d4b5443fSEd Tanous 
107158345856SAbhishek Patel inline void updateUserProperties(
107258345856SAbhishek Patel     std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& username,
1073618c14b4SEd Tanous     const std::optional<std::string>& password,
1074618c14b4SEd Tanous     const std::optional<bool>& enabled,
107558345856SAbhishek Patel     const std::optional<std::string>& roleId, const std::optional<bool>& locked,
1076e518ef32SRavi Teja     std::optional<std::vector<std::string>> accountTypes, bool userSelf,
1077e518ef32SRavi Teja     const std::shared_ptr<persistent_data::UserSession>& session)
10781abe55efSEd Tanous {
1079b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1080b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1081b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
10826c51eab1SEd Tanous 
10836c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1084e518ef32SRavi Teja         dbusObjectPath,
1085e518ef32SRavi Teja         [dbusObjectPath, username, password, roleId, enabled, locked,
1086e518ef32SRavi Teja          accountTypes(std::move(accountTypes)), userSelf, session,
1087e518ef32SRavi Teja          asyncResp{std::move(asyncResp)}](int rc) {
1088e662eae8SEd Tanous         if (rc <= 0)
10896c51eab1SEd Tanous         {
1090d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
10916c51eab1SEd Tanous                                        username);
10926c51eab1SEd Tanous             return;
10936c51eab1SEd Tanous         }
10946c51eab1SEd Tanous 
10956c51eab1SEd Tanous         if (password)
10966c51eab1SEd Tanous         {
10976c51eab1SEd Tanous             int retval = pamUpdatePassword(username, *password);
10986c51eab1SEd Tanous 
10996c51eab1SEd Tanous             if (retval == PAM_USER_UNKNOWN)
11006c51eab1SEd Tanous             {
1101d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11026c51eab1SEd Tanous                                            username);
11036c51eab1SEd Tanous             }
11046c51eab1SEd Tanous             else if (retval == PAM_AUTHTOK_ERR)
11056c51eab1SEd Tanous             {
11066c51eab1SEd Tanous                 // If password is invalid
11079bd80831SJason M. Bills                 messages::propertyValueFormatError(asyncResp->res, nullptr,
11089bd80831SJason M. Bills                                                    "Password");
110962598e31SEd Tanous                 BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
11106c51eab1SEd Tanous             }
11116c51eab1SEd Tanous             else if (retval != PAM_SUCCESS)
11126c51eab1SEd Tanous             {
11136c51eab1SEd Tanous                 messages::internalError(asyncResp->res);
11146c51eab1SEd Tanous                 return;
11156c51eab1SEd Tanous             }
1116e7b1b62bSEd Tanous             else
1117e7b1b62bSEd Tanous             {
1118e518ef32SRavi Teja                 // Remove existing sessions of the user when password changed
1119e518ef32SRavi Teja                 persistent_data::SessionStore::getInstance()
1120e518ef32SRavi Teja                     .removeSessionsByUsernameExceptSession(username, session);
1121e7b1b62bSEd Tanous                 messages::success(asyncResp->res);
1122e7b1b62bSEd Tanous             }
11236c51eab1SEd Tanous         }
11246c51eab1SEd Tanous 
11256c51eab1SEd Tanous         if (enabled)
11266c51eab1SEd Tanous         {
1127e93abac6SGinu George             setDbusProperty(asyncResp, "Enabled",
1128e93abac6SGinu George                             "xyz.openbmc_project.User.Manager", dbusObjectPath,
1129d02aad39SEd Tanous                             "xyz.openbmc_project.User.Attributes",
1130e93abac6SGinu George                             "UserEnabled", *enabled);
11316c51eab1SEd Tanous         }
11326c51eab1SEd Tanous 
11336c51eab1SEd Tanous         if (roleId)
11346c51eab1SEd Tanous         {
11356c51eab1SEd Tanous             std::string priv = getPrivilegeFromRoleId(*roleId);
11366c51eab1SEd Tanous             if (priv.empty())
11376c51eab1SEd Tanous             {
1138e2616cc5SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, true,
1139e2616cc5SEd Tanous                                                  "Locked");
11406c51eab1SEd Tanous                 return;
11416c51eab1SEd Tanous             }
1142e93abac6SGinu George             setDbusProperty(asyncResp, "RoleId",
1143e93abac6SGinu George                             "xyz.openbmc_project.User.Manager", dbusObjectPath,
1144d02aad39SEd Tanous                             "xyz.openbmc_project.User.Attributes",
1145e93abac6SGinu George                             "UserPrivilege", priv);
11466c51eab1SEd Tanous         }
11476c51eab1SEd Tanous 
11486c51eab1SEd Tanous         if (locked)
11496c51eab1SEd Tanous         {
11506c51eab1SEd Tanous             // admin can unlock the account which is locked by
11516c51eab1SEd Tanous             // successive authentication failures but admin should
11526c51eab1SEd Tanous             // not be allowed to lock an account.
11536c51eab1SEd Tanous             if (*locked)
11546c51eab1SEd Tanous             {
11556c51eab1SEd Tanous                 messages::propertyValueNotInList(asyncResp->res, "true",
11566c51eab1SEd Tanous                                                  "Locked");
11576c51eab1SEd Tanous                 return;
11586c51eab1SEd Tanous             }
1159e93abac6SGinu George             setDbusProperty(asyncResp, "Locked",
1160e93abac6SGinu George                             "xyz.openbmc_project.User.Manager", dbusObjectPath,
11619ae226faSGeorge Liu                             "xyz.openbmc_project.User.Attributes",
1162e93abac6SGinu George                             "UserLockedForFailedAttempt", *locked);
11636c51eab1SEd Tanous         }
116458345856SAbhishek Patel 
116558345856SAbhishek Patel         if (accountTypes)
116658345856SAbhishek Patel         {
116758345856SAbhishek Patel             patchAccountTypes(*accountTypes, asyncResp, dbusObjectPath,
116858345856SAbhishek Patel                               userSelf);
116958345856SAbhishek Patel         }
11706c51eab1SEd Tanous     });
11716c51eab1SEd Tanous }
11726c51eab1SEd Tanous 
11734c7d4d33SEd Tanous inline void handleAccountServiceHead(
11744c7d4d33SEd Tanous     App& app, const crow::Request& req,
11751ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
11766c51eab1SEd Tanous {
11773ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
117845ca1b86SEd Tanous     {
117945ca1b86SEd Tanous         return;
118045ca1b86SEd Tanous     }
11814c7d4d33SEd Tanous     asyncResp->res.addHeader(
11824c7d4d33SEd Tanous         boost::beast::http::field::link,
11834c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
11844c7d4d33SEd Tanous }
11854c7d4d33SEd Tanous 
11864c7d4d33SEd Tanous inline void
11871aa375b8SEd Tanous     getClientCertificates(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
11881aa375b8SEd Tanous                           const nlohmann::json::json_pointer& keyLocation)
11891aa375b8SEd Tanous {
11901aa375b8SEd Tanous     boost::urls::url url(
11911aa375b8SEd Tanous         "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates");
11921aa375b8SEd Tanous     std::array<std::string_view, 1> interfaces = {
11931aa375b8SEd Tanous         "xyz.openbmc_project.Certs.Certificate"};
11941aa375b8SEd Tanous     std::string path = "/xyz/openbmc_project/certs/authority/truststore";
11951aa375b8SEd Tanous 
11961aa375b8SEd Tanous     collection_util::getCollectionToKey(asyncResp, url, interfaces, path,
11971aa375b8SEd Tanous                                         keyLocation);
11981aa375b8SEd Tanous }
11991aa375b8SEd Tanous 
12001aa375b8SEd Tanous inline void handleAccountServiceClientCertificatesInstanceHead(
12011aa375b8SEd Tanous     App& app, const crow::Request& req,
12021aa375b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12031aa375b8SEd Tanous     const std::string& /*id*/)
12041aa375b8SEd Tanous {
12051aa375b8SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12061aa375b8SEd Tanous     {
12071aa375b8SEd Tanous         return;
12081aa375b8SEd Tanous     }
12091aa375b8SEd Tanous 
12101aa375b8SEd Tanous     asyncResp->res.addHeader(
12111aa375b8SEd Tanous         boost::beast::http::field::link,
12121aa375b8SEd Tanous         "</redfish/v1/JsonSchemas/Certificate/Certificate.json>; rel=describedby");
12131aa375b8SEd Tanous }
12141aa375b8SEd Tanous 
12151aa375b8SEd Tanous inline void handleAccountServiceClientCertificatesInstanceGet(
12161aa375b8SEd Tanous     App& app, const crow::Request& req,
12171aa375b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
12181aa375b8SEd Tanous {
12191aa375b8SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12201aa375b8SEd Tanous     {
12211aa375b8SEd Tanous         return;
12221aa375b8SEd Tanous     }
12231aa375b8SEd Tanous     BMCWEB_LOG_DEBUG("ClientCertificate Certificate ID={}", id);
12241aa375b8SEd Tanous     const boost::urls::url certURL = boost::urls::format(
12251aa375b8SEd Tanous         "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates/{}",
12261aa375b8SEd Tanous         id);
12271aa375b8SEd Tanous     std::string objPath =
12281aa375b8SEd Tanous         sdbusplus::message::object_path(certs::authorityObjectPath) / id;
12291aa375b8SEd Tanous     getCertificateProperties(
12301aa375b8SEd Tanous         asyncResp, objPath,
12311aa375b8SEd Tanous         "xyz.openbmc_project.Certs.Manager.Authority.Truststore", id, certURL,
12321aa375b8SEd Tanous         "Client Certificate");
12331aa375b8SEd Tanous }
12341aa375b8SEd Tanous 
12351aa375b8SEd Tanous inline void handleAccountServiceClientCertificatesHead(
12361aa375b8SEd Tanous     App& app, const crow::Request& req,
12371aa375b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12381aa375b8SEd Tanous {
12391aa375b8SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12401aa375b8SEd Tanous     {
12411aa375b8SEd Tanous         return;
12421aa375b8SEd Tanous     }
12431aa375b8SEd Tanous 
12441aa375b8SEd Tanous     asyncResp->res.addHeader(
12451aa375b8SEd Tanous         boost::beast::http::field::link,
12461aa375b8SEd Tanous         "</redfish/v1/JsonSchemas/CertificateCollection/CertificateCollection.json>; rel=describedby");
12471aa375b8SEd Tanous }
12481aa375b8SEd Tanous 
12491aa375b8SEd Tanous inline void handleAccountServiceClientCertificatesGet(
12501aa375b8SEd Tanous     App& app, const crow::Request& req,
12511aa375b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12521aa375b8SEd Tanous {
12531aa375b8SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12541aa375b8SEd Tanous     {
12551aa375b8SEd Tanous         return;
12561aa375b8SEd Tanous     }
12571aa375b8SEd Tanous     getClientCertificates(asyncResp, "/Members"_json_pointer);
12581aa375b8SEd Tanous }
12591aa375b8SEd Tanous 
1260*3ce3688aSEd Tanous using account_service::CertificateMappingAttribute;
1261*3ce3688aSEd Tanous using persistent_data::MTLSCommonNameParseMode;
1262*3ce3688aSEd Tanous inline CertificateMappingAttribute
1263*3ce3688aSEd Tanous     getCertificateMapping(MTLSCommonNameParseMode parse)
1264*3ce3688aSEd Tanous {
1265*3ce3688aSEd Tanous     switch (parse)
1266*3ce3688aSEd Tanous     {
1267*3ce3688aSEd Tanous         case MTLSCommonNameParseMode::CommonName:
1268*3ce3688aSEd Tanous         {
1269*3ce3688aSEd Tanous             return CertificateMappingAttribute::CommonName;
1270*3ce3688aSEd Tanous         }
1271*3ce3688aSEd Tanous         break;
1272*3ce3688aSEd Tanous         case MTLSCommonNameParseMode::Whole:
1273*3ce3688aSEd Tanous         {
1274*3ce3688aSEd Tanous             return CertificateMappingAttribute::Whole;
1275*3ce3688aSEd Tanous         }
1276*3ce3688aSEd Tanous         break;
1277*3ce3688aSEd Tanous         case MTLSCommonNameParseMode::UserPrincipalName:
1278*3ce3688aSEd Tanous         {
1279*3ce3688aSEd Tanous             return CertificateMappingAttribute::UserPrincipalName;
1280*3ce3688aSEd Tanous         }
1281*3ce3688aSEd Tanous         break;
1282*3ce3688aSEd Tanous 
1283*3ce3688aSEd Tanous         case MTLSCommonNameParseMode::Meta:
1284*3ce3688aSEd Tanous         {
1285*3ce3688aSEd Tanous             if constexpr (BMCWEB_META_TLS_COMMON_NAME_PARSING)
1286*3ce3688aSEd Tanous             {
1287*3ce3688aSEd Tanous                 return CertificateMappingAttribute::CommonName;
1288*3ce3688aSEd Tanous             }
1289*3ce3688aSEd Tanous         }
1290*3ce3688aSEd Tanous         break;
1291*3ce3688aSEd Tanous         default:
1292*3ce3688aSEd Tanous         {
1293*3ce3688aSEd Tanous             return CertificateMappingAttribute::Invalid;
1294*3ce3688aSEd Tanous         }
1295*3ce3688aSEd Tanous         break;
1296*3ce3688aSEd Tanous     }
1297*3ce3688aSEd Tanous }
1298*3ce3688aSEd Tanous 
12991aa375b8SEd Tanous inline void
13004c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
13014c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13024c7d4d33SEd Tanous {
1303afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1304afd369c6SJiaqing Zhao     {
1305afd369c6SJiaqing Zhao         return;
1306afd369c6SJiaqing Zhao     }
13073e72c202SNinad Palsule 
13083e72c202SNinad Palsule     if (req.session == nullptr)
13093e72c202SNinad Palsule     {
13103e72c202SNinad Palsule         messages::internalError(asyncResp->res);
13113e72c202SNinad Palsule         return;
13123e72c202SNinad Palsule     }
13133e72c202SNinad Palsule 
1314c1019828SEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
1315c1019828SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
1316c1019828SEd Tanous 
1317afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1318afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1319afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1320afd369c6SJiaqing Zhao 
13211476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
13221476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
1323482a69e7SRavi Teja     json["@odata.type"] = "#AccountService.v1_15_0.AccountService";
13241476687dSEd Tanous     json["Id"] = "AccountService";
13251476687dSEd Tanous     json["Name"] = "Account Service";
13261476687dSEd Tanous     json["Description"] = "Account Service";
13271476687dSEd Tanous     json["ServiceEnabled"] = true;
13281476687dSEd Tanous     json["MaxPasswordLength"] = 20;
13291ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
13301476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
1331482a69e7SRavi Teja     json["HTTPBasicAuth"] = authMethodsConfig.basic
1332482a69e7SRavi Teja                                 ? account_service::BasicAuthState::Enabled
1333482a69e7SRavi Teja                                 : account_service::BasicAuthState::Disabled;
1334482a69e7SRavi Teja 
1335482a69e7SRavi Teja     nlohmann::json::array_t allowed;
1336482a69e7SRavi Teja     allowed.emplace_back(account_service::BasicAuthState::Enabled);
1337482a69e7SRavi Teja     allowed.emplace_back(account_service::BasicAuthState::Disabled);
1338482a69e7SRavi Teja     json["HTTPBasicAuth@AllowableValues"] = std::move(allowed);
1339482a69e7SRavi Teja 
13401aa375b8SEd Tanous     nlohmann::json::object_t clientCertificate;
13411aa375b8SEd Tanous     clientCertificate["Enabled"] = authMethodsConfig.tls;
13421aa375b8SEd Tanous     clientCertificate["RespondToUnauthenticatedClients"] = true;
1343*3ce3688aSEd Tanous 
1344*3ce3688aSEd Tanous     using account_service::CertificateMappingAttribute;
1345*3ce3688aSEd Tanous 
1346*3ce3688aSEd Tanous     CertificateMappingAttribute mapping =
1347*3ce3688aSEd Tanous         getCertificateMapping(authMethodsConfig.mTLSCommonNameParsingMode);
1348*3ce3688aSEd Tanous     if (mapping == CertificateMappingAttribute::Invalid)
1349*3ce3688aSEd Tanous     {
1350*3ce3688aSEd Tanous         messages::internalError(asyncResp->res);
1351*3ce3688aSEd Tanous     }
1352*3ce3688aSEd Tanous     else
1353*3ce3688aSEd Tanous     {
1354*3ce3688aSEd Tanous         clientCertificate["CertificateMappingAttribute"] = mapping;
1355*3ce3688aSEd Tanous     }
13561aa375b8SEd Tanous     nlohmann::json::object_t certificates;
13571aa375b8SEd Tanous     certificates["@odata.id"] =
13581aa375b8SEd Tanous         "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates";
13591aa375b8SEd Tanous     certificates["@odata.type"] =
13601aa375b8SEd Tanous         "#CertificateCollection.CertificateCollection";
13611aa375b8SEd Tanous     clientCertificate["Certificates"] = std::move(certificates);
13621aa375b8SEd Tanous     json["MultiFactorAuth"]["ClientCertificate"] = std::move(clientCertificate);
13631aa375b8SEd Tanous 
13641aa375b8SEd Tanous     getClientCertificates(
13651aa375b8SEd Tanous         asyncResp,
13661aa375b8SEd Tanous         "/MultiFactorAuth/ClientCertificate/Certificates/Members"_json_pointer);
13671aa375b8SEd Tanous 
13681476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
13695b5574acSEd Tanous         "#OpenBMCAccountService.v1_0_0.AccountService";
13701476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
13711476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
13721476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
13731476687dSEd Tanous         authMethodsConfig.basic;
13741476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
13751476687dSEd Tanous         authMethodsConfig.sessionToken;
13761ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
13771ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
13781ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
13791476687dSEd Tanous 
13801ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
13811ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
13821ef4c342SEd Tanous     // permissions ConfigureManager
138372048780SAbhishek Patel     Privileges effectiveUserPrivileges =
13843e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
138572048780SAbhishek Patel 
138672048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
138772048780SAbhishek Patel                                          effectiveUserPrivileges))
138872048780SAbhishek Patel     {
13891ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
13901476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
139172048780SAbhishek Patel     }
1392d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1393d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1394d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
13955e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
13961ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
13973d958bbcSAppaRao Puli         if (ec)
13983d958bbcSAppaRao Puli         {
13993d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
14003d958bbcSAppaRao Puli             return;
14013d958bbcSAppaRao Puli         }
1402d1bde9e5SKrzysztof Grobelny 
140362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Got {} properties for AccountService",
140462598e31SEd Tanous                          propertiesList.size());
1405d1bde9e5SKrzysztof Grobelny 
1406d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1407d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1408d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1409d1bde9e5SKrzysztof Grobelny 
1410d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1411d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1412d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1413d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1414d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1415d1bde9e5SKrzysztof Grobelny 
1416d1bde9e5SKrzysztof Grobelny         if (!success)
14173d958bbcSAppaRao Puli         {
1418d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1419d1bde9e5SKrzysztof Grobelny             return;
14203d958bbcSAppaRao Puli         }
1421d1bde9e5SKrzysztof Grobelny 
1422d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
14233d958bbcSAppaRao Puli         {
1424d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
14253d958bbcSAppaRao Puli         }
1426d1bde9e5SKrzysztof Grobelny 
1427d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
14283d958bbcSAppaRao Puli         {
1429d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1430d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1431d1bde9e5SKrzysztof Grobelny         }
1432d1bde9e5SKrzysztof Grobelny 
1433d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
14343d958bbcSAppaRao Puli         {
1435002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1436d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
14373d958bbcSAppaRao Puli         }
1438d1bde9e5SKrzysztof Grobelny     });
14396973a582SRatan Gupta 
144002cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1441ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1442cb13a392SEd Tanous         if (!success)
1443cb13a392SEd Tanous         {
1444cb13a392SEd Tanous             return;
1445cb13a392SEd Tanous         }
1446002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1447ab828d7cSRatan Gupta     };
1448ab828d7cSRatan Gupta 
1449ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1450ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
14511ef4c342SEd Tanous }
14526973a582SRatan Gupta 
1453*3ce3688aSEd Tanous inline void
1454*3ce3688aSEd Tanous     handleCertificateMappingAttributePatch(crow::Response& res,
1455*3ce3688aSEd Tanous                                            const std::string& certMapAttribute)
1456*3ce3688aSEd Tanous {
1457*3ce3688aSEd Tanous     MTLSCommonNameParseMode parseMode =
1458*3ce3688aSEd Tanous         persistent_data::getMTLSCommonNameParseMode(certMapAttribute);
1459*3ce3688aSEd Tanous     if (parseMode == MTLSCommonNameParseMode::Invalid)
1460*3ce3688aSEd Tanous     {
1461*3ce3688aSEd Tanous         messages::propertyValueNotInList(res, "CertificateMappingAttribute",
1462*3ce3688aSEd Tanous                                          certMapAttribute);
1463*3ce3688aSEd Tanous         return;
1464*3ce3688aSEd Tanous     }
1465*3ce3688aSEd Tanous 
1466*3ce3688aSEd Tanous     persistent_data::AuthConfigMethods& authMethodsConfig =
1467*3ce3688aSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
1468*3ce3688aSEd Tanous     authMethodsConfig.mTLSCommonNameParsingMode = parseMode;
1469*3ce3688aSEd Tanous }
1470*3ce3688aSEd Tanous 
14711ef4c342SEd Tanous inline void handleAccountServicePatch(
14721ef4c342SEd Tanous     App& app, const crow::Request& req,
14731ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14741ef4c342SEd Tanous {
14753ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
147645ca1b86SEd Tanous     {
147745ca1b86SEd Tanous         return;
147845ca1b86SEd Tanous     }
1479f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1480f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1481ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1482f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
148310cb44f3SEd Tanous     LdapPatchParams ldapObject;
1484*3ce3688aSEd Tanous     std::optional<std::string> certificateMappingAttribute;
148510cb44f3SEd Tanous     LdapPatchParams activeDirectoryObject;
1486c1019828SEd Tanous     AuthMethods auth;
1487482a69e7SRavi Teja     std::optional<std::string> httpBasicAuth;
1488*3ce3688aSEd Tanous 
1489c1019828SEd Tanous     // clang-format off
149015ed6780SWilly Tu     if (!json_util::readJsonPatch(
1491c1019828SEd Tanous             req, asyncResp->res,
1492c1019828SEd Tanous             "AccountLockoutDuration", unlockTimeout,
1493c1019828SEd Tanous             "AccountLockoutThreshold", lockoutThreshold,
149410cb44f3SEd Tanous             "ActiveDirectory/Authentication/AuthenticationType", activeDirectoryObject.authType,
149510cb44f3SEd Tanous             "ActiveDirectory/Authentication/Password", activeDirectoryObject.password,
149610cb44f3SEd Tanous             "ActiveDirectory/Authentication/Username", activeDirectoryObject.userName,
149710cb44f3SEd Tanous             "ActiveDirectory/LDAPService/SearchSettings/BaseDistinguishedNames", activeDirectoryObject.baseDNList,
149810cb44f3SEd Tanous             "ActiveDirectory/LDAPService/SearchSettings/GroupsAttribute", activeDirectoryObject.groupsAttribute,
149910cb44f3SEd Tanous             "ActiveDirectory/LDAPService/SearchSettings/UsernameAttribute", activeDirectoryObject.userNameAttribute,
150010cb44f3SEd Tanous             "ActiveDirectory/RemoteRoleMapping", activeDirectoryObject.remoteRoleMapData,
150110cb44f3SEd Tanous             "ActiveDirectory/ServiceAddresses", activeDirectoryObject.serviceAddressList,
150210cb44f3SEd Tanous             "ActiveDirectory/ServiceEnabled", activeDirectoryObject.serviceEnabled,
1503*3ce3688aSEd Tanous             "MultiFactorAuth/ClientCertificate/CertificateMappingAttribute", certificateMappingAttribute,
150410cb44f3SEd Tanous             "LDAP/Authentication/AuthenticationType", ldapObject.authType,
150510cb44f3SEd Tanous             "LDAP/Authentication/Password", ldapObject.password,
150610cb44f3SEd Tanous             "LDAP/Authentication/Username", ldapObject.userName,
150710cb44f3SEd Tanous             "LDAP/LDAPService/SearchSettings/BaseDistinguishedNames", ldapObject.baseDNList,
150810cb44f3SEd Tanous             "LDAP/LDAPService/SearchSettings/GroupsAttribute", ldapObject.groupsAttribute,
150910cb44f3SEd Tanous             "LDAP/LDAPService/SearchSettings/UsernameAttribute", ldapObject.userNameAttribute,
151010cb44f3SEd Tanous             "LDAP/RemoteRoleMapping", ldapObject.remoteRoleMapData,
151110cb44f3SEd Tanous             "LDAP/ServiceAddresses", ldapObject.serviceAddressList,
151210cb44f3SEd Tanous             "LDAP/ServiceEnabled", ldapObject.serviceEnabled,
1513c1019828SEd Tanous             "MaxPasswordLength", maxPasswordLength,
1514c1019828SEd Tanous             "MinPasswordLength", minPasswordLength,
1515c1019828SEd Tanous             "Oem/OpenBMC/AuthMethods/BasicAuth", auth.basicAuth,
1516c1019828SEd Tanous             "Oem/OpenBMC/AuthMethods/Cookie", auth.cookie,
1517c1019828SEd Tanous             "Oem/OpenBMC/AuthMethods/SessionToken", auth.sessionToken,
151810cb44f3SEd Tanous             "Oem/OpenBMC/AuthMethods/TLS", auth.tls,
1519482a69e7SRavi Teja             "Oem/OpenBMC/AuthMethods/XToken", auth.xToken,
1520482a69e7SRavi Teja             "HTTPBasicAuth", httpBasicAuth))
1521f5ffd806SEd Tanous     {
1522f5ffd806SEd Tanous         return;
1523f5ffd806SEd Tanous     }
1524c1019828SEd Tanous     // clang-format on
1525f5ffd806SEd Tanous 
1526482a69e7SRavi Teja     if (httpBasicAuth)
1527482a69e7SRavi Teja     {
1528482a69e7SRavi Teja         if (*httpBasicAuth == "Enabled")
1529482a69e7SRavi Teja         {
1530482a69e7SRavi Teja             auth.basicAuth = true;
1531482a69e7SRavi Teja         }
1532482a69e7SRavi Teja         else if (*httpBasicAuth == "Disabled")
1533482a69e7SRavi Teja         {
1534482a69e7SRavi Teja             auth.basicAuth = false;
1535482a69e7SRavi Teja         }
1536482a69e7SRavi Teja         else
1537482a69e7SRavi Teja         {
1538482a69e7SRavi Teja             messages::propertyValueNotInList(asyncResp->res, "HttpBasicAuth",
1539482a69e7SRavi Teja                                              *httpBasicAuth);
1540482a69e7SRavi Teja         }
1541482a69e7SRavi Teja     }
1542482a69e7SRavi Teja 
1543*3ce3688aSEd Tanous     if (certificateMappingAttribute)
1544*3ce3688aSEd Tanous     {
1545*3ce3688aSEd Tanous         handleCertificateMappingAttributePatch(asyncResp->res,
1546*3ce3688aSEd Tanous                                                *certificateMappingAttribute);
1547*3ce3688aSEd Tanous     }
1548*3ce3688aSEd Tanous 
1549f5ffd806SEd Tanous     if (minPasswordLength)
1550f5ffd806SEd Tanous     {
1551d02aad39SEd Tanous         setDbusProperty(
1552e93abac6SGinu George             asyncResp, "MinPasswordLength", "xyz.openbmc_project.User.Manager",
1553d02aad39SEd Tanous             sdbusplus::message::object_path("/xyz/openbmc_project/user"),
15549ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1555e93abac6SGinu George             *minPasswordLength);
1556f5ffd806SEd Tanous     }
1557f5ffd806SEd Tanous 
1558f5ffd806SEd Tanous     if (maxPasswordLength)
1559f5ffd806SEd Tanous     {
15601ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1561f5ffd806SEd Tanous     }
1562f5ffd806SEd Tanous 
156310cb44f3SEd Tanous     handleLDAPPatch(std::move(activeDirectoryObject), asyncResp,
156410cb44f3SEd Tanous                     "ActiveDirectory");
156510cb44f3SEd Tanous     handleLDAPPatch(std::move(ldapObject), asyncResp, "LDAP");
1566f5ffd806SEd Tanous 
1567c1019828SEd Tanous     handleAuthMethodsPatch(asyncResp, auth);
1568f5ffd806SEd Tanous 
1569f5ffd806SEd Tanous     if (unlockTimeout)
1570f5ffd806SEd Tanous     {
1571d02aad39SEd Tanous         setDbusProperty(
1572e93abac6SGinu George             asyncResp, "AccountLockoutDuration",
1573e93abac6SGinu George             "xyz.openbmc_project.User.Manager",
1574d02aad39SEd Tanous             sdbusplus::message::object_path("/xyz/openbmc_project/user"),
15759ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1576e93abac6SGinu George             *unlockTimeout);
1577f5ffd806SEd Tanous     }
1578f5ffd806SEd Tanous     if (lockoutThreshold)
1579f5ffd806SEd Tanous     {
1580d02aad39SEd Tanous         setDbusProperty(
1581e93abac6SGinu George             asyncResp, "AccountLockoutThreshold",
1582e93abac6SGinu George             "xyz.openbmc_project.User.Manager",
1583d02aad39SEd Tanous             sdbusplus::message::object_path("/xyz/openbmc_project/user"),
15849ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy",
1585e93abac6SGinu George             "MaxLoginAttemptBeforeLockout", *lockoutThreshold);
1586f5ffd806SEd Tanous     }
15871ef4c342SEd Tanous }
1588f5ffd806SEd Tanous 
15894c7d4d33SEd Tanous inline void handleAccountCollectionHead(
15901ef4c342SEd Tanous     App& app, const crow::Request& req,
15911ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15921ef4c342SEd Tanous {
15933ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
159445ca1b86SEd Tanous     {
159545ca1b86SEd Tanous         return;
159645ca1b86SEd Tanous     }
15974c7d4d33SEd Tanous     asyncResp->res.addHeader(
15984c7d4d33SEd Tanous         boost::beast::http::field::link,
15994c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
16004c7d4d33SEd Tanous }
16014c7d4d33SEd Tanous 
16024c7d4d33SEd Tanous inline void handleAccountCollectionGet(
16034c7d4d33SEd Tanous     App& app, const crow::Request& req,
16044c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16054c7d4d33SEd Tanous {
1606afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1607afd369c6SJiaqing Zhao     {
1608afd369c6SJiaqing Zhao         return;
1609afd369c6SJiaqing Zhao     }
16103e72c202SNinad Palsule 
16113e72c202SNinad Palsule     if (req.session == nullptr)
16123e72c202SNinad Palsule     {
16133e72c202SNinad Palsule         messages::internalError(asyncResp->res);
16143e72c202SNinad Palsule         return;
16153e72c202SNinad Palsule     }
16163e72c202SNinad Palsule 
1617afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1618afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1619afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
16201476687dSEd Tanous 
16211476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
16221476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
16231ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
16241476687dSEd Tanous                                               "ManagerAccountCollection";
16251476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
16261476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
16270f74e643SEd Tanous 
16286c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
16293e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
16306c51eab1SEd Tanous 
1631f5e29f33SJunLin Chen     std::string thisUser;
1632f5e29f33SJunLin Chen     if (req.session)
1633f5e29f33SJunLin Chen     {
1634f5e29f33SJunLin Chen         thisUser = req.session->username;
1635f5e29f33SJunLin Chen     }
16365eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
16375eb468daSGeorge Liu     dbus::utility::getManagedObjects(
16385eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
1639cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
16405e7e2dc5SEd Tanous             const boost::system::error_code& ec,
1641711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1642b9b2e0b2SEd Tanous         if (ec)
1643b9b2e0b2SEd Tanous         {
1644f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1645b9b2e0b2SEd Tanous             return;
1646b9b2e0b2SEd Tanous         }
1647b9b2e0b2SEd Tanous 
1648cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1649002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1650cef1ddfbSEd Tanous 
1651cef1ddfbSEd Tanous         bool userCanSeeSelf =
1652002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1653cef1ddfbSEd Tanous 
1654002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1655b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1656b9b2e0b2SEd Tanous 
16579eb808c1SEd Tanous         for (const auto& userpath : users)
1658b9b2e0b2SEd Tanous         {
16592dfd18efSEd Tanous             std::string user = userpath.first.filename();
16602dfd18efSEd Tanous             if (user.empty())
1661b9b2e0b2SEd Tanous             {
16622dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
166362598e31SEd Tanous                 BMCWEB_LOG_ERROR("Invalid firmware ID");
16642dfd18efSEd Tanous 
16652dfd18efSEd Tanous                 return;
1666b9b2e0b2SEd Tanous             }
1667f365910cSGunnar Mills 
1668f365910cSGunnar Mills             // As clarified by Redfish here:
1669f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
16706c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
16716c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
16726c51eab1SEd Tanous             // accounts.
16731ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1674f365910cSGunnar Mills             {
16751476687dSEd Tanous                 nlohmann::json::object_t member;
16763b32780dSEd Tanous                 member["@odata.id"] = boost::urls::format(
16773b32780dSEd Tanous                     "/redfish/v1/AccountService/Accounts/{}", user);
1678b2ba3072SPatrick Williams                 memberArray.emplace_back(std::move(member));
1679b9b2e0b2SEd Tanous             }
1680f365910cSGunnar Mills         }
16811ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
16825eb468daSGeorge Liu     });
16831ef4c342SEd Tanous }
16846c51eab1SEd Tanous 
168597e90da3SNinad Palsule inline void processAfterCreateUser(
168697e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
168797e90da3SNinad Palsule     const std::string& username, const std::string& password,
168897e90da3SNinad Palsule     const boost::system::error_code& ec, sdbusplus::message_t& m)
168997e90da3SNinad Palsule {
169097e90da3SNinad Palsule     if (ec)
169197e90da3SNinad Palsule     {
169297e90da3SNinad Palsule         userErrorMessageHandler(m.get_error(), asyncResp, username, "");
169397e90da3SNinad Palsule         return;
169497e90da3SNinad Palsule     }
169597e90da3SNinad Palsule 
169697e90da3SNinad Palsule     if (pamUpdatePassword(username, password) != PAM_SUCCESS)
169797e90da3SNinad Palsule     {
169897e90da3SNinad Palsule         // At this point we have a user that's been
169997e90da3SNinad Palsule         // created, but the password set
170097e90da3SNinad Palsule         // failed.Something is wrong, so delete the user
170197e90da3SNinad Palsule         // that we've already created
170297e90da3SNinad Palsule         sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
170397e90da3SNinad Palsule         tempObjPath /= username;
170497e90da3SNinad Palsule         const std::string userPath(tempObjPath);
170597e90da3SNinad Palsule 
170697e90da3SNinad Palsule         crow::connections::systemBus->async_method_call(
170797e90da3SNinad Palsule             [asyncResp, password](const boost::system::error_code& ec3) {
170897e90da3SNinad Palsule             if (ec3)
170997e90da3SNinad Palsule             {
171097e90da3SNinad Palsule                 messages::internalError(asyncResp->res);
171197e90da3SNinad Palsule                 return;
171297e90da3SNinad Palsule             }
171397e90da3SNinad Palsule 
171497e90da3SNinad Palsule             // If password is invalid
17159bd80831SJason M. Bills             messages::propertyValueFormatError(asyncResp->res, nullptr,
171697e90da3SNinad Palsule                                                "Password");
171797e90da3SNinad Palsule         },
171897e90da3SNinad Palsule             "xyz.openbmc_project.User.Manager", userPath,
171997e90da3SNinad Palsule             "xyz.openbmc_project.Object.Delete", "Delete");
172097e90da3SNinad Palsule 
172162598e31SEd Tanous         BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
172297e90da3SNinad Palsule         return;
172397e90da3SNinad Palsule     }
172497e90da3SNinad Palsule 
172597e90da3SNinad Palsule     messages::created(asyncResp->res);
172697e90da3SNinad Palsule     asyncResp->res.addHeader("Location",
172797e90da3SNinad Palsule                              "/redfish/v1/AccountService/Accounts/" + username);
172897e90da3SNinad Palsule }
172997e90da3SNinad Palsule 
173097e90da3SNinad Palsule inline void processAfterGetAllGroups(
173197e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
173297e90da3SNinad Palsule     const std::string& username, const std::string& password,
1733e01d0c36SEd Tanous     const std::string& roleId, bool enabled,
17349ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes,
173597e90da3SNinad Palsule     const std::vector<std::string>& allGroupsList)
173697e90da3SNinad Palsule {
17373e72c202SNinad Palsule     std::vector<std::string> userGroups;
17389ba73934SNinad Palsule     std::vector<std::string> accountTypeUserGroups;
17399ba73934SNinad Palsule 
17409ba73934SNinad Palsule     // If user specified account types then convert them to unix user groups
17419ba73934SNinad Palsule     if (accountTypes)
17429ba73934SNinad Palsule     {
17439ba73934SNinad Palsule         if (!getUserGroupFromAccountType(asyncResp->res, *accountTypes,
17449ba73934SNinad Palsule                                          accountTypeUserGroups))
17459ba73934SNinad Palsule         {
17469ba73934SNinad Palsule             // Problem in mapping Account Types to User Groups, Error already
17479ba73934SNinad Palsule             // logged.
17489ba73934SNinad Palsule             return;
17499ba73934SNinad Palsule         }
17509ba73934SNinad Palsule     }
17519ba73934SNinad Palsule 
17523e72c202SNinad Palsule     for (const auto& grp : allGroupsList)
17533e72c202SNinad Palsule     {
17549ba73934SNinad Palsule         // If user specified the account type then only accept groups which are
17559ba73934SNinad Palsule         // in the account types group list.
17569ba73934SNinad Palsule         if (!accountTypeUserGroups.empty())
17579ba73934SNinad Palsule         {
17589ba73934SNinad Palsule             bool found = false;
17599ba73934SNinad Palsule             for (const auto& grp1 : accountTypeUserGroups)
17609ba73934SNinad Palsule             {
17619ba73934SNinad Palsule                 if (grp == grp1)
17629ba73934SNinad Palsule                 {
17639ba73934SNinad Palsule                     found = true;
17649ba73934SNinad Palsule                     break;
17659ba73934SNinad Palsule                 }
17669ba73934SNinad Palsule             }
17679ba73934SNinad Palsule             if (!found)
17689ba73934SNinad Palsule             {
17699ba73934SNinad Palsule                 continue;
17709ba73934SNinad Palsule             }
17719ba73934SNinad Palsule         }
17729ba73934SNinad Palsule 
17733e72c202SNinad Palsule         // Console access is provided to the user who is a member of
17743e72c202SNinad Palsule         // hostconsole group and has a administrator role. So, set
17753e72c202SNinad Palsule         // hostconsole group only for the administrator.
17769ba73934SNinad Palsule         if ((grp == "hostconsole") && (roleId != "priv-admin"))
17773e72c202SNinad Palsule         {
17789ba73934SNinad Palsule             if (!accountTypeUserGroups.empty())
17799ba73934SNinad Palsule             {
178062598e31SEd Tanous                 BMCWEB_LOG_ERROR(
178162598e31SEd Tanous                     "Only administrator can get HostConsole access");
17829ba73934SNinad Palsule                 asyncResp->res.result(boost::beast::http::status::bad_request);
17839ba73934SNinad Palsule                 return;
17849ba73934SNinad Palsule             }
17859ba73934SNinad Palsule             continue;
17869ba73934SNinad Palsule         }
17873e72c202SNinad Palsule         userGroups.emplace_back(grp);
17883e72c202SNinad Palsule     }
17899ba73934SNinad Palsule 
17909ba73934SNinad Palsule     // Make sure user specified groups are valid. This is internal error because
17919ba73934SNinad Palsule     // it some inconsistencies between user manager and bmcweb.
17929ba73934SNinad Palsule     if (!accountTypeUserGroups.empty() &&
17939ba73934SNinad Palsule         accountTypeUserGroups.size() != userGroups.size())
17949ba73934SNinad Palsule     {
17959ba73934SNinad Palsule         messages::internalError(asyncResp->res);
17969ba73934SNinad Palsule         return;
17973e72c202SNinad Palsule     }
179897e90da3SNinad Palsule     crow::connections::systemBus->async_method_call(
179997e90da3SNinad Palsule         [asyncResp, username, password](const boost::system::error_code& ec2,
180097e90da3SNinad Palsule                                         sdbusplus::message_t& m) {
180197e90da3SNinad Palsule         processAfterCreateUser(asyncResp, username, password, ec2, m);
180297e90da3SNinad Palsule     },
180397e90da3SNinad Palsule         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
18043e72c202SNinad Palsule         "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
1805e01d0c36SEd Tanous         roleId, enabled);
180697e90da3SNinad Palsule }
180797e90da3SNinad Palsule 
18081ef4c342SEd Tanous inline void handleAccountCollectionPost(
18091ef4c342SEd Tanous     App& app, const crow::Request& req,
18101ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
18111ef4c342SEd Tanous {
18123ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
181345ca1b86SEd Tanous     {
181445ca1b86SEd Tanous         return;
181545ca1b86SEd Tanous     }
18169712f8acSEd Tanous     std::string username;
18179712f8acSEd Tanous     std::string password;
1818e01d0c36SEd Tanous     std::optional<std::string> roleIdJson;
1819e01d0c36SEd Tanous     std::optional<bool> enabledJson;
18209ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes;
1821e01d0c36SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
1822e01d0c36SEd Tanous                                   "Password", password, "RoleId", roleIdJson,
1823e01d0c36SEd Tanous                                   "Enabled", enabledJson, "AccountTypes",
1824e01d0c36SEd Tanous                                   accountTypes))
182504ae99ecSEd Tanous     {
182604ae99ecSEd Tanous         return;
182704ae99ecSEd Tanous     }
182804ae99ecSEd Tanous 
1829e01d0c36SEd Tanous     std::string roleId = roleIdJson.value_or("User");
1830e01d0c36SEd Tanous     std::string priv = getPrivilegeFromRoleId(roleId);
183184e12cb7SAppaRao Puli     if (priv.empty())
183204ae99ecSEd Tanous     {
1833e01d0c36SEd Tanous         messages::propertyValueNotInList(asyncResp->res, roleId, "RoleId");
183404ae99ecSEd Tanous         return;
183504ae99ecSEd Tanous     }
18369712f8acSEd Tanous     roleId = priv;
183704ae99ecSEd Tanous 
1838e01d0c36SEd Tanous     bool enabled = enabledJson.value_or(true);
1839e01d0c36SEd Tanous 
1840599c71d8SAyushi Smriti     // Reading AllGroups property
18411e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
18421ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
18431ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
18441ef4c342SEd Tanous         "AllGroups",
18459ba73934SNinad Palsule         [asyncResp, username, password{std::move(password)}, roleId, enabled,
18469ba73934SNinad Palsule          accountTypes](const boost::system::error_code& ec,
18471e1e598dSJonathan Doman                        const std::vector<std::string>& allGroupsList) {
1848599c71d8SAyushi Smriti         if (ec)
1849599c71d8SAyushi Smriti         {
185062598e31SEd Tanous             BMCWEB_LOG_DEBUG("ERROR with async_method_call");
1851599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1852599c71d8SAyushi Smriti             return;
1853599c71d8SAyushi Smriti         }
1854599c71d8SAyushi Smriti 
18551e1e598dSJonathan Doman         if (allGroupsList.empty())
1856599c71d8SAyushi Smriti         {
1857599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1858599c71d8SAyushi Smriti             return;
1859599c71d8SAyushi Smriti         }
1860599c71d8SAyushi Smriti 
186197e90da3SNinad Palsule         processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
18629ba73934SNinad Palsule                                  accountTypes, allGroupsList);
18631e1e598dSJonathan Doman     });
18641ef4c342SEd Tanous }
1865b9b2e0b2SEd Tanous 
18661ef4c342SEd Tanous inline void
18674c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
18686c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18694c7d4d33SEd Tanous                       const std::string& /*accountName*/)
18701ef4c342SEd Tanous {
18713ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
187245ca1b86SEd Tanous     {
187345ca1b86SEd Tanous         return;
187445ca1b86SEd Tanous     }
18754c7d4d33SEd Tanous     asyncResp->res.addHeader(
18764c7d4d33SEd Tanous         boost::beast::http::field::link,
18774c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
18784c7d4d33SEd Tanous }
1879afd369c6SJiaqing Zhao 
18804c7d4d33SEd Tanous inline void
18814c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
18824c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18834c7d4d33SEd Tanous                      const std::string& accountName)
18844c7d4d33SEd Tanous {
1885afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1886afd369c6SJiaqing Zhao     {
1887afd369c6SJiaqing Zhao         return;
1888afd369c6SJiaqing Zhao     }
1889afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1890afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1891afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1892afd369c6SJiaqing Zhao 
189325b54dbaSEd Tanous     if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
189425b54dbaSEd Tanous     {
1895031514fbSJunLin Chen         // If authentication is disabled, there are no user accounts
189625b54dbaSEd Tanous         messages::resourceNotFound(asyncResp->res, "ManagerAccount",
189725b54dbaSEd Tanous                                    accountName);
1898031514fbSJunLin Chen         return;
189925b54dbaSEd Tanous     }
1900afd369c6SJiaqing Zhao 
1901031514fbSJunLin Chen     if (req.session == nullptr)
1902031514fbSJunLin Chen     {
1903031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1904031514fbSJunLin Chen         return;
1905031514fbSJunLin Chen     }
19066c51eab1SEd Tanous     if (req.session->username != accountName)
1907b9b2e0b2SEd Tanous     {
19086c51eab1SEd Tanous         // At this point we've determined that the user is trying to
19091ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
19101ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
19111ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
19126c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
19133e72c202SNinad Palsule             redfish::getUserPrivileges(*req.session);
19141ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
19151ef4c342SEd Tanous                                                          "ConfigureManager"};
19166c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
19176c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1918900f9497SJoseph Reynolds         {
191962598e31SEd Tanous             BMCWEB_LOG_DEBUG("GET Account denied access");
1920900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1921900f9497SJoseph Reynolds             return;
1922900f9497SJoseph Reynolds         }
1923900f9497SJoseph Reynolds     }
1924900f9497SJoseph Reynolds 
19255eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
19265eb468daSGeorge Liu     dbus::utility::getManagedObjects(
19275eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
19281ef4c342SEd Tanous         [asyncResp,
19295e7e2dc5SEd Tanous          accountName](const boost::system::error_code& ec,
1930711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1931b9b2e0b2SEd Tanous         if (ec)
1932b9b2e0b2SEd Tanous         {
1933f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1934b9b2e0b2SEd Tanous             return;
1935b9b2e0b2SEd Tanous         }
19363544d2a7SEd Tanous         const auto userIt = std::ranges::find_if(
193780f79a40SMichael Shen             users,
193880f79a40SMichael Shen             [accountName](
1939b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
194080f79a40SMichael Shen                                 dbus::utility::DBusInterfacesMap>& user) {
194155f79e6fSEd Tanous             return accountName == user.first.filename();
1942b477fd44SP Dheeraj Srujan Kumar         });
1943b9b2e0b2SEd Tanous 
194484e12cb7SAppaRao Puli         if (userIt == users.end())
1945b9b2e0b2SEd Tanous         {
1946002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1947002d39b4SEd Tanous                                        accountName);
194884e12cb7SAppaRao Puli             return;
194984e12cb7SAppaRao Puli         }
19504e68c45bSAyushi Smriti 
19511476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
195258345856SAbhishek Patel             "#ManagerAccount.v1_7_0.ManagerAccount";
19531476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
19541476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
19551476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
195658345856SAbhishek Patel         asyncResp->res.jsonValue["StrictAccountTypes"] = true;
19574e68c45bSAyushi Smriti 
195884e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
195965b0dc32SEd Tanous         {
1960002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
196165b0dc32SEd Tanous             {
196265b0dc32SEd Tanous                 for (const auto& property : interface.second)
196365b0dc32SEd Tanous                 {
196465b0dc32SEd Tanous                     if (property.first == "UserEnabled")
196565b0dc32SEd Tanous                     {
196665b0dc32SEd Tanous                         const bool* userEnabled =
1967abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
196865b0dc32SEd Tanous                         if (userEnabled == nullptr)
196965b0dc32SEd Tanous                         {
197062598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserEnabled wasn't a bool");
197184e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
197284e12cb7SAppaRao Puli                             return;
197365b0dc32SEd Tanous                         }
1974002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
197565b0dc32SEd Tanous                     }
1976002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
197765b0dc32SEd Tanous                     {
197865b0dc32SEd Tanous                         const bool* userLocked =
1979abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
198065b0dc32SEd Tanous                         if (userLocked == nullptr)
198165b0dc32SEd Tanous                         {
198262598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserLockedForF"
198384e12cb7SAppaRao Puli                                              "ailedAttempt "
198462598e31SEd Tanous                                              "wasn't a bool");
198584e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
198684e12cb7SAppaRao Puli                             return;
198765b0dc32SEd Tanous                         }
1988002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
198920fa6a2cSEd Tanous                         nlohmann::json::array_t allowed;
199020fa6a2cSEd Tanous                         // can only unlock accounts
199120fa6a2cSEd Tanous                         allowed.emplace_back("false");
1992002d39b4SEd Tanous                         asyncResp->res
199320fa6a2cSEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] =
199420fa6a2cSEd Tanous                             std::move(allowed);
199565b0dc32SEd Tanous                     }
199684e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
199784e12cb7SAppaRao Puli                     {
199854fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1999002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
200054fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
200184e12cb7SAppaRao Puli                         {
200262598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserPrivilege wasn't a "
200362598e31SEd Tanous                                              "string");
200484e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
200584e12cb7SAppaRao Puli                             return;
200684e12cb7SAppaRao Puli                         }
20071ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
200854fc587aSNagaraju Goruganti                         if (role.empty())
200984e12cb7SAppaRao Puli                         {
201062598e31SEd Tanous                             BMCWEB_LOG_ERROR("Invalid user role");
201184e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
201284e12cb7SAppaRao Puli                             return;
201384e12cb7SAppaRao Puli                         }
201454fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
201584e12cb7SAppaRao Puli 
20161476687dSEd Tanous                         nlohmann::json& roleEntry =
2017002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
20183b32780dSEd Tanous                         roleEntry["@odata.id"] = boost::urls::format(
20193b32780dSEd Tanous                             "/redfish/v1/AccountService/Roles/{}", role);
202084e12cb7SAppaRao Puli                     }
2021002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
20223bf4e632SJoseph Reynolds                     {
20233bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
20243bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
20253bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
20263bf4e632SJoseph Reynolds                         {
202762598e31SEd Tanous                             BMCWEB_LOG_ERROR(
202862598e31SEd Tanous                                 "UserPasswordExpired wasn't a bool");
20293bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
20303bf4e632SJoseph Reynolds                             return;
20313bf4e632SJoseph Reynolds                         }
2032002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
20333bf4e632SJoseph Reynolds                             *userPasswordExpired;
20343bf4e632SJoseph Reynolds                     }
2035c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
2036c7229815SAbhishek Patel                     {
2037c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
2038c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
2039c7229815SAbhishek Patel                                 &property.second);
2040c7229815SAbhishek Patel                         if (userGroups == nullptr)
2041c7229815SAbhishek Patel                         {
204262598e31SEd Tanous                             BMCWEB_LOG_ERROR(
204362598e31SEd Tanous                                 "userGroups wasn't a string vector");
2044c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2045c7229815SAbhishek Patel                             return;
2046c7229815SAbhishek Patel                         }
2047c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
2048c7229815SAbhishek Patel                         {
204962598e31SEd Tanous                             BMCWEB_LOG_ERROR("userGroups mapping failed");
2050c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2051c7229815SAbhishek Patel                             return;
2052c7229815SAbhishek Patel                         }
2053c7229815SAbhishek Patel                     }
205465b0dc32SEd Tanous                 }
205565b0dc32SEd Tanous             }
205665b0dc32SEd Tanous         }
205765b0dc32SEd Tanous 
20583b32780dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
20593b32780dSEd Tanous             "/redfish/v1/AccountService/Accounts/{}", accountName);
2060b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
2061b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
20625eb468daSGeorge Liu     });
20631ef4c342SEd Tanous }
2064a840879dSEd Tanous 
20651ef4c342SEd Tanous inline void
206620fc307fSGunnar Mills     handleAccountDelete(App& app, const crow::Request& req,
20676c51eab1SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20681ef4c342SEd Tanous                         const std::string& username)
20691ef4c342SEd Tanous {
20703ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
207145ca1b86SEd Tanous     {
207245ca1b86SEd Tanous         return;
207345ca1b86SEd Tanous     }
20741ef4c342SEd Tanous 
207525b54dbaSEd Tanous     if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
207625b54dbaSEd Tanous     {
2077031514fbSJunLin Chen         // If authentication is disabled, there are no user accounts
2078d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
2079031514fbSJunLin Chen         return;
208025b54dbaSEd Tanous     }
20811ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
20821ef4c342SEd Tanous     tempObjPath /= username;
20831ef4c342SEd Tanous     const std::string userPath(tempObjPath);
20841ef4c342SEd Tanous 
20851ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
20865e7e2dc5SEd Tanous         [asyncResp, username](const boost::system::error_code& ec) {
20871ef4c342SEd Tanous         if (ec)
20881ef4c342SEd Tanous         {
2089d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
20901ef4c342SEd Tanous                                        username);
20911ef4c342SEd Tanous             return;
20921ef4c342SEd Tanous         }
20931ef4c342SEd Tanous 
20941ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
20951ef4c342SEd Tanous     },
20961ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
20971ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
20981ef4c342SEd Tanous }
20991ef4c342SEd Tanous 
21001ef4c342SEd Tanous inline void
21011ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
21021ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21031ef4c342SEd Tanous                        const std::string& username)
21041ef4c342SEd Tanous {
21051ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
21061ef4c342SEd Tanous     {
21071ef4c342SEd Tanous         return;
21081ef4c342SEd Tanous     }
210925b54dbaSEd Tanous     if constexpr (BMCWEB_INSECURE_DISABLE_AUTH)
211025b54dbaSEd Tanous     {
21111ef4c342SEd Tanous         // If authentication is disabled, there are no user accounts
2112d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
21131ef4c342SEd Tanous         return;
211425b54dbaSEd Tanous     }
2115a24526dcSEd Tanous     std::optional<std::string> newUserName;
2116a24526dcSEd Tanous     std::optional<std::string> password;
2117a24526dcSEd Tanous     std::optional<bool> enabled;
2118a24526dcSEd Tanous     std::optional<std::string> roleId;
211924c8542dSRatan Gupta     std::optional<bool> locked;
212058345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes;
212158345856SAbhishek Patel 
2122031514fbSJunLin Chen     if (req.session == nullptr)
2123031514fbSJunLin Chen     {
2124031514fbSJunLin Chen         messages::internalError(asyncResp->res);
2125031514fbSJunLin Chen         return;
2126031514fbSJunLin Chen     }
2127031514fbSJunLin Chen 
21282b9c1dfeSEd Tanous     bool userSelf = (username == req.session->username);
21292b9c1dfeSEd Tanous 
2130e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
21313e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
2132e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
2133e9cc5172SEd Tanous     bool userHasConfigureUsers =
2134e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
2135e9cc5172SEd Tanous     if (userHasConfigureUsers)
2136e9cc5172SEd Tanous     {
2137e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
213858345856SAbhishek Patel         if (!json_util::readJsonPatch(
213958345856SAbhishek Patel                 req, asyncResp->res, "UserName", newUserName, "Password",
214058345856SAbhishek Patel                 password, "RoleId", roleId, "Enabled", enabled, "Locked",
214158345856SAbhishek Patel                 locked, "AccountTypes", accountTypes))
2142a840879dSEd Tanous         {
2143a840879dSEd Tanous             return;
2144a840879dSEd Tanous         }
2145e9cc5172SEd Tanous     }
2146e9cc5172SEd Tanous     else
2147900f9497SJoseph Reynolds     {
2148e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
214958345856SAbhishek Patel         if (!userSelf)
2150900f9497SJoseph Reynolds         {
2151900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
2152900f9497SJoseph Reynolds             return;
2153900f9497SJoseph Reynolds         }
2154031514fbSJunLin Chen 
2155e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
21561ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
21571ef4c342SEd Tanous                                       password))
2158e9cc5172SEd Tanous         {
2159e9cc5172SEd Tanous             return;
2160e9cc5172SEd Tanous         }
2161900f9497SJoseph Reynolds     }
2162900f9497SJoseph Reynolds 
216366b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
21646c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
21656c51eab1SEd Tanous     // updating user properties other then username. If username
21666c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
21676c51eab1SEd Tanous     // user rename request.
216866b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
2169a840879dSEd Tanous     {
21701ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
2171e518ef32SRavi Teja                              locked, accountTypes, userSelf, req.session);
217284e12cb7SAppaRao Puli         return;
217384e12cb7SAppaRao Puli     }
217484e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
21756c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
21761ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
2177e518ef32SRavi Teja          locked, userSelf, req, accountTypes(std::move(accountTypes))](
2178e81de512SEd Tanous             const boost::system::error_code& ec, sdbusplus::message_t& m) {
217984e12cb7SAppaRao Puli         if (ec)
218084e12cb7SAppaRao Puli         {
2181002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2182002d39b4SEd Tanous                                     username);
2183a840879dSEd Tanous             return;
2184a840879dSEd Tanous         }
2185a840879dSEd Tanous 
2186002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
2187e518ef32SRavi Teja                              locked, accountTypes, userSelf, req.session);
218884e12cb7SAppaRao Puli     },
21891ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
219084e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
219184e12cb7SAppaRao Puli         *newUserName);
21921ef4c342SEd Tanous }
21931ef4c342SEd Tanous 
21941ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
21951ef4c342SEd Tanous {
21961ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
21974c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
21984c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
21994c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
22004c7d4d33SEd Tanous 
22014c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22021ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
22031ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
22041ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
22051ef4c342SEd Tanous 
22061ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22071ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
22081ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
22091ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
22101ef4c342SEd Tanous 
22111aa375b8SEd Tanous     BMCWEB_ROUTE(
22121aa375b8SEd Tanous         app,
22131aa375b8SEd Tanous         "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates")
22141aa375b8SEd Tanous         .privileges(redfish::privileges::headCertificateCollection)
22151aa375b8SEd Tanous         .methods(boost::beast::http::verb::head)(std::bind_front(
22161aa375b8SEd Tanous             handleAccountServiceClientCertificatesHead, std::ref(app)));
22171aa375b8SEd Tanous 
22181aa375b8SEd Tanous     BMCWEB_ROUTE(
22191aa375b8SEd Tanous         app,
22201aa375b8SEd Tanous         "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates")
22211aa375b8SEd Tanous         .privileges(redfish::privileges::getCertificateCollection)
22221aa375b8SEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
22231aa375b8SEd Tanous             handleAccountServiceClientCertificatesGet, std::ref(app)));
22241aa375b8SEd Tanous 
22251aa375b8SEd Tanous     BMCWEB_ROUTE(
22261aa375b8SEd Tanous         app,
22271aa375b8SEd Tanous         "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates/<str>")
22281aa375b8SEd Tanous         .privileges(redfish::privileges::headCertificate)
22291aa375b8SEd Tanous         .methods(boost::beast::http::verb::head)(std::bind_front(
22301aa375b8SEd Tanous             handleAccountServiceClientCertificatesInstanceHead, std::ref(app)));
22311aa375b8SEd Tanous 
22321aa375b8SEd Tanous     BMCWEB_ROUTE(
22331aa375b8SEd Tanous         app,
22341aa375b8SEd Tanous         "/redfish/v1/AccountService/MultiFactorAuth/ClientCertificate/Certificates/<str>/")
22351aa375b8SEd Tanous         .privileges(redfish::privileges::getCertificate)
22361aa375b8SEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
22371aa375b8SEd Tanous             handleAccountServiceClientCertificatesInstanceGet, std::ref(app)));
22381aa375b8SEd Tanous 
22391ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
22404c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
22414c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
22424c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
22434c7d4d33SEd Tanous 
22444c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
22451ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
22461ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
22471ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
22481ef4c342SEd Tanous 
22491ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
22501ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
22511ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
22521ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
22531ef4c342SEd Tanous 
22541ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
22554c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
22564c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
22574c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
22584c7d4d33SEd Tanous 
22594c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
22601ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
22611ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
22621ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
22631ef4c342SEd Tanous 
22641ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
22651ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
22661ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
22671ef4c342SEd Tanous         // yet
22681ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
22691ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
22701ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
227184e12cb7SAppaRao Puli 
22726c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2273ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
22746c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
227520fc307fSGunnar Mills             std::bind_front(handleAccountDelete, std::ref(app)));
227606e086d9SEd Tanous }
227788d16c9aSLewanczyk, Dawid 
227888d16c9aSLewanczyk, Dawid } // namespace redfish
2279