xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 2b73119c57d054d1a0d67b376ae5651fccfae5ba)
188d16c9aSLewanczyk, Dawid /*
288d16c9aSLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
388d16c9aSLewanczyk, Dawid //
488d16c9aSLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
588d16c9aSLewanczyk, Dawid // you may not use this file except in compliance with the License.
688d16c9aSLewanczyk, Dawid // You may obtain a copy of the License at
788d16c9aSLewanczyk, Dawid //
888d16c9aSLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
988d16c9aSLewanczyk, Dawid //
1088d16c9aSLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1188d16c9aSLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1288d16c9aSLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388d16c9aSLewanczyk, Dawid // See the License for the specific language governing permissions and
1488d16c9aSLewanczyk, Dawid // limitations under the License.
1588d16c9aSLewanczyk, Dawid */
1688d16c9aSLewanczyk, Dawid #pragma once
1788d16c9aSLewanczyk, Dawid 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_utility.hpp"
203ccb3adbSEd Tanous #include "error_messages.hpp"
210ec8b83dSEd Tanous #include "generated/enums/account_service.hpp"
223ccb3adbSEd Tanous #include "openbmc_dbus_rest.hpp"
233ccb3adbSEd Tanous #include "persistent_data.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
250ec8b83dSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
280ec8b83dSEd Tanous 
291e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
30d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
311214b7e7SGunnar Mills 
32*2b73119cSGeorge Liu #include <array>
33c7229815SAbhishek Patel #include <optional>
34c7229815SAbhishek Patel #include <string>
35*2b73119cSGeorge Liu #include <string_view>
36c7229815SAbhishek Patel #include <vector>
37c7229815SAbhishek Patel 
381abe55efSEd Tanous namespace redfish
391abe55efSEd Tanous {
4088d16c9aSLewanczyk, Dawid 
4123a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
426973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
432c70f800SEd Tanous constexpr const char* adConfigObject =
44ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
45ab828d7cSRatan Gupta 
46b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
476973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
486973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
496973a582SRatan Gupta constexpr const char* ldapConfigInterface =
506973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
516973a582SRatan Gupta constexpr const char* ldapCreateInterface =
526973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
536973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
5406785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
5506785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
566973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
576973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
586973a582SRatan Gupta constexpr const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
596973a582SRatan Gupta constexpr const char* mapperObjectPath = "/xyz/openbmc_project/object_mapper";
606973a582SRatan Gupta constexpr const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
616973a582SRatan Gupta 
6254fc587aSNagaraju Goruganti struct LDAPRoleMapData
6354fc587aSNagaraju Goruganti {
6454fc587aSNagaraju Goruganti     std::string groupName;
6554fc587aSNagaraju Goruganti     std::string privilege;
6654fc587aSNagaraju Goruganti };
6754fc587aSNagaraju Goruganti 
686973a582SRatan Gupta struct LDAPConfigData
696973a582SRatan Gupta {
706973a582SRatan Gupta     std::string uri{};
716973a582SRatan Gupta     std::string bindDN{};
726973a582SRatan Gupta     std::string baseDN{};
736973a582SRatan Gupta     std::string searchScope{};
746973a582SRatan Gupta     std::string serverType{};
756973a582SRatan Gupta     bool serviceEnabled = false;
766973a582SRatan Gupta     std::string userNameAttribute{};
776973a582SRatan Gupta     std::string groupAttribute{};
7854fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
796973a582SRatan Gupta };
806973a582SRatan Gupta 
8154fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
8284e12cb7SAppaRao Puli {
8384e12cb7SAppaRao Puli     if (role == "priv-admin")
8484e12cb7SAppaRao Puli     {
8584e12cb7SAppaRao Puli         return "Administrator";
8684e12cb7SAppaRao Puli     }
873174e4dfSEd Tanous     if (role == "priv-user")
8884e12cb7SAppaRao Puli     {
89c80fee55SAppaRao Puli         return "ReadOnly";
9084e12cb7SAppaRao Puli     }
913174e4dfSEd Tanous     if (role == "priv-operator")
9284e12cb7SAppaRao Puli     {
9384e12cb7SAppaRao Puli         return "Operator";
9484e12cb7SAppaRao Puli     }
9584e12cb7SAppaRao Puli     return "";
9684e12cb7SAppaRao Puli }
9754fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9884e12cb7SAppaRao Puli {
9984e12cb7SAppaRao Puli     if (role == "Administrator")
10084e12cb7SAppaRao Puli     {
10184e12cb7SAppaRao Puli         return "priv-admin";
10284e12cb7SAppaRao Puli     }
1033174e4dfSEd Tanous     if (role == "ReadOnly")
10484e12cb7SAppaRao Puli     {
10584e12cb7SAppaRao Puli         return "priv-user";
10684e12cb7SAppaRao Puli     }
1073174e4dfSEd Tanous     if (role == "Operator")
10884e12cb7SAppaRao Puli     {
10984e12cb7SAppaRao Puli         return "priv-operator";
11084e12cb7SAppaRao Puli     }
11184e12cb7SAppaRao Puli     return "";
11284e12cb7SAppaRao Puli }
113b9b2e0b2SEd Tanous 
114c7229815SAbhishek Patel /**
115c7229815SAbhishek Patel  * @brief Maps user group names retrieved from D-Bus object to
116c7229815SAbhishek Patel  * Account Types.
117c7229815SAbhishek Patel  *
118c7229815SAbhishek Patel  * @param[in] userGroups List of User groups
119c7229815SAbhishek Patel  * @param[out] res AccountTypes populated
120c7229815SAbhishek Patel  *
121c7229815SAbhishek Patel  * @return true in case of success, false if UserGroups contains
122c7229815SAbhishek Patel  * invalid group name(s).
123c7229815SAbhishek Patel  */
124c7229815SAbhishek Patel inline bool translateUserGroup(const std::vector<std::string>& userGroups,
125c7229815SAbhishek Patel                                crow::Response& res)
126c7229815SAbhishek Patel {
127c7229815SAbhishek Patel     std::vector<std::string> accountTypes;
128c7229815SAbhishek Patel     for (const auto& userGroup : userGroups)
129c7229815SAbhishek Patel     {
130c7229815SAbhishek Patel         if (userGroup == "redfish")
131c7229815SAbhishek Patel         {
132c7229815SAbhishek Patel             accountTypes.emplace_back("Redfish");
133c7229815SAbhishek Patel             accountTypes.emplace_back("WebUI");
134c7229815SAbhishek Patel         }
135c7229815SAbhishek Patel         else if (userGroup == "ipmi")
136c7229815SAbhishek Patel         {
137c7229815SAbhishek Patel             accountTypes.emplace_back("IPMI");
138c7229815SAbhishek Patel         }
139c7229815SAbhishek Patel         else if (userGroup == "ssh")
140c7229815SAbhishek Patel         {
141c7229815SAbhishek Patel             accountTypes.emplace_back("HostConsole");
142c7229815SAbhishek Patel             accountTypes.emplace_back("ManagerConsole");
143c7229815SAbhishek Patel         }
144c7229815SAbhishek Patel         else if (userGroup == "web")
145c7229815SAbhishek Patel         {
146c7229815SAbhishek Patel             // 'web' is one of the valid groups in the UserGroups property of
147c7229815SAbhishek Patel             // the user account in the D-Bus object. This group is currently not
148c7229815SAbhishek Patel             // doing anything, and is considered to be equivalent to 'redfish'.
149c7229815SAbhishek Patel             // 'redfish' user group is mapped to 'Redfish'and 'WebUI'
150c7229815SAbhishek Patel             // AccountTypes, so do nothing here...
151c7229815SAbhishek Patel         }
152c7229815SAbhishek Patel         else
153c7229815SAbhishek Patel         {
154c7229815SAbhishek Patel             // Invalid user group name. Caller throws an excption.
155c7229815SAbhishek Patel             return false;
156c7229815SAbhishek Patel         }
157c7229815SAbhishek Patel     }
158c7229815SAbhishek Patel 
159c7229815SAbhishek Patel     res.jsonValue["AccountTypes"] = std::move(accountTypes);
160c7229815SAbhishek Patel     return true;
161c7229815SAbhishek Patel }
162c7229815SAbhishek Patel 
1638d1b46d7Szhanghch05 inline void userErrorMessageHandler(
1648d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1658d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
16666b5ca76Sjayaprakash Mutyala {
16766b5ca76Sjayaprakash Mutyala     if (e == nullptr)
16866b5ca76Sjayaprakash Mutyala     {
16966b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
17066b5ca76Sjayaprakash Mutyala         return;
17166b5ca76Sjayaprakash Mutyala     }
17266b5ca76Sjayaprakash Mutyala 
173055806b3SManojkiran Eda     const char* errorMessage = e->name;
17466b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
17566b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
17666b5ca76Sjayaprakash Mutyala     {
177d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
17866b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
17966b5ca76Sjayaprakash Mutyala     }
18066b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
18166b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
18266b5ca76Sjayaprakash Mutyala     {
183d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
18466b5ca76Sjayaprakash Mutyala     }
185d4d25793SEd Tanous     else if ((strcmp(errorMessage,
186d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
187d4d25793SEd Tanous               0) ||
1880fda0f12SGeorge Liu              (strcmp(
1890fda0f12SGeorge Liu                   errorMessage,
1900fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
1910fda0f12SGeorge Liu               0))
19266b5ca76Sjayaprakash Mutyala     {
19366b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
19466b5ca76Sjayaprakash Mutyala     }
19566b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
19666b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
19766b5ca76Sjayaprakash Mutyala     {
19866b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
19966b5ca76Sjayaprakash Mutyala     }
20066b5ca76Sjayaprakash Mutyala     else
20166b5ca76Sjayaprakash Mutyala     {
20266b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
20366b5ca76Sjayaprakash Mutyala     }
20466b5ca76Sjayaprakash Mutyala }
20566b5ca76Sjayaprakash Mutyala 
20681ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
207ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
208ab828d7cSRatan Gupta                                 const std::string& ldapType)
2096973a582SRatan Gupta {
210ab828d7cSRatan Gupta     std::string service =
211ab828d7cSRatan Gupta         (ldapType == "LDAP") ? "LDAPService" : "ActiveDirectoryService";
21254fc587aSNagaraju Goruganti 
2131476687dSEd Tanous     nlohmann::json& ldap = jsonResponse[ldapType];
21454fc587aSNagaraju Goruganti 
2151476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
2161476687dSEd Tanous     ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri});
2170ec8b83dSEd Tanous     ldap["Authentication"]["AuthenticationType"] =
2180ec8b83dSEd Tanous         account_service::AuthenticationTypes::UsernameAndPassword;
2191476687dSEd Tanous     ldap["Authentication"]["Username"] = confData.bindDN;
2201476687dSEd Tanous     ldap["Authentication"]["Password"] = nullptr;
2211476687dSEd Tanous 
2221476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] =
2231476687dSEd Tanous         nlohmann::json::array({confData.baseDN});
2241476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] =
2251476687dSEd Tanous         confData.userNameAttribute;
2261476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] =
2271476687dSEd Tanous         confData.groupAttribute;
2281476687dSEd Tanous 
2291476687dSEd Tanous     nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"];
23054fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
2319eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
23254fc587aSNagaraju Goruganti     {
23354fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
23454fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
235613dabeaSEd Tanous 
236613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
237613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
238329f0348SJorge Cisneros         remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
239329f0348SJorge Cisneros         roleMapArray.emplace_back(std::move(remoteGroup));
24054fc587aSNagaraju Goruganti     }
2416973a582SRatan Gupta }
2426973a582SRatan Gupta 
2436973a582SRatan Gupta /**
24406785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
24506785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
24606785244SRatan Gupta  *
24706785244SRatan Gupta  */
24823a21a1cSEd Tanous inline void handleRoleMapPatch(
2498d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25006785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
251f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
25206785244SRatan Gupta {
25306785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
25406785244SRatan Gupta     {
255f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
25606785244SRatan Gupta 
25706785244SRatan Gupta         if (thisJson.is_null())
25806785244SRatan Gupta         {
25906785244SRatan Gupta             // delete the existing object
26006785244SRatan Gupta             if (index < roleMapObjData.size())
26106785244SRatan Gupta             {
26206785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
26306785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
26406785244SRatan Gupta                      index](const boost::system::error_code ec) {
26506785244SRatan Gupta                     if (ec)
26606785244SRatan Gupta                     {
26706785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
26806785244SRatan Gupta                         messages::internalError(asyncResp->res);
26906785244SRatan Gupta                         return;
27006785244SRatan Gupta                     }
27106785244SRatan Gupta                     asyncResp->res
27206785244SRatan Gupta                         .jsonValue[serverType]["RemoteRoleMapping"][index] =
27306785244SRatan Gupta                         nullptr;
27406785244SRatan Gupta                     },
27506785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
27606785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
27706785244SRatan Gupta             }
27806785244SRatan Gupta             else
27906785244SRatan Gupta             {
28006785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
28106785244SRatan Gupta                 messages::propertyValueTypeError(
28271f52d96SEd Tanous                     asyncResp->res,
28371f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
28471f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
28506785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
28606785244SRatan Gupta                 return;
28706785244SRatan Gupta             }
28806785244SRatan Gupta         }
28906785244SRatan Gupta         else if (thisJson.empty())
29006785244SRatan Gupta         {
29106785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
29206785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
29306785244SRatan Gupta         }
29406785244SRatan Gupta         else
29506785244SRatan Gupta         {
29606785244SRatan Gupta             // update/create the object
29706785244SRatan Gupta             std::optional<std::string> remoteGroup;
29806785244SRatan Gupta             std::optional<std::string> localRole;
29906785244SRatan Gupta 
300f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
301f23b7296SEd Tanous             // readJson is structured
302f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
303f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
304f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
305f23b7296SEd Tanous                                      localRole))
30606785244SRatan Gupta             {
30706785244SRatan Gupta                 continue;
30806785244SRatan Gupta             }
30906785244SRatan Gupta 
31006785244SRatan Gupta             // Update existing RoleMapping Object
31106785244SRatan Gupta             if (index < roleMapObjData.size())
31206785244SRatan Gupta             {
31306785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
31406785244SRatan Gupta                 // If "RemoteGroup" info is provided
31506785244SRatan Gupta                 if (remoteGroup)
31606785244SRatan Gupta                 {
31706785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
31806785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
31906785244SRatan Gupta                          remoteGroup](const boost::system::error_code ec) {
32006785244SRatan Gupta                         if (ec)
32106785244SRatan Gupta                         {
322002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
32306785244SRatan Gupta                             messages::internalError(asyncResp->res);
32406785244SRatan Gupta                             return;
32506785244SRatan Gupta                         }
32606785244SRatan Gupta                         asyncResp->res
327002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
328002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
32906785244SRatan Gupta                         },
33006785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
33106785244SRatan Gupta                         propertyInterface, "Set",
33206785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
33306785244SRatan Gupta                         "GroupName",
334168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
335168e20c1SEd Tanous                             std::move(*remoteGroup)));
33606785244SRatan Gupta                 }
33706785244SRatan Gupta 
33806785244SRatan Gupta                 // If "LocalRole" info is provided
33906785244SRatan Gupta                 if (localRole)
34006785244SRatan Gupta                 {
34106785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
34206785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
34306785244SRatan Gupta                          localRole](const boost::system::error_code ec) {
34406785244SRatan Gupta                         if (ec)
34506785244SRatan Gupta                         {
346002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
34706785244SRatan Gupta                             messages::internalError(asyncResp->res);
34806785244SRatan Gupta                             return;
34906785244SRatan Gupta                         }
35006785244SRatan Gupta                         asyncResp->res
351002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
352002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
35306785244SRatan Gupta                         },
35406785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
35506785244SRatan Gupta                         propertyInterface, "Set",
35606785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
35706785244SRatan Gupta                         "Privilege",
358168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
35906785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
36006785244SRatan Gupta                 }
36106785244SRatan Gupta             }
36206785244SRatan Gupta             // Create a new RoleMapping Object.
36306785244SRatan Gupta             else
36406785244SRatan Gupta             {
36506785244SRatan Gupta                 BMCWEB_LOG_DEBUG
36606785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
36706785244SRatan Gupta                 std::string pathString =
36806785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index);
36906785244SRatan Gupta 
37006785244SRatan Gupta                 if (!localRole)
37106785244SRatan Gupta                 {
37206785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
37306785244SRatan Gupta                                               pathString + "/LocalRole");
37406785244SRatan Gupta                     continue;
37506785244SRatan Gupta                 }
37606785244SRatan Gupta                 if (!remoteGroup)
37706785244SRatan Gupta                 {
37806785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
37906785244SRatan Gupta                                               pathString + "/RemoteGroup");
38006785244SRatan Gupta                     continue;
38106785244SRatan Gupta                 }
38206785244SRatan Gupta 
38306785244SRatan Gupta                 std::string dbusObjectPath;
38406785244SRatan Gupta                 if (serverType == "ActiveDirectory")
38506785244SRatan Gupta                 {
3862c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
38706785244SRatan Gupta                 }
38806785244SRatan Gupta                 else if (serverType == "LDAP")
38906785244SRatan Gupta                 {
39023a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
39106785244SRatan Gupta                 }
39206785244SRatan Gupta 
39306785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
39406785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
39506785244SRatan Gupta 
39606785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
397271584abSEd Tanous                     [asyncResp, serverType, localRole,
39806785244SRatan Gupta                      remoteGroup](const boost::system::error_code ec) {
39906785244SRatan Gupta                     if (ec)
40006785244SRatan Gupta                     {
40106785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
40206785244SRatan Gupta                         messages::internalError(asyncResp->res);
40306785244SRatan Gupta                         return;
40406785244SRatan Gupta                     }
40506785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
40606785244SRatan Gupta                         asyncResp->res
40706785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
4081476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
4091476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
4101476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
4111476687dSEd Tanous                     remoteRoleJson.push_back(std::move(roleMapEntry));
41206785244SRatan Gupta                     },
41306785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
4143174e4dfSEd Tanous                     "Create", *remoteGroup,
41506785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
41606785244SRatan Gupta             }
41706785244SRatan Gupta         }
41806785244SRatan Gupta     }
41906785244SRatan Gupta }
42006785244SRatan Gupta 
42106785244SRatan Gupta /**
4226973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
4236973a582SRatan Gupta  * into JSON
4246973a582SRatan Gupta  */
4256973a582SRatan Gupta template <typename CallbackFunc>
4266973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
4276973a582SRatan Gupta                               CallbackFunc&& callback)
4286973a582SRatan Gupta {
42954fc587aSNagaraju Goruganti 
430*2b73119cSGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
431*2b73119cSGeorge Liu         ldapEnableInterface, ldapConfigInterface};
43254fc587aSNagaraju Goruganti 
433*2b73119cSGeorge Liu     dbus::utility::getDbusObject(
434*2b73119cSGeorge Liu         ldapConfigObjectName, interfaces,
435*2b73119cSGeorge Liu         [callback, ldapType](const boost::system::error_code& ec,
436b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
43754fc587aSNagaraju Goruganti         if (ec || resp.empty())
43854fc587aSNagaraju Goruganti         {
4390fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
440002d39b4SEd Tanous                 << "DBUS response error during getting of service name: " << ec;
44123a21a1cSEd Tanous             LDAPConfigData empty{};
44223a21a1cSEd Tanous             callback(false, empty, ldapType);
44354fc587aSNagaraju Goruganti             return;
44454fc587aSNagaraju Goruganti         }
44554fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
44654fc587aSNagaraju Goruganti         crow::connections::systemBus->async_method_call(
447002d39b4SEd Tanous             [callback,
448002d39b4SEd Tanous              ldapType](const boost::system::error_code errorCode,
449711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
4506973a582SRatan Gupta             LDAPConfigData confData{};
45181ce609eSEd Tanous             if (errorCode)
4526973a582SRatan Gupta             {
453ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
454002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode;
4556973a582SRatan Gupta                 return;
4566973a582SRatan Gupta             }
457ab828d7cSRatan Gupta 
458ab828d7cSRatan Gupta             std::string ldapDbusType;
45954fc587aSNagaraju Goruganti             std::string searchString;
46054fc587aSNagaraju Goruganti 
461ab828d7cSRatan Gupta             if (ldapType == "LDAP")
462ab828d7cSRatan Gupta             {
4630fda0f12SGeorge Liu                 ldapDbusType =
4640fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
46554fc587aSNagaraju Goruganti                 searchString = "openldap";
466ab828d7cSRatan Gupta             }
467ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
468ab828d7cSRatan Gupta             {
46954fc587aSNagaraju Goruganti                 ldapDbusType =
4700fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
47154fc587aSNagaraju Goruganti                 searchString = "active_directory";
472ab828d7cSRatan Gupta             }
473ab828d7cSRatan Gupta             else
474ab828d7cSRatan Gupta             {
475002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type="
476ab828d7cSRatan Gupta                                  << ldapType;
477ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
478ab828d7cSRatan Gupta                 return;
479ab828d7cSRatan Gupta             }
480ab828d7cSRatan Gupta 
481ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
482ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
483ab828d7cSRatan Gupta 
4846973a582SRatan Gupta             for (const auto& object : ldapObjects)
4856973a582SRatan Gupta             {
48654fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
48754fc587aSNagaraju Goruganti                 // given type
488002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
4896973a582SRatan Gupta                 {
490ab828d7cSRatan Gupta                     continue;
491ab828d7cSRatan Gupta                 }
492ab828d7cSRatan Gupta 
4936973a582SRatan Gupta                 for (const auto& interface : object.second)
4946973a582SRatan Gupta                 {
4956973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
4966973a582SRatan Gupta                     {
4976973a582SRatan Gupta                         // rest of the properties are string.
4986973a582SRatan Gupta                         for (const auto& property : interface.second)
4996973a582SRatan Gupta                         {
5006973a582SRatan Gupta                             if (property.first == "Enabled")
5016973a582SRatan Gupta                             {
5026973a582SRatan Gupta                                 const bool* value =
5036973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
5046973a582SRatan Gupta                                 if (value == nullptr)
5056973a582SRatan Gupta                                 {
5066973a582SRatan Gupta                                     continue;
5076973a582SRatan Gupta                                 }
5086973a582SRatan Gupta                                 confData.serviceEnabled = *value;
5096973a582SRatan Gupta                                 break;
5106973a582SRatan Gupta                             }
5116973a582SRatan Gupta                         }
5126973a582SRatan Gupta                     }
5136973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
5146973a582SRatan Gupta                     {
5156973a582SRatan Gupta 
5166973a582SRatan Gupta                         for (const auto& property : interface.second)
5176973a582SRatan Gupta                         {
518271584abSEd Tanous                             const std::string* strValue =
519002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
520271584abSEd Tanous                             if (strValue == nullptr)
5216973a582SRatan Gupta                             {
5226973a582SRatan Gupta                                 continue;
5236973a582SRatan Gupta                             }
5246973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
5256973a582SRatan Gupta                             {
526271584abSEd Tanous                                 confData.uri = *strValue;
5276973a582SRatan Gupta                             }
5286973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
5296973a582SRatan Gupta                             {
530271584abSEd Tanous                                 confData.bindDN = *strValue;
5316973a582SRatan Gupta                             }
5326973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
5336973a582SRatan Gupta                             {
534271584abSEd Tanous                                 confData.baseDN = *strValue;
5356973a582SRatan Gupta                             }
536002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
5376973a582SRatan Gupta                             {
538271584abSEd Tanous                                 confData.searchScope = *strValue;
5396973a582SRatan Gupta                             }
540002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
5416973a582SRatan Gupta                             {
542271584abSEd Tanous                                 confData.groupAttribute = *strValue;
5436973a582SRatan Gupta                             }
544002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
5456973a582SRatan Gupta                             {
546271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
5476973a582SRatan Gupta                             }
54854fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
549ab828d7cSRatan Gupta                             {
550271584abSEd Tanous                                 confData.serverType = *strValue;
55154fc587aSNagaraju Goruganti                             }
55254fc587aSNagaraju Goruganti                         }
55354fc587aSNagaraju Goruganti                     }
554002d39b4SEd Tanous                     else if (interface.first ==
5550fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
55654fc587aSNagaraju Goruganti                     {
55754fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
55854fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
55954fc587aSNagaraju Goruganti                         {
560271584abSEd Tanous                             const std::string* strValue =
561002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
56254fc587aSNagaraju Goruganti 
563271584abSEd Tanous                             if (strValue == nullptr)
56454fc587aSNagaraju Goruganti                             {
56554fc587aSNagaraju Goruganti                                 continue;
56654fc587aSNagaraju Goruganti                             }
56754fc587aSNagaraju Goruganti 
56854fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
56954fc587aSNagaraju Goruganti                             {
570271584abSEd Tanous                                 roleMapData.groupName = *strValue;
57154fc587aSNagaraju Goruganti                             }
57254fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
57354fc587aSNagaraju Goruganti                             {
574271584abSEd Tanous                                 roleMapData.privilege = *strValue;
57554fc587aSNagaraju Goruganti                             }
57654fc587aSNagaraju Goruganti                         }
57754fc587aSNagaraju Goruganti 
578002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
579002d39b4SEd Tanous                                                             roleMapData);
58054fc587aSNagaraju Goruganti                     }
58154fc587aSNagaraju Goruganti                 }
58254fc587aSNagaraju Goruganti             }
583ab828d7cSRatan Gupta             callback(true, confData, ldapType);
58454fc587aSNagaraju Goruganti             },
585002d39b4SEd Tanous             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
586*2b73119cSGeorge Liu         });
5876973a582SRatan Gupta }
5886973a582SRatan Gupta 
5898a07d286SRatan Gupta /**
5908a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
5918a07d286SRatan Gupta  * @param input JSON data
5928a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5938a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
5948a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
5958a07d286SRatan Gupta  */
5964f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
5976c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5986c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
5998a07d286SRatan Gupta {
6008a07d286SRatan Gupta     std::optional<std::string> authType;
6018a07d286SRatan Gupta 
6028a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
6038a07d286SRatan Gupta                              authType, "Username", username, "Password",
6048a07d286SRatan Gupta                              password))
6058a07d286SRatan Gupta     {
6068a07d286SRatan Gupta         return;
6078a07d286SRatan Gupta     }
6088a07d286SRatan Gupta     if (!authType)
6098a07d286SRatan Gupta     {
6108a07d286SRatan Gupta         return;
6118a07d286SRatan Gupta     }
6128a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
6138a07d286SRatan Gupta     {
6148a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
6158a07d286SRatan Gupta                                          "AuthenticationType");
6168a07d286SRatan Gupta         return;
6178a07d286SRatan Gupta     }
6188a07d286SRatan Gupta }
6198a07d286SRatan Gupta /**
6208a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
6218a07d286SRatan Gupta  * @param input JSON data
6228a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6238a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
6248a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
6258a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
6268a07d286SRatan Gupta  */
6278a07d286SRatan Gupta 
6284f48d5f6SEd Tanous inline void
6294f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
6308d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6318a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
6328a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
6338a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
6348a07d286SRatan Gupta {
6358a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
6368a07d286SRatan Gupta 
6378a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
6388a07d286SRatan Gupta                              searchSettings))
6398a07d286SRatan Gupta     {
6408a07d286SRatan Gupta         return;
6418a07d286SRatan Gupta     }
6428a07d286SRatan Gupta     if (!searchSettings)
6438a07d286SRatan Gupta     {
6448a07d286SRatan Gupta         return;
6458a07d286SRatan Gupta     }
6468a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
6478a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
6488a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
6498a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
6508a07d286SRatan Gupta     {
6518a07d286SRatan Gupta         return;
6528a07d286SRatan Gupta     }
6538a07d286SRatan Gupta }
6548a07d286SRatan Gupta /**
6558a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6568a07d286SRatan Gupta           json response with the new value.
6578a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6588a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6598a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6608a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6618a07d286SRatan Gupta  */
6628a07d286SRatan Gupta 
6634f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6648a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6658d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6668a07d286SRatan Gupta     const std::string& ldapServerElementName,
6678a07d286SRatan Gupta     const std::string& ldapConfigObject)
6688a07d286SRatan Gupta {
6698a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6708a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
6718a07d286SRatan Gupta          serviceAddressList](const boost::system::error_code ec) {
6728a07d286SRatan Gupta         if (ec)
6738a07d286SRatan Gupta         {
6748a07d286SRatan Gupta             BMCWEB_LOG_DEBUG
675c61704abSGunnar Mills                 << "Error Occurred in updating the service address";
6768a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6778a07d286SRatan Gupta             return;
6788a07d286SRatan Gupta         }
6798a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
6808a07d286SRatan Gupta             serviceAddressList.front()};
681002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
6828a07d286SRatan Gupta             modifiedserviceAddressList;
6838a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
6848a07d286SRatan Gupta         {
685002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
6868a07d286SRatan Gupta                                             serviceAddressList.front());
6878a07d286SRatan Gupta         }
6888a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
6898a07d286SRatan Gupta         },
6908a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6918a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
692168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
6938a07d286SRatan Gupta }
6948a07d286SRatan Gupta /**
6958a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
6968a07d286SRatan Gupta           json response with the new value.
6978a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
6988a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6998a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7008a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7018a07d286SRatan Gupta  */
7028a07d286SRatan Gupta 
7034f48d5f6SEd Tanous inline void
7044f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
7058d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::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, username,
7118a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7128a07d286SRatan Gupta         if (ec)
7138a07d286SRatan Gupta         {
7146c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
7158a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7168a07d286SRatan Gupta             return;
7178a07d286SRatan Gupta         }
718002d39b4SEd Tanous         asyncResp->res
719002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Username"] =
720002d39b4SEd Tanous             username;
7218a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
7228a07d286SRatan Gupta         },
7238a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
724168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
725168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
7268a07d286SRatan Gupta }
7278a07d286SRatan Gupta 
7288a07d286SRatan Gupta /**
7298a07d286SRatan Gupta  * @brief updates the LDAP password
7308a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
7318a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7328a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7338a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
7348a07d286SRatan Gupta  */
7358a07d286SRatan Gupta 
7364f48d5f6SEd Tanous inline void
7374f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
7388d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7398a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7408a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7418a07d286SRatan Gupta {
7428a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7438a07d286SRatan Gupta         [asyncResp, password,
7448a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7458a07d286SRatan Gupta         if (ec)
7468a07d286SRatan Gupta         {
7476c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
7488a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7498a07d286SRatan Gupta             return;
7508a07d286SRatan Gupta         }
751002d39b4SEd Tanous         asyncResp->res
752002d39b4SEd Tanous             .jsonValue[ldapServerElementName]["Authentication"]["Password"] =
753002d39b4SEd Tanous             "";
7548a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
7558a07d286SRatan Gupta         },
7568a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7578a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
758168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
7598a07d286SRatan Gupta }
7608a07d286SRatan Gupta 
7618a07d286SRatan Gupta /**
7628a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7638a07d286SRatan Gupta           json response with the new value.
7648a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7658a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7668a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7678a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7688a07d286SRatan Gupta  */
7698a07d286SRatan Gupta 
7704f48d5f6SEd Tanous inline void
7714f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7728d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7738a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7748a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7758a07d286SRatan Gupta {
7768a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7778a07d286SRatan Gupta         [asyncResp, baseDNList,
7788a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
7798a07d286SRatan Gupta         if (ec)
7808a07d286SRatan Gupta         {
7816c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
7828a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7838a07d286SRatan Gupta             return;
7848a07d286SRatan Gupta         }
785002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7868a07d286SRatan Gupta         auto& searchSettingsJson =
7878a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7886c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
7896c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
7908a07d286SRatan Gupta         if (baseDNList.size() > 1)
7918a07d286SRatan Gupta         {
792002d39b4SEd Tanous             messages::propertyValueModified(
793002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
7948a07d286SRatan Gupta         }
7958a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
7968a07d286SRatan Gupta         },
7978a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7988a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
799168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
8008a07d286SRatan Gupta }
8018a07d286SRatan Gupta /**
8028a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
8038a07d286SRatan Gupta           json response with the new value.
8048a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
8058a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8068a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8078a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8088a07d286SRatan Gupta  */
8098a07d286SRatan Gupta 
8104f48d5f6SEd Tanous inline void
8114f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
8128d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8138a07d286SRatan Gupta                             const std::string& ldapServerElementName,
8148a07d286SRatan Gupta                             const std::string& ldapConfigObject)
8158a07d286SRatan Gupta {
8168a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8178a07d286SRatan Gupta         [asyncResp, userNameAttribute,
8188a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8198a07d286SRatan Gupta         if (ec)
8208a07d286SRatan Gupta         {
821c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8228a07d286SRatan Gupta                                 "username attribute";
8238a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8248a07d286SRatan Gupta             return;
8258a07d286SRatan Gupta         }
826002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8278a07d286SRatan Gupta         auto& searchSettingsJson =
8288a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8298a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
8308a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
8318a07d286SRatan Gupta         },
8328a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8338a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
834168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
8358a07d286SRatan Gupta }
8368a07d286SRatan Gupta /**
8378a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
8388a07d286SRatan Gupta           json response with the new value.
8398a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
8408a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8418a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8428a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8438a07d286SRatan Gupta  */
8448a07d286SRatan Gupta 
8454f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
8468d1b46d7Szhanghch05     const std::string& groupsAttribute,
8478d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8488a07d286SRatan Gupta     const std::string& ldapServerElementName,
8498a07d286SRatan Gupta     const std::string& ldapConfigObject)
8508a07d286SRatan Gupta {
8518a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8528a07d286SRatan Gupta         [asyncResp, groupsAttribute,
8538a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8548a07d286SRatan Gupta         if (ec)
8558a07d286SRatan Gupta         {
856c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8578a07d286SRatan Gupta                                 "groupname attribute";
8588a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8598a07d286SRatan Gupta             return;
8608a07d286SRatan Gupta         }
861002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8628a07d286SRatan Gupta         auto& searchSettingsJson =
8638a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8648a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8658a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8668a07d286SRatan Gupta         },
8678a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8688a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
869168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
8708a07d286SRatan Gupta }
8718a07d286SRatan Gupta /**
8728a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
8738a07d286SRatan Gupta           json response with the new value.
8748a07d286SRatan Gupta  * @param input JSON data.
8758a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8768a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8778a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8788a07d286SRatan Gupta  */
8798a07d286SRatan Gupta 
8804f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8816c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8828a07d286SRatan Gupta     const std::string& ldapServerElementName,
8838a07d286SRatan Gupta     const std::string& ldapConfigObject)
8848a07d286SRatan Gupta {
8858a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8868a07d286SRatan Gupta         [asyncResp, serviceEnabled,
8878a07d286SRatan Gupta          ldapServerElementName](const boost::system::error_code ec) {
8888a07d286SRatan Gupta         if (ec)
8898a07d286SRatan Gupta         {
890002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
8918a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8928a07d286SRatan Gupta             return;
8938a07d286SRatan Gupta         }
8946c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
8958a07d286SRatan Gupta             serviceEnabled;
8966c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
8978a07d286SRatan Gupta         },
8988a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
899168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
900168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
9018a07d286SRatan Gupta }
9028a07d286SRatan Gupta 
9034f48d5f6SEd Tanous inline void
9044f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
9058d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
90678158631SZbigniew Kurzynski {
90778158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
90878158631SZbigniew Kurzynski     std::optional<bool> cookie;
90978158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
91078158631SZbigniew Kurzynski     std::optional<bool> xToken;
911501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
91278158631SZbigniew Kurzynski 
91378158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
91478158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
915501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
91678158631SZbigniew Kurzynski     {
91778158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
91878158631SZbigniew Kurzynski         return;
91978158631SZbigniew Kurzynski     }
92078158631SZbigniew Kurzynski 
92178158631SZbigniew Kurzynski     // Make a copy of methods configuration
92252cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
92352cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
92478158631SZbigniew Kurzynski 
92578158631SZbigniew Kurzynski     if (basicAuth)
92678158631SZbigniew Kurzynski     {
927f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
928f16f6263SAlan Kuo         messages::actionNotSupported(
9290fda0f12SGeorge Liu             asyncResp->res,
9300fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
931f16f6263SAlan Kuo         return;
932f16f6263SAlan Kuo #endif
93378158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
93478158631SZbigniew Kurzynski     }
93578158631SZbigniew Kurzynski 
93678158631SZbigniew Kurzynski     if (cookie)
93778158631SZbigniew Kurzynski     {
938f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
9390fda0f12SGeorge Liu         messages::actionNotSupported(
9400fda0f12SGeorge Liu             asyncResp->res,
9410fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
942f16f6263SAlan Kuo         return;
943f16f6263SAlan Kuo #endif
94478158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
94578158631SZbigniew Kurzynski     }
94678158631SZbigniew Kurzynski 
94778158631SZbigniew Kurzynski     if (sessionToken)
94878158631SZbigniew Kurzynski     {
949f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
950f16f6263SAlan Kuo         messages::actionNotSupported(
9510fda0f12SGeorge Liu             asyncResp->res,
9520fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
953f16f6263SAlan Kuo         return;
954f16f6263SAlan Kuo #endif
95578158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
95678158631SZbigniew Kurzynski     }
95778158631SZbigniew Kurzynski 
95878158631SZbigniew Kurzynski     if (xToken)
95978158631SZbigniew Kurzynski     {
960f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
9610fda0f12SGeorge Liu         messages::actionNotSupported(
9620fda0f12SGeorge Liu             asyncResp->res,
9630fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
964f16f6263SAlan Kuo         return;
965f16f6263SAlan Kuo #endif
96678158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
96778158631SZbigniew Kurzynski     }
96878158631SZbigniew Kurzynski 
969501f1e58SZbigniew Kurzynski     if (tls)
970501f1e58SZbigniew Kurzynski     {
971f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
9720fda0f12SGeorge Liu         messages::actionNotSupported(
9730fda0f12SGeorge Liu             asyncResp->res,
9740fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
975f16f6263SAlan Kuo         return;
976f16f6263SAlan Kuo #endif
977501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
978501f1e58SZbigniew Kurzynski     }
979501f1e58SZbigniew Kurzynski 
98078158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
981501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
982501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
98378158631SZbigniew Kurzynski     {
98478158631SZbigniew Kurzynski         // Do not allow user to disable everything
98578158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
98678158631SZbigniew Kurzynski                                      "of disabling all available methods");
98778158631SZbigniew Kurzynski         return;
98878158631SZbigniew Kurzynski     }
98978158631SZbigniew Kurzynski 
99052cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
99152cc112dSEd Tanous         authMethodsConfig);
99278158631SZbigniew Kurzynski     // Save configuration immediately
99352cc112dSEd Tanous     persistent_data::getConfig().writeData();
99478158631SZbigniew Kurzynski 
99578158631SZbigniew Kurzynski     messages::success(asyncResp->res);
99678158631SZbigniew Kurzynski }
99778158631SZbigniew Kurzynski 
9988a07d286SRatan Gupta /**
9998a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
10008a07d286SRatan Gupta  *        value and create the LDAP config object.
10018a07d286SRatan Gupta  * @param input JSON data
10028a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
10038a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
10048a07d286SRatan Gupta  */
10058a07d286SRatan Gupta 
10066c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
10078d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10088a07d286SRatan Gupta                             const std::string& serverType)
10098a07d286SRatan Gupta {
1010eb2bbe56SRatan Gupta     std::string dbusObjectPath;
1011eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
1012eb2bbe56SRatan Gupta     {
10132c70f800SEd Tanous         dbusObjectPath = adConfigObject;
1014eb2bbe56SRatan Gupta     }
1015eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
1016eb2bbe56SRatan Gupta     {
101723a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
1018eb2bbe56SRatan Gupta     }
1019cb13a392SEd Tanous     else
1020cb13a392SEd Tanous     {
1021cb13a392SEd Tanous         return;
1022cb13a392SEd Tanous     }
1023eb2bbe56SRatan Gupta 
10248a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
10258a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
10268a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
10278a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
10288a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
10298a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
10308a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
10318a07d286SRatan Gupta     std::optional<std::string> userName;
10328a07d286SRatan Gupta     std::optional<std::string> password;
103306785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
10348a07d286SRatan Gupta 
10358a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
10368a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
10378a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
103806785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
103906785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
10408a07d286SRatan Gupta     {
10418a07d286SRatan Gupta         return;
10428a07d286SRatan Gupta     }
10438a07d286SRatan Gupta 
10448a07d286SRatan Gupta     if (authentication)
10458a07d286SRatan Gupta     {
10468a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
10478a07d286SRatan Gupta                                     password);
10488a07d286SRatan Gupta     }
10498a07d286SRatan Gupta     if (ldapService)
10508a07d286SRatan Gupta     {
10518a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10528a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
10538a07d286SRatan Gupta     }
10548a07d286SRatan Gupta     if (serviceAddressList)
10558a07d286SRatan Gupta     {
105626f6976fSEd Tanous         if (serviceAddressList->empty())
10578a07d286SRatan Gupta         {
10588a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10598a07d286SRatan Gupta                                              "ServiceAddress");
10608a07d286SRatan Gupta             return;
10618a07d286SRatan Gupta         }
10628a07d286SRatan Gupta     }
10638a07d286SRatan Gupta     if (baseDNList)
10648a07d286SRatan Gupta     {
106526f6976fSEd Tanous         if (baseDNList->empty())
10668a07d286SRatan Gupta         {
10678a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10688a07d286SRatan Gupta                                              "BaseDistinguishedNames");
10698a07d286SRatan Gupta             return;
10708a07d286SRatan Gupta         }
10718a07d286SRatan Gupta     }
10728a07d286SRatan Gupta 
10738a07d286SRatan Gupta     // nothing to update, then return
10748a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
107506785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
107606785244SRatan Gupta         !remoteRoleMapData)
10778a07d286SRatan Gupta     {
10788a07d286SRatan Gupta         return;
10798a07d286SRatan Gupta     }
10808a07d286SRatan Gupta 
10818a07d286SRatan Gupta     // Get the existing resource first then keep modifying
10828a07d286SRatan Gupta     // whenever any property gets updated.
1083002d39b4SEd Tanous     getLDAPConfigData(
1084002d39b4SEd Tanous         serverType,
1085002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1086002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1087002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
108823a21a1cSEd Tanous                             const std::string& serverT) {
10898a07d286SRatan Gupta         if (!success)
10908a07d286SRatan Gupta         {
10918a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10928a07d286SRatan Gupta             return;
10938a07d286SRatan Gupta         }
10946c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
10958a07d286SRatan Gupta         if (confData.serviceEnabled)
10968a07d286SRatan Gupta         {
10978a07d286SRatan Gupta             // Disable the service first and update the rest of
10988a07d286SRatan Gupta             // the properties.
10996c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
11008a07d286SRatan Gupta         }
11018a07d286SRatan Gupta 
11028a07d286SRatan Gupta         if (serviceAddressList)
11038a07d286SRatan Gupta         {
11046c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
11056c51eab1SEd Tanous                                       dbusObjectPath);
11068a07d286SRatan Gupta         }
11078a07d286SRatan Gupta         if (userName)
11088a07d286SRatan Gupta         {
11096c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
11108a07d286SRatan Gupta         }
11118a07d286SRatan Gupta         if (password)
11128a07d286SRatan Gupta         {
11136c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
11148a07d286SRatan Gupta         }
11158a07d286SRatan Gupta 
11168a07d286SRatan Gupta         if (baseDNList)
11178a07d286SRatan Gupta         {
11186c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
11198a07d286SRatan Gupta         }
11208a07d286SRatan Gupta         if (userNameAttribute)
11218a07d286SRatan Gupta         {
11226c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
11236c51eab1SEd Tanous                                     dbusObjectPath);
11248a07d286SRatan Gupta         }
11258a07d286SRatan Gupta         if (groupsAttribute)
11268a07d286SRatan Gupta         {
11276c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
11286c51eab1SEd Tanous                                      dbusObjectPath);
11298a07d286SRatan Gupta         }
11308a07d286SRatan Gupta         if (serviceEnabled)
11318a07d286SRatan Gupta         {
11328a07d286SRatan Gupta             // if user has given the value as true then enable
11338a07d286SRatan Gupta             // the service. if user has given false then no-op
11348a07d286SRatan Gupta             // as service is already stopped.
11358a07d286SRatan Gupta             if (*serviceEnabled)
11368a07d286SRatan Gupta             {
11376c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
11386c51eab1SEd Tanous                                          dbusObjectPath);
11398a07d286SRatan Gupta             }
11408a07d286SRatan Gupta         }
11418a07d286SRatan Gupta         else
11428a07d286SRatan Gupta         {
11438a07d286SRatan Gupta             // if user has not given the service enabled value
11448a07d286SRatan Gupta             // then revert it to the same state as it was
11458a07d286SRatan Gupta             // before.
11468a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
114723a21a1cSEd Tanous                                      serverT, dbusObjectPath);
11488a07d286SRatan Gupta         }
114906785244SRatan Gupta 
115006785244SRatan Gupta         if (remoteRoleMapData)
115106785244SRatan Gupta         {
11526c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
11536c51eab1SEd Tanous                                *remoteRoleMapData);
115406785244SRatan Gupta         }
11558a07d286SRatan Gupta         });
11568a07d286SRatan Gupta }
1157d4b5443fSEd Tanous 
11586c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
11596c51eab1SEd Tanous                                  const std::string& username,
1160618c14b4SEd Tanous                                  const std::optional<std::string>& password,
1161618c14b4SEd Tanous                                  const std::optional<bool>& enabled,
1162618c14b4SEd Tanous                                  const std::optional<std::string>& roleId,
1163618c14b4SEd Tanous                                  const std::optional<bool>& locked)
11641abe55efSEd Tanous {
1165b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1166b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1167b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
11686c51eab1SEd Tanous 
11696c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1170618c14b4SEd Tanous         dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
1171618c14b4SEd Tanous                          locked, asyncResp{std::move(asyncResp)}](int rc) {
1172e662eae8SEd Tanous             if (rc <= 0)
11736c51eab1SEd Tanous             {
1174d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11756c51eab1SEd Tanous                                            username);
11766c51eab1SEd Tanous                 return;
11776c51eab1SEd Tanous             }
11786c51eab1SEd Tanous 
11796c51eab1SEd Tanous             if (password)
11806c51eab1SEd Tanous             {
11816c51eab1SEd Tanous                 int retval = pamUpdatePassword(username, *password);
11826c51eab1SEd Tanous 
11836c51eab1SEd Tanous                 if (retval == PAM_USER_UNKNOWN)
11846c51eab1SEd Tanous                 {
1185d8a5d5d8SJiaqing Zhao                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11866c51eab1SEd Tanous                                                username);
11876c51eab1SEd Tanous                 }
11886c51eab1SEd Tanous                 else if (retval == PAM_AUTHTOK_ERR)
11896c51eab1SEd Tanous                 {
11906c51eab1SEd Tanous                     // If password is invalid
1191618c14b4SEd Tanous                     messages::propertyValueFormatError(asyncResp->res,
1192618c14b4SEd Tanous                                                        *password, "Password");
11936c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
11946c51eab1SEd Tanous                 }
11956c51eab1SEd Tanous                 else if (retval != PAM_SUCCESS)
11966c51eab1SEd Tanous                 {
11976c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11986c51eab1SEd Tanous                     return;
11996c51eab1SEd Tanous                 }
1200e7b1b62bSEd Tanous                 else
1201e7b1b62bSEd Tanous                 {
1202e7b1b62bSEd Tanous                     messages::success(asyncResp->res);
1203e7b1b62bSEd Tanous                 }
12046c51eab1SEd Tanous             }
12056c51eab1SEd Tanous 
12066c51eab1SEd Tanous             if (enabled)
12076c51eab1SEd Tanous             {
12086c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12096c51eab1SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
12106c51eab1SEd Tanous                 if (ec)
12116c51eab1SEd Tanous                 {
12126c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12136c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12146c51eab1SEd Tanous                     return;
12156c51eab1SEd Tanous                 }
12166c51eab1SEd Tanous                 messages::success(asyncResp->res);
12176c51eab1SEd Tanous                 return;
12186c51eab1SEd Tanous                     },
1219e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12206c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12216c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserEnabled",
1222168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*enabled});
12236c51eab1SEd Tanous             }
12246c51eab1SEd Tanous 
12256c51eab1SEd Tanous             if (roleId)
12266c51eab1SEd Tanous             {
12276c51eab1SEd Tanous                 std::string priv = getPrivilegeFromRoleId(*roleId);
12286c51eab1SEd Tanous                 if (priv.empty())
12296c51eab1SEd Tanous                 {
12306c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, *roleId,
12316c51eab1SEd Tanous                                                      "RoleId");
12326c51eab1SEd Tanous                     return;
12336c51eab1SEd Tanous                 }
12346c51eab1SEd Tanous 
12356c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12366c51eab1SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
12376c51eab1SEd Tanous                 if (ec)
12386c51eab1SEd Tanous                 {
12396c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12406c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12416c51eab1SEd Tanous                     return;
12426c51eab1SEd Tanous                 }
12436c51eab1SEd Tanous                 messages::success(asyncResp->res);
12446c51eab1SEd Tanous                     },
1245e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12466c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12476c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1248168e20c1SEd Tanous                     dbus::utility::DbusVariantType{priv});
12496c51eab1SEd Tanous             }
12506c51eab1SEd Tanous 
12516c51eab1SEd Tanous             if (locked)
12526c51eab1SEd Tanous             {
12536c51eab1SEd Tanous                 // admin can unlock the account which is locked by
12546c51eab1SEd Tanous                 // successive authentication failures but admin should
12556c51eab1SEd Tanous                 // not be allowed to lock an account.
12566c51eab1SEd Tanous                 if (*locked)
12576c51eab1SEd Tanous                 {
12586c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, "true",
12596c51eab1SEd Tanous                                                      "Locked");
12606c51eab1SEd Tanous                     return;
12616c51eab1SEd Tanous                 }
12626c51eab1SEd Tanous 
12636c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12646c51eab1SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
12656c51eab1SEd Tanous                 if (ec)
12666c51eab1SEd Tanous                 {
12676c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12686c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12696c51eab1SEd Tanous                     return;
12706c51eab1SEd Tanous                 }
12716c51eab1SEd Tanous                 messages::success(asyncResp->res);
12726c51eab1SEd Tanous                 return;
12736c51eab1SEd Tanous                     },
1274e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12756c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12766c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes",
1277168e20c1SEd Tanous                     "UserLockedForFailedAttempt",
1278168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*locked});
12796c51eab1SEd Tanous             }
12806c51eab1SEd Tanous         });
12816c51eab1SEd Tanous }
12826c51eab1SEd Tanous 
12834c7d4d33SEd Tanous inline void handleAccountServiceHead(
12844c7d4d33SEd Tanous     App& app, const crow::Request& req,
12851ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12866c51eab1SEd Tanous {
12874c7d4d33SEd Tanous 
12883ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
128945ca1b86SEd Tanous     {
129045ca1b86SEd Tanous         return;
129145ca1b86SEd Tanous     }
12924c7d4d33SEd Tanous     asyncResp->res.addHeader(
12934c7d4d33SEd Tanous         boost::beast::http::field::link,
12944c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
12954c7d4d33SEd Tanous }
12964c7d4d33SEd Tanous 
12974c7d4d33SEd Tanous inline void
12984c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
12994c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13004c7d4d33SEd Tanous {
13014c7d4d33SEd Tanous     handleAccountServiceHead(app, req, asyncResp);
130252cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
13031ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
130478158631SZbigniew Kurzynski 
13051476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
13061476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
13071476687dSEd Tanous     json["@odata.type"] = "#AccountService."
13081476687dSEd Tanous                           "v1_10_0.AccountService";
13091476687dSEd Tanous     json["Id"] = "AccountService";
13101476687dSEd Tanous     json["Name"] = "Account Service";
13111476687dSEd Tanous     json["Description"] = "Account Service";
13121476687dSEd Tanous     json["ServiceEnabled"] = true;
13131476687dSEd Tanous     json["MaxPasswordLength"] = 20;
13141ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
13151476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
13161476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
13171476687dSEd Tanous         "#OemAccountService.v1_0_0.AccountService";
13181476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
13191476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
13201476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
13211476687dSEd Tanous         authMethodsConfig.basic;
13221476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
13231476687dSEd Tanous         authMethodsConfig.sessionToken;
13241ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
13251ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
13261ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
13271476687dSEd Tanous 
13281ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
13291ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
13301ef4c342SEd Tanous     // permissions ConfigureManager
133172048780SAbhishek Patel     Privileges effectiveUserPrivileges =
133272048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
133372048780SAbhishek Patel 
133472048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
133572048780SAbhishek Patel                                          effectiveUserPrivileges))
133672048780SAbhishek Patel     {
13371ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
13381476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
133972048780SAbhishek Patel     }
1340d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1341d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1342d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
1343002d39b4SEd Tanous         [asyncResp](const boost::system::error_code ec,
13441ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
13453d958bbcSAppaRao Puli         if (ec)
13463d958bbcSAppaRao Puli         {
13473d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
13483d958bbcSAppaRao Puli             return;
13493d958bbcSAppaRao Puli         }
1350d1bde9e5SKrzysztof Grobelny 
13513d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
13523d958bbcSAppaRao Puli                          << "properties for AccountService";
1353d1bde9e5SKrzysztof Grobelny 
1354d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1355d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1356d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1357d1bde9e5SKrzysztof Grobelny 
1358d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1359d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1360d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1361d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1362d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1363d1bde9e5SKrzysztof Grobelny 
1364d1bde9e5SKrzysztof Grobelny         if (!success)
13653d958bbcSAppaRao Puli         {
1366d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1367d1bde9e5SKrzysztof Grobelny             return;
13683d958bbcSAppaRao Puli         }
1369d1bde9e5SKrzysztof Grobelny 
1370d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
13713d958bbcSAppaRao Puli         {
1372d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
13733d958bbcSAppaRao Puli         }
1374d1bde9e5SKrzysztof Grobelny 
1375d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
13763d958bbcSAppaRao Puli         {
1377d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1378d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1379d1bde9e5SKrzysztof Grobelny         }
1380d1bde9e5SKrzysztof Grobelny 
1381d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
13823d958bbcSAppaRao Puli         {
1383002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1384d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
13853d958bbcSAppaRao Puli         }
1386d1bde9e5SKrzysztof Grobelny         });
13876973a582SRatan Gupta 
138802cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1389ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1390cb13a392SEd Tanous         if (!success)
1391cb13a392SEd Tanous         {
1392cb13a392SEd Tanous             return;
1393cb13a392SEd Tanous         }
1394002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1395ab828d7cSRatan Gupta     };
1396ab828d7cSRatan Gupta 
1397ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1398ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
13991ef4c342SEd Tanous }
14006973a582SRatan Gupta 
14011ef4c342SEd Tanous inline void handleAccountServicePatch(
14021ef4c342SEd Tanous     App& app, const crow::Request& req,
14031ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14041ef4c342SEd Tanous {
14053ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
140645ca1b86SEd Tanous     {
140745ca1b86SEd Tanous         return;
140845ca1b86SEd Tanous     }
1409f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1410f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1411ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1412f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1413f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1414f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1415f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1416f5ffd806SEd Tanous 
141715ed6780SWilly Tu     if (!json_util::readJsonPatch(
14181ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
14191ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
14201ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
14211ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1422f5ffd806SEd Tanous             oemObject))
1423f5ffd806SEd Tanous     {
1424f5ffd806SEd Tanous         return;
1425f5ffd806SEd Tanous     }
1426f5ffd806SEd Tanous 
1427f5ffd806SEd Tanous     if (minPasswordLength)
1428f5ffd806SEd Tanous     {
1429ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
1430ef73ad0dSPaul Fertser             [asyncResp](const boost::system::error_code ec) {
1431ef73ad0dSPaul Fertser             if (ec)
1432ef73ad0dSPaul Fertser             {
1433ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1434ef73ad0dSPaul Fertser                 return;
1435ef73ad0dSPaul Fertser             }
1436ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1437ef73ad0dSPaul Fertser             },
14381ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1439ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
14401ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1441ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1442f5ffd806SEd Tanous     }
1443f5ffd806SEd Tanous 
1444f5ffd806SEd Tanous     if (maxPasswordLength)
1445f5ffd806SEd Tanous     {
14461ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1447f5ffd806SEd Tanous     }
1448f5ffd806SEd Tanous 
1449f5ffd806SEd Tanous     if (ldapObject)
1450f5ffd806SEd Tanous     {
1451f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1452f5ffd806SEd Tanous     }
1453f5ffd806SEd Tanous 
1454f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
14551ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1456f5ffd806SEd Tanous                                          oemOpenBMCObject))
1457f5ffd806SEd Tanous     {
1458f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1459f5ffd806SEd Tanous             oemOpenBMCObject &&
1460f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1461f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1462f5ffd806SEd Tanous         {
1463f5ffd806SEd Tanous             if (authMethodsObject)
1464f5ffd806SEd Tanous             {
14651ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1466f5ffd806SEd Tanous             }
1467f5ffd806SEd Tanous         }
1468f5ffd806SEd Tanous     }
1469f5ffd806SEd Tanous 
1470f5ffd806SEd Tanous     if (activeDirectoryObject)
1471f5ffd806SEd Tanous     {
14721ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1473f5ffd806SEd Tanous     }
1474f5ffd806SEd Tanous 
1475f5ffd806SEd Tanous     if (unlockTimeout)
1476f5ffd806SEd Tanous     {
1477f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1478f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1479f5ffd806SEd Tanous             if (ec)
1480f5ffd806SEd Tanous             {
1481f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1482f5ffd806SEd Tanous                 return;
1483f5ffd806SEd Tanous             }
1484f5ffd806SEd Tanous             messages::success(asyncResp->res);
1485f5ffd806SEd Tanous             },
14861ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1487f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
14881ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1489168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1490f5ffd806SEd Tanous     }
1491f5ffd806SEd Tanous     if (lockoutThreshold)
1492f5ffd806SEd Tanous     {
1493f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
1494f5ffd806SEd Tanous             [asyncResp](const boost::system::error_code ec) {
1495f5ffd806SEd Tanous             if (ec)
1496f5ffd806SEd Tanous             {
1497f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1498f5ffd806SEd Tanous                 return;
1499f5ffd806SEd Tanous             }
1500f5ffd806SEd Tanous             messages::success(asyncResp->res);
1501f5ffd806SEd Tanous             },
15021ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1503f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1504f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1505f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1506168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1507f5ffd806SEd Tanous     }
15081ef4c342SEd Tanous }
1509f5ffd806SEd Tanous 
15104c7d4d33SEd Tanous inline void handleAccountCollectionHead(
15111ef4c342SEd Tanous     App& app, const crow::Request& req,
15121ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15131ef4c342SEd Tanous {
15144c7d4d33SEd Tanous 
15153ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
151645ca1b86SEd Tanous     {
151745ca1b86SEd Tanous         return;
151845ca1b86SEd Tanous     }
15194c7d4d33SEd Tanous     asyncResp->res.addHeader(
15204c7d4d33SEd Tanous         boost::beast::http::field::link,
15214c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
15224c7d4d33SEd Tanous }
15234c7d4d33SEd Tanous 
15244c7d4d33SEd Tanous inline void handleAccountCollectionGet(
15254c7d4d33SEd Tanous     App& app, const crow::Request& req,
15264c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15274c7d4d33SEd Tanous {
15284c7d4d33SEd Tanous     handleAccountCollectionHead(app, req, asyncResp);
15291476687dSEd Tanous 
15301476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
15311476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
15321ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
15331476687dSEd Tanous                                               "ManagerAccountCollection";
15341476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
15351476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
15360f74e643SEd Tanous 
15376c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
15386c51eab1SEd Tanous         redfish::getUserPrivileges(req.userRole);
15396c51eab1SEd Tanous 
1540f5e29f33SJunLin Chen     std::string thisUser;
1541f5e29f33SJunLin Chen     if (req.session)
1542f5e29f33SJunLin Chen     {
1543f5e29f33SJunLin Chen         thisUser = req.session->username;
1544f5e29f33SJunLin Chen     }
1545b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
1546cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
1547cef1ddfbSEd Tanous             const boost::system::error_code ec,
1548711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1549b9b2e0b2SEd Tanous         if (ec)
1550b9b2e0b2SEd Tanous         {
1551f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1552b9b2e0b2SEd Tanous             return;
1553b9b2e0b2SEd Tanous         }
1554b9b2e0b2SEd Tanous 
1555cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1556002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1557cef1ddfbSEd Tanous 
1558cef1ddfbSEd Tanous         bool userCanSeeSelf =
1559002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1560cef1ddfbSEd Tanous 
1561002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1562b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1563b9b2e0b2SEd Tanous 
15649eb808c1SEd Tanous         for (const auto& userpath : users)
1565b9b2e0b2SEd Tanous         {
15662dfd18efSEd Tanous             std::string user = userpath.first.filename();
15672dfd18efSEd Tanous             if (user.empty())
1568b9b2e0b2SEd Tanous             {
15692dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
15702dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
15712dfd18efSEd Tanous 
15722dfd18efSEd Tanous                 return;
1573b9b2e0b2SEd Tanous             }
1574f365910cSGunnar Mills 
1575f365910cSGunnar Mills             // As clarified by Redfish here:
1576f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
15776c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
15786c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
15796c51eab1SEd Tanous             // accounts.
15801ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1581f365910cSGunnar Mills             {
15821476687dSEd Tanous                 nlohmann::json::object_t member;
15831476687dSEd Tanous                 member["@odata.id"] =
1584002d39b4SEd Tanous                     "/redfish/v1/AccountService/Accounts/" + user;
15851476687dSEd Tanous                 memberArray.push_back(std::move(member));
1586b9b2e0b2SEd Tanous             }
1587f365910cSGunnar Mills         }
15881ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1589b9b2e0b2SEd Tanous         },
15901ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1591b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15921ef4c342SEd Tanous }
15936c51eab1SEd Tanous 
15941ef4c342SEd Tanous inline void handleAccountCollectionPost(
15951ef4c342SEd Tanous     App& app, const crow::Request& req,
15961ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15971ef4c342SEd Tanous {
15983ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
159945ca1b86SEd Tanous     {
160045ca1b86SEd Tanous         return;
160145ca1b86SEd Tanous     }
16029712f8acSEd Tanous     std::string username;
16039712f8acSEd Tanous     std::string password;
1604a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1605a24526dcSEd Tanous     std::optional<bool> enabled = true;
16061ef4c342SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
16071ef4c342SEd Tanous                                   "Password", password, "RoleId", roleId,
16081ef4c342SEd Tanous                                   "Enabled", enabled))
160904ae99ecSEd Tanous     {
161004ae99ecSEd Tanous         return;
161104ae99ecSEd Tanous     }
161204ae99ecSEd Tanous 
161354fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
161484e12cb7SAppaRao Puli     if (priv.empty())
161504ae99ecSEd Tanous     {
16161ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
161704ae99ecSEd Tanous         return;
161804ae99ecSEd Tanous     }
16199712f8acSEd Tanous     roleId = priv;
162004ae99ecSEd Tanous 
1621599c71d8SAyushi Smriti     // Reading AllGroups property
16221e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
16231ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
16241ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
16251ef4c342SEd Tanous         "AllGroups",
1626599c71d8SAyushi Smriti         [asyncResp, username, password{std::move(password)}, roleId,
1627168e20c1SEd Tanous          enabled](const boost::system::error_code ec,
16281e1e598dSJonathan Doman                   const std::vector<std::string>& allGroupsList) {
1629599c71d8SAyushi Smriti         if (ec)
1630599c71d8SAyushi Smriti         {
1631599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1632599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1633599c71d8SAyushi Smriti             return;
1634599c71d8SAyushi Smriti         }
1635599c71d8SAyushi Smriti 
16361e1e598dSJonathan Doman         if (allGroupsList.empty())
1637599c71d8SAyushi Smriti         {
1638599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1639599c71d8SAyushi Smriti             return;
1640599c71d8SAyushi Smriti         }
1641599c71d8SAyushi Smriti 
164204ae99ecSEd Tanous         crow::connections::systemBus->async_method_call(
16431ef4c342SEd Tanous             [asyncResp, username, password](const boost::system::error_code ec2,
164459d494eeSPatrick Williams                                             sdbusplus::message_t& m) {
164523a21a1cSEd Tanous             if (ec2)
164604ae99ecSEd Tanous             {
16471ef4c342SEd Tanous                 userErrorMessageHandler(m.get_error(), asyncResp, username, "");
164804ae99ecSEd Tanous                 return;
164904ae99ecSEd Tanous             }
165004ae99ecSEd Tanous 
1651002d39b4SEd Tanous             if (pamUpdatePassword(username, password) != PAM_SUCCESS)
165204ae99ecSEd Tanous             {
16536c51eab1SEd Tanous                 // At this point we have a user that's been
16546c51eab1SEd Tanous                 // created, but the password set
16556c51eab1SEd Tanous                 // failed.Something is wrong, so delete the user
16566c51eab1SEd Tanous                 // that we've already created
16571ef4c342SEd Tanous                 sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1658b477fd44SP Dheeraj Srujan Kumar                 tempObjPath /= username;
1659b477fd44SP Dheeraj Srujan Kumar                 const std::string userPath(tempObjPath);
1660b477fd44SP Dheeraj Srujan Kumar 
166104ae99ecSEd Tanous                 crow::connections::systemBus->async_method_call(
16621ef4c342SEd Tanous                     [asyncResp, password](const boost::system::error_code ec3) {
166323a21a1cSEd Tanous                     if (ec3)
166404ae99ecSEd Tanous                     {
1665002d39b4SEd Tanous                         messages::internalError(asyncResp->res);
166604ae99ecSEd Tanous                         return;
166704ae99ecSEd Tanous                     }
166804ae99ecSEd Tanous 
16690d4197e0Sanil kumar appana                     // If password is invalid
16701ef4c342SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, password,
16711ef4c342SEd Tanous                                                        "Password");
167204ae99ecSEd Tanous                     },
1673002d39b4SEd Tanous                     "xyz.openbmc_project.User.Manager", userPath,
1674002d39b4SEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
167504ae99ecSEd Tanous 
167604ae99ecSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
167704ae99ecSEd Tanous                 return;
167804ae99ecSEd Tanous             }
167904ae99ecSEd Tanous 
1680f12894f8SJason M. Bills             messages::created(asyncResp->res);
168104ae99ecSEd Tanous             asyncResp->res.addHeader(
16821ef4c342SEd Tanous                 "Location", "/redfish/v1/AccountService/Accounts/" + username);
168304ae99ecSEd Tanous             },
1684002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1685002d39b4SEd Tanous             "xyz.openbmc_project.User.Manager", "CreateUser", username,
1686002d39b4SEd Tanous             allGroupsList, *roleId, *enabled);
16871e1e598dSJonathan Doman         });
16881ef4c342SEd Tanous }
1689b9b2e0b2SEd Tanous 
16901ef4c342SEd Tanous inline void
16914c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
16926c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16934c7d4d33SEd Tanous                       const std::string& /*accountName*/)
16941ef4c342SEd Tanous {
16954c7d4d33SEd Tanous 
16963ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
169745ca1b86SEd Tanous     {
169845ca1b86SEd Tanous         return;
169945ca1b86SEd Tanous     }
17004c7d4d33SEd Tanous     asyncResp->res.addHeader(
17014c7d4d33SEd Tanous         boost::beast::http::field::link,
17024c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
17034c7d4d33SEd Tanous }
17044c7d4d33SEd Tanous inline void
17054c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
17064c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17074c7d4d33SEd Tanous                      const std::string& accountName)
17084c7d4d33SEd Tanous {
17094c7d4d33SEd Tanous     handleAccountHead(app, req, asyncResp, accountName);
17101ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1711031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1712d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1713031514fbSJunLin Chen     return;
1714031514fbSJunLin Chen 
17151ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1716031514fbSJunLin Chen     if (req.session == nullptr)
1717031514fbSJunLin Chen     {
1718031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1719031514fbSJunLin Chen         return;
1720031514fbSJunLin Chen     }
17216c51eab1SEd Tanous     if (req.session->username != accountName)
1722b9b2e0b2SEd Tanous     {
17236c51eab1SEd Tanous         // At this point we've determined that the user is trying to
17241ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
17251ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
17261ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
17276c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
17286c51eab1SEd Tanous             redfish::getUserPrivileges(req.userRole);
17291ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
17301ef4c342SEd Tanous                                                          "ConfigureManager"};
17316c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
17326c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1733900f9497SJoseph Reynolds         {
1734900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
1735900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1736900f9497SJoseph Reynolds             return;
1737900f9497SJoseph Reynolds         }
1738900f9497SJoseph Reynolds     }
1739900f9497SJoseph Reynolds 
1740b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
17411ef4c342SEd Tanous         [asyncResp,
17421ef4c342SEd Tanous          accountName](const boost::system::error_code ec,
1743711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1744b9b2e0b2SEd Tanous         if (ec)
1745b9b2e0b2SEd Tanous         {
1746f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1747b9b2e0b2SEd Tanous             return;
1748b9b2e0b2SEd Tanous         }
1749711ac7a9SEd Tanous         const auto userIt = std::find_if(
1750b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
1751b477fd44SP Dheeraj Srujan Kumar             [accountName](
1752b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
1753002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
175455f79e6fSEd Tanous             return accountName == user.first.filename();
1755b477fd44SP Dheeraj Srujan Kumar             });
1756b9b2e0b2SEd Tanous 
175784e12cb7SAppaRao Puli         if (userIt == users.end())
1758b9b2e0b2SEd Tanous         {
1759002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1760002d39b4SEd Tanous                                        accountName);
176184e12cb7SAppaRao Puli             return;
176284e12cb7SAppaRao Puli         }
17634e68c45bSAyushi Smriti 
17641476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
17651476687dSEd Tanous             "#ManagerAccount.v1_4_0.ManagerAccount";
17661476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
17671476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
17681476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
17694e68c45bSAyushi Smriti 
177084e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
177165b0dc32SEd Tanous         {
1772002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
177365b0dc32SEd Tanous             {
177465b0dc32SEd Tanous                 for (const auto& property : interface.second)
177565b0dc32SEd Tanous                 {
177665b0dc32SEd Tanous                     if (property.first == "UserEnabled")
177765b0dc32SEd Tanous                     {
177865b0dc32SEd Tanous                         const bool* userEnabled =
1779abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
178065b0dc32SEd Tanous                         if (userEnabled == nullptr)
178165b0dc32SEd Tanous                         {
1782002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
178384e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
178484e12cb7SAppaRao Puli                             return;
178565b0dc32SEd Tanous                         }
1786002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
178765b0dc32SEd Tanous                     }
1788002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
178965b0dc32SEd Tanous                     {
179065b0dc32SEd Tanous                         const bool* userLocked =
1791abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
179265b0dc32SEd Tanous                         if (userLocked == nullptr)
179365b0dc32SEd Tanous                         {
179484e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
179584e12cb7SAppaRao Puli                                                 "ailedAttempt "
179684e12cb7SAppaRao Puli                                                 "wasn't a bool";
179784e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
179884e12cb7SAppaRao Puli                             return;
179965b0dc32SEd Tanous                         }
1800002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
1801002d39b4SEd Tanous                         asyncResp->res
1802002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
18033bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
180465b0dc32SEd Tanous                     }
180584e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
180684e12cb7SAppaRao Puli                     {
180754fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1808002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
180954fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
181084e12cb7SAppaRao Puli                         {
1811002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
181284e12cb7SAppaRao Puli                                                 "string";
181384e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
181484e12cb7SAppaRao Puli                             return;
181584e12cb7SAppaRao Puli                         }
18161ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
181754fc587aSNagaraju Goruganti                         if (role.empty())
181884e12cb7SAppaRao Puli                         {
181984e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
182084e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
182184e12cb7SAppaRao Puli                             return;
182284e12cb7SAppaRao Puli                         }
182354fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
182484e12cb7SAppaRao Puli 
18251476687dSEd Tanous                         nlohmann::json& roleEntry =
1826002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
18271476687dSEd Tanous                         roleEntry["@odata.id"] =
1828002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
182984e12cb7SAppaRao Puli                     }
1830002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
18313bf4e632SJoseph Reynolds                     {
18323bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
18333bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
18343bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
18353bf4e632SJoseph Reynolds                         {
18360fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
18370fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
18383bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
18393bf4e632SJoseph Reynolds                             return;
18403bf4e632SJoseph Reynolds                         }
1841002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
18423bf4e632SJoseph Reynolds                             *userPasswordExpired;
18433bf4e632SJoseph Reynolds                     }
1844c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
1845c7229815SAbhishek Patel                     {
1846c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
1847c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
1848c7229815SAbhishek Patel                                 &property.second);
1849c7229815SAbhishek Patel                         if (userGroups == nullptr)
1850c7229815SAbhishek Patel                         {
1851c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR
1852c7229815SAbhishek Patel                                 << "userGroups wasn't a string vector";
1853c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1854c7229815SAbhishek Patel                             return;
1855c7229815SAbhishek Patel                         }
1856c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
1857c7229815SAbhishek Patel                         {
1858c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR << "userGroups mapping failed";
1859c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1860c7229815SAbhishek Patel                             return;
1861c7229815SAbhishek Patel                         }
1862c7229815SAbhishek Patel                     }
186365b0dc32SEd Tanous                 }
186465b0dc32SEd Tanous             }
186565b0dc32SEd Tanous         }
186665b0dc32SEd Tanous 
1867b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
186884e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
1869b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
1870b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
1871b9b2e0b2SEd Tanous         },
18721ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1873b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
18741ef4c342SEd Tanous }
1875a840879dSEd Tanous 
18761ef4c342SEd Tanous inline void
18771ef4c342SEd Tanous     handleAccounttDelete(App& app, const crow::Request& req,
18786c51eab1SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18791ef4c342SEd Tanous                          const std::string& username)
18801ef4c342SEd Tanous {
18813ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
188245ca1b86SEd Tanous     {
188345ca1b86SEd Tanous         return;
188445ca1b86SEd Tanous     }
18851ef4c342SEd Tanous 
18861ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1887031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1888d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1889031514fbSJunLin Chen     return;
1890031514fbSJunLin Chen 
18911ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
18921ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
18931ef4c342SEd Tanous     tempObjPath /= username;
18941ef4c342SEd Tanous     const std::string userPath(tempObjPath);
18951ef4c342SEd Tanous 
18961ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
18971ef4c342SEd Tanous         [asyncResp, username](const boost::system::error_code ec) {
18981ef4c342SEd Tanous         if (ec)
18991ef4c342SEd Tanous         {
1900d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
19011ef4c342SEd Tanous                                        username);
19021ef4c342SEd Tanous             return;
19031ef4c342SEd Tanous         }
19041ef4c342SEd Tanous 
19051ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
19061ef4c342SEd Tanous         },
19071ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
19081ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
19091ef4c342SEd Tanous }
19101ef4c342SEd Tanous 
19111ef4c342SEd Tanous inline void
19121ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
19131ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19141ef4c342SEd Tanous                        const std::string& username)
19151ef4c342SEd Tanous {
19161ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19171ef4c342SEd Tanous     {
19181ef4c342SEd Tanous         return;
19191ef4c342SEd Tanous     }
19201ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
19211ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
1922d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
19231ef4c342SEd Tanous     return;
19241ef4c342SEd Tanous 
19251ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1926a24526dcSEd Tanous     std::optional<std::string> newUserName;
1927a24526dcSEd Tanous     std::optional<std::string> password;
1928a24526dcSEd Tanous     std::optional<bool> enabled;
1929a24526dcSEd Tanous     std::optional<std::string> roleId;
193024c8542dSRatan Gupta     std::optional<bool> locked;
1931e9cc5172SEd Tanous 
1932031514fbSJunLin Chen     if (req.session == nullptr)
1933031514fbSJunLin Chen     {
1934031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1935031514fbSJunLin Chen         return;
1936031514fbSJunLin Chen     }
1937031514fbSJunLin Chen 
1938e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
1939e9cc5172SEd Tanous         redfish::getUserPrivileges(req.userRole);
1940e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
1941e9cc5172SEd Tanous     bool userHasConfigureUsers =
1942e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
1943e9cc5172SEd Tanous     if (userHasConfigureUsers)
1944e9cc5172SEd Tanous     {
1945e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
19461ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "UserName",
19471ef4c342SEd Tanous                                       newUserName, "Password", password,
19481ef4c342SEd Tanous                                       "RoleId", roleId, "Enabled", enabled,
19491ef4c342SEd Tanous                                       "Locked", locked))
1950a840879dSEd Tanous         {
1951a840879dSEd Tanous             return;
1952a840879dSEd Tanous         }
1953e9cc5172SEd Tanous     }
1954e9cc5172SEd Tanous     else
1955900f9497SJoseph Reynolds     {
1956e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
1957e9cc5172SEd Tanous         if (username != req.session->username)
1958900f9497SJoseph Reynolds         {
1959900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1960900f9497SJoseph Reynolds             return;
1961900f9497SJoseph Reynolds         }
1962031514fbSJunLin Chen 
1963e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
19641ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
19651ef4c342SEd Tanous                                       password))
1966e9cc5172SEd Tanous         {
1967e9cc5172SEd Tanous             return;
1968e9cc5172SEd Tanous         }
1969900f9497SJoseph Reynolds     }
1970900f9497SJoseph Reynolds 
197166b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
19726c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
19736c51eab1SEd Tanous     // updating user properties other then username. If username
19746c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
19756c51eab1SEd Tanous     // user rename request.
197666b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
1977a840879dSEd Tanous     {
19781ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
19791ef4c342SEd Tanous                              locked);
198084e12cb7SAppaRao Puli         return;
198184e12cb7SAppaRao Puli     }
198284e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
19836c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
19841ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
198559d494eeSPatrick Williams          locked](const boost::system::error_code ec, sdbusplus::message_t& m) {
198684e12cb7SAppaRao Puli         if (ec)
198784e12cb7SAppaRao Puli         {
1988002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
1989002d39b4SEd Tanous                                     username);
1990a840879dSEd Tanous             return;
1991a840879dSEd Tanous         }
1992a840879dSEd Tanous 
1993002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
1994002d39b4SEd Tanous                              locked);
199584e12cb7SAppaRao Puli         },
19961ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
199784e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
199884e12cb7SAppaRao Puli         *newUserName);
19991ef4c342SEd Tanous }
20001ef4c342SEd Tanous 
20011ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
20021ef4c342SEd Tanous {
20031ef4c342SEd Tanous 
20041ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20054c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
20064c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20074c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
20084c7d4d33SEd Tanous 
20094c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20101ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
20111ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20121ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
20131ef4c342SEd Tanous 
20141ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20151ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
20161ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20171ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
20181ef4c342SEd Tanous 
20191ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20204c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
20214c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20224c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
20234c7d4d33SEd Tanous 
20244c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20251ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
20261ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20271ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
20281ef4c342SEd Tanous 
20291ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20301ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
20311ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
20321ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
20331ef4c342SEd Tanous 
20341ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20354c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
20364c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20374c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
20384c7d4d33SEd Tanous 
20394c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20401ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
20411ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20421ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
20431ef4c342SEd Tanous 
20441ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20451ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
20461ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
20471ef4c342SEd Tanous         // yet
20481ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
20491ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20501ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
205184e12cb7SAppaRao Puli 
20526c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2053ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
20546c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
20551ef4c342SEd Tanous             std::bind_front(handleAccounttDelete, std::ref(app)));
205606e086d9SEd Tanous }
205788d16c9aSLewanczyk, Dawid 
205888d16c9aSLewanczyk, Dawid } // namespace redfish
2059