Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 17 | #include "node.hpp" |
| 18 | |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 19 | #include <error_messages.hpp> |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 20 | #include <openbmc_dbus_rest.hpp> |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 21 | #include <utils/json_utils.hpp> |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | namespace redfish |
| 24 | { |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 25 | |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 26 | using ManagedObjectType = std::vector<std::pair< |
| 27 | sdbusplus::message::object_path, |
| 28 | boost::container::flat_map< |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 29 | std::string, |
| 30 | boost::container::flat_map< |
| 31 | std::string, sdbusplus::message::variant<bool, std::string>>>>>; |
| 32 | |
| 33 | inline std::string getPrivilegeFromRoleId(boost::beast::string_view role) |
| 34 | { |
| 35 | if (role == "priv-admin") |
| 36 | { |
| 37 | return "Administrator"; |
| 38 | } |
| 39 | else if (role == "priv-callback") |
| 40 | { |
| 41 | return "Callback"; |
| 42 | } |
| 43 | else if (role == "priv-user") |
| 44 | { |
| 45 | return "User"; |
| 46 | } |
| 47 | else if (role == "priv-operator") |
| 48 | { |
| 49 | return "Operator"; |
| 50 | } |
| 51 | return ""; |
| 52 | } |
| 53 | inline std::string getRoleIdFromPrivilege(boost::beast::string_view role) |
| 54 | { |
| 55 | if (role == "Administrator") |
| 56 | { |
| 57 | return "priv-admin"; |
| 58 | } |
| 59 | else if (role == "Callback") |
| 60 | { |
| 61 | return "priv-callback"; |
| 62 | } |
| 63 | else if (role == "User") |
| 64 | { |
| 65 | return "priv-user"; |
| 66 | } |
| 67 | else if (role == "Operator") |
| 68 | { |
| 69 | return "priv-operator"; |
| 70 | } |
| 71 | return ""; |
| 72 | } |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 73 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | class AccountService : public Node |
| 75 | { |
| 76 | public: |
| 77 | AccountService(CrowApp& app) : Node(app, "/redfish/v1/AccountService/") |
| 78 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | entityPrivileges = { |
| 80 | {boost::beast::http::verb::get, |
| 81 | {{"ConfigureUsers"}, {"ConfigureManager"}}}, |
| 82 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 83 | {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, |
| 84 | {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, |
| 85 | {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, |
| 86 | {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; |
| 87 | } |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 88 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 89 | private: |
| 90 | void doGet(crow::Response& res, const crow::Request& req, |
| 91 | const std::vector<std::string>& params) override |
| 92 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 93 | res.jsonValue["@odata.id"] = "/redfish/v1/AccountService"; |
| 94 | res.jsonValue["@odata.type"] = "#AccountService.v1_1_0.AccountService"; |
| 95 | res.jsonValue["@odata.context"] = |
| 96 | "/redfish/v1/$metadata#AccountService.AccountService"; |
| 97 | res.jsonValue["Id"] = "AccountService"; |
| 98 | res.jsonValue["Description"] = "BMC User Accounts"; |
| 99 | res.jsonValue["Name"] = "Account Service"; |
| 100 | res.jsonValue["ServiceEnabled"] = true; |
| 101 | res.jsonValue["MinPasswordLength"] = 1; |
| 102 | res.jsonValue["MaxPasswordLength"] = 20; |
| 103 | res.jsonValue["Accounts"]["@odata.id"] = |
| 104 | "/redfish/v1/AccountService/Accounts"; |
| 105 | res.jsonValue["Roles"]["@odata.id"] = |
| 106 | "/redfish/v1/AccountService/Roles"; |
| 107 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | res.end(); |
| 109 | } |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 110 | }; |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 111 | class AccountsCollection : public Node |
| 112 | { |
| 113 | public: |
| 114 | AccountsCollection(CrowApp& app) : |
| 115 | Node(app, "/redfish/v1/AccountService/Accounts/") |
| 116 | { |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 117 | entityPrivileges = { |
| 118 | {boost::beast::http::verb::get, |
| 119 | {{"ConfigureUsers"}, {"ConfigureManager"}}}, |
| 120 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 121 | {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, |
| 122 | {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, |
| 123 | {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, |
| 124 | {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | void doGet(crow::Response& res, const crow::Request& req, |
| 129 | const std::vector<std::string>& params) override |
| 130 | { |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 131 | auto asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 132 | res.jsonValue = {{"@odata.context", |
| 133 | "/redfish/v1/" |
| 134 | "$metadata#ManagerAccountCollection." |
| 135 | "ManagerAccountCollection"}, |
| 136 | {"@odata.id", "/redfish/v1/AccountService/Accounts"}, |
| 137 | {"@odata.type", "#ManagerAccountCollection." |
| 138 | "ManagerAccountCollection"}, |
| 139 | {"Name", "Accounts Collection"}, |
| 140 | {"Description", "BMC User Accounts"}}; |
| 141 | |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 142 | crow::connections::systemBus->async_method_call( |
| 143 | [asyncResp](const boost::system::error_code ec, |
| 144 | const ManagedObjectType& users) { |
| 145 | if (ec) |
| 146 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 147 | messages::internalError(asyncResp->res); |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | |
| 151 | nlohmann::json& memberArray = |
| 152 | asyncResp->res.jsonValue["Members"]; |
| 153 | memberArray = nlohmann::json::array(); |
| 154 | |
| 155 | asyncResp->res.jsonValue["Members@odata.count"] = users.size(); |
| 156 | for (auto& user : users) |
| 157 | { |
| 158 | const std::string& path = |
| 159 | static_cast<const std::string&>(user.first); |
| 160 | std::size_t lastIndex = path.rfind("/"); |
| 161 | if (lastIndex == std::string::npos) |
| 162 | { |
| 163 | lastIndex = 0; |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | lastIndex += 1; |
| 168 | } |
| 169 | memberArray.push_back( |
| 170 | {{"@odata.id", "/redfish/v1/AccountService/Accounts/" + |
| 171 | path.substr(lastIndex)}}); |
| 172 | } |
| 173 | }, |
| 174 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 175 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 176 | } |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 177 | void doPost(crow::Response& res, const crow::Request& req, |
| 178 | const std::vector<std::string>& params) override |
| 179 | { |
| 180 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 181 | |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 182 | std::string username; |
| 183 | std::string password; |
| 184 | boost::optional<std::string> roleId("User"); |
| 185 | boost::optional<bool> enabled = true; |
| 186 | if (!json_util::readJson(req, res, "UserName", username, "Password", |
| 187 | password, "RoleId", roleId, "Enabled", |
| 188 | enabled)) |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 189 | { |
| 190 | return; |
| 191 | } |
| 192 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 193 | std::string priv = getRoleIdFromPrivilege(*roleId); |
| 194 | if (priv.empty()) |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 195 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 196 | messages::propertyValueNotInList(asyncResp->res, *roleId, "RoleId"); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 197 | return; |
| 198 | } |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 199 | roleId = priv; |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 200 | |
| 201 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 202 | [asyncResp, username, password{std::move(password)}]( |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 203 | const boost::system::error_code ec) { |
| 204 | if (ec) |
| 205 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 206 | messages::resourceAlreadyExists( |
| 207 | asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", |
| 208 | "UserName", username); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (!pamUpdatePassword(username, password)) |
| 213 | { |
| 214 | // At this point we have a user that's been created, but the |
| 215 | // password set failed. Something is wrong, so delete the |
| 216 | // user that we've already created |
| 217 | crow::connections::systemBus->async_method_call( |
| 218 | [asyncResp](const boost::system::error_code ec) { |
| 219 | if (ec) |
| 220 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 221 | messages::internalError(asyncResp->res); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 225 | messages::invalidObject(asyncResp->res, "Password"); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 226 | }, |
| 227 | "xyz.openbmc_project.User.Manager", |
| 228 | "/xyz/openbmc_project/user/" + username, |
| 229 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 230 | |
| 231 | BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; |
| 232 | return; |
| 233 | } |
| 234 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 235 | messages::created(asyncResp->res); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 236 | asyncResp->res.addHeader( |
| 237 | "Location", |
| 238 | "/redfish/v1/AccountService/Accounts/" + username); |
| 239 | }, |
| 240 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 241 | "xyz.openbmc_project.User.Manager", "CreateUser", username, |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 242 | std::array<const char*, 4>{"ipmi", "redfish", "ssh", "web"}, |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 243 | *roleId, *enabled); |
Ed Tanous | 04ae99e | 2018-09-20 15:54:36 -0700 | [diff] [blame] | 244 | } |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 247 | template <typename Callback> |
| 248 | inline void checkDbusPathExists(const std::string& path, Callback&& callback) |
| 249 | { |
| 250 | using GetObjectType = |
| 251 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 252 | |
| 253 | crow::connections::systemBus->async_method_call( |
| 254 | [callback{std::move(callback)}](const boost::system::error_code ec, |
| 255 | const GetObjectType& object_names) { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 256 | callback(!ec && object_names.size() != 0); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 257 | }, |
| 258 | "xyz.openbmc_project.ObjectMapper", |
| 259 | "/xyz/openbmc_project/object_mapper", |
| 260 | "xyz.openbmc_project.ObjectMapper", "GetObject", path, |
| 261 | std::array<std::string, 0>()); |
| 262 | } |
| 263 | |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 264 | class ManagerAccount : public Node |
| 265 | { |
| 266 | public: |
| 267 | ManagerAccount(CrowApp& app) : |
| 268 | Node(app, "/redfish/v1/AccountService/Accounts/<str>/", std::string()) |
| 269 | { |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 270 | entityPrivileges = { |
| 271 | {boost::beast::http::verb::get, |
| 272 | {{"ConfigureUsers"}, {"ConfigureManager"}, {"ConfigureSelf"}}}, |
| 273 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 274 | {boost::beast::http::verb::patch, {{"ConfigureUsers"}}}, |
| 275 | {boost::beast::http::verb::put, {{"ConfigureUsers"}}}, |
| 276 | {boost::beast::http::verb::delete_, {{"ConfigureUsers"}}}, |
| 277 | {boost::beast::http::verb::post, {{"ConfigureUsers"}}}}; |
| 278 | } |
| 279 | |
| 280 | private: |
| 281 | void doGet(crow::Response& res, const crow::Request& req, |
| 282 | const std::vector<std::string>& params) override |
| 283 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 284 | res.jsonValue = { |
| 285 | {"@odata.context", |
| 286 | "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"}, |
| 287 | {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"}, |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 288 | {"Name", "User Account"}, |
| 289 | {"Description", "User Account"}, |
| 290 | {"Password", nullptr}, |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 291 | {"RoleId", "Administrator"}}; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 292 | |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 293 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 294 | |
| 295 | if (params.size() != 1) |
| 296 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 297 | messages::internalError(asyncResp->res); |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
| 301 | crow::connections::systemBus->async_method_call( |
| 302 | [asyncResp, accountName{std::string(params[0])}]( |
| 303 | const boost::system::error_code ec, |
| 304 | const ManagedObjectType& users) { |
| 305 | if (ec) |
| 306 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 307 | messages::internalError(asyncResp->res); |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 308 | return; |
| 309 | } |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 310 | auto userIt = users.begin(); |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 311 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 312 | for (; userIt != users.end(); userIt++) |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 313 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 314 | if (boost::ends_with(userIt->first.str, "/" + accountName)) |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 315 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 316 | break; |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 317 | } |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 318 | } |
| 319 | if (userIt == users.end()) |
| 320 | { |
| 321 | messages::resourceNotFound(asyncResp->res, "ManagerAccount", |
| 322 | accountName); |
| 323 | return; |
| 324 | } |
| 325 | for (const auto& interface : userIt->second) |
| 326 | { |
| 327 | if (interface.first == |
| 328 | "xyz.openbmc_project.User.Attributes") |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 329 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 330 | for (const auto& property : interface.second) |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 331 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 332 | if (property.first == "UserEnabled") |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 333 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 334 | const bool* userEnabled = |
| 335 | sdbusplus::message::variant_ns::get_if< |
| 336 | bool>(&property.second); |
| 337 | if (userEnabled == nullptr) |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 338 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 339 | BMCWEB_LOG_ERROR |
| 340 | << "UserEnabled wasn't a bool"; |
| 341 | messages::internalError(asyncResp->res); |
| 342 | return; |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 343 | } |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 344 | asyncResp->res.jsonValue["Enabled"] = |
| 345 | *userEnabled; |
| 346 | } |
| 347 | else if (property.first == |
| 348 | "UserLockedForFailedAttempt") |
| 349 | { |
| 350 | const bool* userLocked = |
| 351 | sdbusplus::message::variant_ns::get_if< |
| 352 | bool>(&property.second); |
| 353 | if (userLocked == nullptr) |
| 354 | { |
| 355 | BMCWEB_LOG_ERROR << "UserLockedForF" |
| 356 | "ailedAttempt " |
| 357 | "wasn't a bool"; |
| 358 | messages::internalError(asyncResp->res); |
| 359 | return; |
| 360 | } |
| 361 | asyncResp->res.jsonValue["Locked"] = |
| 362 | *userLocked; |
| 363 | } |
| 364 | else if (property.first == "UserPrivilege") |
| 365 | { |
| 366 | const std::string* userRolePtr = |
| 367 | sdbusplus::message::variant_ns::get_if< |
| 368 | std::string>(&property.second); |
| 369 | if (userRolePtr == nullptr) |
| 370 | { |
| 371 | BMCWEB_LOG_ERROR |
| 372 | << "UserPrivilege wasn't a " |
| 373 | "string"; |
| 374 | messages::internalError(asyncResp->res); |
| 375 | return; |
| 376 | } |
| 377 | std::string priv = |
| 378 | getPrivilegeFromRoleId(*userRolePtr); |
| 379 | if (priv.empty()) |
| 380 | { |
| 381 | BMCWEB_LOG_ERROR << "Invalid user role"; |
| 382 | messages::internalError(asyncResp->res); |
| 383 | return; |
| 384 | } |
| 385 | asyncResp->res.jsonValue["RoleId"] = priv; |
| 386 | |
| 387 | asyncResp->res.jsonValue["Links"]["Role"] = { |
| 388 | {"@odata.id", "/redfish/v1/AccountService/" |
| 389 | "Roles/" + |
| 390 | priv}}; |
Ed Tanous | 65b0dc3 | 2018-09-19 16:04:03 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 396 | asyncResp->res.jsonValue["@odata.id"] = |
| 397 | "/redfish/v1/AccountService/Accounts/" + accountName; |
| 398 | asyncResp->res.jsonValue["Id"] = accountName; |
| 399 | asyncResp->res.jsonValue["UserName"] = accountName; |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 400 | }, |
| 401 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 402 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 403 | } |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 404 | |
| 405 | void doPatch(crow::Response& res, const crow::Request& req, |
| 406 | const std::vector<std::string>& params) override |
| 407 | { |
| 408 | auto asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 409 | if (params.size() != 1) |
| 410 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 411 | messages::internalError(asyncResp->res); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 415 | boost::optional<std::string> newUserName; |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 416 | boost::optional<std::string> password; |
| 417 | boost::optional<bool> enabled; |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 418 | boost::optional<std::string> roleId; |
| 419 | if (!json_util::readJson(req, res, "UserName", newUserName, "Password", |
| 420 | password, "RoleId", roleId, "Enabled", |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 421 | enabled)) |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 422 | { |
| 423 | return; |
| 424 | } |
| 425 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 426 | const std::string& username = params[0]; |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 427 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 428 | if (!newUserName) |
| 429 | { |
| 430 | // If the username isn't being updated, we can update the properties |
| 431 | // directly |
| 432 | updateUserProperties(asyncResp, username, password, enabled, |
| 433 | roleId); |
| 434 | return; |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | crow::connections::systemBus->async_method_call( |
| 439 | [this, asyncResp, username, password(std::move(password)), |
| 440 | roleId(std::move(roleId)), enabled(std::move(enabled)), |
| 441 | newUser{std::string(*newUserName)}]( |
| 442 | const boost::system::error_code ec) { |
| 443 | if (ec) |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 444 | { |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 445 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 446 | messages::resourceNotFound( |
| 447 | asyncResp->res, |
| 448 | "#ManagerAccount.v1_0_3.ManagerAccount", username); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | updateUserProperties(asyncResp, newUser, password, enabled, |
| 453 | roleId); |
| 454 | }, |
| 455 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 456 | "xyz.openbmc_project.User.Manager", "RenameUser", username, |
| 457 | *newUserName); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void updateUserProperties(std::shared_ptr<AsyncResp> asyncResp, |
| 462 | const std::string& username, |
| 463 | boost::optional<std::string> password, |
| 464 | boost::optional<bool> enabled, |
| 465 | boost::optional<std::string> roleId) |
| 466 | { |
| 467 | if (password) |
| 468 | { |
| 469 | if (!pamUpdatePassword(username, *password)) |
| 470 | { |
| 471 | BMCWEB_LOG_ERROR << "pamUpdatePassword Failed"; |
| 472 | messages::internalError(asyncResp->res); |
| 473 | return; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (enabled) |
| 478 | { |
| 479 | crow::connections::systemBus->async_method_call( |
| 480 | [asyncResp](const boost::system::error_code ec) { |
| 481 | if (ec) |
| 482 | { |
| 483 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 484 | messages::internalError(asyncResp->res); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 485 | return; |
| 486 | } |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 487 | messages::success(asyncResp->res); |
| 488 | return; |
| 489 | }, |
| 490 | "xyz.openbmc_project.User.Manager", |
| 491 | "/xyz/openbmc_project/user/" + username, |
| 492 | "org.freedesktop.DBus.Properties", "Set", |
| 493 | "xyz.openbmc_project.User.Attributes", "UserEnabled", |
| 494 | sdbusplus::message::variant<bool>{*enabled}); |
| 495 | } |
Ed Tanous | 9712f8a | 2018-09-21 13:38:49 -0700 | [diff] [blame] | 496 | |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 497 | if (roleId) |
| 498 | { |
| 499 | std::string priv = getRoleIdFromPrivilege(*roleId); |
| 500 | if (priv.empty()) |
| 501 | { |
| 502 | messages::propertyValueNotInList(asyncResp->res, *roleId, |
| 503 | "RoleId"); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | crow::connections::systemBus->async_method_call( |
| 508 | [asyncResp](const boost::system::error_code ec) { |
| 509 | if (ec) |
| 510 | { |
| 511 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 512 | messages::internalError(asyncResp->res); |
| 513 | return; |
| 514 | } |
| 515 | messages::success(asyncResp->res); |
| 516 | }, |
| 517 | "xyz.openbmc_project.User.Manager", |
| 518 | "/xyz/openbmc_project/user/" + username, |
| 519 | "org.freedesktop.DBus.Properties", "Set", |
| 520 | "xyz.openbmc_project.User.Attributes", "UserPrivilege", |
| 521 | sdbusplus::message::variant<std::string>{priv}); |
| 522 | } |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 523 | } |
Ed Tanous | 06e086d | 2018-09-19 17:19:52 -0700 | [diff] [blame] | 524 | |
| 525 | void doDelete(crow::Response& res, const crow::Request& req, |
| 526 | const std::vector<std::string>& params) override |
| 527 | { |
| 528 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 529 | |
| 530 | if (params.size() != 1) |
| 531 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 532 | messages::internalError(asyncResp->res); |
Ed Tanous | 06e086d | 2018-09-19 17:19:52 -0700 | [diff] [blame] | 533 | return; |
| 534 | } |
| 535 | |
| 536 | const std::string userPath = "/xyz/openbmc_project/user/" + params[0]; |
| 537 | |
| 538 | crow::connections::systemBus->async_method_call( |
| 539 | [asyncResp, username{std::move(params[0])}]( |
| 540 | const boost::system::error_code ec) { |
| 541 | if (ec) |
| 542 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 543 | messages::resourceNotFound( |
| 544 | asyncResp->res, "#ManagerAccount.v1_0_3.ManagerAccount", |
| 545 | username); |
Ed Tanous | 06e086d | 2018-09-19 17:19:52 -0700 | [diff] [blame] | 546 | return; |
| 547 | } |
| 548 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 549 | messages::accountRemoved(asyncResp->res); |
Ed Tanous | 06e086d | 2018-09-19 17:19:52 -0700 | [diff] [blame] | 550 | }, |
| 551 | "xyz.openbmc_project.User.Manager", userPath, |
| 552 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 553 | } |
AppaRao Puli | 84e12cb | 2018-10-11 01:28:15 +0530 | [diff] [blame^] | 554 | }; |
Lewanczyk, Dawid | 88d16c9 | 2018-02-02 14:51:09 +0100 | [diff] [blame] | 555 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 556 | } // namespace redfish |