xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision e01d0c36af115ed46d54b5dbbacfe3ad92226bd3)
188d16c9aSLewanczyk, Dawid /*
288d16c9aSLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
388d16c9aSLewanczyk, Dawid //
488d16c9aSLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
588d16c9aSLewanczyk, Dawid // you may not use this file except in compliance with the License.
688d16c9aSLewanczyk, Dawid // You may obtain a copy of the License at
788d16c9aSLewanczyk, Dawid //
888d16c9aSLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
988d16c9aSLewanczyk, Dawid //
1088d16c9aSLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1188d16c9aSLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1288d16c9aSLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388d16c9aSLewanczyk, Dawid // See the License for the specific language governing permissions and
1488d16c9aSLewanczyk, Dawid // limitations under the License.
1588d16c9aSLewanczyk, Dawid */
1688d16c9aSLewanczyk, Dawid #pragma once
1788d16c9aSLewanczyk, Dawid 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_utility.hpp"
203ccb3adbSEd Tanous #include "error_messages.hpp"
210ec8b83dSEd Tanous #include "generated/enums/account_service.hpp"
223ccb3adbSEd Tanous #include "openbmc_dbus_rest.hpp"
233ccb3adbSEd Tanous #include "persistent_data.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
250ec8b83dSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
280ec8b83dSEd Tanous 
291e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
30d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
311214b7e7SGunnar Mills 
322b73119cSGeorge Liu #include <array>
33c7229815SAbhishek Patel #include <optional>
34c7229815SAbhishek Patel #include <string>
352b73119cSGeorge Liu #include <string_view>
36c7229815SAbhishek Patel #include <vector>
37c7229815SAbhishek Patel 
381abe55efSEd Tanous namespace redfish
391abe55efSEd Tanous {
4088d16c9aSLewanczyk, Dawid 
4123a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
426973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
432c70f800SEd Tanous constexpr const char* adConfigObject =
44ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
45ab828d7cSRatan Gupta 
46b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
476973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
486973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
496973a582SRatan Gupta constexpr const char* ldapConfigInterface =
506973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
516973a582SRatan Gupta constexpr const char* ldapCreateInterface =
526973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
536973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
5406785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
5506785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
566973a582SRatan Gupta 
5754fc587aSNagaraju Goruganti struct LDAPRoleMapData
5854fc587aSNagaraju Goruganti {
5954fc587aSNagaraju Goruganti     std::string groupName;
6054fc587aSNagaraju Goruganti     std::string privilege;
6154fc587aSNagaraju Goruganti };
6254fc587aSNagaraju Goruganti 
636973a582SRatan Gupta struct LDAPConfigData
646973a582SRatan Gupta {
656973a582SRatan Gupta     std::string uri{};
666973a582SRatan Gupta     std::string bindDN{};
676973a582SRatan Gupta     std::string baseDN{};
686973a582SRatan Gupta     std::string searchScope{};
696973a582SRatan Gupta     std::string serverType{};
706973a582SRatan Gupta     bool serviceEnabled = false;
716973a582SRatan Gupta     std::string userNameAttribute{};
726973a582SRatan Gupta     std::string groupAttribute{};
7354fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
746973a582SRatan Gupta };
756973a582SRatan Gupta 
7654fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7784e12cb7SAppaRao Puli {
7884e12cb7SAppaRao Puli     if (role == "priv-admin")
7984e12cb7SAppaRao Puli     {
8084e12cb7SAppaRao Puli         return "Administrator";
8184e12cb7SAppaRao Puli     }
823174e4dfSEd Tanous     if (role == "priv-user")
8384e12cb7SAppaRao Puli     {
84c80fee55SAppaRao Puli         return "ReadOnly";
8584e12cb7SAppaRao Puli     }
863174e4dfSEd Tanous     if (role == "priv-operator")
8784e12cb7SAppaRao Puli     {
8884e12cb7SAppaRao Puli         return "Operator";
8984e12cb7SAppaRao Puli     }
9084e12cb7SAppaRao Puli     return "";
9184e12cb7SAppaRao Puli }
9254fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9384e12cb7SAppaRao Puli {
9484e12cb7SAppaRao Puli     if (role == "Administrator")
9584e12cb7SAppaRao Puli     {
9684e12cb7SAppaRao Puli         return "priv-admin";
9784e12cb7SAppaRao Puli     }
983174e4dfSEd Tanous     if (role == "ReadOnly")
9984e12cb7SAppaRao Puli     {
10084e12cb7SAppaRao Puli         return "priv-user";
10184e12cb7SAppaRao Puli     }
1023174e4dfSEd Tanous     if (role == "Operator")
10384e12cb7SAppaRao Puli     {
10484e12cb7SAppaRao Puli         return "priv-operator";
10584e12cb7SAppaRao Puli     }
10684e12cb7SAppaRao Puli     return "";
10784e12cb7SAppaRao Puli }
108b9b2e0b2SEd Tanous 
109c7229815SAbhishek Patel /**
110c7229815SAbhishek Patel  * @brief Maps user group names retrieved from D-Bus object to
111c7229815SAbhishek Patel  * Account Types.
112c7229815SAbhishek Patel  *
113c7229815SAbhishek Patel  * @param[in] userGroups List of User groups
114c7229815SAbhishek Patel  * @param[out] res AccountTypes populated
115c7229815SAbhishek Patel  *
116c7229815SAbhishek Patel  * @return true in case of success, false if UserGroups contains
117c7229815SAbhishek Patel  * invalid group name(s).
118c7229815SAbhishek Patel  */
119c7229815SAbhishek Patel inline bool translateUserGroup(const std::vector<std::string>& userGroups,
120c7229815SAbhishek Patel                                crow::Response& res)
121c7229815SAbhishek Patel {
122c7229815SAbhishek Patel     std::vector<std::string> accountTypes;
123c7229815SAbhishek Patel     for (const auto& userGroup : userGroups)
124c7229815SAbhishek Patel     {
125c7229815SAbhishek Patel         if (userGroup == "redfish")
126c7229815SAbhishek Patel         {
127c7229815SAbhishek Patel             accountTypes.emplace_back("Redfish");
128c7229815SAbhishek Patel             accountTypes.emplace_back("WebUI");
129c7229815SAbhishek Patel         }
130c7229815SAbhishek Patel         else if (userGroup == "ipmi")
131c7229815SAbhishek Patel         {
132c7229815SAbhishek Patel             accountTypes.emplace_back("IPMI");
133c7229815SAbhishek Patel         }
134c7229815SAbhishek Patel         else if (userGroup == "ssh")
135c7229815SAbhishek Patel         {
136c7229815SAbhishek Patel             accountTypes.emplace_back("ManagerConsole");
137c7229815SAbhishek Patel         }
1383e72c202SNinad Palsule         else if (userGroup == "hostconsole")
1393e72c202SNinad Palsule         {
1403e72c202SNinad Palsule             // The hostconsole group controls who can access the host console
1413e72c202SNinad Palsule             // port via ssh and websocket.
1423e72c202SNinad Palsule             accountTypes.emplace_back("HostConsole");
1433e72c202SNinad Palsule         }
144c7229815SAbhishek Patel         else if (userGroup == "web")
145c7229815SAbhishek Patel         {
146c7229815SAbhishek Patel             // 'web' is one of the valid groups in the UserGroups property of
147c7229815SAbhishek Patel             // the user account in the D-Bus object. This group is currently not
148c7229815SAbhishek Patel             // doing anything, and is considered to be equivalent to 'redfish'.
149c7229815SAbhishek Patel             // 'redfish' user group is mapped to 'Redfish'and 'WebUI'
150c7229815SAbhishek Patel             // AccountTypes, so do nothing here...
151c7229815SAbhishek Patel         }
152c7229815SAbhishek Patel         else
153c7229815SAbhishek Patel         {
154c7229815SAbhishek Patel             // Invalid user group name. Caller throws an excption.
155c7229815SAbhishek Patel             return false;
156c7229815SAbhishek Patel         }
157c7229815SAbhishek Patel     }
158c7229815SAbhishek Patel 
159c7229815SAbhishek Patel     res.jsonValue["AccountTypes"] = std::move(accountTypes);
160c7229815SAbhishek Patel     return true;
161c7229815SAbhishek Patel }
162c7229815SAbhishek Patel 
16358345856SAbhishek Patel /**
16458345856SAbhishek Patel  * @brief Builds User Groups from the Account Types
16558345856SAbhishek Patel  *
16658345856SAbhishek Patel  * @param[in] asyncResp Async Response
16758345856SAbhishek Patel  * @param[in] accountTypes List of Account Types
16858345856SAbhishek Patel  * @param[out] userGroups List of User Groups mapped from Account Types
16958345856SAbhishek Patel  *
17058345856SAbhishek Patel  * @return true if Account Types mapped to User Groups, false otherwise.
17158345856SAbhishek Patel  */
17258345856SAbhishek Patel inline bool
17358345856SAbhishek Patel     getUserGroupFromAccountType(crow::Response& res,
17458345856SAbhishek Patel                                 const std::vector<std::string>& accountTypes,
17558345856SAbhishek Patel                                 std::vector<std::string>& userGroups)
17658345856SAbhishek Patel {
17758345856SAbhishek Patel     // Need both Redfish and WebUI Account Types to map to 'redfish' User Group
17858345856SAbhishek Patel     bool redfishType = false;
17958345856SAbhishek Patel     bool webUIType = false;
18058345856SAbhishek Patel 
18158345856SAbhishek Patel     for (const auto& accountType : accountTypes)
18258345856SAbhishek Patel     {
18358345856SAbhishek Patel         if (accountType == "Redfish")
18458345856SAbhishek Patel         {
18558345856SAbhishek Patel             redfishType = true;
18658345856SAbhishek Patel         }
18758345856SAbhishek Patel         else if (accountType == "WebUI")
18858345856SAbhishek Patel         {
18958345856SAbhishek Patel             webUIType = true;
19058345856SAbhishek Patel         }
19158345856SAbhishek Patel         else if (accountType == "IPMI")
19258345856SAbhishek Patel         {
19358345856SAbhishek Patel             userGroups.emplace_back("ipmi");
19458345856SAbhishek Patel         }
19558345856SAbhishek Patel         else if (accountType == "HostConsole")
19658345856SAbhishek Patel         {
19758345856SAbhishek Patel             userGroups.emplace_back("hostconsole");
19858345856SAbhishek Patel         }
19958345856SAbhishek Patel         else if (accountType == "ManagerConsole")
20058345856SAbhishek Patel         {
20158345856SAbhishek Patel             userGroups.emplace_back("ssh");
20258345856SAbhishek Patel         }
20358345856SAbhishek Patel         else
20458345856SAbhishek Patel         {
20558345856SAbhishek Patel             // Invalid Account Type
20658345856SAbhishek Patel             messages::propertyValueNotInList(res, "AccountTypes", accountType);
20758345856SAbhishek Patel             return false;
20858345856SAbhishek Patel         }
20958345856SAbhishek Patel     }
21058345856SAbhishek Patel 
21158345856SAbhishek Patel     // Both  Redfish and WebUI Account Types are needed to PATCH
21258345856SAbhishek Patel     if (redfishType ^ webUIType)
21358345856SAbhishek Patel     {
21462598e31SEd Tanous         BMCWEB_LOG_ERROR(
21562598e31SEd Tanous             "Missing Redfish or WebUI Account Type to set redfish User Group");
21658345856SAbhishek Patel         messages::strictAccountTypes(res, "AccountTypes");
21758345856SAbhishek Patel         return false;
21858345856SAbhishek Patel     }
21958345856SAbhishek Patel 
22058345856SAbhishek Patel     if (redfishType && webUIType)
22158345856SAbhishek Patel     {
22258345856SAbhishek Patel         userGroups.emplace_back("redfish");
22358345856SAbhishek Patel     }
22458345856SAbhishek Patel 
22558345856SAbhishek Patel     return true;
22658345856SAbhishek Patel }
22758345856SAbhishek Patel 
22858345856SAbhishek Patel /**
22958345856SAbhishek Patel  * @brief Sets UserGroups property of the user based on the Account Types
23058345856SAbhishek Patel  *
23158345856SAbhishek Patel  * @param[in] accountTypes List of User Account Types
23258345856SAbhishek Patel  * @param[in] asyncResp Async Response
23358345856SAbhishek Patel  * @param[in] dbusObjectPath D-Bus Object Path
23458345856SAbhishek Patel  * @param[in] userSelf true if User is updating OWN Account Types
23558345856SAbhishek Patel  */
23658345856SAbhishek Patel inline void
23758345856SAbhishek Patel     patchAccountTypes(const std::vector<std::string>& accountTypes,
23858345856SAbhishek Patel                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23958345856SAbhishek Patel                       const std::string& dbusObjectPath, bool userSelf)
24058345856SAbhishek Patel {
24158345856SAbhishek Patel     // Check if User is disabling own Redfish Account Type
24258345856SAbhishek Patel     if (userSelf &&
24358345856SAbhishek Patel         (accountTypes.cend() ==
24458345856SAbhishek Patel          std::find(accountTypes.cbegin(), accountTypes.cend(), "Redfish")))
24558345856SAbhishek Patel     {
24662598e31SEd Tanous         BMCWEB_LOG_ERROR(
24762598e31SEd Tanous             "User disabling OWN Redfish Account Type is not allowed");
24858345856SAbhishek Patel         messages::strictAccountTypes(asyncResp->res, "AccountTypes");
24958345856SAbhishek Patel         return;
25058345856SAbhishek Patel     }
25158345856SAbhishek Patel 
25258345856SAbhishek Patel     std::vector<std::string> updatedUserGroups;
25358345856SAbhishek Patel     if (!getUserGroupFromAccountType(asyncResp->res, accountTypes,
25458345856SAbhishek Patel                                      updatedUserGroups))
25558345856SAbhishek Patel     {
25658345856SAbhishek Patel         // Problem in mapping Account Types to User Groups, Error already
25758345856SAbhishek Patel         // logged.
25858345856SAbhishek Patel         return;
25958345856SAbhishek Patel     }
26058345856SAbhishek Patel 
2619ae226faSGeorge Liu     sdbusplus::asio::setProperty(
2629ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
2639ae226faSGeorge Liu         dbusObjectPath, "xyz.openbmc_project.User.Attributes", "UserGroups",
2649ae226faSGeorge Liu         updatedUserGroups, [asyncResp](const boost::system::error_code& ec) {
26558345856SAbhishek Patel             if (ec)
26658345856SAbhishek Patel             {
26762598e31SEd Tanous                 BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
26858345856SAbhishek Patel                 messages::internalError(asyncResp->res);
26958345856SAbhishek Patel                 return;
27058345856SAbhishek Patel             }
27158345856SAbhishek Patel             messages::success(asyncResp->res);
2729ae226faSGeorge Liu         });
27358345856SAbhishek Patel }
27458345856SAbhishek Patel 
2758d1b46d7Szhanghch05 inline void userErrorMessageHandler(
2768d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2778d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
27866b5ca76Sjayaprakash Mutyala {
27966b5ca76Sjayaprakash Mutyala     if (e == nullptr)
28066b5ca76Sjayaprakash Mutyala     {
28166b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
28266b5ca76Sjayaprakash Mutyala         return;
28366b5ca76Sjayaprakash Mutyala     }
28466b5ca76Sjayaprakash Mutyala 
285055806b3SManojkiran Eda     const char* errorMessage = e->name;
28666b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
28766b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
28866b5ca76Sjayaprakash Mutyala     {
289d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
29066b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
29166b5ca76Sjayaprakash Mutyala     }
29266b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
29366b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
29466b5ca76Sjayaprakash Mutyala     {
295d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
29666b5ca76Sjayaprakash Mutyala     }
297d4d25793SEd Tanous     else if ((strcmp(errorMessage,
298d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
299d4d25793SEd Tanous               0) ||
3000fda0f12SGeorge Liu              (strcmp(
3010fda0f12SGeorge Liu                   errorMessage,
3020fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
3030fda0f12SGeorge Liu               0))
30466b5ca76Sjayaprakash Mutyala     {
30566b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
30666b5ca76Sjayaprakash Mutyala     }
30766b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
30866b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
30966b5ca76Sjayaprakash Mutyala     {
31066b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
31166b5ca76Sjayaprakash Mutyala     }
31266b5ca76Sjayaprakash Mutyala     else
31366b5ca76Sjayaprakash Mutyala     {
31466b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
31566b5ca76Sjayaprakash Mutyala     }
31666b5ca76Sjayaprakash Mutyala }
31766b5ca76Sjayaprakash Mutyala 
31881ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
319ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
320ab828d7cSRatan Gupta                                 const std::string& ldapType)
3216973a582SRatan Gupta {
32289492a15SPatrick Williams     std::string service = (ldapType == "LDAP") ? "LDAPService"
32389492a15SPatrick Williams                                                : "ActiveDirectoryService";
32454fc587aSNagaraju Goruganti 
3251476687dSEd Tanous     nlohmann::json& ldap = jsonResponse[ldapType];
32654fc587aSNagaraju Goruganti 
3271476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
3281476687dSEd Tanous     ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri});
3290ec8b83dSEd Tanous     ldap["Authentication"]["AuthenticationType"] =
3300ec8b83dSEd Tanous         account_service::AuthenticationTypes::UsernameAndPassword;
3311476687dSEd Tanous     ldap["Authentication"]["Username"] = confData.bindDN;
3321476687dSEd Tanous     ldap["Authentication"]["Password"] = nullptr;
3331476687dSEd Tanous 
3341476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] =
3351476687dSEd Tanous         nlohmann::json::array({confData.baseDN});
3361476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] =
3371476687dSEd Tanous         confData.userNameAttribute;
3381476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] =
3391476687dSEd Tanous         confData.groupAttribute;
3401476687dSEd Tanous 
3411476687dSEd Tanous     nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"];
34254fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
3439eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
34454fc587aSNagaraju Goruganti     {
34562598e31SEd Tanous         BMCWEB_LOG_DEBUG("Pushing the data groupName={}", obj.second.groupName);
346613dabeaSEd Tanous 
347613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
348613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
349329f0348SJorge Cisneros         remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
350329f0348SJorge Cisneros         roleMapArray.emplace_back(std::move(remoteGroup));
35154fc587aSNagaraju Goruganti     }
3526973a582SRatan Gupta }
3536973a582SRatan Gupta 
3546973a582SRatan Gupta /**
35506785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
35606785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
35706785244SRatan Gupta  *
35806785244SRatan Gupta  */
35923a21a1cSEd Tanous inline void handleRoleMapPatch(
3608d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36106785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
362f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
36306785244SRatan Gupta {
36406785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
36506785244SRatan Gupta     {
366f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
36706785244SRatan Gupta 
36806785244SRatan Gupta         if (thisJson.is_null())
36906785244SRatan Gupta         {
37006785244SRatan Gupta             // delete the existing object
37106785244SRatan Gupta             if (index < roleMapObjData.size())
37206785244SRatan Gupta             {
37306785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
37406785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
3755e7e2dc5SEd Tanous                      index](const boost::system::error_code& ec) {
37606785244SRatan Gupta                     if (ec)
37706785244SRatan Gupta                     {
37862598e31SEd Tanous                         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
37906785244SRatan Gupta                         messages::internalError(asyncResp->res);
38006785244SRatan Gupta                         return;
38106785244SRatan Gupta                     }
38289492a15SPatrick Williams                     asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"]
38389492a15SPatrick Williams                                             [index] = nullptr;
38406785244SRatan Gupta                     },
38506785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
38606785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
38706785244SRatan Gupta             }
38806785244SRatan Gupta             else
38906785244SRatan Gupta             {
39062598e31SEd Tanous                 BMCWEB_LOG_ERROR("Can't delete the object");
3912e8c4bdaSEd Tanous                 messages::propertyValueTypeError(asyncResp->res, thisJson,
3922e8c4bdaSEd Tanous                                                  "RemoteRoleMapping/" +
3932e8c4bdaSEd Tanous                                                      std::to_string(index));
39406785244SRatan Gupta                 return;
39506785244SRatan Gupta             }
39606785244SRatan Gupta         }
39706785244SRatan Gupta         else if (thisJson.empty())
39806785244SRatan Gupta         {
39906785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
40006785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
40106785244SRatan Gupta         }
40206785244SRatan Gupta         else
40306785244SRatan Gupta         {
40406785244SRatan Gupta             // update/create the object
40506785244SRatan Gupta             std::optional<std::string> remoteGroup;
40606785244SRatan Gupta             std::optional<std::string> localRole;
40706785244SRatan Gupta 
408f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
409f23b7296SEd Tanous             // readJson is structured
410f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
411f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
412f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
413f23b7296SEd Tanous                                      localRole))
41406785244SRatan Gupta             {
41506785244SRatan Gupta                 continue;
41606785244SRatan Gupta             }
41706785244SRatan Gupta 
41806785244SRatan Gupta             // Update existing RoleMapping Object
41906785244SRatan Gupta             if (index < roleMapObjData.size())
42006785244SRatan Gupta             {
42162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Update Role Map Object");
42206785244SRatan Gupta                 // If "RemoteGroup" info is provided
42306785244SRatan Gupta                 if (remoteGroup)
42406785244SRatan Gupta                 {
4259ae226faSGeorge Liu                     sdbusplus::asio::setProperty(
4269ae226faSGeorge Liu                         *crow::connections::systemBus, ldapDbusService,
4279ae226faSGeorge Liu                         roleMapObjData[index].first,
4289ae226faSGeorge Liu                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
4299ae226faSGeorge Liu                         "GroupName", *remoteGroup,
43006785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
43125e055a3SRavi Teja                          remoteGroup](const boost::system::error_code& ec,
4327a543894SPatrick Williams                                       const sdbusplus::message_t& msg) {
43306785244SRatan Gupta                         if (ec)
43406785244SRatan Gupta                         {
43525e055a3SRavi Teja                             const sd_bus_error* dbusError = msg.get_error();
43625e055a3SRavi Teja                             if ((dbusError != nullptr) &&
43725e055a3SRavi Teja                                 (dbusError->name ==
43825e055a3SRavi Teja                                  std::string_view(
43925e055a3SRavi Teja                                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
44025e055a3SRavi Teja                             {
44162598e31SEd Tanous                                 BMCWEB_LOG_WARNING("DBUS response error: {}",
44262598e31SEd Tanous                                                    ec);
44325e055a3SRavi Teja                                 messages::propertyValueIncorrect(asyncResp->res,
44425e055a3SRavi Teja                                                                  "RemoteGroup",
44525e055a3SRavi Teja                                                                  *remoteGroup);
44625e055a3SRavi Teja                                 return;
44725e055a3SRavi Teja                             }
44806785244SRatan Gupta                             messages::internalError(asyncResp->res);
44906785244SRatan Gupta                             return;
45006785244SRatan Gupta                         }
45106785244SRatan Gupta                         asyncResp->res
452002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
453002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
4549ae226faSGeorge Liu                         });
45506785244SRatan Gupta                 }
45606785244SRatan Gupta 
45706785244SRatan Gupta                 // If "LocalRole" info is provided
45806785244SRatan Gupta                 if (localRole)
45906785244SRatan Gupta                 {
4609ae226faSGeorge Liu                     sdbusplus::asio::setProperty(
4619ae226faSGeorge Liu                         *crow::connections::systemBus, ldapDbusService,
4629ae226faSGeorge Liu                         roleMapObjData[index].first,
4639ae226faSGeorge Liu                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
4649ae226faSGeorge Liu                         "Privilege", *localRole,
46506785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
46625e055a3SRavi Teja                          localRole](const boost::system::error_code& ec,
4677a543894SPatrick Williams                                     const sdbusplus::message_t& msg) {
46806785244SRatan Gupta                         if (ec)
46906785244SRatan Gupta                         {
47025e055a3SRavi Teja                             const sd_bus_error* dbusError = msg.get_error();
47125e055a3SRavi Teja                             if ((dbusError != nullptr) &&
47225e055a3SRavi Teja                                 (dbusError->name ==
47325e055a3SRavi Teja                                  std::string_view(
47425e055a3SRavi Teja                                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
47525e055a3SRavi Teja                             {
47662598e31SEd Tanous                                 BMCWEB_LOG_WARNING("DBUS response error: {}",
47762598e31SEd Tanous                                                    ec);
47825e055a3SRavi Teja                                 messages::propertyValueIncorrect(
47925e055a3SRavi Teja                                     asyncResp->res, "LocalRole", *localRole);
48025e055a3SRavi Teja                                 return;
48125e055a3SRavi Teja                             }
48206785244SRatan Gupta                             messages::internalError(asyncResp->res);
48306785244SRatan Gupta                             return;
48406785244SRatan Gupta                         }
48506785244SRatan Gupta                         asyncResp->res
486002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
487002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
4889ae226faSGeorge Liu                         });
48906785244SRatan Gupta                 }
49006785244SRatan Gupta             }
49106785244SRatan Gupta             // Create a new RoleMapping Object.
49206785244SRatan Gupta             else
49306785244SRatan Gupta             {
49462598e31SEd Tanous                 BMCWEB_LOG_DEBUG(
49562598e31SEd Tanous                     "setRoleMappingProperties: Creating new Object");
49689492a15SPatrick Williams                 std::string pathString = "RemoteRoleMapping/" +
49789492a15SPatrick Williams                                          std::to_string(index);
49806785244SRatan Gupta 
49906785244SRatan Gupta                 if (!localRole)
50006785244SRatan Gupta                 {
50106785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
50206785244SRatan Gupta                                               pathString + "/LocalRole");
50306785244SRatan Gupta                     continue;
50406785244SRatan Gupta                 }
50506785244SRatan Gupta                 if (!remoteGroup)
50606785244SRatan Gupta                 {
50706785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
50806785244SRatan Gupta                                               pathString + "/RemoteGroup");
50906785244SRatan Gupta                     continue;
51006785244SRatan Gupta                 }
51106785244SRatan Gupta 
51206785244SRatan Gupta                 std::string dbusObjectPath;
51306785244SRatan Gupta                 if (serverType == "ActiveDirectory")
51406785244SRatan Gupta                 {
5152c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
51606785244SRatan Gupta                 }
51706785244SRatan Gupta                 else if (serverType == "LDAP")
51806785244SRatan Gupta                 {
51923a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
52006785244SRatan Gupta                 }
52106785244SRatan Gupta 
52262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Remote Group={},LocalRole={}", *remoteGroup,
52362598e31SEd Tanous                                  *localRole);
52406785244SRatan Gupta 
52506785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
526271584abSEd Tanous                     [asyncResp, serverType, localRole,
5275e7e2dc5SEd Tanous                      remoteGroup](const boost::system::error_code& ec) {
52806785244SRatan Gupta                     if (ec)
52906785244SRatan Gupta                     {
53062598e31SEd Tanous                         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
53106785244SRatan Gupta                         messages::internalError(asyncResp->res);
53206785244SRatan Gupta                         return;
53306785244SRatan Gupta                     }
53406785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
53506785244SRatan Gupta                         asyncResp->res
53606785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
5371476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
5381476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
5391476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
540b2ba3072SPatrick Williams                     remoteRoleJson.emplace_back(std::move(roleMapEntry));
54106785244SRatan Gupta                     },
54206785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
5433174e4dfSEd Tanous                     "Create", *remoteGroup,
54406785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
54506785244SRatan Gupta             }
54606785244SRatan Gupta         }
54706785244SRatan Gupta     }
54806785244SRatan Gupta }
54906785244SRatan Gupta 
55006785244SRatan Gupta /**
5516973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
5526973a582SRatan Gupta  * into JSON
5536973a582SRatan Gupta  */
5546973a582SRatan Gupta template <typename CallbackFunc>
5556973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
5566973a582SRatan Gupta                               CallbackFunc&& callback)
5576973a582SRatan Gupta {
5582b73119cSGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
5592b73119cSGeorge Liu         ldapEnableInterface, ldapConfigInterface};
56054fc587aSNagaraju Goruganti 
5612b73119cSGeorge Liu     dbus::utility::getDbusObject(
5622b73119cSGeorge Liu         ldapConfigObjectName, interfaces,
5632b73119cSGeorge Liu         [callback, ldapType](const boost::system::error_code& ec,
564b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
56554fc587aSNagaraju Goruganti         if (ec || resp.empty())
56654fc587aSNagaraju Goruganti         {
56762598e31SEd Tanous             BMCWEB_LOG_ERROR(
56862598e31SEd Tanous                 "DBUS response error during getting of service name: {}", ec);
56923a21a1cSEd Tanous             LDAPConfigData empty{};
57023a21a1cSEd Tanous             callback(false, empty, ldapType);
57154fc587aSNagaraju Goruganti             return;
57254fc587aSNagaraju Goruganti         }
57354fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
5745eb468daSGeorge Liu         sdbusplus::message::object_path path(ldapRootObject);
5755eb468daSGeorge Liu         dbus::utility::getManagedObjects(
5765eb468daSGeorge Liu             service, path,
577002d39b4SEd Tanous             [callback,
5788b24275dSEd Tanous              ldapType](const boost::system::error_code& ec2,
579711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
5806973a582SRatan Gupta             LDAPConfigData confData{};
5818b24275dSEd Tanous             if (ec2)
5826973a582SRatan Gupta             {
583ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
58462598e31SEd Tanous                 BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec2);
5856973a582SRatan Gupta                 return;
5866973a582SRatan Gupta             }
587ab828d7cSRatan Gupta 
588ab828d7cSRatan Gupta             std::string ldapDbusType;
58954fc587aSNagaraju Goruganti             std::string searchString;
59054fc587aSNagaraju Goruganti 
591ab828d7cSRatan Gupta             if (ldapType == "LDAP")
592ab828d7cSRatan Gupta             {
5930fda0f12SGeorge Liu                 ldapDbusType =
5940fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
59554fc587aSNagaraju Goruganti                 searchString = "openldap";
596ab828d7cSRatan Gupta             }
597ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
598ab828d7cSRatan Gupta             {
59954fc587aSNagaraju Goruganti                 ldapDbusType =
6000fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
60154fc587aSNagaraju Goruganti                 searchString = "active_directory";
602ab828d7cSRatan Gupta             }
603ab828d7cSRatan Gupta             else
604ab828d7cSRatan Gupta             {
60562598e31SEd Tanous                 BMCWEB_LOG_ERROR("Can't get the DbusType for the given type={}",
60662598e31SEd Tanous                                  ldapType);
607ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
608ab828d7cSRatan Gupta                 return;
609ab828d7cSRatan Gupta             }
610ab828d7cSRatan Gupta 
611ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
612ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
613ab828d7cSRatan Gupta 
6146973a582SRatan Gupta             for (const auto& object : ldapObjects)
6156973a582SRatan Gupta             {
61654fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
61754fc587aSNagaraju Goruganti                 // given type
618002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
6196973a582SRatan Gupta                 {
620ab828d7cSRatan Gupta                     continue;
621ab828d7cSRatan Gupta                 }
622ab828d7cSRatan Gupta 
6236973a582SRatan Gupta                 for (const auto& interface : object.second)
6246973a582SRatan Gupta                 {
6256973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
6266973a582SRatan Gupta                     {
6276973a582SRatan Gupta                         // rest of the properties are string.
6286973a582SRatan Gupta                         for (const auto& property : interface.second)
6296973a582SRatan Gupta                         {
6306973a582SRatan Gupta                             if (property.first == "Enabled")
6316973a582SRatan Gupta                             {
6326973a582SRatan Gupta                                 const bool* value =
6336973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
6346973a582SRatan Gupta                                 if (value == nullptr)
6356973a582SRatan Gupta                                 {
6366973a582SRatan Gupta                                     continue;
6376973a582SRatan Gupta                                 }
6386973a582SRatan Gupta                                 confData.serviceEnabled = *value;
6396973a582SRatan Gupta                                 break;
6406973a582SRatan Gupta                             }
6416973a582SRatan Gupta                         }
6426973a582SRatan Gupta                     }
6436973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
6446973a582SRatan Gupta                     {
6456973a582SRatan Gupta                         for (const auto& property : interface.second)
6466973a582SRatan Gupta                         {
647271584abSEd Tanous                             const std::string* strValue =
648002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
649271584abSEd Tanous                             if (strValue == nullptr)
6506973a582SRatan Gupta                             {
6516973a582SRatan Gupta                                 continue;
6526973a582SRatan Gupta                             }
6536973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
6546973a582SRatan Gupta                             {
655271584abSEd Tanous                                 confData.uri = *strValue;
6566973a582SRatan Gupta                             }
6576973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
6586973a582SRatan Gupta                             {
659271584abSEd Tanous                                 confData.bindDN = *strValue;
6606973a582SRatan Gupta                             }
6616973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
6626973a582SRatan Gupta                             {
663271584abSEd Tanous                                 confData.baseDN = *strValue;
6646973a582SRatan Gupta                             }
665002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
6666973a582SRatan Gupta                             {
667271584abSEd Tanous                                 confData.searchScope = *strValue;
6686973a582SRatan Gupta                             }
669002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
6706973a582SRatan Gupta                             {
671271584abSEd Tanous                                 confData.groupAttribute = *strValue;
6726973a582SRatan Gupta                             }
673002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
6746973a582SRatan Gupta                             {
675271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
6766973a582SRatan Gupta                             }
67754fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
678ab828d7cSRatan Gupta                             {
679271584abSEd Tanous                                 confData.serverType = *strValue;
68054fc587aSNagaraju Goruganti                             }
68154fc587aSNagaraju Goruganti                         }
68254fc587aSNagaraju Goruganti                     }
683002d39b4SEd Tanous                     else if (interface.first ==
6840fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
68554fc587aSNagaraju Goruganti                     {
68654fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
68754fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
68854fc587aSNagaraju Goruganti                         {
689271584abSEd Tanous                             const std::string* strValue =
690002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
69154fc587aSNagaraju Goruganti 
692271584abSEd Tanous                             if (strValue == nullptr)
69354fc587aSNagaraju Goruganti                             {
69454fc587aSNagaraju Goruganti                                 continue;
69554fc587aSNagaraju Goruganti                             }
69654fc587aSNagaraju Goruganti 
69754fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
69854fc587aSNagaraju Goruganti                             {
699271584abSEd Tanous                                 roleMapData.groupName = *strValue;
70054fc587aSNagaraju Goruganti                             }
70154fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
70254fc587aSNagaraju Goruganti                             {
703271584abSEd Tanous                                 roleMapData.privilege = *strValue;
70454fc587aSNagaraju Goruganti                             }
70554fc587aSNagaraju Goruganti                         }
70654fc587aSNagaraju Goruganti 
707002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
708002d39b4SEd Tanous                                                             roleMapData);
70954fc587aSNagaraju Goruganti                     }
71054fc587aSNagaraju Goruganti                 }
71154fc587aSNagaraju Goruganti             }
712ab828d7cSRatan Gupta             callback(true, confData, ldapType);
7135eb468daSGeorge Liu             });
7142b73119cSGeorge Liu         });
7156973a582SRatan Gupta }
7166973a582SRatan Gupta 
7178a07d286SRatan Gupta /**
7188a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
7198a07d286SRatan Gupta  * @param input JSON data
7208a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7218a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
7228a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
7238a07d286SRatan Gupta  */
7244f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
7256c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7266c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
7278a07d286SRatan Gupta {
7288a07d286SRatan Gupta     std::optional<std::string> authType;
7298a07d286SRatan Gupta 
7308a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
7318a07d286SRatan Gupta                              authType, "Username", username, "Password",
7328a07d286SRatan Gupta                              password))
7338a07d286SRatan Gupta     {
7348a07d286SRatan Gupta         return;
7358a07d286SRatan Gupta     }
7368a07d286SRatan Gupta     if (!authType)
7378a07d286SRatan Gupta     {
7388a07d286SRatan Gupta         return;
7398a07d286SRatan Gupta     }
7408a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
7418a07d286SRatan Gupta     {
7428a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
7438a07d286SRatan Gupta                                          "AuthenticationType");
7448a07d286SRatan Gupta         return;
7458a07d286SRatan Gupta     }
7468a07d286SRatan Gupta }
7478a07d286SRatan Gupta /**
7488a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
7498a07d286SRatan Gupta  * @param input JSON data
7508a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7518a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
7528a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
7538a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
7548a07d286SRatan Gupta  */
7558a07d286SRatan Gupta 
7564f48d5f6SEd Tanous inline void
7574f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
7588d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7598a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
7608a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
7618a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
7628a07d286SRatan Gupta {
7638a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
7648a07d286SRatan Gupta 
7658a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
7668a07d286SRatan Gupta                              searchSettings))
7678a07d286SRatan Gupta     {
7688a07d286SRatan Gupta         return;
7698a07d286SRatan Gupta     }
7708a07d286SRatan Gupta     if (!searchSettings)
7718a07d286SRatan Gupta     {
7728a07d286SRatan Gupta         return;
7738a07d286SRatan Gupta     }
7748a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
7758a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
7768a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
7778a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
7788a07d286SRatan Gupta     {
7798a07d286SRatan Gupta         return;
7808a07d286SRatan Gupta     }
7818a07d286SRatan Gupta }
7828a07d286SRatan Gupta /**
7838a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
7848a07d286SRatan Gupta           json response with the new value.
7858a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
7868a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7878a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7888a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7898a07d286SRatan Gupta  */
7908a07d286SRatan Gupta 
7914f48d5f6SEd Tanous inline void handleServiceAddressPatch(
7928a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
7938d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7948a07d286SRatan Gupta     const std::string& ldapServerElementName,
7958a07d286SRatan Gupta     const std::string& ldapConfigObject)
7968a07d286SRatan Gupta {
7979ae226faSGeorge Liu     sdbusplus::asio::setProperty(
7989ae226faSGeorge Liu         *crow::connections::systemBus, ldapDbusService, ldapConfigObject,
7999ae226faSGeorge Liu         ldapConfigInterface, "LDAPServerURI", serviceAddressList.front(),
8007a543894SPatrick Williams         [asyncResp, ldapServerElementName, serviceAddressList](
8017a543894SPatrick Williams             const boost::system::error_code& ec, sdbusplus::message_t& msg) {
8028a07d286SRatan Gupta         if (ec)
8038a07d286SRatan Gupta         {
80425e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
80525e055a3SRavi Teja             if ((dbusError != nullptr) &&
80625e055a3SRavi Teja                 (dbusError->name ==
80725e055a3SRavi Teja                  std::string_view(
80825e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
80925e055a3SRavi Teja             {
81062598e31SEd Tanous                 BMCWEB_LOG_WARNING(
81162598e31SEd Tanous                     "Error Occurred in updating the service address");
81225e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
81325e055a3SRavi Teja                                                  "ServiceAddresses",
81425e055a3SRavi Teja                                                  serviceAddressList.front());
81525e055a3SRavi Teja                 return;
81625e055a3SRavi Teja             }
8178a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8188a07d286SRatan Gupta             return;
8198a07d286SRatan Gupta         }
8208a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
8218a07d286SRatan Gupta             serviceAddressList.front()};
822002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
8238a07d286SRatan Gupta             modifiedserviceAddressList;
8248a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
8258a07d286SRatan Gupta         {
826002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
8278a07d286SRatan Gupta                                             serviceAddressList.front());
8288a07d286SRatan Gupta         }
82962598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the service address");
8309ae226faSGeorge Liu         });
8318a07d286SRatan Gupta }
8328a07d286SRatan Gupta /**
8338a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
8348a07d286SRatan Gupta           json response with the new value.
8358a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
8368a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8378a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8388a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8398a07d286SRatan Gupta  */
8408a07d286SRatan Gupta 
8414f48d5f6SEd Tanous inline void
8424f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
8438d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8448a07d286SRatan Gupta                         const std::string& ldapServerElementName,
8458a07d286SRatan Gupta                         const std::string& ldapConfigObject)
8468a07d286SRatan Gupta {
8479ae226faSGeorge Liu     sdbusplus::asio::setProperty(*crow::connections::systemBus, ldapDbusService,
8489ae226faSGeorge Liu                                  ldapConfigObject, ldapConfigInterface,
8499ae226faSGeorge Liu                                  "LDAPBindDN", username,
8509ae226faSGeorge Liu                                  [asyncResp, username, ldapServerElementName](
8519ae226faSGeorge Liu                                      const boost::system::error_code& ec) {
8528a07d286SRatan Gupta         if (ec)
8538a07d286SRatan Gupta         {
85462598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error occurred in updating the username");
8558a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8568a07d286SRatan Gupta             return;
8578a07d286SRatan Gupta         }
85889492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
85989492a15SPatrick Williams                                 ["Username"] = username;
86062598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the username");
8619ae226faSGeorge Liu     });
8628a07d286SRatan Gupta }
8638a07d286SRatan Gupta 
8648a07d286SRatan Gupta /**
8658a07d286SRatan Gupta  * @brief updates the LDAP password
8668a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
8678a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8688a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8698a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
8708a07d286SRatan Gupta  */
8718a07d286SRatan Gupta 
8724f48d5f6SEd Tanous inline void
8734f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
8748d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8758a07d286SRatan Gupta                         const std::string& ldapServerElementName,
8768a07d286SRatan Gupta                         const std::string& ldapConfigObject)
8778a07d286SRatan Gupta {
8789ae226faSGeorge Liu     sdbusplus::asio::setProperty(*crow::connections::systemBus, ldapDbusService,
8799ae226faSGeorge Liu                                  ldapConfigObject, ldapConfigInterface,
8809ae226faSGeorge Liu                                  "LDAPBindDNPassword", password,
8819ae226faSGeorge Liu                                  [asyncResp, password, ldapServerElementName](
8829ae226faSGeorge Liu                                      const boost::system::error_code& ec) {
8838a07d286SRatan Gupta         if (ec)
8848a07d286SRatan Gupta         {
88562598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error occurred in updating the password");
8868a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8878a07d286SRatan Gupta             return;
8888a07d286SRatan Gupta         }
88989492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
89089492a15SPatrick Williams                                 ["Password"] = "";
89162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the password");
8929ae226faSGeorge Liu     });
8938a07d286SRatan Gupta }
8948a07d286SRatan Gupta 
8958a07d286SRatan Gupta /**
8968a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
8978a07d286SRatan Gupta           json response with the new value.
8988a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
8998a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9008a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9018a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9028a07d286SRatan Gupta  */
9038a07d286SRatan Gupta 
9044f48d5f6SEd Tanous inline void
9054f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
9068d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9078a07d286SRatan Gupta                       const std::string& ldapServerElementName,
9088a07d286SRatan Gupta                       const std::string& ldapConfigObject)
9098a07d286SRatan Gupta {
9109ae226faSGeorge Liu     sdbusplus::asio::setProperty(*crow::connections::systemBus, ldapDbusService,
9119ae226faSGeorge Liu                                  ldapConfigObject, ldapConfigInterface,
9129ae226faSGeorge Liu                                  "LDAPBaseDN", baseDNList.front(),
9139ae226faSGeorge Liu                                  [asyncResp, baseDNList, ldapServerElementName](
9149ae226faSGeorge Liu                                      const boost::system::error_code& ec,
9157a543894SPatrick Williams                                      const sdbusplus::message_t& msg) {
9168a07d286SRatan Gupta         if (ec)
9178a07d286SRatan Gupta         {
91862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error Occurred in Updating the base DN");
91925e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
92025e055a3SRavi Teja             if ((dbusError != nullptr) &&
92125e055a3SRavi Teja                 (dbusError->name ==
92225e055a3SRavi Teja                  std::string_view(
92325e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
92425e055a3SRavi Teja             {
92525e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
92625e055a3SRavi Teja                                                  "BaseDistinguishedNames",
92725e055a3SRavi Teja                                                  baseDNList.front());
92825e055a3SRavi Teja                 return;
92925e055a3SRavi Teja             }
9308a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9318a07d286SRatan Gupta             return;
9328a07d286SRatan Gupta         }
933002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
9348a07d286SRatan Gupta         auto& searchSettingsJson =
9358a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
9366c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
9376c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
9388a07d286SRatan Gupta         if (baseDNList.size() > 1)
9398a07d286SRatan Gupta         {
940002d39b4SEd Tanous             messages::propertyValueModified(
941002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
9428a07d286SRatan Gupta         }
94362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the base DN");
9449ae226faSGeorge Liu     });
9458a07d286SRatan Gupta }
9468a07d286SRatan Gupta /**
9478a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
9488a07d286SRatan Gupta           json response with the new value.
9498a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
9508a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9518a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9528a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9538a07d286SRatan Gupta  */
9548a07d286SRatan Gupta 
9554f48d5f6SEd Tanous inline void
9564f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
9578d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9588a07d286SRatan Gupta                             const std::string& ldapServerElementName,
9598a07d286SRatan Gupta                             const std::string& ldapConfigObject)
9608a07d286SRatan Gupta {
9619ae226faSGeorge Liu     sdbusplus::asio::setProperty(
9629ae226faSGeorge Liu         *crow::connections::systemBus, ldapDbusService, ldapConfigObject,
9639ae226faSGeorge Liu         ldapConfigInterface, "UserNameAttribute", userNameAttribute,
9648a07d286SRatan Gupta         [asyncResp, userNameAttribute,
9655e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
9668a07d286SRatan Gupta         if (ec)
9678a07d286SRatan Gupta         {
96862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error Occurred in Updating the "
96962598e31SEd Tanous                              "username attribute");
9708a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9718a07d286SRatan Gupta             return;
9728a07d286SRatan Gupta         }
973002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
9748a07d286SRatan Gupta         auto& searchSettingsJson =
9758a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
9768a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
97762598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the user name attr.");
9789ae226faSGeorge Liu         });
9798a07d286SRatan Gupta }
9808a07d286SRatan Gupta /**
9818a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
9828a07d286SRatan Gupta           json response with the new value.
9838a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
9848a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9858a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9868a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9878a07d286SRatan Gupta  */
9888a07d286SRatan Gupta 
9894f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
9908d1b46d7Szhanghch05     const std::string& groupsAttribute,
9918d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9928a07d286SRatan Gupta     const std::string& ldapServerElementName,
9938a07d286SRatan Gupta     const std::string& ldapConfigObject)
9948a07d286SRatan Gupta {
9959ae226faSGeorge Liu     sdbusplus::asio::setProperty(
9969ae226faSGeorge Liu         *crow::connections::systemBus, ldapDbusService, ldapConfigObject,
9979ae226faSGeorge Liu         ldapConfigInterface, "GroupNameAttribute", groupsAttribute,
9988a07d286SRatan Gupta         [asyncResp, groupsAttribute,
9995e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
10008a07d286SRatan Gupta         if (ec)
10018a07d286SRatan Gupta         {
100262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error Occurred in Updating the "
100362598e31SEd Tanous                              "groupname attribute");
10048a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10058a07d286SRatan Gupta             return;
10068a07d286SRatan Gupta         }
1007002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
10088a07d286SRatan Gupta         auto& searchSettingsJson =
10098a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
10108a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
101162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated the groupname attr");
10129ae226faSGeorge Liu         });
10138a07d286SRatan Gupta }
10148a07d286SRatan Gupta /**
10158a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
10168a07d286SRatan Gupta           json response with the new value.
10178a07d286SRatan Gupta  * @param input JSON data.
10188a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
10198a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
10208a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
10218a07d286SRatan Gupta  */
10228a07d286SRatan Gupta 
10234f48d5f6SEd Tanous inline void handleServiceEnablePatch(
10246c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10258a07d286SRatan Gupta     const std::string& ldapServerElementName,
10268a07d286SRatan Gupta     const std::string& ldapConfigObject)
10278a07d286SRatan Gupta {
10289ae226faSGeorge Liu     sdbusplus::asio::setProperty(
10299ae226faSGeorge Liu         *crow::connections::systemBus, ldapDbusService, ldapConfigObject,
10309ae226faSGeorge Liu         ldapEnableInterface, "Enabled", serviceEnabled,
10318a07d286SRatan Gupta         [asyncResp, serviceEnabled,
10325e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
10338a07d286SRatan Gupta         if (ec)
10348a07d286SRatan Gupta         {
103562598e31SEd Tanous             BMCWEB_LOG_DEBUG("Error Occurred in Updating the service enable");
10368a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10378a07d286SRatan Gupta             return;
10388a07d286SRatan Gupta         }
10396c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
10408a07d286SRatan Gupta             serviceEnabled;
104162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Updated Service enable = {}", serviceEnabled);
10429ae226faSGeorge Liu         });
10438a07d286SRatan Gupta }
10448a07d286SRatan Gupta 
10454f48d5f6SEd Tanous inline void
10464f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
10478d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
104878158631SZbigniew Kurzynski {
104978158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
105078158631SZbigniew Kurzynski     std::optional<bool> cookie;
105178158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
105278158631SZbigniew Kurzynski     std::optional<bool> xToken;
1053501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
105478158631SZbigniew Kurzynski 
105578158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
105678158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
1057501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
105878158631SZbigniew Kurzynski     {
105962598e31SEd Tanous         BMCWEB_LOG_ERROR("Cannot read values from AuthMethod tag");
106078158631SZbigniew Kurzynski         return;
106178158631SZbigniew Kurzynski     }
106278158631SZbigniew Kurzynski 
106378158631SZbigniew Kurzynski     // Make a copy of methods configuration
106452cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
106552cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
106678158631SZbigniew Kurzynski 
106778158631SZbigniew Kurzynski     if (basicAuth)
106878158631SZbigniew Kurzynski     {
1069f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
1070f16f6263SAlan Kuo         messages::actionNotSupported(
10710fda0f12SGeorge Liu             asyncResp->res,
10720fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
1073f16f6263SAlan Kuo         return;
1074f16f6263SAlan Kuo #endif
107578158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
107678158631SZbigniew Kurzynski     }
107778158631SZbigniew Kurzynski 
107878158631SZbigniew Kurzynski     if (cookie)
107978158631SZbigniew Kurzynski     {
1080f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
10810fda0f12SGeorge Liu         messages::actionNotSupported(
10820fda0f12SGeorge Liu             asyncResp->res,
10830fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
1084f16f6263SAlan Kuo         return;
1085f16f6263SAlan Kuo #endif
108678158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
108778158631SZbigniew Kurzynski     }
108878158631SZbigniew Kurzynski 
108978158631SZbigniew Kurzynski     if (sessionToken)
109078158631SZbigniew Kurzynski     {
1091f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
1092f16f6263SAlan Kuo         messages::actionNotSupported(
10930fda0f12SGeorge Liu             asyncResp->res,
10940fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
1095f16f6263SAlan Kuo         return;
1096f16f6263SAlan Kuo #endif
109778158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
109878158631SZbigniew Kurzynski     }
109978158631SZbigniew Kurzynski 
110078158631SZbigniew Kurzynski     if (xToken)
110178158631SZbigniew Kurzynski     {
1102f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
11030fda0f12SGeorge Liu         messages::actionNotSupported(
11040fda0f12SGeorge Liu             asyncResp->res,
11050fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
1106f16f6263SAlan Kuo         return;
1107f16f6263SAlan Kuo #endif
110878158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
110978158631SZbigniew Kurzynski     }
111078158631SZbigniew Kurzynski 
1111501f1e58SZbigniew Kurzynski     if (tls)
1112501f1e58SZbigniew Kurzynski     {
1113f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
11140fda0f12SGeorge Liu         messages::actionNotSupported(
11150fda0f12SGeorge Liu             asyncResp->res,
11160fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
1117f16f6263SAlan Kuo         return;
1118f16f6263SAlan Kuo #endif
1119501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
1120501f1e58SZbigniew Kurzynski     }
1121501f1e58SZbigniew Kurzynski 
112278158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
1123501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
1124501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
112578158631SZbigniew Kurzynski     {
112678158631SZbigniew Kurzynski         // Do not allow user to disable everything
112778158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
112878158631SZbigniew Kurzynski                                      "of disabling all available methods");
112978158631SZbigniew Kurzynski         return;
113078158631SZbigniew Kurzynski     }
113178158631SZbigniew Kurzynski 
113252cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
113352cc112dSEd Tanous         authMethodsConfig);
113478158631SZbigniew Kurzynski     // Save configuration immediately
113552cc112dSEd Tanous     persistent_data::getConfig().writeData();
113678158631SZbigniew Kurzynski 
113778158631SZbigniew Kurzynski     messages::success(asyncResp->res);
113878158631SZbigniew Kurzynski }
113978158631SZbigniew Kurzynski 
11408a07d286SRatan Gupta /**
11418a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
11428a07d286SRatan Gupta  *        value and create the LDAP config object.
11438a07d286SRatan Gupta  * @param input JSON data
11448a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
11458a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
11468a07d286SRatan Gupta  */
11478a07d286SRatan Gupta 
11486c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
11498d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
11508a07d286SRatan Gupta                             const std::string& serverType)
11518a07d286SRatan Gupta {
1152eb2bbe56SRatan Gupta     std::string dbusObjectPath;
1153eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
1154eb2bbe56SRatan Gupta     {
11552c70f800SEd Tanous         dbusObjectPath = adConfigObject;
1156eb2bbe56SRatan Gupta     }
1157eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
1158eb2bbe56SRatan Gupta     {
115923a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
1160eb2bbe56SRatan Gupta     }
1161cb13a392SEd Tanous     else
1162cb13a392SEd Tanous     {
1163cb13a392SEd Tanous         return;
1164cb13a392SEd Tanous     }
1165eb2bbe56SRatan Gupta 
11668a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
11678a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
11688a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
11698a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
11708a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
11718a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
11728a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
11738a07d286SRatan Gupta     std::optional<std::string> userName;
11748a07d286SRatan Gupta     std::optional<std::string> password;
117506785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
11768a07d286SRatan Gupta 
11778a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
11788a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
11798a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
118006785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
118106785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
11828a07d286SRatan Gupta     {
11838a07d286SRatan Gupta         return;
11848a07d286SRatan Gupta     }
11858a07d286SRatan Gupta 
11868a07d286SRatan Gupta     if (authentication)
11878a07d286SRatan Gupta     {
11888a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
11898a07d286SRatan Gupta                                     password);
11908a07d286SRatan Gupta     }
11918a07d286SRatan Gupta     if (ldapService)
11928a07d286SRatan Gupta     {
11938a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
11948a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
11958a07d286SRatan Gupta     }
11968a07d286SRatan Gupta     if (serviceAddressList)
11978a07d286SRatan Gupta     {
119826f6976fSEd Tanous         if (serviceAddressList->empty())
11998a07d286SRatan Gupta         {
1200e2616cc5SEd Tanous             messages::propertyValueNotInList(
1201e2616cc5SEd Tanous                 asyncResp->res, *serviceAddressList, "ServiceAddress");
12028a07d286SRatan Gupta             return;
12038a07d286SRatan Gupta         }
12048a07d286SRatan Gupta     }
12058a07d286SRatan Gupta     if (baseDNList)
12068a07d286SRatan Gupta     {
120726f6976fSEd Tanous         if (baseDNList->empty())
12088a07d286SRatan Gupta         {
1209e2616cc5SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *baseDNList,
12108a07d286SRatan Gupta                                              "BaseDistinguishedNames");
12118a07d286SRatan Gupta             return;
12128a07d286SRatan Gupta         }
12138a07d286SRatan Gupta     }
12148a07d286SRatan Gupta 
12158a07d286SRatan Gupta     // nothing to update, then return
12168a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
121706785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
121806785244SRatan Gupta         !remoteRoleMapData)
12198a07d286SRatan Gupta     {
12208a07d286SRatan Gupta         return;
12218a07d286SRatan Gupta     }
12228a07d286SRatan Gupta 
12238a07d286SRatan Gupta     // Get the existing resource first then keep modifying
12248a07d286SRatan Gupta     // whenever any property gets updated.
1225002d39b4SEd Tanous     getLDAPConfigData(
1226002d39b4SEd Tanous         serverType,
1227002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1228002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1229002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
123023a21a1cSEd Tanous                             const std::string& serverT) {
12318a07d286SRatan Gupta         if (!success)
12328a07d286SRatan Gupta         {
12338a07d286SRatan Gupta             messages::internalError(asyncResp->res);
12348a07d286SRatan Gupta             return;
12358a07d286SRatan Gupta         }
12366c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
12378a07d286SRatan Gupta         if (confData.serviceEnabled)
12388a07d286SRatan Gupta         {
12398a07d286SRatan Gupta             // Disable the service first and update the rest of
12408a07d286SRatan Gupta             // the properties.
12416c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
12428a07d286SRatan Gupta         }
12438a07d286SRatan Gupta 
12448a07d286SRatan Gupta         if (serviceAddressList)
12458a07d286SRatan Gupta         {
12466c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
12476c51eab1SEd Tanous                                       dbusObjectPath);
12488a07d286SRatan Gupta         }
12498a07d286SRatan Gupta         if (userName)
12508a07d286SRatan Gupta         {
12516c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
12528a07d286SRatan Gupta         }
12538a07d286SRatan Gupta         if (password)
12548a07d286SRatan Gupta         {
12556c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
12568a07d286SRatan Gupta         }
12578a07d286SRatan Gupta 
12588a07d286SRatan Gupta         if (baseDNList)
12598a07d286SRatan Gupta         {
12606c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
12618a07d286SRatan Gupta         }
12628a07d286SRatan Gupta         if (userNameAttribute)
12638a07d286SRatan Gupta         {
12646c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
12656c51eab1SEd Tanous                                     dbusObjectPath);
12668a07d286SRatan Gupta         }
12678a07d286SRatan Gupta         if (groupsAttribute)
12688a07d286SRatan Gupta         {
12696c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
12706c51eab1SEd Tanous                                      dbusObjectPath);
12718a07d286SRatan Gupta         }
12728a07d286SRatan Gupta         if (serviceEnabled)
12738a07d286SRatan Gupta         {
12748a07d286SRatan Gupta             // if user has given the value as true then enable
12758a07d286SRatan Gupta             // the service. if user has given false then no-op
12768a07d286SRatan Gupta             // as service is already stopped.
12778a07d286SRatan Gupta             if (*serviceEnabled)
12788a07d286SRatan Gupta             {
12796c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
12806c51eab1SEd Tanous                                          dbusObjectPath);
12818a07d286SRatan Gupta             }
12828a07d286SRatan Gupta         }
12838a07d286SRatan Gupta         else
12848a07d286SRatan Gupta         {
12858a07d286SRatan Gupta             // if user has not given the service enabled value
12868a07d286SRatan Gupta             // then revert it to the same state as it was
12878a07d286SRatan Gupta             // before.
12888a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
128923a21a1cSEd Tanous                                      serverT, dbusObjectPath);
12908a07d286SRatan Gupta         }
129106785244SRatan Gupta 
129206785244SRatan Gupta         if (remoteRoleMapData)
129306785244SRatan Gupta         {
12946c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
12956c51eab1SEd Tanous                                *remoteRoleMapData);
129606785244SRatan Gupta         }
12978a07d286SRatan Gupta         });
12988a07d286SRatan Gupta }
1299d4b5443fSEd Tanous 
130058345856SAbhishek Patel inline void updateUserProperties(
130158345856SAbhishek Patel     std::shared_ptr<bmcweb::AsyncResp> asyncResp, const std::string& username,
1302618c14b4SEd Tanous     const std::optional<std::string>& password,
1303618c14b4SEd Tanous     const std::optional<bool>& enabled,
130458345856SAbhishek Patel     const std::optional<std::string>& roleId, const std::optional<bool>& locked,
130558345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes, bool userSelf)
13061abe55efSEd Tanous {
1307b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1308b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1309b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
13106c51eab1SEd Tanous 
13116c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1312618c14b4SEd Tanous         dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
131358345856SAbhishek Patel                          locked, accountTypes(std::move(accountTypes)),
131458345856SAbhishek Patel                          userSelf, asyncResp{std::move(asyncResp)}](int rc) {
1315e662eae8SEd Tanous             if (rc <= 0)
13166c51eab1SEd Tanous             {
1317d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
13186c51eab1SEd Tanous                                            username);
13196c51eab1SEd Tanous                 return;
13206c51eab1SEd Tanous             }
13216c51eab1SEd Tanous 
13226c51eab1SEd Tanous             if (password)
13236c51eab1SEd Tanous             {
13246c51eab1SEd Tanous                 int retval = pamUpdatePassword(username, *password);
13256c51eab1SEd Tanous 
13266c51eab1SEd Tanous                 if (retval == PAM_USER_UNKNOWN)
13276c51eab1SEd Tanous                 {
1328d8a5d5d8SJiaqing Zhao                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
13296c51eab1SEd Tanous                                                username);
13306c51eab1SEd Tanous                 }
13316c51eab1SEd Tanous                 else if (retval == PAM_AUTHTOK_ERR)
13326c51eab1SEd Tanous                 {
13336c51eab1SEd Tanous                     // If password is invalid
1334618c14b4SEd Tanous                     messages::propertyValueFormatError(asyncResp->res,
1335618c14b4SEd Tanous                                                        *password, "Password");
133662598e31SEd Tanous                     BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
13376c51eab1SEd Tanous                 }
13386c51eab1SEd Tanous                 else if (retval != PAM_SUCCESS)
13396c51eab1SEd Tanous                 {
13406c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
13416c51eab1SEd Tanous                     return;
13426c51eab1SEd Tanous                 }
1343e7b1b62bSEd Tanous                 else
1344e7b1b62bSEd Tanous                 {
1345e7b1b62bSEd Tanous                     messages::success(asyncResp->res);
1346e7b1b62bSEd Tanous                 }
13476c51eab1SEd Tanous             }
13486c51eab1SEd Tanous 
13496c51eab1SEd Tanous             if (enabled)
13506c51eab1SEd Tanous             {
13519ae226faSGeorge Liu                 sdbusplus::asio::setProperty(
13529ae226faSGeorge Liu                     *crow::connections::systemBus,
13539ae226faSGeorge Liu                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
13549ae226faSGeorge Liu                     "xyz.openbmc_project.User.Attributes", "UserEnabled",
13559ae226faSGeorge Liu                     *enabled, [asyncResp](const boost::system::error_code& ec) {
13566c51eab1SEd Tanous                         if (ec)
13576c51eab1SEd Tanous                         {
135862598e31SEd Tanous                             BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
13596c51eab1SEd Tanous                             messages::internalError(asyncResp->res);
13606c51eab1SEd Tanous                             return;
13616c51eab1SEd Tanous                         }
13626c51eab1SEd Tanous                         messages::success(asyncResp->res);
13639ae226faSGeorge Liu                     });
13646c51eab1SEd Tanous             }
13656c51eab1SEd Tanous 
13666c51eab1SEd Tanous             if (roleId)
13676c51eab1SEd Tanous             {
13686c51eab1SEd Tanous                 std::string priv = getPrivilegeFromRoleId(*roleId);
13696c51eab1SEd Tanous                 if (priv.empty())
13706c51eab1SEd Tanous                 {
1371e2616cc5SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, true,
1372e2616cc5SEd Tanous                                                      "Locked");
13736c51eab1SEd Tanous                     return;
13746c51eab1SEd Tanous                 }
13756c51eab1SEd Tanous 
13769ae226faSGeorge Liu                 sdbusplus::asio::setProperty(
13779ae226faSGeorge Liu                     *crow::connections::systemBus,
13789ae226faSGeorge Liu                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
13799ae226faSGeorge Liu                     "xyz.openbmc_project.User.Attributes", "UserPrivilege",
13809ae226faSGeorge Liu                     priv, [asyncResp](const boost::system::error_code& ec) {
13816c51eab1SEd Tanous                         if (ec)
13826c51eab1SEd Tanous                         {
138362598e31SEd Tanous                             BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
13846c51eab1SEd Tanous                             messages::internalError(asyncResp->res);
13856c51eab1SEd Tanous                             return;
13866c51eab1SEd Tanous                         }
13876c51eab1SEd Tanous                         messages::success(asyncResp->res);
13889ae226faSGeorge Liu                     });
13896c51eab1SEd Tanous             }
13906c51eab1SEd Tanous 
13916c51eab1SEd Tanous             if (locked)
13926c51eab1SEd Tanous             {
13936c51eab1SEd Tanous                 // admin can unlock the account which is locked by
13946c51eab1SEd Tanous                 // successive authentication failures but admin should
13956c51eab1SEd Tanous                 // not be allowed to lock an account.
13966c51eab1SEd Tanous                 if (*locked)
13976c51eab1SEd Tanous                 {
13986c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, "true",
13996c51eab1SEd Tanous                                                      "Locked");
14006c51eab1SEd Tanous                     return;
14016c51eab1SEd Tanous                 }
14026c51eab1SEd Tanous 
14039ae226faSGeorge Liu                 sdbusplus::asio::setProperty(
14049ae226faSGeorge Liu                     *crow::connections::systemBus,
14059ae226faSGeorge Liu                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
14069ae226faSGeorge Liu                     "xyz.openbmc_project.User.Attributes",
14079ae226faSGeorge Liu                     "UserLockedForFailedAttempt", *locked,
14085e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
14096c51eab1SEd Tanous                 if (ec)
14106c51eab1SEd Tanous                 {
141162598e31SEd Tanous                     BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
14126c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
14136c51eab1SEd Tanous                     return;
14146c51eab1SEd Tanous                 }
14156c51eab1SEd Tanous                 messages::success(asyncResp->res);
14169ae226faSGeorge Liu                     });
14176c51eab1SEd Tanous             }
141858345856SAbhishek Patel 
141958345856SAbhishek Patel             if (accountTypes)
142058345856SAbhishek Patel             {
142158345856SAbhishek Patel                 patchAccountTypes(*accountTypes, asyncResp, dbusObjectPath,
142258345856SAbhishek Patel                                   userSelf);
142358345856SAbhishek Patel             }
14246c51eab1SEd Tanous         });
14256c51eab1SEd Tanous }
14266c51eab1SEd Tanous 
14274c7d4d33SEd Tanous inline void handleAccountServiceHead(
14284c7d4d33SEd Tanous     App& app, const crow::Request& req,
14291ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14306c51eab1SEd Tanous {
14313ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
143245ca1b86SEd Tanous     {
143345ca1b86SEd Tanous         return;
143445ca1b86SEd Tanous     }
14354c7d4d33SEd Tanous     asyncResp->res.addHeader(
14364c7d4d33SEd Tanous         boost::beast::http::field::link,
14374c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
14384c7d4d33SEd Tanous }
14394c7d4d33SEd Tanous 
14404c7d4d33SEd Tanous inline void
14414c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
14424c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14434c7d4d33SEd Tanous {
1444afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1445afd369c6SJiaqing Zhao     {
1446afd369c6SJiaqing Zhao         return;
1447afd369c6SJiaqing Zhao     }
14483e72c202SNinad Palsule 
14493e72c202SNinad Palsule     if (req.session == nullptr)
14503e72c202SNinad Palsule     {
14513e72c202SNinad Palsule         messages::internalError(asyncResp->res);
14523e72c202SNinad Palsule         return;
14533e72c202SNinad Palsule     }
14543e72c202SNinad Palsule 
1455afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1456afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1457afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1458afd369c6SJiaqing Zhao 
145952cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
14601ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
146178158631SZbigniew Kurzynski 
14621476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
14631476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
14641476687dSEd Tanous     json["@odata.type"] = "#AccountService."
14651476687dSEd Tanous                           "v1_10_0.AccountService";
14661476687dSEd Tanous     json["Id"] = "AccountService";
14671476687dSEd Tanous     json["Name"] = "Account Service";
14681476687dSEd Tanous     json["Description"] = "Account Service";
14691476687dSEd Tanous     json["ServiceEnabled"] = true;
14701476687dSEd Tanous     json["MaxPasswordLength"] = 20;
14711ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
14721476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
14731476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
14745b5574acSEd Tanous         "#OpenBMCAccountService.v1_0_0.AccountService";
14751476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
14761476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
14771476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
14781476687dSEd Tanous         authMethodsConfig.basic;
14791476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
14801476687dSEd Tanous         authMethodsConfig.sessionToken;
14811ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
14821ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
14831ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
14841476687dSEd Tanous 
14851ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
14861ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
14871ef4c342SEd Tanous     // permissions ConfigureManager
148872048780SAbhishek Patel     Privileges effectiveUserPrivileges =
14893e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
149072048780SAbhishek Patel 
149172048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
149272048780SAbhishek Patel                                          effectiveUserPrivileges))
149372048780SAbhishek Patel     {
14941ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
14951476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
149672048780SAbhishek Patel     }
1497d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1498d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1499d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
15005e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
15011ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
15023d958bbcSAppaRao Puli         if (ec)
15033d958bbcSAppaRao Puli         {
15043d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
15053d958bbcSAppaRao Puli             return;
15063d958bbcSAppaRao Puli         }
1507d1bde9e5SKrzysztof Grobelny 
150862598e31SEd Tanous         BMCWEB_LOG_DEBUG("Got {}properties for AccountService",
150962598e31SEd Tanous                          propertiesList.size());
1510d1bde9e5SKrzysztof Grobelny 
1511d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1512d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1513d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1514d1bde9e5SKrzysztof Grobelny 
1515d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1516d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1517d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1518d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1519d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1520d1bde9e5SKrzysztof Grobelny 
1521d1bde9e5SKrzysztof Grobelny         if (!success)
15223d958bbcSAppaRao Puli         {
1523d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1524d1bde9e5SKrzysztof Grobelny             return;
15253d958bbcSAppaRao Puli         }
1526d1bde9e5SKrzysztof Grobelny 
1527d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
15283d958bbcSAppaRao Puli         {
1529d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
15303d958bbcSAppaRao Puli         }
1531d1bde9e5SKrzysztof Grobelny 
1532d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
15333d958bbcSAppaRao Puli         {
1534d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1535d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1536d1bde9e5SKrzysztof Grobelny         }
1537d1bde9e5SKrzysztof Grobelny 
1538d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
15393d958bbcSAppaRao Puli         {
1540002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1541d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
15423d958bbcSAppaRao Puli         }
1543d1bde9e5SKrzysztof Grobelny         });
15446973a582SRatan Gupta 
154502cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1546ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1547cb13a392SEd Tanous         if (!success)
1548cb13a392SEd Tanous         {
1549cb13a392SEd Tanous             return;
1550cb13a392SEd Tanous         }
1551002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1552ab828d7cSRatan Gupta     };
1553ab828d7cSRatan Gupta 
1554ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1555ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
15561ef4c342SEd Tanous }
15576973a582SRatan Gupta 
15581ef4c342SEd Tanous inline void handleAccountServicePatch(
15591ef4c342SEd Tanous     App& app, const crow::Request& req,
15601ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15611ef4c342SEd Tanous {
15623ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
156345ca1b86SEd Tanous     {
156445ca1b86SEd Tanous         return;
156545ca1b86SEd Tanous     }
1566f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1567f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1568ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1569f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1570f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1571f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1572f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1573f5ffd806SEd Tanous 
157415ed6780SWilly Tu     if (!json_util::readJsonPatch(
15751ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
15761ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
15771ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
15781ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1579f5ffd806SEd Tanous             oemObject))
1580f5ffd806SEd Tanous     {
1581f5ffd806SEd Tanous         return;
1582f5ffd806SEd Tanous     }
1583f5ffd806SEd Tanous 
1584f5ffd806SEd Tanous     if (minPasswordLength)
1585f5ffd806SEd Tanous     {
15869ae226faSGeorge Liu         sdbusplus::asio::setProperty(
15879ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
15889ae226faSGeorge Liu             "/xyz/openbmc_project/user",
15899ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
15909ae226faSGeorge Liu             *minPasswordLength,
15915e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1592ef73ad0dSPaul Fertser             if (ec)
1593ef73ad0dSPaul Fertser             {
1594ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1595ef73ad0dSPaul Fertser                 return;
1596ef73ad0dSPaul Fertser             }
1597ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
15989ae226faSGeorge Liu             });
1599f5ffd806SEd Tanous     }
1600f5ffd806SEd Tanous 
1601f5ffd806SEd Tanous     if (maxPasswordLength)
1602f5ffd806SEd Tanous     {
16031ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1604f5ffd806SEd Tanous     }
1605f5ffd806SEd Tanous 
1606f5ffd806SEd Tanous     if (ldapObject)
1607f5ffd806SEd Tanous     {
1608f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1609f5ffd806SEd Tanous     }
1610f5ffd806SEd Tanous 
1611f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
16121ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1613f5ffd806SEd Tanous                                          oemOpenBMCObject))
1614f5ffd806SEd Tanous     {
1615f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1616f5ffd806SEd Tanous             oemOpenBMCObject &&
1617f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1618f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1619f5ffd806SEd Tanous         {
1620f5ffd806SEd Tanous             if (authMethodsObject)
1621f5ffd806SEd Tanous             {
16221ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1623f5ffd806SEd Tanous             }
1624f5ffd806SEd Tanous         }
1625f5ffd806SEd Tanous     }
1626f5ffd806SEd Tanous 
1627f5ffd806SEd Tanous     if (activeDirectoryObject)
1628f5ffd806SEd Tanous     {
16291ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1630f5ffd806SEd Tanous     }
1631f5ffd806SEd Tanous 
1632f5ffd806SEd Tanous     if (unlockTimeout)
1633f5ffd806SEd Tanous     {
16349ae226faSGeorge Liu         sdbusplus::asio::setProperty(
16359ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
16369ae226faSGeorge Liu             "/xyz/openbmc_project/user",
16379ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
16389ae226faSGeorge Liu             *unlockTimeout, [asyncResp](const boost::system::error_code& ec) {
1639f5ffd806SEd Tanous                 if (ec)
1640f5ffd806SEd Tanous                 {
1641f5ffd806SEd Tanous                     messages::internalError(asyncResp->res);
1642f5ffd806SEd Tanous                     return;
1643f5ffd806SEd Tanous                 }
1644f5ffd806SEd Tanous                 messages::success(asyncResp->res);
16459ae226faSGeorge Liu             });
1646f5ffd806SEd Tanous     }
1647f5ffd806SEd Tanous     if (lockoutThreshold)
1648f5ffd806SEd Tanous     {
16499ae226faSGeorge Liu         sdbusplus::asio::setProperty(
16509ae226faSGeorge Liu             *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
16519ae226faSGeorge Liu             "/xyz/openbmc_project/user",
16529ae226faSGeorge Liu             "xyz.openbmc_project.User.AccountPolicy",
16539ae226faSGeorge Liu             "MaxLoginAttemptBeforeLockout", *lockoutThreshold,
16545e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1655f5ffd806SEd Tanous             if (ec)
1656f5ffd806SEd Tanous             {
1657f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1658f5ffd806SEd Tanous                 return;
1659f5ffd806SEd Tanous             }
1660f5ffd806SEd Tanous             messages::success(asyncResp->res);
16619ae226faSGeorge Liu             });
1662f5ffd806SEd Tanous     }
16631ef4c342SEd Tanous }
1664f5ffd806SEd Tanous 
16654c7d4d33SEd Tanous inline void handleAccountCollectionHead(
16661ef4c342SEd Tanous     App& app, const crow::Request& req,
16671ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16681ef4c342SEd Tanous {
16693ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
167045ca1b86SEd Tanous     {
167145ca1b86SEd Tanous         return;
167245ca1b86SEd Tanous     }
16734c7d4d33SEd Tanous     asyncResp->res.addHeader(
16744c7d4d33SEd Tanous         boost::beast::http::field::link,
16754c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
16764c7d4d33SEd Tanous }
16774c7d4d33SEd Tanous 
16784c7d4d33SEd Tanous inline void handleAccountCollectionGet(
16794c7d4d33SEd Tanous     App& app, const crow::Request& req,
16804c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16814c7d4d33SEd Tanous {
1682afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1683afd369c6SJiaqing Zhao     {
1684afd369c6SJiaqing Zhao         return;
1685afd369c6SJiaqing Zhao     }
16863e72c202SNinad Palsule 
16873e72c202SNinad Palsule     if (req.session == nullptr)
16883e72c202SNinad Palsule     {
16893e72c202SNinad Palsule         messages::internalError(asyncResp->res);
16903e72c202SNinad Palsule         return;
16913e72c202SNinad Palsule     }
16923e72c202SNinad Palsule 
1693afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1694afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1695afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
16961476687dSEd Tanous 
16971476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
16981476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
16991ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
17001476687dSEd Tanous                                               "ManagerAccountCollection";
17011476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
17021476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
17030f74e643SEd Tanous 
17046c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
17053e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
17066c51eab1SEd Tanous 
1707f5e29f33SJunLin Chen     std::string thisUser;
1708f5e29f33SJunLin Chen     if (req.session)
1709f5e29f33SJunLin Chen     {
1710f5e29f33SJunLin Chen         thisUser = req.session->username;
1711f5e29f33SJunLin Chen     }
17125eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
17135eb468daSGeorge Liu     dbus::utility::getManagedObjects(
17145eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
1715cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
17165e7e2dc5SEd Tanous             const boost::system::error_code& ec,
1717711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1718b9b2e0b2SEd Tanous         if (ec)
1719b9b2e0b2SEd Tanous         {
1720f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1721b9b2e0b2SEd Tanous             return;
1722b9b2e0b2SEd Tanous         }
1723b9b2e0b2SEd Tanous 
1724cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1725002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1726cef1ddfbSEd Tanous 
1727cef1ddfbSEd Tanous         bool userCanSeeSelf =
1728002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1729cef1ddfbSEd Tanous 
1730002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1731b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1732b9b2e0b2SEd Tanous 
17339eb808c1SEd Tanous         for (const auto& userpath : users)
1734b9b2e0b2SEd Tanous         {
17352dfd18efSEd Tanous             std::string user = userpath.first.filename();
17362dfd18efSEd Tanous             if (user.empty())
1737b9b2e0b2SEd Tanous             {
17382dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
173962598e31SEd Tanous                 BMCWEB_LOG_ERROR("Invalid firmware ID");
17402dfd18efSEd Tanous 
17412dfd18efSEd Tanous                 return;
1742b9b2e0b2SEd Tanous             }
1743f365910cSGunnar Mills 
1744f365910cSGunnar Mills             // As clarified by Redfish here:
1745f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
17466c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
17476c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
17486c51eab1SEd Tanous             // accounts.
17491ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1750f365910cSGunnar Mills             {
17511476687dSEd Tanous                 nlohmann::json::object_t member;
175289492a15SPatrick Williams                 member["@odata.id"] = "/redfish/v1/AccountService/Accounts/" +
175389492a15SPatrick Williams                                       user;
1754b2ba3072SPatrick Williams                 memberArray.emplace_back(std::move(member));
1755b9b2e0b2SEd Tanous             }
1756f365910cSGunnar Mills         }
17571ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
17585eb468daSGeorge Liu         });
17591ef4c342SEd Tanous }
17606c51eab1SEd Tanous 
176197e90da3SNinad Palsule inline void processAfterCreateUser(
176297e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
176397e90da3SNinad Palsule     const std::string& username, const std::string& password,
176497e90da3SNinad Palsule     const boost::system::error_code& ec, sdbusplus::message_t& m)
176597e90da3SNinad Palsule {
176697e90da3SNinad Palsule     if (ec)
176797e90da3SNinad Palsule     {
176897e90da3SNinad Palsule         userErrorMessageHandler(m.get_error(), asyncResp, username, "");
176997e90da3SNinad Palsule         return;
177097e90da3SNinad Palsule     }
177197e90da3SNinad Palsule 
177297e90da3SNinad Palsule     if (pamUpdatePassword(username, password) != PAM_SUCCESS)
177397e90da3SNinad Palsule     {
177497e90da3SNinad Palsule         // At this point we have a user that's been
177597e90da3SNinad Palsule         // created, but the password set
177697e90da3SNinad Palsule         // failed.Something is wrong, so delete the user
177797e90da3SNinad Palsule         // that we've already created
177897e90da3SNinad Palsule         sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
177997e90da3SNinad Palsule         tempObjPath /= username;
178097e90da3SNinad Palsule         const std::string userPath(tempObjPath);
178197e90da3SNinad Palsule 
178297e90da3SNinad Palsule         crow::connections::systemBus->async_method_call(
178397e90da3SNinad Palsule             [asyncResp, password](const boost::system::error_code& ec3) {
178497e90da3SNinad Palsule             if (ec3)
178597e90da3SNinad Palsule             {
178697e90da3SNinad Palsule                 messages::internalError(asyncResp->res);
178797e90da3SNinad Palsule                 return;
178897e90da3SNinad Palsule             }
178997e90da3SNinad Palsule 
179097e90da3SNinad Palsule             // If password is invalid
179197e90da3SNinad Palsule             messages::propertyValueFormatError(asyncResp->res, password,
179297e90da3SNinad Palsule                                                "Password");
179397e90da3SNinad Palsule             },
179497e90da3SNinad Palsule             "xyz.openbmc_project.User.Manager", userPath,
179597e90da3SNinad Palsule             "xyz.openbmc_project.Object.Delete", "Delete");
179697e90da3SNinad Palsule 
179762598e31SEd Tanous         BMCWEB_LOG_ERROR("pamUpdatePassword Failed");
179897e90da3SNinad Palsule         return;
179997e90da3SNinad Palsule     }
180097e90da3SNinad Palsule 
180197e90da3SNinad Palsule     messages::created(asyncResp->res);
180297e90da3SNinad Palsule     asyncResp->res.addHeader("Location",
180397e90da3SNinad Palsule                              "/redfish/v1/AccountService/Accounts/" + username);
180497e90da3SNinad Palsule }
180597e90da3SNinad Palsule 
180697e90da3SNinad Palsule inline void processAfterGetAllGroups(
180797e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
180897e90da3SNinad Palsule     const std::string& username, const std::string& password,
1809*e01d0c36SEd Tanous     const std::string& roleId, bool enabled,
18109ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes,
181197e90da3SNinad Palsule     const std::vector<std::string>& allGroupsList)
181297e90da3SNinad Palsule {
18133e72c202SNinad Palsule     std::vector<std::string> userGroups;
18149ba73934SNinad Palsule     std::vector<std::string> accountTypeUserGroups;
18159ba73934SNinad Palsule 
18169ba73934SNinad Palsule     // If user specified account types then convert them to unix user groups
18179ba73934SNinad Palsule     if (accountTypes)
18189ba73934SNinad Palsule     {
18199ba73934SNinad Palsule         if (!getUserGroupFromAccountType(asyncResp->res, *accountTypes,
18209ba73934SNinad Palsule                                          accountTypeUserGroups))
18219ba73934SNinad Palsule         {
18229ba73934SNinad Palsule             // Problem in mapping Account Types to User Groups, Error already
18239ba73934SNinad Palsule             // logged.
18249ba73934SNinad Palsule             return;
18259ba73934SNinad Palsule         }
18269ba73934SNinad Palsule     }
18279ba73934SNinad Palsule 
18283e72c202SNinad Palsule     for (const auto& grp : allGroupsList)
18293e72c202SNinad Palsule     {
18309ba73934SNinad Palsule         // If user specified the account type then only accept groups which are
18319ba73934SNinad Palsule         // in the account types group list.
18329ba73934SNinad Palsule         if (!accountTypeUserGroups.empty())
18339ba73934SNinad Palsule         {
18349ba73934SNinad Palsule             bool found = false;
18359ba73934SNinad Palsule             for (const auto& grp1 : accountTypeUserGroups)
18369ba73934SNinad Palsule             {
18379ba73934SNinad Palsule                 if (grp == grp1)
18389ba73934SNinad Palsule                 {
18399ba73934SNinad Palsule                     found = true;
18409ba73934SNinad Palsule                     break;
18419ba73934SNinad Palsule                 }
18429ba73934SNinad Palsule             }
18439ba73934SNinad Palsule             if (!found)
18449ba73934SNinad Palsule             {
18459ba73934SNinad Palsule                 continue;
18469ba73934SNinad Palsule             }
18479ba73934SNinad Palsule         }
18489ba73934SNinad Palsule 
18493e72c202SNinad Palsule         // Console access is provided to the user who is a member of
18503e72c202SNinad Palsule         // hostconsole group and has a administrator role. So, set
18513e72c202SNinad Palsule         // hostconsole group only for the administrator.
18529ba73934SNinad Palsule         if ((grp == "hostconsole") && (roleId != "priv-admin"))
18533e72c202SNinad Palsule         {
18549ba73934SNinad Palsule             if (!accountTypeUserGroups.empty())
18559ba73934SNinad Palsule             {
185662598e31SEd Tanous                 BMCWEB_LOG_ERROR(
185762598e31SEd Tanous                     "Only administrator can get HostConsole access");
18589ba73934SNinad Palsule                 asyncResp->res.result(boost::beast::http::status::bad_request);
18599ba73934SNinad Palsule                 return;
18609ba73934SNinad Palsule             }
18619ba73934SNinad Palsule             continue;
18629ba73934SNinad Palsule         }
18633e72c202SNinad Palsule         userGroups.emplace_back(grp);
18643e72c202SNinad Palsule     }
18659ba73934SNinad Palsule 
18669ba73934SNinad Palsule     // Make sure user specified groups are valid. This is internal error because
18679ba73934SNinad Palsule     // it some inconsistencies between user manager and bmcweb.
18689ba73934SNinad Palsule     if (!accountTypeUserGroups.empty() &&
18699ba73934SNinad Palsule         accountTypeUserGroups.size() != userGroups.size())
18709ba73934SNinad Palsule     {
18719ba73934SNinad Palsule         messages::internalError(asyncResp->res);
18729ba73934SNinad Palsule         return;
18733e72c202SNinad Palsule     }
187497e90da3SNinad Palsule     crow::connections::systemBus->async_method_call(
187597e90da3SNinad Palsule         [asyncResp, username, password](const boost::system::error_code& ec2,
187697e90da3SNinad Palsule                                         sdbusplus::message_t& m) {
187797e90da3SNinad Palsule         processAfterCreateUser(asyncResp, username, password, ec2, m);
187897e90da3SNinad Palsule         },
187997e90da3SNinad Palsule         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
18803e72c202SNinad Palsule         "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
1881*e01d0c36SEd Tanous         roleId, enabled);
188297e90da3SNinad Palsule }
188397e90da3SNinad Palsule 
18841ef4c342SEd Tanous inline void handleAccountCollectionPost(
18851ef4c342SEd Tanous     App& app, const crow::Request& req,
18861ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
18871ef4c342SEd Tanous {
18883ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
188945ca1b86SEd Tanous     {
189045ca1b86SEd Tanous         return;
189145ca1b86SEd Tanous     }
18929712f8acSEd Tanous     std::string username;
18939712f8acSEd Tanous     std::string password;
1894*e01d0c36SEd Tanous     std::optional<std::string> roleIdJson;
1895*e01d0c36SEd Tanous     std::optional<bool> enabledJson;
18969ba73934SNinad Palsule     std::optional<std::vector<std::string>> accountTypes;
1897*e01d0c36SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
1898*e01d0c36SEd Tanous                                   "Password", password, "RoleId", roleIdJson,
1899*e01d0c36SEd Tanous                                   "Enabled", enabledJson, "AccountTypes",
1900*e01d0c36SEd Tanous                                   accountTypes))
190104ae99ecSEd Tanous     {
190204ae99ecSEd Tanous         return;
190304ae99ecSEd Tanous     }
190404ae99ecSEd Tanous 
1905*e01d0c36SEd Tanous     std::string roleId = roleIdJson.value_or("User");
1906*e01d0c36SEd Tanous     std::string priv = getPrivilegeFromRoleId(roleId);
190784e12cb7SAppaRao Puli     if (priv.empty())
190804ae99ecSEd Tanous     {
1909*e01d0c36SEd Tanous         messages::propertyValueNotInList(asyncResp->res, roleId, "RoleId");
191004ae99ecSEd Tanous         return;
191104ae99ecSEd Tanous     }
19129712f8acSEd Tanous     roleId = priv;
191304ae99ecSEd Tanous 
1914*e01d0c36SEd Tanous     bool enabled = enabledJson.value_or(true);
1915*e01d0c36SEd Tanous 
1916599c71d8SAyushi Smriti     // Reading AllGroups property
19171e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
19181ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
19191ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
19201ef4c342SEd Tanous         "AllGroups",
19219ba73934SNinad Palsule         [asyncResp, username, password{std::move(password)}, roleId, enabled,
19229ba73934SNinad Palsule          accountTypes](const boost::system::error_code& ec,
19231e1e598dSJonathan Doman                        const std::vector<std::string>& allGroupsList) {
1924599c71d8SAyushi Smriti         if (ec)
1925599c71d8SAyushi Smriti         {
192662598e31SEd Tanous             BMCWEB_LOG_DEBUG("ERROR with async_method_call");
1927599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1928599c71d8SAyushi Smriti             return;
1929599c71d8SAyushi Smriti         }
1930599c71d8SAyushi Smriti 
19311e1e598dSJonathan Doman         if (allGroupsList.empty())
1932599c71d8SAyushi Smriti         {
1933599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1934599c71d8SAyushi Smriti             return;
1935599c71d8SAyushi Smriti         }
1936599c71d8SAyushi Smriti 
193797e90da3SNinad Palsule         processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
19389ba73934SNinad Palsule                                  accountTypes, allGroupsList);
19391e1e598dSJonathan Doman         });
19401ef4c342SEd Tanous }
1941b9b2e0b2SEd Tanous 
19421ef4c342SEd Tanous inline void
19434c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
19446c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19454c7d4d33SEd Tanous                       const std::string& /*accountName*/)
19461ef4c342SEd Tanous {
19473ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
194845ca1b86SEd Tanous     {
194945ca1b86SEd Tanous         return;
195045ca1b86SEd Tanous     }
19514c7d4d33SEd Tanous     asyncResp->res.addHeader(
19524c7d4d33SEd Tanous         boost::beast::http::field::link,
19534c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
19544c7d4d33SEd Tanous }
1955afd369c6SJiaqing Zhao 
19564c7d4d33SEd Tanous inline void
19574c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
19584c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19594c7d4d33SEd Tanous                      const std::string& accountName)
19604c7d4d33SEd Tanous {
1961afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1962afd369c6SJiaqing Zhao     {
1963afd369c6SJiaqing Zhao         return;
1964afd369c6SJiaqing Zhao     }
1965afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1966afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1967afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1968afd369c6SJiaqing Zhao 
19691ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1970031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1971d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1972031514fbSJunLin Chen     return;
19731ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1974afd369c6SJiaqing Zhao 
1975031514fbSJunLin Chen     if (req.session == nullptr)
1976031514fbSJunLin Chen     {
1977031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1978031514fbSJunLin Chen         return;
1979031514fbSJunLin Chen     }
19806c51eab1SEd Tanous     if (req.session->username != accountName)
1981b9b2e0b2SEd Tanous     {
19826c51eab1SEd Tanous         // At this point we've determined that the user is trying to
19831ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
19841ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
19851ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
19866c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
19873e72c202SNinad Palsule             redfish::getUserPrivileges(*req.session);
19881ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
19891ef4c342SEd Tanous                                                          "ConfigureManager"};
19906c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
19916c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1992900f9497SJoseph Reynolds         {
199362598e31SEd Tanous             BMCWEB_LOG_DEBUG("GET Account denied access");
1994900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1995900f9497SJoseph Reynolds             return;
1996900f9497SJoseph Reynolds         }
1997900f9497SJoseph Reynolds     }
1998900f9497SJoseph Reynolds 
19995eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/user");
20005eb468daSGeorge Liu     dbus::utility::getManagedObjects(
20015eb468daSGeorge Liu         "xyz.openbmc_project.User.Manager", path,
20021ef4c342SEd Tanous         [asyncResp,
20035e7e2dc5SEd Tanous          accountName](const boost::system::error_code& ec,
2004711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
2005b9b2e0b2SEd Tanous         if (ec)
2006b9b2e0b2SEd Tanous         {
2007f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2008b9b2e0b2SEd Tanous             return;
2009b9b2e0b2SEd Tanous         }
2010711ac7a9SEd Tanous         const auto userIt = std::find_if(
2011b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
2012b477fd44SP Dheeraj Srujan Kumar             [accountName](
2013b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
2014002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
201555f79e6fSEd Tanous             return accountName == user.first.filename();
2016b477fd44SP Dheeraj Srujan Kumar             });
2017b9b2e0b2SEd Tanous 
201884e12cb7SAppaRao Puli         if (userIt == users.end())
2019b9b2e0b2SEd Tanous         {
2020002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
2021002d39b4SEd Tanous                                        accountName);
202284e12cb7SAppaRao Puli             return;
202384e12cb7SAppaRao Puli         }
20244e68c45bSAyushi Smriti 
20251476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
202658345856SAbhishek Patel             "#ManagerAccount.v1_7_0.ManagerAccount";
20271476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
20281476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
20291476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
203058345856SAbhishek Patel         asyncResp->res.jsonValue["StrictAccountTypes"] = true;
20314e68c45bSAyushi Smriti 
203284e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
203365b0dc32SEd Tanous         {
2034002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
203565b0dc32SEd Tanous             {
203665b0dc32SEd Tanous                 for (const auto& property : interface.second)
203765b0dc32SEd Tanous                 {
203865b0dc32SEd Tanous                     if (property.first == "UserEnabled")
203965b0dc32SEd Tanous                     {
204065b0dc32SEd Tanous                         const bool* userEnabled =
2041abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
204265b0dc32SEd Tanous                         if (userEnabled == nullptr)
204365b0dc32SEd Tanous                         {
204462598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserEnabled wasn't a bool");
204584e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
204684e12cb7SAppaRao Puli                             return;
204765b0dc32SEd Tanous                         }
2048002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
204965b0dc32SEd Tanous                     }
2050002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
205165b0dc32SEd Tanous                     {
205265b0dc32SEd Tanous                         const bool* userLocked =
2053abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
205465b0dc32SEd Tanous                         if (userLocked == nullptr)
205565b0dc32SEd Tanous                         {
205662598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserLockedForF"
205784e12cb7SAppaRao Puli                                              "ailedAttempt "
205862598e31SEd Tanous                                              "wasn't a bool");
205984e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
206084e12cb7SAppaRao Puli                             return;
206165b0dc32SEd Tanous                         }
2062002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
2063002d39b4SEd Tanous                         asyncResp->res
2064002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
20653bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
206665b0dc32SEd Tanous                     }
206784e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
206884e12cb7SAppaRao Puli                     {
206954fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
2070002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
207154fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
207284e12cb7SAppaRao Puli                         {
207362598e31SEd Tanous                             BMCWEB_LOG_ERROR("UserPrivilege wasn't a "
207462598e31SEd Tanous                                              "string");
207584e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
207684e12cb7SAppaRao Puli                             return;
207784e12cb7SAppaRao Puli                         }
20781ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
207954fc587aSNagaraju Goruganti                         if (role.empty())
208084e12cb7SAppaRao Puli                         {
208162598e31SEd Tanous                             BMCWEB_LOG_ERROR("Invalid user role");
208284e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
208384e12cb7SAppaRao Puli                             return;
208484e12cb7SAppaRao Puli                         }
208554fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
208684e12cb7SAppaRao Puli 
20871476687dSEd Tanous                         nlohmann::json& roleEntry =
2088002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
20891476687dSEd Tanous                         roleEntry["@odata.id"] =
2090002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
209184e12cb7SAppaRao Puli                     }
2092002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
20933bf4e632SJoseph Reynolds                     {
20943bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
20953bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
20963bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
20973bf4e632SJoseph Reynolds                         {
209862598e31SEd Tanous                             BMCWEB_LOG_ERROR(
209962598e31SEd Tanous                                 "UserPasswordExpired wasn't a bool");
21003bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
21013bf4e632SJoseph Reynolds                             return;
21023bf4e632SJoseph Reynolds                         }
2103002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
21043bf4e632SJoseph Reynolds                             *userPasswordExpired;
21053bf4e632SJoseph Reynolds                     }
2106c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
2107c7229815SAbhishek Patel                     {
2108c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
2109c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
2110c7229815SAbhishek Patel                                 &property.second);
2111c7229815SAbhishek Patel                         if (userGroups == nullptr)
2112c7229815SAbhishek Patel                         {
211362598e31SEd Tanous                             BMCWEB_LOG_ERROR(
211462598e31SEd Tanous                                 "userGroups wasn't a string vector");
2115c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2116c7229815SAbhishek Patel                             return;
2117c7229815SAbhishek Patel                         }
2118c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
2119c7229815SAbhishek Patel                         {
212062598e31SEd Tanous                             BMCWEB_LOG_ERROR("userGroups mapping failed");
2121c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
2122c7229815SAbhishek Patel                             return;
2123c7229815SAbhishek Patel                         }
2124c7229815SAbhishek Patel                     }
212565b0dc32SEd Tanous                 }
212665b0dc32SEd Tanous             }
212765b0dc32SEd Tanous         }
212865b0dc32SEd Tanous 
2129b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
213084e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
2131b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
2132b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
21335eb468daSGeorge Liu         });
21341ef4c342SEd Tanous }
2135a840879dSEd Tanous 
21361ef4c342SEd Tanous inline void
213720fc307fSGunnar Mills     handleAccountDelete(App& app, const crow::Request& req,
21386c51eab1SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21391ef4c342SEd Tanous                         const std::string& username)
21401ef4c342SEd Tanous {
21413ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
214245ca1b86SEd Tanous     {
214345ca1b86SEd Tanous         return;
214445ca1b86SEd Tanous     }
21451ef4c342SEd Tanous 
21461ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
2147031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
2148d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
2149031514fbSJunLin Chen     return;
2150031514fbSJunLin Chen 
21511ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
21521ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
21531ef4c342SEd Tanous     tempObjPath /= username;
21541ef4c342SEd Tanous     const std::string userPath(tempObjPath);
21551ef4c342SEd Tanous 
21561ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
21575e7e2dc5SEd Tanous         [asyncResp, username](const boost::system::error_code& ec) {
21581ef4c342SEd Tanous         if (ec)
21591ef4c342SEd Tanous         {
2160d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
21611ef4c342SEd Tanous                                        username);
21621ef4c342SEd Tanous             return;
21631ef4c342SEd Tanous         }
21641ef4c342SEd Tanous 
21651ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
21661ef4c342SEd Tanous         },
21671ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
21681ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
21691ef4c342SEd Tanous }
21701ef4c342SEd Tanous 
21711ef4c342SEd Tanous inline void
21721ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
21731ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21741ef4c342SEd Tanous                        const std::string& username)
21751ef4c342SEd Tanous {
21761ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
21771ef4c342SEd Tanous     {
21781ef4c342SEd Tanous         return;
21791ef4c342SEd Tanous     }
21801ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
21811ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
2182d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
21831ef4c342SEd Tanous     return;
21841ef4c342SEd Tanous 
21851ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
2186a24526dcSEd Tanous     std::optional<std::string> newUserName;
2187a24526dcSEd Tanous     std::optional<std::string> password;
2188a24526dcSEd Tanous     std::optional<bool> enabled;
2189a24526dcSEd Tanous     std::optional<std::string> roleId;
219024c8542dSRatan Gupta     std::optional<bool> locked;
219158345856SAbhishek Patel     std::optional<std::vector<std::string>> accountTypes;
219258345856SAbhishek Patel 
219358345856SAbhishek Patel     bool userSelf = (username == req.session->username);
2194e9cc5172SEd Tanous 
2195031514fbSJunLin Chen     if (req.session == nullptr)
2196031514fbSJunLin Chen     {
2197031514fbSJunLin Chen         messages::internalError(asyncResp->res);
2198031514fbSJunLin Chen         return;
2199031514fbSJunLin Chen     }
2200031514fbSJunLin Chen 
2201e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
22023e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
2203e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
2204e9cc5172SEd Tanous     bool userHasConfigureUsers =
2205e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
2206e9cc5172SEd Tanous     if (userHasConfigureUsers)
2207e9cc5172SEd Tanous     {
2208e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
220958345856SAbhishek Patel         if (!json_util::readJsonPatch(
221058345856SAbhishek Patel                 req, asyncResp->res, "UserName", newUserName, "Password",
221158345856SAbhishek Patel                 password, "RoleId", roleId, "Enabled", enabled, "Locked",
221258345856SAbhishek Patel                 locked, "AccountTypes", accountTypes))
2213a840879dSEd Tanous         {
2214a840879dSEd Tanous             return;
2215a840879dSEd Tanous         }
2216e9cc5172SEd Tanous     }
2217e9cc5172SEd Tanous     else
2218900f9497SJoseph Reynolds     {
2219e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
222058345856SAbhishek Patel         if (!userSelf)
2221900f9497SJoseph Reynolds         {
2222900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
2223900f9497SJoseph Reynolds             return;
2224900f9497SJoseph Reynolds         }
2225031514fbSJunLin Chen 
2226e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
22271ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
22281ef4c342SEd Tanous                                       password))
2229e9cc5172SEd Tanous         {
2230e9cc5172SEd Tanous             return;
2231e9cc5172SEd Tanous         }
2232900f9497SJoseph Reynolds     }
2233900f9497SJoseph Reynolds 
223466b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
22356c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
22366c51eab1SEd Tanous     // updating user properties other then username. If username
22376c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
22386c51eab1SEd Tanous     // user rename request.
223966b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
2240a840879dSEd Tanous     {
22411ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
224258345856SAbhishek Patel                              locked, accountTypes, userSelf);
224384e12cb7SAppaRao Puli         return;
224484e12cb7SAppaRao Puli     }
224584e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
22466c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
22471ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
224858345856SAbhishek Patel          locked, userSelf, accountTypes(std::move(accountTypes))](
2249e81de512SEd Tanous             const boost::system::error_code& ec, sdbusplus::message_t& m) {
225084e12cb7SAppaRao Puli         if (ec)
225184e12cb7SAppaRao Puli         {
2252002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2253002d39b4SEd Tanous                                     username);
2254a840879dSEd Tanous             return;
2255a840879dSEd Tanous         }
2256a840879dSEd Tanous 
2257002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
225858345856SAbhishek Patel                              locked, accountTypes, userSelf);
225984e12cb7SAppaRao Puli         },
22601ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
226184e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
226284e12cb7SAppaRao Puli         *newUserName);
22631ef4c342SEd Tanous }
22641ef4c342SEd Tanous 
22651ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
22661ef4c342SEd Tanous {
22671ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22684c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
22694c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
22704c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
22714c7d4d33SEd Tanous 
22724c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22731ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
22741ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
22751ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
22761ef4c342SEd Tanous 
22771ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
22781ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
22791ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
22801ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
22811ef4c342SEd Tanous 
22821ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
22834c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
22844c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
22854c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
22864c7d4d33SEd Tanous 
22874c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
22881ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
22891ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
22901ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
22911ef4c342SEd Tanous 
22921ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
22931ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
22941ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
22951ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
22961ef4c342SEd Tanous 
22971ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
22984c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
22994c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
23004c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
23014c7d4d33SEd Tanous 
23024c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23031ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
23041ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
23051ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
23061ef4c342SEd Tanous 
23071ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
23081ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
23091ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
23101ef4c342SEd Tanous         // yet
23111ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
23121ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
23131ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
231484e12cb7SAppaRao Puli 
23156c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2316ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
23176c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
231820fc307fSGunnar Mills             std::bind_front(handleAccountDelete, std::ref(app)));
231906e086d9SEd Tanous }
232088d16c9aSLewanczyk, Dawid 
232188d16c9aSLewanczyk, Dawid } // namespace redfish
2322