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