xref: /openbmc/bmcweb/redfish-core/include/utils/log_services_utils.hpp (revision 79a916a26256a2464121b670181bbf0b10c1deee)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
4 
5 #pragma once
6 
7 #include "async_resp.hpp"
8 #include "boost_formatters.hpp"
9 #include "error_messages.hpp"
10 #include "http_body.hpp"
11 #include "http_response.hpp"
12 #include "logging.hpp"
13 
14 #include <asm-generic/errno.h>
15 #include <unistd.h>
16 
17 #include <boost/beast/http/field.hpp>
18 #include <boost/beast/http/status.hpp>
19 #include <boost/beast/http/verb.hpp>
20 #include <boost/system/linux_error.hpp>
21 #include <boost/url/format.hpp>
22 #include <boost/url/url.hpp>
23 #include <sdbusplus/message.hpp>
24 #include <sdbusplus/message/native_types.hpp>
25 #include <sdbusplus/unpack_properties.hpp>
26 
27 #include <cstdio>
28 #include <string>
29 
30 namespace redfish
31 {
32 namespace log_services_utils
33 {
34 
checkSizeLimit(int fd,crow::Response & res)35 inline bool checkSizeLimit(int fd, crow::Response& res)
36 {
37     long long int size = lseek(fd, 0, SEEK_END);
38     if (size <= 0)
39     {
40         BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
41                          size);
42         messages::internalError(res);
43         return false;
44     }
45 
46     // Arbitrary max size of 20MB to accommodate BMC dumps
47     constexpr long long int maxFileSize = 20LL * 1024LL * 1024LL;
48     if (size > maxFileSize)
49     {
50         BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
51                          size, maxFileSize);
52         messages::internalError(res);
53         return false;
54     }
55     off_t rc = lseek(fd, 0, SEEK_SET);
56     if (rc < 0)
57     {
58         BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
59         messages::internalError(res);
60         return false;
61     }
62     return true;
63 }
64 
downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & entryID,const std::string & downloadEntryType,const boost::system::error_code & ec,const sdbusplus::message::unix_fd & unixfd)65 inline void downloadEntryCallback(
66     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
67     const std::string& entryID, const std::string& downloadEntryType,
68     const boost::system::error_code& ec,
69     const sdbusplus::message::unix_fd& unixfd)
70 {
71     if (ec.value() == EBADR)
72     {
73         messages::resourceNotFound(asyncResp->res, "EntryAttachment", entryID);
74         return;
75     }
76     if (ec)
77     {
78         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
79         messages::internalError(asyncResp->res);
80         return;
81     }
82 
83     // Make sure we know how to process the retrieved entry attachment
84     if ((downloadEntryType != "BMC") && (downloadEntryType != "System"))
85     {
86         BMCWEB_LOG_ERROR("downloadEntryCallback() invalid entry type: {}",
87                          downloadEntryType);
88         messages::internalError(asyncResp->res);
89     }
90 
91     int fd = -1;
92     fd = dup(unixfd);
93     if (fd < 0)
94     {
95         BMCWEB_LOG_ERROR("Failed to open file");
96         messages::internalError(asyncResp->res);
97         return;
98     }
99     if (!checkSizeLimit(fd, asyncResp->res))
100     {
101         close(fd);
102         return;
103     }
104     if (downloadEntryType == "System")
105     {
106         if (!asyncResp->res.openFd(fd, bmcweb::EncodingType::Base64))
107         {
108             messages::internalError(asyncResp->res);
109             close(fd);
110             return;
111         }
112         asyncResp->res.addHeader(
113             boost::beast::http::field::content_transfer_encoding, "Base64");
114         return;
115     }
116     if (!asyncResp->res.openFd(fd))
117     {
118         messages::internalError(asyncResp->res);
119         close(fd);
120         return;
121     }
122     asyncResp->res.addHeader(boost::beast::http::field::content_type,
123                              "application/octet-stream");
124 }
125 } // namespace log_services_utils
126 } // namespace redfish
127