xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision c80fee55c3663e5ac620a4d11378799c91867b76)
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 #include "node.hpp"
1888d16c9aSLewanczyk, Dawid 
1924c8542dSRatan Gupta #include <dbus_utility.hpp>
2065b0dc32SEd Tanous #include <error_messages.hpp>
21b9b2e0b2SEd Tanous #include <openbmc_dbus_rest.hpp>
22a840879dSEd Tanous #include <utils/json_utils.hpp>
23abf2add6SEd Tanous #include <variant>
24b9b2e0b2SEd Tanous 
251abe55efSEd Tanous namespace redfish
261abe55efSEd Tanous {
2788d16c9aSLewanczyk, Dawid 
286973a582SRatan Gupta constexpr const char* ldapConfigObject =
296973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
30ab828d7cSRatan Gupta constexpr const char* ADConfigObject =
31ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
32ab828d7cSRatan Gupta 
336973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
346973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
356973a582SRatan Gupta constexpr const char* ldapConfigInterface =
366973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
376973a582SRatan Gupta constexpr const char* ldapCreateInterface =
386973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
396973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
4006785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
4106785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
426973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
436973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
446973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
456973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
466973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
476973a582SRatan Gupta 
4854fc587aSNagaraju Goruganti struct LDAPRoleMapData
4954fc587aSNagaraju Goruganti {
5054fc587aSNagaraju Goruganti     std::string groupName;
5154fc587aSNagaraju Goruganti     std::string privilege;
5254fc587aSNagaraju Goruganti };
5354fc587aSNagaraju Goruganti 
546973a582SRatan Gupta struct LDAPConfigData
556973a582SRatan Gupta {
566973a582SRatan Gupta     std::string uri{};
576973a582SRatan Gupta     std::string bindDN{};
586973a582SRatan Gupta     std::string baseDN{};
596973a582SRatan Gupta     std::string searchScope{};
606973a582SRatan Gupta     std::string serverType{};
616973a582SRatan Gupta     bool serviceEnabled = false;
626973a582SRatan Gupta     std::string userNameAttribute{};
636973a582SRatan Gupta     std::string groupAttribute{};
6454fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
656973a582SRatan Gupta };
666973a582SRatan Gupta 
67b9b2e0b2SEd Tanous using ManagedObjectType = std::vector<std::pair<
68b9b2e0b2SEd Tanous     sdbusplus::message::object_path,
69b9b2e0b2SEd Tanous     boost::container::flat_map<
70abf2add6SEd Tanous         std::string, boost::container::flat_map<
71abf2add6SEd Tanous                          std::string, std::variant<bool, std::string>>>>>;
726973a582SRatan Gupta using GetObjectType =
736973a582SRatan Gupta     std::vector<std::pair<std::string, std::vector<std::string>>>;
7484e12cb7SAppaRao Puli 
7554fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7684e12cb7SAppaRao Puli {
7784e12cb7SAppaRao Puli     if (role == "priv-admin")
7884e12cb7SAppaRao Puli     {
7984e12cb7SAppaRao Puli         return "Administrator";
8084e12cb7SAppaRao Puli     }
8184e12cb7SAppaRao Puli     else if (role == "priv-callback")
8284e12cb7SAppaRao Puli     {
8384e12cb7SAppaRao Puli         return "Callback";
8484e12cb7SAppaRao Puli     }
8584e12cb7SAppaRao Puli     else if (role == "priv-user")
8684e12cb7SAppaRao Puli     {
87*c80fee55SAppaRao Puli         return "ReadOnly";
8884e12cb7SAppaRao Puli     }
8984e12cb7SAppaRao Puli     else if (role == "priv-operator")
9084e12cb7SAppaRao Puli     {
9184e12cb7SAppaRao Puli         return "Operator";
9284e12cb7SAppaRao Puli     }
9384e12cb7SAppaRao Puli     return "";
9484e12cb7SAppaRao Puli }
9554fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9684e12cb7SAppaRao Puli {
9784e12cb7SAppaRao Puli     if (role == "Administrator")
9884e12cb7SAppaRao Puli     {
9984e12cb7SAppaRao Puli         return "priv-admin";
10084e12cb7SAppaRao Puli     }
10184e12cb7SAppaRao Puli     else if (role == "Callback")
10284e12cb7SAppaRao Puli     {
10384e12cb7SAppaRao Puli         return "priv-callback";
10484e12cb7SAppaRao Puli     }
105*c80fee55SAppaRao Puli     else if (role == "ReadOnly")
10684e12cb7SAppaRao Puli     {
10784e12cb7SAppaRao Puli         return "priv-user";
10884e12cb7SAppaRao Puli     }
10984e12cb7SAppaRao Puli     else if (role == "Operator")
11084e12cb7SAppaRao Puli     {
11184e12cb7SAppaRao Puli         return "priv-operator";
11284e12cb7SAppaRao Puli     }
11384e12cb7SAppaRao Puli     return "";
11484e12cb7SAppaRao Puli }
115b9b2e0b2SEd Tanous 
1166973a582SRatan Gupta void parseLDAPConfigData(nlohmann::json& json_response,
117ab828d7cSRatan Gupta                          const LDAPConfigData& confData,
118ab828d7cSRatan Gupta                          const std::string& ldapType)
1196973a582SRatan Gupta {
120ab828d7cSRatan Gupta     std::string service =
121ab828d7cSRatan Gupta         (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService";
12237cce918SMarri Devender Rao     nlohmann::json ldap = {
1236973a582SRatan Gupta         {"AccountProviderType", service},
1246973a582SRatan Gupta         {"ServiceEnabled", confData.serviceEnabled},
1256973a582SRatan Gupta         {"ServiceAddresses", nlohmann::json::array({confData.uri})},
1266973a582SRatan Gupta         {"Authentication",
1276973a582SRatan Gupta          {{"AuthenticationType", "UsernameAndPassword"},
1286973a582SRatan Gupta           {"Username", confData.bindDN},
1296973a582SRatan Gupta           {"Password", nullptr}}},
1306973a582SRatan Gupta         {"LDAPService",
1316973a582SRatan Gupta          {{"SearchSettings",
1326973a582SRatan Gupta            {{"BaseDistinguishedNames",
1336973a582SRatan Gupta              nlohmann::json::array({confData.baseDN})},
1346973a582SRatan Gupta             {"UsernameAttribute", confData.userNameAttribute},
1356973a582SRatan Gupta             {"GroupsAttribute", confData.groupAttribute}}}}},
1366973a582SRatan Gupta     };
13754fc587aSNagaraju Goruganti 
13837cce918SMarri Devender Rao     json_response[ldapType].update(std::move(ldap));
13954fc587aSNagaraju Goruganti 
14054fc587aSNagaraju Goruganti     nlohmann::json& roleMapArray = json_response[ldapType]["RemoteRoleMapping"];
14154fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
14254fc587aSNagaraju Goruganti     for (auto& obj : confData.groupRoleList)
14354fc587aSNagaraju Goruganti     {
14454fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
14554fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
14654fc587aSNagaraju Goruganti         roleMapArray.push_back(
14754fc587aSNagaraju Goruganti             {nlohmann::json::array({"RemoteGroup", obj.second.groupName}),
14854fc587aSNagaraju Goruganti              nlohmann::json::array(
14954fc587aSNagaraju Goruganti                  {"LocalRole", getRoleIdFromPrivilege(obj.second.privilege)})});
15054fc587aSNagaraju Goruganti     }
1516973a582SRatan Gupta }
1526973a582SRatan Gupta 
1536973a582SRatan Gupta /**
15406785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
15506785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
15606785244SRatan Gupta  *
15706785244SRatan Gupta  */
15806785244SRatan Gupta static void handleRoleMapPatch(
15906785244SRatan Gupta     const std::shared_ptr<AsyncResp>& asyncResp,
16006785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
16106785244SRatan Gupta     const std::string& serverType, std::vector<nlohmann::json>& input)
16206785244SRatan Gupta {
16306785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
16406785244SRatan Gupta     {
16506785244SRatan Gupta         nlohmann::json& thisJson = input[index];
16606785244SRatan Gupta 
16706785244SRatan Gupta         if (thisJson.is_null())
16806785244SRatan Gupta         {
16906785244SRatan Gupta             // delete the existing object
17006785244SRatan Gupta             if (index < roleMapObjData.size())
17106785244SRatan Gupta             {
17206785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
17306785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
17406785244SRatan Gupta                      index](const boost::system::error_code ec) {
17506785244SRatan Gupta                         if (ec)
17606785244SRatan Gupta                         {
17706785244SRatan Gupta                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
17806785244SRatan Gupta                             messages::internalError(asyncResp->res);
17906785244SRatan Gupta                             return;
18006785244SRatan Gupta                         }
18106785244SRatan Gupta                         asyncResp->res
18206785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"][index] =
18306785244SRatan Gupta                             nullptr;
18406785244SRatan Gupta                     },
18506785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
18606785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
18706785244SRatan Gupta             }
18806785244SRatan Gupta             else
18906785244SRatan Gupta             {
19006785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
19106785244SRatan Gupta                 messages::propertyValueTypeError(
19206785244SRatan Gupta                     asyncResp->res, thisJson.dump(),
19306785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
19406785244SRatan Gupta                 return;
19506785244SRatan Gupta             }
19606785244SRatan Gupta         }
19706785244SRatan Gupta         else if (thisJson.empty())
19806785244SRatan Gupta         {
19906785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
20006785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
20106785244SRatan Gupta         }
20206785244SRatan Gupta         else
20306785244SRatan Gupta         {
20406785244SRatan Gupta             // update/create the object
20506785244SRatan Gupta             std::optional<std::string> remoteGroup;
20606785244SRatan Gupta             std::optional<std::string> localRole;
20706785244SRatan Gupta 
20806785244SRatan Gupta             if (!json_util::readJson(thisJson, asyncResp->res, "RemoteGroup",
20906785244SRatan Gupta                                      remoteGroup, "LocalRole", localRole))
21006785244SRatan Gupta             {
21106785244SRatan Gupta                 continue;
21206785244SRatan Gupta             }
21306785244SRatan Gupta 
21406785244SRatan Gupta             // Update existing RoleMapping Object
21506785244SRatan Gupta             if (index < roleMapObjData.size())
21606785244SRatan Gupta             {
21706785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
21806785244SRatan Gupta                 // If "RemoteGroup" info is provided
21906785244SRatan Gupta                 if (remoteGroup)
22006785244SRatan Gupta                 {
22106785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
22206785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
22306785244SRatan Gupta                          remoteGroup](const boost::system::error_code ec) {
22406785244SRatan Gupta                             if (ec)
22506785244SRatan Gupta                             {
22606785244SRatan Gupta                                 BMCWEB_LOG_ERROR << "DBUS response error: "
22706785244SRatan Gupta                                                  << ec;
22806785244SRatan Gupta                                 messages::internalError(asyncResp->res);
22906785244SRatan Gupta                                 return;
23006785244SRatan Gupta                             }
23106785244SRatan Gupta                             asyncResp->res
23206785244SRatan Gupta                                 .jsonValue[serverType]["RemoteRoleMapping"]
23306785244SRatan Gupta                                           [index]["RemoteGroup"] = *remoteGroup;
23406785244SRatan Gupta                         },
23506785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
23606785244SRatan Gupta                         propertyInterface, "Set",
23706785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
23806785244SRatan Gupta                         "GroupName",
23906785244SRatan Gupta                         std::variant<std::string>(std::move(*remoteGroup)));
24006785244SRatan Gupta                 }
24106785244SRatan Gupta 
24206785244SRatan Gupta                 // If "LocalRole" info is provided
24306785244SRatan Gupta                 if (localRole)
24406785244SRatan Gupta                 {
24506785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
24606785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
24706785244SRatan Gupta                          localRole](const boost::system::error_code ec) {
24806785244SRatan Gupta                             if (ec)
24906785244SRatan Gupta                             {
25006785244SRatan Gupta                                 BMCWEB_LOG_ERROR << "DBUS response error: "
25106785244SRatan Gupta                                                  << ec;
25206785244SRatan Gupta                                 messages::internalError(asyncResp->res);
25306785244SRatan Gupta                                 return;
25406785244SRatan Gupta                             }
25506785244SRatan Gupta                             asyncResp->res
25606785244SRatan Gupta                                 .jsonValue[serverType]["RemoteRoleMapping"]
25706785244SRatan Gupta                                           [index]["LocalRole"] = *localRole;
25806785244SRatan Gupta                         },
25906785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
26006785244SRatan Gupta                         propertyInterface, "Set",
26106785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
26206785244SRatan Gupta                         "Privilege",
26306785244SRatan Gupta                         std::variant<std::string>(
26406785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
26506785244SRatan Gupta                 }
26606785244SRatan Gupta             }
26706785244SRatan Gupta             // Create a new RoleMapping Object.
26806785244SRatan Gupta             else
26906785244SRatan Gupta             {
27006785244SRatan Gupta                 BMCWEB_LOG_DEBUG
27106785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
27206785244SRatan Gupta                 std::string pathString =
27306785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index);
27406785244SRatan Gupta 
27506785244SRatan Gupta                 if (!localRole)
27606785244SRatan Gupta                 {
27706785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
27806785244SRatan Gupta                                               pathString + "/LocalRole");
27906785244SRatan Gupta                     continue;
28006785244SRatan Gupta                 }
28106785244SRatan Gupta                 if (!remoteGroup)
28206785244SRatan Gupta                 {
28306785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
28406785244SRatan Gupta                                               pathString + "/RemoteGroup");
28506785244SRatan Gupta                     continue;
28606785244SRatan Gupta                 }
28706785244SRatan Gupta 
28806785244SRatan Gupta                 std::string dbusObjectPath;
28906785244SRatan Gupta                 if (serverType == "ActiveDirectory")
29006785244SRatan Gupta                 {
29106785244SRatan Gupta                     dbusObjectPath = ADConfigObject;
29206785244SRatan Gupta                 }
29306785244SRatan Gupta                 else if (serverType == "LDAP")
29406785244SRatan Gupta                 {
29506785244SRatan Gupta                     dbusObjectPath = ldapConfigObject;
29606785244SRatan Gupta                 }
29706785244SRatan Gupta 
29806785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
29906785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
30006785244SRatan Gupta 
30106785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
302271584abSEd Tanous                     [asyncResp, serverType, localRole,
30306785244SRatan Gupta                      remoteGroup](const boost::system::error_code ec) {
30406785244SRatan Gupta                         if (ec)
30506785244SRatan Gupta                         {
30606785244SRatan Gupta                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
30706785244SRatan Gupta                             messages::internalError(asyncResp->res);
30806785244SRatan Gupta                             return;
30906785244SRatan Gupta                         }
31006785244SRatan Gupta                         nlohmann::json& remoteRoleJson =
31106785244SRatan Gupta                             asyncResp->res
31206785244SRatan Gupta                                 .jsonValue[serverType]["RemoteRoleMapping"];
31306785244SRatan Gupta                         remoteRoleJson.push_back(
31406785244SRatan Gupta                             {{"LocalRole", *localRole},
31506785244SRatan Gupta                              {"RemoteGroup", *remoteGroup}});
31606785244SRatan Gupta                     },
31706785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
31806785244SRatan Gupta                     "Create", std::move(*remoteGroup),
31906785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
32006785244SRatan Gupta             }
32106785244SRatan Gupta         }
32206785244SRatan Gupta     }
32306785244SRatan Gupta }
32406785244SRatan Gupta 
32506785244SRatan Gupta /**
3266973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
3276973a582SRatan Gupta  * into JSON
3286973a582SRatan Gupta  */
3296973a582SRatan Gupta template <typename CallbackFunc>
3306973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
3316973a582SRatan Gupta                               CallbackFunc&& callback)
3326973a582SRatan Gupta {
33354fc587aSNagaraju Goruganti 
33454fc587aSNagaraju Goruganti     const std::array<const char*, 2> interfaces = {ldapEnableInterface,
33554fc587aSNagaraju Goruganti                                                    ldapConfigInterface};
33654fc587aSNagaraju Goruganti 
33754fc587aSNagaraju Goruganti     crow::connections::systemBus->async_method_call(
33854fc587aSNagaraju Goruganti         [callback, ldapType](const boost::system::error_code ec,
33954fc587aSNagaraju Goruganti                              const GetObjectType& resp) {
34054fc587aSNagaraju Goruganti             LDAPConfigData confData{};
34154fc587aSNagaraju Goruganti             if (ec || resp.empty())
34254fc587aSNagaraju Goruganti             {
34354fc587aSNagaraju Goruganti                 BMCWEB_LOG_ERROR << "DBUS response error during getting of "
34454fc587aSNagaraju Goruganti                                     "service name: "
34554fc587aSNagaraju Goruganti                                  << ec;
34654fc587aSNagaraju Goruganti                 callback(false, confData, ldapType);
34754fc587aSNagaraju Goruganti                 return;
34854fc587aSNagaraju Goruganti             }
34954fc587aSNagaraju Goruganti             std::string service = resp.begin()->first;
35054fc587aSNagaraju Goruganti             crow::connections::systemBus->async_method_call(
35154fc587aSNagaraju Goruganti                 [callback, ldapType](const boost::system::error_code error_code,
3526973a582SRatan Gupta                                      const ManagedObjectType& ldapObjects) {
3536973a582SRatan Gupta                     LDAPConfigData confData{};
3546973a582SRatan Gupta                     if (error_code)
3556973a582SRatan Gupta                     {
356ab828d7cSRatan Gupta                         callback(false, confData, ldapType);
35754fc587aSNagaraju Goruganti                         BMCWEB_LOG_ERROR << "D-Bus responses error: "
35854fc587aSNagaraju Goruganti                                          << error_code;
3596973a582SRatan Gupta                         return;
3606973a582SRatan Gupta                     }
361ab828d7cSRatan Gupta 
362ab828d7cSRatan Gupta                     std::string ldapDbusType;
36354fc587aSNagaraju Goruganti                     std::string searchString;
36454fc587aSNagaraju Goruganti 
365ab828d7cSRatan Gupta                     if (ldapType == "LDAP")
366ab828d7cSRatan Gupta                     {
36754fc587aSNagaraju Goruganti                         ldapDbusType = "xyz.openbmc_project.User.Ldap.Config."
36854fc587aSNagaraju Goruganti                                        "Type.OpenLdap";
36954fc587aSNagaraju Goruganti                         searchString = "openldap";
370ab828d7cSRatan Gupta                     }
371ab828d7cSRatan Gupta                     else if (ldapType == "ActiveDirectory")
372ab828d7cSRatan Gupta                     {
37354fc587aSNagaraju Goruganti                         ldapDbusType =
37454fc587aSNagaraju Goruganti                             "xyz.openbmc_project.User.Ldap.Config.Type."
375ab828d7cSRatan Gupta                             "ActiveDirectory";
37654fc587aSNagaraju Goruganti                         searchString = "active_directory";
377ab828d7cSRatan Gupta                     }
378ab828d7cSRatan Gupta                     else
379ab828d7cSRatan Gupta                     {
38054fc587aSNagaraju Goruganti                         BMCWEB_LOG_ERROR
38154fc587aSNagaraju Goruganti                             << "Can't get the DbusType for the given type="
382ab828d7cSRatan Gupta                             << ldapType;
383ab828d7cSRatan Gupta                         callback(false, confData, ldapType);
384ab828d7cSRatan Gupta                         return;
385ab828d7cSRatan Gupta                     }
386ab828d7cSRatan Gupta 
387ab828d7cSRatan Gupta                     std::string ldapEnableInterfaceStr = ldapEnableInterface;
388ab828d7cSRatan Gupta                     std::string ldapConfigInterfaceStr = ldapConfigInterface;
389ab828d7cSRatan Gupta 
3906973a582SRatan Gupta                     for (const auto& object : ldapObjects)
3916973a582SRatan Gupta                     {
39254fc587aSNagaraju Goruganti                         // let's find the object whose ldap type is equal to the
39354fc587aSNagaraju Goruganti                         // given type
39454fc587aSNagaraju Goruganti                         if (object.first.str.find(searchString) ==
39554fc587aSNagaraju Goruganti                             std::string::npos)
3966973a582SRatan Gupta                         {
397ab828d7cSRatan Gupta                             continue;
398ab828d7cSRatan Gupta                         }
399ab828d7cSRatan Gupta 
4006973a582SRatan Gupta                         for (const auto& interface : object.second)
4016973a582SRatan Gupta                         {
4026973a582SRatan Gupta                             if (interface.first == ldapEnableInterfaceStr)
4036973a582SRatan Gupta                             {
4046973a582SRatan Gupta                                 // rest of the properties are string.
4056973a582SRatan Gupta                                 for (const auto& property : interface.second)
4066973a582SRatan Gupta                                 {
4076973a582SRatan Gupta                                     if (property.first == "Enabled")
4086973a582SRatan Gupta                                     {
4096973a582SRatan Gupta                                         const bool* value =
4106973a582SRatan Gupta                                             std::get_if<bool>(&property.second);
4116973a582SRatan Gupta                                         if (value == nullptr)
4126973a582SRatan Gupta                                         {
4136973a582SRatan Gupta                                             continue;
4146973a582SRatan Gupta                                         }
4156973a582SRatan Gupta                                         confData.serviceEnabled = *value;
4166973a582SRatan Gupta                                         break;
4176973a582SRatan Gupta                                     }
4186973a582SRatan Gupta                                 }
4196973a582SRatan Gupta                             }
4206973a582SRatan Gupta                             else if (interface.first == ldapConfigInterfaceStr)
4216973a582SRatan Gupta                             {
4226973a582SRatan Gupta 
4236973a582SRatan Gupta                                 for (const auto& property : interface.second)
4246973a582SRatan Gupta                                 {
425271584abSEd Tanous                                     const std::string* strValue =
42654fc587aSNagaraju Goruganti                                         std::get_if<std::string>(
42754fc587aSNagaraju Goruganti                                             &property.second);
428271584abSEd Tanous                                     if (strValue == nullptr)
4296973a582SRatan Gupta                                     {
4306973a582SRatan Gupta                                         continue;
4316973a582SRatan Gupta                                     }
4326973a582SRatan Gupta                                     if (property.first == "LDAPServerURI")
4336973a582SRatan Gupta                                     {
434271584abSEd Tanous                                         confData.uri = *strValue;
4356973a582SRatan Gupta                                     }
4366973a582SRatan Gupta                                     else if (property.first == "LDAPBindDN")
4376973a582SRatan Gupta                                     {
438271584abSEd Tanous                                         confData.bindDN = *strValue;
4396973a582SRatan Gupta                                     }
4406973a582SRatan Gupta                                     else if (property.first == "LDAPBaseDN")
4416973a582SRatan Gupta                                     {
442271584abSEd Tanous                                         confData.baseDN = *strValue;
4436973a582SRatan Gupta                                     }
44454fc587aSNagaraju Goruganti                                     else if (property.first ==
44554fc587aSNagaraju Goruganti                                              "LDAPSearchScope")
4466973a582SRatan Gupta                                     {
447271584abSEd Tanous                                         confData.searchScope = *strValue;
4486973a582SRatan Gupta                                     }
44954fc587aSNagaraju Goruganti                                     else if (property.first ==
45054fc587aSNagaraju Goruganti                                              "GroupNameAttribute")
4516973a582SRatan Gupta                                     {
452271584abSEd Tanous                                         confData.groupAttribute = *strValue;
4536973a582SRatan Gupta                                     }
45454fc587aSNagaraju Goruganti                                     else if (property.first ==
45554fc587aSNagaraju Goruganti                                              "UserNameAttribute")
4566973a582SRatan Gupta                                     {
457271584abSEd Tanous                                         confData.userNameAttribute = *strValue;
4586973a582SRatan Gupta                                     }
45954fc587aSNagaraju Goruganti                                     else if (property.first == "LDAPType")
460ab828d7cSRatan Gupta                                     {
461271584abSEd Tanous                                         confData.serverType = *strValue;
46254fc587aSNagaraju Goruganti                                     }
46354fc587aSNagaraju Goruganti                                 }
46454fc587aSNagaraju Goruganti                             }
46554fc587aSNagaraju Goruganti                             else if (interface.first ==
46654fc587aSNagaraju Goruganti                                      "xyz.openbmc_project.User."
46754fc587aSNagaraju Goruganti                                      "PrivilegeMapperEntry")
46854fc587aSNagaraju Goruganti                             {
46954fc587aSNagaraju Goruganti                                 LDAPRoleMapData roleMapData{};
47054fc587aSNagaraju Goruganti                                 for (const auto& property : interface.second)
47154fc587aSNagaraju Goruganti                                 {
472271584abSEd Tanous                                     const std::string* strValue =
47354fc587aSNagaraju Goruganti                                         std::get_if<std::string>(
47454fc587aSNagaraju Goruganti                                             &property.second);
47554fc587aSNagaraju Goruganti 
476271584abSEd Tanous                                     if (strValue == nullptr)
47754fc587aSNagaraju Goruganti                                     {
47854fc587aSNagaraju Goruganti                                         continue;
47954fc587aSNagaraju Goruganti                                     }
48054fc587aSNagaraju Goruganti 
48154fc587aSNagaraju Goruganti                                     if (property.first == "GroupName")
48254fc587aSNagaraju Goruganti                                     {
483271584abSEd Tanous                                         roleMapData.groupName = *strValue;
48454fc587aSNagaraju Goruganti                                     }
48554fc587aSNagaraju Goruganti                                     else if (property.first == "Privilege")
48654fc587aSNagaraju Goruganti                                     {
487271584abSEd Tanous                                         roleMapData.privilege = *strValue;
48854fc587aSNagaraju Goruganti                                     }
48954fc587aSNagaraju Goruganti                                 }
49054fc587aSNagaraju Goruganti 
49154fc587aSNagaraju Goruganti                                 confData.groupRoleList.push_back(std::make_pair(
49254fc587aSNagaraju Goruganti                                     object.first.str, roleMapData));
49354fc587aSNagaraju Goruganti                             }
49454fc587aSNagaraju Goruganti                         }
49554fc587aSNagaraju Goruganti                     }
496ab828d7cSRatan Gupta                     callback(true, confData, ldapType);
49754fc587aSNagaraju Goruganti                 },
49854fc587aSNagaraju Goruganti                 service, ldapRootObject, dbusObjManagerIntf,
4996973a582SRatan Gupta                 "GetManagedObjects");
50054fc587aSNagaraju Goruganti         },
50154fc587aSNagaraju Goruganti         mapperBusName, mapperObjectPath, mapperIntf, "GetObject",
50254fc587aSNagaraju Goruganti         ldapConfigObject, interfaces);
5036973a582SRatan Gupta }
5046973a582SRatan Gupta 
5051abe55efSEd Tanous class AccountService : public Node
5061abe55efSEd Tanous {
50788d16c9aSLewanczyk, Dawid   public:
5081abe55efSEd Tanous     AccountService(CrowApp& app) : Node(app, "/redfish/v1/AccountService/")
5091abe55efSEd Tanous     {
5103ebd75f7SEd Tanous         entityPrivileges = {
5114b1b8683SBorawski.Lukasz             {boost::beast::http::verb::get,
5124b1b8683SBorawski.Lukasz              {{"ConfigureUsers"}, {"ConfigureManager"}}},
513e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
514e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
515e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
516e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
517e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
51888d16c9aSLewanczyk, Dawid     }
51988d16c9aSLewanczyk, Dawid 
52088d16c9aSLewanczyk, Dawid   private:
5218a07d286SRatan Gupta     /**
5228a07d286SRatan Gupta      * @brief parses the authentication section under the LDAP
5238a07d286SRatan Gupta      * @param input JSON data
5248a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
5258a07d286SRatan Gupta      * @param userName  userName to be filled from the given JSON.
5268a07d286SRatan Gupta      * @param password  password to be filled from the given JSON.
5278a07d286SRatan Gupta      */
5288a07d286SRatan Gupta     void
5298a07d286SRatan Gupta         parseLDAPAuthenticationJson(nlohmann::json input,
5308a07d286SRatan Gupta                                     const std::shared_ptr<AsyncResp>& asyncResp,
5318a07d286SRatan Gupta                                     std::optional<std::string>& username,
5328a07d286SRatan Gupta                                     std::optional<std::string>& password)
5338a07d286SRatan Gupta     {
5348a07d286SRatan Gupta         std::optional<std::string> authType;
5358a07d286SRatan Gupta 
5368a07d286SRatan Gupta         if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
5378a07d286SRatan Gupta                                  authType, "Username", username, "Password",
5388a07d286SRatan Gupta                                  password))
5398a07d286SRatan Gupta         {
5408a07d286SRatan Gupta             return;
5418a07d286SRatan Gupta         }
5428a07d286SRatan Gupta         if (!authType)
5438a07d286SRatan Gupta         {
5448a07d286SRatan Gupta             return;
5458a07d286SRatan Gupta         }
5468a07d286SRatan Gupta         if (*authType != "UsernameAndPassword")
5478a07d286SRatan Gupta         {
5488a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, *authType,
5498a07d286SRatan Gupta                                              "AuthenticationType");
5508a07d286SRatan Gupta             return;
5518a07d286SRatan Gupta         }
5528a07d286SRatan Gupta     }
5538a07d286SRatan Gupta     /**
5548a07d286SRatan Gupta      * @brief parses the LDAPService section under the LDAP
5558a07d286SRatan Gupta      * @param input JSON data
5568a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
5578a07d286SRatan Gupta      * @param baseDNList baseDN to be filled from the given JSON.
5588a07d286SRatan Gupta      * @param userNameAttribute  userName to be filled from the given JSON.
5598a07d286SRatan Gupta      * @param groupaAttribute  password to be filled from the given JSON.
5608a07d286SRatan Gupta      */
5618a07d286SRatan Gupta 
5628a07d286SRatan Gupta     void parseLDAPServiceJson(
5638a07d286SRatan Gupta         nlohmann::json input, const std::shared_ptr<AsyncResp>& asyncResp,
5648a07d286SRatan Gupta         std::optional<std::vector<std::string>>& baseDNList,
5658a07d286SRatan Gupta         std::optional<std::string>& userNameAttribute,
5668a07d286SRatan Gupta         std::optional<std::string>& groupsAttribute)
5678a07d286SRatan Gupta     {
5688a07d286SRatan Gupta         std::optional<nlohmann::json> searchSettings;
5698a07d286SRatan Gupta 
5708a07d286SRatan Gupta         if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
5718a07d286SRatan Gupta                                  searchSettings))
5728a07d286SRatan Gupta         {
5738a07d286SRatan Gupta             return;
5748a07d286SRatan Gupta         }
5758a07d286SRatan Gupta         if (!searchSettings)
5768a07d286SRatan Gupta         {
5778a07d286SRatan Gupta             return;
5788a07d286SRatan Gupta         }
5798a07d286SRatan Gupta         if (!json_util::readJson(*searchSettings, asyncResp->res,
5808a07d286SRatan Gupta                                  "BaseDistinguishedNames", baseDNList,
5818a07d286SRatan Gupta                                  "UsernameAttribute", userNameAttribute,
5828a07d286SRatan Gupta                                  "GroupsAttribute", groupsAttribute))
5838a07d286SRatan Gupta         {
5848a07d286SRatan Gupta             return;
5858a07d286SRatan Gupta         }
5868a07d286SRatan Gupta     }
5878a07d286SRatan Gupta     /**
5888a07d286SRatan Gupta      * @brief updates the LDAP server address and updates the
5898a07d286SRatan Gupta               json response with the new value.
5908a07d286SRatan Gupta      * @param serviceAddressList address to be updated.
5918a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
5928a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
5938a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
5948a07d286SRatan Gupta      */
5958a07d286SRatan Gupta 
5968a07d286SRatan Gupta     void handleServiceAddressPatch(
5978a07d286SRatan Gupta         const std::vector<std::string>& serviceAddressList,
5988a07d286SRatan Gupta         const std::shared_ptr<AsyncResp>& asyncResp,
5998a07d286SRatan Gupta         const std::string& ldapServerElementName,
6008a07d286SRatan Gupta         const std::string& ldapConfigObject)
6018a07d286SRatan Gupta     {
6028a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
6038a07d286SRatan Gupta             [asyncResp, ldapServerElementName,
6048a07d286SRatan Gupta              serviceAddressList](const boost::system::error_code ec) {
6058a07d286SRatan Gupta                 if (ec)
6068a07d286SRatan Gupta                 {
6078a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG
6088a07d286SRatan Gupta                         << "Error Occured in updating the service address";
6098a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
6108a07d286SRatan Gupta                     return;
6118a07d286SRatan Gupta                 }
6128a07d286SRatan Gupta                 std::vector<std::string> modifiedserviceAddressList = {
6138a07d286SRatan Gupta                     serviceAddressList.front()};
6148a07d286SRatan Gupta                 asyncResp->res
6158a07d286SRatan Gupta                     .jsonValue[ldapServerElementName]["ServiceAddresses"] =
6168a07d286SRatan Gupta                     modifiedserviceAddressList;
6178a07d286SRatan Gupta                 if ((serviceAddressList).size() > 1)
6188a07d286SRatan Gupta                 {
6198a07d286SRatan Gupta                     messages::propertyValueModified(asyncResp->res,
6208a07d286SRatan Gupta                                                     "ServiceAddresses",
6218a07d286SRatan Gupta                                                     serviceAddressList.front());
6228a07d286SRatan Gupta                 }
6238a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the service address";
6248a07d286SRatan Gupta             },
6258a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6268a07d286SRatan Gupta             ldapConfigInterface, "LDAPServerURI",
6278a07d286SRatan Gupta             std::variant<std::string>(serviceAddressList.front()));
6288a07d286SRatan Gupta     }
6298a07d286SRatan Gupta     /**
6308a07d286SRatan Gupta      * @brief updates the LDAP Bind DN and updates the
6318a07d286SRatan Gupta               json response with the new value.
6328a07d286SRatan Gupta      * @param username name of the user which needs to be updated.
6338a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
6348a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
6358a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
6368a07d286SRatan Gupta      */
6378a07d286SRatan Gupta 
6388a07d286SRatan Gupta     void handleUserNamePatch(const std::string& username,
6398a07d286SRatan Gupta                              const std::shared_ptr<AsyncResp>& asyncResp,
6408a07d286SRatan Gupta                              const std::string& ldapServerElementName,
6418a07d286SRatan Gupta                              const std::string& ldapConfigObject)
6428a07d286SRatan Gupta     {
6438a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
6448a07d286SRatan Gupta             [asyncResp, username,
6458a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
6468a07d286SRatan Gupta                 if (ec)
6478a07d286SRatan Gupta                 {
6488a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG
6498a07d286SRatan Gupta                         << "Error occured in updating the username";
6508a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
6518a07d286SRatan Gupta                     return;
6528a07d286SRatan Gupta                 }
6538a07d286SRatan Gupta                 asyncResp->res.jsonValue[ldapServerElementName]
6548a07d286SRatan Gupta                                         ["Authentication"]["Username"] =
6558a07d286SRatan Gupta                     username;
6568a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the username";
6578a07d286SRatan Gupta             },
6588a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6598a07d286SRatan Gupta             ldapConfigInterface, "LDAPBindDN",
6608a07d286SRatan Gupta             std::variant<std::string>(username));
6618a07d286SRatan Gupta     }
6628a07d286SRatan Gupta 
6638a07d286SRatan Gupta     /**
6648a07d286SRatan Gupta      * @brief updates the LDAP password
6658a07d286SRatan Gupta      * @param password : ldap password which needs to be updated.
6668a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
6678a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
6688a07d286SRatan Gupta      *        server(openLDAP/ActiveDirectory)
6698a07d286SRatan Gupta      */
6708a07d286SRatan Gupta 
6718a07d286SRatan Gupta     void handlePasswordPatch(const std::string& password,
6728a07d286SRatan Gupta                              const std::shared_ptr<AsyncResp>& asyncResp,
6738a07d286SRatan Gupta                              const std::string& ldapServerElementName,
6748a07d286SRatan Gupta                              const std::string& ldapConfigObject)
6758a07d286SRatan Gupta     {
6768a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
6778a07d286SRatan Gupta             [asyncResp, password,
6788a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
6798a07d286SRatan Gupta                 if (ec)
6808a07d286SRatan Gupta                 {
6818a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG
6828a07d286SRatan Gupta                         << "Error occured in updating the password";
6838a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
6848a07d286SRatan Gupta                     return;
6858a07d286SRatan Gupta                 }
6868a07d286SRatan Gupta                 asyncResp->res.jsonValue[ldapServerElementName]
6878a07d286SRatan Gupta                                         ["Authentication"]["Password"] = "";
6888a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the password";
6898a07d286SRatan Gupta             },
6908a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6918a07d286SRatan Gupta             ldapConfigInterface, "LDAPBindDNPassword",
6928a07d286SRatan Gupta             std::variant<std::string>(password));
6938a07d286SRatan Gupta     }
6948a07d286SRatan Gupta 
6958a07d286SRatan Gupta     /**
6968a07d286SRatan Gupta      * @brief updates the LDAP BaseDN and updates the
6978a07d286SRatan Gupta               json response with the new value.
6988a07d286SRatan Gupta      * @param baseDNList baseDN list which needs to be updated.
6998a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
7008a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
7018a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
7028a07d286SRatan Gupta      */
7038a07d286SRatan Gupta 
7048a07d286SRatan Gupta     void handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7058a07d286SRatan Gupta                            const std::shared_ptr<AsyncResp>& asyncResp,
7068a07d286SRatan Gupta                            const std::string& ldapServerElementName,
7078a07d286SRatan Gupta                            const std::string& ldapConfigObject)
7088a07d286SRatan Gupta     {
7098a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
7108a07d286SRatan Gupta             [asyncResp, baseDNList,
7118a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
7128a07d286SRatan Gupta                 if (ec)
7138a07d286SRatan Gupta                 {
7148a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG << "Error Occured in Updating the base DN";
7158a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
7168a07d286SRatan Gupta                     return;
7178a07d286SRatan Gupta                 }
7188a07d286SRatan Gupta                 auto& serverTypeJson =
7198a07d286SRatan Gupta                     asyncResp->res.jsonValue[ldapServerElementName];
7208a07d286SRatan Gupta                 auto& searchSettingsJson =
7218a07d286SRatan Gupta                     serverTypeJson["LDAPService"]["SearchSettings"];
7228a07d286SRatan Gupta                 std::vector<std::string> modifiedBaseDNList = {
7238a07d286SRatan Gupta                     baseDNList.front()};
7248a07d286SRatan Gupta                 searchSettingsJson["BaseDistinguishedNames"] =
7258a07d286SRatan Gupta                     modifiedBaseDNList;
7268a07d286SRatan Gupta                 if (baseDNList.size() > 1)
7278a07d286SRatan Gupta                 {
7288a07d286SRatan Gupta                     messages::propertyValueModified(asyncResp->res,
7298a07d286SRatan Gupta                                                     "BaseDistinguishedNames",
7308a07d286SRatan Gupta                                                     baseDNList.front());
7318a07d286SRatan Gupta                 }
7328a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the base DN";
7338a07d286SRatan Gupta             },
7348a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7358a07d286SRatan Gupta             ldapConfigInterface, "LDAPBaseDN",
7368a07d286SRatan Gupta             std::variant<std::string>(baseDNList.front()));
7378a07d286SRatan Gupta     }
7388a07d286SRatan Gupta     /**
7398a07d286SRatan Gupta      * @brief updates the LDAP user name attribute and updates the
7408a07d286SRatan Gupta               json response with the new value.
7418a07d286SRatan Gupta      * @param userNameAttribute attribute to be updated.
7428a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
7438a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
7448a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
7458a07d286SRatan Gupta      */
7468a07d286SRatan Gupta 
7478a07d286SRatan Gupta     void handleUserNameAttrPatch(const std::string& userNameAttribute,
7488a07d286SRatan Gupta                                  const std::shared_ptr<AsyncResp>& asyncResp,
7498a07d286SRatan Gupta                                  const std::string& ldapServerElementName,
7508a07d286SRatan Gupta                                  const std::string& ldapConfigObject)
7518a07d286SRatan Gupta     {
7528a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
7538a07d286SRatan Gupta             [asyncResp, userNameAttribute,
7548a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
7558a07d286SRatan Gupta                 if (ec)
7568a07d286SRatan Gupta                 {
7578a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG << "Error Occured in Updating the "
7588a07d286SRatan Gupta                                         "username attribute";
7598a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
7608a07d286SRatan Gupta                     return;
7618a07d286SRatan Gupta                 }
7628a07d286SRatan Gupta                 auto& serverTypeJson =
7638a07d286SRatan Gupta                     asyncResp->res.jsonValue[ldapServerElementName];
7648a07d286SRatan Gupta                 auto& searchSettingsJson =
7658a07d286SRatan Gupta                     serverTypeJson["LDAPService"]["SearchSettings"];
7668a07d286SRatan Gupta                 searchSettingsJson["UsernameAttribute"] = userNameAttribute;
7678a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the user name attr.";
7688a07d286SRatan Gupta             },
7698a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7708a07d286SRatan Gupta             ldapConfigInterface, "UserNameAttribute",
7718a07d286SRatan Gupta             std::variant<std::string>(userNameAttribute));
7728a07d286SRatan Gupta     }
7738a07d286SRatan Gupta     /**
7748a07d286SRatan Gupta      * @brief updates the LDAP group attribute and updates the
7758a07d286SRatan Gupta               json response with the new value.
7768a07d286SRatan Gupta      * @param groupsAttribute attribute to be updated.
7778a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
7788a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
7798a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
7808a07d286SRatan Gupta      */
7818a07d286SRatan Gupta 
7828a07d286SRatan Gupta     void handleGroupNameAttrPatch(const std::string& groupsAttribute,
7838a07d286SRatan Gupta                                   const std::shared_ptr<AsyncResp>& asyncResp,
7848a07d286SRatan Gupta                                   const std::string& ldapServerElementName,
7858a07d286SRatan Gupta                                   const std::string& ldapConfigObject)
7868a07d286SRatan Gupta     {
7878a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
7888a07d286SRatan Gupta             [asyncResp, groupsAttribute,
7898a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
7908a07d286SRatan Gupta                 if (ec)
7918a07d286SRatan Gupta                 {
7928a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG << "Error Occured in Updating the "
7938a07d286SRatan Gupta                                         "groupname attribute";
7948a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
7958a07d286SRatan Gupta                     return;
7968a07d286SRatan Gupta                 }
7978a07d286SRatan Gupta                 auto& serverTypeJson =
7988a07d286SRatan Gupta                     asyncResp->res.jsonValue[ldapServerElementName];
7998a07d286SRatan Gupta                 auto& searchSettingsJson =
8008a07d286SRatan Gupta                     serverTypeJson["LDAPService"]["SearchSettings"];
8018a07d286SRatan Gupta                 searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8028a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8038a07d286SRatan Gupta             },
8048a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8058a07d286SRatan Gupta             ldapConfigInterface, "GroupNameAttribute",
8068a07d286SRatan Gupta             std::variant<std::string>(groupsAttribute));
8078a07d286SRatan Gupta     }
8088a07d286SRatan Gupta     /**
8098a07d286SRatan Gupta      * @brief updates the LDAP service enable and updates the
8108a07d286SRatan Gupta               json response with the new value.
8118a07d286SRatan Gupta      * @param input JSON data.
8128a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
8138a07d286SRatan Gupta      * @param ldapServerElementName Type of LDAP
8148a07d286SRatan Gupta      server(openLDAP/ActiveDirectory)
8158a07d286SRatan Gupta      */
8168a07d286SRatan Gupta 
8178a07d286SRatan Gupta     void handleServiceEnablePatch(bool serviceEnabled,
8188a07d286SRatan Gupta                                   const std::shared_ptr<AsyncResp>& asyncResp,
8198a07d286SRatan Gupta                                   const std::string& ldapServerElementName,
8208a07d286SRatan Gupta                                   const std::string& ldapConfigObject)
8218a07d286SRatan Gupta     {
8228a07d286SRatan Gupta         crow::connections::systemBus->async_method_call(
8238a07d286SRatan Gupta             [asyncResp, serviceEnabled,
8248a07d286SRatan Gupta              ldapServerElementName](const boost::system::error_code ec) {
8258a07d286SRatan Gupta                 if (ec)
8268a07d286SRatan Gupta                 {
8278a07d286SRatan Gupta                     BMCWEB_LOG_DEBUG
8288a07d286SRatan Gupta                         << "Error Occured in Updating the service enable";
8298a07d286SRatan Gupta                     messages::internalError(asyncResp->res);
8308a07d286SRatan Gupta                     return;
8318a07d286SRatan Gupta                 }
8328a07d286SRatan Gupta                 asyncResp->res
8338a07d286SRatan Gupta                     .jsonValue[ldapServerElementName]["ServiceEnabled"] =
8348a07d286SRatan Gupta                     serviceEnabled;
8358a07d286SRatan Gupta                 BMCWEB_LOG_DEBUG << "Updated Service enable = "
8368a07d286SRatan Gupta                                  << serviceEnabled;
8378a07d286SRatan Gupta             },
8388a07d286SRatan Gupta             ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8398a07d286SRatan Gupta             ldapEnableInterface, "Enabled", std::variant<bool>(serviceEnabled));
8408a07d286SRatan Gupta     }
8418a07d286SRatan Gupta 
8428a07d286SRatan Gupta     /**
8438a07d286SRatan Gupta      * @brief Get the required values from the given JSON, validates the
8448a07d286SRatan Gupta      *        value and create the LDAP config object.
8458a07d286SRatan Gupta      * @param input JSON data
8468a07d286SRatan Gupta      * @param asyncResp pointer to the JSON response
8478a07d286SRatan Gupta      * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
8488a07d286SRatan Gupta      */
8498a07d286SRatan Gupta 
8508a07d286SRatan Gupta     void handleLDAPPatch(nlohmann::json& input,
8518a07d286SRatan Gupta                          const std::shared_ptr<AsyncResp>& asyncResp,
8528a07d286SRatan Gupta                          const crow::Request& req,
8538a07d286SRatan Gupta                          const std::vector<std::string>& params,
8548a07d286SRatan Gupta                          const std::string& serverType)
8558a07d286SRatan Gupta     {
856eb2bbe56SRatan Gupta         std::string dbusObjectPath;
857eb2bbe56SRatan Gupta         if (serverType == "ActiveDirectory")
858eb2bbe56SRatan Gupta         {
859eb2bbe56SRatan Gupta             dbusObjectPath = ADConfigObject;
860eb2bbe56SRatan Gupta         }
861eb2bbe56SRatan Gupta         else if (serverType == "LDAP")
862eb2bbe56SRatan Gupta         {
863eb2bbe56SRatan Gupta             dbusObjectPath = ldapConfigObject;
864eb2bbe56SRatan Gupta         }
865eb2bbe56SRatan Gupta 
8668a07d286SRatan Gupta         std::optional<nlohmann::json> authentication;
8678a07d286SRatan Gupta         std::optional<nlohmann::json> ldapService;
8688a07d286SRatan Gupta         std::optional<std::string> accountProviderType;
8698a07d286SRatan Gupta         std::optional<std::vector<std::string>> serviceAddressList;
8708a07d286SRatan Gupta         std::optional<bool> serviceEnabled;
8718a07d286SRatan Gupta         std::optional<std::vector<std::string>> baseDNList;
8728a07d286SRatan Gupta         std::optional<std::string> userNameAttribute;
8738a07d286SRatan Gupta         std::optional<std::string> groupsAttribute;
8748a07d286SRatan Gupta         std::optional<std::string> userName;
8758a07d286SRatan Gupta         std::optional<std::string> password;
87606785244SRatan Gupta         std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
8778a07d286SRatan Gupta 
8788a07d286SRatan Gupta         if (!json_util::readJson(input, asyncResp->res, "Authentication",
8798a07d286SRatan Gupta                                  authentication, "LDAPService", ldapService,
8808a07d286SRatan Gupta                                  "ServiceAddresses", serviceAddressList,
8818a07d286SRatan Gupta                                  "AccountProviderType", accountProviderType,
88206785244SRatan Gupta                                  "ServiceEnabled", serviceEnabled,
88306785244SRatan Gupta                                  "RemoteRoleMapping", remoteRoleMapData))
8848a07d286SRatan Gupta         {
8858a07d286SRatan Gupta             return;
8868a07d286SRatan Gupta         }
8878a07d286SRatan Gupta 
8888a07d286SRatan Gupta         if (authentication)
8898a07d286SRatan Gupta         {
8908a07d286SRatan Gupta             parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
8918a07d286SRatan Gupta                                         password);
8928a07d286SRatan Gupta         }
8938a07d286SRatan Gupta         if (ldapService)
8948a07d286SRatan Gupta         {
8958a07d286SRatan Gupta             parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
8968a07d286SRatan Gupta                                  userNameAttribute, groupsAttribute);
8978a07d286SRatan Gupta         }
8988a07d286SRatan Gupta         if (accountProviderType)
8998a07d286SRatan Gupta         {
9008a07d286SRatan Gupta             messages::propertyNotWritable(asyncResp->res,
9018a07d286SRatan Gupta                                           "AccountProviderType");
9028a07d286SRatan Gupta         }
9038a07d286SRatan Gupta         if (serviceAddressList)
9048a07d286SRatan Gupta         {
9058a07d286SRatan Gupta             if ((*serviceAddressList).size() == 0)
9068a07d286SRatan Gupta             {
9078a07d286SRatan Gupta                 messages::propertyValueNotInList(asyncResp->res, "[]",
9088a07d286SRatan Gupta                                                  "ServiceAddress");
9098a07d286SRatan Gupta                 return;
9108a07d286SRatan Gupta             }
9118a07d286SRatan Gupta         }
9128a07d286SRatan Gupta         if (baseDNList)
9138a07d286SRatan Gupta         {
9148a07d286SRatan Gupta             if ((*baseDNList).size() == 0)
9158a07d286SRatan Gupta             {
9168a07d286SRatan Gupta                 messages::propertyValueNotInList(asyncResp->res, "[]",
9178a07d286SRatan Gupta                                                  "BaseDistinguishedNames");
9188a07d286SRatan Gupta                 return;
9198a07d286SRatan Gupta             }
9208a07d286SRatan Gupta         }
9218a07d286SRatan Gupta 
9228a07d286SRatan Gupta         // nothing to update, then return
9238a07d286SRatan Gupta         if (!userName && !password && !serviceAddressList && !baseDNList &&
92406785244SRatan Gupta             !userNameAttribute && !groupsAttribute && !serviceEnabled &&
92506785244SRatan Gupta             !remoteRoleMapData)
9268a07d286SRatan Gupta         {
9278a07d286SRatan Gupta             return;
9288a07d286SRatan Gupta         }
9298a07d286SRatan Gupta 
9308a07d286SRatan Gupta         // Get the existing resource first then keep modifying
9318a07d286SRatan Gupta         // whenever any property gets updated.
932ab828d7cSRatan Gupta         getLDAPConfigData(serverType, [this, asyncResp, userName, password,
933ab828d7cSRatan Gupta                                        baseDNList, userNameAttribute,
934ab828d7cSRatan Gupta                                        groupsAttribute, accountProviderType,
935eb2bbe56SRatan Gupta                                        serviceAddressList, serviceEnabled,
93606785244SRatan Gupta                                        dbusObjectPath, remoteRoleMapData](
937ab828d7cSRatan Gupta                                           bool success, LDAPConfigData confData,
938ab828d7cSRatan Gupta                                           const std::string& serverType) {
9398a07d286SRatan Gupta             if (!success)
9408a07d286SRatan Gupta             {
9418a07d286SRatan Gupta                 messages::internalError(asyncResp->res);
9428a07d286SRatan Gupta                 return;
9438a07d286SRatan Gupta             }
944ab828d7cSRatan Gupta             parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverType);
9458a07d286SRatan Gupta             if (confData.serviceEnabled)
9468a07d286SRatan Gupta             {
9478a07d286SRatan Gupta                 // Disable the service first and update the rest of
9488a07d286SRatan Gupta                 // the properties.
9498a07d286SRatan Gupta                 handleServiceEnablePatch(false, asyncResp, serverType,
950eb2bbe56SRatan Gupta                                          dbusObjectPath);
9518a07d286SRatan Gupta             }
9528a07d286SRatan Gupta 
9538a07d286SRatan Gupta             if (serviceAddressList)
9548a07d286SRatan Gupta             {
9558a07d286SRatan Gupta                 handleServiceAddressPatch(*serviceAddressList, asyncResp,
956eb2bbe56SRatan Gupta                                           serverType, dbusObjectPath);
9578a07d286SRatan Gupta             }
9588a07d286SRatan Gupta             if (userName)
9598a07d286SRatan Gupta             {
9608a07d286SRatan Gupta                 handleUserNamePatch(*userName, asyncResp, serverType,
961eb2bbe56SRatan Gupta                                     dbusObjectPath);
9628a07d286SRatan Gupta             }
9638a07d286SRatan Gupta             if (password)
9648a07d286SRatan Gupta             {
9658a07d286SRatan Gupta                 handlePasswordPatch(*password, asyncResp, serverType,
966eb2bbe56SRatan Gupta                                     dbusObjectPath);
9678a07d286SRatan Gupta             }
9688a07d286SRatan Gupta 
9698a07d286SRatan Gupta             if (baseDNList)
9708a07d286SRatan Gupta             {
9718a07d286SRatan Gupta                 handleBaseDNPatch(*baseDNList, asyncResp, serverType,
972eb2bbe56SRatan Gupta                                   dbusObjectPath);
9738a07d286SRatan Gupta             }
9748a07d286SRatan Gupta             if (userNameAttribute)
9758a07d286SRatan Gupta             {
9768a07d286SRatan Gupta                 handleUserNameAttrPatch(*userNameAttribute, asyncResp,
977eb2bbe56SRatan Gupta                                         serverType, dbusObjectPath);
9788a07d286SRatan Gupta             }
9798a07d286SRatan Gupta             if (groupsAttribute)
9808a07d286SRatan Gupta             {
9818a07d286SRatan Gupta                 handleGroupNameAttrPatch(*groupsAttribute, asyncResp,
982eb2bbe56SRatan Gupta                                          serverType, dbusObjectPath);
9838a07d286SRatan Gupta             }
9848a07d286SRatan Gupta             if (serviceEnabled)
9858a07d286SRatan Gupta             {
9868a07d286SRatan Gupta                 // if user has given the value as true then enable
9878a07d286SRatan Gupta                 // the service. if user has given false then no-op
9888a07d286SRatan Gupta                 // as service is already stopped.
9898a07d286SRatan Gupta                 if (*serviceEnabled)
9908a07d286SRatan Gupta                 {
9918a07d286SRatan Gupta                     handleServiceEnablePatch(*serviceEnabled, asyncResp,
992eb2bbe56SRatan Gupta                                              serverType, dbusObjectPath);
9938a07d286SRatan Gupta                 }
9948a07d286SRatan Gupta             }
9958a07d286SRatan Gupta             else
9968a07d286SRatan Gupta             {
9978a07d286SRatan Gupta                 // if user has not given the service enabled value
9988a07d286SRatan Gupta                 // then revert it to the same state as it was
9998a07d286SRatan Gupta                 // before.
10008a07d286SRatan Gupta                 handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
1001eb2bbe56SRatan Gupta                                          serverType, dbusObjectPath);
10028a07d286SRatan Gupta             }
100306785244SRatan Gupta 
100406785244SRatan Gupta             if (remoteRoleMapData)
100506785244SRatan Gupta             {
100606785244SRatan Gupta                 std::vector<nlohmann::json> remoteRoleMap =
100706785244SRatan Gupta                     std::move(*remoteRoleMapData);
100806785244SRatan Gupta 
100906785244SRatan Gupta                 handleRoleMapPatch(asyncResp, confData.groupRoleList,
101006785244SRatan Gupta                                    serverType, remoteRoleMap);
101106785244SRatan Gupta             }
10128a07d286SRatan Gupta         });
10138a07d286SRatan Gupta     }
1014d4b5443fSEd Tanous 
101555c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
10161abe55efSEd Tanous                const std::vector<std::string>& params) override
10171abe55efSEd Tanous     {
10183d958bbcSAppaRao Puli         auto asyncResp = std::make_shared<AsyncResp>(res);
10193d958bbcSAppaRao Puli         res.jsonValue = {
10203d958bbcSAppaRao Puli             {"@odata.context", "/redfish/v1/"
10213d958bbcSAppaRao Puli                                "$metadata#AccountService.AccountService"},
10223d958bbcSAppaRao Puli             {"@odata.id", "/redfish/v1/AccountService"},
10233d958bbcSAppaRao Puli             {"@odata.type", "#AccountService."
102437cce918SMarri Devender Rao                             "v1_4_0.AccountService"},
10253d958bbcSAppaRao Puli             {"Id", "AccountService"},
10263d958bbcSAppaRao Puli             {"Name", "Account Service"},
10273d958bbcSAppaRao Puli             {"Description", "Account Service"},
10283d958bbcSAppaRao Puli             {"ServiceEnabled", true},
1029343ff2e1SAppaRao Puli             {"MaxPasswordLength", 20},
10303d958bbcSAppaRao Puli             {"Accounts",
10313d958bbcSAppaRao Puli              {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}},
103237cce918SMarri Devender Rao             {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}},
103337cce918SMarri Devender Rao             {"LDAP",
103437cce918SMarri Devender Rao              {{"Certificates",
103537cce918SMarri Devender Rao                {{"@odata.id",
103637cce918SMarri Devender Rao                  "/redfish/v1/AccountService/LDAP/Certificates"}}}}}};
10373d958bbcSAppaRao Puli         crow::connections::systemBus->async_method_call(
10383d958bbcSAppaRao Puli             [asyncResp](
10393d958bbcSAppaRao Puli                 const boost::system::error_code ec,
10403d958bbcSAppaRao Puli                 const std::vector<std::pair<
1041abf2add6SEd Tanous                     std::string, std::variant<uint32_t, uint16_t, uint8_t>>>&
10423d958bbcSAppaRao Puli                     propertiesList) {
10433d958bbcSAppaRao Puli                 if (ec)
10443d958bbcSAppaRao Puli                 {
10453d958bbcSAppaRao Puli                     messages::internalError(asyncResp->res);
10463d958bbcSAppaRao Puli                     return;
10473d958bbcSAppaRao Puli                 }
10483d958bbcSAppaRao Puli                 BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
10493d958bbcSAppaRao Puli                                  << "properties for AccountService";
10503d958bbcSAppaRao Puli                 for (const std::pair<std::string,
1051abf2add6SEd Tanous                                      std::variant<uint32_t, uint16_t, uint8_t>>&
10523d958bbcSAppaRao Puli                          property : propertiesList)
10533d958bbcSAppaRao Puli                 {
10543d958bbcSAppaRao Puli                     if (property.first == "MinPasswordLength")
10553d958bbcSAppaRao Puli                     {
10563d958bbcSAppaRao Puli                         const uint8_t* value =
1057abf2add6SEd Tanous                             std::get_if<uint8_t>(&property.second);
10583d958bbcSAppaRao Puli                         if (value != nullptr)
10593d958bbcSAppaRao Puli                         {
10603d958bbcSAppaRao Puli                             asyncResp->res.jsonValue["MinPasswordLength"] =
10613d958bbcSAppaRao Puli                                 *value;
10623d958bbcSAppaRao Puli                         }
10633d958bbcSAppaRao Puli                     }
10643d958bbcSAppaRao Puli                     if (property.first == "AccountUnlockTimeout")
10653d958bbcSAppaRao Puli                     {
10663d958bbcSAppaRao Puli                         const uint32_t* value =
1067abf2add6SEd Tanous                             std::get_if<uint32_t>(&property.second);
10683d958bbcSAppaRao Puli                         if (value != nullptr)
10693d958bbcSAppaRao Puli                         {
10703d958bbcSAppaRao Puli                             asyncResp->res.jsonValue["AccountLockoutDuration"] =
10713d958bbcSAppaRao Puli                                 *value;
10723d958bbcSAppaRao Puli                         }
10733d958bbcSAppaRao Puli                     }
10743d958bbcSAppaRao Puli                     if (property.first == "MaxLoginAttemptBeforeLockout")
10753d958bbcSAppaRao Puli                     {
10763d958bbcSAppaRao Puli                         const uint16_t* value =
1077abf2add6SEd Tanous                             std::get_if<uint16_t>(&property.second);
10783d958bbcSAppaRao Puli                         if (value != nullptr)
10793d958bbcSAppaRao Puli                         {
10803d958bbcSAppaRao Puli                             asyncResp->res
10813d958bbcSAppaRao Puli                                 .jsonValue["AccountLockoutThreshold"] = *value;
10823d958bbcSAppaRao Puli                         }
10833d958bbcSAppaRao Puli                     }
10843d958bbcSAppaRao Puli                 }
10853d958bbcSAppaRao Puli             },
10863d958bbcSAppaRao Puli             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
10873d958bbcSAppaRao Puli             "org.freedesktop.DBus.Properties", "GetAll",
10883d958bbcSAppaRao Puli             "xyz.openbmc_project.User.AccountPolicy");
10896973a582SRatan Gupta 
1090ab828d7cSRatan Gupta         auto callback = [asyncResp](bool success, LDAPConfigData& confData,
1091ab828d7cSRatan Gupta                                     const std::string& ldapType) {
1092ab828d7cSRatan Gupta             parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1093ab828d7cSRatan Gupta         };
1094ab828d7cSRatan Gupta 
1095ab828d7cSRatan Gupta         getLDAPConfigData("LDAP", callback);
1096ab828d7cSRatan Gupta         getLDAPConfigData("ActiveDirectory", callback);
10973d958bbcSAppaRao Puli     }
10986973a582SRatan Gupta 
10993d958bbcSAppaRao Puli     void doPatch(crow::Response& res, const crow::Request& req,
11003d958bbcSAppaRao Puli                  const std::vector<std::string>& params) override
11013d958bbcSAppaRao Puli     {
11023d958bbcSAppaRao Puli         auto asyncResp = std::make_shared<AsyncResp>(res);
11033d958bbcSAppaRao Puli 
11043d958bbcSAppaRao Puli         std::optional<uint32_t> unlockTimeout;
11053d958bbcSAppaRao Puli         std::optional<uint16_t> lockoutThreshold;
110619fb6e71SRatan Gupta         std::optional<uint16_t> minPasswordLength;
110719fb6e71SRatan Gupta         std::optional<uint16_t> maxPasswordLength;
11088a07d286SRatan Gupta         std::optional<nlohmann::json> ldapObject;
1109eb2bbe56SRatan Gupta         std::optional<nlohmann::json> activeDirectoryObject;
111019fb6e71SRatan Gupta 
11113d958bbcSAppaRao Puli         if (!json_util::readJson(req, res, "AccountLockoutDuration",
11123d958bbcSAppaRao Puli                                  unlockTimeout, "AccountLockoutThreshold",
111319fb6e71SRatan Gupta                                  lockoutThreshold, "MaxPasswordLength",
111419fb6e71SRatan Gupta                                  maxPasswordLength, "MinPasswordLength",
1115eb2bbe56SRatan Gupta                                  minPasswordLength, "LDAP", ldapObject,
1116eb2bbe56SRatan Gupta                                  "ActiveDirectory", activeDirectoryObject))
11173d958bbcSAppaRao Puli         {
11183d958bbcSAppaRao Puli             return;
11193d958bbcSAppaRao Puli         }
112019fb6e71SRatan Gupta 
112119fb6e71SRatan Gupta         if (minPasswordLength)
112219fb6e71SRatan Gupta         {
112319fb6e71SRatan Gupta             messages::propertyNotWritable(asyncResp->res, "MinPasswordLength");
112419fb6e71SRatan Gupta         }
112519fb6e71SRatan Gupta 
112619fb6e71SRatan Gupta         if (maxPasswordLength)
112719fb6e71SRatan Gupta         {
112819fb6e71SRatan Gupta             messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
112919fb6e71SRatan Gupta         }
113019fb6e71SRatan Gupta 
11318a07d286SRatan Gupta         if (ldapObject)
11328a07d286SRatan Gupta         {
11338a07d286SRatan Gupta             handleLDAPPatch(*ldapObject, asyncResp, req, params, "LDAP");
11348a07d286SRatan Gupta         }
11358a07d286SRatan Gupta 
1136eb2bbe56SRatan Gupta         if (activeDirectoryObject)
1137eb2bbe56SRatan Gupta         {
1138eb2bbe56SRatan Gupta             handleLDAPPatch(*activeDirectoryObject, asyncResp, req, params,
1139eb2bbe56SRatan Gupta                             "ActiveDirectory");
1140eb2bbe56SRatan Gupta         }
1141eb2bbe56SRatan Gupta 
11423d958bbcSAppaRao Puli         if (unlockTimeout)
11433d958bbcSAppaRao Puli         {
11443d958bbcSAppaRao Puli             crow::connections::systemBus->async_method_call(
11453d958bbcSAppaRao Puli                 [asyncResp](const boost::system::error_code ec) {
11463d958bbcSAppaRao Puli                     if (ec)
11473d958bbcSAppaRao Puli                     {
11483d958bbcSAppaRao Puli                         messages::internalError(asyncResp->res);
11493d958bbcSAppaRao Puli                         return;
11503d958bbcSAppaRao Puli                     }
1151add6133bSRatan Gupta                     messages::success(asyncResp->res);
11523d958bbcSAppaRao Puli                 },
11533d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
11543d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "Set",
11553d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy",
1156abf2add6SEd Tanous                 "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout));
11573d958bbcSAppaRao Puli         }
11583d958bbcSAppaRao Puli         if (lockoutThreshold)
11593d958bbcSAppaRao Puli         {
11603d958bbcSAppaRao Puli             crow::connections::systemBus->async_method_call(
11613d958bbcSAppaRao Puli                 [asyncResp](const boost::system::error_code ec) {
11623d958bbcSAppaRao Puli                     if (ec)
11633d958bbcSAppaRao Puli                     {
11643d958bbcSAppaRao Puli                         messages::internalError(asyncResp->res);
11653d958bbcSAppaRao Puli                         return;
11663d958bbcSAppaRao Puli                     }
1167add6133bSRatan Gupta                     messages::success(asyncResp->res);
11683d958bbcSAppaRao Puli                 },
11693d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
11703d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "Set",
11713d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy",
11723d958bbcSAppaRao Puli                 "MaxLoginAttemptBeforeLockout",
1173abf2add6SEd Tanous                 std::variant<uint16_t>(*lockoutThreshold));
11743d958bbcSAppaRao Puli         }
117588d16c9aSLewanczyk, Dawid     }
117688d16c9aSLewanczyk, Dawid };
1177f00032dbSTanous 
1178b9b2e0b2SEd Tanous class AccountsCollection : public Node
1179b9b2e0b2SEd Tanous {
1180b9b2e0b2SEd Tanous   public:
1181b9b2e0b2SEd Tanous     AccountsCollection(CrowApp& app) :
1182b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/")
1183b9b2e0b2SEd Tanous     {
1184b9b2e0b2SEd Tanous         entityPrivileges = {
1185b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
1186b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}}},
1187b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1188b9b2e0b2SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
1189b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
1190b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
1191b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
1192b9b2e0b2SEd Tanous     }
1193b9b2e0b2SEd Tanous 
1194b9b2e0b2SEd Tanous   private:
1195b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
1196b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
1197b9b2e0b2SEd Tanous     {
1198b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
11990f74e643SEd Tanous         res.jsonValue = {{"@odata.context",
12000f74e643SEd Tanous                           "/redfish/v1/"
12010f74e643SEd Tanous                           "$metadata#ManagerAccountCollection."
12020f74e643SEd Tanous                           "ManagerAccountCollection"},
12030f74e643SEd Tanous                          {"@odata.id", "/redfish/v1/AccountService/Accounts"},
12040f74e643SEd Tanous                          {"@odata.type", "#ManagerAccountCollection."
12050f74e643SEd Tanous                                          "ManagerAccountCollection"},
12060f74e643SEd Tanous                          {"Name", "Accounts Collection"},
12070f74e643SEd Tanous                          {"Description", "BMC User Accounts"}};
12080f74e643SEd Tanous 
1209b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
1210b9b2e0b2SEd Tanous             [asyncResp](const boost::system::error_code ec,
1211b9b2e0b2SEd Tanous                         const ManagedObjectType& users) {
1212b9b2e0b2SEd Tanous                 if (ec)
1213b9b2e0b2SEd Tanous                 {
1214f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
1215b9b2e0b2SEd Tanous                     return;
1216b9b2e0b2SEd Tanous                 }
1217b9b2e0b2SEd Tanous 
1218b9b2e0b2SEd Tanous                 nlohmann::json& memberArray =
1219b9b2e0b2SEd Tanous                     asyncResp->res.jsonValue["Members"];
1220b9b2e0b2SEd Tanous                 memberArray = nlohmann::json::array();
1221b9b2e0b2SEd Tanous 
1222b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = users.size();
1223b9b2e0b2SEd Tanous                 for (auto& user : users)
1224b9b2e0b2SEd Tanous                 {
1225b9b2e0b2SEd Tanous                     const std::string& path =
1226b9b2e0b2SEd Tanous                         static_cast<const std::string&>(user.first);
1227b9b2e0b2SEd Tanous                     std::size_t lastIndex = path.rfind("/");
1228b9b2e0b2SEd Tanous                     if (lastIndex == std::string::npos)
1229b9b2e0b2SEd Tanous                     {
1230b9b2e0b2SEd Tanous                         lastIndex = 0;
1231b9b2e0b2SEd Tanous                     }
1232b9b2e0b2SEd Tanous                     else
1233b9b2e0b2SEd Tanous                     {
1234b9b2e0b2SEd Tanous                         lastIndex += 1;
1235b9b2e0b2SEd Tanous                     }
1236b9b2e0b2SEd Tanous                     memberArray.push_back(
1237b9b2e0b2SEd Tanous                         {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
1238b9b2e0b2SEd Tanous                                            path.substr(lastIndex)}});
1239b9b2e0b2SEd Tanous                 }
1240b9b2e0b2SEd Tanous             },
1241b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1242b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1243b9b2e0b2SEd Tanous     }
124404ae99ecSEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
124504ae99ecSEd Tanous                 const std::vector<std::string>& params) override
124604ae99ecSEd Tanous     {
124704ae99ecSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
124804ae99ecSEd Tanous 
12499712f8acSEd Tanous         std::string username;
12509712f8acSEd Tanous         std::string password;
1251a24526dcSEd Tanous         std::optional<std::string> roleId("User");
1252a24526dcSEd Tanous         std::optional<bool> enabled = true;
12539712f8acSEd Tanous         if (!json_util::readJson(req, res, "UserName", username, "Password",
12549712f8acSEd Tanous                                  password, "RoleId", roleId, "Enabled",
12559712f8acSEd Tanous                                  enabled))
125604ae99ecSEd Tanous         {
125704ae99ecSEd Tanous             return;
125804ae99ecSEd Tanous         }
125904ae99ecSEd Tanous 
126054fc587aSNagaraju Goruganti         std::string priv = getPrivilegeFromRoleId(*roleId);
126184e12cb7SAppaRao Puli         if (priv.empty())
126204ae99ecSEd Tanous         {
1263f12894f8SJason M. Bills             messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
126404ae99ecSEd Tanous             return;
126504ae99ecSEd Tanous         }
12669712f8acSEd Tanous         roleId = priv;
126704ae99ecSEd Tanous 
1268599c71d8SAyushi Smriti         // Reading AllGroups property
1269599c71d8SAyushi Smriti         crow::connections::systemBus->async_method_call(
1270599c71d8SAyushi Smriti             [asyncResp, username, password{std::move(password)}, roleId,
1271599c71d8SAyushi Smriti              enabled](const boost::system::error_code ec,
1272599c71d8SAyushi Smriti                       const std::variant<std::vector<std::string>>& allGroups) {
1273599c71d8SAyushi Smriti                 if (ec)
1274599c71d8SAyushi Smriti                 {
1275599c71d8SAyushi Smriti                     BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1276599c71d8SAyushi Smriti                     messages::internalError(asyncResp->res);
1277599c71d8SAyushi Smriti                     return;
1278599c71d8SAyushi Smriti                 }
1279599c71d8SAyushi Smriti 
1280599c71d8SAyushi Smriti                 const std::vector<std::string>* allGroupsList =
1281599c71d8SAyushi Smriti                     std::get_if<std::vector<std::string>>(&allGroups);
1282599c71d8SAyushi Smriti 
1283599c71d8SAyushi Smriti                 if (allGroupsList == nullptr || allGroupsList->empty())
1284599c71d8SAyushi Smriti                 {
1285599c71d8SAyushi Smriti                     messages::internalError(asyncResp->res);
1286599c71d8SAyushi Smriti                     return;
1287599c71d8SAyushi Smriti                 }
1288599c71d8SAyushi Smriti 
128904ae99ecSEd Tanous                 crow::connections::systemBus->async_method_call(
12909712f8acSEd Tanous                     [asyncResp, username, password{std::move(password)}](
129104ae99ecSEd Tanous                         const boost::system::error_code ec) {
129204ae99ecSEd Tanous                         if (ec)
129304ae99ecSEd Tanous                         {
129404ae99ecSEd Tanous                             messages::resourceAlreadyExists(
1295599c71d8SAyushi Smriti                                 asyncResp->res,
1296599c71d8SAyushi Smriti                                 "#ManagerAccount.v1_0_3.ManagerAccount",
1297f12894f8SJason M. Bills                                 "UserName", username);
129804ae99ecSEd Tanous                             return;
129904ae99ecSEd Tanous                         }
130004ae99ecSEd Tanous 
130104ae99ecSEd Tanous                         if (!pamUpdatePassword(username, password))
130204ae99ecSEd Tanous                         {
1303599c71d8SAyushi Smriti                             // At this point we have a user that's been created,
1304599c71d8SAyushi Smriti                             // but the password set failed.Something is wrong,
1305599c71d8SAyushi Smriti                             // so delete the user that we've already created
130604ae99ecSEd Tanous                             crow::connections::systemBus->async_method_call(
1307599c71d8SAyushi Smriti                                 [asyncResp](
1308599c71d8SAyushi Smriti                                     const boost::system::error_code ec) {
130904ae99ecSEd Tanous                                     if (ec)
131004ae99ecSEd Tanous                                     {
1311f12894f8SJason M. Bills                                         messages::internalError(asyncResp->res);
131204ae99ecSEd Tanous                                         return;
131304ae99ecSEd Tanous                                     }
131404ae99ecSEd Tanous 
1315599c71d8SAyushi Smriti                                     messages::invalidObject(asyncResp->res,
1316599c71d8SAyushi Smriti                                                             "Password");
131704ae99ecSEd Tanous                                 },
131804ae99ecSEd Tanous                                 "xyz.openbmc_project.User.Manager",
131904ae99ecSEd Tanous                                 "/xyz/openbmc_project/user/" + username,
132004ae99ecSEd Tanous                                 "xyz.openbmc_project.Object.Delete", "Delete");
132104ae99ecSEd Tanous 
132204ae99ecSEd Tanous                             BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
132304ae99ecSEd Tanous                             return;
132404ae99ecSEd Tanous                         }
132504ae99ecSEd Tanous 
1326f12894f8SJason M. Bills                         messages::created(asyncResp->res);
132704ae99ecSEd Tanous                         asyncResp->res.addHeader(
132804ae99ecSEd Tanous                             "Location",
132904ae99ecSEd Tanous                             "/redfish/v1/AccountService/Accounts/" + username);
133004ae99ecSEd Tanous                     },
1331599c71d8SAyushi Smriti                     "xyz.openbmc_project.User.Manager",
1332599c71d8SAyushi Smriti                     "/xyz/openbmc_project/user",
13339712f8acSEd Tanous                     "xyz.openbmc_project.User.Manager", "CreateUser", username,
1334599c71d8SAyushi Smriti                     *allGroupsList, *roleId, *enabled);
1335599c71d8SAyushi Smriti             },
1336599c71d8SAyushi Smriti             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1337599c71d8SAyushi Smriti             "org.freedesktop.DBus.Properties", "Get",
1338599c71d8SAyushi Smriti             "xyz.openbmc_project.User.Manager", "AllGroups");
133904ae99ecSEd Tanous     }
1340b9b2e0b2SEd Tanous };
1341b9b2e0b2SEd Tanous 
1342b9b2e0b2SEd Tanous class ManagerAccount : public Node
1343b9b2e0b2SEd Tanous {
1344b9b2e0b2SEd Tanous   public:
1345b9b2e0b2SEd Tanous     ManagerAccount(CrowApp& app) :
1346b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string())
1347b9b2e0b2SEd Tanous     {
1348b9b2e0b2SEd Tanous         entityPrivileges = {
1349b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
1350b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}},
1351b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1352b9b2e0b2SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
1353b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
1354b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
1355b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
1356b9b2e0b2SEd Tanous     }
1357b9b2e0b2SEd Tanous 
1358b9b2e0b2SEd Tanous   private:
1359b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
1360b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
1361b9b2e0b2SEd Tanous     {
13620f74e643SEd Tanous 
1363b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1364b9b2e0b2SEd Tanous 
1365b9b2e0b2SEd Tanous         if (params.size() != 1)
1366b9b2e0b2SEd Tanous         {
1367f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1368b9b2e0b2SEd Tanous             return;
1369b9b2e0b2SEd Tanous         }
1370b9b2e0b2SEd Tanous 
1371b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
1372b9b2e0b2SEd Tanous             [asyncResp, accountName{std::string(params[0])}](
1373b9b2e0b2SEd Tanous                 const boost::system::error_code ec,
1374b9b2e0b2SEd Tanous                 const ManagedObjectType& users) {
1375b9b2e0b2SEd Tanous                 if (ec)
1376b9b2e0b2SEd Tanous                 {
1377f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
1378b9b2e0b2SEd Tanous                     return;
1379b9b2e0b2SEd Tanous                 }
138084e12cb7SAppaRao Puli                 auto userIt = users.begin();
1381b9b2e0b2SEd Tanous 
138284e12cb7SAppaRao Puli                 for (; userIt != users.end(); userIt++)
1383b9b2e0b2SEd Tanous                 {
138484e12cb7SAppaRao Puli                     if (boost::ends_with(userIt->first.str, "/" + accountName))
1385b9b2e0b2SEd Tanous                     {
138684e12cb7SAppaRao Puli                         break;
1387b9b2e0b2SEd Tanous                     }
1388b9b2e0b2SEd Tanous                 }
138984e12cb7SAppaRao Puli                 if (userIt == users.end())
1390b9b2e0b2SEd Tanous                 {
139184e12cb7SAppaRao Puli                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
139284e12cb7SAppaRao Puli                                                accountName);
139384e12cb7SAppaRao Puli                     return;
139484e12cb7SAppaRao Puli                 }
13954e68c45bSAyushi Smriti 
13964e68c45bSAyushi Smriti                 asyncResp->res.jsonValue = {
13974e68c45bSAyushi Smriti                     {"@odata.context",
13984e68c45bSAyushi Smriti                      "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
13994e68c45bSAyushi Smriti                     {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"},
14004e68c45bSAyushi Smriti                     {"Name", "User Account"},
14014e68c45bSAyushi Smriti                     {"Description", "User Account"},
14024e68c45bSAyushi Smriti                     {"Password", nullptr}};
14034e68c45bSAyushi Smriti 
140484e12cb7SAppaRao Puli                 for (const auto& interface : userIt->second)
140565b0dc32SEd Tanous                 {
140665b0dc32SEd Tanous                     if (interface.first ==
140765b0dc32SEd Tanous                         "xyz.openbmc_project.User.Attributes")
140865b0dc32SEd Tanous                     {
140965b0dc32SEd Tanous                         for (const auto& property : interface.second)
141065b0dc32SEd Tanous                         {
141165b0dc32SEd Tanous                             if (property.first == "UserEnabled")
141265b0dc32SEd Tanous                             {
141365b0dc32SEd Tanous                                 const bool* userEnabled =
1414abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
141565b0dc32SEd Tanous                                 if (userEnabled == nullptr)
141665b0dc32SEd Tanous                                 {
141765b0dc32SEd Tanous                                     BMCWEB_LOG_ERROR
141865b0dc32SEd Tanous                                         << "UserEnabled wasn't a bool";
141984e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
142084e12cb7SAppaRao Puli                                     return;
142165b0dc32SEd Tanous                                 }
142265b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Enabled"] =
142365b0dc32SEd Tanous                                     *userEnabled;
142465b0dc32SEd Tanous                             }
142565b0dc32SEd Tanous                             else if (property.first ==
142665b0dc32SEd Tanous                                      "UserLockedForFailedAttempt")
142765b0dc32SEd Tanous                             {
142865b0dc32SEd Tanous                                 const bool* userLocked =
1429abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
143065b0dc32SEd Tanous                                 if (userLocked == nullptr)
143165b0dc32SEd Tanous                                 {
143284e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "UserLockedForF"
143384e12cb7SAppaRao Puli                                                         "ailedAttempt "
143484e12cb7SAppaRao Puli                                                         "wasn't a bool";
143584e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
143684e12cb7SAppaRao Puli                                     return;
143765b0dc32SEd Tanous                                 }
143865b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Locked"] =
143965b0dc32SEd Tanous                                     *userLocked;
144024c8542dSRatan Gupta                                 asyncResp->res.jsonValue
144124c8542dSRatan Gupta                                     ["Locked@Redfish.AllowableValues"] = {
14424d64ce34SGunnar Mills                                     "false"};
144365b0dc32SEd Tanous                             }
144484e12cb7SAppaRao Puli                             else if (property.first == "UserPrivilege")
144584e12cb7SAppaRao Puli                             {
144654fc587aSNagaraju Goruganti                                 const std::string* userPrivPtr =
1447abf2add6SEd Tanous                                     std::get_if<std::string>(&property.second);
144854fc587aSNagaraju Goruganti                                 if (userPrivPtr == nullptr)
144984e12cb7SAppaRao Puli                                 {
145084e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR
145184e12cb7SAppaRao Puli                                         << "UserPrivilege wasn't a "
145284e12cb7SAppaRao Puli                                            "string";
145384e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
145484e12cb7SAppaRao Puli                                     return;
145584e12cb7SAppaRao Puli                                 }
145654fc587aSNagaraju Goruganti                                 std::string role =
145754fc587aSNagaraju Goruganti                                     getRoleIdFromPrivilege(*userPrivPtr);
145854fc587aSNagaraju Goruganti                                 if (role.empty())
145984e12cb7SAppaRao Puli                                 {
146084e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "Invalid user role";
146184e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
146284e12cb7SAppaRao Puli                                     return;
146384e12cb7SAppaRao Puli                                 }
146454fc587aSNagaraju Goruganti                                 asyncResp->res.jsonValue["RoleId"] = role;
146584e12cb7SAppaRao Puli 
146684e12cb7SAppaRao Puli                                 asyncResp->res.jsonValue["Links"]["Role"] = {
146784e12cb7SAppaRao Puli                                     {"@odata.id", "/redfish/v1/AccountService/"
146884e12cb7SAppaRao Puli                                                   "Roles/" +
146954fc587aSNagaraju Goruganti                                                       role}};
147084e12cb7SAppaRao Puli                             }
147165b0dc32SEd Tanous                         }
147265b0dc32SEd Tanous                     }
147365b0dc32SEd Tanous                 }
147465b0dc32SEd Tanous 
1475b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
147684e12cb7SAppaRao Puli                     "/redfish/v1/AccountService/Accounts/" + accountName;
1477b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Id"] = accountName;
1478b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["UserName"] = accountName;
1479b9b2e0b2SEd Tanous             },
1480b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1481b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1482b9b2e0b2SEd Tanous     }
1483a840879dSEd Tanous 
1484a840879dSEd Tanous     void doPatch(crow::Response& res, const crow::Request& req,
1485a840879dSEd Tanous                  const std::vector<std::string>& params) override
1486a840879dSEd Tanous     {
1487a840879dSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
1488a840879dSEd Tanous         if (params.size() != 1)
1489a840879dSEd Tanous         {
1490f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1491a840879dSEd Tanous             return;
1492a840879dSEd Tanous         }
1493a840879dSEd Tanous 
1494a24526dcSEd Tanous         std::optional<std::string> newUserName;
1495a24526dcSEd Tanous         std::optional<std::string> password;
1496a24526dcSEd Tanous         std::optional<bool> enabled;
1497a24526dcSEd Tanous         std::optional<std::string> roleId;
149824c8542dSRatan Gupta         std::optional<bool> locked;
149984e12cb7SAppaRao Puli         if (!json_util::readJson(req, res, "UserName", newUserName, "Password",
150024c8542dSRatan Gupta                                  password, "RoleId", roleId, "Enabled", enabled,
150124c8542dSRatan Gupta                                  "Locked", locked))
1502a840879dSEd Tanous         {
1503a840879dSEd Tanous             return;
1504a840879dSEd Tanous         }
1505a840879dSEd Tanous 
150684e12cb7SAppaRao Puli         const std::string& username = params[0];
150784e12cb7SAppaRao Puli 
150884e12cb7SAppaRao Puli         if (!newUserName)
1509a840879dSEd Tanous         {
151054fc587aSNagaraju Goruganti             // If the username isn't being updated, we can update the
151154fc587aSNagaraju Goruganti             // properties directly
151224c8542dSRatan Gupta             updateUserProperties(asyncResp, username, password, enabled, roleId,
151324c8542dSRatan Gupta                                  locked);
151484e12cb7SAppaRao Puli             return;
151584e12cb7SAppaRao Puli         }
151684e12cb7SAppaRao Puli         else
151784e12cb7SAppaRao Puli         {
151884e12cb7SAppaRao Puli             crow::connections::systemBus->async_method_call(
151984e12cb7SAppaRao Puli                 [this, asyncResp, username, password(std::move(password)),
152084e12cb7SAppaRao Puli                  roleId(std::move(roleId)), enabled(std::move(enabled)),
1521271584abSEd Tanous                  newUser{*newUserName}, locked(std::move(locked))](
152284e12cb7SAppaRao Puli                     const boost::system::error_code ec) {
152384e12cb7SAppaRao Puli                     if (ec)
152484e12cb7SAppaRao Puli                     {
152584e12cb7SAppaRao Puli                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1526a840879dSEd Tanous                         messages::resourceNotFound(
152784e12cb7SAppaRao Puli                             asyncResp->res,
152884e12cb7SAppaRao Puli                             "#ManagerAccount.v1_0_3.ManagerAccount", username);
1529a840879dSEd Tanous                         return;
1530a840879dSEd Tanous                     }
1531a840879dSEd Tanous 
153284e12cb7SAppaRao Puli                     updateUserProperties(asyncResp, newUser, password, enabled,
153324c8542dSRatan Gupta                                          roleId, locked);
153484e12cb7SAppaRao Puli                 },
153584e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
153684e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "RenameUser", username,
153784e12cb7SAppaRao Puli                 *newUserName);
153884e12cb7SAppaRao Puli         }
153984e12cb7SAppaRao Puli     }
154084e12cb7SAppaRao Puli 
154184e12cb7SAppaRao Puli     void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp,
154284e12cb7SAppaRao Puli                               const std::string& username,
1543a24526dcSEd Tanous                               std::optional<std::string> password,
1544a24526dcSEd Tanous                               std::optional<bool> enabled,
154524c8542dSRatan Gupta                               std::optional<std::string> roleId,
154624c8542dSRatan Gupta                               std::optional<bool> locked)
154784e12cb7SAppaRao Puli     {
15489712f8acSEd Tanous         if (password)
1549a840879dSEd Tanous         {
15509712f8acSEd Tanous             if (!pamUpdatePassword(username, *password))
1551a840879dSEd Tanous             {
1552a840879dSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
1553f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1554a840879dSEd Tanous                 return;
1555a840879dSEd Tanous             }
1556a840879dSEd Tanous         }
1557a840879dSEd Tanous 
155824c8542dSRatan Gupta         std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username;
155924c8542dSRatan Gupta         dbus::utility::escapePathForDbus(dbusObjectPath);
156024c8542dSRatan Gupta 
156122c33710SRatan Gupta         dbus::utility::checkDbusPathExists(
156224c8542dSRatan Gupta             dbusObjectPath,
156324c8542dSRatan Gupta             [dbusObjectPath(std::move(dbusObjectPath)), username,
156424c8542dSRatan Gupta              password(std::move(password)), roleId(std::move(roleId)),
156524c8542dSRatan Gupta              enabled(std::move(enabled)), locked(std::move(locked)),
156624c8542dSRatan Gupta              asyncResp{std::move(asyncResp)}](int rc) {
156724c8542dSRatan Gupta                 if (!rc)
156824c8542dSRatan Gupta                 {
156924c8542dSRatan Gupta                     messages::invalidObject(asyncResp->res, username.c_str());
157024c8542dSRatan Gupta                     return;
157124c8542dSRatan Gupta                 }
15729712f8acSEd Tanous                 if (enabled)
1573a840879dSEd Tanous                 {
1574a840879dSEd Tanous                     crow::connections::systemBus->async_method_call(
1575a840879dSEd Tanous                         [asyncResp](const boost::system::error_code ec) {
1576a840879dSEd Tanous                             if (ec)
1577a840879dSEd Tanous                             {
157824c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
157924c8542dSRatan Gupta                                                  << ec;
1580f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
1581a840879dSEd Tanous                                 return;
1582a840879dSEd Tanous                             }
158384e12cb7SAppaRao Puli                             messages::success(asyncResp->res);
158484e12cb7SAppaRao Puli                             return;
158584e12cb7SAppaRao Puli                         },
158684e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Manager",
158724c8542dSRatan Gupta                         dbusObjectPath.c_str(),
158884e12cb7SAppaRao Puli                         "org.freedesktop.DBus.Properties", "Set",
158984e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserEnabled",
1590abf2add6SEd Tanous                         std::variant<bool>{*enabled});
159184e12cb7SAppaRao Puli                 }
159284e12cb7SAppaRao Puli 
159384e12cb7SAppaRao Puli                 if (roleId)
159484e12cb7SAppaRao Puli                 {
159554fc587aSNagaraju Goruganti                     std::string priv = getPrivilegeFromRoleId(*roleId);
159684e12cb7SAppaRao Puli                     if (priv.empty())
159784e12cb7SAppaRao Puli                     {
159824c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res,
159924c8542dSRatan Gupta                                                          *roleId, "RoleId");
160084e12cb7SAppaRao Puli                         return;
160184e12cb7SAppaRao Puli                     }
160284e12cb7SAppaRao Puli 
160384e12cb7SAppaRao Puli                     crow::connections::systemBus->async_method_call(
160484e12cb7SAppaRao Puli                         [asyncResp](const boost::system::error_code ec) {
160584e12cb7SAppaRao Puli                             if (ec)
160684e12cb7SAppaRao Puli                             {
160724c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
160824c8542dSRatan Gupta                                                  << ec;
160984e12cb7SAppaRao Puli                                 messages::internalError(asyncResp->res);
161084e12cb7SAppaRao Puli                                 return;
161184e12cb7SAppaRao Puli                             }
1612f12894f8SJason M. Bills                             messages::success(asyncResp->res);
1613a840879dSEd Tanous                         },
1614a840879dSEd Tanous                         "xyz.openbmc_project.User.Manager",
161524c8542dSRatan Gupta                         dbusObjectPath.c_str(),
1616a840879dSEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
161784e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1618abf2add6SEd Tanous                         std::variant<std::string>{priv});
1619a840879dSEd Tanous                 }
162024c8542dSRatan Gupta 
162124c8542dSRatan Gupta                 if (locked)
162224c8542dSRatan Gupta                 {
162324c8542dSRatan Gupta                     // admin can unlock the account which is locked by
162454fc587aSNagaraju Goruganti                     // successive authentication failures but admin should
162554fc587aSNagaraju Goruganti                     // not be allowed to lock an account.
162624c8542dSRatan Gupta                     if (*locked)
162724c8542dSRatan Gupta                     {
162824c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res, "true",
162924c8542dSRatan Gupta                                                          "Locked");
163024c8542dSRatan Gupta                         return;
163124c8542dSRatan Gupta                     }
163224c8542dSRatan Gupta 
163324c8542dSRatan Gupta                     crow::connections::systemBus->async_method_call(
163424c8542dSRatan Gupta                         [asyncResp](const boost::system::error_code ec) {
163524c8542dSRatan Gupta                             if (ec)
163624c8542dSRatan Gupta                             {
163724c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
163824c8542dSRatan Gupta                                                  << ec;
163924c8542dSRatan Gupta                                 messages::internalError(asyncResp->res);
164024c8542dSRatan Gupta                                 return;
164124c8542dSRatan Gupta                             }
164224c8542dSRatan Gupta                             messages::success(asyncResp->res);
164324c8542dSRatan Gupta                             return;
164424c8542dSRatan Gupta                         },
164524c8542dSRatan Gupta                         "xyz.openbmc_project.User.Manager",
164624c8542dSRatan Gupta                         dbusObjectPath.c_str(),
164724c8542dSRatan Gupta                         "org.freedesktop.DBus.Properties", "Set",
164824c8542dSRatan Gupta                         "xyz.openbmc_project.User.Attributes",
164924c8542dSRatan Gupta                         "UserLockedForFailedAttempt",
165024c8542dSRatan Gupta                         sdbusplus::message::variant<bool>{*locked});
165124c8542dSRatan Gupta                 }
165224c8542dSRatan Gupta             });
1653a840879dSEd Tanous     }
165406e086d9SEd Tanous 
165506e086d9SEd Tanous     void doDelete(crow::Response& res, const crow::Request& req,
165606e086d9SEd Tanous                   const std::vector<std::string>& params) override
165706e086d9SEd Tanous     {
165806e086d9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
165906e086d9SEd Tanous 
166006e086d9SEd Tanous         if (params.size() != 1)
166106e086d9SEd Tanous         {
1662f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
166306e086d9SEd Tanous             return;
166406e086d9SEd Tanous         }
166506e086d9SEd Tanous 
166606e086d9SEd Tanous         const std::string userPath = "/xyz/openbmc_project/user/" + params[0];
166706e086d9SEd Tanous 
166806e086d9SEd Tanous         crow::connections::systemBus->async_method_call(
166906e086d9SEd Tanous             [asyncResp, username{std::move(params[0])}](
167006e086d9SEd Tanous                 const boost::system::error_code ec) {
167106e086d9SEd Tanous                 if (ec)
167206e086d9SEd Tanous                 {
167306e086d9SEd Tanous                     messages::resourceNotFound(
1674f12894f8SJason M. Bills                         asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
1675f12894f8SJason M. Bills                         username);
167606e086d9SEd Tanous                     return;
167706e086d9SEd Tanous                 }
167806e086d9SEd Tanous 
1679f12894f8SJason M. Bills                 messages::accountRemoved(asyncResp->res);
168006e086d9SEd Tanous             },
168106e086d9SEd Tanous             "xyz.openbmc_project.User.Manager", userPath,
168206e086d9SEd Tanous             "xyz.openbmc_project.Object.Delete", "Delete");
168306e086d9SEd Tanous     }
168484e12cb7SAppaRao Puli };
168588d16c9aSLewanczyk, Dawid 
168688d16c9aSLewanczyk, Dawid } // namespace redfish
1687