xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision 24c8542dce451ba02df51e83174dbcdabc6bf09f)
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 
19*24c8542dSRatan 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                     }
1853d958bbcSAppaRao Puli                 },
1863d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1873d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "Set",
1883d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy",
189abf2add6SEd Tanous                 "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout));
1903d958bbcSAppaRao Puli         }
1913d958bbcSAppaRao Puli         if (lockoutThreshold)
1923d958bbcSAppaRao Puli         {
1933d958bbcSAppaRao Puli             crow::connections::systemBus->async_method_call(
1943d958bbcSAppaRao Puli                 [asyncResp](const boost::system::error_code ec) {
1953d958bbcSAppaRao Puli                     if (ec)
1963d958bbcSAppaRao Puli                     {
1973d958bbcSAppaRao Puli                         messages::internalError(asyncResp->res);
1983d958bbcSAppaRao Puli                         return;
1993d958bbcSAppaRao Puli                     }
2003d958bbcSAppaRao Puli                 },
2013d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
2023d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "Set",
2033d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy",
2043d958bbcSAppaRao Puli                 "MaxLoginAttemptBeforeLockout",
205abf2add6SEd Tanous                 std::variant<uint16_t>(*lockoutThreshold));
2063d958bbcSAppaRao Puli         }
20788d16c9aSLewanczyk, Dawid     }
20888d16c9aSLewanczyk, Dawid };
209b9b2e0b2SEd Tanous class AccountsCollection : public Node
210b9b2e0b2SEd Tanous {
211b9b2e0b2SEd Tanous   public:
212b9b2e0b2SEd Tanous     AccountsCollection(CrowApp& app) :
213b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/")
214b9b2e0b2SEd Tanous     {
215b9b2e0b2SEd Tanous         entityPrivileges = {
216b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
217b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}}},
218b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
219b9b2e0b2SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
220b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
221b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
222b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
223b9b2e0b2SEd Tanous     }
224b9b2e0b2SEd Tanous 
225b9b2e0b2SEd Tanous   private:
226b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
227b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
228b9b2e0b2SEd Tanous     {
229b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
2300f74e643SEd Tanous         res.jsonValue = {{"@odata.context",
2310f74e643SEd Tanous                           "/redfish/v1/"
2320f74e643SEd Tanous                           "$metadata#ManagerAccountCollection."
2330f74e643SEd Tanous                           "ManagerAccountCollection"},
2340f74e643SEd Tanous                          {"@odata.id", "/redfish/v1/AccountService/Accounts"},
2350f74e643SEd Tanous                          {"@odata.type", "#ManagerAccountCollection."
2360f74e643SEd Tanous                                          "ManagerAccountCollection"},
2370f74e643SEd Tanous                          {"Name", "Accounts Collection"},
2380f74e643SEd Tanous                          {"Description", "BMC User Accounts"}};
2390f74e643SEd Tanous 
240b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
241b9b2e0b2SEd Tanous             [asyncResp](const boost::system::error_code ec,
242b9b2e0b2SEd Tanous                         const ManagedObjectType& users) {
243b9b2e0b2SEd Tanous                 if (ec)
244b9b2e0b2SEd Tanous                 {
245f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
246b9b2e0b2SEd Tanous                     return;
247b9b2e0b2SEd Tanous                 }
248b9b2e0b2SEd Tanous 
249b9b2e0b2SEd Tanous                 nlohmann::json& memberArray =
250b9b2e0b2SEd Tanous                     asyncResp->res.jsonValue["Members"];
251b9b2e0b2SEd Tanous                 memberArray = nlohmann::json::array();
252b9b2e0b2SEd Tanous 
253b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = users.size();
254b9b2e0b2SEd Tanous                 for (auto& user : users)
255b9b2e0b2SEd Tanous                 {
256b9b2e0b2SEd Tanous                     const std::string& path =
257b9b2e0b2SEd Tanous                         static_cast<const std::string&>(user.first);
258b9b2e0b2SEd Tanous                     std::size_t lastIndex = path.rfind("/");
259b9b2e0b2SEd Tanous                     if (lastIndex == std::string::npos)
260b9b2e0b2SEd Tanous                     {
261b9b2e0b2SEd Tanous                         lastIndex = 0;
262b9b2e0b2SEd Tanous                     }
263b9b2e0b2SEd Tanous                     else
264b9b2e0b2SEd Tanous                     {
265b9b2e0b2SEd Tanous                         lastIndex += 1;
266b9b2e0b2SEd Tanous                     }
267b9b2e0b2SEd Tanous                     memberArray.push_back(
268b9b2e0b2SEd Tanous                         {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
269b9b2e0b2SEd Tanous                                            path.substr(lastIndex)}});
270b9b2e0b2SEd Tanous                 }
271b9b2e0b2SEd Tanous             },
272b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
273b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
274b9b2e0b2SEd Tanous     }
27504ae99ecSEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
27604ae99ecSEd Tanous                 const std::vector<std::string>& params) override
27704ae99ecSEd Tanous     {
27804ae99ecSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
27904ae99ecSEd Tanous 
2809712f8acSEd Tanous         std::string username;
2819712f8acSEd Tanous         std::string password;
282a24526dcSEd Tanous         std::optional<std::string> roleId("User");
283a24526dcSEd Tanous         std::optional<bool> enabled = true;
2849712f8acSEd Tanous         if (!json_util::readJson(req, res, "UserName", username, "Password",
2859712f8acSEd Tanous                                  password, "RoleId", roleId, "Enabled",
2869712f8acSEd Tanous                                  enabled))
28704ae99ecSEd Tanous         {
28804ae99ecSEd Tanous             return;
28904ae99ecSEd Tanous         }
29004ae99ecSEd Tanous 
29184e12cb7SAppaRao Puli         std::string priv = getRoleIdFromPrivilege(*roleId);
29284e12cb7SAppaRao Puli         if (priv.empty())
29304ae99ecSEd Tanous         {
294f12894f8SJason M. Bills             messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
29504ae99ecSEd Tanous             return;
29604ae99ecSEd Tanous         }
2979712f8acSEd Tanous         roleId = priv;
29804ae99ecSEd Tanous 
29904ae99ecSEd Tanous         crow::connections::systemBus->async_method_call(
3009712f8acSEd Tanous             [asyncResp, username, password{std::move(password)}](
30104ae99ecSEd Tanous                 const boost::system::error_code ec) {
30204ae99ecSEd Tanous                 if (ec)
30304ae99ecSEd Tanous                 {
30404ae99ecSEd Tanous                     messages::resourceAlreadyExists(
305f12894f8SJason M. Bills                         asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
306f12894f8SJason M. Bills                         "UserName", username);
30704ae99ecSEd Tanous                     return;
30804ae99ecSEd Tanous                 }
30904ae99ecSEd Tanous 
31004ae99ecSEd Tanous                 if (!pamUpdatePassword(username, password))
31104ae99ecSEd Tanous                 {
31204ae99ecSEd Tanous                     // At this point we have a user that's been created, but the
31304ae99ecSEd Tanous                     // password set failed.  Something is wrong, so delete the
31404ae99ecSEd Tanous                     // user that we've already created
31504ae99ecSEd Tanous                     crow::connections::systemBus->async_method_call(
31604ae99ecSEd Tanous                         [asyncResp](const boost::system::error_code ec) {
31704ae99ecSEd Tanous                             if (ec)
31804ae99ecSEd Tanous                             {
319f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
32004ae99ecSEd Tanous                                 return;
32104ae99ecSEd Tanous                             }
32204ae99ecSEd Tanous 
323f12894f8SJason M. Bills                             messages::invalidObject(asyncResp->res, "Password");
32404ae99ecSEd Tanous                         },
32504ae99ecSEd Tanous                         "xyz.openbmc_project.User.Manager",
32604ae99ecSEd Tanous                         "/xyz/openbmc_project/user/" + username,
32704ae99ecSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
32804ae99ecSEd Tanous 
32904ae99ecSEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
33004ae99ecSEd Tanous                     return;
33104ae99ecSEd Tanous                 }
33204ae99ecSEd Tanous 
333f12894f8SJason M. Bills                 messages::created(asyncResp->res);
33404ae99ecSEd Tanous                 asyncResp->res.addHeader(
33504ae99ecSEd Tanous                     "Location",
33604ae99ecSEd Tanous                     "/redfish/v1/AccountService/Accounts/" + username);
33704ae99ecSEd Tanous             },
33804ae99ecSEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
3399712f8acSEd Tanous             "xyz.openbmc_project.User.Manager", "CreateUser", username,
34004ae99ecSEd Tanous             std::array<const char*, 4>{"ipmi", "redfish", "ssh", "web"},
3419712f8acSEd Tanous             *roleId, *enabled);
34204ae99ecSEd Tanous     }
343b9b2e0b2SEd Tanous };
344b9b2e0b2SEd Tanous 
345a840879dSEd Tanous template <typename Callback>
346a840879dSEd Tanous inline void checkDbusPathExists(const std::string& path, Callback&& callback)
347a840879dSEd Tanous {
348a840879dSEd Tanous     using GetObjectType =
349a840879dSEd Tanous         std::vector<std::pair<std::string, std::vector<std::string>>>;
350a840879dSEd Tanous 
351a840879dSEd Tanous     crow::connections::systemBus->async_method_call(
352a840879dSEd Tanous         [callback{std::move(callback)}](const boost::system::error_code ec,
353a840879dSEd Tanous                                         const GetObjectType& object_names) {
35484e12cb7SAppaRao Puli             callback(!ec && object_names.size() != 0);
355a840879dSEd Tanous         },
356a840879dSEd Tanous         "xyz.openbmc_project.ObjectMapper",
357a840879dSEd Tanous         "/xyz/openbmc_project/object_mapper",
358a840879dSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetObject", path,
359a840879dSEd Tanous         std::array<std::string, 0>());
360a840879dSEd Tanous }
361a840879dSEd Tanous 
362b9b2e0b2SEd Tanous class ManagerAccount : public Node
363b9b2e0b2SEd Tanous {
364b9b2e0b2SEd Tanous   public:
365b9b2e0b2SEd Tanous     ManagerAccount(CrowApp& app) :
366b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string())
367b9b2e0b2SEd Tanous     {
368b9b2e0b2SEd Tanous         entityPrivileges = {
369b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
370b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}},
371b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
372b9b2e0b2SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
373b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
374b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
375b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
376b9b2e0b2SEd Tanous     }
377b9b2e0b2SEd Tanous 
378b9b2e0b2SEd Tanous   private:
379b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
380b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
381b9b2e0b2SEd Tanous     {
3820f74e643SEd Tanous         res.jsonValue = {
3830f74e643SEd Tanous             {"@odata.context",
3840f74e643SEd Tanous              "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
3850f74e643SEd Tanous             {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"},
3860f74e643SEd Tanous             {"Name", "User Account"},
3870f74e643SEd Tanous             {"Description", "User Account"},
3880f74e643SEd Tanous             {"Password", nullptr},
38984e12cb7SAppaRao Puli             {"RoleId", "Administrator"}};
3900f74e643SEd Tanous 
391b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
392b9b2e0b2SEd Tanous 
393b9b2e0b2SEd Tanous         if (params.size() != 1)
394b9b2e0b2SEd Tanous         {
395f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
396b9b2e0b2SEd Tanous             return;
397b9b2e0b2SEd Tanous         }
398b9b2e0b2SEd Tanous 
399b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
400b9b2e0b2SEd Tanous             [asyncResp, accountName{std::string(params[0])}](
401b9b2e0b2SEd Tanous                 const boost::system::error_code ec,
402b9b2e0b2SEd Tanous                 const ManagedObjectType& users) {
403b9b2e0b2SEd Tanous                 if (ec)
404b9b2e0b2SEd Tanous                 {
405f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
406b9b2e0b2SEd Tanous                     return;
407b9b2e0b2SEd Tanous                 }
40884e12cb7SAppaRao Puli                 auto userIt = users.begin();
409b9b2e0b2SEd Tanous 
41084e12cb7SAppaRao Puli                 for (; userIt != users.end(); userIt++)
411b9b2e0b2SEd Tanous                 {
41284e12cb7SAppaRao Puli                     if (boost::ends_with(userIt->first.str, "/" + accountName))
413b9b2e0b2SEd Tanous                     {
41484e12cb7SAppaRao Puli                         break;
415b9b2e0b2SEd Tanous                     }
416b9b2e0b2SEd Tanous                 }
41784e12cb7SAppaRao Puli                 if (userIt == users.end())
418b9b2e0b2SEd Tanous                 {
41984e12cb7SAppaRao Puli                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
42084e12cb7SAppaRao Puli                                                accountName);
42184e12cb7SAppaRao Puli                     return;
42284e12cb7SAppaRao Puli                 }
42384e12cb7SAppaRao Puli                 for (const auto& interface : userIt->second)
42465b0dc32SEd Tanous                 {
42565b0dc32SEd Tanous                     if (interface.first ==
42665b0dc32SEd Tanous                         "xyz.openbmc_project.User.Attributes")
42765b0dc32SEd Tanous                     {
42865b0dc32SEd Tanous                         for (const auto& property : interface.second)
42965b0dc32SEd Tanous                         {
43065b0dc32SEd Tanous                             if (property.first == "UserEnabled")
43165b0dc32SEd Tanous                             {
43265b0dc32SEd Tanous                                 const bool* userEnabled =
433abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
43465b0dc32SEd Tanous                                 if (userEnabled == nullptr)
43565b0dc32SEd Tanous                                 {
43665b0dc32SEd Tanous                                     BMCWEB_LOG_ERROR
43765b0dc32SEd Tanous                                         << "UserEnabled wasn't a bool";
43884e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
43984e12cb7SAppaRao Puli                                     return;
44065b0dc32SEd Tanous                                 }
44165b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Enabled"] =
44265b0dc32SEd Tanous                                     *userEnabled;
44365b0dc32SEd Tanous                             }
44465b0dc32SEd Tanous                             else if (property.first ==
44565b0dc32SEd Tanous                                      "UserLockedForFailedAttempt")
44665b0dc32SEd Tanous                             {
44765b0dc32SEd Tanous                                 const bool* userLocked =
448abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
44965b0dc32SEd Tanous                                 if (userLocked == nullptr)
45065b0dc32SEd Tanous                                 {
45184e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "UserLockedForF"
45284e12cb7SAppaRao Puli                                                         "ailedAttempt "
45384e12cb7SAppaRao Puli                                                         "wasn't a bool";
45484e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
45584e12cb7SAppaRao Puli                                     return;
45665b0dc32SEd Tanous                                 }
45765b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Locked"] =
45865b0dc32SEd Tanous                                     *userLocked;
459*24c8542dSRatan Gupta                                 asyncResp->res.jsonValue
460*24c8542dSRatan Gupta                                     ["Locked@Redfish.AllowableValues"] = {
461*24c8542dSRatan Gupta                                     false};
46265b0dc32SEd Tanous                             }
46384e12cb7SAppaRao Puli                             else if (property.first == "UserPrivilege")
46484e12cb7SAppaRao Puli                             {
46584e12cb7SAppaRao Puli                                 const std::string* userRolePtr =
466abf2add6SEd Tanous                                     std::get_if<std::string>(&property.second);
46784e12cb7SAppaRao Puli                                 if (userRolePtr == nullptr)
46884e12cb7SAppaRao Puli                                 {
46984e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR
47084e12cb7SAppaRao Puli                                         << "UserPrivilege wasn't a "
47184e12cb7SAppaRao Puli                                            "string";
47284e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
47384e12cb7SAppaRao Puli                                     return;
47484e12cb7SAppaRao Puli                                 }
47584e12cb7SAppaRao Puli                                 std::string priv =
47684e12cb7SAppaRao Puli                                     getPrivilegeFromRoleId(*userRolePtr);
47784e12cb7SAppaRao Puli                                 if (priv.empty())
47884e12cb7SAppaRao Puli                                 {
47984e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "Invalid user role";
48084e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
48184e12cb7SAppaRao Puli                                     return;
48284e12cb7SAppaRao Puli                                 }
48384e12cb7SAppaRao Puli                                 asyncResp->res.jsonValue["RoleId"] = priv;
48484e12cb7SAppaRao Puli 
48584e12cb7SAppaRao Puli                                 asyncResp->res.jsonValue["Links"]["Role"] = {
48684e12cb7SAppaRao Puli                                     {"@odata.id", "/redfish/v1/AccountService/"
48784e12cb7SAppaRao Puli                                                   "Roles/" +
48884e12cb7SAppaRao Puli                                                       priv}};
48984e12cb7SAppaRao Puli                             }
49065b0dc32SEd Tanous                         }
49165b0dc32SEd Tanous                     }
49265b0dc32SEd Tanous                 }
49365b0dc32SEd Tanous 
494b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
49584e12cb7SAppaRao Puli                     "/redfish/v1/AccountService/Accounts/" + accountName;
496b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Id"] = accountName;
497b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["UserName"] = accountName;
498b9b2e0b2SEd Tanous             },
499b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
500b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
501b9b2e0b2SEd Tanous     }
502a840879dSEd Tanous 
503a840879dSEd Tanous     void doPatch(crow::Response& res, const crow::Request& req,
504a840879dSEd Tanous                  const std::vector<std::string>& params) override
505a840879dSEd Tanous     {
506a840879dSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
507a840879dSEd Tanous         if (params.size() != 1)
508a840879dSEd Tanous         {
509f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
510a840879dSEd Tanous             return;
511a840879dSEd Tanous         }
512a840879dSEd Tanous 
513a24526dcSEd Tanous         std::optional<std::string> newUserName;
514a24526dcSEd Tanous         std::optional<std::string> password;
515a24526dcSEd Tanous         std::optional<bool> enabled;
516a24526dcSEd Tanous         std::optional<std::string> roleId;
517*24c8542dSRatan Gupta         std::optional<bool> locked;
51884e12cb7SAppaRao Puli         if (!json_util::readJson(req, res, "UserName", newUserName, "Password",
519*24c8542dSRatan Gupta                                  password, "RoleId", roleId, "Enabled", enabled,
520*24c8542dSRatan Gupta                                  "Locked", locked))
521a840879dSEd Tanous         {
522a840879dSEd Tanous             return;
523a840879dSEd Tanous         }
524a840879dSEd Tanous 
52584e12cb7SAppaRao Puli         const std::string& username = params[0];
52684e12cb7SAppaRao Puli 
52784e12cb7SAppaRao Puli         if (!newUserName)
528a840879dSEd Tanous         {
52984e12cb7SAppaRao Puli             // If the username isn't being updated, we can update the properties
53084e12cb7SAppaRao Puli             // directly
531*24c8542dSRatan Gupta             updateUserProperties(asyncResp, username, password, enabled, roleId,
532*24c8542dSRatan Gupta                                  locked);
53384e12cb7SAppaRao Puli             return;
53484e12cb7SAppaRao Puli         }
53584e12cb7SAppaRao Puli         else
53684e12cb7SAppaRao Puli         {
53784e12cb7SAppaRao Puli             crow::connections::systemBus->async_method_call(
53884e12cb7SAppaRao Puli                 [this, asyncResp, username, password(std::move(password)),
53984e12cb7SAppaRao Puli                  roleId(std::move(roleId)), enabled(std::move(enabled)),
540*24c8542dSRatan Gupta                  newUser{std::string(*newUserName)}, locked(std::move(locked))](
54184e12cb7SAppaRao Puli                     const boost::system::error_code ec) {
54284e12cb7SAppaRao Puli                     if (ec)
54384e12cb7SAppaRao Puli                     {
54484e12cb7SAppaRao Puli                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
545a840879dSEd Tanous                         messages::resourceNotFound(
54684e12cb7SAppaRao Puli                             asyncResp->res,
54784e12cb7SAppaRao Puli                             "#ManagerAccount.v1_0_3.ManagerAccount", username);
548a840879dSEd Tanous                         return;
549a840879dSEd Tanous                     }
550a840879dSEd Tanous 
55184e12cb7SAppaRao Puli                     updateUserProperties(asyncResp, newUser, password, enabled,
552*24c8542dSRatan Gupta                                          roleId, locked);
55384e12cb7SAppaRao Puli                 },
55484e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
55584e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "RenameUser", username,
55684e12cb7SAppaRao Puli                 *newUserName);
55784e12cb7SAppaRao Puli         }
55884e12cb7SAppaRao Puli     }
55984e12cb7SAppaRao Puli 
56084e12cb7SAppaRao Puli     void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp,
56184e12cb7SAppaRao Puli                               const std::string& username,
562a24526dcSEd Tanous                               std::optional<std::string> password,
563a24526dcSEd Tanous                               std::optional<bool> enabled,
564*24c8542dSRatan Gupta                               std::optional<std::string> roleId,
565*24c8542dSRatan Gupta                               std::optional<bool> locked)
56684e12cb7SAppaRao Puli     {
5679712f8acSEd Tanous         if (password)
568a840879dSEd Tanous         {
5699712f8acSEd Tanous             if (!pamUpdatePassword(username, *password))
570a840879dSEd Tanous             {
571a840879dSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
572f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
573a840879dSEd Tanous                 return;
574a840879dSEd Tanous             }
575a840879dSEd Tanous         }
576a840879dSEd Tanous 
577*24c8542dSRatan Gupta         std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username;
578*24c8542dSRatan Gupta         dbus::utility::escapePathForDbus(dbusObjectPath);
579*24c8542dSRatan Gupta 
580*24c8542dSRatan Gupta         checkDbusPathExists(
581*24c8542dSRatan Gupta             dbusObjectPath,
582*24c8542dSRatan Gupta             [dbusObjectPath(std::move(dbusObjectPath)), username,
583*24c8542dSRatan Gupta              password(std::move(password)), roleId(std::move(roleId)),
584*24c8542dSRatan Gupta              enabled(std::move(enabled)), locked(std::move(locked)),
585*24c8542dSRatan Gupta              asyncResp{std::move(asyncResp)}](int rc) {
586*24c8542dSRatan Gupta                 if (!rc)
587*24c8542dSRatan Gupta                 {
588*24c8542dSRatan Gupta                     messages::invalidObject(asyncResp->res, username.c_str());
589*24c8542dSRatan Gupta                     return;
590*24c8542dSRatan Gupta                 }
5919712f8acSEd Tanous                 if (enabled)
592a840879dSEd Tanous                 {
593a840879dSEd Tanous                     crow::connections::systemBus->async_method_call(
594a840879dSEd Tanous                         [asyncResp](const boost::system::error_code ec) {
595a840879dSEd Tanous                             if (ec)
596a840879dSEd Tanous                             {
597*24c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
598*24c8542dSRatan Gupta                                                  << ec;
599f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
600a840879dSEd Tanous                                 return;
601a840879dSEd Tanous                             }
60284e12cb7SAppaRao Puli                             messages::success(asyncResp->res);
60384e12cb7SAppaRao Puli                             return;
60484e12cb7SAppaRao Puli                         },
60584e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Manager",
606*24c8542dSRatan Gupta                         dbusObjectPath.c_str(),
60784e12cb7SAppaRao Puli                         "org.freedesktop.DBus.Properties", "Set",
60884e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserEnabled",
609abf2add6SEd Tanous                         std::variant<bool>{*enabled});
61084e12cb7SAppaRao Puli                 }
61184e12cb7SAppaRao Puli 
61284e12cb7SAppaRao Puli                 if (roleId)
61384e12cb7SAppaRao Puli                 {
61484e12cb7SAppaRao Puli                     std::string priv = getRoleIdFromPrivilege(*roleId);
61584e12cb7SAppaRao Puli                     if (priv.empty())
61684e12cb7SAppaRao Puli                     {
617*24c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res,
618*24c8542dSRatan Gupta                                                          *roleId, "RoleId");
61984e12cb7SAppaRao Puli                         return;
62084e12cb7SAppaRao Puli                     }
62184e12cb7SAppaRao Puli 
62284e12cb7SAppaRao Puli                     crow::connections::systemBus->async_method_call(
62384e12cb7SAppaRao Puli                         [asyncResp](const boost::system::error_code ec) {
62484e12cb7SAppaRao Puli                             if (ec)
62584e12cb7SAppaRao Puli                             {
626*24c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
627*24c8542dSRatan Gupta                                                  << ec;
62884e12cb7SAppaRao Puli                                 messages::internalError(asyncResp->res);
62984e12cb7SAppaRao Puli                                 return;
63084e12cb7SAppaRao Puli                             }
631f12894f8SJason M. Bills                             messages::success(asyncResp->res);
632a840879dSEd Tanous                         },
633a840879dSEd Tanous                         "xyz.openbmc_project.User.Manager",
634*24c8542dSRatan Gupta                         dbusObjectPath.c_str(),
635a840879dSEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
63684e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserPrivilege",
637abf2add6SEd Tanous                         std::variant<std::string>{priv});
638a840879dSEd Tanous                 }
639*24c8542dSRatan Gupta 
640*24c8542dSRatan Gupta                 if (locked)
641*24c8542dSRatan Gupta                 {
642*24c8542dSRatan Gupta                     // admin can unlock the account which is locked by
643*24c8542dSRatan Gupta                     // successive authentication failures but admin should not
644*24c8542dSRatan Gupta                     // be allowed to lock an account.
645*24c8542dSRatan Gupta                     if (*locked)
646*24c8542dSRatan Gupta                     {
647*24c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res, "true",
648*24c8542dSRatan Gupta                                                          "Locked");
649*24c8542dSRatan Gupta                         return;
650*24c8542dSRatan Gupta                     }
651*24c8542dSRatan Gupta 
652*24c8542dSRatan Gupta                     crow::connections::systemBus->async_method_call(
653*24c8542dSRatan Gupta                         [asyncResp](const boost::system::error_code ec) {
654*24c8542dSRatan Gupta                             if (ec)
655*24c8542dSRatan Gupta                             {
656*24c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
657*24c8542dSRatan Gupta                                                  << ec;
658*24c8542dSRatan Gupta                                 messages::internalError(asyncResp->res);
659*24c8542dSRatan Gupta                                 return;
660*24c8542dSRatan Gupta                             }
661*24c8542dSRatan Gupta                             messages::success(asyncResp->res);
662*24c8542dSRatan Gupta                             return;
663*24c8542dSRatan Gupta                         },
664*24c8542dSRatan Gupta                         "xyz.openbmc_project.User.Manager",
665*24c8542dSRatan Gupta                         dbusObjectPath.c_str(),
666*24c8542dSRatan Gupta                         "org.freedesktop.DBus.Properties", "Set",
667*24c8542dSRatan Gupta                         "xyz.openbmc_project.User.Attributes",
668*24c8542dSRatan Gupta                         "UserLockedForFailedAttempt",
669*24c8542dSRatan Gupta                         sdbusplus::message::variant<bool>{*locked});
670*24c8542dSRatan Gupta                 }
671*24c8542dSRatan Gupta             });
672a840879dSEd Tanous     }
67306e086d9SEd Tanous 
67406e086d9SEd Tanous     void doDelete(crow::Response& res, const crow::Request& req,
67506e086d9SEd Tanous                   const std::vector<std::string>& params) override
67606e086d9SEd Tanous     {
67706e086d9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
67806e086d9SEd Tanous 
67906e086d9SEd Tanous         if (params.size() != 1)
68006e086d9SEd Tanous         {
681f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
68206e086d9SEd Tanous             return;
68306e086d9SEd Tanous         }
68406e086d9SEd Tanous 
68506e086d9SEd Tanous         const std::string userPath = "/xyz/openbmc_project/user/" + params[0];
68606e086d9SEd Tanous 
68706e086d9SEd Tanous         crow::connections::systemBus->async_method_call(
68806e086d9SEd Tanous             [asyncResp, username{std::move(params[0])}](
68906e086d9SEd Tanous                 const boost::system::error_code ec) {
69006e086d9SEd Tanous                 if (ec)
69106e086d9SEd Tanous                 {
69206e086d9SEd Tanous                     messages::resourceNotFound(
693f12894f8SJason M. Bills                         asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
694f12894f8SJason M. Bills                         username);
69506e086d9SEd Tanous                     return;
69606e086d9SEd Tanous                 }
69706e086d9SEd Tanous 
698f12894f8SJason M. Bills                 messages::accountRemoved(asyncResp->res);
69906e086d9SEd Tanous             },
70006e086d9SEd Tanous             "xyz.openbmc_project.User.Manager", userPath,
70106e086d9SEd Tanous             "xyz.openbmc_project.Object.Delete", "Delete");
70206e086d9SEd Tanous     }
70384e12cb7SAppaRao Puli };
70488d16c9aSLewanczyk, Dawid 
70588d16c9aSLewanczyk, Dawid } // namespace redfish
706