xref: /openbmc/bmcweb/features/redfish/lib/account_service.hpp (revision f00032db72e7ee88946bb4839b5d5311a3e092b3)
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},
105343ff2e1SAppaRao Puli             {"MaxPasswordLength", 20},
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;
17019fb6e71SRatan Gupta         std::optional<uint16_t> minPasswordLength;
17119fb6e71SRatan Gupta         std::optional<uint16_t> maxPasswordLength;
17219fb6e71SRatan Gupta 
1733d958bbcSAppaRao Puli         if (!json_util::readJson(req, res, "AccountLockoutDuration",
1743d958bbcSAppaRao Puli                                  unlockTimeout, "AccountLockoutThreshold",
17519fb6e71SRatan Gupta                                  lockoutThreshold, "MaxPasswordLength",
17619fb6e71SRatan Gupta                                  maxPasswordLength, "MinPasswordLength",
17719fb6e71SRatan Gupta                                  minPasswordLength))
1783d958bbcSAppaRao Puli         {
1793d958bbcSAppaRao Puli             return;
1803d958bbcSAppaRao Puli         }
18119fb6e71SRatan Gupta 
18219fb6e71SRatan Gupta         if (minPasswordLength)
18319fb6e71SRatan Gupta         {
18419fb6e71SRatan Gupta             messages::propertyNotWritable(asyncResp->res, "MinPasswordLength");
18519fb6e71SRatan Gupta         }
18619fb6e71SRatan Gupta 
18719fb6e71SRatan Gupta         if (maxPasswordLength)
18819fb6e71SRatan Gupta         {
18919fb6e71SRatan Gupta             messages::propertyNotWritable(asyncResp->res, "MaxPasswordLength");
19019fb6e71SRatan Gupta         }
19119fb6e71SRatan Gupta 
1923d958bbcSAppaRao Puli         if (unlockTimeout)
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                     }
201add6133bSRatan 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",
206abf2add6SEd Tanous                 "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout));
2073d958bbcSAppaRao Puli         }
2083d958bbcSAppaRao Puli         if (lockoutThreshold)
2093d958bbcSAppaRao Puli         {
2103d958bbcSAppaRao Puli             crow::connections::systemBus->async_method_call(
2113d958bbcSAppaRao Puli                 [asyncResp](const boost::system::error_code ec) {
2123d958bbcSAppaRao Puli                     if (ec)
2133d958bbcSAppaRao Puli                     {
2143d958bbcSAppaRao Puli                         messages::internalError(asyncResp->res);
2153d958bbcSAppaRao Puli                         return;
2163d958bbcSAppaRao Puli                     }
217add6133bSRatan Gupta                     messages::success(asyncResp->res);
2183d958bbcSAppaRao Puli                 },
2193d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
2203d958bbcSAppaRao Puli                 "org.freedesktop.DBus.Properties", "Set",
2213d958bbcSAppaRao Puli                 "xyz.openbmc_project.User.AccountPolicy",
2223d958bbcSAppaRao Puli                 "MaxLoginAttemptBeforeLockout",
223abf2add6SEd Tanous                 std::variant<uint16_t>(*lockoutThreshold));
2243d958bbcSAppaRao Puli         }
22588d16c9aSLewanczyk, Dawid     }
22688d16c9aSLewanczyk, Dawid };
227*f00032dbSTanous 
228b9b2e0b2SEd Tanous class AccountsCollection : public Node
229b9b2e0b2SEd Tanous {
230b9b2e0b2SEd Tanous   public:
231b9b2e0b2SEd Tanous     AccountsCollection(CrowApp& app) :
232b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/")
233b9b2e0b2SEd Tanous     {
234b9b2e0b2SEd Tanous         entityPrivileges = {
235b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
236b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}}},
237b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
238b9b2e0b2SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
239b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
240b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
241b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
242b9b2e0b2SEd Tanous     }
243b9b2e0b2SEd Tanous 
244b9b2e0b2SEd Tanous   private:
245b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
246b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
247b9b2e0b2SEd Tanous     {
248b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
2490f74e643SEd Tanous         res.jsonValue = {{"@odata.context",
2500f74e643SEd Tanous                           "/redfish/v1/"
2510f74e643SEd Tanous                           "$metadata#ManagerAccountCollection."
2520f74e643SEd Tanous                           "ManagerAccountCollection"},
2530f74e643SEd Tanous                          {"@odata.id", "/redfish/v1/AccountService/Accounts"},
2540f74e643SEd Tanous                          {"@odata.type", "#ManagerAccountCollection."
2550f74e643SEd Tanous                                          "ManagerAccountCollection"},
2560f74e643SEd Tanous                          {"Name", "Accounts Collection"},
2570f74e643SEd Tanous                          {"Description", "BMC User Accounts"}};
2580f74e643SEd Tanous 
259b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
260b9b2e0b2SEd Tanous             [asyncResp](const boost::system::error_code ec,
261b9b2e0b2SEd Tanous                         const ManagedObjectType& users) {
262b9b2e0b2SEd Tanous                 if (ec)
263b9b2e0b2SEd Tanous                 {
264f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
265b9b2e0b2SEd Tanous                     return;
266b9b2e0b2SEd Tanous                 }
267b9b2e0b2SEd Tanous 
268b9b2e0b2SEd Tanous                 nlohmann::json& memberArray =
269b9b2e0b2SEd Tanous                     asyncResp->res.jsonValue["Members"];
270b9b2e0b2SEd Tanous                 memberArray = nlohmann::json::array();
271b9b2e0b2SEd Tanous 
272b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = users.size();
273b9b2e0b2SEd Tanous                 for (auto& user : users)
274b9b2e0b2SEd Tanous                 {
275b9b2e0b2SEd Tanous                     const std::string& path =
276b9b2e0b2SEd Tanous                         static_cast<const std::string&>(user.first);
277b9b2e0b2SEd Tanous                     std::size_t lastIndex = path.rfind("/");
278b9b2e0b2SEd Tanous                     if (lastIndex == std::string::npos)
279b9b2e0b2SEd Tanous                     {
280b9b2e0b2SEd Tanous                         lastIndex = 0;
281b9b2e0b2SEd Tanous                     }
282b9b2e0b2SEd Tanous                     else
283b9b2e0b2SEd Tanous                     {
284b9b2e0b2SEd Tanous                         lastIndex += 1;
285b9b2e0b2SEd Tanous                     }
286b9b2e0b2SEd Tanous                     memberArray.push_back(
287b9b2e0b2SEd Tanous                         {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
288b9b2e0b2SEd Tanous                                            path.substr(lastIndex)}});
289b9b2e0b2SEd Tanous                 }
290b9b2e0b2SEd Tanous             },
291b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
292b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
293b9b2e0b2SEd Tanous     }
29404ae99ecSEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
29504ae99ecSEd Tanous                 const std::vector<std::string>& params) override
29604ae99ecSEd Tanous     {
29704ae99ecSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
29804ae99ecSEd Tanous 
2999712f8acSEd Tanous         std::string username;
3009712f8acSEd Tanous         std::string password;
301a24526dcSEd Tanous         std::optional<std::string> roleId("User");
302a24526dcSEd Tanous         std::optional<bool> enabled = true;
3039712f8acSEd Tanous         if (!json_util::readJson(req, res, "UserName", username, "Password",
3049712f8acSEd Tanous                                  password, "RoleId", roleId, "Enabled",
3059712f8acSEd Tanous                                  enabled))
30604ae99ecSEd Tanous         {
30704ae99ecSEd Tanous             return;
30804ae99ecSEd Tanous         }
30904ae99ecSEd Tanous 
31084e12cb7SAppaRao Puli         std::string priv = getRoleIdFromPrivilege(*roleId);
31184e12cb7SAppaRao Puli         if (priv.empty())
31204ae99ecSEd Tanous         {
313f12894f8SJason M. Bills             messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId");
31404ae99ecSEd Tanous             return;
31504ae99ecSEd Tanous         }
3169712f8acSEd Tanous         roleId = priv;
31704ae99ecSEd Tanous 
31804ae99ecSEd Tanous         crow::connections::systemBus->async_method_call(
3199712f8acSEd Tanous             [asyncResp, username, password{std::move(password)}](
32004ae99ecSEd Tanous                 const boost::system::error_code ec) {
32104ae99ecSEd Tanous                 if (ec)
32204ae99ecSEd Tanous                 {
32304ae99ecSEd Tanous                     messages::resourceAlreadyExists(
324f12894f8SJason M. Bills                         asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
325f12894f8SJason M. Bills                         "UserName", username);
32604ae99ecSEd Tanous                     return;
32704ae99ecSEd Tanous                 }
32804ae99ecSEd Tanous 
32904ae99ecSEd Tanous                 if (!pamUpdatePassword(username, password))
33004ae99ecSEd Tanous                 {
33104ae99ecSEd Tanous                     // At this point we have a user that's been created, but the
33204ae99ecSEd Tanous                     // password set failed.  Something is wrong, so delete the
33304ae99ecSEd Tanous                     // user that we've already created
33404ae99ecSEd Tanous                     crow::connections::systemBus->async_method_call(
33504ae99ecSEd Tanous                         [asyncResp](const boost::system::error_code ec) {
33604ae99ecSEd Tanous                             if (ec)
33704ae99ecSEd Tanous                             {
338f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
33904ae99ecSEd Tanous                                 return;
34004ae99ecSEd Tanous                             }
34104ae99ecSEd Tanous 
342f12894f8SJason M. Bills                             messages::invalidObject(asyncResp->res, "Password");
34304ae99ecSEd Tanous                         },
34404ae99ecSEd Tanous                         "xyz.openbmc_project.User.Manager",
34504ae99ecSEd Tanous                         "/xyz/openbmc_project/user/" + username,
34604ae99ecSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
34704ae99ecSEd Tanous 
34804ae99ecSEd Tanous                     BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
34904ae99ecSEd Tanous                     return;
35004ae99ecSEd Tanous                 }
35104ae99ecSEd Tanous 
352f12894f8SJason M. Bills                 messages::created(asyncResp->res);
35304ae99ecSEd Tanous                 asyncResp->res.addHeader(
35404ae99ecSEd Tanous                     "Location",
35504ae99ecSEd Tanous                     "/redfish/v1/AccountService/Accounts/" + username);
35604ae99ecSEd Tanous             },
35704ae99ecSEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
3589712f8acSEd Tanous             "xyz.openbmc_project.User.Manager", "CreateUser", username,
35904ae99ecSEd Tanous             std::array<const char*, 4>{"ipmi", "redfish", "ssh", "web"},
3609712f8acSEd Tanous             *roleId, *enabled);
36104ae99ecSEd Tanous     }
362b9b2e0b2SEd Tanous };
363b9b2e0b2SEd Tanous 
364a840879dSEd Tanous template <typename Callback>
365a840879dSEd Tanous inline void checkDbusPathExists(const std::string& path, Callback&& callback)
366a840879dSEd Tanous {
367a840879dSEd Tanous     using GetObjectType =
368a840879dSEd Tanous         std::vector<std::pair<std::string, std::vector<std::string>>>;
369a840879dSEd Tanous 
370a840879dSEd Tanous     crow::connections::systemBus->async_method_call(
371a840879dSEd Tanous         [callback{std::move(callback)}](const boost::system::error_code ec,
372a840879dSEd Tanous                                         const GetObjectType& object_names) {
37384e12cb7SAppaRao Puli             callback(!ec && object_names.size() != 0);
374a840879dSEd Tanous         },
375a840879dSEd Tanous         "xyz.openbmc_project.ObjectMapper",
376a840879dSEd Tanous         "/xyz/openbmc_project/object_mapper",
377a840879dSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetObject", path,
378a840879dSEd Tanous         std::array<std::string, 0>());
379a840879dSEd Tanous }
380a840879dSEd Tanous 
381b9b2e0b2SEd Tanous class ManagerAccount : public Node
382b9b2e0b2SEd Tanous {
383b9b2e0b2SEd Tanous   public:
384b9b2e0b2SEd Tanous     ManagerAccount(CrowApp& app) :
385b9b2e0b2SEd Tanous         Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string())
386b9b2e0b2SEd Tanous     {
387b9b2e0b2SEd Tanous         entityPrivileges = {
388b9b2e0b2SEd Tanous             {boost::beast::http::verb::get,
389b9b2e0b2SEd Tanous              {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}},
390b9b2e0b2SEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
391b9b2e0b2SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureUsers"}}},
392b9b2e0b2SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureUsers"}}},
393b9b2e0b2SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}},
394b9b2e0b2SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureUsers"}}}};
395b9b2e0b2SEd Tanous     }
396b9b2e0b2SEd Tanous 
397b9b2e0b2SEd Tanous   private:
398b9b2e0b2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
399b9b2e0b2SEd Tanous                const std::vector<std::string>& params) override
400b9b2e0b2SEd Tanous     {
4010f74e643SEd Tanous         res.jsonValue = {
4020f74e643SEd Tanous             {"@odata.context",
4030f74e643SEd Tanous              "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
4040f74e643SEd Tanous             {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"},
4050f74e643SEd Tanous             {"Name", "User Account"},
4060f74e643SEd Tanous             {"Description", "User Account"},
4070f74e643SEd Tanous             {"Password", nullptr},
40884e12cb7SAppaRao Puli             {"RoleId", "Administrator"}};
4090f74e643SEd Tanous 
410b9b2e0b2SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
411b9b2e0b2SEd Tanous 
412b9b2e0b2SEd Tanous         if (params.size() != 1)
413b9b2e0b2SEd Tanous         {
414f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
415b9b2e0b2SEd Tanous             return;
416b9b2e0b2SEd Tanous         }
417b9b2e0b2SEd Tanous 
418b9b2e0b2SEd Tanous         crow::connections::systemBus->async_method_call(
419b9b2e0b2SEd Tanous             [asyncResp, accountName{std::string(params[0])}](
420b9b2e0b2SEd Tanous                 const boost::system::error_code ec,
421b9b2e0b2SEd Tanous                 const ManagedObjectType& users) {
422b9b2e0b2SEd Tanous                 if (ec)
423b9b2e0b2SEd Tanous                 {
424f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
425b9b2e0b2SEd Tanous                     return;
426b9b2e0b2SEd Tanous                 }
42784e12cb7SAppaRao Puli                 auto userIt = users.begin();
428b9b2e0b2SEd Tanous 
42984e12cb7SAppaRao Puli                 for (; userIt != users.end(); userIt++)
430b9b2e0b2SEd Tanous                 {
43184e12cb7SAppaRao Puli                     if (boost::ends_with(userIt->first.str, "/" + accountName))
432b9b2e0b2SEd Tanous                     {
43384e12cb7SAppaRao Puli                         break;
434b9b2e0b2SEd Tanous                     }
435b9b2e0b2SEd Tanous                 }
43684e12cb7SAppaRao Puli                 if (userIt == users.end())
437b9b2e0b2SEd Tanous                 {
43884e12cb7SAppaRao Puli                     messages::resourceNotFound(asyncResp->res, "ManagerAccount",
43984e12cb7SAppaRao Puli                                                accountName);
44084e12cb7SAppaRao Puli                     return;
44184e12cb7SAppaRao Puli                 }
44284e12cb7SAppaRao Puli                 for (const auto& interface : userIt->second)
44365b0dc32SEd Tanous                 {
44465b0dc32SEd Tanous                     if (interface.first ==
44565b0dc32SEd Tanous                         "xyz.openbmc_project.User.Attributes")
44665b0dc32SEd Tanous                     {
44765b0dc32SEd Tanous                         for (const auto& property : interface.second)
44865b0dc32SEd Tanous                         {
44965b0dc32SEd Tanous                             if (property.first == "UserEnabled")
45065b0dc32SEd Tanous                             {
45165b0dc32SEd Tanous                                 const bool* userEnabled =
452abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
45365b0dc32SEd Tanous                                 if (userEnabled == nullptr)
45465b0dc32SEd Tanous                                 {
45565b0dc32SEd Tanous                                     BMCWEB_LOG_ERROR
45665b0dc32SEd Tanous                                         << "UserEnabled wasn't a bool";
45784e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
45884e12cb7SAppaRao Puli                                     return;
45965b0dc32SEd Tanous                                 }
46065b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Enabled"] =
46165b0dc32SEd Tanous                                     *userEnabled;
46265b0dc32SEd Tanous                             }
46365b0dc32SEd Tanous                             else if (property.first ==
46465b0dc32SEd Tanous                                      "UserLockedForFailedAttempt")
46565b0dc32SEd Tanous                             {
46665b0dc32SEd Tanous                                 const bool* userLocked =
467abf2add6SEd Tanous                                     std::get_if<bool>(&property.second);
46865b0dc32SEd Tanous                                 if (userLocked == nullptr)
46965b0dc32SEd Tanous                                 {
47084e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "UserLockedForF"
47184e12cb7SAppaRao Puli                                                         "ailedAttempt "
47284e12cb7SAppaRao Puli                                                         "wasn't a bool";
47384e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
47484e12cb7SAppaRao Puli                                     return;
47565b0dc32SEd Tanous                                 }
47665b0dc32SEd Tanous                                 asyncResp->res.jsonValue["Locked"] =
47765b0dc32SEd Tanous                                     *userLocked;
47824c8542dSRatan Gupta                                 asyncResp->res.jsonValue
47924c8542dSRatan Gupta                                     ["Locked@Redfish.AllowableValues"] = {
48024c8542dSRatan Gupta                                     false};
48165b0dc32SEd Tanous                             }
48284e12cb7SAppaRao Puli                             else if (property.first == "UserPrivilege")
48384e12cb7SAppaRao Puli                             {
48484e12cb7SAppaRao Puli                                 const std::string* userRolePtr =
485abf2add6SEd Tanous                                     std::get_if<std::string>(&property.second);
48684e12cb7SAppaRao Puli                                 if (userRolePtr == nullptr)
48784e12cb7SAppaRao Puli                                 {
48884e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR
48984e12cb7SAppaRao Puli                                         << "UserPrivilege wasn't a "
49084e12cb7SAppaRao Puli                                            "string";
49184e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
49284e12cb7SAppaRao Puli                                     return;
49384e12cb7SAppaRao Puli                                 }
49484e12cb7SAppaRao Puli                                 std::string priv =
49584e12cb7SAppaRao Puli                                     getPrivilegeFromRoleId(*userRolePtr);
49684e12cb7SAppaRao Puli                                 if (priv.empty())
49784e12cb7SAppaRao Puli                                 {
49884e12cb7SAppaRao Puli                                     BMCWEB_LOG_ERROR << "Invalid user role";
49984e12cb7SAppaRao Puli                                     messages::internalError(asyncResp->res);
50084e12cb7SAppaRao Puli                                     return;
50184e12cb7SAppaRao Puli                                 }
50284e12cb7SAppaRao Puli                                 asyncResp->res.jsonValue["RoleId"] = priv;
50384e12cb7SAppaRao Puli 
50484e12cb7SAppaRao Puli                                 asyncResp->res.jsonValue["Links"]["Role"] = {
50584e12cb7SAppaRao Puli                                     {"@odata.id", "/redfish/v1/AccountService/"
50684e12cb7SAppaRao Puli                                                   "Roles/" +
50784e12cb7SAppaRao Puli                                                       priv}};
50884e12cb7SAppaRao Puli                             }
50965b0dc32SEd Tanous                         }
51065b0dc32SEd Tanous                     }
51165b0dc32SEd Tanous                 }
51265b0dc32SEd Tanous 
513b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
51484e12cb7SAppaRao Puli                     "/redfish/v1/AccountService/Accounts/" + accountName;
515b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["Id"] = accountName;
516b9b2e0b2SEd Tanous                 asyncResp->res.jsonValue["UserName"] = accountName;
517b9b2e0b2SEd Tanous             },
518b9b2e0b2SEd Tanous             "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
519b9b2e0b2SEd Tanous             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
520b9b2e0b2SEd Tanous     }
521a840879dSEd Tanous 
522a840879dSEd Tanous     void doPatch(crow::Response& res, const crow::Request& req,
523a840879dSEd Tanous                  const std::vector<std::string>& params) override
524a840879dSEd Tanous     {
525a840879dSEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
526a840879dSEd Tanous         if (params.size() != 1)
527a840879dSEd Tanous         {
528f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
529a840879dSEd Tanous             return;
530a840879dSEd Tanous         }
531a840879dSEd Tanous 
532a24526dcSEd Tanous         std::optional<std::string> newUserName;
533a24526dcSEd Tanous         std::optional<std::string> password;
534a24526dcSEd Tanous         std::optional<bool> enabled;
535a24526dcSEd Tanous         std::optional<std::string> roleId;
53624c8542dSRatan Gupta         std::optional<bool> locked;
53784e12cb7SAppaRao Puli         if (!json_util::readJson(req, res, "UserName", newUserName, "Password",
53824c8542dSRatan Gupta                                  password, "RoleId", roleId, "Enabled", enabled,
53924c8542dSRatan Gupta                                  "Locked", locked))
540a840879dSEd Tanous         {
541a840879dSEd Tanous             return;
542a840879dSEd Tanous         }
543a840879dSEd Tanous 
54484e12cb7SAppaRao Puli         const std::string& username = params[0];
54584e12cb7SAppaRao Puli 
54684e12cb7SAppaRao Puli         if (!newUserName)
547a840879dSEd Tanous         {
54884e12cb7SAppaRao Puli             // If the username isn't being updated, we can update the properties
54984e12cb7SAppaRao Puli             // directly
55024c8542dSRatan Gupta             updateUserProperties(asyncResp, username, password, enabled, roleId,
55124c8542dSRatan Gupta                                  locked);
55284e12cb7SAppaRao Puli             return;
55384e12cb7SAppaRao Puli         }
55484e12cb7SAppaRao Puli         else
55584e12cb7SAppaRao Puli         {
55684e12cb7SAppaRao Puli             crow::connections::systemBus->async_method_call(
55784e12cb7SAppaRao Puli                 [this, asyncResp, username, password(std::move(password)),
55884e12cb7SAppaRao Puli                  roleId(std::move(roleId)), enabled(std::move(enabled)),
55924c8542dSRatan Gupta                  newUser{std::string(*newUserName)}, locked(std::move(locked))](
56084e12cb7SAppaRao Puli                     const boost::system::error_code ec) {
56184e12cb7SAppaRao Puli                     if (ec)
56284e12cb7SAppaRao Puli                     {
56384e12cb7SAppaRao Puli                         BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
564a840879dSEd Tanous                         messages::resourceNotFound(
56584e12cb7SAppaRao Puli                             asyncResp->res,
56684e12cb7SAppaRao Puli                             "#ManagerAccount.v1_0_3.ManagerAccount", username);
567a840879dSEd Tanous                         return;
568a840879dSEd Tanous                     }
569a840879dSEd Tanous 
57084e12cb7SAppaRao Puli                     updateUserProperties(asyncResp, newUser, password, enabled,
57124c8542dSRatan Gupta                                          roleId, locked);
57284e12cb7SAppaRao Puli                 },
57384e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
57484e12cb7SAppaRao Puli                 "xyz.openbmc_project.User.Manager", "RenameUser", username,
57584e12cb7SAppaRao Puli                 *newUserName);
57684e12cb7SAppaRao Puli         }
57784e12cb7SAppaRao Puli     }
57884e12cb7SAppaRao Puli 
57984e12cb7SAppaRao Puli     void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp,
58084e12cb7SAppaRao Puli                               const std::string& username,
581a24526dcSEd Tanous                               std::optional<std::string> password,
582a24526dcSEd Tanous                               std::optional<bool> enabled,
58324c8542dSRatan Gupta                               std::optional<std::string> roleId,
58424c8542dSRatan Gupta                               std::optional<bool> locked)
58584e12cb7SAppaRao Puli     {
5869712f8acSEd Tanous         if (password)
587a840879dSEd Tanous         {
5889712f8acSEd Tanous             if (!pamUpdatePassword(username, *password))
589a840879dSEd Tanous             {
590a840879dSEd Tanous                 BMCWEB_LOG_ERROR << "pamUpdatePassword Failed";
591f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
592a840879dSEd Tanous                 return;
593a840879dSEd Tanous             }
594a840879dSEd Tanous         }
595a840879dSEd Tanous 
59624c8542dSRatan Gupta         std::string dbusObjectPath = "/xyz/openbmc_project/user/" + username;
59724c8542dSRatan Gupta         dbus::utility::escapePathForDbus(dbusObjectPath);
59824c8542dSRatan Gupta 
59924c8542dSRatan Gupta         checkDbusPathExists(
60024c8542dSRatan Gupta             dbusObjectPath,
60124c8542dSRatan Gupta             [dbusObjectPath(std::move(dbusObjectPath)), username,
60224c8542dSRatan Gupta              password(std::move(password)), roleId(std::move(roleId)),
60324c8542dSRatan Gupta              enabled(std::move(enabled)), locked(std::move(locked)),
60424c8542dSRatan Gupta              asyncResp{std::move(asyncResp)}](int rc) {
60524c8542dSRatan Gupta                 if (!rc)
60624c8542dSRatan Gupta                 {
60724c8542dSRatan Gupta                     messages::invalidObject(asyncResp->res, username.c_str());
60824c8542dSRatan Gupta                     return;
60924c8542dSRatan Gupta                 }
6109712f8acSEd Tanous                 if (enabled)
611a840879dSEd Tanous                 {
612a840879dSEd Tanous                     crow::connections::systemBus->async_method_call(
613a840879dSEd Tanous                         [asyncResp](const boost::system::error_code ec) {
614a840879dSEd Tanous                             if (ec)
615a840879dSEd Tanous                             {
61624c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
61724c8542dSRatan Gupta                                                  << ec;
618f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
619a840879dSEd Tanous                                 return;
620a840879dSEd Tanous                             }
62184e12cb7SAppaRao Puli                             messages::success(asyncResp->res);
62284e12cb7SAppaRao Puli                             return;
62384e12cb7SAppaRao Puli                         },
62484e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Manager",
62524c8542dSRatan Gupta                         dbusObjectPath.c_str(),
62684e12cb7SAppaRao Puli                         "org.freedesktop.DBus.Properties", "Set",
62784e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserEnabled",
628abf2add6SEd Tanous                         std::variant<bool>{*enabled});
62984e12cb7SAppaRao Puli                 }
63084e12cb7SAppaRao Puli 
63184e12cb7SAppaRao Puli                 if (roleId)
63284e12cb7SAppaRao Puli                 {
63384e12cb7SAppaRao Puli                     std::string priv = getRoleIdFromPrivilege(*roleId);
63484e12cb7SAppaRao Puli                     if (priv.empty())
63584e12cb7SAppaRao Puli                     {
63624c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res,
63724c8542dSRatan Gupta                                                          *roleId, "RoleId");
63884e12cb7SAppaRao Puli                         return;
63984e12cb7SAppaRao Puli                     }
64084e12cb7SAppaRao Puli 
64184e12cb7SAppaRao Puli                     crow::connections::systemBus->async_method_call(
64284e12cb7SAppaRao Puli                         [asyncResp](const boost::system::error_code ec) {
64384e12cb7SAppaRao Puli                             if (ec)
64484e12cb7SAppaRao Puli                             {
64524c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
64624c8542dSRatan Gupta                                                  << ec;
64784e12cb7SAppaRao Puli                                 messages::internalError(asyncResp->res);
64884e12cb7SAppaRao Puli                                 return;
64984e12cb7SAppaRao Puli                             }
650f12894f8SJason M. Bills                             messages::success(asyncResp->res);
651a840879dSEd Tanous                         },
652a840879dSEd Tanous                         "xyz.openbmc_project.User.Manager",
65324c8542dSRatan Gupta                         dbusObjectPath.c_str(),
654a840879dSEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
65584e12cb7SAppaRao Puli                         "xyz.openbmc_project.User.Attributes", "UserPrivilege",
656abf2add6SEd Tanous                         std::variant<std::string>{priv});
657a840879dSEd Tanous                 }
65824c8542dSRatan Gupta 
65924c8542dSRatan Gupta                 if (locked)
66024c8542dSRatan Gupta                 {
66124c8542dSRatan Gupta                     // admin can unlock the account which is locked by
66224c8542dSRatan Gupta                     // successive authentication failures but admin should not
66324c8542dSRatan Gupta                     // be allowed to lock an account.
66424c8542dSRatan Gupta                     if (*locked)
66524c8542dSRatan Gupta                     {
66624c8542dSRatan Gupta                         messages::propertyValueNotInList(asyncResp->res, "true",
66724c8542dSRatan Gupta                                                          "Locked");
66824c8542dSRatan Gupta                         return;
66924c8542dSRatan Gupta                     }
67024c8542dSRatan Gupta 
67124c8542dSRatan Gupta                     crow::connections::systemBus->async_method_call(
67224c8542dSRatan Gupta                         [asyncResp](const boost::system::error_code ec) {
67324c8542dSRatan Gupta                             if (ec)
67424c8542dSRatan Gupta                             {
67524c8542dSRatan Gupta                                 BMCWEB_LOG_ERROR << "D-Bus responses error: "
67624c8542dSRatan Gupta                                                  << ec;
67724c8542dSRatan Gupta                                 messages::internalError(asyncResp->res);
67824c8542dSRatan Gupta                                 return;
67924c8542dSRatan Gupta                             }
68024c8542dSRatan Gupta                             messages::success(asyncResp->res);
68124c8542dSRatan Gupta                             return;
68224c8542dSRatan Gupta                         },
68324c8542dSRatan Gupta                         "xyz.openbmc_project.User.Manager",
68424c8542dSRatan Gupta                         dbusObjectPath.c_str(),
68524c8542dSRatan Gupta                         "org.freedesktop.DBus.Properties", "Set",
68624c8542dSRatan Gupta                         "xyz.openbmc_project.User.Attributes",
68724c8542dSRatan Gupta                         "UserLockedForFailedAttempt",
68824c8542dSRatan Gupta                         sdbusplus::message::variant<bool>{*locked});
68924c8542dSRatan Gupta                 }
69024c8542dSRatan Gupta             });
691a840879dSEd Tanous     }
69206e086d9SEd Tanous 
69306e086d9SEd Tanous     void doDelete(crow::Response& res, const crow::Request& req,
69406e086d9SEd Tanous                   const std::vector<std::string>& params) override
69506e086d9SEd Tanous     {
69606e086d9SEd Tanous         auto asyncResp = std::make_shared<AsyncResp>(res);
69706e086d9SEd Tanous 
69806e086d9SEd Tanous         if (params.size() != 1)
69906e086d9SEd Tanous         {
700f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
70106e086d9SEd Tanous             return;
70206e086d9SEd Tanous         }
70306e086d9SEd Tanous 
70406e086d9SEd Tanous         const std::string userPath = "/xyz/openbmc_project/user/" + params[0];
70506e086d9SEd Tanous 
70606e086d9SEd Tanous         crow::connections::systemBus->async_method_call(
70706e086d9SEd Tanous             [asyncResp, username{std::move(params[0])}](
70806e086d9SEd Tanous                 const boost::system::error_code ec) {
70906e086d9SEd Tanous                 if (ec)
71006e086d9SEd Tanous                 {
71106e086d9SEd Tanous                     messages::resourceNotFound(
712f12894f8SJason M. Bills                         asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount",
713f12894f8SJason M. Bills                         username);
71406e086d9SEd Tanous                     return;
71506e086d9SEd Tanous                 }
71606e086d9SEd Tanous 
717f12894f8SJason M. Bills                 messages::accountRemoved(asyncResp->res);
71806e086d9SEd Tanous             },
71906e086d9SEd Tanous             "xyz.openbmc_project.User.Manager", userPath,
72006e086d9SEd Tanous             "xyz.openbmc_project.Object.Delete", "Delete");
72106e086d9SEd Tanous     }
72284e12cb7SAppaRao Puli };
72388d16c9aSLewanczyk, Dawid 
72488d16c9aSLewanczyk, Dawid } // namespace redfish
725