xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 3e72c2027aa4e64b9892ab0d3970358ba446f1fa)
188d16c9aSLewanczyk, Dawid /*
288d16c9aSLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
388d16c9aSLewanczyk, Dawid //
488d16c9aSLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
588d16c9aSLewanczyk, Dawid // you may not use this file except in compliance with the License.
688d16c9aSLewanczyk, Dawid // You may obtain a copy of the License at
788d16c9aSLewanczyk, Dawid //
888d16c9aSLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
988d16c9aSLewanczyk, Dawid //
1088d16c9aSLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1188d16c9aSLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1288d16c9aSLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388d16c9aSLewanczyk, Dawid // See the License for the specific language governing permissions and
1488d16c9aSLewanczyk, Dawid // limitations under the License.
1588d16c9aSLewanczyk, Dawid */
1688d16c9aSLewanczyk, Dawid #pragma once
1788d16c9aSLewanczyk, Dawid 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_utility.hpp"
203ccb3adbSEd Tanous #include "error_messages.hpp"
210ec8b83dSEd Tanous #include "generated/enums/account_service.hpp"
223ccb3adbSEd Tanous #include "openbmc_dbus_rest.hpp"
233ccb3adbSEd Tanous #include "persistent_data.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
250ec8b83dSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
280ec8b83dSEd Tanous 
291e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
30d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
311214b7e7SGunnar Mills 
322b73119cSGeorge Liu #include <array>
33c7229815SAbhishek Patel #include <optional>
34c7229815SAbhishek Patel #include <string>
352b73119cSGeorge Liu #include <string_view>
36c7229815SAbhishek Patel #include <vector>
37c7229815SAbhishek Patel 
381abe55efSEd Tanous namespace redfish
391abe55efSEd Tanous {
4088d16c9aSLewanczyk, Dawid 
4123a21a1cSEd Tanous constexpr const char* ldapConfigObjectName =
426973a582SRatan Gupta     "/xyz/openbmc_project/user/ldap/openldap";
432c70f800SEd Tanous constexpr const char* adConfigObject =
44ab828d7cSRatan Gupta     "/xyz/openbmc_project/user/ldap/active_directory";
45ab828d7cSRatan Gupta 
46b477fd44SP Dheeraj Srujan Kumar constexpr const char* rootUserDbusPath = "/xyz/openbmc_project/user/";
476973a582SRatan Gupta constexpr const char* ldapRootObject = "/xyz/openbmc_project/user/ldap";
486973a582SRatan Gupta constexpr const char* ldapDbusService = "xyz.openbmc_project.Ldap.Config";
496973a582SRatan Gupta constexpr const char* ldapConfigInterface =
506973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Config";
516973a582SRatan Gupta constexpr const char* ldapCreateInterface =
526973a582SRatan Gupta     "xyz.openbmc_project.User.Ldap.Create";
536973a582SRatan Gupta constexpr const char* ldapEnableInterface = "xyz.openbmc_project.Object.Enable";
5406785244SRatan Gupta constexpr const char* ldapPrivMapperInterface =
5506785244SRatan Gupta     "xyz.openbmc_project.User.PrivilegeMapper";
566973a582SRatan Gupta constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
576973a582SRatan Gupta constexpr const char* propertyInterface = "org.freedesktop.DBus.Properties";
586973a582SRatan Gupta 
5954fc587aSNagaraju Goruganti struct LDAPRoleMapData
6054fc587aSNagaraju Goruganti {
6154fc587aSNagaraju Goruganti     std::string groupName;
6254fc587aSNagaraju Goruganti     std::string privilege;
6354fc587aSNagaraju Goruganti };
6454fc587aSNagaraju Goruganti 
656973a582SRatan Gupta struct LDAPConfigData
666973a582SRatan Gupta {
676973a582SRatan Gupta     std::string uri{};
686973a582SRatan Gupta     std::string bindDN{};
696973a582SRatan Gupta     std::string baseDN{};
706973a582SRatan Gupta     std::string searchScope{};
716973a582SRatan Gupta     std::string serverType{};
726973a582SRatan Gupta     bool serviceEnabled = false;
736973a582SRatan Gupta     std::string userNameAttribute{};
746973a582SRatan Gupta     std::string groupAttribute{};
7554fc587aSNagaraju Goruganti     std::vector<std::pair<std::string, LDAPRoleMapData>> groupRoleList;
766973a582SRatan Gupta };
776973a582SRatan Gupta 
7854fc587aSNagaraju Goruganti inline std::string getRoleIdFromPrivilege(std::string_view role)
7984e12cb7SAppaRao Puli {
8084e12cb7SAppaRao Puli     if (role == "priv-admin")
8184e12cb7SAppaRao Puli     {
8284e12cb7SAppaRao Puli         return "Administrator";
8384e12cb7SAppaRao Puli     }
843174e4dfSEd Tanous     if (role == "priv-user")
8584e12cb7SAppaRao Puli     {
86c80fee55SAppaRao Puli         return "ReadOnly";
8784e12cb7SAppaRao Puli     }
883174e4dfSEd Tanous     if (role == "priv-operator")
8984e12cb7SAppaRao Puli     {
9084e12cb7SAppaRao Puli         return "Operator";
9184e12cb7SAppaRao Puli     }
9284e12cb7SAppaRao Puli     return "";
9384e12cb7SAppaRao Puli }
9454fc587aSNagaraju Goruganti inline std::string getPrivilegeFromRoleId(std::string_view role)
9584e12cb7SAppaRao Puli {
9684e12cb7SAppaRao Puli     if (role == "Administrator")
9784e12cb7SAppaRao Puli     {
9884e12cb7SAppaRao Puli         return "priv-admin";
9984e12cb7SAppaRao Puli     }
1003174e4dfSEd Tanous     if (role == "ReadOnly")
10184e12cb7SAppaRao Puli     {
10284e12cb7SAppaRao Puli         return "priv-user";
10384e12cb7SAppaRao Puli     }
1043174e4dfSEd Tanous     if (role == "Operator")
10584e12cb7SAppaRao Puli     {
10684e12cb7SAppaRao Puli         return "priv-operator";
10784e12cb7SAppaRao Puli     }
10884e12cb7SAppaRao Puli     return "";
10984e12cb7SAppaRao Puli }
110b9b2e0b2SEd Tanous 
111c7229815SAbhishek Patel /**
112c7229815SAbhishek Patel  * @brief Maps user group names retrieved from D-Bus object to
113c7229815SAbhishek Patel  * Account Types.
114c7229815SAbhishek Patel  *
115c7229815SAbhishek Patel  * @param[in] userGroups List of User groups
116c7229815SAbhishek Patel  * @param[out] res AccountTypes populated
117c7229815SAbhishek Patel  *
118c7229815SAbhishek Patel  * @return true in case of success, false if UserGroups contains
119c7229815SAbhishek Patel  * invalid group name(s).
120c7229815SAbhishek Patel  */
121c7229815SAbhishek Patel inline bool translateUserGroup(const std::vector<std::string>& userGroups,
122c7229815SAbhishek Patel                                crow::Response& res)
123c7229815SAbhishek Patel {
124c7229815SAbhishek Patel     std::vector<std::string> accountTypes;
125c7229815SAbhishek Patel     for (const auto& userGroup : userGroups)
126c7229815SAbhishek Patel     {
127c7229815SAbhishek Patel         if (userGroup == "redfish")
128c7229815SAbhishek Patel         {
129c7229815SAbhishek Patel             accountTypes.emplace_back("Redfish");
130c7229815SAbhishek Patel             accountTypes.emplace_back("WebUI");
131c7229815SAbhishek Patel         }
132c7229815SAbhishek Patel         else if (userGroup == "ipmi")
133c7229815SAbhishek Patel         {
134c7229815SAbhishek Patel             accountTypes.emplace_back("IPMI");
135c7229815SAbhishek Patel         }
136c7229815SAbhishek Patel         else if (userGroup == "ssh")
137c7229815SAbhishek Patel         {
138c7229815SAbhishek Patel             accountTypes.emplace_back("ManagerConsole");
139c7229815SAbhishek Patel         }
140*3e72c202SNinad Palsule         else if (userGroup == "hostconsole")
141*3e72c202SNinad Palsule         {
142*3e72c202SNinad Palsule             // The hostconsole group controls who can access the host console
143*3e72c202SNinad Palsule             // port via ssh and websocket.
144*3e72c202SNinad Palsule             accountTypes.emplace_back("HostConsole");
145*3e72c202SNinad Palsule         }
146c7229815SAbhishek Patel         else if (userGroup == "web")
147c7229815SAbhishek Patel         {
148c7229815SAbhishek Patel             // 'web' is one of the valid groups in the UserGroups property of
149c7229815SAbhishek Patel             // the user account in the D-Bus object. This group is currently not
150c7229815SAbhishek Patel             // doing anything, and is considered to be equivalent to 'redfish'.
151c7229815SAbhishek Patel             // 'redfish' user group is mapped to 'Redfish'and 'WebUI'
152c7229815SAbhishek Patel             // AccountTypes, so do nothing here...
153c7229815SAbhishek Patel         }
154c7229815SAbhishek Patel         else
155c7229815SAbhishek Patel         {
156c7229815SAbhishek Patel             // Invalid user group name. Caller throws an excption.
157c7229815SAbhishek Patel             return false;
158c7229815SAbhishek Patel         }
159c7229815SAbhishek Patel     }
160c7229815SAbhishek Patel 
161c7229815SAbhishek Patel     res.jsonValue["AccountTypes"] = std::move(accountTypes);
162c7229815SAbhishek Patel     return true;
163c7229815SAbhishek Patel }
164c7229815SAbhishek Patel 
1658d1b46d7Szhanghch05 inline void userErrorMessageHandler(
1668d1b46d7Szhanghch05     const sd_bus_error* e, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1678d1b46d7Szhanghch05     const std::string& newUser, const std::string& username)
16866b5ca76Sjayaprakash Mutyala {
16966b5ca76Sjayaprakash Mutyala     if (e == nullptr)
17066b5ca76Sjayaprakash Mutyala     {
17166b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
17266b5ca76Sjayaprakash Mutyala         return;
17366b5ca76Sjayaprakash Mutyala     }
17466b5ca76Sjayaprakash Mutyala 
175055806b3SManojkiran Eda     const char* errorMessage = e->name;
17666b5ca76Sjayaprakash Mutyala     if (strcmp(errorMessage,
17766b5ca76Sjayaprakash Mutyala                "xyz.openbmc_project.User.Common.Error.UserNameExists") == 0)
17866b5ca76Sjayaprakash Mutyala     {
179d8a5d5d8SJiaqing Zhao         messages::resourceAlreadyExists(asyncResp->res, "ManagerAccount",
18066b5ca76Sjayaprakash Mutyala                                         "UserName", newUser);
18166b5ca76Sjayaprakash Mutyala     }
18266b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage, "xyz.openbmc_project.User.Common.Error."
18366b5ca76Sjayaprakash Mutyala                                   "UserNameDoesNotExist") == 0)
18466b5ca76Sjayaprakash Mutyala     {
185d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
18666b5ca76Sjayaprakash Mutyala     }
187d4d25793SEd Tanous     else if ((strcmp(errorMessage,
188d4d25793SEd Tanous                      "xyz.openbmc_project.Common.Error.InvalidArgument") ==
189d4d25793SEd Tanous               0) ||
1900fda0f12SGeorge Liu              (strcmp(
1910fda0f12SGeorge Liu                   errorMessage,
1920fda0f12SGeorge Liu                   "xyz.openbmc_project.User.Common.Error.UserNameGroupFail") ==
1930fda0f12SGeorge Liu               0))
19466b5ca76Sjayaprakash Mutyala     {
19566b5ca76Sjayaprakash Mutyala         messages::propertyValueFormatError(asyncResp->res, newUser, "UserName");
19666b5ca76Sjayaprakash Mutyala     }
19766b5ca76Sjayaprakash Mutyala     else if (strcmp(errorMessage,
19866b5ca76Sjayaprakash Mutyala                     "xyz.openbmc_project.User.Common.Error.NoResource") == 0)
19966b5ca76Sjayaprakash Mutyala     {
20066b5ca76Sjayaprakash Mutyala         messages::createLimitReachedForResource(asyncResp->res);
20166b5ca76Sjayaprakash Mutyala     }
20266b5ca76Sjayaprakash Mutyala     else
20366b5ca76Sjayaprakash Mutyala     {
20466b5ca76Sjayaprakash Mutyala         messages::internalError(asyncResp->res);
20566b5ca76Sjayaprakash Mutyala     }
20666b5ca76Sjayaprakash Mutyala }
20766b5ca76Sjayaprakash Mutyala 
20881ce609eSEd Tanous inline void parseLDAPConfigData(nlohmann::json& jsonResponse,
209ab828d7cSRatan Gupta                                 const LDAPConfigData& confData,
210ab828d7cSRatan Gupta                                 const std::string& ldapType)
2116973a582SRatan Gupta {
21289492a15SPatrick Williams     std::string service = (ldapType == "LDAP") ? "LDAPService"
21389492a15SPatrick Williams                                                : "ActiveDirectoryService";
21454fc587aSNagaraju Goruganti 
2151476687dSEd Tanous     nlohmann::json& ldap = jsonResponse[ldapType];
21654fc587aSNagaraju Goruganti 
2171476687dSEd Tanous     ldap["ServiceEnabled"] = confData.serviceEnabled;
2181476687dSEd Tanous     ldap["ServiceAddresses"] = nlohmann::json::array({confData.uri});
2190ec8b83dSEd Tanous     ldap["Authentication"]["AuthenticationType"] =
2200ec8b83dSEd Tanous         account_service::AuthenticationTypes::UsernameAndPassword;
2211476687dSEd Tanous     ldap["Authentication"]["Username"] = confData.bindDN;
2221476687dSEd Tanous     ldap["Authentication"]["Password"] = nullptr;
2231476687dSEd Tanous 
2241476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["BaseDistinguishedNames"] =
2251476687dSEd Tanous         nlohmann::json::array({confData.baseDN});
2261476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["UsernameAttribute"] =
2271476687dSEd Tanous         confData.userNameAttribute;
2281476687dSEd Tanous     ldap["LDAPService"]["SearchSettings"]["GroupsAttribute"] =
2291476687dSEd Tanous         confData.groupAttribute;
2301476687dSEd Tanous 
2311476687dSEd Tanous     nlohmann::json& roleMapArray = ldap["RemoteRoleMapping"];
23254fc587aSNagaraju Goruganti     roleMapArray = nlohmann::json::array();
2339eb808c1SEd Tanous     for (const auto& obj : confData.groupRoleList)
23454fc587aSNagaraju Goruganti     {
23554fc587aSNagaraju Goruganti         BMCWEB_LOG_DEBUG << "Pushing the data groupName="
23654fc587aSNagaraju Goruganti                          << obj.second.groupName << "\n";
237613dabeaSEd Tanous 
238613dabeaSEd Tanous         nlohmann::json::object_t remoteGroup;
239613dabeaSEd Tanous         remoteGroup["RemoteGroup"] = obj.second.groupName;
240329f0348SJorge Cisneros         remoteGroup["LocalRole"] = getRoleIdFromPrivilege(obj.second.privilege);
241329f0348SJorge Cisneros         roleMapArray.emplace_back(std::move(remoteGroup));
24254fc587aSNagaraju Goruganti     }
2436973a582SRatan Gupta }
2446973a582SRatan Gupta 
2456973a582SRatan Gupta /**
24606785244SRatan Gupta  *  @brief validates given JSON input and then calls appropriate method to
24706785244SRatan Gupta  * create, to delete or to set Rolemapping object based on the given input.
24806785244SRatan Gupta  *
24906785244SRatan Gupta  */
25023a21a1cSEd Tanous inline void handleRoleMapPatch(
2518d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25206785244SRatan Gupta     const std::vector<std::pair<std::string, LDAPRoleMapData>>& roleMapObjData,
253f23b7296SEd Tanous     const std::string& serverType, const std::vector<nlohmann::json>& input)
25406785244SRatan Gupta {
25506785244SRatan Gupta     for (size_t index = 0; index < input.size(); index++)
25606785244SRatan Gupta     {
257f23b7296SEd Tanous         const nlohmann::json& thisJson = input[index];
25806785244SRatan Gupta 
25906785244SRatan Gupta         if (thisJson.is_null())
26006785244SRatan Gupta         {
26106785244SRatan Gupta             // delete the existing object
26206785244SRatan Gupta             if (index < roleMapObjData.size())
26306785244SRatan Gupta             {
26406785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
26506785244SRatan Gupta                     [asyncResp, roleMapObjData, serverType,
2665e7e2dc5SEd Tanous                      index](const boost::system::error_code& ec) {
26706785244SRatan Gupta                     if (ec)
26806785244SRatan Gupta                     {
26906785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
27006785244SRatan Gupta                         messages::internalError(asyncResp->res);
27106785244SRatan Gupta                         return;
27206785244SRatan Gupta                     }
27389492a15SPatrick Williams                     asyncResp->res.jsonValue[serverType]["RemoteRoleMapping"]
27489492a15SPatrick Williams                                             [index] = nullptr;
27506785244SRatan Gupta                     },
27606785244SRatan Gupta                     ldapDbusService, roleMapObjData[index].first,
27706785244SRatan Gupta                     "xyz.openbmc_project.Object.Delete", "Delete");
27806785244SRatan Gupta             }
27906785244SRatan Gupta             else
28006785244SRatan Gupta             {
28106785244SRatan Gupta                 BMCWEB_LOG_ERROR << "Can't delete the object";
28206785244SRatan Gupta                 messages::propertyValueTypeError(
28371f52d96SEd Tanous                     asyncResp->res,
28471f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
28571f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
28606785244SRatan Gupta                     "RemoteRoleMapping/" + std::to_string(index));
28706785244SRatan Gupta                 return;
28806785244SRatan Gupta             }
28906785244SRatan Gupta         }
29006785244SRatan Gupta         else if (thisJson.empty())
29106785244SRatan Gupta         {
29206785244SRatan Gupta             // Don't do anything for the empty objects,parse next json
29306785244SRatan Gupta             // eg {"RemoteRoleMapping",[{}]}
29406785244SRatan Gupta         }
29506785244SRatan Gupta         else
29606785244SRatan Gupta         {
29706785244SRatan Gupta             // update/create the object
29806785244SRatan Gupta             std::optional<std::string> remoteGroup;
29906785244SRatan Gupta             std::optional<std::string> localRole;
30006785244SRatan Gupta 
301f23b7296SEd Tanous             // This is a copy, but it's required in this case because of how
302f23b7296SEd Tanous             // readJson is structured
303f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
304f23b7296SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res,
305f23b7296SEd Tanous                                      "RemoteGroup", remoteGroup, "LocalRole",
306f23b7296SEd Tanous                                      localRole))
30706785244SRatan Gupta             {
30806785244SRatan Gupta                 continue;
30906785244SRatan Gupta             }
31006785244SRatan Gupta 
31106785244SRatan Gupta             // Update existing RoleMapping Object
31206785244SRatan Gupta             if (index < roleMapObjData.size())
31306785244SRatan Gupta             {
31406785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Update Role Map Object";
31506785244SRatan Gupta                 // If "RemoteGroup" info is provided
31606785244SRatan Gupta                 if (remoteGroup)
31706785244SRatan Gupta                 {
31806785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
31906785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
3205e7e2dc5SEd Tanous                          remoteGroup](const boost::system::error_code& ec) {
32106785244SRatan Gupta                         if (ec)
32206785244SRatan Gupta                         {
323002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
32406785244SRatan Gupta                             messages::internalError(asyncResp->res);
32506785244SRatan Gupta                             return;
32606785244SRatan Gupta                         }
32706785244SRatan Gupta                         asyncResp->res
328002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
329002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
33006785244SRatan Gupta                         },
33106785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
33206785244SRatan Gupta                         propertyInterface, "Set",
33306785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
33406785244SRatan Gupta                         "GroupName",
335168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
336168e20c1SEd Tanous                             std::move(*remoteGroup)));
33706785244SRatan Gupta                 }
33806785244SRatan Gupta 
33906785244SRatan Gupta                 // If "LocalRole" info is provided
34006785244SRatan Gupta                 if (localRole)
34106785244SRatan Gupta                 {
34206785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
34306785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
3445e7e2dc5SEd Tanous                          localRole](const boost::system::error_code& ec) {
34506785244SRatan Gupta                         if (ec)
34606785244SRatan Gupta                         {
347002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
34806785244SRatan Gupta                             messages::internalError(asyncResp->res);
34906785244SRatan Gupta                             return;
35006785244SRatan Gupta                         }
35106785244SRatan Gupta                         asyncResp->res
352002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
353002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
35406785244SRatan Gupta                         },
35506785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
35606785244SRatan Gupta                         propertyInterface, "Set",
35706785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
35806785244SRatan Gupta                         "Privilege",
359168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
36006785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
36106785244SRatan Gupta                 }
36206785244SRatan Gupta             }
36306785244SRatan Gupta             // Create a new RoleMapping Object.
36406785244SRatan Gupta             else
36506785244SRatan Gupta             {
36606785244SRatan Gupta                 BMCWEB_LOG_DEBUG
36706785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
36889492a15SPatrick Williams                 std::string pathString = "RemoteRoleMapping/" +
36989492a15SPatrick Williams                                          std::to_string(index);
37006785244SRatan Gupta 
37106785244SRatan Gupta                 if (!localRole)
37206785244SRatan Gupta                 {
37306785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
37406785244SRatan Gupta                                               pathString + "/LocalRole");
37506785244SRatan Gupta                     continue;
37606785244SRatan Gupta                 }
37706785244SRatan Gupta                 if (!remoteGroup)
37806785244SRatan Gupta                 {
37906785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
38006785244SRatan Gupta                                               pathString + "/RemoteGroup");
38106785244SRatan Gupta                     continue;
38206785244SRatan Gupta                 }
38306785244SRatan Gupta 
38406785244SRatan Gupta                 std::string dbusObjectPath;
38506785244SRatan Gupta                 if (serverType == "ActiveDirectory")
38606785244SRatan Gupta                 {
3872c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
38806785244SRatan Gupta                 }
38906785244SRatan Gupta                 else if (serverType == "LDAP")
39006785244SRatan Gupta                 {
39123a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
39206785244SRatan Gupta                 }
39306785244SRatan Gupta 
39406785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
39506785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
39606785244SRatan Gupta 
39706785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
398271584abSEd Tanous                     [asyncResp, serverType, localRole,
3995e7e2dc5SEd Tanous                      remoteGroup](const boost::system::error_code& ec) {
40006785244SRatan Gupta                     if (ec)
40106785244SRatan Gupta                     {
40206785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
40306785244SRatan Gupta                         messages::internalError(asyncResp->res);
40406785244SRatan Gupta                         return;
40506785244SRatan Gupta                     }
40606785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
40706785244SRatan Gupta                         asyncResp->res
40806785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
4091476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
4101476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
4111476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
412b2ba3072SPatrick Williams                     remoteRoleJson.emplace_back(std::move(roleMapEntry));
41306785244SRatan Gupta                     },
41406785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
4153174e4dfSEd Tanous                     "Create", *remoteGroup,
41606785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
41706785244SRatan Gupta             }
41806785244SRatan Gupta         }
41906785244SRatan Gupta     }
42006785244SRatan Gupta }
42106785244SRatan Gupta 
42206785244SRatan Gupta /**
4236973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
4246973a582SRatan Gupta  * into JSON
4256973a582SRatan Gupta  */
4266973a582SRatan Gupta template <typename CallbackFunc>
4276973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
4286973a582SRatan Gupta                               CallbackFunc&& callback)
4296973a582SRatan Gupta {
4302b73119cSGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
4312b73119cSGeorge Liu         ldapEnableInterface, ldapConfigInterface};
43254fc587aSNagaraju Goruganti 
4332b73119cSGeorge Liu     dbus::utility::getDbusObject(
4342b73119cSGeorge Liu         ldapConfigObjectName, interfaces,
4352b73119cSGeorge 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,
4485e7e2dc5SEd 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                         for (const auto& property : interface.second)
5166973a582SRatan Gupta                         {
517271584abSEd Tanous                             const std::string* strValue =
518002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
519271584abSEd Tanous                             if (strValue == nullptr)
5206973a582SRatan Gupta                             {
5216973a582SRatan Gupta                                 continue;
5226973a582SRatan Gupta                             }
5236973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
5246973a582SRatan Gupta                             {
525271584abSEd Tanous                                 confData.uri = *strValue;
5266973a582SRatan Gupta                             }
5276973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
5286973a582SRatan Gupta                             {
529271584abSEd Tanous                                 confData.bindDN = *strValue;
5306973a582SRatan Gupta                             }
5316973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
5326973a582SRatan Gupta                             {
533271584abSEd Tanous                                 confData.baseDN = *strValue;
5346973a582SRatan Gupta                             }
535002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
5366973a582SRatan Gupta                             {
537271584abSEd Tanous                                 confData.searchScope = *strValue;
5386973a582SRatan Gupta                             }
539002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
5406973a582SRatan Gupta                             {
541271584abSEd Tanous                                 confData.groupAttribute = *strValue;
5426973a582SRatan Gupta                             }
543002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
5446973a582SRatan Gupta                             {
545271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
5466973a582SRatan Gupta                             }
54754fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
548ab828d7cSRatan Gupta                             {
549271584abSEd Tanous                                 confData.serverType = *strValue;
55054fc587aSNagaraju Goruganti                             }
55154fc587aSNagaraju Goruganti                         }
55254fc587aSNagaraju Goruganti                     }
553002d39b4SEd Tanous                     else if (interface.first ==
5540fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
55554fc587aSNagaraju Goruganti                     {
55654fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
55754fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
55854fc587aSNagaraju Goruganti                         {
559271584abSEd Tanous                             const std::string* strValue =
560002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
56154fc587aSNagaraju Goruganti 
562271584abSEd Tanous                             if (strValue == nullptr)
56354fc587aSNagaraju Goruganti                             {
56454fc587aSNagaraju Goruganti                                 continue;
56554fc587aSNagaraju Goruganti                             }
56654fc587aSNagaraju Goruganti 
56754fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
56854fc587aSNagaraju Goruganti                             {
569271584abSEd Tanous                                 roleMapData.groupName = *strValue;
57054fc587aSNagaraju Goruganti                             }
57154fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
57254fc587aSNagaraju Goruganti                             {
573271584abSEd Tanous                                 roleMapData.privilege = *strValue;
57454fc587aSNagaraju Goruganti                             }
57554fc587aSNagaraju Goruganti                         }
57654fc587aSNagaraju Goruganti 
577002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
578002d39b4SEd Tanous                                                             roleMapData);
57954fc587aSNagaraju Goruganti                     }
58054fc587aSNagaraju Goruganti                 }
58154fc587aSNagaraju Goruganti             }
582ab828d7cSRatan Gupta             callback(true, confData, ldapType);
58354fc587aSNagaraju Goruganti             },
584002d39b4SEd Tanous             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
5852b73119cSGeorge Liu         });
5866973a582SRatan Gupta }
5876973a582SRatan Gupta 
5888a07d286SRatan Gupta /**
5898a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
5908a07d286SRatan Gupta  * @param input JSON data
5918a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
5928a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
5938a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
5948a07d286SRatan Gupta  */
5954f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
5966c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5976c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
5988a07d286SRatan Gupta {
5998a07d286SRatan Gupta     std::optional<std::string> authType;
6008a07d286SRatan Gupta 
6018a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
6028a07d286SRatan Gupta                              authType, "Username", username, "Password",
6038a07d286SRatan Gupta                              password))
6048a07d286SRatan Gupta     {
6058a07d286SRatan Gupta         return;
6068a07d286SRatan Gupta     }
6078a07d286SRatan Gupta     if (!authType)
6088a07d286SRatan Gupta     {
6098a07d286SRatan Gupta         return;
6108a07d286SRatan Gupta     }
6118a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
6128a07d286SRatan Gupta     {
6138a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
6148a07d286SRatan Gupta                                          "AuthenticationType");
6158a07d286SRatan Gupta         return;
6168a07d286SRatan Gupta     }
6178a07d286SRatan Gupta }
6188a07d286SRatan Gupta /**
6198a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
6208a07d286SRatan Gupta  * @param input JSON data
6218a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6228a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
6238a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
6248a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
6258a07d286SRatan Gupta  */
6268a07d286SRatan Gupta 
6274f48d5f6SEd Tanous inline void
6284f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
6298d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6308a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
6318a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
6328a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
6338a07d286SRatan Gupta {
6348a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
6358a07d286SRatan Gupta 
6368a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
6378a07d286SRatan Gupta                              searchSettings))
6388a07d286SRatan Gupta     {
6398a07d286SRatan Gupta         return;
6408a07d286SRatan Gupta     }
6418a07d286SRatan Gupta     if (!searchSettings)
6428a07d286SRatan Gupta     {
6438a07d286SRatan Gupta         return;
6448a07d286SRatan Gupta     }
6458a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
6468a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
6478a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
6488a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
6498a07d286SRatan Gupta     {
6508a07d286SRatan Gupta         return;
6518a07d286SRatan Gupta     }
6528a07d286SRatan Gupta }
6538a07d286SRatan Gupta /**
6548a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6558a07d286SRatan Gupta           json response with the new value.
6568a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6578a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6588a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6598a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6608a07d286SRatan Gupta  */
6618a07d286SRatan Gupta 
6624f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6638a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6648d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6658a07d286SRatan Gupta     const std::string& ldapServerElementName,
6668a07d286SRatan Gupta     const std::string& ldapConfigObject)
6678a07d286SRatan Gupta {
6688a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6698a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
6705e7e2dc5SEd Tanous          serviceAddressList](const boost::system::error_code& ec) {
6718a07d286SRatan Gupta         if (ec)
6728a07d286SRatan Gupta         {
6738a07d286SRatan Gupta             BMCWEB_LOG_DEBUG
674c61704abSGunnar Mills                 << "Error Occurred in updating the service address";
6758a07d286SRatan Gupta             messages::internalError(asyncResp->res);
6768a07d286SRatan Gupta             return;
6778a07d286SRatan Gupta         }
6788a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
6798a07d286SRatan Gupta             serviceAddressList.front()};
680002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
6818a07d286SRatan Gupta             modifiedserviceAddressList;
6828a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
6838a07d286SRatan Gupta         {
684002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
6858a07d286SRatan Gupta                                             serviceAddressList.front());
6868a07d286SRatan Gupta         }
6878a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
6888a07d286SRatan Gupta         },
6898a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
6908a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
691168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
6928a07d286SRatan Gupta }
6938a07d286SRatan Gupta /**
6948a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
6958a07d286SRatan Gupta           json response with the new value.
6968a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
6978a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6988a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6998a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7008a07d286SRatan Gupta  */
7018a07d286SRatan Gupta 
7024f48d5f6SEd Tanous inline void
7034f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
7048d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7058a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7068a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7078a07d286SRatan Gupta {
7088a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7098a07d286SRatan Gupta         [asyncResp, username,
7105e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
7118a07d286SRatan Gupta         if (ec)
7128a07d286SRatan Gupta         {
7136c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
7148a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7158a07d286SRatan Gupta             return;
7168a07d286SRatan Gupta         }
71789492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
71889492a15SPatrick Williams                                 ["Username"] = username;
7198a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
7208a07d286SRatan Gupta         },
7218a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
722168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
723168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
7248a07d286SRatan Gupta }
7258a07d286SRatan Gupta 
7268a07d286SRatan Gupta /**
7278a07d286SRatan Gupta  * @brief updates the LDAP password
7288a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
7298a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7308a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7318a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
7328a07d286SRatan Gupta  */
7338a07d286SRatan Gupta 
7344f48d5f6SEd Tanous inline void
7354f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
7368d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7378a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7388a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7398a07d286SRatan Gupta {
7408a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7418a07d286SRatan Gupta         [asyncResp, password,
7425e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
7438a07d286SRatan Gupta         if (ec)
7448a07d286SRatan Gupta         {
7456c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
7468a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7478a07d286SRatan Gupta             return;
7488a07d286SRatan Gupta         }
74989492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
75089492a15SPatrick Williams                                 ["Password"] = "";
7518a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
7528a07d286SRatan Gupta         },
7538a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7548a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
755168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
7568a07d286SRatan Gupta }
7578a07d286SRatan Gupta 
7588a07d286SRatan Gupta /**
7598a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7608a07d286SRatan Gupta           json response with the new value.
7618a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7628a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7638a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7648a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7658a07d286SRatan Gupta  */
7668a07d286SRatan Gupta 
7674f48d5f6SEd Tanous inline void
7684f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
7698d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7708a07d286SRatan Gupta                       const std::string& ldapServerElementName,
7718a07d286SRatan Gupta                       const std::string& ldapConfigObject)
7728a07d286SRatan Gupta {
7738a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7748a07d286SRatan Gupta         [asyncResp, baseDNList,
7755e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
7768a07d286SRatan Gupta         if (ec)
7778a07d286SRatan Gupta         {
7786c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
7798a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7808a07d286SRatan Gupta             return;
7818a07d286SRatan Gupta         }
782002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
7838a07d286SRatan Gupta         auto& searchSettingsJson =
7848a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
7856c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
7866c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
7878a07d286SRatan Gupta         if (baseDNList.size() > 1)
7888a07d286SRatan Gupta         {
789002d39b4SEd Tanous             messages::propertyValueModified(
790002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
7918a07d286SRatan Gupta         }
7928a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
7938a07d286SRatan Gupta         },
7948a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7958a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
796168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
7978a07d286SRatan Gupta }
7988a07d286SRatan Gupta /**
7998a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
8008a07d286SRatan Gupta           json response with the new value.
8018a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
8028a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8038a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8048a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8058a07d286SRatan Gupta  */
8068a07d286SRatan Gupta 
8074f48d5f6SEd Tanous inline void
8084f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
8098d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8108a07d286SRatan Gupta                             const std::string& ldapServerElementName,
8118a07d286SRatan Gupta                             const std::string& ldapConfigObject)
8128a07d286SRatan Gupta {
8138a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8148a07d286SRatan Gupta         [asyncResp, userNameAttribute,
8155e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
8168a07d286SRatan Gupta         if (ec)
8178a07d286SRatan Gupta         {
818c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8198a07d286SRatan Gupta                                 "username attribute";
8208a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8218a07d286SRatan Gupta             return;
8228a07d286SRatan Gupta         }
823002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8248a07d286SRatan Gupta         auto& searchSettingsJson =
8258a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8268a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
8278a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
8288a07d286SRatan Gupta         },
8298a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8308a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
831168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
8328a07d286SRatan Gupta }
8338a07d286SRatan Gupta /**
8348a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
8358a07d286SRatan Gupta           json response with the new value.
8368a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
8378a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8388a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8398a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8408a07d286SRatan Gupta  */
8418a07d286SRatan Gupta 
8424f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
8438d1b46d7Szhanghch05     const std::string& groupsAttribute,
8448d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8458a07d286SRatan Gupta     const std::string& ldapServerElementName,
8468a07d286SRatan Gupta     const std::string& ldapConfigObject)
8478a07d286SRatan Gupta {
8488a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8498a07d286SRatan Gupta         [asyncResp, groupsAttribute,
8505e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
8518a07d286SRatan Gupta         if (ec)
8528a07d286SRatan Gupta         {
853c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8548a07d286SRatan Gupta                                 "groupname attribute";
8558a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8568a07d286SRatan Gupta             return;
8578a07d286SRatan Gupta         }
858002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8598a07d286SRatan Gupta         auto& searchSettingsJson =
8608a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8618a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
8628a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
8638a07d286SRatan Gupta         },
8648a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8658a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
866168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
8678a07d286SRatan Gupta }
8688a07d286SRatan Gupta /**
8698a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
8708a07d286SRatan Gupta           json response with the new value.
8718a07d286SRatan Gupta  * @param input JSON data.
8728a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8738a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8748a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8758a07d286SRatan Gupta  */
8768a07d286SRatan Gupta 
8774f48d5f6SEd Tanous inline void handleServiceEnablePatch(
8786c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8798a07d286SRatan Gupta     const std::string& ldapServerElementName,
8808a07d286SRatan Gupta     const std::string& ldapConfigObject)
8818a07d286SRatan Gupta {
8828a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8838a07d286SRatan Gupta         [asyncResp, serviceEnabled,
8845e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
8858a07d286SRatan Gupta         if (ec)
8868a07d286SRatan Gupta         {
887002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
8888a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8898a07d286SRatan Gupta             return;
8908a07d286SRatan Gupta         }
8916c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
8928a07d286SRatan Gupta             serviceEnabled;
8936c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
8948a07d286SRatan Gupta         },
8958a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
896168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
897168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
8988a07d286SRatan Gupta }
8998a07d286SRatan Gupta 
9004f48d5f6SEd Tanous inline void
9014f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
9028d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
90378158631SZbigniew Kurzynski {
90478158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
90578158631SZbigniew Kurzynski     std::optional<bool> cookie;
90678158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
90778158631SZbigniew Kurzynski     std::optional<bool> xToken;
908501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
90978158631SZbigniew Kurzynski 
91078158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
91178158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
912501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
91378158631SZbigniew Kurzynski     {
91478158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
91578158631SZbigniew Kurzynski         return;
91678158631SZbigniew Kurzynski     }
91778158631SZbigniew Kurzynski 
91878158631SZbigniew Kurzynski     // Make a copy of methods configuration
91952cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
92052cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
92178158631SZbigniew Kurzynski 
92278158631SZbigniew Kurzynski     if (basicAuth)
92378158631SZbigniew Kurzynski     {
924f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
925f16f6263SAlan Kuo         messages::actionNotSupported(
9260fda0f12SGeorge Liu             asyncResp->res,
9270fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
928f16f6263SAlan Kuo         return;
929f16f6263SAlan Kuo #endif
93078158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
93178158631SZbigniew Kurzynski     }
93278158631SZbigniew Kurzynski 
93378158631SZbigniew Kurzynski     if (cookie)
93478158631SZbigniew Kurzynski     {
935f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
9360fda0f12SGeorge Liu         messages::actionNotSupported(
9370fda0f12SGeorge Liu             asyncResp->res,
9380fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
939f16f6263SAlan Kuo         return;
940f16f6263SAlan Kuo #endif
94178158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
94278158631SZbigniew Kurzynski     }
94378158631SZbigniew Kurzynski 
94478158631SZbigniew Kurzynski     if (sessionToken)
94578158631SZbigniew Kurzynski     {
946f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
947f16f6263SAlan Kuo         messages::actionNotSupported(
9480fda0f12SGeorge Liu             asyncResp->res,
9490fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
950f16f6263SAlan Kuo         return;
951f16f6263SAlan Kuo #endif
95278158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
95378158631SZbigniew Kurzynski     }
95478158631SZbigniew Kurzynski 
95578158631SZbigniew Kurzynski     if (xToken)
95678158631SZbigniew Kurzynski     {
957f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
9580fda0f12SGeorge Liu         messages::actionNotSupported(
9590fda0f12SGeorge Liu             asyncResp->res,
9600fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
961f16f6263SAlan Kuo         return;
962f16f6263SAlan Kuo #endif
96378158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
96478158631SZbigniew Kurzynski     }
96578158631SZbigniew Kurzynski 
966501f1e58SZbigniew Kurzynski     if (tls)
967501f1e58SZbigniew Kurzynski     {
968f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
9690fda0f12SGeorge Liu         messages::actionNotSupported(
9700fda0f12SGeorge Liu             asyncResp->res,
9710fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
972f16f6263SAlan Kuo         return;
973f16f6263SAlan Kuo #endif
974501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
975501f1e58SZbigniew Kurzynski     }
976501f1e58SZbigniew Kurzynski 
97778158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
978501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
979501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
98078158631SZbigniew Kurzynski     {
98178158631SZbigniew Kurzynski         // Do not allow user to disable everything
98278158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
98378158631SZbigniew Kurzynski                                      "of disabling all available methods");
98478158631SZbigniew Kurzynski         return;
98578158631SZbigniew Kurzynski     }
98678158631SZbigniew Kurzynski 
98752cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
98852cc112dSEd Tanous         authMethodsConfig);
98978158631SZbigniew Kurzynski     // Save configuration immediately
99052cc112dSEd Tanous     persistent_data::getConfig().writeData();
99178158631SZbigniew Kurzynski 
99278158631SZbigniew Kurzynski     messages::success(asyncResp->res);
99378158631SZbigniew Kurzynski }
99478158631SZbigniew Kurzynski 
9958a07d286SRatan Gupta /**
9968a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
9978a07d286SRatan Gupta  *        value and create the LDAP config object.
9988a07d286SRatan Gupta  * @param input JSON data
9998a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
10008a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
10018a07d286SRatan Gupta  */
10028a07d286SRatan Gupta 
10036c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
10048d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10058a07d286SRatan Gupta                             const std::string& serverType)
10068a07d286SRatan Gupta {
1007eb2bbe56SRatan Gupta     std::string dbusObjectPath;
1008eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
1009eb2bbe56SRatan Gupta     {
10102c70f800SEd Tanous         dbusObjectPath = adConfigObject;
1011eb2bbe56SRatan Gupta     }
1012eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
1013eb2bbe56SRatan Gupta     {
101423a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
1015eb2bbe56SRatan Gupta     }
1016cb13a392SEd Tanous     else
1017cb13a392SEd Tanous     {
1018cb13a392SEd Tanous         return;
1019cb13a392SEd Tanous     }
1020eb2bbe56SRatan Gupta 
10218a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
10228a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
10238a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
10248a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
10258a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
10268a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
10278a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
10288a07d286SRatan Gupta     std::optional<std::string> userName;
10298a07d286SRatan Gupta     std::optional<std::string> password;
103006785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
10318a07d286SRatan Gupta 
10328a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
10338a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
10348a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
103506785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
103606785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
10378a07d286SRatan Gupta     {
10388a07d286SRatan Gupta         return;
10398a07d286SRatan Gupta     }
10408a07d286SRatan Gupta 
10418a07d286SRatan Gupta     if (authentication)
10428a07d286SRatan Gupta     {
10438a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
10448a07d286SRatan Gupta                                     password);
10458a07d286SRatan Gupta     }
10468a07d286SRatan Gupta     if (ldapService)
10478a07d286SRatan Gupta     {
10488a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10498a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
10508a07d286SRatan Gupta     }
10518a07d286SRatan Gupta     if (serviceAddressList)
10528a07d286SRatan Gupta     {
105326f6976fSEd Tanous         if (serviceAddressList->empty())
10548a07d286SRatan Gupta         {
10558a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10568a07d286SRatan Gupta                                              "ServiceAddress");
10578a07d286SRatan Gupta             return;
10588a07d286SRatan Gupta         }
10598a07d286SRatan Gupta     }
10608a07d286SRatan Gupta     if (baseDNList)
10618a07d286SRatan Gupta     {
106226f6976fSEd Tanous         if (baseDNList->empty())
10638a07d286SRatan Gupta         {
10648a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
10658a07d286SRatan Gupta                                              "BaseDistinguishedNames");
10668a07d286SRatan Gupta             return;
10678a07d286SRatan Gupta         }
10688a07d286SRatan Gupta     }
10698a07d286SRatan Gupta 
10708a07d286SRatan Gupta     // nothing to update, then return
10718a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
107206785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
107306785244SRatan Gupta         !remoteRoleMapData)
10748a07d286SRatan Gupta     {
10758a07d286SRatan Gupta         return;
10768a07d286SRatan Gupta     }
10778a07d286SRatan Gupta 
10788a07d286SRatan Gupta     // Get the existing resource first then keep modifying
10798a07d286SRatan Gupta     // whenever any property gets updated.
1080002d39b4SEd Tanous     getLDAPConfigData(
1081002d39b4SEd Tanous         serverType,
1082002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1083002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1084002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
108523a21a1cSEd Tanous                             const std::string& serverT) {
10868a07d286SRatan Gupta         if (!success)
10878a07d286SRatan Gupta         {
10888a07d286SRatan Gupta             messages::internalError(asyncResp->res);
10898a07d286SRatan Gupta             return;
10908a07d286SRatan Gupta         }
10916c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
10928a07d286SRatan Gupta         if (confData.serviceEnabled)
10938a07d286SRatan Gupta         {
10948a07d286SRatan Gupta             // Disable the service first and update the rest of
10958a07d286SRatan Gupta             // the properties.
10966c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
10978a07d286SRatan Gupta         }
10988a07d286SRatan Gupta 
10998a07d286SRatan Gupta         if (serviceAddressList)
11008a07d286SRatan Gupta         {
11016c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
11026c51eab1SEd Tanous                                       dbusObjectPath);
11038a07d286SRatan Gupta         }
11048a07d286SRatan Gupta         if (userName)
11058a07d286SRatan Gupta         {
11066c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
11078a07d286SRatan Gupta         }
11088a07d286SRatan Gupta         if (password)
11098a07d286SRatan Gupta         {
11106c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
11118a07d286SRatan Gupta         }
11128a07d286SRatan Gupta 
11138a07d286SRatan Gupta         if (baseDNList)
11148a07d286SRatan Gupta         {
11156c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
11168a07d286SRatan Gupta         }
11178a07d286SRatan Gupta         if (userNameAttribute)
11188a07d286SRatan Gupta         {
11196c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
11206c51eab1SEd Tanous                                     dbusObjectPath);
11218a07d286SRatan Gupta         }
11228a07d286SRatan Gupta         if (groupsAttribute)
11238a07d286SRatan Gupta         {
11246c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
11256c51eab1SEd Tanous                                      dbusObjectPath);
11268a07d286SRatan Gupta         }
11278a07d286SRatan Gupta         if (serviceEnabled)
11288a07d286SRatan Gupta         {
11298a07d286SRatan Gupta             // if user has given the value as true then enable
11308a07d286SRatan Gupta             // the service. if user has given false then no-op
11318a07d286SRatan Gupta             // as service is already stopped.
11328a07d286SRatan Gupta             if (*serviceEnabled)
11338a07d286SRatan Gupta             {
11346c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
11356c51eab1SEd Tanous                                          dbusObjectPath);
11368a07d286SRatan Gupta             }
11378a07d286SRatan Gupta         }
11388a07d286SRatan Gupta         else
11398a07d286SRatan Gupta         {
11408a07d286SRatan Gupta             // if user has not given the service enabled value
11418a07d286SRatan Gupta             // then revert it to the same state as it was
11428a07d286SRatan Gupta             // before.
11438a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
114423a21a1cSEd Tanous                                      serverT, dbusObjectPath);
11458a07d286SRatan Gupta         }
114606785244SRatan Gupta 
114706785244SRatan Gupta         if (remoteRoleMapData)
114806785244SRatan Gupta         {
11496c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
11506c51eab1SEd Tanous                                *remoteRoleMapData);
115106785244SRatan Gupta         }
11528a07d286SRatan Gupta         });
11538a07d286SRatan Gupta }
1154d4b5443fSEd Tanous 
11556c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
11566c51eab1SEd Tanous                                  const std::string& username,
1157618c14b4SEd Tanous                                  const std::optional<std::string>& password,
1158618c14b4SEd Tanous                                  const std::optional<bool>& enabled,
1159618c14b4SEd Tanous                                  const std::optional<std::string>& roleId,
1160618c14b4SEd Tanous                                  const std::optional<bool>& locked)
11611abe55efSEd Tanous {
1162b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1163b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1164b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
11656c51eab1SEd Tanous 
11666c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1167618c14b4SEd Tanous         dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
1168618c14b4SEd Tanous                          locked, asyncResp{std::move(asyncResp)}](int rc) {
1169e662eae8SEd Tanous             if (rc <= 0)
11706c51eab1SEd Tanous             {
1171d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11726c51eab1SEd Tanous                                            username);
11736c51eab1SEd Tanous                 return;
11746c51eab1SEd Tanous             }
11756c51eab1SEd Tanous 
11766c51eab1SEd Tanous             if (password)
11776c51eab1SEd Tanous             {
11786c51eab1SEd Tanous                 int retval = pamUpdatePassword(username, *password);
11796c51eab1SEd Tanous 
11806c51eab1SEd Tanous                 if (retval == PAM_USER_UNKNOWN)
11816c51eab1SEd Tanous                 {
1182d8a5d5d8SJiaqing Zhao                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
11836c51eab1SEd Tanous                                                username);
11846c51eab1SEd Tanous                 }
11856c51eab1SEd Tanous                 else if (retval == PAM_AUTHTOK_ERR)
11866c51eab1SEd Tanous                 {
11876c51eab1SEd Tanous                     // If password is invalid
1188618c14b4SEd Tanous                     messages::propertyValueFormatError(asyncResp->res,
1189618c14b4SEd Tanous                                                        *password, "Password");
11906c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
11916c51eab1SEd Tanous                 }
11926c51eab1SEd Tanous                 else if (retval != PAM_SUCCESS)
11936c51eab1SEd Tanous                 {
11946c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
11956c51eab1SEd Tanous                     return;
11966c51eab1SEd Tanous                 }
1197e7b1b62bSEd Tanous                 else
1198e7b1b62bSEd Tanous                 {
1199e7b1b62bSEd Tanous                     messages::success(asyncResp->res);
1200e7b1b62bSEd Tanous                 }
12016c51eab1SEd Tanous             }
12026c51eab1SEd Tanous 
12036c51eab1SEd Tanous             if (enabled)
12046c51eab1SEd Tanous             {
12056c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12065e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
12076c51eab1SEd Tanous                 if (ec)
12086c51eab1SEd Tanous                 {
12096c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12106c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12116c51eab1SEd Tanous                     return;
12126c51eab1SEd Tanous                 }
12136c51eab1SEd Tanous                 messages::success(asyncResp->res);
12146c51eab1SEd Tanous                 return;
12156c51eab1SEd Tanous                     },
1216e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12176c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12186c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserEnabled",
1219168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*enabled});
12206c51eab1SEd Tanous             }
12216c51eab1SEd Tanous 
12226c51eab1SEd Tanous             if (roleId)
12236c51eab1SEd Tanous             {
12246c51eab1SEd Tanous                 std::string priv = getPrivilegeFromRoleId(*roleId);
12256c51eab1SEd Tanous                 if (priv.empty())
12266c51eab1SEd Tanous                 {
12276c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, *roleId,
12286c51eab1SEd Tanous                                                      "RoleId");
12296c51eab1SEd Tanous                     return;
12306c51eab1SEd Tanous                 }
12316c51eab1SEd Tanous 
12326c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12335e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
12346c51eab1SEd Tanous                 if (ec)
12356c51eab1SEd Tanous                 {
12366c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12376c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12386c51eab1SEd Tanous                     return;
12396c51eab1SEd Tanous                 }
12406c51eab1SEd Tanous                 messages::success(asyncResp->res);
12416c51eab1SEd Tanous                     },
1242e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12436c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12446c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1245168e20c1SEd Tanous                     dbus::utility::DbusVariantType{priv});
12466c51eab1SEd Tanous             }
12476c51eab1SEd Tanous 
12486c51eab1SEd Tanous             if (locked)
12496c51eab1SEd Tanous             {
12506c51eab1SEd Tanous                 // admin can unlock the account which is locked by
12516c51eab1SEd Tanous                 // successive authentication failures but admin should
12526c51eab1SEd Tanous                 // not be allowed to lock an account.
12536c51eab1SEd Tanous                 if (*locked)
12546c51eab1SEd Tanous                 {
12556c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, "true",
12566c51eab1SEd Tanous                                                      "Locked");
12576c51eab1SEd Tanous                     return;
12586c51eab1SEd Tanous                 }
12596c51eab1SEd Tanous 
12606c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12615e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
12626c51eab1SEd Tanous                 if (ec)
12636c51eab1SEd Tanous                 {
12646c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12656c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12666c51eab1SEd Tanous                     return;
12676c51eab1SEd Tanous                 }
12686c51eab1SEd Tanous                 messages::success(asyncResp->res);
12696c51eab1SEd Tanous                 return;
12706c51eab1SEd Tanous                     },
1271e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12726c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12736c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes",
1274168e20c1SEd Tanous                     "UserLockedForFailedAttempt",
1275168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*locked});
12766c51eab1SEd Tanous             }
12776c51eab1SEd Tanous         });
12786c51eab1SEd Tanous }
12796c51eab1SEd Tanous 
12804c7d4d33SEd Tanous inline void handleAccountServiceHead(
12814c7d4d33SEd Tanous     App& app, const crow::Request& req,
12821ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12836c51eab1SEd Tanous {
12843ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
128545ca1b86SEd Tanous     {
128645ca1b86SEd Tanous         return;
128745ca1b86SEd Tanous     }
12884c7d4d33SEd Tanous     asyncResp->res.addHeader(
12894c7d4d33SEd Tanous         boost::beast::http::field::link,
12904c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
12914c7d4d33SEd Tanous }
12924c7d4d33SEd Tanous 
12934c7d4d33SEd Tanous inline void
12944c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
12954c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12964c7d4d33SEd Tanous {
1297afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1298afd369c6SJiaqing Zhao     {
1299afd369c6SJiaqing Zhao         return;
1300afd369c6SJiaqing Zhao     }
1301*3e72c202SNinad Palsule 
1302*3e72c202SNinad Palsule     if (req.session == nullptr)
1303*3e72c202SNinad Palsule     {
1304*3e72c202SNinad Palsule         messages::internalError(asyncResp->res);
1305*3e72c202SNinad Palsule         return;
1306*3e72c202SNinad Palsule     }
1307*3e72c202SNinad Palsule 
1308afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1309afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1310afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1311afd369c6SJiaqing Zhao 
131252cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
13131ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
131478158631SZbigniew Kurzynski 
13151476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
13161476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
13171476687dSEd Tanous     json["@odata.type"] = "#AccountService."
13181476687dSEd Tanous                           "v1_10_0.AccountService";
13191476687dSEd Tanous     json["Id"] = "AccountService";
13201476687dSEd Tanous     json["Name"] = "Account Service";
13211476687dSEd Tanous     json["Description"] = "Account Service";
13221476687dSEd Tanous     json["ServiceEnabled"] = true;
13231476687dSEd Tanous     json["MaxPasswordLength"] = 20;
13241ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
13251476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
13261476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
13275b5574acSEd Tanous         "#OpenBMCAccountService.v1_0_0.AccountService";
13281476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
13291476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
13301476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
13311476687dSEd Tanous         authMethodsConfig.basic;
13321476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
13331476687dSEd Tanous         authMethodsConfig.sessionToken;
13341ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
13351ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
13361ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
13371476687dSEd Tanous 
13381ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
13391ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
13401ef4c342SEd Tanous     // permissions ConfigureManager
134172048780SAbhishek Patel     Privileges effectiveUserPrivileges =
1342*3e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
134372048780SAbhishek Patel 
134472048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
134572048780SAbhishek Patel                                          effectiveUserPrivileges))
134672048780SAbhishek Patel     {
13471ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
13481476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
134972048780SAbhishek Patel     }
1350d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1351d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1352d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
13535e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
13541ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
13553d958bbcSAppaRao Puli         if (ec)
13563d958bbcSAppaRao Puli         {
13573d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
13583d958bbcSAppaRao Puli             return;
13593d958bbcSAppaRao Puli         }
1360d1bde9e5SKrzysztof Grobelny 
13613d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
13623d958bbcSAppaRao Puli                          << "properties for AccountService";
1363d1bde9e5SKrzysztof Grobelny 
1364d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1365d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1366d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1367d1bde9e5SKrzysztof Grobelny 
1368d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1369d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1370d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1371d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1372d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1373d1bde9e5SKrzysztof Grobelny 
1374d1bde9e5SKrzysztof Grobelny         if (!success)
13753d958bbcSAppaRao Puli         {
1376d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1377d1bde9e5SKrzysztof Grobelny             return;
13783d958bbcSAppaRao Puli         }
1379d1bde9e5SKrzysztof Grobelny 
1380d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
13813d958bbcSAppaRao Puli         {
1382d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
13833d958bbcSAppaRao Puli         }
1384d1bde9e5SKrzysztof Grobelny 
1385d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
13863d958bbcSAppaRao Puli         {
1387d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1388d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1389d1bde9e5SKrzysztof Grobelny         }
1390d1bde9e5SKrzysztof Grobelny 
1391d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
13923d958bbcSAppaRao Puli         {
1393002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1394d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
13953d958bbcSAppaRao Puli         }
1396d1bde9e5SKrzysztof Grobelny         });
13976973a582SRatan Gupta 
139802cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1399ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1400cb13a392SEd Tanous         if (!success)
1401cb13a392SEd Tanous         {
1402cb13a392SEd Tanous             return;
1403cb13a392SEd Tanous         }
1404002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1405ab828d7cSRatan Gupta     };
1406ab828d7cSRatan Gupta 
1407ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1408ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
14091ef4c342SEd Tanous }
14106973a582SRatan Gupta 
14111ef4c342SEd Tanous inline void handleAccountServicePatch(
14121ef4c342SEd Tanous     App& app, const crow::Request& req,
14131ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14141ef4c342SEd Tanous {
14153ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
141645ca1b86SEd Tanous     {
141745ca1b86SEd Tanous         return;
141845ca1b86SEd Tanous     }
1419f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1420f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1421ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1422f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1423f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1424f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1425f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1426f5ffd806SEd Tanous 
142715ed6780SWilly Tu     if (!json_util::readJsonPatch(
14281ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
14291ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
14301ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
14311ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1432f5ffd806SEd Tanous             oemObject))
1433f5ffd806SEd Tanous     {
1434f5ffd806SEd Tanous         return;
1435f5ffd806SEd Tanous     }
1436f5ffd806SEd Tanous 
1437f5ffd806SEd Tanous     if (minPasswordLength)
1438f5ffd806SEd Tanous     {
1439ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
14405e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1441ef73ad0dSPaul Fertser             if (ec)
1442ef73ad0dSPaul Fertser             {
1443ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1444ef73ad0dSPaul Fertser                 return;
1445ef73ad0dSPaul Fertser             }
1446ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1447ef73ad0dSPaul Fertser             },
14481ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1449ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
14501ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1451ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1452f5ffd806SEd Tanous     }
1453f5ffd806SEd Tanous 
1454f5ffd806SEd Tanous     if (maxPasswordLength)
1455f5ffd806SEd Tanous     {
14561ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1457f5ffd806SEd Tanous     }
1458f5ffd806SEd Tanous 
1459f5ffd806SEd Tanous     if (ldapObject)
1460f5ffd806SEd Tanous     {
1461f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1462f5ffd806SEd Tanous     }
1463f5ffd806SEd Tanous 
1464f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
14651ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1466f5ffd806SEd Tanous                                          oemOpenBMCObject))
1467f5ffd806SEd Tanous     {
1468f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1469f5ffd806SEd Tanous             oemOpenBMCObject &&
1470f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1471f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1472f5ffd806SEd Tanous         {
1473f5ffd806SEd Tanous             if (authMethodsObject)
1474f5ffd806SEd Tanous             {
14751ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1476f5ffd806SEd Tanous             }
1477f5ffd806SEd Tanous         }
1478f5ffd806SEd Tanous     }
1479f5ffd806SEd Tanous 
1480f5ffd806SEd Tanous     if (activeDirectoryObject)
1481f5ffd806SEd Tanous     {
14821ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1483f5ffd806SEd Tanous     }
1484f5ffd806SEd Tanous 
1485f5ffd806SEd Tanous     if (unlockTimeout)
1486f5ffd806SEd Tanous     {
1487f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
14885e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1489f5ffd806SEd Tanous             if (ec)
1490f5ffd806SEd Tanous             {
1491f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1492f5ffd806SEd Tanous                 return;
1493f5ffd806SEd Tanous             }
1494f5ffd806SEd Tanous             messages::success(asyncResp->res);
1495f5ffd806SEd Tanous             },
14961ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1497f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
14981ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1499168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1500f5ffd806SEd Tanous     }
1501f5ffd806SEd Tanous     if (lockoutThreshold)
1502f5ffd806SEd Tanous     {
1503f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
15045e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1505f5ffd806SEd Tanous             if (ec)
1506f5ffd806SEd Tanous             {
1507f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1508f5ffd806SEd Tanous                 return;
1509f5ffd806SEd Tanous             }
1510f5ffd806SEd Tanous             messages::success(asyncResp->res);
1511f5ffd806SEd Tanous             },
15121ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1513f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1514f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1515f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1516168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1517f5ffd806SEd Tanous     }
15181ef4c342SEd Tanous }
1519f5ffd806SEd Tanous 
15204c7d4d33SEd Tanous inline void handleAccountCollectionHead(
15211ef4c342SEd Tanous     App& app, const crow::Request& req,
15221ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15231ef4c342SEd Tanous {
15243ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
152545ca1b86SEd Tanous     {
152645ca1b86SEd Tanous         return;
152745ca1b86SEd Tanous     }
15284c7d4d33SEd Tanous     asyncResp->res.addHeader(
15294c7d4d33SEd Tanous         boost::beast::http::field::link,
15304c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
15314c7d4d33SEd Tanous }
15324c7d4d33SEd Tanous 
15334c7d4d33SEd Tanous inline void handleAccountCollectionGet(
15344c7d4d33SEd Tanous     App& app, const crow::Request& req,
15354c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15364c7d4d33SEd Tanous {
1537afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1538afd369c6SJiaqing Zhao     {
1539afd369c6SJiaqing Zhao         return;
1540afd369c6SJiaqing Zhao     }
1541*3e72c202SNinad Palsule 
1542*3e72c202SNinad Palsule     if (req.session == nullptr)
1543*3e72c202SNinad Palsule     {
1544*3e72c202SNinad Palsule         messages::internalError(asyncResp->res);
1545*3e72c202SNinad Palsule         return;
1546*3e72c202SNinad Palsule     }
1547*3e72c202SNinad Palsule 
1548afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1549afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1550afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
15511476687dSEd Tanous 
15521476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
15531476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
15541ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
15551476687dSEd Tanous                                               "ManagerAccountCollection";
15561476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
15571476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
15580f74e643SEd Tanous 
15596c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
1560*3e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
15616c51eab1SEd Tanous 
1562f5e29f33SJunLin Chen     std::string thisUser;
1563f5e29f33SJunLin Chen     if (req.session)
1564f5e29f33SJunLin Chen     {
1565f5e29f33SJunLin Chen         thisUser = req.session->username;
1566f5e29f33SJunLin Chen     }
1567b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
1568cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
15695e7e2dc5SEd Tanous             const boost::system::error_code& ec,
1570711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1571b9b2e0b2SEd Tanous         if (ec)
1572b9b2e0b2SEd Tanous         {
1573f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1574b9b2e0b2SEd Tanous             return;
1575b9b2e0b2SEd Tanous         }
1576b9b2e0b2SEd Tanous 
1577cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1578002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1579cef1ddfbSEd Tanous 
1580cef1ddfbSEd Tanous         bool userCanSeeSelf =
1581002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1582cef1ddfbSEd Tanous 
1583002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1584b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1585b9b2e0b2SEd Tanous 
15869eb808c1SEd Tanous         for (const auto& userpath : users)
1587b9b2e0b2SEd Tanous         {
15882dfd18efSEd Tanous             std::string user = userpath.first.filename();
15892dfd18efSEd Tanous             if (user.empty())
1590b9b2e0b2SEd Tanous             {
15912dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
15922dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
15932dfd18efSEd Tanous 
15942dfd18efSEd Tanous                 return;
1595b9b2e0b2SEd Tanous             }
1596f365910cSGunnar Mills 
1597f365910cSGunnar Mills             // As clarified by Redfish here:
1598f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
15996c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
16006c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
16016c51eab1SEd Tanous             // accounts.
16021ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1603f365910cSGunnar Mills             {
16041476687dSEd Tanous                 nlohmann::json::object_t member;
160589492a15SPatrick Williams                 member["@odata.id"] = "/redfish/v1/AccountService/Accounts/" +
160689492a15SPatrick Williams                                       user;
1607b2ba3072SPatrick Williams                 memberArray.emplace_back(std::move(member));
1608b9b2e0b2SEd Tanous             }
1609f365910cSGunnar Mills         }
16101ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1611b9b2e0b2SEd Tanous         },
16121ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1613b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16141ef4c342SEd Tanous }
16156c51eab1SEd Tanous 
161697e90da3SNinad Palsule inline void processAfterCreateUser(
161797e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
161897e90da3SNinad Palsule     const std::string& username, const std::string& password,
161997e90da3SNinad Palsule     const boost::system::error_code& ec, sdbusplus::message_t& m)
162097e90da3SNinad Palsule {
162197e90da3SNinad Palsule     if (ec)
162297e90da3SNinad Palsule     {
162397e90da3SNinad Palsule         userErrorMessageHandler(m.get_error(), asyncResp, username, "");
162497e90da3SNinad Palsule         return;
162597e90da3SNinad Palsule     }
162697e90da3SNinad Palsule 
162797e90da3SNinad Palsule     if (pamUpdatePassword(username, password) != PAM_SUCCESS)
162897e90da3SNinad Palsule     {
162997e90da3SNinad Palsule         // At this point we have a user that's been
163097e90da3SNinad Palsule         // created, but the password set
163197e90da3SNinad Palsule         // failed.Something is wrong, so delete the user
163297e90da3SNinad Palsule         // that we've already created
163397e90da3SNinad Palsule         sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
163497e90da3SNinad Palsule         tempObjPath /= username;
163597e90da3SNinad Palsule         const std::string userPath(tempObjPath);
163697e90da3SNinad Palsule 
163797e90da3SNinad Palsule         crow::connections::systemBus->async_method_call(
163897e90da3SNinad Palsule             [asyncResp, password](const boost::system::error_code& ec3) {
163997e90da3SNinad Palsule             if (ec3)
164097e90da3SNinad Palsule             {
164197e90da3SNinad Palsule                 messages::internalError(asyncResp->res);
164297e90da3SNinad Palsule                 return;
164397e90da3SNinad Palsule             }
164497e90da3SNinad Palsule 
164597e90da3SNinad Palsule             // If password is invalid
164697e90da3SNinad Palsule             messages::propertyValueFormatError(asyncResp->res, password,
164797e90da3SNinad Palsule                                                "Password");
164897e90da3SNinad Palsule             },
164997e90da3SNinad Palsule             "xyz.openbmc_project.User.Manager", userPath,
165097e90da3SNinad Palsule             "xyz.openbmc_project.Object.Delete", "Delete");
165197e90da3SNinad Palsule 
165297e90da3SNinad Palsule         BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
165397e90da3SNinad Palsule         return;
165497e90da3SNinad Palsule     }
165597e90da3SNinad Palsule 
165697e90da3SNinad Palsule     messages::created(asyncResp->res);
165797e90da3SNinad Palsule     asyncResp->res.addHeader("Location",
165897e90da3SNinad Palsule                              "/redfish/v1/AccountService/Accounts/" + username);
165997e90da3SNinad Palsule }
166097e90da3SNinad Palsule 
166197e90da3SNinad Palsule inline void processAfterGetAllGroups(
166297e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
166397e90da3SNinad Palsule     const std::string& username, const std::string& password,
166497e90da3SNinad Palsule     const std::optional<std::string>& roleId, std::optional<bool> enabled,
166597e90da3SNinad Palsule     const std::vector<std::string>& allGroupsList)
166697e90da3SNinad Palsule 
166797e90da3SNinad Palsule {
1668*3e72c202SNinad Palsule     std::vector<std::string> userGroups;
1669*3e72c202SNinad Palsule     for (const auto& grp : allGroupsList)
1670*3e72c202SNinad Palsule     {
1671*3e72c202SNinad Palsule         // Console access is provided to the user who is a member of
1672*3e72c202SNinad Palsule         // hostconsole group and has a administrator role. So, set
1673*3e72c202SNinad Palsule         // hostconsole group only for the administrator.
1674*3e72c202SNinad Palsule         if ((grp != "hostconsole") || (roleId == "priv-admin"))
1675*3e72c202SNinad Palsule         {
1676*3e72c202SNinad Palsule             userGroups.emplace_back(grp);
1677*3e72c202SNinad Palsule         }
1678*3e72c202SNinad Palsule     }
1679*3e72c202SNinad Palsule 
168097e90da3SNinad Palsule     crow::connections::systemBus->async_method_call(
168197e90da3SNinad Palsule         [asyncResp, username, password](const boost::system::error_code& ec2,
168297e90da3SNinad Palsule                                         sdbusplus::message_t& m) {
168397e90da3SNinad Palsule         processAfterCreateUser(asyncResp, username, password, ec2, m);
168497e90da3SNinad Palsule         },
168597e90da3SNinad Palsule         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1686*3e72c202SNinad Palsule         "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
1687*3e72c202SNinad Palsule         *roleId, *enabled);
168897e90da3SNinad Palsule }
168997e90da3SNinad Palsule 
16901ef4c342SEd Tanous inline void handleAccountCollectionPost(
16911ef4c342SEd Tanous     App& app, const crow::Request& req,
16921ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
16931ef4c342SEd Tanous {
16943ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
169545ca1b86SEd Tanous     {
169645ca1b86SEd Tanous         return;
169745ca1b86SEd Tanous     }
16989712f8acSEd Tanous     std::string username;
16999712f8acSEd Tanous     std::string password;
1700a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1701a24526dcSEd Tanous     std::optional<bool> enabled = true;
17021ef4c342SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
17031ef4c342SEd Tanous                                   "Password", password, "RoleId", roleId,
17041ef4c342SEd Tanous                                   "Enabled", enabled))
170504ae99ecSEd Tanous     {
170604ae99ecSEd Tanous         return;
170704ae99ecSEd Tanous     }
170804ae99ecSEd Tanous 
170954fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
171084e12cb7SAppaRao Puli     if (priv.empty())
171104ae99ecSEd Tanous     {
17121ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
171304ae99ecSEd Tanous         return;
171404ae99ecSEd Tanous     }
17159712f8acSEd Tanous     roleId = priv;
171604ae99ecSEd Tanous 
1717599c71d8SAyushi Smriti     // Reading AllGroups property
17181e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
17191ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
17201ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
17211ef4c342SEd Tanous         "AllGroups",
1722599c71d8SAyushi Smriti         [asyncResp, username, password{std::move(password)}, roleId,
17235e7e2dc5SEd Tanous          enabled](const boost::system::error_code& ec,
17241e1e598dSJonathan Doman                   const std::vector<std::string>& allGroupsList) {
1725599c71d8SAyushi Smriti         if (ec)
1726599c71d8SAyushi Smriti         {
1727599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1728599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1729599c71d8SAyushi Smriti             return;
1730599c71d8SAyushi Smriti         }
1731599c71d8SAyushi Smriti 
17321e1e598dSJonathan Doman         if (allGroupsList.empty())
1733599c71d8SAyushi Smriti         {
1734599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1735599c71d8SAyushi Smriti             return;
1736599c71d8SAyushi Smriti         }
1737599c71d8SAyushi Smriti 
173897e90da3SNinad Palsule         processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
173997e90da3SNinad Palsule                                  allGroupsList);
17401e1e598dSJonathan Doman         });
17411ef4c342SEd Tanous }
1742b9b2e0b2SEd Tanous 
17431ef4c342SEd Tanous inline void
17444c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
17456c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17464c7d4d33SEd Tanous                       const std::string& /*accountName*/)
17471ef4c342SEd Tanous {
17483ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
174945ca1b86SEd Tanous     {
175045ca1b86SEd Tanous         return;
175145ca1b86SEd Tanous     }
17524c7d4d33SEd Tanous     asyncResp->res.addHeader(
17534c7d4d33SEd Tanous         boost::beast::http::field::link,
17544c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
17554c7d4d33SEd Tanous }
1756afd369c6SJiaqing Zhao 
17574c7d4d33SEd Tanous inline void
17584c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
17594c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17604c7d4d33SEd Tanous                      const std::string& accountName)
17614c7d4d33SEd Tanous {
1762afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1763afd369c6SJiaqing Zhao     {
1764afd369c6SJiaqing Zhao         return;
1765afd369c6SJiaqing Zhao     }
1766afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1767afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1768afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1769afd369c6SJiaqing Zhao 
17701ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1771031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1772d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1773031514fbSJunLin Chen     return;
17741ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1775afd369c6SJiaqing Zhao 
1776031514fbSJunLin Chen     if (req.session == nullptr)
1777031514fbSJunLin Chen     {
1778031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1779031514fbSJunLin Chen         return;
1780031514fbSJunLin Chen     }
17816c51eab1SEd Tanous     if (req.session->username != accountName)
1782b9b2e0b2SEd Tanous     {
17836c51eab1SEd Tanous         // At this point we've determined that the user is trying to
17841ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
17851ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
17861ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
17876c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
1788*3e72c202SNinad Palsule             redfish::getUserPrivileges(*req.session);
17891ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
17901ef4c342SEd Tanous                                                          "ConfigureManager"};
17916c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
17926c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1793900f9497SJoseph Reynolds         {
1794900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
1795900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1796900f9497SJoseph Reynolds             return;
1797900f9497SJoseph Reynolds         }
1798900f9497SJoseph Reynolds     }
1799900f9497SJoseph Reynolds 
1800b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
18011ef4c342SEd Tanous         [asyncResp,
18025e7e2dc5SEd Tanous          accountName](const boost::system::error_code& ec,
1803711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1804b9b2e0b2SEd Tanous         if (ec)
1805b9b2e0b2SEd Tanous         {
1806f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1807b9b2e0b2SEd Tanous             return;
1808b9b2e0b2SEd Tanous         }
1809711ac7a9SEd Tanous         const auto userIt = std::find_if(
1810b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
1811b477fd44SP Dheeraj Srujan Kumar             [accountName](
1812b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
1813002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
181455f79e6fSEd Tanous             return accountName == user.first.filename();
1815b477fd44SP Dheeraj Srujan Kumar             });
1816b9b2e0b2SEd Tanous 
181784e12cb7SAppaRao Puli         if (userIt == users.end())
1818b9b2e0b2SEd Tanous         {
1819002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1820002d39b4SEd Tanous                                        accountName);
182184e12cb7SAppaRao Puli             return;
182284e12cb7SAppaRao Puli         }
18234e68c45bSAyushi Smriti 
18241476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
18251476687dSEd Tanous             "#ManagerAccount.v1_4_0.ManagerAccount";
18261476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
18271476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
18281476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
18294e68c45bSAyushi Smriti 
183084e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
183165b0dc32SEd Tanous         {
1832002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
183365b0dc32SEd Tanous             {
183465b0dc32SEd Tanous                 for (const auto& property : interface.second)
183565b0dc32SEd Tanous                 {
183665b0dc32SEd Tanous                     if (property.first == "UserEnabled")
183765b0dc32SEd Tanous                     {
183865b0dc32SEd Tanous                         const bool* userEnabled =
1839abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
184065b0dc32SEd Tanous                         if (userEnabled == nullptr)
184165b0dc32SEd Tanous                         {
1842002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
184384e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
184484e12cb7SAppaRao Puli                             return;
184565b0dc32SEd Tanous                         }
1846002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
184765b0dc32SEd Tanous                     }
1848002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
184965b0dc32SEd Tanous                     {
185065b0dc32SEd Tanous                         const bool* userLocked =
1851abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
185265b0dc32SEd Tanous                         if (userLocked == nullptr)
185365b0dc32SEd Tanous                         {
185484e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
185584e12cb7SAppaRao Puli                                                 "ailedAttempt "
185684e12cb7SAppaRao Puli                                                 "wasn't a bool";
185784e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
185884e12cb7SAppaRao Puli                             return;
185965b0dc32SEd Tanous                         }
1860002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
1861002d39b4SEd Tanous                         asyncResp->res
1862002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
18633bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
186465b0dc32SEd Tanous                     }
186584e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
186684e12cb7SAppaRao Puli                     {
186754fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1868002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
186954fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
187084e12cb7SAppaRao Puli                         {
1871002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
187284e12cb7SAppaRao Puli                                                 "string";
187384e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
187484e12cb7SAppaRao Puli                             return;
187584e12cb7SAppaRao Puli                         }
18761ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
187754fc587aSNagaraju Goruganti                         if (role.empty())
187884e12cb7SAppaRao Puli                         {
187984e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
188084e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
188184e12cb7SAppaRao Puli                             return;
188284e12cb7SAppaRao Puli                         }
188354fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
188484e12cb7SAppaRao Puli 
18851476687dSEd Tanous                         nlohmann::json& roleEntry =
1886002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
18871476687dSEd Tanous                         roleEntry["@odata.id"] =
1888002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
188984e12cb7SAppaRao Puli                     }
1890002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
18913bf4e632SJoseph Reynolds                     {
18923bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
18933bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
18943bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
18953bf4e632SJoseph Reynolds                         {
18960fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
18970fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
18983bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
18993bf4e632SJoseph Reynolds                             return;
19003bf4e632SJoseph Reynolds                         }
1901002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
19023bf4e632SJoseph Reynolds                             *userPasswordExpired;
19033bf4e632SJoseph Reynolds                     }
1904c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
1905c7229815SAbhishek Patel                     {
1906c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
1907c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
1908c7229815SAbhishek Patel                                 &property.second);
1909c7229815SAbhishek Patel                         if (userGroups == nullptr)
1910c7229815SAbhishek Patel                         {
1911c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR
1912c7229815SAbhishek Patel                                 << "userGroups wasn't a string vector";
1913c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1914c7229815SAbhishek Patel                             return;
1915c7229815SAbhishek Patel                         }
1916c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
1917c7229815SAbhishek Patel                         {
1918c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR << "userGroups mapping failed";
1919c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1920c7229815SAbhishek Patel                             return;
1921c7229815SAbhishek Patel                         }
1922c7229815SAbhishek Patel                     }
192365b0dc32SEd Tanous                 }
192465b0dc32SEd Tanous             }
192565b0dc32SEd Tanous         }
192665b0dc32SEd Tanous 
1927b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
192884e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
1929b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
1930b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
1931b9b2e0b2SEd Tanous         },
19321ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1933b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19341ef4c342SEd Tanous }
1935a840879dSEd Tanous 
19361ef4c342SEd Tanous inline void
193720fc307fSGunnar Mills     handleAccountDelete(App& app, const crow::Request& req,
19386c51eab1SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19391ef4c342SEd Tanous                         const std::string& username)
19401ef4c342SEd Tanous {
19413ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
194245ca1b86SEd Tanous     {
194345ca1b86SEd Tanous         return;
194445ca1b86SEd Tanous     }
19451ef4c342SEd Tanous 
19461ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1947031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1948d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1949031514fbSJunLin Chen     return;
1950031514fbSJunLin Chen 
19511ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
19521ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
19531ef4c342SEd Tanous     tempObjPath /= username;
19541ef4c342SEd Tanous     const std::string userPath(tempObjPath);
19551ef4c342SEd Tanous 
19561ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
19575e7e2dc5SEd Tanous         [asyncResp, username](const boost::system::error_code& ec) {
19581ef4c342SEd Tanous         if (ec)
19591ef4c342SEd Tanous         {
1960d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
19611ef4c342SEd Tanous                                        username);
19621ef4c342SEd Tanous             return;
19631ef4c342SEd Tanous         }
19641ef4c342SEd Tanous 
19651ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
19661ef4c342SEd Tanous         },
19671ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
19681ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
19691ef4c342SEd Tanous }
19701ef4c342SEd Tanous 
19711ef4c342SEd Tanous inline void
19721ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
19731ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19741ef4c342SEd Tanous                        const std::string& username)
19751ef4c342SEd Tanous {
19761ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19771ef4c342SEd Tanous     {
19781ef4c342SEd Tanous         return;
19791ef4c342SEd Tanous     }
19801ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
19811ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
1982d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
19831ef4c342SEd Tanous     return;
19841ef4c342SEd Tanous 
19851ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1986a24526dcSEd Tanous     std::optional<std::string> newUserName;
1987a24526dcSEd Tanous     std::optional<std::string> password;
1988a24526dcSEd Tanous     std::optional<bool> enabled;
1989a24526dcSEd Tanous     std::optional<std::string> roleId;
199024c8542dSRatan Gupta     std::optional<bool> locked;
1991e9cc5172SEd Tanous 
1992031514fbSJunLin Chen     if (req.session == nullptr)
1993031514fbSJunLin Chen     {
1994031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1995031514fbSJunLin Chen         return;
1996031514fbSJunLin Chen     }
1997031514fbSJunLin Chen 
1998e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
1999*3e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
2000e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
2001e9cc5172SEd Tanous     bool userHasConfigureUsers =
2002e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
2003e9cc5172SEd Tanous     if (userHasConfigureUsers)
2004e9cc5172SEd Tanous     {
2005e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
20061ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "UserName",
20071ef4c342SEd Tanous                                       newUserName, "Password", password,
20081ef4c342SEd Tanous                                       "RoleId", roleId, "Enabled", enabled,
20091ef4c342SEd Tanous                                       "Locked", locked))
2010a840879dSEd Tanous         {
2011a840879dSEd Tanous             return;
2012a840879dSEd Tanous         }
2013e9cc5172SEd Tanous     }
2014e9cc5172SEd Tanous     else
2015900f9497SJoseph Reynolds     {
2016e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
2017e9cc5172SEd Tanous         if (username != req.session->username)
2018900f9497SJoseph Reynolds         {
2019900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
2020900f9497SJoseph Reynolds             return;
2021900f9497SJoseph Reynolds         }
2022031514fbSJunLin Chen 
2023e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
20241ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
20251ef4c342SEd Tanous                                       password))
2026e9cc5172SEd Tanous         {
2027e9cc5172SEd Tanous             return;
2028e9cc5172SEd Tanous         }
2029900f9497SJoseph Reynolds     }
2030900f9497SJoseph Reynolds 
203166b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
20326c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
20336c51eab1SEd Tanous     // updating user properties other then username. If username
20346c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
20356c51eab1SEd Tanous     // user rename request.
203666b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
2037a840879dSEd Tanous     {
20381ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
20391ef4c342SEd Tanous                              locked);
204084e12cb7SAppaRao Puli         return;
204184e12cb7SAppaRao Puli     }
204284e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
20436c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
20441ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
20455e7e2dc5SEd Tanous          locked](const boost::system::error_code& ec, sdbusplus::message_t& m) {
204684e12cb7SAppaRao Puli         if (ec)
204784e12cb7SAppaRao Puli         {
2048002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2049002d39b4SEd Tanous                                     username);
2050a840879dSEd Tanous             return;
2051a840879dSEd Tanous         }
2052a840879dSEd Tanous 
2053002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
2054002d39b4SEd Tanous                              locked);
205584e12cb7SAppaRao Puli         },
20561ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
205784e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
205884e12cb7SAppaRao Puli         *newUserName);
20591ef4c342SEd Tanous }
20601ef4c342SEd Tanous 
20611ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
20621ef4c342SEd Tanous {
20631ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20644c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
20654c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20664c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
20674c7d4d33SEd Tanous 
20684c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20691ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
20701ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20711ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
20721ef4c342SEd Tanous 
20731ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
20741ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
20751ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
20761ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
20771ef4c342SEd Tanous 
20781ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20794c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
20804c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20814c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
20824c7d4d33SEd Tanous 
20834c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20841ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
20851ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
20861ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
20871ef4c342SEd Tanous 
20881ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
20891ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
20901ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
20911ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
20921ef4c342SEd Tanous 
20931ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20944c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
20954c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
20964c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
20974c7d4d33SEd Tanous 
20984c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
20991ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
21001ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
21011ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
21021ef4c342SEd Tanous 
21031ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
21041ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
21051ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
21061ef4c342SEd Tanous         // yet
21071ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
21081ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
21091ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
211084e12cb7SAppaRao Puli 
21116c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2112ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
21136c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
211420fc307fSGunnar Mills             std::bind_front(handleAccountDelete, std::ref(app)));
211506e086d9SEd Tanous }
211688d16c9aSLewanczyk, Dawid 
211788d16c9aSLewanczyk, Dawid } // namespace redfish
2118