summaryrefslogtreecommitdiff
path: root/redfish-core
diff options
context:
space:
mode:
Diffstat (limited to 'redfish-core')
-rw-r--r--redfish-core/lib/log_services.hpp81
1 files changed, 40 insertions, 41 deletions
diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index 371ae44867..24f0251ee1 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -45,6 +45,7 @@
#include <array>
#include <charconv>
+#include <cstddef>
#include <filesystem>
#include <iterator>
#include <optional>
@@ -745,7 +746,35 @@ inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
std::format("{}/entry/{}", getDumpPath(dumpType), entryID),
"xyz.openbmc_project.Object.Delete", "Delete");
}
+inline bool checkSizeLimit(int fd, crow::Response& res)
+{
+ long long int size = lseek(fd, 0, SEEK_END);
+ if (size <= 0)
+ {
+ BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
+ size);
+ messages::internalError(res);
+ return false;
+ }
+ // Arbitrary max size of 20MB to accommodate BMC dumps
+ constexpr long long int maxFileSize = 20LL * 1024LL * 1024LL;
+ if (size > maxFileSize)
+ {
+ BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
+ size, maxFileSize);
+ messages::internalError(res);
+ return false;
+ }
+ off_t rc = lseek(fd, 0, SEEK_SET);
+ if (rc < 0)
+ {
+ BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
+ messages::internalError(res);
+ return false;
+ }
+ return true;
+}
inline void
downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& entryID,
@@ -781,59 +810,29 @@ inline void
messages::internalError(asyncResp->res);
return;
}
-
- long long int size = lseek(fd, 0, SEEK_END);
- if (size <= 0)
+ if (!checkSizeLimit(fd, asyncResp->res))
{
- BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
- size);
- messages::internalError(asyncResp->res);
close(fd);
return;
}
-
- // Arbitrary max size of 20MB to accommodate BMC dumps
- constexpr int maxFileSize = 20 * 1024 * 1024;
- if (size > maxFileSize)
- {
- BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
- size, maxFileSize);
- messages::internalError(asyncResp->res);
- close(fd);
- return;
- }
- long long int rc = lseek(fd, 0, SEEK_SET);
- if (rc < 0)
+ if (downloadEntryType == "System")
{
- BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
- messages::internalError(asyncResp->res);
- close(fd);
+ if (!asyncResp->res.openFd(fd, bmcweb::EncodingType::Base64))
+ {
+ messages::internalError(asyncResp->res);
+ close(fd);
+ return;
+ }
+ asyncResp->res.addHeader(
+ boost::beast::http::field::content_transfer_encoding, "Base64");
return;
}
-
- std::string body;
- body.resize(static_cast<size_t>(size), '\0');
- rc = read(fd, body.data(), body.size());
- if ((rc == -1) || (rc != size))
+ if (!asyncResp->res.openFd(fd))
{
- BMCWEB_LOG_ERROR("Failed to read in file");
messages::internalError(asyncResp->res);
close(fd);
return;
}
- close(fd);
- if (downloadEntryType == "System")
- {
- // Base64 encode response.
- asyncResp->res.write(crow::utility::base64encode(body));
- asyncResp->res.addHeader(
- boost::beast::http::field::content_transfer_encoding, "Base64");
- }
- else
- {
- asyncResp->res.write(std::move(body));
- }
-
asyncResp->res.addHeader(boost::beast::http::field::content_type,
"application/octet-stream");
}