Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [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 |
| 17 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 18 | #include "account_service.hpp" |
| 19 | #include "app.hpp" |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 20 | #include "dbus_utility.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 21 | #include "query.hpp" |
| 22 | #include "registries/privilege_registry.hpp" |
| 23 | #include "utils/json_utils.hpp" |
| 24 | |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 25 | #include <boost/process/async_pipe.hpp> |
| 26 | #include <boost/type_traits/has_dereference.hpp> |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 27 | #include <boost/url/url_view.hpp> |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 28 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 29 | #include <array> |
| 30 | #include <string_view> |
| 31 | |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 32 | namespace redfish |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 33 | { |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 34 | /** |
| 35 | * @brief Function extracts transfer protocol name from URI. |
| 36 | */ |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 37 | inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri) |
| 38 | { |
| 39 | boost::urls::result<boost::urls::url_view> url = |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 40 | boost::urls::parse_uri(imageUri); |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 41 | if (!url) |
| 42 | { |
| 43 | return "None"; |
| 44 | } |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 45 | std::string_view scheme = url->scheme(); |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 46 | if (scheme == "smb") |
| 47 | { |
| 48 | return "CIFS"; |
| 49 | } |
| 50 | if (scheme == "https") |
| 51 | { |
| 52 | return "HTTPS"; |
| 53 | } |
| 54 | |
| 55 | return "None"; |
| 56 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * @brief Read all known properties from VM object interfaces |
| 60 | */ |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 61 | inline void |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 62 | vmParseInterfaceObject(const dbus::utility::DBusInteracesMap& interfaces, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 63 | const std::shared_ptr<bmcweb::AsyncResp>& aResp) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 64 | { |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 65 | for (const auto& [interface, values] : interfaces) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 66 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 67 | if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint") |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 68 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 69 | for (const auto& [property, value] : values) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 70 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 71 | if (property == "EndpointId") |
| 72 | { |
| 73 | const std::string* endpointIdValue = |
| 74 | std::get_if<std::string>(&value); |
| 75 | if (endpointIdValue == nullptr) |
| 76 | { |
| 77 | continue; |
| 78 | } |
| 79 | if (!endpointIdValue->empty()) |
| 80 | { |
| 81 | // Proxy mode |
| 82 | aResp->res |
| 83 | .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] = |
| 84 | *endpointIdValue; |
| 85 | aResp->res.jsonValue["TransferProtocolType"] = "OEM"; |
| 86 | } |
| 87 | } |
| 88 | if (property == "ImageURL") |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 89 | { |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 90 | const std::string* imageUrlValue = |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 91 | std::get_if<std::string>(&value); |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 92 | if (imageUrlValue != nullptr && !imageUrlValue->empty()) |
Przemyslaw Czarnowski | da4784d | 2020-11-06 09:58:25 +0100 | [diff] [blame] | 93 | { |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 94 | std::filesystem::path filePath = *imageUrlValue; |
| 95 | if (!filePath.has_filename()) |
| 96 | { |
| 97 | // this will handle https share, which not |
| 98 | // necessarily has to have filename given. |
| 99 | aResp->res.jsonValue["ImageName"] = ""; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | aResp->res.jsonValue["ImageName"] = |
| 104 | filePath.filename(); |
| 105 | } |
Przemyslaw Czarnowski | da4784d | 2020-11-06 09:58:25 +0100 | [diff] [blame] | 106 | |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 107 | aResp->res.jsonValue["Image"] = *imageUrlValue; |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 108 | aResp->res.jsonValue["TransferProtocolType"] = |
| 109 | getTransferProtocolTypeFromUri(*imageUrlValue); |
| 110 | |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 111 | aResp->res.jsonValue["ConnectedVia"] = "URI"; |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 112 | } |
| 113 | } |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 114 | if (property == "WriteProtected") |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 115 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 116 | const bool* writeProtectedValue = std::get_if<bool>(&value); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 117 | if (writeProtectedValue != nullptr) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 118 | { |
Anna Platash | 9e319cf | 2020-11-17 10:18:31 +0100 | [diff] [blame] | 119 | aResp->res.jsonValue["WriteProtected"] = |
| 120 | *writeProtectedValue; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 125 | if (interface == "xyz.openbmc_project.VirtualMedia.Process") |
| 126 | { |
| 127 | for (const auto& [property, value] : values) |
| 128 | { |
| 129 | if (property == "Active") |
| 130 | { |
| 131 | const bool* activeValue = std::get_if<bool>(&value); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 132 | if (activeValue == nullptr) |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 133 | { |
| 134 | BMCWEB_LOG_DEBUG << "Value Active not found"; |
| 135 | return; |
| 136 | } |
| 137 | aResp->res.jsonValue["Inserted"] = *activeValue; |
| 138 | |
Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 139 | if (*activeValue) |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 140 | { |
| 141 | aResp->res.jsonValue["ConnectedVia"] = "Applet"; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @brief Fill template for Virtual Media Item. |
| 151 | */ |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 152 | inline nlohmann::json vmItemTemplate(const std::string& name, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 153 | const std::string& resName) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 154 | { |
| 155 | nlohmann::json item; |
Ed Tanous | fdb2034 | 2022-06-03 09:56:52 -0700 | [diff] [blame] | 156 | item["@odata.id"] = crow::utility::urlFromPieces( |
| 157 | "redfish", "v1", "Managers", name, "VirtualMedia", resName); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 158 | |
Przemyslaw Czarnowski | d04ba32 | 2020-01-21 12:41:56 +0100 | [diff] [blame] | 159 | item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia"; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 160 | item["Name"] = "Virtual Removable Media"; |
| 161 | item["Id"] = resName; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 162 | item["WriteProtected"] = true; |
Ed Tanous | 613dabe | 2022-07-09 11:17:36 -0700 | [diff] [blame] | 163 | item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"}); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 164 | item["TransferMethod"] = "Stream"; |
Przemyslaw Czarnowski | d04ba32 | 2020-01-21 12:41:56 +0100 | [diff] [blame] | 165 | item["Oem"]["OpenBMC"]["@odata.type"] = |
| 166 | "#OemVirtualMedia.v1_0_0.VirtualMedia"; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 167 | |
| 168 | return item; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @brief Fills collection data |
| 173 | */ |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 174 | inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> aResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 175 | const std::string& service, |
| 176 | const std::string& name) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 177 | { |
| 178 | BMCWEB_LOG_DEBUG << "Get available Virtual Media resources."; |
| 179 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 180 | [name, aResp{std::move(aResp)}]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 181 | const boost::system::error_code& ec, |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 182 | const dbus::utility::ManagedObjectType& subtree) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 183 | if (ec) |
| 184 | { |
| 185 | BMCWEB_LOG_DEBUG << "DBUS response error"; |
| 186 | return; |
| 187 | } |
| 188 | nlohmann::json& members = aResp->res.jsonValue["Members"]; |
| 189 | members = nlohmann::json::array(); |
| 190 | |
| 191 | for (const auto& object : subtree) |
| 192 | { |
| 193 | nlohmann::json item; |
| 194 | std::string path = object.first.filename(); |
| 195 | if (path.empty()) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 196 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 197 | continue; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 198 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 199 | |
Ed Tanous | fdb2034 | 2022-06-03 09:56:52 -0700 | [diff] [blame] | 200 | item["@odata.id"] = crow::utility::urlFromPieces( |
| 201 | "redfish", "v1", "Managers", name, "VirtualMedia", path); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 202 | members.emplace_back(std::move(item)); |
| 203 | } |
| 204 | aResp->res.jsonValue["Members@odata.count"] = members.size(); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 205 | }, |
| 206 | service, "/xyz/openbmc_project/VirtualMedia", |
| 207 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * @brief Fills data for specific resource |
| 212 | */ |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 213 | inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 214 | const std::string& service, const std::string& name, |
| 215 | const std::string& resName) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 216 | { |
| 217 | BMCWEB_LOG_DEBUG << "Get Virtual Media resource data."; |
| 218 | |
| 219 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 914e2d5 | 2022-01-07 11:38:34 -0800 | [diff] [blame] | 220 | [resName, name, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 221 | aResp](const boost::system::error_code& ec, |
Ed Tanous | 914e2d5 | 2022-01-07 11:38:34 -0800 | [diff] [blame] | 222 | const dbus::utility::ManagedObjectType& subtree) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 223 | if (ec) |
| 224 | { |
| 225 | BMCWEB_LOG_DEBUG << "DBUS response error"; |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 226 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
| 230 | for (const auto& item : subtree) |
| 231 | { |
| 232 | std::string thispath = item.first.filename(); |
| 233 | if (thispath.empty()) |
| 234 | { |
| 235 | continue; |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 238 | if (thispath != resName) |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 239 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 240 | continue; |
| 241 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 242 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 243 | // "Legacy"/"Proxy" |
| 244 | auto mode = item.first.parent_path(); |
| 245 | // "VirtualMedia" |
| 246 | auto type = mode.parent_path(); |
| 247 | if (mode.filename().empty() || type.filename().empty()) |
| 248 | { |
| 249 | continue; |
| 250 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 251 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 252 | if (type.filename() != "VirtualMedia") |
| 253 | { |
| 254 | continue; |
| 255 | } |
Przemyslaw Czarnowski | 1a6258d | 2021-04-14 11:02:46 +0200 | [diff] [blame] | 256 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 257 | aResp->res.jsonValue = vmItemTemplate(name, resName); |
Przemyslaw Czarnowski | 1a6258d | 2021-04-14 11:02:46 +0200 | [diff] [blame] | 258 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 259 | // Check if dbus path is Legacy type |
| 260 | if (mode.filename() == "Legacy") |
| 261 | { |
| 262 | aResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"] |
Ed Tanous | fdb2034 | 2022-06-03 09:56:52 -0700 | [diff] [blame] | 263 | ["target"] = crow::utility::urlFromPieces( |
| 264 | "redfish", "v1", "Managers", name, "VirtualMedia", resName, |
| 265 | "Actions", "VirtualMedia.InsertMedia"); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 268 | vmParseInterfaceObject(item.second, aResp); |
| 269 | |
| 270 | aResp->res |
| 271 | .jsonValue["Actions"]["#VirtualMedia.EjectMedia"]["target"] = |
Ed Tanous | fdb2034 | 2022-06-03 09:56:52 -0700 | [diff] [blame] | 272 | crow::utility::urlFromPieces("redfish", "v1", "Managers", name, |
| 273 | "VirtualMedia", resName, "Actions", |
| 274 | "VirtualMedia.EjectMedia"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 278 | messages::resourceNotFound(aResp->res, "VirtualMedia", resName); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 279 | }, |
| 280 | service, "/xyz/openbmc_project/VirtualMedia", |
| 281 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 282 | } |
| 283 | |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 284 | /** |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 285 | * @brief Transfer protocols supported for InsertMedia action. |
| 286 | * |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 287 | */ |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 288 | enum class TransferProtocol |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 289 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 290 | https, |
| 291 | smb, |
| 292 | invalid |
| 293 | }; |
| 294 | |
| 295 | /** |
| 296 | * @brief Function extracts transfer protocol type from URI. |
| 297 | * |
| 298 | */ |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 299 | inline std::optional<TransferProtocol> |
Ed Tanous | ace85d6 | 2021-10-26 12:45:59 -0700 | [diff] [blame] | 300 | getTransferProtocolFromUri(const boost::urls::url_view& imageUri) |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 301 | { |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 302 | std::string_view scheme = imageUri.scheme(); |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 303 | if (scheme == "smb") |
| 304 | { |
| 305 | return TransferProtocol::smb; |
| 306 | } |
| 307 | if (scheme == "https") |
| 308 | { |
| 309 | return TransferProtocol::https; |
| 310 | } |
| 311 | if (!scheme.empty()) |
| 312 | { |
| 313 | return TransferProtocol::invalid; |
| 314 | } |
| 315 | |
| 316 | return {}; |
| 317 | } |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 318 | |
| 319 | /** |
| 320 | * @brief Function convert transfer protocol from string param. |
| 321 | * |
| 322 | */ |
| 323 | inline std::optional<TransferProtocol> getTransferProtocolFromParam( |
| 324 | const std::optional<std::string>& transferProtocolType) |
| 325 | { |
| 326 | if (transferProtocolType == std::nullopt) |
Agata Olender | c6f4e01 | 2020-03-11 15:19:07 +0100 | [diff] [blame] | 327 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 328 | return {}; |
Agata Olender | c6f4e01 | 2020-03-11 15:19:07 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 331 | if (*transferProtocolType == "CIFS") |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 332 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 333 | return TransferProtocol::smb; |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 334 | } |
| 335 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 336 | if (*transferProtocolType == "HTTPS") |
| 337 | { |
| 338 | return TransferProtocol::https; |
| 339 | } |
| 340 | |
| 341 | return TransferProtocol::invalid; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * @brief Function extends URI with transfer protocol type. |
| 346 | * |
| 347 | */ |
| 348 | inline std::string |
| 349 | getUriWithTransferProtocol(const std::string& imageUri, |
| 350 | const TransferProtocol& transferProtocol) |
| 351 | { |
| 352 | if (transferProtocol == TransferProtocol::smb) |
| 353 | { |
| 354 | return "smb://" + imageUri; |
| 355 | } |
| 356 | |
| 357 | if (transferProtocol == TransferProtocol::https) |
| 358 | { |
| 359 | return "https://" + imageUri; |
| 360 | } |
| 361 | |
| 362 | return imageUri; |
| 363 | } |
| 364 | |
Przemyslaw Czarnowski | 1f2a40c | 2022-06-24 13:47:08 +0200 | [diff] [blame] | 365 | struct InsertMediaActionParams |
| 366 | { |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 367 | std::optional<std::string> imageUrl; |
Przemyslaw Czarnowski | 1f2a40c | 2022-06-24 13:47:08 +0200 | [diff] [blame] | 368 | std::optional<std::string> userName; |
| 369 | std::optional<std::string> password; |
| 370 | std::optional<std::string> transferMethod; |
| 371 | std::optional<std::string> transferProtocolType; |
| 372 | std::optional<bool> writeProtected = true; |
| 373 | std::optional<bool> inserted; |
| 374 | }; |
| 375 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 376 | template <typename T> |
| 377 | static void secureCleanup(T& value) |
| 378 | { |
Ed Tanous | 4ecc618 | 2022-01-07 09:36:26 -0800 | [diff] [blame] | 379 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 380 | auto raw = const_cast<typename T::value_type*>(value.data()); |
| 381 | explicit_bzero(raw, value.size() * sizeof(*raw)); |
| 382 | } |
| 383 | |
| 384 | class Credentials |
| 385 | { |
| 386 | public: |
| 387 | Credentials(std::string&& user, std::string&& password) : |
| 388 | userBuf(std::move(user)), passBuf(std::move(password)) |
| 389 | {} |
| 390 | |
| 391 | ~Credentials() |
| 392 | { |
| 393 | secureCleanup(userBuf); |
| 394 | secureCleanup(passBuf); |
| 395 | } |
| 396 | |
| 397 | const std::string& user() |
| 398 | { |
| 399 | return userBuf; |
| 400 | } |
| 401 | |
| 402 | const std::string& password() |
| 403 | { |
| 404 | return passBuf; |
| 405 | } |
| 406 | |
| 407 | Credentials() = delete; |
| 408 | Credentials(const Credentials&) = delete; |
| 409 | Credentials& operator=(const Credentials&) = delete; |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 410 | Credentials(Credentials&&) = delete; |
| 411 | Credentials& operator=(Credentials&&) = delete; |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 412 | |
| 413 | private: |
| 414 | std::string userBuf; |
| 415 | std::string passBuf; |
| 416 | }; |
| 417 | |
| 418 | class CredentialsProvider |
| 419 | { |
| 420 | public: |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 421 | template <typename T> |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 422 | struct Deleter |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 423 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 424 | void operator()(T* buff) const |
| 425 | { |
| 426 | if (buff) |
| 427 | { |
| 428 | secureCleanup(*buff); |
| 429 | delete buff; |
| 430 | } |
| 431 | } |
| 432 | }; |
| 433 | |
| 434 | using Buffer = std::vector<char>; |
| 435 | using SecureBuffer = std::unique_ptr<Buffer, Deleter<Buffer>>; |
| 436 | // Using explicit definition instead of std::function to avoid implicit |
| 437 | // conversions eg. stack copy instead of reference |
| 438 | using FormatterFunc = void(const std::string& username, |
| 439 | const std::string& password, Buffer& dest); |
| 440 | |
| 441 | CredentialsProvider(std::string&& user, std::string&& password) : |
| 442 | credentials(std::move(user), std::move(password)) |
| 443 | {} |
| 444 | |
| 445 | const std::string& user() |
| 446 | { |
| 447 | return credentials.user(); |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 450 | const std::string& password() |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 451 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 452 | return credentials.password(); |
| 453 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 454 | |
Ed Tanous | 1917ee9 | 2022-06-30 22:30:50 -0700 | [diff] [blame] | 455 | SecureBuffer pack(FormatterFunc* formatter) |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 456 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 457 | SecureBuffer packed{new Buffer{}}; |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 458 | if (formatter != nullptr) |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 459 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 460 | formatter(credentials.user(), credentials.password(), *packed); |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 463 | return packed; |
| 464 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 465 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 466 | private: |
| 467 | Credentials credentials; |
| 468 | }; |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 469 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 470 | // Wrapper for boost::async_pipe ensuring proper pipe cleanup |
| 471 | template <typename Buffer> |
| 472 | class Pipe |
| 473 | { |
| 474 | public: |
| 475 | using unix_fd = sdbusplus::message::unix_fd; |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 476 | |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 477 | Pipe(boost::asio::io_context& io, Buffer&& bufferIn) : |
| 478 | impl(io), buffer{std::move(bufferIn)} |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 479 | {} |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 480 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 481 | ~Pipe() |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 482 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 483 | // Named pipe needs to be explicitly removed |
| 484 | impl.close(); |
| 485 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 486 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 487 | Pipe(const Pipe&) = delete; |
| 488 | Pipe(Pipe&&) = delete; |
| 489 | Pipe& operator=(const Pipe&) = delete; |
| 490 | Pipe& operator=(Pipe&&) = delete; |
| 491 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 492 | unix_fd fd() |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 493 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 494 | return unix_fd{impl.native_source()}; |
| 495 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 496 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 497 | template <typename WriteHandler> |
| 498 | void asyncWrite(WriteHandler&& handler) |
| 499 | { |
| 500 | impl.async_write_some(data(), std::forward<WriteHandler>(handler)); |
| 501 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 502 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 503 | private: |
| 504 | // Specialization for pointer types |
| 505 | template <typename B = Buffer> |
| 506 | typename std::enable_if<boost::has_dereference<B>::value, |
| 507 | boost::asio::const_buffer>::type |
| 508 | data() |
| 509 | { |
| 510 | return boost::asio::buffer(*buffer); |
| 511 | } |
| 512 | |
| 513 | template <typename B = Buffer> |
| 514 | typename std::enable_if<!boost::has_dereference<B>::value, |
| 515 | boost::asio::const_buffer>::type |
| 516 | data() |
| 517 | { |
| 518 | return boost::asio::buffer(buffer); |
| 519 | } |
| 520 | |
| 521 | const std::string name; |
| 522 | boost::process::async_pipe impl; |
| 523 | Buffer buffer; |
| 524 | }; |
| 525 | |
| 526 | /** |
| 527 | * @brief Function transceives data with dbus directly. |
| 528 | * |
| 529 | * All BMC state properties will be retrieved before sending reset request. |
| 530 | */ |
| 531 | inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 532 | const std::string& service, const std::string& name, |
| 533 | const std::string& imageUrl, const bool rw, |
| 534 | std::string&& userName, std::string&& password) |
| 535 | { |
| 536 | using SecurePipe = Pipe<CredentialsProvider::SecureBuffer>; |
| 537 | constexpr const size_t secretLimit = 1024; |
| 538 | |
| 539 | std::shared_ptr<SecurePipe> secretPipe; |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 540 | dbus::utility::DbusVariantType unixFd = -1; |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 541 | |
| 542 | if (!userName.empty() || !password.empty()) |
| 543 | { |
| 544 | // Encapsulate in safe buffer |
| 545 | CredentialsProvider credentials(std::move(userName), |
| 546 | std::move(password)); |
| 547 | |
| 548 | // Payload must contain data + NULL delimiters |
| 549 | if (credentials.user().size() + credentials.password().size() + 2 > |
| 550 | secretLimit) |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 551 | { |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 552 | BMCWEB_LOG_ERROR << "Credentials too long to handle"; |
| 553 | messages::unrecognizedRequestBody(asyncResp->res); |
| 554 | return; |
| 555 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 556 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 557 | // Pack secret |
| 558 | auto secret = credentials.pack( |
| 559 | [](const auto& user, const auto& pass, auto& buff) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 560 | std::copy(user.begin(), user.end(), std::back_inserter(buff)); |
| 561 | buff.push_back('\0'); |
| 562 | std::copy(pass.begin(), pass.end(), std::back_inserter(buff)); |
| 563 | buff.push_back('\0'); |
| 564 | }); |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 565 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 566 | // Open pipe |
| 567 | secretPipe = std::make_shared<SecurePipe>( |
| 568 | crow::connections::systemBus->get_io_context(), std::move(secret)); |
| 569 | unixFd = secretPipe->fd(); |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 570 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 571 | // Pass secret over pipe |
| 572 | secretPipe->asyncWrite( |
| 573 | [asyncResp](const boost::system::error_code& ec, std::size_t) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 574 | if (ec) |
| 575 | { |
| 576 | BMCWEB_LOG_ERROR << "Failed to pass secret: " << ec; |
| 577 | messages::internalError(asyncResp->res); |
| 578 | } |
| 579 | }); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 580 | } |
Adrian Ambrożewicz | 988fb7b | 2020-01-13 18:52:46 +0100 | [diff] [blame] | 581 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 582 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 583 | [asyncResp, secretPipe](const boost::system::error_code& ec, |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 584 | bool success) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 585 | if (ec) |
| 586 | { |
| 587 | BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec; |
| 588 | messages::internalError(asyncResp->res); |
| 589 | } |
| 590 | else if (!success) |
| 591 | { |
| 592 | BMCWEB_LOG_ERROR << "Service responded with error"; |
| 593 | messages::generalError(asyncResp->res); |
| 594 | } |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 595 | }, |
| 596 | service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, |
| 597 | "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", imageUrl, rw, |
| 598 | unixFd); |
| 599 | } |
| 600 | |
| 601 | /** |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 602 | * @brief Function validate parameters of insert media request. |
| 603 | * |
| 604 | */ |
| 605 | inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 606 | const std::string& service, |
| 607 | const std::string& resName, |
| 608 | InsertMediaActionParams& actionParams) |
| 609 | { |
| 610 | BMCWEB_LOG_DEBUG << "Validation started"; |
| 611 | // required param imageUrl must not be empty |
| 612 | if (!actionParams.imageUrl) |
| 613 | { |
| 614 | BMCWEB_LOG_ERROR << "Request action parameter Image is empty."; |
| 615 | |
| 616 | messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image"); |
| 617 | |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | // optional param inserted must be true |
| 622 | if ((actionParams.inserted != std::nullopt) && !*actionParams.inserted) |
| 623 | { |
| 624 | BMCWEB_LOG_ERROR |
| 625 | << "Request action optional parameter Inserted must be true."; |
| 626 | |
| 627 | messages::actionParameterNotSupported(asyncResp->res, "Inserted", |
| 628 | "InsertMedia"); |
| 629 | |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | // optional param transferMethod must be stream |
| 634 | if ((actionParams.transferMethod != std::nullopt) && |
| 635 | (*actionParams.transferMethod != "Stream")) |
| 636 | { |
| 637 | BMCWEB_LOG_ERROR << "Request action optional parameter " |
| 638 | "TransferMethod must be Stream."; |
| 639 | |
| 640 | messages::actionParameterNotSupported(asyncResp->res, "TransferMethod", |
| 641 | "InsertMedia"); |
| 642 | |
| 643 | return; |
| 644 | } |
| 645 | boost::urls::result<boost::urls::url_view> url = |
| 646 | boost::urls::parse_uri(*actionParams.imageUrl); |
| 647 | if (!url) |
| 648 | { |
| 649 | messages::actionParameterValueFormatError( |
| 650 | asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia"); |
| 651 | return; |
| 652 | } |
| 653 | std::optional<TransferProtocol> uriTransferProtocolType = |
| 654 | getTransferProtocolFromUri(*url); |
| 655 | |
| 656 | std::optional<TransferProtocol> paramTransferProtocolType = |
| 657 | getTransferProtocolFromParam(actionParams.transferProtocolType); |
| 658 | |
| 659 | // ImageUrl does not contain valid protocol type |
| 660 | if (*uriTransferProtocolType == TransferProtocol::invalid) |
| 661 | { |
| 662 | BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must " |
| 663 | "contain specified protocol type from list: " |
| 664 | "(smb, https)."; |
| 665 | |
| 666 | messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); |
| 667 | |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | // transferProtocolType should contain value from list |
| 672 | if (*paramTransferProtocolType == TransferProtocol::invalid) |
| 673 | { |
| 674 | BMCWEB_LOG_ERROR << "Request action parameter TransferProtocolType " |
| 675 | "must be provided with value from list: " |
| 676 | "(CIFS, HTTPS)."; |
| 677 | |
| 678 | messages::propertyValueNotInList(asyncResp->res, |
| 679 | *actionParams.transferProtocolType, |
| 680 | "TransferProtocolType"); |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | // valid transfer protocol not provided either with URI nor param |
| 685 | if ((uriTransferProtocolType == std::nullopt) && |
| 686 | (paramTransferProtocolType == std::nullopt)) |
| 687 | { |
| 688 | BMCWEB_LOG_ERROR << "Request action parameter ImageUrl must " |
| 689 | "contain specified protocol type or param " |
| 690 | "TransferProtocolType must be provided."; |
| 691 | |
| 692 | messages::resourceAtUriInUnknownFormat(asyncResp->res, *url); |
| 693 | |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | // valid transfer protocol provided both with URI and param |
| 698 | if ((paramTransferProtocolType != std::nullopt) && |
| 699 | (uriTransferProtocolType != std::nullopt)) |
| 700 | { |
| 701 | // check if protocol is the same for URI and param |
| 702 | if (*paramTransferProtocolType != *uriTransferProtocolType) |
| 703 | { |
| 704 | BMCWEB_LOG_ERROR << "Request action parameter " |
| 705 | "TransferProtocolType must contain the " |
| 706 | "same protocol type as protocol type " |
| 707 | "provided with param imageUrl."; |
| 708 | |
| 709 | messages::actionParameterValueTypeError( |
| 710 | asyncResp->res, *actionParams.transferProtocolType, |
| 711 | "TransferProtocolType", "InsertMedia"); |
| 712 | |
| 713 | return; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | // validation passed, add protocol to URI if needed |
| 718 | if (uriTransferProtocolType == std::nullopt) |
| 719 | { |
| 720 | actionParams.imageUrl = getUriWithTransferProtocol( |
| 721 | *actionParams.imageUrl, *paramTransferProtocolType); |
| 722 | } |
| 723 | |
| 724 | doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl, |
| 725 | !(*actionParams.writeProtected), |
| 726 | std::move(*actionParams.userName), |
| 727 | std::move(*actionParams.password)); |
| 728 | } |
| 729 | |
| 730 | /** |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 731 | * @brief Function transceives data with dbus directly. |
| 732 | * |
| 733 | * All BMC state properties will be retrieved before sending reset request. |
| 734 | */ |
Ed Tanous | 24e740a | 2023-02-24 12:08:58 -0800 | [diff] [blame^] | 735 | inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 736 | const std::string& service, const std::string& name, |
| 737 | bool legacy) |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 738 | { |
| 739 | |
| 740 | // Legacy mount requires parameter with image |
| 741 | if (legacy) |
| 742 | { |
Adrian Ambrożewicz | d6da5be | 2020-01-13 18:31:01 +0100 | [diff] [blame] | 743 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 744 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 745 | if (ec) |
| 746 | { |
| 747 | BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec; |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 748 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 749 | messages::internalError(asyncResp->res); |
| 750 | return; |
| 751 | } |
Adrian Ambrożewicz | d6da5be | 2020-01-13 18:31:01 +0100 | [diff] [blame] | 752 | }, |
| 753 | service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name, |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 754 | "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount"); |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 755 | } |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 756 | else // proxy |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 757 | { |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 758 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 759 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 760 | if (ec) |
| 761 | { |
| 762 | BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec; |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 763 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 764 | messages::internalError(asyncResp->res); |
| 765 | return; |
| 766 | } |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 767 | }, |
| 768 | service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name, |
| 769 | "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount"); |
| 770 | } |
| 771 | } |
| 772 | |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 773 | inline void handleManagersVirtualMediaActionInsertPost( |
| 774 | crow::App& app, const crow::Request& req, |
| 775 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 776 | const std::string& name, const std::string& resName) |
| 777 | { |
| 778 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 779 | { |
| 780 | return; |
| 781 | } |
| 782 | if (name != "bmc") |
| 783 | { |
Przemyslaw Czarnowski | 1f2a40c | 2022-06-24 13:47:08 +0200 | [diff] [blame] | 784 | messages::resourceNotFound(asyncResp->res, "VirtualMedia.InsertMedia", |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 785 | resName); |
| 786 | |
| 787 | return; |
| 788 | } |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 789 | std::optional<InsertMediaActionParams> actionParams = |
| 790 | InsertMediaActionParams(); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 791 | |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 792 | // Read obligatory parameters (url of image) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 793 | if (!json_util::readJsonAction( |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 794 | req, asyncResp->res, "Image", actionParams->imageUrl, |
| 795 | "WriteProtected", actionParams->writeProtected, "UserName", |
| 796 | actionParams->userName, "Password", actionParams->password, |
| 797 | "Inserted", actionParams->inserted, "TransferMethod", |
| 798 | actionParams->transferMethod, "TransferProtocolType", |
| 799 | actionParams->transferProtocolType)) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 800 | { |
| 801 | return; |
| 802 | } |
| 803 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 804 | dbus::utility::getDbusObject( |
| 805 | "/xyz/openbmc_project/VirtualMedia", {}, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 806 | [asyncResp, actionParams, |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 807 | resName](const boost::system::error_code& ec, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 808 | const dbus::utility::MapperGetObject& getObjectType) mutable { |
| 809 | if (ec) |
| 810 | { |
| 811 | BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec; |
| 812 | messages::internalError(asyncResp->res); |
| 813 | |
| 814 | return; |
| 815 | } |
| 816 | std::string service = getObjectType.begin()->first; |
| 817 | BMCWEB_LOG_DEBUG << "GetObjectType: " << service; |
| 818 | |
| 819 | crow::connections::systemBus->async_method_call( |
| 820 | [service, resName, actionParams, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 821 | asyncResp](const boost::system::error_code& ec2, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 822 | dbus::utility::ManagedObjectType& subtree) mutable { |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 823 | if (ec2) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 824 | { |
| 825 | BMCWEB_LOG_DEBUG << "DBUS response error"; |
Przemyslaw Czarnowski | 1f2a40c | 2022-06-24 13:47:08 +0200 | [diff] [blame] | 826 | messages::internalError(asyncResp->res); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 827 | |
| 828 | return; |
| 829 | } |
| 830 | |
| 831 | for (const auto& object : subtree) |
| 832 | { |
| 833 | const std::string& path = |
| 834 | static_cast<const std::string&>(object.first); |
| 835 | |
| 836 | std::size_t lastIndex = path.rfind('/'); |
| 837 | if (lastIndex == std::string::npos) |
| 838 | { |
| 839 | continue; |
| 840 | } |
| 841 | |
| 842 | lastIndex += 1; |
| 843 | |
| 844 | if (path.substr(lastIndex) == resName) |
| 845 | { |
| 846 | lastIndex = path.rfind("Proxy"); |
| 847 | if (lastIndex != std::string::npos) |
| 848 | { |
| 849 | // Not possible in proxy mode |
| 850 | BMCWEB_LOG_DEBUG << "InsertMedia not " |
| 851 | "allowed in proxy mode"; |
| 852 | messages::resourceNotFound(asyncResp->res, |
| 853 | "VirtualMedia.InsertMedia", |
| 854 | resName); |
| 855 | |
| 856 | return; |
| 857 | } |
| 858 | |
| 859 | lastIndex = path.rfind("Legacy"); |
| 860 | if (lastIndex == std::string::npos) |
| 861 | { |
| 862 | continue; |
| 863 | } |
| 864 | |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 865 | if (!actionParams) |
| 866 | { |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | validateParams(asyncResp, service, resName, *actionParams); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 871 | |
| 872 | return; |
| 873 | } |
| 874 | } |
| 875 | BMCWEB_LOG_DEBUG << "Parent item not found"; |
| 876 | messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); |
| 877 | }, |
| 878 | service, "/xyz/openbmc_project/VirtualMedia", |
| 879 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 880 | }); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | inline void handleManagersVirtualMediaActionEject( |
| 884 | crow::App& app, const crow::Request& req, |
| 885 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 886 | const std::string& managerName, const std::string& resName) |
| 887 | { |
| 888 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 889 | { |
| 890 | return; |
| 891 | } |
| 892 | if (managerName != "bmc") |
| 893 | { |
Przemyslaw Czarnowski | 120fa86 | 2022-06-24 15:10:48 +0200 | [diff] [blame] | 894 | messages::resourceNotFound(asyncResp->res, "VirtualMedia.EjectMedia", |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 895 | resName); |
| 896 | |
| 897 | return; |
| 898 | } |
| 899 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 900 | dbus::utility::getDbusObject( |
| 901 | "/xyz/openbmc_project/VirtualMedia", {}, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 902 | [asyncResp, |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 903 | resName](const boost::system::error_code& ec2, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 904 | const dbus::utility::MapperGetObject& getObjectType) { |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 905 | if (ec2) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 906 | { |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 907 | BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec2; |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 908 | messages::internalError(asyncResp->res); |
| 909 | |
| 910 | return; |
| 911 | } |
| 912 | std::string service = getObjectType.begin()->first; |
| 913 | BMCWEB_LOG_DEBUG << "GetObjectType: " << service; |
| 914 | |
| 915 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 916 | [resName, service, asyncResp{asyncResp}]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 917 | const boost::system::error_code& ec, |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 918 | const dbus::utility::ManagedObjectType& subtree) { |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 919 | if (ec) |
| 920 | { |
| 921 | BMCWEB_LOG_DEBUG << "DBUS response error"; |
Przemyslaw Czarnowski | 1f2a40c | 2022-06-24 13:47:08 +0200 | [diff] [blame] | 922 | messages::internalError(asyncResp->res); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 923 | |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | for (const auto& object : subtree) |
| 928 | { |
| 929 | const std::string& path = |
| 930 | static_cast<const std::string&>(object.first); |
| 931 | |
| 932 | std::size_t lastIndex = path.rfind('/'); |
| 933 | if (lastIndex == std::string::npos) |
| 934 | { |
| 935 | continue; |
| 936 | } |
| 937 | |
| 938 | lastIndex += 1; |
| 939 | |
| 940 | if (path.substr(lastIndex) == resName) |
| 941 | { |
| 942 | lastIndex = path.rfind("Proxy"); |
| 943 | if (lastIndex != std::string::npos) |
| 944 | { |
| 945 | // Proxy mode |
Ed Tanous | 24e740a | 2023-02-24 12:08:58 -0800 | [diff] [blame^] | 946 | doEjectAction(asyncResp, service, resName, false); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | lastIndex = path.rfind("Legacy"); |
| 950 | if (lastIndex != std::string::npos) |
| 951 | { |
| 952 | // Legacy mode |
Ed Tanous | 24e740a | 2023-02-24 12:08:58 -0800 | [diff] [blame^] | 953 | doEjectAction(asyncResp, service, resName, true); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | return; |
| 957 | } |
| 958 | } |
| 959 | BMCWEB_LOG_DEBUG << "Parent item not found"; |
| 960 | messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); |
| 961 | }, |
| 962 | service, "/xyz/openbmc_project/VirtualMedia", |
| 963 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 964 | }); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | inline void handleManagersVirtualMediaCollectionGet( |
| 968 | crow::App& app, const crow::Request& req, |
| 969 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 970 | const std::string& name) |
| 971 | { |
| 972 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 973 | { |
| 974 | return; |
| 975 | } |
| 976 | if (name != "bmc") |
| 977 | { |
| 978 | messages::resourceNotFound(asyncResp->res, "VirtualMedia", name); |
| 979 | |
| 980 | return; |
| 981 | } |
| 982 | |
| 983 | asyncResp->res.jsonValue["@odata.type"] = |
| 984 | "#VirtualMediaCollection.VirtualMediaCollection"; |
| 985 | asyncResp->res.jsonValue["Name"] = "Virtual Media Services"; |
Ed Tanous | fdb2034 | 2022-06-03 09:56:52 -0700 | [diff] [blame] | 986 | asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces( |
| 987 | "redfish", "v1", "Managers", name, "VirtualMedia"); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 988 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 989 | dbus::utility::getDbusObject( |
| 990 | "/xyz/openbmc_project/VirtualMedia", {}, |
| 991 | [asyncResp, name](const boost::system::error_code& ec, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 992 | const dbus::utility::MapperGetObject& getObjectType) { |
| 993 | if (ec) |
| 994 | { |
| 995 | BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec; |
| 996 | messages::internalError(asyncResp->res); |
| 997 | |
| 998 | return; |
| 999 | } |
| 1000 | std::string service = getObjectType.begin()->first; |
| 1001 | BMCWEB_LOG_DEBUG << "GetObjectType: " << service; |
| 1002 | |
| 1003 | getVmResourceList(asyncResp, service, name); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 1004 | }); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | inline void |
| 1008 | handleVirtualMediaGet(crow::App& app, const crow::Request& req, |
| 1009 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1010 | const std::string& name, const std::string& resName) |
| 1011 | { |
| 1012 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1013 | { |
| 1014 | return; |
| 1015 | } |
| 1016 | if (name != "bmc") |
| 1017 | { |
| 1018 | messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName); |
| 1019 | |
| 1020 | return; |
| 1021 | } |
| 1022 | |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 1023 | dbus::utility::getDbusObject( |
| 1024 | "/xyz/openbmc_project/VirtualMedia", {}, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1025 | [asyncResp, name, |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 1026 | resName](const boost::system::error_code& ec, |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1027 | const dbus::utility::MapperGetObject& getObjectType) { |
| 1028 | if (ec) |
| 1029 | { |
| 1030 | BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: " << ec; |
| 1031 | messages::internalError(asyncResp->res); |
| 1032 | |
| 1033 | return; |
| 1034 | } |
| 1035 | std::string service = getObjectType.begin()->first; |
| 1036 | BMCWEB_LOG_DEBUG << "GetObjectType: " << service; |
| 1037 | |
| 1038 | getVmData(asyncResp, service, name, resName); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 1039 | }); |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1042 | inline void requestNBDVirtualMediaRoutes(App& app) |
| 1043 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1044 | BMCWEB_ROUTE( |
| 1045 | app, |
| 1046 | "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1047 | .privileges(redfish::privileges::postVirtualMedia) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1048 | .methods(boost::beast::http::verb::post)(std::bind_front( |
| 1049 | handleManagersVirtualMediaActionInsertPost, std::ref(app))); |
Przemyslaw Czarnowski | e13c276 | 2019-09-02 17:32:43 +0200 | [diff] [blame] | 1050 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1051 | BMCWEB_ROUTE( |
| 1052 | app, |
| 1053 | "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1054 | .privileges(redfish::privileges::postVirtualMedia) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1055 | .methods(boost::beast::http::verb::post)(std::bind_front( |
| 1056 | handleManagersVirtualMediaActionEject, std::ref(app))); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1057 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1058 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1059 | .privileges(redfish::privileges::getVirtualMediaCollection) |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1060 | .methods(boost::beast::http::verb::get)(std::bind_front( |
| 1061 | handleManagersVirtualMediaCollectionGet, std::ref(app))); |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 1062 | |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1063 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1064 | .privileges(redfish::privileges::getVirtualMedia) |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1065 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 96825be | 2022-06-03 09:43:38 -0700 | [diff] [blame] | 1066 | std::bind_front(handleVirtualMediaGet, std::ref(app))); |
Ed Tanous | 22db172 | 2021-06-09 10:53:51 -0700 | [diff] [blame] | 1067 | } |
Przemyslaw Czarnowski | 107077d | 2019-07-11 10:16:43 +0200 | [diff] [blame] | 1068 | |
| 1069 | } // namespace redfish |