xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision add6133b61dca3568063f1b564f24e8cee0ba578)
188d16c9aSLewanczyk, Dawid /*
288d16c9aSLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
388d16c9aSLewanczyk, Dawid //
488d16c9aSLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
588d16c9aSLewanczyk, Dawid // you may not use this file except in compliance with the License.
688d16c9aSLewanczyk, Dawid // You may obtain a copy of the License at
788d16c9aSLewanczyk, Dawid //
888d16c9aSLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
988d16c9aSLewanczyk, Dawid //
1088d16c9aSLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1188d16c9aSLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1288d16c9aSLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388d16c9aSLewanczyk, Dawid // See the License for the specific language governing permissions and
1488d16c9aSLewanczyk, Dawid // limitations under the License.
1588d16c9aSLewanczyk, Dawid */
1688d16c9aSLewanczyk, Dawid #pragma once
1788d16c9aSLewanczyk, Dawid #include "node.hpp"
1888d16c9aSLewanczyk, Dawid 
1924c8542dSRatan Gupta #include <dbus_utility.hpp>
2065b0dc32SEd Tanous #include <error_messages.hpp>
21b9b2e0b2SEd Tanous #include <openbmc_dbus_rest.hpp>
22a840879dSEd Tanous #include <utils/json_utils.hpp>
23abf2add6SEd Tanous #include <variant>
24b9b2e0b2SEd Tanous 
251abe55efSEd Tanous namespace redfish
261abe55efSEd Tanous {
2788d16c9aSLewanczyk, Dawid 
28b9b2e0b2SEd Tanous using ManagedObjectType = std::vector<std::pair<
29b9b2e0b2SEd Tanous     sdbusplus::message::object_path,
30b9b2e0b2SEd Tanous     boost::container::flat_map<
31abf2add6SEd Tanous         std::string, boost::container::flat_map<
32abf2add6SEd Tanous                          std::string, std::variant<bool, std::string>>>>>;
3384e12cb7SAppaRao Puli 
3484e12cb7SAppaRao Puli inline std::string getPrivilegeFromRoleId(boost::beast::string_view role)
3584e12cb7SAppaRao Puli {
3684e12cb7SAppaRao Puli     if (role == "priv-admin")
3784e12cb7SAppaRao Puli     {
3884e12cb7SAppaRao Puli         return "Administrator";
3984e12cb7SAppaRao Puli     }
4084e12cb7SAppaRao Puli     else if (role == "priv-callback")
4184e12cb7SAppaRao Puli     {
4284e12cb7SAppaRao Puli         return "Callback";
4384e12cb7SAppaRao Puli     }
4484e12cb7SAppaRao Puli     else if (role == "priv-user")
4584e12cb7SAppaRao Puli     {
4684e12cb7SAppaRao Puli         return "User";
4784e12cb7SAppaRao Puli     }
4884e12cb7SAppaRao Puli     else if (role == "priv-operator")
4984e12cb7SAppaRao Puli     {
5084e12cb7SAppaRao Puli         return "Operator";
5184e12cb7SAppaRao Puli     }
5284e12cb7SAppaRao Puli     return "";
5384e12cb7SAppaRao Puli }
5484e12cb7SAppaRao Puli inline std::string getRoleIdFromPrivilege(boost::beast::string_view role)
5584e12cb7SAppaRao Puli {
5684e12cb7SAppaRao Puli     if (role == "Administrator")
5784e12cb7SAppaRao Puli     {
5884e12cb7SAppaRao Puli         return "priv-admin";
5984e12cb7SAppaRao Puli     }
6084e12cb7SAppaRao Puli     else if (role == "Callback")
6184e12cb7SAppaRao Puli     {
6284e12cb7SAppaRao Puli         return "priv-callback";
6384e12cb7SAppaRao Puli     }
6484e12cb7SAppaRao Puli     else if (role == "User")
6584e12cb7SAppaRao Puli     {
6684e12cb7SAppaRao Puli         return "priv-user";
6784e12cb7SAppaRao Puli     }
6884e12cb7SAppaRao Puli     else if (role == "Operator")
6984e12cb7SAppaRao Puli     {
7084e12cb7SAppaRao Puli         return "priv-operator";
7184e12cb7SAppaRao Puli     }
7284e12cb7SAppaRao Puli     return "";
7384e12cb7SAppaRao Puli }
74b9b2e0b2SEd Tanous 
751abe55efSEd Tanous class AccountService : public Node
761abe55efSEd Tanous {
7788d16c9aSLewanczyk, Dawid   public:
781abe55efSEd Tanous     AccountService(CrowApp& app) : Node(app, "/redfish/v1/AccountService/")
791abe55efSEd Tanous     {
803ebd75f7SEd Tanous         entityPrivileges = {
814b1b8683SBorawski.Lukasz             {boost::beast::http::verb::get,
824b1b8683SBorawski.Lukasz              {{"ConfigureUsers"}, {"ConfigureManager"}}},
83e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
84e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
85e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
86e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
87e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
8888d16c9aSLewanczyk, Dawid     }
8988d16c9aSLewanczyk, Dawid 
9088d16c9aSLewanczyk, Dawid   private:
9155c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
921abe55efSEd Tanous                const std::vector<std::string>& params) override
931abe55efSEd Tanous     {
943d958bbcSAppaRao Puli         auto asyncResp = std::make_shared<AsyncResp>(res);
953d958bbcSAppaRao Puli         res.jsonValue = {
963d958bbcSAppaRao Puli             {"@odata.context", "/redfish/v1/"
973d958bbcSAppaRao Puli                                "$metadata#AccountService.AccountService"},
983d958bbcSAppaRao Puli             {"@odata.id", "/redfish/v1/AccountService"},
993d958bbcSAppaRao Puli             {"@odata.type", "#AccountService."
1003d958bbcSAppaRao Puli                             "v1_1_0.AccountService"},
1013d958bbcSAppaRao Puli             {"Id", "AccountService"},
1023d958bbcSAppaRao Puli             {"Name", "Account Service"},
1033d958bbcSAppaRao Puli             {"Description", "Account Service"},
1043d958bbcSAppaRao Puli             {"ServiceEnabled", true},
1053d958bbcSAppaRao Puli             {"MaxPasswordLength", 31},
1063d958bbcSAppaRao Puli             {"Accounts",
1073d958bbcSAppaRao Puli              {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}},
1083d958bbcSAppaRao Puli             {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}}};
1090f74e643SEd Tanous 
1103d958bbcSAppaRao Puli         crow::connections::systemBus->async_method_call(
1113d958bbcSAppaRao Puli             [asyncResp](
1123d958bbcSAppaRao Puli                 const boost::system::error_code ec,
1133d958bbcSAppaRao Puli                 const std::vector<std::pair<
114abf2add6SEd Tanous                     std::string, std::variant<uint32_t, uint16_t, uint8_t>>>&
1153d958bbcSAppaRao Puli                     propertiesList) {
1163d958bbcSAppaRao Puli                 if (ec)
1173d958bbcSAppaRao Puli                 {
1183d958bbcSAppaRao Puli                     messages::internalError(asyncResp->res);
1193d958bbcSAppaRao Puli                     return;
1203d958bbcSAppaRao Puli                 }
1213d958bbcSAppaRao Puli                 BMCWEB_LOG_DEBUG << "Got " << propertiesList.size()
1223d958bbcSAppaRao Puli                                  << "properties for AccountService";
1233d958bbcSAppaRao Puli                 for (const std::pair<std::string,
124abf2add6SEd Tanous                                      std::variant<uint32_t, uint16_t, uint8_t>>&
1253d958bbcSAppaRao Puli                          property : propertiesList)
1263d958bbcSAppaRao Puli                 {
1273d958bbcSAppaRao Puli                     if (property.first == "MinPasswordLength")
1283d958bbcSAppaRao Puli                     {
1293d958bbcSAppaRao Puli                         const uint8_t* value =
130abf2add6SEd Tanous                             std::get_if<uint8_t>(&property.second);
1313d958bbcSAppaRao Puli                         if (value != nullptr)
1323d958bbcSAppaRao Puli                         {
1333d958bbcSAppaRao Puli                             asyncResp->res.jsonValue["MinPasswordLength"] =
1343d958bbcSAppaRao Puli                                 *value;
1353d958bbcSAppaRao Puli                         }
1363d958bbcSAppaRao Puli                     }
1373d958bbcSAppaRao Puli                     if (property.first == "AccountUnlockTimeout")
1383d958bbcSAppaRao Puli                     {
1393d958bbcSAppaRao Puli                         const uint32_t* value =
140abf2add6SEd Tanous                             std::get_if<uint32_t>(&property.second);
1413d958bbcSAppaRao Puli                         if (value != nullptr)
1423d958bbcSAppaRao Puli                         {
1433d958bbcSAppaRao Puli                             asyncResp->res.jsonValue["AccountLockoutDuration"] =
1443d958bbcSAppaRao Puli                                 *value;
1453d958bbcSAppaRao Puli                         }
1463d958bbcSAppaRao Puli                     }
1473d958bbcSAppaRao Puli                     if (property.first == "MaxLoginAttemptBeforeLockout")
1483d958bbcSAppaRao Puli                     {
1493d958bbcSAppaRao Puli                         const uint16_t* value =
150abf2add6SEd Tanous                             std::get_if<uint16_t>(&property.second);
1513d958bbcSAppaRao Puli                         if (value != nullptr)
1523d958bbcSAppaRao Puli                         {
1533d958bbcSAppaRao Puli                             asyncResp->res
1543d958bbcSAppaRao Puli                                 .jsonValue["AccountLockoutThreshold"] = *value;
1553d958bbcSAppaRao Puli                         }
1563d958bbcSAppaRao Puli                     }
1573d958bbcSAppaRao Puli                 }
1583d958bbcSAppaRao Puli             },
1593d958bbcSAppaRao Puli             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1603d958bbcSAppaRao Puli             "org.freedesktop.DBus.Properties", "GetAll",
1613d958bbcSAppaRao Puli             "xyz.openbmc_project.User.AccountPolicy");
1623d958bbcSAppaRao Puli     }
1633d958bbcSAppaRao Puli     void doPatch(crow::Response& res, const crow::Request& req,
1643d958bbcSAppaRao Puli                  const std::vector<std::string>& params) override
1653d958bbcSAppaRao Puli     {
1663d958bbcSAppaRao Puli         auto asyncResp = std::make_shared<AsyncResp>(res);
1673d958bbcSAppaRao Puli 
1683d958bbcSAppaRao Puli         std::optional<uint32_t> unlockTimeout;
1693d958bbcSAppaRao Puli         std::optional<uint16_t> lockoutThreshold;
1703d958bbcSAppaRao Puli         if (!json_util::readJson(req, res, "AccountLockoutDuration",
1713d958bbcSAppaRao Puli                                  unlockTimeout, "AccountLockoutThreshold",
1723d958bbcSAppaRao Puli                                  lockoutThreshold))
1733d958bbcSAppaRao Puli         {
1743d958bbcSAppaRao Puli             return;
1753d958bbcSAppaRao Puli         }
1763d958bbcSAppaRao Puli         if (unlockTimeout)
1773d958bbcSAppaRao Puli         {
1783d958bbcSAppaRao Puli             crow::connections::systemBus->async_method_call(
1793d958bbcSAppaRao Puli                 [asyncResp](const boost::system::error_code ec) {
1803d958bbcSAppaRao Puli                     if (ec)
1813d958bbcSAppaRao Puli                     {
1823d958bbcSAppaRao Puli                         messages::internalError(asyncResp->res);
1833d958bbcSAppaRao Puli                         return;
1843d958bbcSAppaRao Puli                     }
185*add6133bSRatan Gupta                     messages::success(asyncResp->res);
1863d958bbcSAppaRao Puli                 },
1873d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1883d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "Set",
1893d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy",
190abf2add6SEd Tanous                 "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout));
1913d958bbcSAppaRao Puli         }
1923d958bbcSAppaRao Puli         if (lockoutThreshold)
1933d958bbcSAppaRao Puli         {
1943d958bbcSAppaRao Puli             crow::connections::systemBus->async_method_call(
1953d958bbcSAppaRao Puli                 [asyncResp](const boost::system::error_code ec) {
1963d958bbcSAppaRao Puli                     if (ec)
1973d958bbcSAppaRao Puli                     {
1983d958bbcSAppaRao Puli                         messages::internalError(asyncResp->res);
1993d958bbcSAppaRao Puli                         return;
2003d958bbcSAppaRao Puli                     }
201*add6133bSRatan Gupta                     messages::success(asyncResp->res);
2023d958bbcSAppaRao Puli                 },
2033d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
2043d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "Set",
2053d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy",
2063d958bbcSAppaRao Puli                 "MaxLoginAttemptBeforeLockout",
207abf2add6SEd Tanous                 std::variant<uint16_t>(*lockoutThreshold));
2083d958bbcSAppaRao Puli         }
20988d16c9aSLewanczyk, Dawid     }
21088d16c9aSLewanczyk, Dawid };
211b9b2e0b2SEd Tanous class AccountsCollection : public Node
212b9b2e0b2SEd Tanous {
213b9b2e0b2SEd Tanous   public:
214b9b2e0b2SEd Tanous     AccountsCollection(CrowApp& app) :
215b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/")
216b9b2e0b2SEd Tanous     {
217b9b2e0b2SEd Tanous         entityPrivileges = {
218b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
219b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}}},
220b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
221b9b2e0b2SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
222b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
223b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
224b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
225b9b2e0b2SEd Tanous     }
226b9b2e0b2SEd Tanous 
227b9b2e0b2SEd Tanous   private:
228b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
229b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
230b9b2e0b2SEd Tanous     {
231b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
2320f74e643SEd Tanous         res.jsonValue = {{"@odata.context",
2330f74e643SEd Tanous                           "/redfish/v1/"
2340f74e643SEd Tanous                           "$metadata#ManagerAccountCollection."
2350f74e643SEd Tanous                           "ManagerAccountCollection"},
2360f74e643SEd Tanous                          {"@odata.id", "/redfish/v1/AccountService/Accounts"},
2370f74e643SEd Tanous                          {"@odata.type", "#ManagerAccountCollection."
2380f74e643SEd Tanous                                          "ManagerAccountCollection"},
2390f74e643SEd Tanous                          {"Name", "Accounts Collection"},
2400f74e643SEd Tanous                          {"Description", "BMC User Accounts"}};
2410f74e643SEd Tanous 
242b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
243b9b2e0b2SEd Tanous             [asyncResp](const boost::system::error_code ec,
244b9b2e0b2SEd Tanous                         const ManagedObjectType& users) {
245b9b2e0b2SEd Tanous                 if (ec)
246b9b2e0b2SEd Tanous                 {
247f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
248b9b2e0b2SEd Tanous                     return;
249b9b2e0b2SEd Tanous                 }
250b9b2e0b2SEd Tanous 
251b9b2e0b2SEd Tanous                 nlohmann::json& memberArray =
252b9b2e0b2SEd Tanous                     asyncResp->res.jsonValue["Members"];
253b9b2e0b2SEd Tanous                 memberArray = nlohmann::json::array();
254b9b2e0b2SEd Tanous 
255b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = users.size();
256b9b2e0b2SEd Tanous                 for (auto& user : users)
257b9b2e0b2SEd Tanous                 {
258b9b2e0b2SEd Tanous                     const std::string& path =
259b9b2e0b2SEd Tanous                         static_cast<const std::string&>(user.first);
260b9b2e0b2SEd Tanous                     std::size_t lastIndex = path.rfind("/");
261b9b2e0b2SEd Tanous                     if (lastIndex == std::string::npos)
262b9b2e0b2SEd Tanous                     {
263b9b2e0b2SEd Tanous                         lastIndex = 0;
264b9b2e0b2SEd Tanous                     }
265b9b2e0b2SEd Tanous                     else
266b9b2e0b2SEd Tanous                     {
267b9b2e0b2SEd Tanous                         lastIndex += 1;
268b9b2e0b2SEd Tanous                     }
269b9b2e0b2SEd Tanous                     memberArray.push_back(
270b9b2e0b2SEd Tanous                         {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
271b9b2e0b2SEd Tanous                                            path.substr(lastIndex)}});
272b9b2e0b2SEd Tanous                 }
273b9b2e0b2SEd Tanous             },
274b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
275b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
276b9b2e0b2SEd Tanous     }
27704ae99ecSEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
27804ae99ecSEd Tanous                 const std::vector<std::string>& params) override
27904ae99ecSEd Tanous     {
28004ae99ecSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
28104ae99ecSEd Tanous 
2829712f8acSEd Tanous         std::string username;
2839712f8acSEd Tanous         std::string password;
284a24526dcSEd Tanous         std::optional<std::string> roleId("User");
285a24526dcSEd Tanous         std::optional<bool> enabled = true;
2869712f8acSEd Tanous         if (!json_util::readJson(req, res, "UserName", username, "Password",
2879712f8acSEd Tanous                                  password, "RoleId", roleId, "Enabled",
2889712f8acSEd Tanous                                  enabled))
28904ae99ecSEd Tanous         {
29004ae99ecSEd Tanous             return;
29104ae99ecSEd Tanous         }
29204ae99ecSEd Tanous 
29384e12cb7SAppaRao Puli         std::string priv = getRoleIdFromPrivilege(*roleId);
29484e12cb7SAppaRao Puli         if (priv.empty())
29504ae99ecSEd Tanous         {
296f12894f8SJason M. Bills             messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
29704ae99ecSEd Tanous             return;
29804ae99ecSEd Tanous         }
2999712f8acSEd Tanous         roleId = priv;
30004ae99ecSEd Tanous 
30104ae99ecSEd Tanous         crow::connections::systemBus->async_method_call(
3029712f8acSEd Tanous             [asyncResp, username, password{std::move(password)}](
30304ae99ecSEd Tanous                 const boost::system::error_code ec) {
30404ae99ecSEd Tanous                 if (ec)
30504ae99ecSEd Tanous                 {
30604ae99ecSEd Tanous                     messages::resourceAlreadyExists(
307f12894f8SJason M. Bills                         asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
308f12894f8SJason M. Bills                         "UserName", username);
30904ae99ecSEd Tanous                     return;
31004ae99ecSEd Tanous                 }
31104ae99ecSEd Tanous 
31204ae99ecSEd Tanous                 if (!pamUpdatePassword(username, password))
31304ae99ecSEd Tanous                 {
31404ae99ecSEd Tanous                     // At this point we have a user that's been created, but the
31504ae99ecSEd Tanous                     // password set failed.  Something is wrong, so delete the
31604ae99ecSEd Tanous                     // user that we've already created
31704ae99ecSEd Tanous                     crow::connections::systemBus->async_method_call(
31804ae99ecSEd Tanous                         [asyncResp](const boost::system::error_code ec) {
31904ae99ecSEd Tanous                             if (ec)
32004ae99ecSEd Tanous                             {
321f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
32204ae99ecSEd Tanous                                 return;
32304ae99ecSEd Tanous                             }
32404ae99ecSEd Tanous 
325f12894f8SJason M. Bills                             messages::invalidObject(asyncResp->res, "Password");
32604ae99ecSEd Tanous                         },
32704ae99ecSEd Tanous                         "xyz.openbmc_project.User.Manager",
32804ae99ecSEd Tanous                         "/xyz/openbmc_project/user/" + username,
32904ae99ecSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
33004ae99ecSEd Tanous 
33104ae99ecSEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
33204ae99ecSEd Tanous                     return;
33304ae99ecSEd Tanous                 }
33404ae99ecSEd Tanous 
335f12894f8SJason M. Bills                 messages::created(asyncResp->res);
33604ae99ecSEd Tanous                 asyncResp->res.addHeader(
33704ae99ecSEd Tanous                     "Location",
33804ae99ecSEd Tanous                     "/redfish/v1/AccountService/Accounts/" + username);
33904ae99ecSEd Tanous             },
34004ae99ecSEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
3419712f8acSEd Tanous             "xyz.openbmc_project.User.Manager", "CreateUser", username,
34204ae99ecSEd Tanous             std::array<const char*, 4>{"ipmi", "redfish", "ssh", "web"},
3439712f8acSEd Tanous             *roleId, *enabled);
34404ae99ecSEd Tanous     }
345b9b2e0b2SEd Tanous };
346b9b2e0b2SEd Tanous 
347a840879dSEd Tanous template <typename Callback>
348a840879dSEd Tanous inline void checkDbusPathExists(const std::string& path, Callback&& callback)
349a840879dSEd Tanous {
350a840879dSEd Tanous     using GetObjectType =
351a840879dSEd Tanous         std::vector<std::pair<std::string, std::vector<std::string>>>;
352a840879dSEd Tanous 
353a840879dSEd Tanous     crow::connections::systemBus->async_method_call(
354a840879dSEd Tanous         [callback{std::move(callback)}](const boost::system::error_code ec,
355a840879dSEd Tanous                                         const GetObjectType& object_names) {
35684e12cb7SAppaRao Puli             callback(!ec && object_names.size() != 0);
357a840879dSEd Tanous         },
358a840879dSEd Tanous         "xyz.openbmc_project.ObjectMapper",
359a840879dSEd Tanous         "/xyz/openbmc_project/object_mapper",
360a840879dSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetObject", path,
361a840879dSEd Tanous         std::array<std::string, 0>());
362a840879dSEd Tanous }
363a840879dSEd Tanous 
364b9b2e0b2SEd Tanous class ManagerAccount : public Node
365b9b2e0b2SEd Tanous {
366b9b2e0b2SEd Tanous   public:
367b9b2e0b2SEd Tanous     ManagerAccount(CrowApp& app) :
368b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string())
369b9b2e0b2SEd Tanous     {
370b9b2e0b2SEd Tanous         entityPrivileges = {
371b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
372b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}},
373b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
374b9b2e0b2SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
375b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
376b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
377b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
378b9b2e0b2SEd Tanous     }
379b9b2e0b2SEd Tanous 
380b9b2e0b2SEd Tanous   private:
381b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
382b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
383b9b2e0b2SEd Tanous     {
3840f74e643SEd Tanous         res.jsonValue = {
3850f74e643SEd Tanous             {"@odata.context",
3860f74e643SEd Tanous              "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
3870f74e643SEd Tanous             {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"},
3880f74e643SEd Tanous             {"Name", "User Account"},
3890f74e643SEd Tanous             {"Description", "User Account"},
3900f74e643SEd Tanous             {"Password", nullptr},
39184e12cb7SAppaRao Puli             {"RoleId", "Administrator"}};
3920f74e643SEd Tanous 
393b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
394b9b2e0b2SEd Tanous 
395b9b2e0b2SEd Tanous         if (params.size() != 1)
396b9b2e0b2SEd Tanous         {
397f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
398b9b2e0b2SEd Tanous             return;
399b9b2e0b2SEd Tanous         }
400b9b2e0b2SEd Tanous 
401b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
402b9b2e0b2SEd Tanous             [asyncResp, accountName{std::string(params[0])}](
403b9b2e0b2SEd Tanous                 const boost::system::error_code ec,
404b9b2e0b2SEd Tanous                 const ManagedObjectType& users) {
405b9b2e0b2SEd Tanous                 if (ec)
406b9b2e0b2SEd Tanous                 {
407f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
408b9b2e0b2SEd Tanous                     return;
409b9b2e0b2SEd Tanous                 }
41084e12cb7SAppaRao Puli                 auto userIt = users.begin();
411b9b2e0b2SEd Tanous 
41284e12cb7SAppaRao Puli                 for (; userIt != users.end(); userIt++)
413b9b2e0b2SEd Tanous                 {
41484e12cb7SAppaRao Puli                     if (boost::ends_with(userIt->first.str, "/" + accountName))
415b9b2e0b2SEd Tanous                     {
41684e12cb7SAppaRao Puli                         break;
417b9b2e0b2SEd Tanous                     }
418b9b2e0b2SEd Tanous                 }
41984e12cb7SAppaRao Puli                 if (userIt == users.end())
420b9b2e0b2SEd Tanous                 {
42184e12cb7SAppaRao Puli                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
42284e12cb7SAppaRao Puli                                                accountName);
42384e12cb7SAppaRao Puli                     return;
42484e12cb7SAppaRao Puli                 }
42584e12cb7SAppaRao Puli                 for (const auto& interface : userIt->second)
42665b0dc32SEd Tanous                 {
42765b0dc32SEd Tanous                     if (interface.first ==
42865b0dc32SEd Tanous                         "xyz.openbmc_project.User.Attributes")
42965b0dc32SEd Tanous                     {
43065b0dc32SEd Tanous                         for (const auto& property : interface.second)
43165b0dc32SEd Tanous                         {
43265b0dc32SEd Tanous                             if (property.first == "UserEnabled")
43365b0dc32SEd Tanous                             {
43465b0dc32SEd Tanous                                 const bool* userEnabled =
435abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
43665b0dc32SEd Tanous                                 if (userEnabled == nullptr)
43765b0dc32SEd Tanous                                 {
43865b0dc32SEd Tanous                                     BMCWEB_LOG_ERROR
43965b0dc32SEd Tanous                                         << "UserEnabled wasn't a bool";
44084e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
44184e12cb7SAppaRao Puli                                     return;
44265b0dc32SEd Tanous                                 }
44365b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Enabled"] =
44465b0dc32SEd Tanous                                     *userEnabled;
44565b0dc32SEd Tanous                             }
44665b0dc32SEd Tanous                             else if (property.first ==
44765b0dc32SEd Tanous                                      "UserLockedForFailedAttempt")
44865b0dc32SEd Tanous                             {
44965b0dc32SEd Tanous                                 const bool* userLocked =
450abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
45165b0dc32SEd Tanous                                 if (userLocked == nullptr)
45265b0dc32SEd Tanous                                 {
45384e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "UserLockedForF"
45484e12cb7SAppaRao Puli                                                         "ailedAttempt "
45584e12cb7SAppaRao Puli                                                         "wasn't a bool";
45684e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
45784e12cb7SAppaRao Puli                                     return;
45865b0dc32SEd Tanous                                 }
45965b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Locked"] =
46065b0dc32SEd Tanous                                     *userLocked;
46124c8542dSRatan Gupta                                 asyncResp->res.jsonValue
46224c8542dSRatan Gupta                                     ["Locked@Redfish.AllowableValues"] = {
46324c8542dSRatan Gupta                                     false};
46465b0dc32SEd Tanous                             }
46584e12cb7SAppaRao Puli                             else if (property.first == "UserPrivilege")
46684e12cb7SAppaRao Puli                             {
46784e12cb7SAppaRao Puli                                 const std::string* userRolePtr =
468abf2add6SEd Tanous                                     std::get_if<std::string>(&property.second);
46984e12cb7SAppaRao Puli                                 if (userRolePtr == nullptr)
47084e12cb7SAppaRao Puli                                 {
47184e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR
47284e12cb7SAppaRao Puli                                         << "UserPrivilege wasn't a "
47384e12cb7SAppaRao Puli                                            "string";
47484e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
47584e12cb7SAppaRao Puli                                     return;
47684e12cb7SAppaRao Puli                                 }
47784e12cb7SAppaRao Puli                                 std::string priv =
47884e12cb7SAppaRao Puli                                     getPrivilegeFromRoleId(*userRolePtr);
47984e12cb7SAppaRao Puli                                 if (priv.empty())
48084e12cb7SAppaRao Puli                                 {
48184e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "Invalid user role";
48284e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
48384e12cb7SAppaRao Puli                                     return;
48484e12cb7SAppaRao Puli                                 }
48584e12cb7SAppaRao Puli                                 asyncResp->res.jsonValue["RoleId"] = priv;
48684e12cb7SAppaRao Puli 
48784e12cb7SAppaRao Puli                                 asyncResp->res.jsonValue["Links"]["Role"] = {
48884e12cb7SAppaRao Puli                                     {"@odata.id", "/redfish/v1/AccountService/"
48984e12cb7SAppaRao Puli                                                   "Roles/" +
49084e12cb7SAppaRao Puli                                                       priv}};
49184e12cb7SAppaRao Puli                             }
49265b0dc32SEd Tanous                         }
49365b0dc32SEd Tanous                     }
49465b0dc32SEd Tanous                 }
49565b0dc32SEd Tanous 
496b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
49784e12cb7SAppaRao Puli                     "/redfish/v1/AccountService/Accounts/" + accountName;
498b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Id"] = accountName;
499b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["UserName"] = accountName;
500b9b2e0b2SEd Tanous             },
501b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
502b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
503b9b2e0b2SEd Tanous     }
504a840879dSEd Tanous 
505a840879dSEd Tanous     void doPatch(crow::Response& res, const crow::Request& req,
506a840879dSEd Tanous                  const std::vector<std::string>& params) override
507a840879dSEd Tanous     {
508a840879dSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
509a840879dSEd Tanous         if (params.size() != 1)
510a840879dSEd Tanous         {
511f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
512a840879dSEd Tanous             return;
513a840879dSEd Tanous         }
514a840879dSEd Tanous 
515a24526dcSEd Tanous         std::optional<std::string> newUserName;
516a24526dcSEd Tanous         std::optional<std::string> password;
517a24526dcSEd Tanous         std::optional<bool> enabled;
518a24526dcSEd Tanous         std::optional<std::string> roleId;
51924c8542dSRatan Gupta         std::optional<bool> locked;
52084e12cb7SAppaRao Puli         if (!json_util::readJson(req, res, "UserName", newUserName, "Password",
52124c8542dSRatan Gupta                                  password, "RoleId", roleId, "Enabled", enabled,
52224c8542dSRatan Gupta                                  "Locked", locked))
523a840879dSEd Tanous         {
524a840879dSEd Tanous             return;
525a840879dSEd Tanous         }
526a840879dSEd Tanous 
52784e12cb7SAppaRao Puli         const std::string& username = params[0];
52884e12cb7SAppaRao Puli 
52984e12cb7SAppaRao Puli         if (!newUserName)
530a840879dSEd Tanous         {
53184e12cb7SAppaRao Puli             // If the username isn't being updated, we can update the properties
53284e12cb7SAppaRao Puli             // directly
53324c8542dSRatan Gupta             updateUserProperties(asyncResp, username, password, enabled, roleId,
53424c8542dSRatan Gupta                                  locked);
53584e12cb7SAppaRao Puli             return;
53684e12cb7SAppaRao Puli         }
53784e12cb7SAppaRao Puli         else
53884e12cb7SAppaRao Puli         {
53984e12cb7SAppaRao Puli             crow::connections::systemBus->async_method_call(
54084e12cb7SAppaRao Puli                 [this, asyncResp, username, password(std::move(password)),
54184e12cb7SAppaRao Puli                  roleId(std::move(roleId)), enabled(std::move(enabled)),
54224c8542dSRatan Gupta                  newUser{std::string(*newUserName)}, locked(std::move(locked))](
54384e12cb7SAppaRao Puli                     const boost::system::error_code ec) {
54484e12cb7SAppaRao Puli                     if (ec)
54584e12cb7SAppaRao Puli                     {
54684e12cb7SAppaRao Puli                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
547a840879dSEd Tanous                         messages::resourceNotFound(
54884e12cb7SAppaRao Puli                             asyncResp->res,
54984e12cb7SAppaRao Puli                             "#ManagerAccount.v1_0_3.ManagerAccount", username);
550a840879dSEd Tanous                         return;
551a840879dSEd Tanous                     }
552a840879dSEd Tanous 
55384e12cb7SAppaRao Puli                     updateUserProperties(asyncResp, newUser, password, enabled,
55424c8542dSRatan Gupta                                          roleId, locked);
55584e12cb7SAppaRao Puli                 },
55684e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
55784e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "RenameUser", username,
55884e12cb7SAppaRao Puli                 *newUserName);
55984e12cb7SAppaRao Puli         }
56084e12cb7SAppaRao Puli     }
56184e12cb7SAppaRao Puli 
56284e12cb7SAppaRao Puli     void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp,
56384e12cb7SAppaRao Puli                               const std::string& username,
564a24526dcSEd Tanous                               std::optional<std::string> password,
565a24526dcSEd Tanous                               std::optional<bool> enabled,
56624c8542dSRatan Gupta                               std::optional<std::string> roleId,
56724c8542dSRatan Gupta                               std::optional<bool> locked)
56884e12cb7SAppaRao Puli     {
5699712f8acSEd Tanous         if (password)
570a840879dSEd Tanous         {
5719712f8acSEd Tanous             if (!pamUpdatePassword(username, *password))
572a840879dSEd Tanous             {
573a840879dSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
574f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
575a840879dSEd Tanous                 return;
576a840879dSEd Tanous             }
577a840879dSEd Tanous         }
578a840879dSEd Tanous 
57924c8542dSRatan Gupta         std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username;
58024c8542dSRatan Gupta         dbus::utility::escapePathForDbus(dbusObjectPath);
58124c8542dSRatan Gupta 
58224c8542dSRatan Gupta         checkDbusPathExists(
58324c8542dSRatan Gupta             dbusObjectPath,
58424c8542dSRatan Gupta             [dbusObjectPath(std::move(dbusObjectPath)), username,
58524c8542dSRatan Gupta              password(std::move(password)), roleId(std::move(roleId)),
58624c8542dSRatan Gupta              enabled(std::move(enabled)), locked(std::move(locked)),
58724c8542dSRatan Gupta              asyncResp{std::move(asyncResp)}](int rc) {
58824c8542dSRatan Gupta                 if (!rc)
58924c8542dSRatan Gupta                 {
59024c8542dSRatan Gupta                     messages::invalidObject(asyncResp->res, username.c_str());
59124c8542dSRatan Gupta                     return;
59224c8542dSRatan Gupta                 }
5939712f8acSEd Tanous                 if (enabled)
594a840879dSEd Tanous                 {
595a840879dSEd Tanous                     crow::connections::systemBus->async_method_call(
596a840879dSEd Tanous                         [asyncResp](const boost::system::error_code ec) {
597a840879dSEd Tanous                             if (ec)
598a840879dSEd Tanous                             {
59924c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
60024c8542dSRatan Gupta                                                  << ec;
601f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
602a840879dSEd Tanous                                 return;
603a840879dSEd Tanous                             }
60484e12cb7SAppaRao Puli                             messages::success(asyncResp->res);
60584e12cb7SAppaRao Puli                             return;
60684e12cb7SAppaRao Puli                         },
60784e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Manager",
60824c8542dSRatan Gupta                         dbusObjectPath.c_str(),
60984e12cb7SAppaRao Puli                         "org.freedesktop.DBus.Properties", "Set",
61084e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserEnabled",
611abf2add6SEd Tanous                         std::variant<bool>{*enabled});
61284e12cb7SAppaRao Puli                 }
61384e12cb7SAppaRao Puli 
61484e12cb7SAppaRao Puli                 if (roleId)
61584e12cb7SAppaRao Puli                 {
61684e12cb7SAppaRao Puli                     std::string priv = getRoleIdFromPrivilege(*roleId);
61784e12cb7SAppaRao Puli                     if (priv.empty())
61884e12cb7SAppaRao Puli                     {
61924c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res,
62024c8542dSRatan Gupta                                                          *roleId, "RoleId");
62184e12cb7SAppaRao Puli                         return;
62284e12cb7SAppaRao Puli                     }
62384e12cb7SAppaRao Puli 
62484e12cb7SAppaRao Puli                     crow::connections::systemBus->async_method_call(
62584e12cb7SAppaRao Puli                         [asyncResp](const boost::system::error_code ec) {
62684e12cb7SAppaRao Puli                             if (ec)
62784e12cb7SAppaRao Puli                             {
62824c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
62924c8542dSRatan Gupta                                                  << ec;
63084e12cb7SAppaRao Puli                                 messages::internalError(asyncResp->res);
63184e12cb7SAppaRao Puli                                 return;
63284e12cb7SAppaRao Puli                             }
633f12894f8SJason M. Bills                             messages::success(asyncResp->res);
634a840879dSEd Tanous                         },
635a840879dSEd Tanous                         "xyz.openbmc_project.User.Manager",
63624c8542dSRatan Gupta                         dbusObjectPath.c_str(),
637a840879dSEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
63884e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserPrivilege",
639abf2add6SEd Tanous                         std::variant<std::string>{priv});
640a840879dSEd Tanous                 }
64124c8542dSRatan Gupta 
64224c8542dSRatan Gupta                 if (locked)
64324c8542dSRatan Gupta                 {
64424c8542dSRatan Gupta                     // admin can unlock the account which is locked by
64524c8542dSRatan Gupta                     // successive authentication failures but admin should not
64624c8542dSRatan Gupta                     // be allowed to lock an account.
64724c8542dSRatan Gupta                     if (*locked)
64824c8542dSRatan Gupta                     {
64924c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res, "true",
65024c8542dSRatan Gupta                                                          "Locked");
65124c8542dSRatan Gupta                         return;
65224c8542dSRatan Gupta                     }
65324c8542dSRatan Gupta 
65424c8542dSRatan Gupta                     crow::connections::systemBus->async_method_call(
65524c8542dSRatan Gupta                         [asyncResp](const boost::system::error_code ec) {
65624c8542dSRatan Gupta                             if (ec)
65724c8542dSRatan Gupta                             {
65824c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
65924c8542dSRatan Gupta                                                  << ec;
66024c8542dSRatan Gupta                                 messages::internalError(asyncResp->res);
66124c8542dSRatan Gupta                                 return;
66224c8542dSRatan Gupta                             }
66324c8542dSRatan Gupta                             messages::success(asyncResp->res);
66424c8542dSRatan Gupta                             return;
66524c8542dSRatan Gupta                         },
66624c8542dSRatan Gupta                         "xyz.openbmc_project.User.Manager",
66724c8542dSRatan Gupta                         dbusObjectPath.c_str(),
66824c8542dSRatan Gupta                         "org.freedesktop.DBus.Properties", "Set",
66924c8542dSRatan Gupta                         "xyz.openbmc_project.User.Attributes",
67024c8542dSRatan Gupta                         "UserLockedForFailedAttempt",
67124c8542dSRatan Gupta                         sdbusplus::message::variant<bool>{*locked});
67224c8542dSRatan Gupta                 }
67324c8542dSRatan Gupta             });
674a840879dSEd Tanous     }
67506e086d9SEd Tanous 
67606e086d9SEd Tanous     void doDelete(crow::Response& res, const crow::Request& req,
67706e086d9SEd Tanous                   const std::vector<std::string>& params) override
67806e086d9SEd Tanous     {
67906e086d9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
68006e086d9SEd Tanous 
68106e086d9SEd Tanous         if (params.size() != 1)
68206e086d9SEd Tanous         {
683f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
68406e086d9SEd Tanous             return;
68506e086d9SEd Tanous         }
68606e086d9SEd Tanous 
68706e086d9SEd Tanous         const std::string userPath = "/xyz/openbmc_project/user/" + params[0];
68806e086d9SEd Tanous 
68906e086d9SEd Tanous         crow::connections::systemBus->async_method_call(
69006e086d9SEd Tanous             [asyncResp, username{std::move(params[0])}](
69106e086d9SEd Tanous                 const boost::system::error_code ec) {
69206e086d9SEd Tanous                 if (ec)
69306e086d9SEd Tanous                 {
69406e086d9SEd Tanous                     messages::resourceNotFound(
695f12894f8SJason M. Bills                         asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
696f12894f8SJason M. Bills                         username);
69706e086d9SEd Tanous                     return;
69806e086d9SEd Tanous                 }
69906e086d9SEd Tanous 
700f12894f8SJason M. Bills                 messages::accountRemoved(asyncResp->res);
70106e086d9SEd Tanous             },
70206e086d9SEd Tanous             "xyz.openbmc_project.User.Manager", userPath,
70306e086d9SEd Tanous             "xyz.openbmc_project.Object.Delete", "Delete");
70406e086d9SEd Tanous     }
70584e12cb7SAppaRao Puli };
70688d16c9aSLewanczyk, Dawid 
70788d16c9aSLewanczyk, Dawid } // namespace redfish
708