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 1965b0dc32SEd Tanous #include <error_messages.hpp> 20b9b2e0b2SEd Tanous #include <openbmc_dbus_rest.hpp> 21a840879dSEd Tanous #include <utils/json_utils.hpp> 22*abf2add6SEd Tanous #include <variant> 23b9b2e0b2SEd Tanous 241abe55efSEd Tanous namespace redfish 251abe55efSEd Tanous { 2688d16c9aSLewanczyk, Dawid 27b9b2e0b2SEd Tanous using ManagedObjectType = std::vector<std::pair< 28b9b2e0b2SEd Tanous sdbusplus::message::object_path, 29b9b2e0b2SEd Tanous boost::container::flat_map< 30*abf2add6SEd Tanous std::string, boost::container::flat_map< 31*abf2add6SEd Tanous std::string, std::variant<bool, std::string>>>>>; 3284e12cb7SAppaRao Puli 3384e12cb7SAppaRao Puli inline std::string getPrivilegeFromRoleId(boost::beast::string_view role) 3484e12cb7SAppaRao Puli { 3584e12cb7SAppaRao Puli if (role == "priv-admin") 3684e12cb7SAppaRao Puli { 3784e12cb7SAppaRao Puli return "Administrator"; 3884e12cb7SAppaRao Puli } 3984e12cb7SAppaRao Puli else if (role == "priv-callback") 4084e12cb7SAppaRao Puli { 4184e12cb7SAppaRao Puli return "Callback"; 4284e12cb7SAppaRao Puli } 4384e12cb7SAppaRao Puli else if (role == "priv-user") 4484e12cb7SAppaRao Puli { 4584e12cb7SAppaRao Puli return "User"; 4684e12cb7SAppaRao Puli } 4784e12cb7SAppaRao Puli else if (role == "priv-operator") 4884e12cb7SAppaRao Puli { 4984e12cb7SAppaRao Puli return "Operator"; 5084e12cb7SAppaRao Puli } 5184e12cb7SAppaRao Puli return ""; 5284e12cb7SAppaRao Puli } 5384e12cb7SAppaRao Puli inline std::string getRoleIdFromPrivilege(boost::beast::string_view role) 5484e12cb7SAppaRao Puli { 5584e12cb7SAppaRao Puli if (role == "Administrator") 5684e12cb7SAppaRao Puli { 5784e12cb7SAppaRao Puli return "priv-admin"; 5884e12cb7SAppaRao Puli } 5984e12cb7SAppaRao Puli else if (role == "Callback") 6084e12cb7SAppaRao Puli { 6184e12cb7SAppaRao Puli return "priv-callback"; 6284e12cb7SAppaRao Puli } 6384e12cb7SAppaRao Puli else if (role == "User") 6484e12cb7SAppaRao Puli { 6584e12cb7SAppaRao Puli return "priv-user"; 6684e12cb7SAppaRao Puli } 6784e12cb7SAppaRao Puli else if (role == "Operator") 6884e12cb7SAppaRao Puli { 6984e12cb7SAppaRao Puli return "priv-operator"; 7084e12cb7SAppaRao Puli } 7184e12cb7SAppaRao Puli return ""; 7284e12cb7SAppaRao Puli } 73b9b2e0b2SEd Tanous 741abe55efSEd Tanous class AccountService : public Node 751abe55efSEd Tanous { 7688d16c9aSLewanczyk, Dawid public: 771abe55efSEd Tanous AccountService(CrowApp& app) : Node(app, "/redfish/v1/AccountService/") 781abe55efSEd Tanous { 793ebd75f7SEd Tanous entityPrivileges = { 804b1b8683SBorawski.Lukasz {boost::beast::http::verb::get, 814b1b8683SBorawski.Lukasz {{"ConfigureUsers"}, {"ConfigureManager"}}}, 82e0d918bcSEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 83e0d918bcSEd Tanous {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, 84e0d918bcSEd Tanous {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, 85e0d918bcSEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, 86e0d918bcSEd Tanous {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; 8788d16c9aSLewanczyk, Dawid } 8888d16c9aSLewanczyk, Dawid 8988d16c9aSLewanczyk, Dawid private: 9055c7b7a2SEd Tanous void doGet(crow::Response& res, const crow::Request& req, 911abe55efSEd Tanous const std::vector<std::string>& params) override 921abe55efSEd Tanous { 933d958bbcSAppaRao Puli auto asyncResp = std::make_shared<AsyncResp>(res); 943d958bbcSAppaRao Puli res.jsonValue = { 953d958bbcSAppaRao Puli {"@odata.context", "/redfish/v1/" 963d958bbcSAppaRao Puli "$metadata#AccountService.AccountService"}, 973d958bbcSAppaRao Puli {"@odata.id", "/redfish/v1/AccountService"}, 983d958bbcSAppaRao Puli {"@odata.type", "#AccountService." 993d958bbcSAppaRao Puli "v1_1_0.AccountService"}, 1003d958bbcSAppaRao Puli {"Id", "AccountService"}, 1013d958bbcSAppaRao Puli {"Name", "Account Service"}, 1023d958bbcSAppaRao Puli {"Description", "Account Service"}, 1033d958bbcSAppaRao Puli {"ServiceEnabled", true}, 1043d958bbcSAppaRao Puli {"MaxPasswordLength", 31}, 1053d958bbcSAppaRao Puli {"Accounts", 1063d958bbcSAppaRao Puli {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}}, 1073d958bbcSAppaRao Puli {"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}}}; 1080f74e643SEd Tanous 1093d958bbcSAppaRao Puli crow::connections::systemBus->async_method_call( 1103d958bbcSAppaRao Puli [asyncResp]( 1113d958bbcSAppaRao Puli const boost::system::error_code ec, 1123d958bbcSAppaRao Puli const std::vector<std::pair< 113*abf2add6SEd Tanous std::string, std::variant<uint32_t, uint16_t, uint8_t>>>& 1143d958bbcSAppaRao Puli propertiesList) { 1153d958bbcSAppaRao Puli if (ec) 1163d958bbcSAppaRao Puli { 1173d958bbcSAppaRao Puli messages::internalError(asyncResp->res); 1183d958bbcSAppaRao Puli return; 1193d958bbcSAppaRao Puli } 1203d958bbcSAppaRao Puli BMCWEB_LOG_DEBUG << "Got " << propertiesList.size() 1213d958bbcSAppaRao Puli << "properties for AccountService"; 1223d958bbcSAppaRao Puli for (const std::pair<std::string, 123*abf2add6SEd Tanous std::variant<uint32_t, uint16_t, uint8_t>>& 1243d958bbcSAppaRao Puli property : propertiesList) 1253d958bbcSAppaRao Puli { 1263d958bbcSAppaRao Puli if (property.first == "MinPasswordLength") 1273d958bbcSAppaRao Puli { 1283d958bbcSAppaRao Puli const uint8_t* value = 129*abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 1303d958bbcSAppaRao Puli if (value != nullptr) 1313d958bbcSAppaRao Puli { 1323d958bbcSAppaRao Puli asyncResp->res.jsonValue["MinPasswordLength"] = 1333d958bbcSAppaRao Puli *value; 1343d958bbcSAppaRao Puli } 1353d958bbcSAppaRao Puli } 1363d958bbcSAppaRao Puli if (property.first == "AccountUnlockTimeout") 1373d958bbcSAppaRao Puli { 1383d958bbcSAppaRao Puli const uint32_t* value = 139*abf2add6SEd Tanous std::get_if<uint32_t>(&property.second); 1403d958bbcSAppaRao Puli if (value != nullptr) 1413d958bbcSAppaRao Puli { 1423d958bbcSAppaRao Puli asyncResp->res.jsonValue["AccountLockoutDuration"] = 1433d958bbcSAppaRao Puli *value; 1443d958bbcSAppaRao Puli } 1453d958bbcSAppaRao Puli } 1463d958bbcSAppaRao Puli if (property.first == "MaxLoginAttemptBeforeLockout") 1473d958bbcSAppaRao Puli { 1483d958bbcSAppaRao Puli const uint16_t* value = 149*abf2add6SEd Tanous std::get_if<uint16_t>(&property.second); 1503d958bbcSAppaRao Puli if (value != nullptr) 1513d958bbcSAppaRao Puli { 1523d958bbcSAppaRao Puli asyncResp->res 1533d958bbcSAppaRao Puli .jsonValue["AccountLockoutThreshold"] = *value; 1543d958bbcSAppaRao Puli } 1553d958bbcSAppaRao Puli } 1563d958bbcSAppaRao Puli } 1573d958bbcSAppaRao Puli }, 1583d958bbcSAppaRao Puli "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1593d958bbcSAppaRao Puli "org.freedesktop.DBus.Properties", "GetAll", 1603d958bbcSAppaRao Puli "xyz.openbmc_project.User.AccountPolicy"); 1613d958bbcSAppaRao Puli } 1623d958bbcSAppaRao Puli void doPatch(crow::Response& res, const crow::Request& req, 1633d958bbcSAppaRao Puli const std::vector<std::string>& params) override 1643d958bbcSAppaRao Puli { 1653d958bbcSAppaRao Puli auto asyncResp = std::make_shared<AsyncResp>(res); 1663d958bbcSAppaRao Puli 1673d958bbcSAppaRao Puli std::optional<uint32_t> unlockTimeout; 1683d958bbcSAppaRao Puli std::optional<uint16_t> lockoutThreshold; 1693d958bbcSAppaRao Puli if (!json_util::readJson(req, res, "AccountLockoutDuration", 1703d958bbcSAppaRao Puli unlockTimeout, "AccountLockoutThreshold", 1713d958bbcSAppaRao Puli lockoutThreshold)) 1723d958bbcSAppaRao Puli { 1733d958bbcSAppaRao Puli return; 1743d958bbcSAppaRao Puli } 1753d958bbcSAppaRao Puli if (unlockTimeout) 1763d958bbcSAppaRao Puli { 1773d958bbcSAppaRao Puli crow::connections::systemBus->async_method_call( 1783d958bbcSAppaRao Puli [asyncResp](const boost::system::error_code ec) { 1793d958bbcSAppaRao Puli if (ec) 1803d958bbcSAppaRao Puli { 1813d958bbcSAppaRao Puli messages::internalError(asyncResp->res); 1823d958bbcSAppaRao Puli return; 1833d958bbcSAppaRao Puli } 1843d958bbcSAppaRao Puli }, 1853d958bbcSAppaRao Puli "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 1863d958bbcSAppaRao Puli "org.freedesktop.DBus.Properties", "Set", 1873d958bbcSAppaRao Puli "xyz.openbmc_project.User.AccountPolicy", 188*abf2add6SEd Tanous "AccountUnlockTimeout", std::variant<uint32_t>(*unlockTimeout)); 1893d958bbcSAppaRao Puli } 1903d958bbcSAppaRao Puli if (lockoutThreshold) 1913d958bbcSAppaRao Puli { 1923d958bbcSAppaRao Puli crow::connections::systemBus->async_method_call( 1933d958bbcSAppaRao Puli [asyncResp](const boost::system::error_code ec) { 1943d958bbcSAppaRao Puli if (ec) 1953d958bbcSAppaRao Puli { 1963d958bbcSAppaRao Puli messages::internalError(asyncResp->res); 1973d958bbcSAppaRao Puli return; 1983d958bbcSAppaRao Puli } 1993d958bbcSAppaRao Puli }, 2003d958bbcSAppaRao Puli "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 2013d958bbcSAppaRao Puli "org.freedesktop.DBus.Properties", "Set", 2023d958bbcSAppaRao Puli "xyz.openbmc_project.User.AccountPolicy", 2033d958bbcSAppaRao Puli "MaxLoginAttemptBeforeLockout", 204*abf2add6SEd Tanous std::variant<uint16_t>(*lockoutThreshold)); 2053d958bbcSAppaRao Puli } 20688d16c9aSLewanczyk, Dawid } 20788d16c9aSLewanczyk, Dawid }; 208b9b2e0b2SEd Tanous class AccountsCollection : public Node 209b9b2e0b2SEd Tanous { 210b9b2e0b2SEd Tanous public: 211b9b2e0b2SEd Tanous AccountsCollection(CrowApp& app) : 212b9b2e0b2SEd Tanous Node(app, "/redfish/v1/AccountService/Accounts/") 213b9b2e0b2SEd Tanous { 214b9b2e0b2SEd Tanous entityPrivileges = { 215b9b2e0b2SEd Tanous {boost::beast::http::verb::get, 216b9b2e0b2SEd Tanous {{"ConfigureUsers"}, {"ConfigureManager"}}}, 217b9b2e0b2SEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 218b9b2e0b2SEd Tanous {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, 219b9b2e0b2SEd Tanous {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, 220b9b2e0b2SEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, 221b9b2e0b2SEd Tanous {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; 222b9b2e0b2SEd Tanous } 223b9b2e0b2SEd Tanous 224b9b2e0b2SEd Tanous private: 225b9b2e0b2SEd Tanous void doGet(crow::Response& res, const crow::Request& req, 226b9b2e0b2SEd Tanous const std::vector<std::string>& params) override 227b9b2e0b2SEd Tanous { 228b9b2e0b2SEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 2290f74e643SEd Tanous res.jsonValue = {{"@odata.context", 2300f74e643SEd Tanous "/redfish/v1/" 2310f74e643SEd Tanous "$metadata#ManagerAccountCollection." 2320f74e643SEd Tanous "ManagerAccountCollection"}, 2330f74e643SEd Tanous {"@odata.id", "/redfish/v1/AccountService/Accounts"}, 2340f74e643SEd Tanous {"@odata.type", "#ManagerAccountCollection." 2350f74e643SEd Tanous "ManagerAccountCollection"}, 2360f74e643SEd Tanous {"Name", "Accounts Collection"}, 2370f74e643SEd Tanous {"Description", "BMC User Accounts"}}; 2380f74e643SEd Tanous 239b9b2e0b2SEd Tanous crow::connections::systemBus->async_method_call( 240b9b2e0b2SEd Tanous [asyncResp](const boost::system::error_code ec, 241b9b2e0b2SEd Tanous const ManagedObjectType& users) { 242b9b2e0b2SEd Tanous if (ec) 243b9b2e0b2SEd Tanous { 244f12894f8SJason M. Bills messages::internalError(asyncResp->res); 245b9b2e0b2SEd Tanous return; 246b9b2e0b2SEd Tanous } 247b9b2e0b2SEd Tanous 248b9b2e0b2SEd Tanous nlohmann::json& memberArray = 249b9b2e0b2SEd Tanous asyncResp->res.jsonValue["Members"]; 250b9b2e0b2SEd Tanous memberArray = nlohmann::json::array(); 251b9b2e0b2SEd Tanous 252b9b2e0b2SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = users.size(); 253b9b2e0b2SEd Tanous for (auto& user : users) 254b9b2e0b2SEd Tanous { 255b9b2e0b2SEd Tanous const std::string& path = 256b9b2e0b2SEd Tanous static_cast<const std::string&>(user.first); 257b9b2e0b2SEd Tanous std::size_t lastIndex = path.rfind("/"); 258b9b2e0b2SEd Tanous if (lastIndex == std::string::npos) 259b9b2e0b2SEd Tanous { 260b9b2e0b2SEd Tanous lastIndex = 0; 261b9b2e0b2SEd Tanous } 262b9b2e0b2SEd Tanous else 263b9b2e0b2SEd Tanous { 264b9b2e0b2SEd Tanous lastIndex += 1; 265b9b2e0b2SEd Tanous } 266b9b2e0b2SEd Tanous memberArray.push_back( 267b9b2e0b2SEd Tanous {{"@odata.id", "/redfish/v1/AccountService/Accounts/" + 268b9b2e0b2SEd Tanous path.substr(lastIndex)}}); 269b9b2e0b2SEd Tanous } 270b9b2e0b2SEd Tanous }, 271b9b2e0b2SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 272b9b2e0b2SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 273b9b2e0b2SEd Tanous } 27404ae99ecSEd Tanous void doPost(crow::Response& res, const crow::Request& req, 27504ae99ecSEd Tanous const std::vector<std::string>& params) override 27604ae99ecSEd Tanous { 27704ae99ecSEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 27804ae99ecSEd Tanous 2799712f8acSEd Tanous std::string username; 2809712f8acSEd Tanous std::string password; 281a24526dcSEd Tanous std::optional<std::string> roleId("User"); 282a24526dcSEd Tanous std::optional<bool> enabled = true; 2839712f8acSEd Tanous if (!json_util::readJson(req, res, "UserName", username, "Password", 2849712f8acSEd Tanous password, "RoleId", roleId, "Enabled", 2859712f8acSEd Tanous enabled)) 28604ae99ecSEd Tanous { 28704ae99ecSEd Tanous return; 28804ae99ecSEd Tanous } 28904ae99ecSEd Tanous 29084e12cb7SAppaRao Puli std::string priv = getRoleIdFromPrivilege(*roleId); 29184e12cb7SAppaRao Puli if (priv.empty()) 29204ae99ecSEd Tanous { 293f12894f8SJason M. Bills messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId"); 29404ae99ecSEd Tanous return; 29504ae99ecSEd Tanous } 2969712f8acSEd Tanous roleId = priv; 29704ae99ecSEd Tanous 29804ae99ecSEd Tanous crow::connections::systemBus->async_method_call( 2999712f8acSEd Tanous [asyncResp, username, password{std::move(password)}]( 30004ae99ecSEd Tanous const boost::system::error_code ec) { 30104ae99ecSEd Tanous if (ec) 30204ae99ecSEd Tanous { 30304ae99ecSEd Tanous messages::resourceAlreadyExists( 304f12894f8SJason M. Bills asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", 305f12894f8SJason M. Bills "UserName", username); 30604ae99ecSEd Tanous return; 30704ae99ecSEd Tanous } 30804ae99ecSEd Tanous 30904ae99ecSEd Tanous if (!pamUpdatePassword(username, password)) 31004ae99ecSEd Tanous { 31104ae99ecSEd Tanous // At this point we have a user that's been created, but the 31204ae99ecSEd Tanous // password set failed. Something is wrong, so delete the 31304ae99ecSEd Tanous // user that we've already created 31404ae99ecSEd Tanous crow::connections::systemBus->async_method_call( 31504ae99ecSEd Tanous [asyncResp](const boost::system::error_code ec) { 31604ae99ecSEd Tanous if (ec) 31704ae99ecSEd Tanous { 318f12894f8SJason M. Bills messages::internalError(asyncResp->res); 31904ae99ecSEd Tanous return; 32004ae99ecSEd Tanous } 32104ae99ecSEd Tanous 322f12894f8SJason M. Bills messages::invalidObject(asyncResp->res, "Password"); 32304ae99ecSEd Tanous }, 32404ae99ecSEd Tanous "xyz.openbmc_project.User.Manager", 32504ae99ecSEd Tanous "/xyz/openbmc_project/user/" + username, 32604ae99ecSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 32704ae99ecSEd Tanous 32804ae99ecSEd Tanous BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; 32904ae99ecSEd Tanous return; 33004ae99ecSEd Tanous } 33104ae99ecSEd Tanous 332f12894f8SJason M. Bills messages::created(asyncResp->res); 33304ae99ecSEd Tanous asyncResp->res.addHeader( 33404ae99ecSEd Tanous "Location", 33504ae99ecSEd Tanous "/redfish/v1/AccountService/Accounts/" + username); 33604ae99ecSEd Tanous }, 33704ae99ecSEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 3389712f8acSEd Tanous "xyz.openbmc_project.User.Manager", "CreateUser", username, 33904ae99ecSEd Tanous std::array<const char*, 4>{"ipmi", "redfish", "ssh", "web"}, 3409712f8acSEd Tanous *roleId, *enabled); 34104ae99ecSEd Tanous } 342b9b2e0b2SEd Tanous }; 343b9b2e0b2SEd Tanous 344a840879dSEd Tanous template <typename Callback> 345a840879dSEd Tanous inline void checkDbusPathExists(const std::string& path, Callback&& callback) 346a840879dSEd Tanous { 347a840879dSEd Tanous using GetObjectType = 348a840879dSEd Tanous std::vector<std::pair<std::string, std::vector<std::string>>>; 349a840879dSEd Tanous 350a840879dSEd Tanous crow::connections::systemBus->async_method_call( 351a840879dSEd Tanous [callback{std::move(callback)}](const boost::system::error_code ec, 352a840879dSEd Tanous const GetObjectType& object_names) { 35384e12cb7SAppaRao Puli callback(!ec && object_names.size() != 0); 354a840879dSEd Tanous }, 355a840879dSEd Tanous "xyz.openbmc_project.ObjectMapper", 356a840879dSEd Tanous "/xyz/openbmc_project/object_mapper", 357a840879dSEd Tanous "xyz.openbmc_project.ObjectMapper", "GetObject", path, 358a840879dSEd Tanous std::array<std::string, 0>()); 359a840879dSEd Tanous } 360a840879dSEd Tanous 361b9b2e0b2SEd Tanous class ManagerAccount : public Node 362b9b2e0b2SEd Tanous { 363b9b2e0b2SEd Tanous public: 364b9b2e0b2SEd Tanous ManagerAccount(CrowApp& app) : 365b9b2e0b2SEd Tanous Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string()) 366b9b2e0b2SEd Tanous { 367b9b2e0b2SEd Tanous entityPrivileges = { 368b9b2e0b2SEd Tanous {boost::beast::http::verb::get, 369b9b2e0b2SEd Tanous {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}}, 370b9b2e0b2SEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 371b9b2e0b2SEd Tanous {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, 372b9b2e0b2SEd Tanous {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, 373b9b2e0b2SEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, 374b9b2e0b2SEd Tanous {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; 375b9b2e0b2SEd Tanous } 376b9b2e0b2SEd Tanous 377b9b2e0b2SEd Tanous private: 378b9b2e0b2SEd Tanous void doGet(crow::Response& res, const crow::Request& req, 379b9b2e0b2SEd Tanous const std::vector<std::string>& params) override 380b9b2e0b2SEd Tanous { 3810f74e643SEd Tanous res.jsonValue = { 3820f74e643SEd Tanous {"@odata.context", 3830f74e643SEd Tanous "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"}, 3840f74e643SEd Tanous {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"}, 3850f74e643SEd Tanous {"Name", "User Account"}, 3860f74e643SEd Tanous {"Description", "User Account"}, 3870f74e643SEd Tanous {"Password", nullptr}, 38884e12cb7SAppaRao Puli {"RoleId", "Administrator"}}; 3890f74e643SEd Tanous 390b9b2e0b2SEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 391b9b2e0b2SEd Tanous 392b9b2e0b2SEd Tanous if (params.size() != 1) 393b9b2e0b2SEd Tanous { 394f12894f8SJason M. Bills messages::internalError(asyncResp->res); 395b9b2e0b2SEd Tanous return; 396b9b2e0b2SEd Tanous } 397b9b2e0b2SEd Tanous 398b9b2e0b2SEd Tanous crow::connections::systemBus->async_method_call( 399b9b2e0b2SEd Tanous [asyncResp, accountName{std::string(params[0])}]( 400b9b2e0b2SEd Tanous const boost::system::error_code ec, 401b9b2e0b2SEd Tanous const ManagedObjectType& users) { 402b9b2e0b2SEd Tanous if (ec) 403b9b2e0b2SEd Tanous { 404f12894f8SJason M. Bills messages::internalError(asyncResp->res); 405b9b2e0b2SEd Tanous return; 406b9b2e0b2SEd Tanous } 40784e12cb7SAppaRao Puli auto userIt = users.begin(); 408b9b2e0b2SEd Tanous 40984e12cb7SAppaRao Puli for (; userIt != users.end(); userIt++) 410b9b2e0b2SEd Tanous { 41184e12cb7SAppaRao Puli if (boost::ends_with(userIt->first.str, "/" + accountName)) 412b9b2e0b2SEd Tanous { 41384e12cb7SAppaRao Puli break; 414b9b2e0b2SEd Tanous } 415b9b2e0b2SEd Tanous } 41684e12cb7SAppaRao Puli if (userIt == users.end()) 417b9b2e0b2SEd Tanous { 41884e12cb7SAppaRao Puli messages::resourceNotFound(asyncResp->res, "ManagerAccount", 41984e12cb7SAppaRao Puli accountName); 42084e12cb7SAppaRao Puli return; 42184e12cb7SAppaRao Puli } 42284e12cb7SAppaRao Puli for (const auto& interface : userIt->second) 42365b0dc32SEd Tanous { 42465b0dc32SEd Tanous if (interface.first == 42565b0dc32SEd Tanous "xyz.openbmc_project.User.Attributes") 42665b0dc32SEd Tanous { 42765b0dc32SEd Tanous for (const auto& property : interface.second) 42865b0dc32SEd Tanous { 42965b0dc32SEd Tanous if (property.first == "UserEnabled") 43065b0dc32SEd Tanous { 43165b0dc32SEd Tanous const bool* userEnabled = 432*abf2add6SEd Tanous std::get_if<bool>(&property.second); 43365b0dc32SEd Tanous if (userEnabled == nullptr) 43465b0dc32SEd Tanous { 43565b0dc32SEd Tanous BMCWEB_LOG_ERROR 43665b0dc32SEd Tanous << "UserEnabled wasn't a bool"; 43784e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 43884e12cb7SAppaRao Puli return; 43965b0dc32SEd Tanous } 44065b0dc32SEd Tanous asyncResp->res.jsonValue["Enabled"] = 44165b0dc32SEd Tanous *userEnabled; 44265b0dc32SEd Tanous } 44365b0dc32SEd Tanous else if (property.first == 44465b0dc32SEd Tanous "UserLockedForFailedAttempt") 44565b0dc32SEd Tanous { 44665b0dc32SEd Tanous const bool* userLocked = 447*abf2add6SEd Tanous std::get_if<bool>(&property.second); 44865b0dc32SEd Tanous if (userLocked == nullptr) 44965b0dc32SEd Tanous { 45084e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "UserLockedForF" 45184e12cb7SAppaRao Puli "ailedAttempt " 45284e12cb7SAppaRao Puli "wasn't a bool"; 45384e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 45484e12cb7SAppaRao Puli return; 45565b0dc32SEd Tanous } 45665b0dc32SEd Tanous asyncResp->res.jsonValue["Locked"] = 45765b0dc32SEd Tanous *userLocked; 45865b0dc32SEd Tanous } 45984e12cb7SAppaRao Puli else if (property.first == "UserPrivilege") 46084e12cb7SAppaRao Puli { 46184e12cb7SAppaRao Puli const std::string* userRolePtr = 462*abf2add6SEd Tanous std::get_if<std::string>(&property.second); 46384e12cb7SAppaRao Puli if (userRolePtr == nullptr) 46484e12cb7SAppaRao Puli { 46584e12cb7SAppaRao Puli BMCWEB_LOG_ERROR 46684e12cb7SAppaRao Puli << "UserPrivilege wasn't a " 46784e12cb7SAppaRao Puli "string"; 46884e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 46984e12cb7SAppaRao Puli return; 47084e12cb7SAppaRao Puli } 47184e12cb7SAppaRao Puli std::string priv = 47284e12cb7SAppaRao Puli getPrivilegeFromRoleId(*userRolePtr); 47384e12cb7SAppaRao Puli if (priv.empty()) 47484e12cb7SAppaRao Puli { 47584e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "Invalid user role"; 47684e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 47784e12cb7SAppaRao Puli return; 47884e12cb7SAppaRao Puli } 47984e12cb7SAppaRao Puli asyncResp->res.jsonValue["RoleId"] = priv; 48084e12cb7SAppaRao Puli 48184e12cb7SAppaRao Puli asyncResp->res.jsonValue["Links"]["Role"] = { 48284e12cb7SAppaRao Puli {"@odata.id", "/redfish/v1/AccountService/" 48384e12cb7SAppaRao Puli "Roles/" + 48484e12cb7SAppaRao Puli priv}}; 48584e12cb7SAppaRao Puli } 48665b0dc32SEd Tanous } 48765b0dc32SEd Tanous } 48865b0dc32SEd Tanous } 48965b0dc32SEd Tanous 490b9b2e0b2SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 49184e12cb7SAppaRao Puli "/redfish/v1/AccountService/Accounts/" + accountName; 492b9b2e0b2SEd Tanous asyncResp->res.jsonValue["Id"] = accountName; 493b9b2e0b2SEd Tanous asyncResp->res.jsonValue["UserName"] = accountName; 494b9b2e0b2SEd Tanous }, 495b9b2e0b2SEd Tanous "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 496b9b2e0b2SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 497b9b2e0b2SEd Tanous } 498a840879dSEd Tanous 499a840879dSEd Tanous void doPatch(crow::Response& res, const crow::Request& req, 500a840879dSEd Tanous const std::vector<std::string>& params) override 501a840879dSEd Tanous { 502a840879dSEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 503a840879dSEd Tanous if (params.size() != 1) 504a840879dSEd Tanous { 505f12894f8SJason M. Bills messages::internalError(asyncResp->res); 506a840879dSEd Tanous return; 507a840879dSEd Tanous } 508a840879dSEd Tanous 509a24526dcSEd Tanous std::optional<std::string> newUserName; 510a24526dcSEd Tanous std::optional<std::string> password; 511a24526dcSEd Tanous std::optional<bool> enabled; 512a24526dcSEd Tanous std::optional<std::string> roleId; 51384e12cb7SAppaRao Puli if (!json_util::readJson(req, res, "UserName", newUserName, "Password", 51484e12cb7SAppaRao Puli password, "RoleId", roleId, "Enabled", 5159712f8acSEd Tanous enabled)) 516a840879dSEd Tanous { 517a840879dSEd Tanous return; 518a840879dSEd Tanous } 519a840879dSEd Tanous 52084e12cb7SAppaRao Puli const std::string& username = params[0]; 52184e12cb7SAppaRao Puli 52284e12cb7SAppaRao Puli if (!newUserName) 523a840879dSEd Tanous { 52484e12cb7SAppaRao Puli // If the username isn't being updated, we can update the properties 52584e12cb7SAppaRao Puli // directly 52684e12cb7SAppaRao Puli updateUserProperties(asyncResp, username, password, enabled, 52784e12cb7SAppaRao Puli roleId); 52884e12cb7SAppaRao Puli return; 52984e12cb7SAppaRao Puli } 53084e12cb7SAppaRao Puli else 53184e12cb7SAppaRao Puli { 53284e12cb7SAppaRao Puli crow::connections::systemBus->async_method_call( 53384e12cb7SAppaRao Puli [this, asyncResp, username, password(std::move(password)), 53484e12cb7SAppaRao Puli roleId(std::move(roleId)), enabled(std::move(enabled)), 53584e12cb7SAppaRao Puli newUser{std::string(*newUserName)}]( 53684e12cb7SAppaRao Puli const boost::system::error_code ec) { 53784e12cb7SAppaRao Puli if (ec) 53884e12cb7SAppaRao Puli { 53984e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 540a840879dSEd Tanous messages::resourceNotFound( 54184e12cb7SAppaRao Puli asyncResp->res, 54284e12cb7SAppaRao Puli "#ManagerAccount.v1_0_3.ManagerAccount", username); 543a840879dSEd Tanous return; 544a840879dSEd Tanous } 545a840879dSEd Tanous 54684e12cb7SAppaRao Puli updateUserProperties(asyncResp, newUser, password, enabled, 54784e12cb7SAppaRao Puli roleId); 54884e12cb7SAppaRao Puli }, 54984e12cb7SAppaRao Puli "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", 55084e12cb7SAppaRao Puli "xyz.openbmc_project.User.Manager", "RenameUser", username, 55184e12cb7SAppaRao Puli *newUserName); 55284e12cb7SAppaRao Puli } 55384e12cb7SAppaRao Puli } 55484e12cb7SAppaRao Puli 55584e12cb7SAppaRao Puli void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp, 55684e12cb7SAppaRao Puli const std::string& username, 557a24526dcSEd Tanous std::optional<std::string> password, 558a24526dcSEd Tanous std::optional<bool> enabled, 559a24526dcSEd Tanous std::optional<std::string> roleId) 56084e12cb7SAppaRao Puli { 5619712f8acSEd Tanous if (password) 562a840879dSEd Tanous { 5639712f8acSEd Tanous if (!pamUpdatePassword(username, *password)) 564a840879dSEd Tanous { 565a840879dSEd Tanous BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; 566f12894f8SJason M. Bills messages::internalError(asyncResp->res); 567a840879dSEd Tanous return; 568a840879dSEd Tanous } 569a840879dSEd Tanous } 570a840879dSEd Tanous 5719712f8acSEd Tanous if (enabled) 572a840879dSEd Tanous { 573a840879dSEd Tanous crow::connections::systemBus->async_method_call( 574a840879dSEd Tanous [asyncResp](const boost::system::error_code ec) { 575a840879dSEd Tanous if (ec) 576a840879dSEd Tanous { 57784e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 578f12894f8SJason M. Bills messages::internalError(asyncResp->res); 579a840879dSEd Tanous return; 580a840879dSEd Tanous } 58184e12cb7SAppaRao Puli messages::success(asyncResp->res); 58284e12cb7SAppaRao Puli return; 58384e12cb7SAppaRao Puli }, 58484e12cb7SAppaRao Puli "xyz.openbmc_project.User.Manager", 58584e12cb7SAppaRao Puli "/xyz/openbmc_project/user/" + username, 58684e12cb7SAppaRao Puli "org.freedesktop.DBus.Properties", "Set", 58784e12cb7SAppaRao Puli "xyz.openbmc_project.User.Attributes", "UserEnabled", 588*abf2add6SEd Tanous std::variant<bool>{*enabled}); 58984e12cb7SAppaRao Puli } 59084e12cb7SAppaRao Puli 59184e12cb7SAppaRao Puli if (roleId) 59284e12cb7SAppaRao Puli { 59384e12cb7SAppaRao Puli std::string priv = getRoleIdFromPrivilege(*roleId); 59484e12cb7SAppaRao Puli if (priv.empty()) 59584e12cb7SAppaRao Puli { 59684e12cb7SAppaRao Puli messages::propertyValueNotInList(asyncResp->res, *roleId, 59784e12cb7SAppaRao Puli "RoleId"); 59884e12cb7SAppaRao Puli return; 59984e12cb7SAppaRao Puli } 60084e12cb7SAppaRao Puli 60184e12cb7SAppaRao Puli crow::connections::systemBus->async_method_call( 60284e12cb7SAppaRao Puli [asyncResp](const boost::system::error_code ec) { 60384e12cb7SAppaRao Puli if (ec) 60484e12cb7SAppaRao Puli { 60584e12cb7SAppaRao Puli BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 60684e12cb7SAppaRao Puli messages::internalError(asyncResp->res); 60784e12cb7SAppaRao Puli return; 60884e12cb7SAppaRao Puli } 609f12894f8SJason M. Bills messages::success(asyncResp->res); 610a840879dSEd Tanous }, 611a840879dSEd Tanous "xyz.openbmc_project.User.Manager", 61284e12cb7SAppaRao Puli "/xyz/openbmc_project/user/" + username, 613a840879dSEd Tanous "org.freedesktop.DBus.Properties", "Set", 61484e12cb7SAppaRao Puli "xyz.openbmc_project.User.Attributes", "UserPrivilege", 615*abf2add6SEd Tanous std::variant<std::string>{priv}); 616a840879dSEd Tanous } 617a840879dSEd Tanous } 61806e086d9SEd Tanous 61906e086d9SEd Tanous void doDelete(crow::Response& res, const crow::Request& req, 62006e086d9SEd Tanous const std::vector<std::string>& params) override 62106e086d9SEd Tanous { 62206e086d9SEd Tanous auto asyncResp = std::make_shared<AsyncResp>(res); 62306e086d9SEd Tanous 62406e086d9SEd Tanous if (params.size() != 1) 62506e086d9SEd Tanous { 626f12894f8SJason M. Bills messages::internalError(asyncResp->res); 62706e086d9SEd Tanous return; 62806e086d9SEd Tanous } 62906e086d9SEd Tanous 63006e086d9SEd Tanous const std::string userPath = "/xyz/openbmc_project/user/" + params[0]; 63106e086d9SEd Tanous 63206e086d9SEd Tanous crow::connections::systemBus->async_method_call( 63306e086d9SEd Tanous [asyncResp, username{std::move(params[0])}]( 63406e086d9SEd Tanous const boost::system::error_code ec) { 63506e086d9SEd Tanous if (ec) 63606e086d9SEd Tanous { 63706e086d9SEd Tanous messages::resourceNotFound( 638f12894f8SJason M. Bills asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", 639f12894f8SJason M. Bills username); 64006e086d9SEd Tanous return; 64106e086d9SEd Tanous } 64206e086d9SEd Tanous 643f12894f8SJason M. Bills messages::accountRemoved(asyncResp->res); 64406e086d9SEd Tanous }, 64506e086d9SEd Tanous "xyz.openbmc_project.User.Manager", userPath, 64606e086d9SEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 64706e086d9SEd Tanous } 64884e12cb7SAppaRao Puli }; 64988d16c9aSLewanczyk, Dawid 65088d16c9aSLewanczyk, Dawid } // namespace redfish 651