xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 25e055a38495ce7d08e551a365e47e6b6c9bf7b1)
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         }
1403e72c202SNinad Palsule         else if (userGroup == "hostconsole")
1413e72c202SNinad Palsule         {
1423e72c202SNinad Palsule             // The hostconsole group controls who can access the host console
1433e72c202SNinad Palsule             // port via ssh and websocket.
1443e72c202SNinad Palsule             accountTypes.emplace_back("HostConsole");
1453e72c202SNinad 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,
320*25e055a3SRavi Teja                          remoteGroup](const boost::system::error_code& ec,
321*25e055a3SRavi Teja                                       const sdbusplus::message::message& msg) {
32206785244SRatan Gupta                         if (ec)
32306785244SRatan Gupta                         {
324*25e055a3SRavi Teja                             const sd_bus_error* dbusError = msg.get_error();
325*25e055a3SRavi Teja                             if ((dbusError != nullptr) &&
326*25e055a3SRavi Teja                                 (dbusError->name ==
327*25e055a3SRavi Teja                                  std::string_view(
328*25e055a3SRavi Teja                                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
329*25e055a3SRavi Teja                             {
330*25e055a3SRavi Teja                                 BMCWEB_LOG_WARNING << "DBUS response error: "
331*25e055a3SRavi Teja                                                    << ec;
332*25e055a3SRavi Teja                                 messages::propertyValueIncorrect(asyncResp->res,
333*25e055a3SRavi Teja                                                                  "RemoteGroup",
334*25e055a3SRavi Teja                                                                  *remoteGroup);
335*25e055a3SRavi Teja                                 return;
336*25e055a3SRavi Teja                             }
33706785244SRatan Gupta                             messages::internalError(asyncResp->res);
33806785244SRatan Gupta                             return;
33906785244SRatan Gupta                         }
34006785244SRatan Gupta                         asyncResp->res
341002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
342002d39b4SEd Tanous                                       ["RemoteGroup"] = *remoteGroup;
34306785244SRatan Gupta                         },
34406785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
34506785244SRatan Gupta                         propertyInterface, "Set",
34606785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
34706785244SRatan Gupta                         "GroupName",
348168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
349168e20c1SEd Tanous                             std::move(*remoteGroup)));
35006785244SRatan Gupta                 }
35106785244SRatan Gupta 
35206785244SRatan Gupta                 // If "LocalRole" info is provided
35306785244SRatan Gupta                 if (localRole)
35406785244SRatan Gupta                 {
35506785244SRatan Gupta                     crow::connections::systemBus->async_method_call(
35606785244SRatan Gupta                         [asyncResp, roleMapObjData, serverType, index,
357*25e055a3SRavi Teja                          localRole](const boost::system::error_code& ec,
358*25e055a3SRavi Teja                                     const sdbusplus::message::message& msg) {
35906785244SRatan Gupta                         if (ec)
36006785244SRatan Gupta                         {
361*25e055a3SRavi Teja                             const sd_bus_error* dbusError = msg.get_error();
362*25e055a3SRavi Teja                             if ((dbusError != nullptr) &&
363*25e055a3SRavi Teja                                 (dbusError->name ==
364*25e055a3SRavi Teja                                  std::string_view(
365*25e055a3SRavi Teja                                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
366*25e055a3SRavi Teja                             {
367*25e055a3SRavi Teja                                 BMCWEB_LOG_WARNING << "DBUS response error: "
368*25e055a3SRavi Teja                                                    << ec;
369*25e055a3SRavi Teja                                 messages::propertyValueIncorrect(
370*25e055a3SRavi Teja                                     asyncResp->res, "LocalRole", *localRole);
371*25e055a3SRavi Teja                                 return;
372*25e055a3SRavi Teja                             }
37306785244SRatan Gupta                             messages::internalError(asyncResp->res);
37406785244SRatan Gupta                             return;
37506785244SRatan Gupta                         }
37606785244SRatan Gupta                         asyncResp->res
377002d39b4SEd Tanous                             .jsonValue[serverType]["RemoteRoleMapping"][index]
378002d39b4SEd Tanous                                       ["LocalRole"] = *localRole;
37906785244SRatan Gupta                         },
38006785244SRatan Gupta                         ldapDbusService, roleMapObjData[index].first,
38106785244SRatan Gupta                         propertyInterface, "Set",
38206785244SRatan Gupta                         "xyz.openbmc_project.User.PrivilegeMapperEntry",
38306785244SRatan Gupta                         "Privilege",
384168e20c1SEd Tanous                         dbus::utility::DbusVariantType(
38506785244SRatan Gupta                             getPrivilegeFromRoleId(std::move(*localRole))));
38606785244SRatan Gupta                 }
38706785244SRatan Gupta             }
38806785244SRatan Gupta             // Create a new RoleMapping Object.
38906785244SRatan Gupta             else
39006785244SRatan Gupta             {
39106785244SRatan Gupta                 BMCWEB_LOG_DEBUG
39206785244SRatan Gupta                     << "setRoleMappingProperties: Creating new Object";
39389492a15SPatrick Williams                 std::string pathString = "RemoteRoleMapping/" +
39489492a15SPatrick Williams                                          std::to_string(index);
39506785244SRatan Gupta 
39606785244SRatan Gupta                 if (!localRole)
39706785244SRatan Gupta                 {
39806785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
39906785244SRatan Gupta                                               pathString + "/LocalRole");
40006785244SRatan Gupta                     continue;
40106785244SRatan Gupta                 }
40206785244SRatan Gupta                 if (!remoteGroup)
40306785244SRatan Gupta                 {
40406785244SRatan Gupta                     messages::propertyMissing(asyncResp->res,
40506785244SRatan Gupta                                               pathString + "/RemoteGroup");
40606785244SRatan Gupta                     continue;
40706785244SRatan Gupta                 }
40806785244SRatan Gupta 
40906785244SRatan Gupta                 std::string dbusObjectPath;
41006785244SRatan Gupta                 if (serverType == "ActiveDirectory")
41106785244SRatan Gupta                 {
4122c70f800SEd Tanous                     dbusObjectPath = adConfigObject;
41306785244SRatan Gupta                 }
41406785244SRatan Gupta                 else if (serverType == "LDAP")
41506785244SRatan Gupta                 {
41623a21a1cSEd Tanous                     dbusObjectPath = ldapConfigObjectName;
41706785244SRatan Gupta                 }
41806785244SRatan Gupta 
41906785244SRatan Gupta                 BMCWEB_LOG_DEBUG << "Remote Group=" << *remoteGroup
42006785244SRatan Gupta                                  << ",LocalRole=" << *localRole;
42106785244SRatan Gupta 
42206785244SRatan Gupta                 crow::connections::systemBus->async_method_call(
423271584abSEd Tanous                     [asyncResp, serverType, localRole,
4245e7e2dc5SEd Tanous                      remoteGroup](const boost::system::error_code& ec) {
42506785244SRatan Gupta                     if (ec)
42606785244SRatan Gupta                     {
42706785244SRatan Gupta                         BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
42806785244SRatan Gupta                         messages::internalError(asyncResp->res);
42906785244SRatan Gupta                         return;
43006785244SRatan Gupta                     }
43106785244SRatan Gupta                     nlohmann::json& remoteRoleJson =
43206785244SRatan Gupta                         asyncResp->res
43306785244SRatan Gupta                             .jsonValue[serverType]["RemoteRoleMapping"];
4341476687dSEd Tanous                     nlohmann::json::object_t roleMapEntry;
4351476687dSEd Tanous                     roleMapEntry["LocalRole"] = *localRole;
4361476687dSEd Tanous                     roleMapEntry["RemoteGroup"] = *remoteGroup;
437b2ba3072SPatrick Williams                     remoteRoleJson.emplace_back(std::move(roleMapEntry));
43806785244SRatan Gupta                     },
43906785244SRatan Gupta                     ldapDbusService, dbusObjectPath, ldapPrivMapperInterface,
4403174e4dfSEd Tanous                     "Create", *remoteGroup,
44106785244SRatan Gupta                     getPrivilegeFromRoleId(std::move(*localRole)));
44206785244SRatan Gupta             }
44306785244SRatan Gupta         }
44406785244SRatan Gupta     }
44506785244SRatan Gupta }
44606785244SRatan Gupta 
44706785244SRatan Gupta /**
4486973a582SRatan Gupta  * Function that retrieves all properties for LDAP config object
4496973a582SRatan Gupta  * into JSON
4506973a582SRatan Gupta  */
4516973a582SRatan Gupta template <typename CallbackFunc>
4526973a582SRatan Gupta inline void getLDAPConfigData(const std::string& ldapType,
4536973a582SRatan Gupta                               CallbackFunc&& callback)
4546973a582SRatan Gupta {
4552b73119cSGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
4562b73119cSGeorge Liu         ldapEnableInterface, ldapConfigInterface};
45754fc587aSNagaraju Goruganti 
4582b73119cSGeorge Liu     dbus::utility::getDbusObject(
4592b73119cSGeorge Liu         ldapConfigObjectName, interfaces,
4602b73119cSGeorge Liu         [callback, ldapType](const boost::system::error_code& ec,
461b9d36b47SEd Tanous                              const dbus::utility::MapperGetObject& resp) {
46254fc587aSNagaraju Goruganti         if (ec || resp.empty())
46354fc587aSNagaraju Goruganti         {
4640fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
465002d39b4SEd Tanous                 << "DBUS response error during getting of service name: " << ec;
46623a21a1cSEd Tanous             LDAPConfigData empty{};
46723a21a1cSEd Tanous             callback(false, empty, ldapType);
46854fc587aSNagaraju Goruganti             return;
46954fc587aSNagaraju Goruganti         }
47054fc587aSNagaraju Goruganti         std::string service = resp.begin()->first;
47154fc587aSNagaraju Goruganti         crow::connections::systemBus->async_method_call(
472002d39b4SEd Tanous             [callback,
4735e7e2dc5SEd Tanous              ldapType](const boost::system::error_code& errorCode,
474711ac7a9SEd Tanous                        const dbus::utility::ManagedObjectType& ldapObjects) {
4756973a582SRatan Gupta             LDAPConfigData confData{};
47681ce609eSEd Tanous             if (errorCode)
4776973a582SRatan Gupta             {
478ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
479002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << errorCode;
4806973a582SRatan Gupta                 return;
4816973a582SRatan Gupta             }
482ab828d7cSRatan Gupta 
483ab828d7cSRatan Gupta             std::string ldapDbusType;
48454fc587aSNagaraju Goruganti             std::string searchString;
48554fc587aSNagaraju Goruganti 
486ab828d7cSRatan Gupta             if (ldapType == "LDAP")
487ab828d7cSRatan Gupta             {
4880fda0f12SGeorge Liu                 ldapDbusType =
4890fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap";
49054fc587aSNagaraju Goruganti                 searchString = "openldap";
491ab828d7cSRatan Gupta             }
492ab828d7cSRatan Gupta             else if (ldapType == "ActiveDirectory")
493ab828d7cSRatan Gupta             {
49454fc587aSNagaraju Goruganti                 ldapDbusType =
4950fda0f12SGeorge Liu                     "xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory";
49654fc587aSNagaraju Goruganti                 searchString = "active_directory";
497ab828d7cSRatan Gupta             }
498ab828d7cSRatan Gupta             else
499ab828d7cSRatan Gupta             {
500002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Can't get the DbusType for the given type="
501ab828d7cSRatan Gupta                                  << ldapType;
502ab828d7cSRatan Gupta                 callback(false, confData, ldapType);
503ab828d7cSRatan Gupta                 return;
504ab828d7cSRatan Gupta             }
505ab828d7cSRatan Gupta 
506ab828d7cSRatan Gupta             std::string ldapEnableInterfaceStr = ldapEnableInterface;
507ab828d7cSRatan Gupta             std::string ldapConfigInterfaceStr = ldapConfigInterface;
508ab828d7cSRatan Gupta 
5096973a582SRatan Gupta             for (const auto& object : ldapObjects)
5106973a582SRatan Gupta             {
51154fc587aSNagaraju Goruganti                 // let's find the object whose ldap type is equal to the
51254fc587aSNagaraju Goruganti                 // given type
513002d39b4SEd Tanous                 if (object.first.str.find(searchString) == std::string::npos)
5146973a582SRatan Gupta                 {
515ab828d7cSRatan Gupta                     continue;
516ab828d7cSRatan Gupta                 }
517ab828d7cSRatan Gupta 
5186973a582SRatan Gupta                 for (const auto& interface : object.second)
5196973a582SRatan Gupta                 {
5206973a582SRatan Gupta                     if (interface.first == ldapEnableInterfaceStr)
5216973a582SRatan Gupta                     {
5226973a582SRatan Gupta                         // rest of the properties are string.
5236973a582SRatan Gupta                         for (const auto& property : interface.second)
5246973a582SRatan Gupta                         {
5256973a582SRatan Gupta                             if (property.first == "Enabled")
5266973a582SRatan Gupta                             {
5276973a582SRatan Gupta                                 const bool* value =
5286973a582SRatan Gupta                                     std::get_if<bool>(&property.second);
5296973a582SRatan Gupta                                 if (value == nullptr)
5306973a582SRatan Gupta                                 {
5316973a582SRatan Gupta                                     continue;
5326973a582SRatan Gupta                                 }
5336973a582SRatan Gupta                                 confData.serviceEnabled = *value;
5346973a582SRatan Gupta                                 break;
5356973a582SRatan Gupta                             }
5366973a582SRatan Gupta                         }
5376973a582SRatan Gupta                     }
5386973a582SRatan Gupta                     else if (interface.first == ldapConfigInterfaceStr)
5396973a582SRatan Gupta                     {
5406973a582SRatan Gupta                         for (const auto& property : interface.second)
5416973a582SRatan Gupta                         {
542271584abSEd Tanous                             const std::string* strValue =
543002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
544271584abSEd Tanous                             if (strValue == nullptr)
5456973a582SRatan Gupta                             {
5466973a582SRatan Gupta                                 continue;
5476973a582SRatan Gupta                             }
5486973a582SRatan Gupta                             if (property.first == "LDAPServerURI")
5496973a582SRatan Gupta                             {
550271584abSEd Tanous                                 confData.uri = *strValue;
5516973a582SRatan Gupta                             }
5526973a582SRatan Gupta                             else if (property.first == "LDAPBindDN")
5536973a582SRatan Gupta                             {
554271584abSEd Tanous                                 confData.bindDN = *strValue;
5556973a582SRatan Gupta                             }
5566973a582SRatan Gupta                             else if (property.first == "LDAPBaseDN")
5576973a582SRatan Gupta                             {
558271584abSEd Tanous                                 confData.baseDN = *strValue;
5596973a582SRatan Gupta                             }
560002d39b4SEd Tanous                             else if (property.first == "LDAPSearchScope")
5616973a582SRatan Gupta                             {
562271584abSEd Tanous                                 confData.searchScope = *strValue;
5636973a582SRatan Gupta                             }
564002d39b4SEd Tanous                             else if (property.first == "GroupNameAttribute")
5656973a582SRatan Gupta                             {
566271584abSEd Tanous                                 confData.groupAttribute = *strValue;
5676973a582SRatan Gupta                             }
568002d39b4SEd Tanous                             else if (property.first == "UserNameAttribute")
5696973a582SRatan Gupta                             {
570271584abSEd Tanous                                 confData.userNameAttribute = *strValue;
5716973a582SRatan Gupta                             }
57254fc587aSNagaraju Goruganti                             else if (property.first == "LDAPType")
573ab828d7cSRatan Gupta                             {
574271584abSEd Tanous                                 confData.serverType = *strValue;
57554fc587aSNagaraju Goruganti                             }
57654fc587aSNagaraju Goruganti                         }
57754fc587aSNagaraju Goruganti                     }
578002d39b4SEd Tanous                     else if (interface.first ==
5790fda0f12SGeorge Liu                              "xyz.openbmc_project.User.PrivilegeMapperEntry")
58054fc587aSNagaraju Goruganti                     {
58154fc587aSNagaraju Goruganti                         LDAPRoleMapData roleMapData{};
58254fc587aSNagaraju Goruganti                         for (const auto& property : interface.second)
58354fc587aSNagaraju Goruganti                         {
584271584abSEd Tanous                             const std::string* strValue =
585002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
58654fc587aSNagaraju Goruganti 
587271584abSEd Tanous                             if (strValue == nullptr)
58854fc587aSNagaraju Goruganti                             {
58954fc587aSNagaraju Goruganti                                 continue;
59054fc587aSNagaraju Goruganti                             }
59154fc587aSNagaraju Goruganti 
59254fc587aSNagaraju Goruganti                             if (property.first == "GroupName")
59354fc587aSNagaraju Goruganti                             {
594271584abSEd Tanous                                 roleMapData.groupName = *strValue;
59554fc587aSNagaraju Goruganti                             }
59654fc587aSNagaraju Goruganti                             else if (property.first == "Privilege")
59754fc587aSNagaraju Goruganti                             {
598271584abSEd Tanous                                 roleMapData.privilege = *strValue;
59954fc587aSNagaraju Goruganti                             }
60054fc587aSNagaraju Goruganti                         }
60154fc587aSNagaraju Goruganti 
602002d39b4SEd Tanous                         confData.groupRoleList.emplace_back(object.first.str,
603002d39b4SEd Tanous                                                             roleMapData);
60454fc587aSNagaraju Goruganti                     }
60554fc587aSNagaraju Goruganti                 }
60654fc587aSNagaraju Goruganti             }
607ab828d7cSRatan Gupta             callback(true, confData, ldapType);
60854fc587aSNagaraju Goruganti             },
609002d39b4SEd Tanous             service, ldapRootObject, dbusObjManagerIntf, "GetManagedObjects");
6102b73119cSGeorge Liu         });
6116973a582SRatan Gupta }
6126973a582SRatan Gupta 
6138a07d286SRatan Gupta /**
6148a07d286SRatan Gupta  * @brief parses the authentication section under the LDAP
6158a07d286SRatan Gupta  * @param input JSON data
6168a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6178a07d286SRatan Gupta  * @param userName  userName to be filled from the given JSON.
6188a07d286SRatan Gupta  * @param password  password to be filled from the given JSON.
6198a07d286SRatan Gupta  */
6204f48d5f6SEd Tanous inline void parseLDAPAuthenticationJson(
6216c51eab1SEd Tanous     nlohmann::json input, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6226c51eab1SEd Tanous     std::optional<std::string>& username, std::optional<std::string>& password)
6238a07d286SRatan Gupta {
6248a07d286SRatan Gupta     std::optional<std::string> authType;
6258a07d286SRatan Gupta 
6268a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "AuthenticationType",
6278a07d286SRatan Gupta                              authType, "Username", username, "Password",
6288a07d286SRatan Gupta                              password))
6298a07d286SRatan Gupta     {
6308a07d286SRatan Gupta         return;
6318a07d286SRatan Gupta     }
6328a07d286SRatan Gupta     if (!authType)
6338a07d286SRatan Gupta     {
6348a07d286SRatan Gupta         return;
6358a07d286SRatan Gupta     }
6368a07d286SRatan Gupta     if (*authType != "UsernameAndPassword")
6378a07d286SRatan Gupta     {
6388a07d286SRatan Gupta         messages::propertyValueNotInList(asyncResp->res, *authType,
6398a07d286SRatan Gupta                                          "AuthenticationType");
6408a07d286SRatan Gupta         return;
6418a07d286SRatan Gupta     }
6428a07d286SRatan Gupta }
6438a07d286SRatan Gupta /**
6448a07d286SRatan Gupta  * @brief parses the LDAPService section under the LDAP
6458a07d286SRatan Gupta  * @param input JSON data
6468a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6478a07d286SRatan Gupta  * @param baseDNList baseDN to be filled from the given JSON.
6488a07d286SRatan Gupta  * @param userNameAttribute  userName to be filled from the given JSON.
6498a07d286SRatan Gupta  * @param groupaAttribute  password to be filled from the given JSON.
6508a07d286SRatan Gupta  */
6518a07d286SRatan Gupta 
6524f48d5f6SEd Tanous inline void
6534f48d5f6SEd Tanous     parseLDAPServiceJson(nlohmann::json input,
6548d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6558a07d286SRatan Gupta                          std::optional<std::vector<std::string>>& baseDNList,
6568a07d286SRatan Gupta                          std::optional<std::string>& userNameAttribute,
6578a07d286SRatan Gupta                          std::optional<std::string>& groupsAttribute)
6588a07d286SRatan Gupta {
6598a07d286SRatan Gupta     std::optional<nlohmann::json> searchSettings;
6608a07d286SRatan Gupta 
6618a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "SearchSettings",
6628a07d286SRatan Gupta                              searchSettings))
6638a07d286SRatan Gupta     {
6648a07d286SRatan Gupta         return;
6658a07d286SRatan Gupta     }
6668a07d286SRatan Gupta     if (!searchSettings)
6678a07d286SRatan Gupta     {
6688a07d286SRatan Gupta         return;
6698a07d286SRatan Gupta     }
6708a07d286SRatan Gupta     if (!json_util::readJson(*searchSettings, asyncResp->res,
6718a07d286SRatan Gupta                              "BaseDistinguishedNames", baseDNList,
6728a07d286SRatan Gupta                              "UsernameAttribute", userNameAttribute,
6738a07d286SRatan Gupta                              "GroupsAttribute", groupsAttribute))
6748a07d286SRatan Gupta     {
6758a07d286SRatan Gupta         return;
6768a07d286SRatan Gupta     }
6778a07d286SRatan Gupta }
6788a07d286SRatan Gupta /**
6798a07d286SRatan Gupta  * @brief updates the LDAP server address and updates the
6808a07d286SRatan Gupta           json response with the new value.
6818a07d286SRatan Gupta  * @param serviceAddressList address to be updated.
6828a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
6838a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
6848a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
6858a07d286SRatan Gupta  */
6868a07d286SRatan Gupta 
6874f48d5f6SEd Tanous inline void handleServiceAddressPatch(
6888a07d286SRatan Gupta     const std::vector<std::string>& serviceAddressList,
6898d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6908a07d286SRatan Gupta     const std::string& ldapServerElementName,
6918a07d286SRatan Gupta     const std::string& ldapConfigObject)
6928a07d286SRatan Gupta {
6938a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
6948a07d286SRatan Gupta         [asyncResp, ldapServerElementName,
695*25e055a3SRavi Teja          serviceAddressList](const boost::system::error_code& ec,
696*25e055a3SRavi Teja                              sdbusplus::message::message& msg) {
6978a07d286SRatan Gupta         if (ec)
6988a07d286SRatan Gupta         {
699*25e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
700*25e055a3SRavi Teja             if ((dbusError != nullptr) &&
701*25e055a3SRavi Teja                 (dbusError->name ==
702*25e055a3SRavi Teja                  std::string_view(
703*25e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
704*25e055a3SRavi Teja             {
705*25e055a3SRavi Teja                 BMCWEB_LOG_WARNING
706c61704abSGunnar Mills                     << "Error Occurred in updating the service address";
707*25e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
708*25e055a3SRavi Teja                                                  "ServiceAddresses",
709*25e055a3SRavi Teja                                                  serviceAddressList.front());
710*25e055a3SRavi Teja                 return;
711*25e055a3SRavi Teja             }
7128a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7138a07d286SRatan Gupta             return;
7148a07d286SRatan Gupta         }
7158a07d286SRatan Gupta         std::vector<std::string> modifiedserviceAddressList = {
7168a07d286SRatan Gupta             serviceAddressList.front()};
717002d39b4SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceAddresses"] =
7188a07d286SRatan Gupta             modifiedserviceAddressList;
7198a07d286SRatan Gupta         if ((serviceAddressList).size() > 1)
7208a07d286SRatan Gupta         {
721002d39b4SEd Tanous             messages::propertyValueModified(asyncResp->res, "ServiceAddresses",
7228a07d286SRatan Gupta                                             serviceAddressList.front());
7238a07d286SRatan Gupta         }
7248a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the service address";
7258a07d286SRatan Gupta         },
7268a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7278a07d286SRatan Gupta         ldapConfigInterface, "LDAPServerURI",
728168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceAddressList.front()));
7298a07d286SRatan Gupta }
7308a07d286SRatan Gupta /**
7318a07d286SRatan Gupta  * @brief updates the LDAP Bind DN and updates the
7328a07d286SRatan Gupta           json response with the new value.
7338a07d286SRatan Gupta  * @param username name of the user which needs to be updated.
7348a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7358a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7368a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
7378a07d286SRatan Gupta  */
7388a07d286SRatan Gupta 
7394f48d5f6SEd Tanous inline void
7404f48d5f6SEd Tanous     handleUserNamePatch(const std::string& username,
7418d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7428a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7438a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7448a07d286SRatan Gupta {
7458a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7468a07d286SRatan Gupta         [asyncResp, username,
7475e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
7488a07d286SRatan Gupta         if (ec)
7498a07d286SRatan Gupta         {
7506c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the username";
7518a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7528a07d286SRatan Gupta             return;
7538a07d286SRatan Gupta         }
75489492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
75589492a15SPatrick Williams                                 ["Username"] = username;
7568a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the username";
7578a07d286SRatan Gupta         },
7588a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
759168e20c1SEd Tanous         ldapConfigInterface, "LDAPBindDN",
760168e20c1SEd Tanous         dbus::utility::DbusVariantType(username));
7618a07d286SRatan Gupta }
7628a07d286SRatan Gupta 
7638a07d286SRatan Gupta /**
7648a07d286SRatan Gupta  * @brief updates the LDAP password
7658a07d286SRatan Gupta  * @param password : ldap password which needs to be updated.
7668a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
7678a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
7688a07d286SRatan Gupta  *        server(openLDAP/ActiveDirectory)
7698a07d286SRatan Gupta  */
7708a07d286SRatan Gupta 
7714f48d5f6SEd Tanous inline void
7724f48d5f6SEd Tanous     handlePasswordPatch(const std::string& password,
7738d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7748a07d286SRatan Gupta                         const std::string& ldapServerElementName,
7758a07d286SRatan Gupta                         const std::string& ldapConfigObject)
7768a07d286SRatan Gupta {
7778a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
7788a07d286SRatan Gupta         [asyncResp, password,
7795e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
7808a07d286SRatan Gupta         if (ec)
7818a07d286SRatan Gupta         {
7826c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error occurred in updating the password";
7838a07d286SRatan Gupta             messages::internalError(asyncResp->res);
7848a07d286SRatan Gupta             return;
7858a07d286SRatan Gupta         }
78689492a15SPatrick Williams         asyncResp->res.jsonValue[ldapServerElementName]["Authentication"]
78789492a15SPatrick Williams                                 ["Password"] = "";
7888a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the password";
7898a07d286SRatan Gupta         },
7908a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
7918a07d286SRatan Gupta         ldapConfigInterface, "LDAPBindDNPassword",
792168e20c1SEd Tanous         dbus::utility::DbusVariantType(password));
7938a07d286SRatan Gupta }
7948a07d286SRatan Gupta 
7958a07d286SRatan Gupta /**
7968a07d286SRatan Gupta  * @brief updates the LDAP BaseDN and updates the
7978a07d286SRatan Gupta           json response with the new value.
7988a07d286SRatan Gupta  * @param baseDNList baseDN list which needs to be updated.
7998a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8008a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8018a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8028a07d286SRatan Gupta  */
8038a07d286SRatan Gupta 
8044f48d5f6SEd Tanous inline void
8054f48d5f6SEd Tanous     handleBaseDNPatch(const std::vector<std::string>& baseDNList,
8068d1b46d7Szhanghch05                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8078a07d286SRatan Gupta                       const std::string& ldapServerElementName,
8088a07d286SRatan Gupta                       const std::string& ldapConfigObject)
8098a07d286SRatan Gupta {
8108a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8118a07d286SRatan Gupta         [asyncResp, baseDNList,
812*25e055a3SRavi Teja          ldapServerElementName](const boost::system::error_code& ec,
813*25e055a3SRavi Teja                                 const sdbusplus::message::message& msg) {
8148a07d286SRatan Gupta         if (ec)
8158a07d286SRatan Gupta         {
8166c51eab1SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the base DN";
817*25e055a3SRavi Teja             const sd_bus_error* dbusError = msg.get_error();
818*25e055a3SRavi Teja             if ((dbusError != nullptr) &&
819*25e055a3SRavi Teja                 (dbusError->name ==
820*25e055a3SRavi Teja                  std::string_view(
821*25e055a3SRavi Teja                      "xyz.openbmc_project.Common.Error.InvalidArgument")))
822*25e055a3SRavi Teja             {
823*25e055a3SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res,
824*25e055a3SRavi Teja                                                  "BaseDistinguishedNames",
825*25e055a3SRavi Teja                                                  baseDNList.front());
826*25e055a3SRavi Teja                 return;
827*25e055a3SRavi Teja             }
8288a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8298a07d286SRatan Gupta             return;
8308a07d286SRatan Gupta         }
831002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8328a07d286SRatan Gupta         auto& searchSettingsJson =
8338a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8346c51eab1SEd Tanous         std::vector<std::string> modifiedBaseDNList = {baseDNList.front()};
8356c51eab1SEd Tanous         searchSettingsJson["BaseDistinguishedNames"] = modifiedBaseDNList;
8368a07d286SRatan Gupta         if (baseDNList.size() > 1)
8378a07d286SRatan Gupta         {
838002d39b4SEd Tanous             messages::propertyValueModified(
839002d39b4SEd Tanous                 asyncResp->res, "BaseDistinguishedNames", baseDNList.front());
8408a07d286SRatan Gupta         }
8418a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the base DN";
8428a07d286SRatan Gupta         },
8438a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8448a07d286SRatan Gupta         ldapConfigInterface, "LDAPBaseDN",
845168e20c1SEd Tanous         dbus::utility::DbusVariantType(baseDNList.front()));
8468a07d286SRatan Gupta }
8478a07d286SRatan Gupta /**
8488a07d286SRatan Gupta  * @brief updates the LDAP user name attribute and updates the
8498a07d286SRatan Gupta           json response with the new value.
8508a07d286SRatan Gupta  * @param userNameAttribute attribute to be updated.
8518a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8528a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8538a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8548a07d286SRatan Gupta  */
8558a07d286SRatan Gupta 
8564f48d5f6SEd Tanous inline void
8574f48d5f6SEd Tanous     handleUserNameAttrPatch(const std::string& userNameAttribute,
8588d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8598a07d286SRatan Gupta                             const std::string& ldapServerElementName,
8608a07d286SRatan Gupta                             const std::string& ldapConfigObject)
8618a07d286SRatan Gupta {
8628a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8638a07d286SRatan Gupta         [asyncResp, userNameAttribute,
8645e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
8658a07d286SRatan Gupta         if (ec)
8668a07d286SRatan Gupta         {
867c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
8688a07d286SRatan Gupta                                 "username attribute";
8698a07d286SRatan Gupta             messages::internalError(asyncResp->res);
8708a07d286SRatan Gupta             return;
8718a07d286SRatan Gupta         }
872002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
8738a07d286SRatan Gupta         auto& searchSettingsJson =
8748a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
8758a07d286SRatan Gupta         searchSettingsJson["UsernameAttribute"] = userNameAttribute;
8768a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the user name attr.";
8778a07d286SRatan Gupta         },
8788a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
8798a07d286SRatan Gupta         ldapConfigInterface, "UserNameAttribute",
880168e20c1SEd Tanous         dbus::utility::DbusVariantType(userNameAttribute));
8818a07d286SRatan Gupta }
8828a07d286SRatan Gupta /**
8838a07d286SRatan Gupta  * @brief updates the LDAP group attribute and updates the
8848a07d286SRatan Gupta           json response with the new value.
8858a07d286SRatan Gupta  * @param groupsAttribute attribute to be updated.
8868a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
8878a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
8888a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
8898a07d286SRatan Gupta  */
8908a07d286SRatan Gupta 
8914f48d5f6SEd Tanous inline void handleGroupNameAttrPatch(
8928d1b46d7Szhanghch05     const std::string& groupsAttribute,
8938d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8948a07d286SRatan Gupta     const std::string& ldapServerElementName,
8958a07d286SRatan Gupta     const std::string& ldapConfigObject)
8968a07d286SRatan Gupta {
8978a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
8988a07d286SRatan Gupta         [asyncResp, groupsAttribute,
8995e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
9008a07d286SRatan Gupta         if (ec)
9018a07d286SRatan Gupta         {
902c61704abSGunnar Mills             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the "
9038a07d286SRatan Gupta                                 "groupname attribute";
9048a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9058a07d286SRatan Gupta             return;
9068a07d286SRatan Gupta         }
907002d39b4SEd Tanous         auto& serverTypeJson = asyncResp->res.jsonValue[ldapServerElementName];
9088a07d286SRatan Gupta         auto& searchSettingsJson =
9098a07d286SRatan Gupta             serverTypeJson["LDAPService"]["SearchSettings"];
9108a07d286SRatan Gupta         searchSettingsJson["GroupsAttribute"] = groupsAttribute;
9118a07d286SRatan Gupta         BMCWEB_LOG_DEBUG << "Updated the groupname attr";
9128a07d286SRatan Gupta         },
9138a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
9148a07d286SRatan Gupta         ldapConfigInterface, "GroupNameAttribute",
915168e20c1SEd Tanous         dbus::utility::DbusVariantType(groupsAttribute));
9168a07d286SRatan Gupta }
9178a07d286SRatan Gupta /**
9188a07d286SRatan Gupta  * @brief updates the LDAP service enable and updates the
9198a07d286SRatan Gupta           json response with the new value.
9208a07d286SRatan Gupta  * @param input JSON data.
9218a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
9228a07d286SRatan Gupta  * @param ldapServerElementName Type of LDAP
9238a07d286SRatan Gupta  server(openLDAP/ActiveDirectory)
9248a07d286SRatan Gupta  */
9258a07d286SRatan Gupta 
9264f48d5f6SEd Tanous inline void handleServiceEnablePatch(
9276c51eab1SEd Tanous     bool serviceEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9288a07d286SRatan Gupta     const std::string& ldapServerElementName,
9298a07d286SRatan Gupta     const std::string& ldapConfigObject)
9308a07d286SRatan Gupta {
9318a07d286SRatan Gupta     crow::connections::systemBus->async_method_call(
9328a07d286SRatan Gupta         [asyncResp, serviceEnabled,
9335e7e2dc5SEd Tanous          ldapServerElementName](const boost::system::error_code& ec) {
9348a07d286SRatan Gupta         if (ec)
9358a07d286SRatan Gupta         {
936002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "Error Occurred in Updating the service enable";
9378a07d286SRatan Gupta             messages::internalError(asyncResp->res);
9388a07d286SRatan Gupta             return;
9398a07d286SRatan Gupta         }
9406c51eab1SEd Tanous         asyncResp->res.jsonValue[ldapServerElementName]["ServiceEnabled"] =
9418a07d286SRatan Gupta             serviceEnabled;
9426c51eab1SEd Tanous         BMCWEB_LOG_DEBUG << "Updated Service enable = " << serviceEnabled;
9438a07d286SRatan Gupta         },
9448a07d286SRatan Gupta         ldapDbusService, ldapConfigObject, propertyInterface, "Set",
945168e20c1SEd Tanous         ldapEnableInterface, "Enabled",
946168e20c1SEd Tanous         dbus::utility::DbusVariantType(serviceEnabled));
9478a07d286SRatan Gupta }
9488a07d286SRatan Gupta 
9494f48d5f6SEd Tanous inline void
9504f48d5f6SEd Tanous     handleAuthMethodsPatch(nlohmann::json& input,
9518d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
95278158631SZbigniew Kurzynski {
95378158631SZbigniew Kurzynski     std::optional<bool> basicAuth;
95478158631SZbigniew Kurzynski     std::optional<bool> cookie;
95578158631SZbigniew Kurzynski     std::optional<bool> sessionToken;
95678158631SZbigniew Kurzynski     std::optional<bool> xToken;
957501f1e58SZbigniew Kurzynski     std::optional<bool> tls;
95878158631SZbigniew Kurzynski 
95978158631SZbigniew Kurzynski     if (!json_util::readJson(input, asyncResp->res, "BasicAuth", basicAuth,
96078158631SZbigniew Kurzynski                              "Cookie", cookie, "SessionToken", sessionToken,
961501f1e58SZbigniew Kurzynski                              "XToken", xToken, "TLS", tls))
96278158631SZbigniew Kurzynski     {
96378158631SZbigniew Kurzynski         BMCWEB_LOG_ERROR << "Cannot read values from AuthMethod tag";
96478158631SZbigniew Kurzynski         return;
96578158631SZbigniew Kurzynski     }
96678158631SZbigniew Kurzynski 
96778158631SZbigniew Kurzynski     // Make a copy of methods configuration
96852cc112dSEd Tanous     persistent_data::AuthConfigMethods authMethodsConfig =
96952cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
97078158631SZbigniew Kurzynski 
97178158631SZbigniew Kurzynski     if (basicAuth)
97278158631SZbigniew Kurzynski     {
973f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_BASIC_AUTHENTICATION
974f16f6263SAlan Kuo         messages::actionNotSupported(
9750fda0f12SGeorge Liu             asyncResp->res,
9760fda0f12SGeorge Liu             "Setting BasicAuth when basic-auth feature is disabled");
977f16f6263SAlan Kuo         return;
978f16f6263SAlan Kuo #endif
97978158631SZbigniew Kurzynski         authMethodsConfig.basic = *basicAuth;
98078158631SZbigniew Kurzynski     }
98178158631SZbigniew Kurzynski 
98278158631SZbigniew Kurzynski     if (cookie)
98378158631SZbigniew Kurzynski     {
984f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_COOKIE_AUTHENTICATION
9850fda0f12SGeorge Liu         messages::actionNotSupported(
9860fda0f12SGeorge Liu             asyncResp->res,
9870fda0f12SGeorge Liu             "Setting Cookie when cookie-auth feature is disabled");
988f16f6263SAlan Kuo         return;
989f16f6263SAlan Kuo #endif
99078158631SZbigniew Kurzynski         authMethodsConfig.cookie = *cookie;
99178158631SZbigniew Kurzynski     }
99278158631SZbigniew Kurzynski 
99378158631SZbigniew Kurzynski     if (sessionToken)
99478158631SZbigniew Kurzynski     {
995f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_SESSION_AUTHENTICATION
996f16f6263SAlan Kuo         messages::actionNotSupported(
9970fda0f12SGeorge Liu             asyncResp->res,
9980fda0f12SGeorge Liu             "Setting SessionToken when session-auth feature is disabled");
999f16f6263SAlan Kuo         return;
1000f16f6263SAlan Kuo #endif
100178158631SZbigniew Kurzynski         authMethodsConfig.sessionToken = *sessionToken;
100278158631SZbigniew Kurzynski     }
100378158631SZbigniew Kurzynski 
100478158631SZbigniew Kurzynski     if (xToken)
100578158631SZbigniew Kurzynski     {
1006f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_XTOKEN_AUTHENTICATION
10070fda0f12SGeorge Liu         messages::actionNotSupported(
10080fda0f12SGeorge Liu             asyncResp->res,
10090fda0f12SGeorge Liu             "Setting XToken when xtoken-auth feature is disabled");
1010f16f6263SAlan Kuo         return;
1011f16f6263SAlan Kuo #endif
101278158631SZbigniew Kurzynski         authMethodsConfig.xtoken = *xToken;
101378158631SZbigniew Kurzynski     }
101478158631SZbigniew Kurzynski 
1015501f1e58SZbigniew Kurzynski     if (tls)
1016501f1e58SZbigniew Kurzynski     {
1017f16f6263SAlan Kuo #ifndef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
10180fda0f12SGeorge Liu         messages::actionNotSupported(
10190fda0f12SGeorge Liu             asyncResp->res,
10200fda0f12SGeorge Liu             "Setting TLS when mutual-tls-auth feature is disabled");
1021f16f6263SAlan Kuo         return;
1022f16f6263SAlan Kuo #endif
1023501f1e58SZbigniew Kurzynski         authMethodsConfig.tls = *tls;
1024501f1e58SZbigniew Kurzynski     }
1025501f1e58SZbigniew Kurzynski 
102678158631SZbigniew Kurzynski     if (!authMethodsConfig.basic && !authMethodsConfig.cookie &&
1027501f1e58SZbigniew Kurzynski         !authMethodsConfig.sessionToken && !authMethodsConfig.xtoken &&
1028501f1e58SZbigniew Kurzynski         !authMethodsConfig.tls)
102978158631SZbigniew Kurzynski     {
103078158631SZbigniew Kurzynski         // Do not allow user to disable everything
103178158631SZbigniew Kurzynski         messages::actionNotSupported(asyncResp->res,
103278158631SZbigniew Kurzynski                                      "of disabling all available methods");
103378158631SZbigniew Kurzynski         return;
103478158631SZbigniew Kurzynski     }
103578158631SZbigniew Kurzynski 
103652cc112dSEd Tanous     persistent_data::SessionStore::getInstance().updateAuthMethodsConfig(
103752cc112dSEd Tanous         authMethodsConfig);
103878158631SZbigniew Kurzynski     // Save configuration immediately
103952cc112dSEd Tanous     persistent_data::getConfig().writeData();
104078158631SZbigniew Kurzynski 
104178158631SZbigniew Kurzynski     messages::success(asyncResp->res);
104278158631SZbigniew Kurzynski }
104378158631SZbigniew Kurzynski 
10448a07d286SRatan Gupta /**
10458a07d286SRatan Gupta  * @brief Get the required values from the given JSON, validates the
10468a07d286SRatan Gupta  *        value and create the LDAP config object.
10478a07d286SRatan Gupta  * @param input JSON data
10488a07d286SRatan Gupta  * @param asyncResp pointer to the JSON response
10498a07d286SRatan Gupta  * @param serverType Type of LDAP server(openLDAP/ActiveDirectory)
10508a07d286SRatan Gupta  */
10518a07d286SRatan Gupta 
10526c51eab1SEd Tanous inline void handleLDAPPatch(nlohmann::json& input,
10538d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10548a07d286SRatan Gupta                             const std::string& serverType)
10558a07d286SRatan Gupta {
1056eb2bbe56SRatan Gupta     std::string dbusObjectPath;
1057eb2bbe56SRatan Gupta     if (serverType == "ActiveDirectory")
1058eb2bbe56SRatan Gupta     {
10592c70f800SEd Tanous         dbusObjectPath = adConfigObject;
1060eb2bbe56SRatan Gupta     }
1061eb2bbe56SRatan Gupta     else if (serverType == "LDAP")
1062eb2bbe56SRatan Gupta     {
106323a21a1cSEd Tanous         dbusObjectPath = ldapConfigObjectName;
1064eb2bbe56SRatan Gupta     }
1065cb13a392SEd Tanous     else
1066cb13a392SEd Tanous     {
1067cb13a392SEd Tanous         return;
1068cb13a392SEd Tanous     }
1069eb2bbe56SRatan Gupta 
10708a07d286SRatan Gupta     std::optional<nlohmann::json> authentication;
10718a07d286SRatan Gupta     std::optional<nlohmann::json> ldapService;
10728a07d286SRatan Gupta     std::optional<std::vector<std::string>> serviceAddressList;
10738a07d286SRatan Gupta     std::optional<bool> serviceEnabled;
10748a07d286SRatan Gupta     std::optional<std::vector<std::string>> baseDNList;
10758a07d286SRatan Gupta     std::optional<std::string> userNameAttribute;
10768a07d286SRatan Gupta     std::optional<std::string> groupsAttribute;
10778a07d286SRatan Gupta     std::optional<std::string> userName;
10788a07d286SRatan Gupta     std::optional<std::string> password;
107906785244SRatan Gupta     std::optional<std::vector<nlohmann::json>> remoteRoleMapData;
10808a07d286SRatan Gupta 
10818a07d286SRatan Gupta     if (!json_util::readJson(input, asyncResp->res, "Authentication",
10828a07d286SRatan Gupta                              authentication, "LDAPService", ldapService,
10838a07d286SRatan Gupta                              "ServiceAddresses", serviceAddressList,
108406785244SRatan Gupta                              "ServiceEnabled", serviceEnabled,
108506785244SRatan Gupta                              "RemoteRoleMapping", remoteRoleMapData))
10868a07d286SRatan Gupta     {
10878a07d286SRatan Gupta         return;
10888a07d286SRatan Gupta     }
10898a07d286SRatan Gupta 
10908a07d286SRatan Gupta     if (authentication)
10918a07d286SRatan Gupta     {
10928a07d286SRatan Gupta         parseLDAPAuthenticationJson(*authentication, asyncResp, userName,
10938a07d286SRatan Gupta                                     password);
10948a07d286SRatan Gupta     }
10958a07d286SRatan Gupta     if (ldapService)
10968a07d286SRatan Gupta     {
10978a07d286SRatan Gupta         parseLDAPServiceJson(*ldapService, asyncResp, baseDNList,
10988a07d286SRatan Gupta                              userNameAttribute, groupsAttribute);
10998a07d286SRatan Gupta     }
11008a07d286SRatan Gupta     if (serviceAddressList)
11018a07d286SRatan Gupta     {
110226f6976fSEd Tanous         if (serviceAddressList->empty())
11038a07d286SRatan Gupta         {
11048a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
11058a07d286SRatan Gupta                                              "ServiceAddress");
11068a07d286SRatan Gupta             return;
11078a07d286SRatan Gupta         }
11088a07d286SRatan Gupta     }
11098a07d286SRatan Gupta     if (baseDNList)
11108a07d286SRatan Gupta     {
111126f6976fSEd Tanous         if (baseDNList->empty())
11128a07d286SRatan Gupta         {
11138a07d286SRatan Gupta             messages::propertyValueNotInList(asyncResp->res, "[]",
11148a07d286SRatan Gupta                                              "BaseDistinguishedNames");
11158a07d286SRatan Gupta             return;
11168a07d286SRatan Gupta         }
11178a07d286SRatan Gupta     }
11188a07d286SRatan Gupta 
11198a07d286SRatan Gupta     // nothing to update, then return
11208a07d286SRatan Gupta     if (!userName && !password && !serviceAddressList && !baseDNList &&
112106785244SRatan Gupta         !userNameAttribute && !groupsAttribute && !serviceEnabled &&
112206785244SRatan Gupta         !remoteRoleMapData)
11238a07d286SRatan Gupta     {
11248a07d286SRatan Gupta         return;
11258a07d286SRatan Gupta     }
11268a07d286SRatan Gupta 
11278a07d286SRatan Gupta     // Get the existing resource first then keep modifying
11288a07d286SRatan Gupta     // whenever any property gets updated.
1129002d39b4SEd Tanous     getLDAPConfigData(
1130002d39b4SEd Tanous         serverType,
1131002d39b4SEd Tanous         [asyncResp, userName, password, baseDNList, userNameAttribute,
1132002d39b4SEd Tanous          groupsAttribute, serviceAddressList, serviceEnabled, dbusObjectPath,
1133002d39b4SEd Tanous          remoteRoleMapData](bool success, const LDAPConfigData& confData,
113423a21a1cSEd Tanous                             const std::string& serverT) {
11358a07d286SRatan Gupta         if (!success)
11368a07d286SRatan Gupta         {
11378a07d286SRatan Gupta             messages::internalError(asyncResp->res);
11388a07d286SRatan Gupta             return;
11398a07d286SRatan Gupta         }
11406c51eab1SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, serverT);
11418a07d286SRatan Gupta         if (confData.serviceEnabled)
11428a07d286SRatan Gupta         {
11438a07d286SRatan Gupta             // Disable the service first and update the rest of
11448a07d286SRatan Gupta             // the properties.
11456c51eab1SEd Tanous             handleServiceEnablePatch(false, asyncResp, serverT, dbusObjectPath);
11468a07d286SRatan Gupta         }
11478a07d286SRatan Gupta 
11488a07d286SRatan Gupta         if (serviceAddressList)
11498a07d286SRatan Gupta         {
11506c51eab1SEd Tanous             handleServiceAddressPatch(*serviceAddressList, asyncResp, serverT,
11516c51eab1SEd Tanous                                       dbusObjectPath);
11528a07d286SRatan Gupta         }
11538a07d286SRatan Gupta         if (userName)
11548a07d286SRatan Gupta         {
11556c51eab1SEd Tanous             handleUserNamePatch(*userName, asyncResp, serverT, dbusObjectPath);
11568a07d286SRatan Gupta         }
11578a07d286SRatan Gupta         if (password)
11588a07d286SRatan Gupta         {
11596c51eab1SEd Tanous             handlePasswordPatch(*password, asyncResp, serverT, dbusObjectPath);
11608a07d286SRatan Gupta         }
11618a07d286SRatan Gupta 
11628a07d286SRatan Gupta         if (baseDNList)
11638a07d286SRatan Gupta         {
11646c51eab1SEd Tanous             handleBaseDNPatch(*baseDNList, asyncResp, serverT, dbusObjectPath);
11658a07d286SRatan Gupta         }
11668a07d286SRatan Gupta         if (userNameAttribute)
11678a07d286SRatan Gupta         {
11686c51eab1SEd Tanous             handleUserNameAttrPatch(*userNameAttribute, asyncResp, serverT,
11696c51eab1SEd Tanous                                     dbusObjectPath);
11708a07d286SRatan Gupta         }
11718a07d286SRatan Gupta         if (groupsAttribute)
11728a07d286SRatan Gupta         {
11736c51eab1SEd Tanous             handleGroupNameAttrPatch(*groupsAttribute, asyncResp, serverT,
11746c51eab1SEd Tanous                                      dbusObjectPath);
11758a07d286SRatan Gupta         }
11768a07d286SRatan Gupta         if (serviceEnabled)
11778a07d286SRatan Gupta         {
11788a07d286SRatan Gupta             // if user has given the value as true then enable
11798a07d286SRatan Gupta             // the service. if user has given false then no-op
11808a07d286SRatan Gupta             // as service is already stopped.
11818a07d286SRatan Gupta             if (*serviceEnabled)
11828a07d286SRatan Gupta             {
11836c51eab1SEd Tanous                 handleServiceEnablePatch(*serviceEnabled, asyncResp, serverT,
11846c51eab1SEd Tanous                                          dbusObjectPath);
11858a07d286SRatan Gupta             }
11868a07d286SRatan Gupta         }
11878a07d286SRatan Gupta         else
11888a07d286SRatan Gupta         {
11898a07d286SRatan Gupta             // if user has not given the service enabled value
11908a07d286SRatan Gupta             // then revert it to the same state as it was
11918a07d286SRatan Gupta             // before.
11928a07d286SRatan Gupta             handleServiceEnablePatch(confData.serviceEnabled, asyncResp,
119323a21a1cSEd Tanous                                      serverT, dbusObjectPath);
11948a07d286SRatan Gupta         }
119506785244SRatan Gupta 
119606785244SRatan Gupta         if (remoteRoleMapData)
119706785244SRatan Gupta         {
11986c51eab1SEd Tanous             handleRoleMapPatch(asyncResp, confData.groupRoleList, serverT,
11996c51eab1SEd Tanous                                *remoteRoleMapData);
120006785244SRatan Gupta         }
12018a07d286SRatan Gupta         });
12028a07d286SRatan Gupta }
1203d4b5443fSEd Tanous 
12046c51eab1SEd Tanous inline void updateUserProperties(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
12056c51eab1SEd Tanous                                  const std::string& username,
1206618c14b4SEd Tanous                                  const std::optional<std::string>& password,
1207618c14b4SEd Tanous                                  const std::optional<bool>& enabled,
1208618c14b4SEd Tanous                                  const std::optional<std::string>& roleId,
1209618c14b4SEd Tanous                                  const std::optional<bool>& locked)
12101abe55efSEd Tanous {
1211b477fd44SP Dheeraj Srujan Kumar     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
1212b477fd44SP Dheeraj Srujan Kumar     tempObjPath /= username;
1213b477fd44SP Dheeraj Srujan Kumar     std::string dbusObjectPath(tempObjPath);
12146c51eab1SEd Tanous 
12156c51eab1SEd Tanous     dbus::utility::checkDbusPathExists(
1216618c14b4SEd Tanous         dbusObjectPath, [dbusObjectPath, username, password, roleId, enabled,
1217618c14b4SEd Tanous                          locked, asyncResp{std::move(asyncResp)}](int rc) {
1218e662eae8SEd Tanous             if (rc <= 0)
12196c51eab1SEd Tanous             {
1220d8a5d5d8SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "ManagerAccount",
12216c51eab1SEd Tanous                                            username);
12226c51eab1SEd Tanous                 return;
12236c51eab1SEd Tanous             }
12246c51eab1SEd Tanous 
12256c51eab1SEd Tanous             if (password)
12266c51eab1SEd Tanous             {
12276c51eab1SEd Tanous                 int retval = pamUpdatePassword(username, *password);
12286c51eab1SEd Tanous 
12296c51eab1SEd Tanous                 if (retval == PAM_USER_UNKNOWN)
12306c51eab1SEd Tanous                 {
1231d8a5d5d8SJiaqing Zhao                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
12326c51eab1SEd Tanous                                                username);
12336c51eab1SEd Tanous                 }
12346c51eab1SEd Tanous                 else if (retval == PAM_AUTHTOK_ERR)
12356c51eab1SEd Tanous                 {
12366c51eab1SEd Tanous                     // If password is invalid
1237618c14b4SEd Tanous                     messages::propertyValueFormatError(asyncResp->res,
1238618c14b4SEd Tanous                                                        *password, "Password");
12396c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
12406c51eab1SEd Tanous                 }
12416c51eab1SEd Tanous                 else if (retval != PAM_SUCCESS)
12426c51eab1SEd Tanous                 {
12436c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12446c51eab1SEd Tanous                     return;
12456c51eab1SEd Tanous                 }
1246e7b1b62bSEd Tanous                 else
1247e7b1b62bSEd Tanous                 {
1248e7b1b62bSEd Tanous                     messages::success(asyncResp->res);
1249e7b1b62bSEd Tanous                 }
12506c51eab1SEd Tanous             }
12516c51eab1SEd Tanous 
12526c51eab1SEd Tanous             if (enabled)
12536c51eab1SEd Tanous             {
12546c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12555e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
12566c51eab1SEd Tanous                 if (ec)
12576c51eab1SEd Tanous                 {
12586c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12596c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12606c51eab1SEd Tanous                     return;
12616c51eab1SEd Tanous                 }
12626c51eab1SEd Tanous                 messages::success(asyncResp->res);
12636c51eab1SEd Tanous                 return;
12646c51eab1SEd Tanous                     },
1265e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12666c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12676c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserEnabled",
1268168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*enabled});
12696c51eab1SEd Tanous             }
12706c51eab1SEd Tanous 
12716c51eab1SEd Tanous             if (roleId)
12726c51eab1SEd Tanous             {
12736c51eab1SEd Tanous                 std::string priv = getPrivilegeFromRoleId(*roleId);
12746c51eab1SEd Tanous                 if (priv.empty())
12756c51eab1SEd Tanous                 {
12766c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, *roleId,
12776c51eab1SEd Tanous                                                      "RoleId");
12786c51eab1SEd Tanous                     return;
12796c51eab1SEd Tanous                 }
12806c51eab1SEd Tanous 
12816c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
12825e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
12836c51eab1SEd Tanous                 if (ec)
12846c51eab1SEd Tanous                 {
12856c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
12866c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
12876c51eab1SEd Tanous                     return;
12886c51eab1SEd Tanous                 }
12896c51eab1SEd Tanous                 messages::success(asyncResp->res);
12906c51eab1SEd Tanous                     },
1291e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
12926c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
12936c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes", "UserPrivilege",
1294168e20c1SEd Tanous                     dbus::utility::DbusVariantType{priv});
12956c51eab1SEd Tanous             }
12966c51eab1SEd Tanous 
12976c51eab1SEd Tanous             if (locked)
12986c51eab1SEd Tanous             {
12996c51eab1SEd Tanous                 // admin can unlock the account which is locked by
13006c51eab1SEd Tanous                 // successive authentication failures but admin should
13016c51eab1SEd Tanous                 // not be allowed to lock an account.
13026c51eab1SEd Tanous                 if (*locked)
13036c51eab1SEd Tanous                 {
13046c51eab1SEd Tanous                     messages::propertyValueNotInList(asyncResp->res, "true",
13056c51eab1SEd Tanous                                                      "Locked");
13066c51eab1SEd Tanous                     return;
13076c51eab1SEd Tanous                 }
13086c51eab1SEd Tanous 
13096c51eab1SEd Tanous                 crow::connections::systemBus->async_method_call(
13105e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
13116c51eab1SEd Tanous                 if (ec)
13126c51eab1SEd Tanous                 {
13136c51eab1SEd Tanous                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
13146c51eab1SEd Tanous                     messages::internalError(asyncResp->res);
13156c51eab1SEd Tanous                     return;
13166c51eab1SEd Tanous                 }
13176c51eab1SEd Tanous                 messages::success(asyncResp->res);
13186c51eab1SEd Tanous                 return;
13196c51eab1SEd Tanous                     },
1320e05aec50SEd Tanous                     "xyz.openbmc_project.User.Manager", dbusObjectPath,
13216c51eab1SEd Tanous                     "org.freedesktop.DBus.Properties", "Set",
13226c51eab1SEd Tanous                     "xyz.openbmc_project.User.Attributes",
1323168e20c1SEd Tanous                     "UserLockedForFailedAttempt",
1324168e20c1SEd Tanous                     dbus::utility::DbusVariantType{*locked});
13256c51eab1SEd Tanous             }
13266c51eab1SEd Tanous         });
13276c51eab1SEd Tanous }
13286c51eab1SEd Tanous 
13294c7d4d33SEd Tanous inline void handleAccountServiceHead(
13304c7d4d33SEd Tanous     App& app, const crow::Request& req,
13311ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13326c51eab1SEd Tanous {
13333ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
133445ca1b86SEd Tanous     {
133545ca1b86SEd Tanous         return;
133645ca1b86SEd Tanous     }
13374c7d4d33SEd Tanous     asyncResp->res.addHeader(
13384c7d4d33SEd Tanous         boost::beast::http::field::link,
13394c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
13404c7d4d33SEd Tanous }
13414c7d4d33SEd Tanous 
13424c7d4d33SEd Tanous inline void
13434c7d4d33SEd Tanous     handleAccountServiceGet(App& app, const crow::Request& req,
13444c7d4d33SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13454c7d4d33SEd Tanous {
1346afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1347afd369c6SJiaqing Zhao     {
1348afd369c6SJiaqing Zhao         return;
1349afd369c6SJiaqing Zhao     }
13503e72c202SNinad Palsule 
13513e72c202SNinad Palsule     if (req.session == nullptr)
13523e72c202SNinad Palsule     {
13533e72c202SNinad Palsule         messages::internalError(asyncResp->res);
13543e72c202SNinad Palsule         return;
13553e72c202SNinad Palsule     }
13563e72c202SNinad Palsule 
1357afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1358afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1359afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/AccountService/AccountService.json>; rel=describedby");
1360afd369c6SJiaqing Zhao 
136152cc112dSEd Tanous     const persistent_data::AuthConfigMethods& authMethodsConfig =
13621ef4c342SEd Tanous         persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
136378158631SZbigniew Kurzynski 
13641476687dSEd Tanous     nlohmann::json& json = asyncResp->res.jsonValue;
13651476687dSEd Tanous     json["@odata.id"] = "/redfish/v1/AccountService";
13661476687dSEd Tanous     json["@odata.type"] = "#AccountService."
13671476687dSEd Tanous                           "v1_10_0.AccountService";
13681476687dSEd Tanous     json["Id"] = "AccountService";
13691476687dSEd Tanous     json["Name"] = "Account Service";
13701476687dSEd Tanous     json["Description"] = "Account Service";
13711476687dSEd Tanous     json["ServiceEnabled"] = true;
13721476687dSEd Tanous     json["MaxPasswordLength"] = 20;
13731ef4c342SEd Tanous     json["Accounts"]["@odata.id"] = "/redfish/v1/AccountService/Accounts";
13741476687dSEd Tanous     json["Roles"]["@odata.id"] = "/redfish/v1/AccountService/Roles";
13751476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.type"] =
13765b5574acSEd Tanous         "#OpenBMCAccountService.v1_0_0.AccountService";
13771476687dSEd Tanous     json["Oem"]["OpenBMC"]["@odata.id"] =
13781476687dSEd Tanous         "/redfish/v1/AccountService#/Oem/OpenBMC";
13791476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["BasicAuth"] =
13801476687dSEd Tanous         authMethodsConfig.basic;
13811476687dSEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["SessionToken"] =
13821476687dSEd Tanous         authMethodsConfig.sessionToken;
13831ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["XToken"] = authMethodsConfig.xtoken;
13841ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["Cookie"] = authMethodsConfig.cookie;
13851ef4c342SEd Tanous     json["Oem"]["OpenBMC"]["AuthMethods"]["TLS"] = authMethodsConfig.tls;
13861476687dSEd Tanous 
13871ef4c342SEd Tanous     // /redfish/v1/AccountService/LDAP/Certificates is something only
13881ef4c342SEd Tanous     // ConfigureManager can access then only display when the user has
13891ef4c342SEd Tanous     // permissions ConfigureManager
139072048780SAbhishek Patel     Privileges effectiveUserPrivileges =
13913e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
139272048780SAbhishek Patel 
139372048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
139472048780SAbhishek Patel                                          effectiveUserPrivileges))
139572048780SAbhishek Patel     {
13961ef4c342SEd Tanous         asyncResp->res.jsonValue["LDAP"]["Certificates"]["@odata.id"] =
13971476687dSEd Tanous             "/redfish/v1/AccountService/LDAP/Certificates";
139872048780SAbhishek Patel     }
1399d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
1400d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
1401d1bde9e5SKrzysztof Grobelny         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.AccountPolicy",
14025e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
14031ef4c342SEd Tanous                     const dbus::utility::DBusPropertiesMap& propertiesList) {
14043d958bbcSAppaRao Puli         if (ec)
14053d958bbcSAppaRao Puli         {
14063d958bbcSAppaRao Puli             messages::internalError(asyncResp->res);
14073d958bbcSAppaRao Puli             return;
14083d958bbcSAppaRao Puli         }
1409d1bde9e5SKrzysztof Grobelny 
14103d958bbcSAppaRao Puli         BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
14113d958bbcSAppaRao Puli                          << "properties for AccountService";
1412d1bde9e5SKrzysztof Grobelny 
1413d1bde9e5SKrzysztof Grobelny         const uint8_t* minPasswordLength = nullptr;
1414d1bde9e5SKrzysztof Grobelny         const uint32_t* accountUnlockTimeout = nullptr;
1415d1bde9e5SKrzysztof Grobelny         const uint16_t* maxLoginAttemptBeforeLockout = nullptr;
1416d1bde9e5SKrzysztof Grobelny 
1417d1bde9e5SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
1418d1bde9e5SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), propertiesList,
1419d1bde9e5SKrzysztof Grobelny             "MinPasswordLength", minPasswordLength, "AccountUnlockTimeout",
1420d1bde9e5SKrzysztof Grobelny             accountUnlockTimeout, "MaxLoginAttemptBeforeLockout",
1421d1bde9e5SKrzysztof Grobelny             maxLoginAttemptBeforeLockout);
1422d1bde9e5SKrzysztof Grobelny 
1423d1bde9e5SKrzysztof Grobelny         if (!success)
14243d958bbcSAppaRao Puli         {
1425d1bde9e5SKrzysztof Grobelny             messages::internalError(asyncResp->res);
1426d1bde9e5SKrzysztof Grobelny             return;
14273d958bbcSAppaRao Puli         }
1428d1bde9e5SKrzysztof Grobelny 
1429d1bde9e5SKrzysztof Grobelny         if (minPasswordLength != nullptr)
14303d958bbcSAppaRao Puli         {
1431d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["MinPasswordLength"] = *minPasswordLength;
14323d958bbcSAppaRao Puli         }
1433d1bde9e5SKrzysztof Grobelny 
1434d1bde9e5SKrzysztof Grobelny         if (accountUnlockTimeout != nullptr)
14353d958bbcSAppaRao Puli         {
1436d1bde9e5SKrzysztof Grobelny             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1437d1bde9e5SKrzysztof Grobelny                 *accountUnlockTimeout;
1438d1bde9e5SKrzysztof Grobelny         }
1439d1bde9e5SKrzysztof Grobelny 
1440d1bde9e5SKrzysztof Grobelny         if (maxLoginAttemptBeforeLockout != nullptr)
14413d958bbcSAppaRao Puli         {
1442002d39b4SEd Tanous             asyncResp->res.jsonValue["AccountLockoutThreshold"] =
1443d1bde9e5SKrzysztof Grobelny                 *maxLoginAttemptBeforeLockout;
14443d958bbcSAppaRao Puli         }
1445d1bde9e5SKrzysztof Grobelny         });
14466973a582SRatan Gupta 
144702cad96eSEd Tanous     auto callback = [asyncResp](bool success, const LDAPConfigData& confData,
1448ab828d7cSRatan Gupta                                 const std::string& ldapType) {
1449cb13a392SEd Tanous         if (!success)
1450cb13a392SEd Tanous         {
1451cb13a392SEd Tanous             return;
1452cb13a392SEd Tanous         }
1453002d39b4SEd Tanous         parseLDAPConfigData(asyncResp->res.jsonValue, confData, ldapType);
1454ab828d7cSRatan Gupta     };
1455ab828d7cSRatan Gupta 
1456ab828d7cSRatan Gupta     getLDAPConfigData("LDAP", callback);
1457ab828d7cSRatan Gupta     getLDAPConfigData("ActiveDirectory", callback);
14581ef4c342SEd Tanous }
14596973a582SRatan Gupta 
14601ef4c342SEd Tanous inline void handleAccountServicePatch(
14611ef4c342SEd Tanous     App& app, const crow::Request& req,
14621ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14631ef4c342SEd Tanous {
14643ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
146545ca1b86SEd Tanous     {
146645ca1b86SEd Tanous         return;
146745ca1b86SEd Tanous     }
1468f5ffd806SEd Tanous     std::optional<uint32_t> unlockTimeout;
1469f5ffd806SEd Tanous     std::optional<uint16_t> lockoutThreshold;
1470ef73ad0dSPaul Fertser     std::optional<uint8_t> minPasswordLength;
1471f5ffd806SEd Tanous     std::optional<uint16_t> maxPasswordLength;
1472f5ffd806SEd Tanous     std::optional<nlohmann::json> ldapObject;
1473f5ffd806SEd Tanous     std::optional<nlohmann::json> activeDirectoryObject;
1474f5ffd806SEd Tanous     std::optional<nlohmann::json> oemObject;
1475f5ffd806SEd Tanous 
147615ed6780SWilly Tu     if (!json_util::readJsonPatch(
14771ef4c342SEd Tanous             req, asyncResp->res, "AccountLockoutDuration", unlockTimeout,
14781ef4c342SEd Tanous             "AccountLockoutThreshold", lockoutThreshold, "MaxPasswordLength",
14791ef4c342SEd Tanous             maxPasswordLength, "MinPasswordLength", minPasswordLength, "LDAP",
14801ef4c342SEd Tanous             ldapObject, "ActiveDirectory", activeDirectoryObject, "Oem",
1481f5ffd806SEd Tanous             oemObject))
1482f5ffd806SEd Tanous     {
1483f5ffd806SEd Tanous         return;
1484f5ffd806SEd Tanous     }
1485f5ffd806SEd Tanous 
1486f5ffd806SEd Tanous     if (minPasswordLength)
1487f5ffd806SEd Tanous     {
1488ef73ad0dSPaul Fertser         crow::connections::systemBus->async_method_call(
14895e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1490ef73ad0dSPaul Fertser             if (ec)
1491ef73ad0dSPaul Fertser             {
1492ef73ad0dSPaul Fertser                 messages::internalError(asyncResp->res);
1493ef73ad0dSPaul Fertser                 return;
1494ef73ad0dSPaul Fertser             }
1495ef73ad0dSPaul Fertser             messages::success(asyncResp->res);
1496ef73ad0dSPaul Fertser             },
14971ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1498ef73ad0dSPaul Fertser             "org.freedesktop.DBus.Properties", "Set",
14991ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "MinPasswordLength",
1500ef73ad0dSPaul Fertser             dbus::utility::DbusVariantType(*minPasswordLength));
1501f5ffd806SEd Tanous     }
1502f5ffd806SEd Tanous 
1503f5ffd806SEd Tanous     if (maxPasswordLength)
1504f5ffd806SEd Tanous     {
15051ef4c342SEd Tanous         messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
1506f5ffd806SEd Tanous     }
1507f5ffd806SEd Tanous 
1508f5ffd806SEd Tanous     if (ldapObject)
1509f5ffd806SEd Tanous     {
1510f5ffd806SEd Tanous         handleLDAPPatch(*ldapObject, asyncResp, "LDAP");
1511f5ffd806SEd Tanous     }
1512f5ffd806SEd Tanous 
1513f5ffd806SEd Tanous     if (std::optional<nlohmann::json> oemOpenBMCObject;
15141ef4c342SEd Tanous         oemObject && json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
1515f5ffd806SEd Tanous                                          oemOpenBMCObject))
1516f5ffd806SEd Tanous     {
1517f5ffd806SEd Tanous         if (std::optional<nlohmann::json> authMethodsObject;
1518f5ffd806SEd Tanous             oemOpenBMCObject &&
1519f5ffd806SEd Tanous             json_util::readJson(*oemOpenBMCObject, asyncResp->res,
1520f5ffd806SEd Tanous                                 "AuthMethods", authMethodsObject))
1521f5ffd806SEd Tanous         {
1522f5ffd806SEd Tanous             if (authMethodsObject)
1523f5ffd806SEd Tanous             {
15241ef4c342SEd Tanous                 handleAuthMethodsPatch(*authMethodsObject, asyncResp);
1525f5ffd806SEd Tanous             }
1526f5ffd806SEd Tanous         }
1527f5ffd806SEd Tanous     }
1528f5ffd806SEd Tanous 
1529f5ffd806SEd Tanous     if (activeDirectoryObject)
1530f5ffd806SEd Tanous     {
15311ef4c342SEd Tanous         handleLDAPPatch(*activeDirectoryObject, asyncResp, "ActiveDirectory");
1532f5ffd806SEd Tanous     }
1533f5ffd806SEd Tanous 
1534f5ffd806SEd Tanous     if (unlockTimeout)
1535f5ffd806SEd Tanous     {
1536f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
15375e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1538f5ffd806SEd Tanous             if (ec)
1539f5ffd806SEd Tanous             {
1540f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1541f5ffd806SEd Tanous                 return;
1542f5ffd806SEd Tanous             }
1543f5ffd806SEd Tanous             messages::success(asyncResp->res);
1544f5ffd806SEd Tanous             },
15451ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1546f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
15471ef4c342SEd Tanous             "xyz.openbmc_project.User.AccountPolicy", "AccountUnlockTimeout",
1548168e20c1SEd Tanous             dbus::utility::DbusVariantType(*unlockTimeout));
1549f5ffd806SEd Tanous     }
1550f5ffd806SEd Tanous     if (lockoutThreshold)
1551f5ffd806SEd Tanous     {
1552f5ffd806SEd Tanous         crow::connections::systemBus->async_method_call(
15535e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1554f5ffd806SEd Tanous             if (ec)
1555f5ffd806SEd Tanous             {
1556f5ffd806SEd Tanous                 messages::internalError(asyncResp->res);
1557f5ffd806SEd Tanous                 return;
1558f5ffd806SEd Tanous             }
1559f5ffd806SEd Tanous             messages::success(asyncResp->res);
1560f5ffd806SEd Tanous             },
15611ef4c342SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1562f5ffd806SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1563f5ffd806SEd Tanous             "xyz.openbmc_project.User.AccountPolicy",
1564f5ffd806SEd Tanous             "MaxLoginAttemptBeforeLockout",
1565168e20c1SEd Tanous             dbus::utility::DbusVariantType(*lockoutThreshold));
1566f5ffd806SEd Tanous     }
15671ef4c342SEd Tanous }
1568f5ffd806SEd Tanous 
15694c7d4d33SEd Tanous inline void handleAccountCollectionHead(
15701ef4c342SEd Tanous     App& app, const crow::Request& req,
15711ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15721ef4c342SEd Tanous {
15733ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
157445ca1b86SEd Tanous     {
157545ca1b86SEd Tanous         return;
157645ca1b86SEd Tanous     }
15774c7d4d33SEd Tanous     asyncResp->res.addHeader(
15784c7d4d33SEd Tanous         boost::beast::http::field::link,
15794c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
15804c7d4d33SEd Tanous }
15814c7d4d33SEd Tanous 
15824c7d4d33SEd Tanous inline void handleAccountCollectionGet(
15834c7d4d33SEd Tanous     App& app, const crow::Request& req,
15844c7d4d33SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
15854c7d4d33SEd Tanous {
1586afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1587afd369c6SJiaqing Zhao     {
1588afd369c6SJiaqing Zhao         return;
1589afd369c6SJiaqing Zhao     }
15903e72c202SNinad Palsule 
15913e72c202SNinad Palsule     if (req.session == nullptr)
15923e72c202SNinad Palsule     {
15933e72c202SNinad Palsule         messages::internalError(asyncResp->res);
15943e72c202SNinad Palsule         return;
15953e72c202SNinad Palsule     }
15963e72c202SNinad Palsule 
1597afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1598afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1599afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccountCollection.json>; rel=describedby");
16001476687dSEd Tanous 
16011476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
16021476687dSEd Tanous         "/redfish/v1/AccountService/Accounts";
16031ef4c342SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#ManagerAccountCollection."
16041476687dSEd Tanous                                               "ManagerAccountCollection";
16051476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Accounts Collection";
16061476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "BMC User Accounts";
16070f74e643SEd Tanous 
16086c51eab1SEd Tanous     Privileges effectiveUserPrivileges =
16093e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
16106c51eab1SEd Tanous 
1611f5e29f33SJunLin Chen     std::string thisUser;
1612f5e29f33SJunLin Chen     if (req.session)
1613f5e29f33SJunLin Chen     {
1614f5e29f33SJunLin Chen         thisUser = req.session->username;
1615f5e29f33SJunLin Chen     }
1616b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
1617cef1ddfbSEd Tanous         [asyncResp, thisUser, effectiveUserPrivileges](
16185e7e2dc5SEd Tanous             const boost::system::error_code& ec,
1619711ac7a9SEd Tanous             const dbus::utility::ManagedObjectType& users) {
1620b9b2e0b2SEd Tanous         if (ec)
1621b9b2e0b2SEd Tanous         {
1622f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1623b9b2e0b2SEd Tanous             return;
1624b9b2e0b2SEd Tanous         }
1625b9b2e0b2SEd Tanous 
1626cef1ddfbSEd Tanous         bool userCanSeeAllAccounts =
1627002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"});
1628cef1ddfbSEd Tanous 
1629cef1ddfbSEd Tanous         bool userCanSeeSelf =
1630002d39b4SEd Tanous             effectiveUserPrivileges.isSupersetOf({"ConfigureSelf"});
1631cef1ddfbSEd Tanous 
1632002d39b4SEd Tanous         nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1633b9b2e0b2SEd Tanous         memberArray = nlohmann::json::array();
1634b9b2e0b2SEd Tanous 
16359eb808c1SEd Tanous         for (const auto& userpath : users)
1636b9b2e0b2SEd Tanous         {
16372dfd18efSEd Tanous             std::string user = userpath.first.filename();
16382dfd18efSEd Tanous             if (user.empty())
1639b9b2e0b2SEd Tanous             {
16402dfd18efSEd Tanous                 messages::internalError(asyncResp->res);
16412dfd18efSEd Tanous                 BMCWEB_LOG_ERROR << "Invalid firmware ID";
16422dfd18efSEd Tanous 
16432dfd18efSEd Tanous                 return;
1644b9b2e0b2SEd Tanous             }
1645f365910cSGunnar Mills 
1646f365910cSGunnar Mills             // As clarified by Redfish here:
1647f365910cSGunnar Mills             // https://redfishforum.com/thread/281/manageraccountcollection-change-allows-account-enumeration
16486c51eab1SEd Tanous             // Users without ConfigureUsers, only see their own
16496c51eab1SEd Tanous             // account. Users with ConfigureUsers, see all
16506c51eab1SEd Tanous             // accounts.
16511ef4c342SEd Tanous             if (userCanSeeAllAccounts || (thisUser == user && userCanSeeSelf))
1652f365910cSGunnar Mills             {
16531476687dSEd Tanous                 nlohmann::json::object_t member;
165489492a15SPatrick Williams                 member["@odata.id"] = "/redfish/v1/AccountService/Accounts/" +
165589492a15SPatrick Williams                                       user;
1656b2ba3072SPatrick Williams                 memberArray.emplace_back(std::move(member));
1657b9b2e0b2SEd Tanous             }
1658f365910cSGunnar Mills         }
16591ef4c342SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1660b9b2e0b2SEd Tanous         },
16611ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1662b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16631ef4c342SEd Tanous }
16646c51eab1SEd Tanous 
166597e90da3SNinad Palsule inline void processAfterCreateUser(
166697e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
166797e90da3SNinad Palsule     const std::string& username, const std::string& password,
166897e90da3SNinad Palsule     const boost::system::error_code& ec, sdbusplus::message_t& m)
166997e90da3SNinad Palsule {
167097e90da3SNinad Palsule     if (ec)
167197e90da3SNinad Palsule     {
167297e90da3SNinad Palsule         userErrorMessageHandler(m.get_error(), asyncResp, username, "");
167397e90da3SNinad Palsule         return;
167497e90da3SNinad Palsule     }
167597e90da3SNinad Palsule 
167697e90da3SNinad Palsule     if (pamUpdatePassword(username, password) != PAM_SUCCESS)
167797e90da3SNinad Palsule     {
167897e90da3SNinad Palsule         // At this point we have a user that's been
167997e90da3SNinad Palsule         // created, but the password set
168097e90da3SNinad Palsule         // failed.Something is wrong, so delete the user
168197e90da3SNinad Palsule         // that we've already created
168297e90da3SNinad Palsule         sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
168397e90da3SNinad Palsule         tempObjPath /= username;
168497e90da3SNinad Palsule         const std::string userPath(tempObjPath);
168597e90da3SNinad Palsule 
168697e90da3SNinad Palsule         crow::connections::systemBus->async_method_call(
168797e90da3SNinad Palsule             [asyncResp, password](const boost::system::error_code& ec3) {
168897e90da3SNinad Palsule             if (ec3)
168997e90da3SNinad Palsule             {
169097e90da3SNinad Palsule                 messages::internalError(asyncResp->res);
169197e90da3SNinad Palsule                 return;
169297e90da3SNinad Palsule             }
169397e90da3SNinad Palsule 
169497e90da3SNinad Palsule             // If password is invalid
169597e90da3SNinad Palsule             messages::propertyValueFormatError(asyncResp->res, password,
169697e90da3SNinad Palsule                                                "Password");
169797e90da3SNinad Palsule             },
169897e90da3SNinad Palsule             "xyz.openbmc_project.User.Manager", userPath,
169997e90da3SNinad Palsule             "xyz.openbmc_project.Object.Delete", "Delete");
170097e90da3SNinad Palsule 
170197e90da3SNinad Palsule         BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
170297e90da3SNinad Palsule         return;
170397e90da3SNinad Palsule     }
170497e90da3SNinad Palsule 
170597e90da3SNinad Palsule     messages::created(asyncResp->res);
170697e90da3SNinad Palsule     asyncResp->res.addHeader("Location",
170797e90da3SNinad Palsule                              "/redfish/v1/AccountService/Accounts/" + username);
170897e90da3SNinad Palsule }
170997e90da3SNinad Palsule 
171097e90da3SNinad Palsule inline void processAfterGetAllGroups(
171197e90da3SNinad Palsule     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
171297e90da3SNinad Palsule     const std::string& username, const std::string& password,
171397e90da3SNinad Palsule     const std::optional<std::string>& roleId, std::optional<bool> enabled,
171497e90da3SNinad Palsule     const std::vector<std::string>& allGroupsList)
171597e90da3SNinad Palsule 
171697e90da3SNinad Palsule {
17173e72c202SNinad Palsule     std::vector<std::string> userGroups;
17183e72c202SNinad Palsule     for (const auto& grp : allGroupsList)
17193e72c202SNinad Palsule     {
17203e72c202SNinad Palsule         // Console access is provided to the user who is a member of
17213e72c202SNinad Palsule         // hostconsole group and has a administrator role. So, set
17223e72c202SNinad Palsule         // hostconsole group only for the administrator.
17233e72c202SNinad Palsule         if ((grp != "hostconsole") || (roleId == "priv-admin"))
17243e72c202SNinad Palsule         {
17253e72c202SNinad Palsule             userGroups.emplace_back(grp);
17263e72c202SNinad Palsule         }
17273e72c202SNinad Palsule     }
17283e72c202SNinad Palsule 
172997e90da3SNinad Palsule     crow::connections::systemBus->async_method_call(
173097e90da3SNinad Palsule         [asyncResp, username, password](const boost::system::error_code& ec2,
173197e90da3SNinad Palsule                                         sdbusplus::message_t& m) {
173297e90da3SNinad Palsule         processAfterCreateUser(asyncResp, username, password, ec2, m);
173397e90da3SNinad Palsule         },
173497e90da3SNinad Palsule         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
17353e72c202SNinad Palsule         "xyz.openbmc_project.User.Manager", "CreateUser", username, userGroups,
17363e72c202SNinad Palsule         *roleId, *enabled);
173797e90da3SNinad Palsule }
173897e90da3SNinad Palsule 
17391ef4c342SEd Tanous inline void handleAccountCollectionPost(
17401ef4c342SEd Tanous     App& app, const crow::Request& req,
17411ef4c342SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
17421ef4c342SEd Tanous {
17433ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
174445ca1b86SEd Tanous     {
174545ca1b86SEd Tanous         return;
174645ca1b86SEd Tanous     }
17479712f8acSEd Tanous     std::string username;
17489712f8acSEd Tanous     std::string password;
1749a24526dcSEd Tanous     std::optional<std::string> roleId("User");
1750a24526dcSEd Tanous     std::optional<bool> enabled = true;
17511ef4c342SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
17521ef4c342SEd Tanous                                   "Password", password, "RoleId", roleId,
17531ef4c342SEd Tanous                                   "Enabled", enabled))
175404ae99ecSEd Tanous     {
175504ae99ecSEd Tanous         return;
175604ae99ecSEd Tanous     }
175704ae99ecSEd Tanous 
175854fc587aSNagaraju Goruganti     std::string priv = getPrivilegeFromRoleId(*roleId);
175984e12cb7SAppaRao Puli     if (priv.empty())
176004ae99ecSEd Tanous     {
17611ef4c342SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
176204ae99ecSEd Tanous         return;
176304ae99ecSEd Tanous     }
17649712f8acSEd Tanous     roleId = priv;
176504ae99ecSEd Tanous 
1766599c71d8SAyushi Smriti     // Reading AllGroups property
17671e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::vector<std::string>>(
17681ef4c342SEd Tanous         *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
17691ef4c342SEd Tanous         "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
17701ef4c342SEd Tanous         "AllGroups",
1771599c71d8SAyushi Smriti         [asyncResp, username, password{std::move(password)}, roleId,
17725e7e2dc5SEd Tanous          enabled](const boost::system::error_code& ec,
17731e1e598dSJonathan Doman                   const std::vector<std::string>& allGroupsList) {
1774599c71d8SAyushi Smriti         if (ec)
1775599c71d8SAyushi Smriti         {
1776599c71d8SAyushi Smriti             BMCWEB_LOG_DEBUG << "ERROR with async_method_call";
1777599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1778599c71d8SAyushi Smriti             return;
1779599c71d8SAyushi Smriti         }
1780599c71d8SAyushi Smriti 
17811e1e598dSJonathan Doman         if (allGroupsList.empty())
1782599c71d8SAyushi Smriti         {
1783599c71d8SAyushi Smriti             messages::internalError(asyncResp->res);
1784599c71d8SAyushi Smriti             return;
1785599c71d8SAyushi Smriti         }
1786599c71d8SAyushi Smriti 
178797e90da3SNinad Palsule         processAfterGetAllGroups(asyncResp, username, password, roleId, enabled,
178897e90da3SNinad Palsule                                  allGroupsList);
17891e1e598dSJonathan Doman         });
17901ef4c342SEd Tanous }
1791b9b2e0b2SEd Tanous 
17921ef4c342SEd Tanous inline void
17934c7d4d33SEd Tanous     handleAccountHead(App& app, const crow::Request& req,
17946c51eab1SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17954c7d4d33SEd Tanous                       const std::string& /*accountName*/)
17961ef4c342SEd Tanous {
17973ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
179845ca1b86SEd Tanous     {
179945ca1b86SEd Tanous         return;
180045ca1b86SEd Tanous     }
18014c7d4d33SEd Tanous     asyncResp->res.addHeader(
18024c7d4d33SEd Tanous         boost::beast::http::field::link,
18034c7d4d33SEd Tanous         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
18044c7d4d33SEd Tanous }
1805afd369c6SJiaqing Zhao 
18064c7d4d33SEd Tanous inline void
18074c7d4d33SEd Tanous     handleAccountGet(App& app, const crow::Request& req,
18084c7d4d33SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18094c7d4d33SEd Tanous                      const std::string& accountName)
18104c7d4d33SEd Tanous {
1811afd369c6SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1812afd369c6SJiaqing Zhao     {
1813afd369c6SJiaqing Zhao         return;
1814afd369c6SJiaqing Zhao     }
1815afd369c6SJiaqing Zhao     asyncResp->res.addHeader(
1816afd369c6SJiaqing Zhao         boost::beast::http::field::link,
1817afd369c6SJiaqing Zhao         "</redfish/v1/JsonSchemas/ManagerAccount/ManagerAccount.json>; rel=describedby");
1818afd369c6SJiaqing Zhao 
18191ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1820031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1821d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", accountName);
1822031514fbSJunLin Chen     return;
18231ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1824afd369c6SJiaqing Zhao 
1825031514fbSJunLin Chen     if (req.session == nullptr)
1826031514fbSJunLin Chen     {
1827031514fbSJunLin Chen         messages::internalError(asyncResp->res);
1828031514fbSJunLin Chen         return;
1829031514fbSJunLin Chen     }
18306c51eab1SEd Tanous     if (req.session->username != accountName)
1831b9b2e0b2SEd Tanous     {
18326c51eab1SEd Tanous         // At this point we've determined that the user is trying to
18331ef4c342SEd Tanous         // modify a user that isn't them.  We need to verify that they
18341ef4c342SEd Tanous         // have permissions to modify other users, so re-run the auth
18351ef4c342SEd Tanous         // check with the same permissions, minus ConfigureSelf.
18366c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
18373e72c202SNinad Palsule             redfish::getUserPrivileges(*req.session);
18381ef4c342SEd Tanous         Privileges requiredPermissionsToChangeNonSelf = {"ConfigureUsers",
18391ef4c342SEd Tanous                                                          "ConfigureManager"};
18406c51eab1SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf(
18416c51eab1SEd Tanous                 requiredPermissionsToChangeNonSelf))
1842900f9497SJoseph Reynolds         {
1843900f9497SJoseph Reynolds             BMCWEB_LOG_DEBUG << "GET Account denied access";
1844900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
1845900f9497SJoseph Reynolds             return;
1846900f9497SJoseph Reynolds         }
1847900f9497SJoseph Reynolds     }
1848900f9497SJoseph Reynolds 
1849b9b2e0b2SEd Tanous     crow::connections::systemBus->async_method_call(
18501ef4c342SEd Tanous         [asyncResp,
18515e7e2dc5SEd Tanous          accountName](const boost::system::error_code& ec,
1852711ac7a9SEd Tanous                       const dbus::utility::ManagedObjectType& users) {
1853b9b2e0b2SEd Tanous         if (ec)
1854b9b2e0b2SEd Tanous         {
1855f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1856b9b2e0b2SEd Tanous             return;
1857b9b2e0b2SEd Tanous         }
1858711ac7a9SEd Tanous         const auto userIt = std::find_if(
1859b477fd44SP Dheeraj Srujan Kumar             users.begin(), users.end(),
1860b477fd44SP Dheeraj Srujan Kumar             [accountName](
1861b477fd44SP Dheeraj Srujan Kumar                 const std::pair<sdbusplus::message::object_path,
1862002d39b4SEd Tanous                                 dbus::utility::DBusInteracesMap>& user) {
186355f79e6fSEd Tanous             return accountName == user.first.filename();
1864b477fd44SP Dheeraj Srujan Kumar             });
1865b9b2e0b2SEd Tanous 
186684e12cb7SAppaRao Puli         if (userIt == users.end())
1867b9b2e0b2SEd Tanous         {
1868002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
1869002d39b4SEd Tanous                                        accountName);
187084e12cb7SAppaRao Puli             return;
187184e12cb7SAppaRao Puli         }
18724e68c45bSAyushi Smriti 
18731476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
18741476687dSEd Tanous             "#ManagerAccount.v1_4_0.ManagerAccount";
18751476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Account";
18761476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "User Account";
18771476687dSEd Tanous         asyncResp->res.jsonValue["Password"] = nullptr;
18784e68c45bSAyushi Smriti 
187984e12cb7SAppaRao Puli         for (const auto& interface : userIt->second)
188065b0dc32SEd Tanous         {
1881002d39b4SEd Tanous             if (interface.first == "xyz.openbmc_project.User.Attributes")
188265b0dc32SEd Tanous             {
188365b0dc32SEd Tanous                 for (const auto& property : interface.second)
188465b0dc32SEd Tanous                 {
188565b0dc32SEd Tanous                     if (property.first == "UserEnabled")
188665b0dc32SEd Tanous                     {
188765b0dc32SEd Tanous                         const bool* userEnabled =
1888abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
188965b0dc32SEd Tanous                         if (userEnabled == nullptr)
189065b0dc32SEd Tanous                         {
1891002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserEnabled wasn't a bool";
189284e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
189384e12cb7SAppaRao Puli                             return;
189465b0dc32SEd Tanous                         }
1895002d39b4SEd Tanous                         asyncResp->res.jsonValue["Enabled"] = *userEnabled;
189665b0dc32SEd Tanous                     }
1897002d39b4SEd Tanous                     else if (property.first == "UserLockedForFailedAttempt")
189865b0dc32SEd Tanous                     {
189965b0dc32SEd Tanous                         const bool* userLocked =
1900abf2add6SEd Tanous                             std::get_if<bool>(&property.second);
190165b0dc32SEd Tanous                         if (userLocked == nullptr)
190265b0dc32SEd Tanous                         {
190384e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "UserLockedForF"
190484e12cb7SAppaRao Puli                                                 "ailedAttempt "
190584e12cb7SAppaRao Puli                                                 "wasn't a bool";
190684e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
190784e12cb7SAppaRao Puli                             return;
190865b0dc32SEd Tanous                         }
1909002d39b4SEd Tanous                         asyncResp->res.jsonValue["Locked"] = *userLocked;
1910002d39b4SEd Tanous                         asyncResp->res
1911002d39b4SEd Tanous                             .jsonValue["Locked@Redfish.AllowableValues"] = {
19123bf4e632SJoseph Reynolds                             "false"}; // can only unlock accounts
191365b0dc32SEd Tanous                     }
191484e12cb7SAppaRao Puli                     else if (property.first == "UserPrivilege")
191584e12cb7SAppaRao Puli                     {
191654fc587aSNagaraju Goruganti                         const std::string* userPrivPtr =
1917002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
191854fc587aSNagaraju Goruganti                         if (userPrivPtr == nullptr)
191984e12cb7SAppaRao Puli                         {
1920002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "UserPrivilege wasn't a "
192184e12cb7SAppaRao Puli                                                 "string";
192284e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
192384e12cb7SAppaRao Puli                             return;
192484e12cb7SAppaRao Puli                         }
19251ef4c342SEd Tanous                         std::string role = getRoleIdFromPrivilege(*userPrivPtr);
192654fc587aSNagaraju Goruganti                         if (role.empty())
192784e12cb7SAppaRao Puli                         {
192884e12cb7SAppaRao Puli                             BMCWEB_LOG_ERROR << "Invalid user role";
192984e12cb7SAppaRao Puli                             messages::internalError(asyncResp->res);
193084e12cb7SAppaRao Puli                             return;
193184e12cb7SAppaRao Puli                         }
193254fc587aSNagaraju Goruganti                         asyncResp->res.jsonValue["RoleId"] = role;
193384e12cb7SAppaRao Puli 
19341476687dSEd Tanous                         nlohmann::json& roleEntry =
1935002d39b4SEd Tanous                             asyncResp->res.jsonValue["Links"]["Role"];
19361476687dSEd Tanous                         roleEntry["@odata.id"] =
1937002d39b4SEd Tanous                             "/redfish/v1/AccountService/Roles/" + role;
193884e12cb7SAppaRao Puli                     }
1939002d39b4SEd Tanous                     else if (property.first == "UserPasswordExpired")
19403bf4e632SJoseph Reynolds                     {
19413bf4e632SJoseph Reynolds                         const bool* userPasswordExpired =
19423bf4e632SJoseph Reynolds                             std::get_if<bool>(&property.second);
19433bf4e632SJoseph Reynolds                         if (userPasswordExpired == nullptr)
19443bf4e632SJoseph Reynolds                         {
19450fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
19460fda0f12SGeorge Liu                                 << "UserPasswordExpired wasn't a bool";
19473bf4e632SJoseph Reynolds                             messages::internalError(asyncResp->res);
19483bf4e632SJoseph Reynolds                             return;
19493bf4e632SJoseph Reynolds                         }
1950002d39b4SEd Tanous                         asyncResp->res.jsonValue["PasswordChangeRequired"] =
19513bf4e632SJoseph Reynolds                             *userPasswordExpired;
19523bf4e632SJoseph Reynolds                     }
1953c7229815SAbhishek Patel                     else if (property.first == "UserGroups")
1954c7229815SAbhishek Patel                     {
1955c7229815SAbhishek Patel                         const std::vector<std::string>* userGroups =
1956c7229815SAbhishek Patel                             std::get_if<std::vector<std::string>>(
1957c7229815SAbhishek Patel                                 &property.second);
1958c7229815SAbhishek Patel                         if (userGroups == nullptr)
1959c7229815SAbhishek Patel                         {
1960c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR
1961c7229815SAbhishek Patel                                 << "userGroups wasn't a string vector";
1962c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1963c7229815SAbhishek Patel                             return;
1964c7229815SAbhishek Patel                         }
1965c7229815SAbhishek Patel                         if (!translateUserGroup(*userGroups, asyncResp->res))
1966c7229815SAbhishek Patel                         {
1967c7229815SAbhishek Patel                             BMCWEB_LOG_ERROR << "userGroups mapping failed";
1968c7229815SAbhishek Patel                             messages::internalError(asyncResp->res);
1969c7229815SAbhishek Patel                             return;
1970c7229815SAbhishek Patel                         }
1971c7229815SAbhishek Patel                     }
197265b0dc32SEd Tanous                 }
197365b0dc32SEd Tanous             }
197465b0dc32SEd Tanous         }
197565b0dc32SEd Tanous 
1976b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
197784e12cb7SAppaRao Puli             "/redfish/v1/AccountService/Accounts/" + accountName;
1978b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["Id"] = accountName;
1979b9b2e0b2SEd Tanous         asyncResp->res.jsonValue["UserName"] = accountName;
1980b9b2e0b2SEd Tanous         },
19811ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1982b9b2e0b2SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19831ef4c342SEd Tanous }
1984a840879dSEd Tanous 
19851ef4c342SEd Tanous inline void
198620fc307fSGunnar Mills     handleAccountDelete(App& app, const crow::Request& req,
19876c51eab1SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19881ef4c342SEd Tanous                         const std::string& username)
19891ef4c342SEd Tanous {
19903ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
199145ca1b86SEd Tanous     {
199245ca1b86SEd Tanous         return;
199345ca1b86SEd Tanous     }
19941ef4c342SEd Tanous 
19951ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
1996031514fbSJunLin Chen     // If authentication is disabled, there are no user accounts
1997d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
1998031514fbSJunLin Chen     return;
1999031514fbSJunLin Chen 
20001ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
20011ef4c342SEd Tanous     sdbusplus::message::object_path tempObjPath(rootUserDbusPath);
20021ef4c342SEd Tanous     tempObjPath /= username;
20031ef4c342SEd Tanous     const std::string userPath(tempObjPath);
20041ef4c342SEd Tanous 
20051ef4c342SEd Tanous     crow::connections::systemBus->async_method_call(
20065e7e2dc5SEd Tanous         [asyncResp, username](const boost::system::error_code& ec) {
20071ef4c342SEd Tanous         if (ec)
20081ef4c342SEd Tanous         {
2009d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "ManagerAccount",
20101ef4c342SEd Tanous                                        username);
20111ef4c342SEd Tanous             return;
20121ef4c342SEd Tanous         }
20131ef4c342SEd Tanous 
20141ef4c342SEd Tanous         messages::accountRemoved(asyncResp->res);
20151ef4c342SEd Tanous         },
20161ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", userPath,
20171ef4c342SEd Tanous         "xyz.openbmc_project.Object.Delete", "Delete");
20181ef4c342SEd Tanous }
20191ef4c342SEd Tanous 
20201ef4c342SEd Tanous inline void
20211ef4c342SEd Tanous     handleAccountPatch(App& app, const crow::Request& req,
20221ef4c342SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20231ef4c342SEd Tanous                        const std::string& username)
20241ef4c342SEd Tanous {
20251ef4c342SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
20261ef4c342SEd Tanous     {
20271ef4c342SEd Tanous         return;
20281ef4c342SEd Tanous     }
20291ef4c342SEd Tanous #ifdef BMCWEB_INSECURE_DISABLE_AUTHENTICATION
20301ef4c342SEd Tanous     // If authentication is disabled, there are no user accounts
2031d8a5d5d8SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "ManagerAccount", username);
20321ef4c342SEd Tanous     return;
20331ef4c342SEd Tanous 
20341ef4c342SEd Tanous #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION
2035a24526dcSEd Tanous     std::optional<std::string> newUserName;
2036a24526dcSEd Tanous     std::optional<std::string> password;
2037a24526dcSEd Tanous     std::optional<bool> enabled;
2038a24526dcSEd Tanous     std::optional<std::string> roleId;
203924c8542dSRatan Gupta     std::optional<bool> locked;
2040e9cc5172SEd Tanous 
2041031514fbSJunLin Chen     if (req.session == nullptr)
2042031514fbSJunLin Chen     {
2043031514fbSJunLin Chen         messages::internalError(asyncResp->res);
2044031514fbSJunLin Chen         return;
2045031514fbSJunLin Chen     }
2046031514fbSJunLin Chen 
2047e9cc5172SEd Tanous     Privileges effectiveUserPrivileges =
20483e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
2049e9cc5172SEd Tanous     Privileges configureUsers = {"ConfigureUsers"};
2050e9cc5172SEd Tanous     bool userHasConfigureUsers =
2051e9cc5172SEd Tanous         effectiveUserPrivileges.isSupersetOf(configureUsers);
2052e9cc5172SEd Tanous     if (userHasConfigureUsers)
2053e9cc5172SEd Tanous     {
2054e9cc5172SEd Tanous         // Users with ConfigureUsers can modify for all users
20551ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "UserName",
20561ef4c342SEd Tanous                                       newUserName, "Password", password,
20571ef4c342SEd Tanous                                       "RoleId", roleId, "Enabled", enabled,
20581ef4c342SEd Tanous                                       "Locked", locked))
2059a840879dSEd Tanous         {
2060a840879dSEd Tanous             return;
2061a840879dSEd Tanous         }
2062e9cc5172SEd Tanous     }
2063e9cc5172SEd Tanous     else
2064900f9497SJoseph Reynolds     {
2065e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their own account
2066e9cc5172SEd Tanous         if (username != req.session->username)
2067900f9497SJoseph Reynolds         {
2068900f9497SJoseph Reynolds             messages::insufficientPrivilege(asyncResp->res);
2069900f9497SJoseph Reynolds             return;
2070900f9497SJoseph Reynolds         }
2071031514fbSJunLin Chen 
2072e9cc5172SEd Tanous         // ConfigureSelf accounts can only modify their password
20731ef4c342SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "Password",
20741ef4c342SEd Tanous                                       password))
2075e9cc5172SEd Tanous         {
2076e9cc5172SEd Tanous             return;
2077e9cc5172SEd Tanous         }
2078900f9497SJoseph Reynolds     }
2079900f9497SJoseph Reynolds 
208066b5ca76Sjayaprakash Mutyala     // if user name is not provided in the patch method or if it
20816c51eab1SEd Tanous     // matches the user name in the URI, then we are treating it as
20826c51eab1SEd Tanous     // updating user properties other then username. If username
20836c51eab1SEd Tanous     // provided doesn't match the URI, then we are treating this as
20846c51eab1SEd Tanous     // user rename request.
208566b5ca76Sjayaprakash Mutyala     if (!newUserName || (newUserName.value() == username))
2086a840879dSEd Tanous     {
20871ef4c342SEd Tanous         updateUserProperties(asyncResp, username, password, enabled, roleId,
20881ef4c342SEd Tanous                              locked);
208984e12cb7SAppaRao Puli         return;
209084e12cb7SAppaRao Puli     }
209184e12cb7SAppaRao Puli     crow::connections::systemBus->async_method_call(
20926c51eab1SEd Tanous         [asyncResp, username, password(std::move(password)),
20931ef4c342SEd Tanous          roleId(std::move(roleId)), enabled, newUser{std::string(*newUserName)},
20945e7e2dc5SEd Tanous          locked](const boost::system::error_code& ec, sdbusplus::message_t& m) {
209584e12cb7SAppaRao Puli         if (ec)
209684e12cb7SAppaRao Puli         {
2097002d39b4SEd Tanous             userErrorMessageHandler(m.get_error(), asyncResp, newUser,
2098002d39b4SEd Tanous                                     username);
2099a840879dSEd Tanous             return;
2100a840879dSEd Tanous         }
2101a840879dSEd Tanous 
2102002d39b4SEd Tanous         updateUserProperties(asyncResp, newUser, password, enabled, roleId,
2103002d39b4SEd Tanous                              locked);
210484e12cb7SAppaRao Puli         },
21051ef4c342SEd Tanous         "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
210684e12cb7SAppaRao Puli         "xyz.openbmc_project.User.Manager", "RenameUser", username,
210784e12cb7SAppaRao Puli         *newUserName);
21081ef4c342SEd Tanous }
21091ef4c342SEd Tanous 
21101ef4c342SEd Tanous inline void requestAccountServiceRoutes(App& app)
21111ef4c342SEd Tanous {
21121ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
21134c7d4d33SEd Tanous         .privileges(redfish::privileges::headAccountService)
21144c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
21154c7d4d33SEd Tanous             std::bind_front(handleAccountServiceHead, std::ref(app)));
21164c7d4d33SEd Tanous 
21174c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
21181ef4c342SEd Tanous         .privileges(redfish::privileges::getAccountService)
21191ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
21201ef4c342SEd Tanous             std::bind_front(handleAccountServiceGet, std::ref(app)));
21211ef4c342SEd Tanous 
21221ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/")
21231ef4c342SEd Tanous         .privileges(redfish::privileges::patchAccountService)
21241ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
21251ef4c342SEd Tanous             std::bind_front(handleAccountServicePatch, std::ref(app)));
21261ef4c342SEd Tanous 
21271ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
21284c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccountCollection)
21294c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
21304c7d4d33SEd Tanous             std::bind_front(handleAccountCollectionHead, std::ref(app)));
21314c7d4d33SEd Tanous 
21324c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
21331ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccountCollection)
21341ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
21351ef4c342SEd Tanous             std::bind_front(handleAccountCollectionGet, std::ref(app)));
21361ef4c342SEd Tanous 
21371ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
21381ef4c342SEd Tanous         .privileges(redfish::privileges::postManagerAccountCollection)
21391ef4c342SEd Tanous         .methods(boost::beast::http::verb::post)(
21401ef4c342SEd Tanous             std::bind_front(handleAccountCollectionPost, std::ref(app)));
21411ef4c342SEd Tanous 
21421ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
21434c7d4d33SEd Tanous         .privileges(redfish::privileges::headManagerAccount)
21444c7d4d33SEd Tanous         .methods(boost::beast::http::verb::head)(
21454c7d4d33SEd Tanous             std::bind_front(handleAccountHead, std::ref(app)));
21464c7d4d33SEd Tanous 
21474c7d4d33SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
21481ef4c342SEd Tanous         .privileges(redfish::privileges::getManagerAccount)
21491ef4c342SEd Tanous         .methods(boost::beast::http::verb::get)(
21501ef4c342SEd Tanous             std::bind_front(handleAccountGet, std::ref(app)));
21511ef4c342SEd Tanous 
21521ef4c342SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
21531ef4c342SEd Tanous         // TODO this privilege should be using the generated endpoints, but
21541ef4c342SEd Tanous         // because of the special handling of ConfigureSelf, it's not able to
21551ef4c342SEd Tanous         // yet
21561ef4c342SEd Tanous         .privileges({{"ConfigureUsers"}, {"ConfigureSelf"}})
21571ef4c342SEd Tanous         .methods(boost::beast::http::verb::patch)(
21581ef4c342SEd Tanous             std::bind_front(handleAccountPatch, std::ref(app)));
215984e12cb7SAppaRao Puli 
21606c51eab1SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
2161ed398213SEd Tanous         .privileges(redfish::privileges::deleteManagerAccount)
21626c51eab1SEd Tanous         .methods(boost::beast::http::verb::delete_)(
216320fc307fSGunnar Mills             std::bind_front(handleAccountDelete, std::ref(app)));
216406e086d9SEd Tanous }
216588d16c9aSLewanczyk, Dawid 
216688d16c9aSLewanczyk, Dawid } // namespace redfish
2167