Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 3 | #pragma once |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 4 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "app.hpp" |
| 6 | #include "async_resp.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 7 | #include "http_request.hpp" |
Sunitha Harish | 56d0bb0 | 2024-04-06 03:35:34 -0500 | [diff] [blame] | 8 | #include "ibm/utils.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | #include "logging.hpp" |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 10 | #include "str_utility.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 11 | #include "utils/json_utils.hpp" |
| 12 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 13 | #include <boost/beast/core/string_type.hpp> |
| 14 | #include <boost/beast/http/field.hpp> |
| 15 | #include <boost/beast/http/status.hpp> |
| 16 | #include <boost/beast/http/verb.hpp> |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 17 | #include <nlohmann/json.hpp> |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 18 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 19 | #include <cstddef> |
| 20 | #include <cstdint> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 21 | #include <filesystem> |
| 22 | #include <fstream> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 23 | #include <iterator> |
| 24 | #include <memory> |
| 25 | #include <string> |
| 26 | #include <system_error> |
| 27 | #include <utility> |
| 28 | #include <vector> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 29 | |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 30 | namespace crow |
| 31 | { |
| 32 | namespace ibm_mc |
| 33 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 34 | constexpr const char* methodNotAllowedMsg = "Method Not Allowed"; |
| 35 | constexpr const char* resourceNotFoundMsg = "Resource Not Found"; |
| 36 | constexpr const char* contentNotAcceptableMsg = "Content Not Acceptable"; |
| 37 | constexpr const char* internalServerError = "Internal Server Error"; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 38 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 39 | constexpr size_t maxSaveareaDirSize = |
Sunitha Harish | f8a4347 | 2022-08-08 02:07:12 -0500 | [diff] [blame] | 40 | 25000000; // Allow save area dir size to be max 25MB |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 41 | constexpr size_t minSaveareaFileSize = |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 42 | 100; // Allow save area file size of minimum 100B |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 43 | constexpr size_t maxSaveareaFileSize = |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 44 | 500000; // Allow save area file size upto 500KB |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 45 | constexpr size_t maxBroadcastMsgSize = |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 46 | 1000; // Allow Broadcast message size upto 1KB |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 47 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 48 | inline void handleFilePut(const crow::Request& req, |
| 49 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 02379d3 | 2020-09-15 21:15:44 -0700 | [diff] [blame] | 50 | const std::string& fileID) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 51 | { |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 52 | std::error_code ec; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 53 | // Check the content-type of the request |
Sunitha Harish | 086d32c | 2021-02-01 02:11:49 -0600 | [diff] [blame] | 54 | boost::beast::string_view contentType = req.getHeaderValue("content-type"); |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 55 | if (!bmcweb::asciiIEquals(contentType, "application/octet-stream")) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 56 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 57 | asyncResp->res.result(boost::beast::http::status::not_acceptable); |
| 58 | asyncResp->res.jsonValue["Description"] = contentNotAcceptableMsg; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 59 | return; |
| 60 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 61 | BMCWEB_LOG_DEBUG( |
| 62 | "File upload in application/octet-stream format. Continue.."); |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 63 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 64 | BMCWEB_LOG_DEBUG( |
| 65 | "handleIbmPut: Request to create/update the save-area file"); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 66 | std::string_view path = |
| 67 | "/var/lib/bmcweb/ibm-management-console/configfiles"; |
| 68 | if (!crow::ibm_utils::createDirectory(path)) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 69 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 70 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 71 | asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 72 | return; |
| 73 | } |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 74 | |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 75 | std::ofstream file; |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 76 | std::filesystem::path loc( |
| 77 | "/var/lib/bmcweb/ibm-management-console/configfiles"); |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 78 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 79 | // Get the current size of the savearea directory |
| 80 | std::filesystem::recursive_directory_iterator iter(loc, ec); |
| 81 | if (ec) |
| 82 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 83 | asyncResp->res.result( |
| 84 | boost::beast::http::status::internal_server_error); |
| 85 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 86 | BMCWEB_LOG_DEBUG("handleIbmPut: Failed to prepare save-area " |
| 87 | "directory iterator. ec : {}", |
| 88 | ec.message()); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | std::uintmax_t saveAreaDirSize = 0; |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 92 | for (const auto& it : iter) |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 93 | { |
| 94 | if (!std::filesystem::is_directory(it, ec)) |
| 95 | { |
| 96 | if (ec) |
| 97 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 98 | asyncResp->res.result( |
| 99 | boost::beast::http::status::internal_server_error); |
| 100 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 101 | BMCWEB_LOG_DEBUG("handleIbmPut: Failed to find save-area " |
| 102 | "directory . ec : {}", |
| 103 | ec.message()); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | std::uintmax_t fileSize = std::filesystem::file_size(it, ec); |
| 107 | if (ec) |
| 108 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 109 | asyncResp->res.result( |
| 110 | boost::beast::http::status::internal_server_error); |
| 111 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 112 | BMCWEB_LOG_DEBUG("handleIbmPut: Failed to find save-area " |
| 113 | "file size inside the directory . ec : {}", |
| 114 | ec.message()); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | saveAreaDirSize += fileSize; |
| 118 | } |
| 119 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 120 | BMCWEB_LOG_DEBUG("saveAreaDirSize: {}", saveAreaDirSize); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 121 | |
| 122 | // Get the file size getting uploaded |
Ed Tanous | 33c6b58 | 2023-02-14 15:05:48 -0800 | [diff] [blame] | 123 | const std::string& data = req.body(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 124 | BMCWEB_LOG_DEBUG("data length: {}", data.length()); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 125 | |
| 126 | if (data.length() < minSaveareaFileSize) |
| 127 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 128 | asyncResp->res.result(boost::beast::http::status::bad_request); |
| 129 | asyncResp->res.jsonValue["Description"] = |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 130 | "File size is less than minimum allowed size[100B]"; |
| 131 | return; |
| 132 | } |
| 133 | if (data.length() > maxSaveareaFileSize) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 134 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 135 | asyncResp->res.result(boost::beast::http::status::bad_request); |
| 136 | asyncResp->res.jsonValue["Description"] = |
Ratan Gupta | e46946a | 2020-05-11 13:22:59 +0530 | [diff] [blame] | 137 | "File size exceeds maximum allowed size[500KB]"; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 138 | return; |
| 139 | } |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 140 | |
| 141 | // Form the file path |
| 142 | loc /= fileID; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 143 | BMCWEB_LOG_DEBUG("Writing to the file: {}", loc.string()); |
Asmitha Karunanithi | 10693fa | 2020-07-27 02:27:49 -0500 | [diff] [blame] | 144 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 145 | // Check if the same file exists in the directory |
| 146 | bool fileExists = std::filesystem::exists(loc, ec); |
| 147 | if (ec) |
Asmitha Karunanithi | 10693fa | 2020-07-27 02:27:49 -0500 | [diff] [blame] | 148 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 149 | asyncResp->res.result( |
| 150 | boost::beast::http::status::internal_server_error); |
| 151 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 152 | BMCWEB_LOG_DEBUG("handleIbmPut: Failed to find if file exists. ec : {}", |
| 153 | ec.message()); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 154 | return; |
Asmitha Karunanithi | 10693fa | 2020-07-27 02:27:49 -0500 | [diff] [blame] | 155 | } |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 156 | |
| 157 | std::uintmax_t newSizeToWrite = 0; |
| 158 | if (fileExists) |
| 159 | { |
| 160 | // File exists. Get the current file size |
| 161 | std::uintmax_t currentFileSize = std::filesystem::file_size(loc, ec); |
| 162 | if (ec) |
| 163 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 164 | asyncResp->res.result( |
| 165 | boost::beast::http::status::internal_server_error); |
| 166 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 167 | BMCWEB_LOG_DEBUG("handleIbmPut: Failed to find file size. ec : {}", |
| 168 | ec.message()); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | // Calculate the difference in the file size. |
| 172 | // If the data.length is greater than the existing file size, then |
| 173 | // calculate the difference. Else consider the delta size as zero - |
| 174 | // because there is no increase in the total directory size. |
| 175 | // We need to add the diff only if the incoming data is larger than the |
| 176 | // existing filesize |
| 177 | if (data.length() > currentFileSize) |
| 178 | { |
| 179 | newSizeToWrite = data.length() - currentFileSize; |
| 180 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 181 | BMCWEB_LOG_DEBUG("newSizeToWrite: {}", newSizeToWrite); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
| 185 | // This is a new file upload |
| 186 | newSizeToWrite = data.length(); |
| 187 | } |
| 188 | |
| 189 | // Calculate the total dir size before writing the new file |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 190 | BMCWEB_LOG_DEBUG("total new size: {}", saveAreaDirSize + newSizeToWrite); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 191 | |
| 192 | if ((saveAreaDirSize + newSizeToWrite) > maxSaveareaDirSize) |
| 193 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 194 | asyncResp->res.result(boost::beast::http::status::bad_request); |
| 195 | asyncResp->res.jsonValue["Description"] = |
| 196 | "File size does not fit in the savearea " |
Sunitha Harish | f8a4347 | 2022-08-08 02:07:12 -0500 | [diff] [blame] | 197 | "directory maximum allowed size[25MB]"; |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 201 | file.open(loc, std::ofstream::out); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 202 | |
| 203 | // set the permission of the file to 600 |
| 204 | std::filesystem::perms permission = std::filesystem::perms::owner_write | |
| 205 | std::filesystem::perms::owner_read; |
| 206 | std::filesystem::permissions(loc, permission); |
| 207 | |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 208 | if (file.fail()) |
| 209 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 210 | BMCWEB_LOG_DEBUG("Error while opening the file for writing"); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 211 | asyncResp->res.result( |
| 212 | boost::beast::http::status::internal_server_error); |
| 213 | asyncResp->res.jsonValue["Description"] = |
| 214 | "Error while creating the file"; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 215 | return; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 216 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 217 | file << data; |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 218 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 219 | // Push an event |
| 220 | if (fileExists) |
| 221 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 222 | BMCWEB_LOG_DEBUG("config file is updated"); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 223 | asyncResp->res.jsonValue["Description"] = "File Updated"; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 224 | } |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 225 | else |
| 226 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 227 | BMCWEB_LOG_DEBUG("config file is created"); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 228 | asyncResp->res.jsonValue["Description"] = "File Created"; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 229 | } |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 230 | } |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 231 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 232 | inline void handleConfigFileList( |
| 233 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 234 | { |
| 235 | std::vector<std::string> pathObjList; |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 236 | std::filesystem::path loc( |
| 237 | "/var/lib/bmcweb/ibm-management-console/configfiles"); |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 238 | if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc)) |
| 239 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 240 | for (const auto& file : std::filesystem::directory_iterator(loc)) |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 241 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 242 | const std::filesystem::path& pathObj = file.path(); |
cm-jishnu | 5a19396 | 2022-12-02 03:45:27 -0600 | [diff] [blame] | 243 | if (std::filesystem::is_regular_file(pathObj)) |
| 244 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 245 | pathObjList.emplace_back( |
| 246 | "/ibm/v1/Host/ConfigFiles/" + pathObj.filename().string()); |
cm-jishnu | 5a19396 | 2022-12-02 03:45:27 -0600 | [diff] [blame] | 247 | } |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 248 | } |
| 249 | } |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 250 | asyncResp->res.jsonValue["@odata.type"] = |
| 251 | "#IBMConfigFile.v1_0_0.IBMConfigFile"; |
| 252 | asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/Host/ConfigFiles/"; |
| 253 | asyncResp->res.jsonValue["Id"] = "ConfigFiles"; |
| 254 | asyncResp->res.jsonValue["Name"] = "ConfigFiles"; |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 255 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 256 | asyncResp->res.jsonValue["Members"] = std::move(pathObjList); |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 257 | asyncResp->res.jsonValue["Actions"]["#IBMConfigFiles.DeleteAll"]["target"] = |
| 258 | "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll"; |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 259 | } |
| 260 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 261 | inline void deleteConfigFiles( |
| 262 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 263 | { |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 264 | std::error_code ec; |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 265 | std::filesystem::path loc( |
| 266 | "/var/lib/bmcweb/ibm-management-console/configfiles"); |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 267 | if (std::filesystem::exists(loc) && std::filesystem::is_directory(loc)) |
| 268 | { |
| 269 | std::filesystem::remove_all(loc, ec); |
| 270 | if (ec) |
| 271 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 272 | asyncResp->res.result( |
| 273 | boost::beast::http::status::internal_server_error); |
| 274 | asyncResp->res.jsonValue["Description"] = internalServerError; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 275 | BMCWEB_LOG_DEBUG("deleteConfigFiles: Failed to delete the " |
| 276 | "config files directory. ec : {}", |
| 277 | ec.message()); |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 278 | } |
| 279 | } |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 280 | } |
| 281 | |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 282 | inline void handleFileGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 283 | const std::string& fileID) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 284 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 285 | BMCWEB_LOG_DEBUG("HandleGet on SaveArea files on path: {}", fileID); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 286 | std::filesystem::path loc( |
| 287 | "/var/lib/bmcweb/ibm-management-console/configfiles/" + fileID); |
cm-jishnu | 5a19396 | 2022-12-02 03:45:27 -0600 | [diff] [blame] | 288 | if (!std::filesystem::exists(loc) || !std::filesystem::is_regular_file(loc)) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 289 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 290 | BMCWEB_LOG_WARNING("{} Not found", loc.string()); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 291 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 292 | asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
| 296 | std::ifstream readfile(loc.string()); |
| 297 | if (!readfile) |
| 298 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 299 | BMCWEB_LOG_WARNING("{} Not found", loc.string()); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 300 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 301 | asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 305 | std::string contentDispositionParam = |
| 306 | "attachment; filename=\"" + fileID + "\""; |
Ed Tanous | d9f6c62 | 2022-03-17 09:12:17 -0700 | [diff] [blame] | 307 | asyncResp->res.addHeader(boost::beast::http::field::content_disposition, |
| 308 | contentDispositionParam); |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 309 | std::string fileData; |
| 310 | fileData = {std::istreambuf_iterator<char>(readfile), |
| 311 | std::istreambuf_iterator<char>()}; |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 312 | asyncResp->res.jsonValue["Data"] = fileData; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 315 | inline void handleFileDelete( |
| 316 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 317 | const std::string& fileID) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 318 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 319 | std::string filePath( |
| 320 | "/var/lib/bmcweb/ibm-management-console/configfiles/" + fileID); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 321 | BMCWEB_LOG_DEBUG("Removing the file : {}", filePath); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 322 | std::ifstream fileOpen(filePath.c_str()); |
| 323 | if (static_cast<bool>(fileOpen)) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 324 | { |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 325 | if (remove(filePath.c_str()) == 0) |
| 326 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 327 | BMCWEB_LOG_DEBUG("File removed!"); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 328 | asyncResp->res.jsonValue["Description"] = "File Deleted"; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 329 | } |
| 330 | else |
| 331 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 332 | BMCWEB_LOG_ERROR("File not removed!"); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 333 | asyncResp->res.result( |
| 334 | boost::beast::http::status::internal_server_error); |
| 335 | asyncResp->res.jsonValue["Description"] = internalServerError; |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 336 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 337 | } |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 338 | else |
| 339 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 340 | BMCWEB_LOG_WARNING("File not found!"); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 341 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 342 | asyncResp->res.jsonValue["Description"] = resourceNotFoundMsg; |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 343 | } |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 344 | } |
| 345 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 346 | inline void handleBroadcastService( |
| 347 | const crow::Request& req, |
| 348 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 349 | { |
| 350 | std::string broadcastMsg; |
| 351 | |
Willy Tu | 15ed678 | 2021-12-14 11:03:16 -0800 | [diff] [blame] | 352 | if (!redfish::json_util::readJsonPatch(req, asyncResp->res, "Message", |
| 353 | broadcastMsg)) |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 354 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 355 | BMCWEB_LOG_DEBUG("Not a Valid JSON"); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 356 | asyncResp->res.result(boost::beast::http::status::bad_request); |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | if (broadcastMsg.size() > maxBroadcastMsgSize) |
| 360 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 361 | BMCWEB_LOG_ERROR("Message size exceeds maximum allowed size[1KB]"); |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 362 | asyncResp->res.result(boost::beast::http::status::bad_request); |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 363 | return; |
| 364 | } |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 365 | } |
| 366 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 367 | inline void handleFileUrl(const crow::Request& req, |
| 368 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 369 | const std::string& fileID) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 370 | { |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 371 | if (req.method() == boost::beast::http::verb::put) |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 372 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 373 | handleFilePut(req, asyncResp, fileID); |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 374 | return; |
| 375 | } |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 376 | if (req.method() == boost::beast::http::verb::get) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 377 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 378 | handleFileGet(asyncResp, fileID); |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 379 | return; |
| 380 | } |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 381 | if (req.method() == boost::beast::http::verb::delete_) |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 382 | { |
Sunitha Harish | db81c07 | 2021-01-21 23:33:21 -0600 | [diff] [blame] | 383 | handleFileDelete(asyncResp, fileID); |
asmithakarun | 1c7b07c | 2019-09-09 03:42:59 -0500 | [diff] [blame] | 384 | return; |
| 385 | } |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 386 | } |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 387 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 388 | inline bool isValidConfigFileName(const std::string& fileName, |
| 389 | crow::Response& res) |
| 390 | { |
| 391 | if (fileName.empty()) |
| 392 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 393 | BMCWEB_LOG_ERROR("Empty filename"); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 394 | res.jsonValue["Description"] = "Empty file path in the url"; |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | // ConfigFile name is allowed to take upper and lowercase letters, |
| 399 | // numbers and hyphen |
| 400 | std::size_t found = fileName.find_first_not_of( |
| 401 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-"); |
| 402 | if (found != std::string::npos) |
| 403 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 404 | BMCWEB_LOG_ERROR("Unsupported character in filename: {}", fileName); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 405 | res.jsonValue["Description"] = "Unsupported character in filename"; |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | // Check the filename length |
| 410 | if (fileName.length() > 20) |
| 411 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 412 | BMCWEB_LOG_ERROR("Name must be maximum 20 characters. " |
| 413 | "Input filename length is: {}", |
| 414 | fileName.length()); |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 415 | res.jsonValue["Description"] = "Filename must be maximum 20 characters"; |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | return true; |
| 420 | } |
| 421 | |
Ed Tanous | 02379d3 | 2020-09-15 21:15:44 -0700 | [diff] [blame] | 422 | inline void requestRoutes(App& app) |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 423 | { |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 424 | // allowed only for admin |
| 425 | BMCWEB_ROUTE(app, "/ibm/v1/") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 426 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 427 | .methods(boost::beast::http::verb::get)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 428 | [](const crow::Request&, |
| 429 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 430 | asyncResp->res.jsonValue["@odata.type"] = |
| 431 | "#ibmServiceRoot.v1_0_0.ibmServiceRoot"; |
| 432 | asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/"; |
| 433 | asyncResp->res.jsonValue["Id"] = "IBM Rest RootService"; |
| 434 | asyncResp->res.jsonValue["Name"] = "IBM Service Root"; |
| 435 | asyncResp->res.jsonValue["ConfigFiles"]["@odata.id"] = |
| 436 | "/ibm/v1/Host/ConfigFiles"; |
| 437 | asyncResp->res.jsonValue["BroadcastService"]["@odata.id"] = |
| 438 | "/ibm/v1/HMC/BroadcastService"; |
| 439 | }); |
Sunitha Harish | 97b0e43 | 2019-11-21 04:59:29 -0600 | [diff] [blame] | 440 | |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 441 | BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 442 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 443 | .methods(boost::beast::http::verb::get)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 444 | [](const crow::Request&, |
| 445 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 446 | handleConfigFileList(asyncResp); |
| 447 | }); |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 448 | |
| 449 | BMCWEB_ROUTE(app, |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 450 | "/ibm/v1/Host/ConfigFiles/Actions/IBMConfigFiles.DeleteAll") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 451 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 452 | .methods(boost::beast::http::verb::post)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 453 | [](const crow::Request&, |
| 454 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 455 | deleteConfigFiles(asyncResp); |
| 456 | }); |
Ratan Gupta | d3630cb | 2019-12-14 11:21:35 +0530 | [diff] [blame] | 457 | |
Sunitha Harish | 7c0bbe7 | 2020-07-30 08:25:28 -0500 | [diff] [blame] | 458 | BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles/<str>") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 459 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 460 | .methods(boost::beast::http::verb::put, boost::beast::http::verb::get, |
| 461 | boost::beast::http::verb::delete_)( |
| 462 | [](const crow::Request& req, |
| 463 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 464 | const std::string& fileName) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 465 | BMCWEB_LOG_DEBUG("ConfigFile : {}", fileName); |
| 466 | // Validate the incoming fileName |
| 467 | if (!isValidConfigFileName(fileName, asyncResp->res)) |
| 468 | { |
| 469 | asyncResp->res.result( |
| 470 | boost::beast::http::status::bad_request); |
| 471 | return; |
| 472 | } |
| 473 | handleFileUrl(req, asyncResp, fileName); |
| 474 | }); |
Ratan Gupta | 734a1c3 | 2019-12-14 11:53:48 +0530 | [diff] [blame] | 475 | |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 476 | BMCWEB_ROUTE(app, "/ibm/v1/HMC/BroadcastService") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 477 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Asmitha Karunanithi | 5738de5 | 2020-07-17 02:03:31 -0500 | [diff] [blame] | 478 | .methods(boost::beast::http::verb::post)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 479 | [](const crow::Request& req, |
| 480 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 481 | handleBroadcastService(req, asyncResp); |
| 482 | }); |
Ratan Gupta | 453fed0 | 2019-12-14 09:39:47 +0530 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | } // namespace ibm_mc |
| 486 | } // namespace crow |