Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 7531298 | 2021-02-11 14:26:02 -0800 | [diff] [blame] | 2 | #include "bmcweb_config.h" |
Adriana Kobylak | 0e1cf26 | 2019-12-05 13:57:57 -0600 | [diff] [blame] | 3 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 4 | #include "authorization.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 5 | #include "http_response.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 6 | #include "http_utility.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 7 | #include "logging.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 8 | #include "utility.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 10 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 11 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 12 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 13 | #include <boost/asio/ip/tcp.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 14 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 15 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 16 | #include <boost/beast/core/flat_static_buffer.hpp> |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 17 | #include <boost/beast/ssl/ssl_stream.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 18 | #include <boost/beast/websocket.hpp> |
Ed Tanous | d32c4fa | 2021-09-14 13:16:51 -0700 | [diff] [blame] | 19 | #include <boost/url/url_view.hpp> |
Ed Tanous | 57fce80 | 2019-05-21 13:00:34 -0700 | [diff] [blame] | 20 | #include <json_html_serializer.hpp> |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 21 | #include <security_headers.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 22 | #include <ssl_key_handler.hpp> |
| 23 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 24 | #include <atomic> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 25 | #include <chrono> |
| 26 | #include <vector> |
| 27 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | namespace crow |
| 29 | { |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 30 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | inline void prettyPrintJson(crow::Response& res) |
| 32 | { |
Ed Tanous | 57fce80 | 2019-05-21 13:00:34 -0700 | [diff] [blame] | 33 | json_html_util::dumpHtml(res.body(), res.jsonValue); |
| 34 | |
Ed Tanous | 93ef580 | 2019-01-03 10:15:41 -0800 | [diff] [blame] | 35 | res.addHeader("Content-Type", "text/html;charset=UTF-8"); |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 38 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 39 | static std::atomic<int> connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 40 | #endif |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 41 | |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 42 | // request body limit size set by the bmcwebHttpReqBodyLimitMb option |
Adriana Kobylak | 0e1cf26 | 2019-12-05 13:57:57 -0600 | [diff] [blame] | 43 | constexpr unsigned int httpReqBodyLimit = |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 44 | 1024 * 1024 * bmcwebHttpReqBodyLimitMb; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 45 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 46 | constexpr uint64_t loggedOutPostBodyLimit = 4096; |
| 47 | |
| 48 | constexpr uint32_t httpHeaderLimit = 8192; |
| 49 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 50 | template <typename Adaptor, typename Handler> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 51 | class Connection : |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 52 | public std::enable_shared_from_this<Connection<Adaptor, Handler>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | { |
| 54 | public: |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 55 | Connection(Handler* handlerIn, boost::asio::steady_timer&& timerIn, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 56 | std::function<std::string()>& getCachedDateStrF, |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 57 | Adaptor adaptorIn) : |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 58 | adaptor(std::move(adaptorIn)), |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 59 | handler(handlerIn), timer(std::move(timerIn)), |
| 60 | getCachedDateStr(getCachedDateStrF) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 61 | { |
| 62 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 63 | parser->body_limit(httpReqBodyLimit); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 64 | parser->header_limit(httpHeaderLimit); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 65 | |
| 66 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 67 | prepareMutualTls(); |
| 68 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 69 | |
| 70 | #ifdef BMCWEB_ENABLE_DEBUG |
| 71 | connectionCount++; |
| 72 | BMCWEB_LOG_DEBUG << this << " Connection open, total " |
| 73 | << connectionCount; |
| 74 | #endif |
| 75 | } |
| 76 | |
| 77 | ~Connection() |
| 78 | { |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 79 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 80 | cancelDeadlineTimer(); |
| 81 | #ifdef BMCWEB_ENABLE_DEBUG |
| 82 | connectionCount--; |
| 83 | BMCWEB_LOG_DEBUG << this << " Connection closed, total " |
| 84 | << connectionCount; |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | void prepareMutualTls() |
| 89 | { |
Jonathan Doman | 83deb7d | 2020-11-16 17:00:22 -0800 | [diff] [blame] | 90 | std::error_code error; |
| 91 | std::filesystem::path caPath(ensuressl::trustStorePath); |
| 92 | auto caAvailable = !std::filesystem::is_empty(caPath, error); |
| 93 | caAvailable = caAvailable && !error; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 94 | if (caAvailable && persistent_data::SessionStore::getInstance() |
| 95 | .getAuthMethodsConfig() |
| 96 | .tls) |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 97 | { |
| 98 | adaptor.set_verify_mode(boost::asio::ssl::verify_peer); |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 99 | std::string id = "bmcweb"; |
| 100 | int ret = SSL_set_session_id_context( |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 101 | adaptor.native_handle(), |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 102 | reinterpret_cast<const unsigned char*>(id.c_str()), |
| 103 | static_cast<unsigned int>(id.length())); |
| 104 | if (ret == 0) |
| 105 | { |
| 106 | BMCWEB_LOG_ERROR << this << " failed to set SSL id"; |
| 107 | } |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 110 | adaptor.set_verify_callback([this]( |
| 111 | bool preverified, |
| 112 | boost::asio::ssl::verify_context& ctx) { |
| 113 | // do nothing if TLS is disabled |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 114 | if (!persistent_data::SessionStore::getInstance() |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 115 | .getAuthMethodsConfig() |
| 116 | .tls) |
| 117 | { |
| 118 | BMCWEB_LOG_DEBUG << this << " TLS auth_config is disabled"; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 119 | return true; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // We always return true to allow full auth flow |
| 123 | if (!preverified) |
| 124 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 125 | BMCWEB_LOG_DEBUG << this << " TLS preverification failed."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | X509_STORE_CTX* cts = ctx.native_handle(); |
| 130 | if (cts == nullptr) |
| 131 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 132 | BMCWEB_LOG_DEBUG << this << " Cannot get native TLS handle."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
| 136 | // Get certificate |
| 137 | X509* peerCert = |
| 138 | X509_STORE_CTX_get_current_cert(ctx.native_handle()); |
| 139 | if (peerCert == nullptr) |
| 140 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 141 | BMCWEB_LOG_DEBUG << this |
| 142 | << " Cannot get current TLS certificate."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | |
| 146 | // Check if certificate is OK |
| 147 | int error = X509_STORE_CTX_get_error(cts); |
| 148 | if (error != X509_V_OK) |
| 149 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 150 | BMCWEB_LOG_INFO << this << " Last TLS error is: " << error; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | // Check that we have reached final certificate in chain |
| 154 | int32_t depth = X509_STORE_CTX_get_error_depth(cts); |
| 155 | if (depth != 0) |
| 156 | |
| 157 | { |
| 158 | BMCWEB_LOG_DEBUG |
| 159 | << this << " Certificate verification in progress (depth " |
| 160 | << depth << "), waiting to reach final depth"; |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | BMCWEB_LOG_DEBUG << this |
| 165 | << " Certificate verification of final depth"; |
| 166 | |
| 167 | // Verify KeyUsage |
| 168 | bool isKeyUsageDigitalSignature = false; |
| 169 | bool isKeyUsageKeyAgreement = false; |
| 170 | |
| 171 | ASN1_BIT_STRING* usage = static_cast<ASN1_BIT_STRING*>( |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 172 | X509_get_ext_d2i(peerCert, NID_key_usage, nullptr, nullptr)); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 173 | |
| 174 | if (usage == nullptr) |
| 175 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 176 | BMCWEB_LOG_DEBUG << this << " TLS usage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
| 180 | for (int i = 0; i < usage->length; i++) |
| 181 | { |
| 182 | if (KU_DIGITAL_SIGNATURE & usage->data[i]) |
| 183 | { |
| 184 | isKeyUsageDigitalSignature = true; |
| 185 | } |
| 186 | if (KU_KEY_AGREEMENT & usage->data[i]) |
| 187 | { |
| 188 | isKeyUsageKeyAgreement = true; |
| 189 | } |
| 190 | } |
Vernon Mauery | b937830 | 2021-06-16 14:06:57 -0700 | [diff] [blame] | 191 | ASN1_BIT_STRING_free(usage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 192 | |
| 193 | if (!isKeyUsageDigitalSignature || !isKeyUsageKeyAgreement) |
| 194 | { |
| 195 | BMCWEB_LOG_DEBUG << this |
| 196 | << " Certificate ExtendedKeyUsage does " |
| 197 | "not allow provided certificate to " |
| 198 | "be used for user authentication"; |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | // Determine that ExtendedKeyUsage includes Client Auth |
| 203 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 204 | stack_st_ASN1_OBJECT* extUsage = |
| 205 | static_cast<stack_st_ASN1_OBJECT*>(X509_get_ext_d2i( |
| 206 | peerCert, NID_ext_key_usage, nullptr, nullptr)); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 207 | |
| 208 | if (extUsage == nullptr) |
| 209 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 210 | BMCWEB_LOG_DEBUG << this << " TLS extUsage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | |
| 214 | bool isExKeyUsageClientAuth = false; |
| 215 | for (int i = 0; i < sk_ASN1_OBJECT_num(extUsage); i++) |
| 216 | { |
| 217 | if (NID_client_auth == |
| 218 | OBJ_obj2nid(sk_ASN1_OBJECT_value(extUsage, i))) |
| 219 | { |
| 220 | isExKeyUsageClientAuth = true; |
| 221 | break; |
| 222 | } |
| 223 | } |
Zbigniew Kurzynski | 09d02f8 | 2020-03-30 13:41:42 +0200 | [diff] [blame] | 224 | sk_ASN1_OBJECT_free(extUsage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 225 | |
| 226 | // Certificate has to have proper key usages set |
| 227 | if (!isExKeyUsageClientAuth) |
| 228 | { |
| 229 | BMCWEB_LOG_DEBUG << this |
| 230 | << " Certificate ExtendedKeyUsage does " |
| 231 | "not allow provided certificate to " |
| 232 | "be used for user authentication"; |
| 233 | return true; |
| 234 | } |
| 235 | std::string sslUser; |
| 236 | // Extract username contained in CommonName |
| 237 | sslUser.resize(256, '\0'); |
| 238 | |
| 239 | int status = X509_NAME_get_text_by_NID( |
| 240 | X509_get_subject_name(peerCert), NID_commonName, sslUser.data(), |
| 241 | static_cast<int>(sslUser.size())); |
| 242 | |
| 243 | if (status == -1) |
| 244 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 245 | BMCWEB_LOG_DEBUG |
| 246 | << this << " TLS cannot get username to create session"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
| 250 | size_t lastChar = sslUser.find('\0'); |
| 251 | if (lastChar == std::string::npos || lastChar == 0) |
| 252 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 253 | BMCWEB_LOG_DEBUG << this << " Invalid TLS user name"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 254 | return true; |
| 255 | } |
| 256 | sslUser.resize(lastChar); |
Sunitha Harish | d323922 | 2021-02-24 15:33:29 +0530 | [diff] [blame] | 257 | std::string unsupportedClientId = ""; |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 258 | sessionIsFromTransport = true; |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 259 | userSession = persistent_data::SessionStore::getInstance() |
| 260 | .generateUserSession( |
Jiaqing Zhao | 41d61c8 | 2021-12-07 13:21:47 +0800 | [diff] [blame] | 261 | sslUser, req->ipAddress, unsupportedClientId, |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 262 | persistent_data::PersistenceType::TIMEOUT); |
| 263 | if (userSession != nullptr) |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 264 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 265 | BMCWEB_LOG_DEBUG |
| 266 | << this |
| 267 | << " Generating TLS session: " << userSession->uniqueId; |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 268 | } |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 269 | return true; |
| 270 | }); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 273 | Adaptor& socket() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 274 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 275 | return adaptor; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 278 | void start() |
| 279 | { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 280 | startDeadline(); |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 281 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 282 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 283 | // asynchronous "start" |
| 284 | if constexpr (std::is_same_v<Adaptor, |
| 285 | boost::beast::ssl_stream< |
| 286 | boost::asio::ip::tcp::socket>>) |
| 287 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 288 | adaptor.async_handshake(boost::asio::ssl::stream_base::server, |
| 289 | [this, self(shared_from_this())]( |
| 290 | const boost::system::error_code& ec) { |
| 291 | if (ec) |
| 292 | { |
| 293 | return; |
| 294 | } |
| 295 | doReadHeaders(); |
| 296 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 297 | } |
| 298 | else |
| 299 | { |
| 300 | doReadHeaders(); |
| 301 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 302 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 303 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 304 | void handle() |
| 305 | { |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 306 | std::error_code reqEc; |
| 307 | crow::Request& thisReq = req.emplace(parser->release(), reqEc); |
| 308 | if (reqEc) |
| 309 | { |
| 310 | BMCWEB_LOG_DEBUG << "Request failed to construct" << reqEc; |
| 311 | return; |
| 312 | } |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 313 | thisReq.session = userSession; |
| 314 | |
Ivan Mikhaylov | f65b0be | 2021-04-19 10:05:30 +0000 | [diff] [blame] | 315 | // Fetch the client IP address |
| 316 | readClientIp(); |
| 317 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 318 | // Check for HTTP version 1.1. |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 319 | if (thisReq.version() == 11) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 320 | { |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 321 | if (thisReq.getHeaderValue(boost::beast::http::field::host).empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 322 | { |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 323 | res.result(boost::beast::http::status::bad_request); |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 324 | completeRequest(); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 325 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 328 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 329 | BMCWEB_LOG_INFO << "Request: " |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 330 | << " " << this << " HTTP/" << thisReq.version() / 10 |
| 331 | << "." << thisReq.version() % 10 << ' ' |
| 332 | << thisReq.methodString() << " " << thisReq.target() |
| 333 | << " " << thisReq.ipAddress; |
Ed Tanous | d32c4fa | 2021-09-14 13:16:51 -0700 | [diff] [blame] | 334 | |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 335 | res.setCompleteRequestHandler(nullptr); |
| 336 | res.isAliveHelper = [this]() -> bool { return isAlive(); }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 337 | |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 338 | thisReq.ioService = static_cast<decltype(thisReq.ioService)>( |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 339 | &adaptor.get_executor().context()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 340 | |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 341 | if (res.completed) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 342 | { |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 343 | completeRequest(); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 344 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 345 | } |
Nan Zhou | 8682c5a | 2021-11-13 11:00:07 -0800 | [diff] [blame] | 346 | #ifndef BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
Ed Tanous | efee36b | 2021-09-22 19:09:11 -0700 | [diff] [blame] | 347 | if (!crow::authorization::isOnAllowlist(req->url, req->method()) && |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 348 | thisReq.session == nullptr) |
| 349 | { |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 350 | BMCWEB_LOG_WARNING << "[AuthMiddleware] authorization failed"; |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 351 | forward_unauthorized::sendUnauthorized( |
| 352 | req->url, req->getHeaderValue("User-Agent"), |
| 353 | req->getHeaderValue("Accept"), res); |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 354 | completeRequest(); |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 355 | return; |
| 356 | } |
Nan Zhou | 8682c5a | 2021-11-13 11:00:07 -0800 | [diff] [blame] | 357 | #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 358 | res.setCompleteRequestHandler([self(shared_from_this())] { |
| 359 | boost::asio::post(self->adaptor.get_executor(), |
| 360 | [self] { self->completeRequest(); }); |
| 361 | }); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 362 | |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 363 | if (thisReq.isUpgrade() && |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 364 | boost::iequals( |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 365 | thisReq.getHeaderValue(boost::beast::http::field::upgrade), |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 366 | "websocket")) |
| 367 | { |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 368 | handler->handleUpgrade(thisReq, res, std::move(adaptor)); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 369 | // delete lambda with self shared_ptr |
| 370 | // to enable connection destruction |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 371 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 372 | return; |
| 373 | } |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 374 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res); |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 375 | handler->handle(thisReq, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 376 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 377 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 378 | bool isAlive() |
| 379 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 380 | if constexpr (std::is_same_v<Adaptor, |
| 381 | boost::beast::ssl_stream< |
| 382 | boost::asio::ip::tcp::socket>>) |
| 383 | { |
| 384 | return adaptor.next_layer().is_open(); |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | return adaptor.is_open(); |
| 389 | } |
| 390 | } |
| 391 | void close() |
| 392 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 393 | if constexpr (std::is_same_v<Adaptor, |
| 394 | boost::beast::ssl_stream< |
| 395 | boost::asio::ip::tcp::socket>>) |
| 396 | { |
| 397 | adaptor.next_layer().close(); |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 398 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 399 | if (userSession != nullptr) |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 400 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 401 | BMCWEB_LOG_DEBUG |
| 402 | << this |
| 403 | << " Removing TLS session: " << userSession->uniqueId; |
| 404 | persistent_data::SessionStore::getInstance().removeSession( |
| 405 | userSession); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 406 | } |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 407 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 408 | } |
| 409 | else |
| 410 | { |
| 411 | adaptor.close(); |
| 412 | } |
| 413 | } |
| 414 | |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 415 | void completeRequest() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 416 | { |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 417 | if (!req) |
| 418 | { |
| 419 | return; |
| 420 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 421 | BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' |
| 422 | << res.resultInt() << " keepalive=" << req->keepAlive(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 423 | |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 424 | addSecurityHeaders(*req, res); |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 425 | |
Ed Tanous | cf099fa | 2021-08-25 12:37:31 -0700 | [diff] [blame] | 426 | crow::authorization::cleanupTempSession(*req); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 427 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 428 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 429 | { |
| 430 | // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << |
| 431 | // isReading |
| 432 | // << ' ' << isWriting; |
| 433 | // delete this; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 434 | |
| 435 | // delete lambda with self shared_ptr |
| 436 | // to enable connection destruction |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 437 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | if (res.body().empty() && !res.jsonValue.empty()) |
| 441 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 442 | if (http_helpers::requestPrefersHtml(req->getHeaderValue("Accept"))) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 443 | { |
| 444 | prettyPrintJson(res); |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | res.jsonMode(); |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 449 | res.body() = res.jsonValue.dump( |
| 450 | 2, ' ', true, nlohmann::json::error_handler_t::replace); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 453 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 454 | if (res.resultInt() >= 400 && res.body().empty()) |
| 455 | { |
| 456 | res.body() = std::string(res.reason()); |
| 457 | } |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 458 | |
| 459 | if (res.result() == boost::beast::http::status::no_content) |
| 460 | { |
| 461 | // Boost beast throws if content is provided on a no-content |
| 462 | // response. Ideally, this would never happen, but in the case that |
| 463 | // it does, we don't want to throw. |
| 464 | BMCWEB_LOG_CRITICAL |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 465 | << this << " Response content provided but code was no-content"; |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 466 | res.body().clear(); |
| 467 | } |
| 468 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 469 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
| 470 | |
| 471 | res.keepAlive(req->keepAlive()); |
| 472 | |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 473 | doWrite(); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 474 | |
| 475 | // delete lambda with self shared_ptr |
| 476 | // to enable connection destruction |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 477 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 480 | void readClientIp() |
| 481 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 482 | boost::asio::ip::address ip; |
| 483 | boost::system::error_code ec = getClientIp(ip); |
| 484 | if (ec) |
| 485 | { |
| 486 | return; |
| 487 | } |
| 488 | req->ipAddress = ip; |
| 489 | } |
| 490 | |
| 491 | boost::system::error_code getClientIp(boost::asio::ip::address& ip) |
| 492 | { |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 493 | boost::system::error_code ec; |
| 494 | BMCWEB_LOG_DEBUG << "Fetch the client IP address"; |
| 495 | boost::asio::ip::tcp::endpoint endpoint = |
| 496 | boost::beast::get_lowest_layer(adaptor).remote_endpoint(ec); |
| 497 | |
| 498 | if (ec) |
| 499 | { |
| 500 | // If remote endpoint fails keep going. "ClientOriginIPAddress" |
| 501 | // will be empty. |
| 502 | BMCWEB_LOG_ERROR << "Failed to get the client's IP Address. ec : " |
| 503 | << ec; |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 504 | return ec; |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 505 | } |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 506 | ip = endpoint.address(); |
| 507 | return ec; |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 508 | } |
| 509 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 510 | private: |
| 511 | void doReadHeaders() |
| 512 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 513 | BMCWEB_LOG_DEBUG << this << " doReadHeaders"; |
| 514 | |
| 515 | // Clean up any previous Connection. |
| 516 | boost::beast::http::async_read_header( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 517 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 518 | [this, |
| 519 | self(shared_from_this())](const boost::system::error_code& ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 520 | std::size_t bytesTransferred) { |
Andrew Geissler | 1c801e1 | 2021-10-08 14:36:01 -0500 | [diff] [blame] | 521 | BMCWEB_LOG_DEBUG << this << " async_read_header " |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 522 | << bytesTransferred << " Bytes"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 523 | bool errorWhileReading = false; |
| 524 | if (ec) |
| 525 | { |
| 526 | errorWhileReading = true; |
Andrew Geissler | 1c801e1 | 2021-10-08 14:36:01 -0500 | [diff] [blame] | 527 | if (ec == boost::asio::error::eof) |
| 528 | { |
| 529 | BMCWEB_LOG_WARNING |
| 530 | << this << " Error while reading: " << ec.message(); |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | BMCWEB_LOG_ERROR |
| 535 | << this << " Error while reading: " << ec.message(); |
| 536 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 537 | } |
| 538 | else |
| 539 | { |
| 540 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 541 | // websocket, treat as an error |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 542 | if (!isAlive() && |
| 543 | !boost::beast::websocket::is_upgrade(parser->get())) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 544 | { |
| 545 | errorWhileReading = true; |
| 546 | } |
| 547 | } |
| 548 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 549 | cancelDeadlineTimer(); |
| 550 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 551 | if (errorWhileReading) |
| 552 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 553 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 554 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 558 | readClientIp(); |
Ed Tanous | 92ccb88 | 2020-08-18 10:36:33 -0700 | [diff] [blame] | 559 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 560 | boost::asio::ip::address ip; |
| 561 | if (getClientIp(ip)) |
| 562 | { |
| 563 | BMCWEB_LOG_DEBUG << "Unable to get client IP"; |
| 564 | } |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 565 | sessionIsFromTransport = false; |
Nan Zhou | 8682c5a | 2021-11-13 11:00:07 -0800 | [diff] [blame] | 566 | #ifndef BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
| 567 | boost::beast::http::verb method = parser->get().method(); |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 568 | userSession = crow::authorization::authenticate( |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 569 | ip, res, method, parser->get().base(), userSession); |
Nan Zhou | 8682c5a | 2021-11-13 11:00:07 -0800 | [diff] [blame] | 570 | #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 571 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 572 | bool loggedIn = userSession != nullptr; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 573 | if (!loggedIn) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 574 | { |
| 575 | const boost::optional<uint64_t> contentLength = |
| 576 | parser->content_length(); |
| 577 | if (contentLength && |
| 578 | *contentLength > loggedOutPostBodyLimit) |
| 579 | { |
| 580 | BMCWEB_LOG_DEBUG << "Content length greater than limit " |
| 581 | << *contentLength; |
| 582 | close(); |
| 583 | return; |
| 584 | } |
| 585 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 586 | BMCWEB_LOG_DEBUG << "Starting quick deadline"; |
| 587 | } |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 588 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 589 | doRead(); |
| 590 | }); |
| 591 | } |
| 592 | |
| 593 | void doRead() |
| 594 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 595 | BMCWEB_LOG_DEBUG << this << " doRead"; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 596 | startDeadline(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 597 | boost::beast::http::async_read( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 598 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 599 | [this, |
| 600 | self(shared_from_this())](const boost::system::error_code& ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 601 | std::size_t bytesTransferred) { |
| 602 | BMCWEB_LOG_DEBUG << this << " async_read " << bytesTransferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 603 | << " Bytes"; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 604 | cancelDeadlineTimer(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 605 | if (ec) |
| 606 | { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 607 | BMCWEB_LOG_ERROR |
| 608 | << this << " Error while reading: " << ec.message(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 609 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 610 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | handle(); |
| 614 | }); |
| 615 | } |
| 616 | |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 617 | void doWrite() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 618 | { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 619 | BMCWEB_LOG_DEBUG << this << " doWrite"; |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 620 | res.preparePayload(); |
| 621 | serializer.emplace(*res.stringResponse); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 622 | startDeadline(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 623 | boost::beast::http::async_write( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 624 | adaptor, *serializer, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 625 | [this, |
| 626 | self(shared_from_this())](const boost::system::error_code& ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 627 | std::size_t bytesTransferred) { |
| 628 | BMCWEB_LOG_DEBUG << this << " async_write " << bytesTransferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 629 | << " bytes"; |
| 630 | |
James Feist | 54d8bb1 | 2020-07-20 13:28:59 -0700 | [diff] [blame] | 631 | cancelDeadlineTimer(); |
| 632 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 633 | if (ec) |
| 634 | { |
| 635 | BMCWEB_LOG_DEBUG << this << " from write(2)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 636 | return; |
| 637 | } |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 638 | if (!res.keepAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 639 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 640 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 641 | BMCWEB_LOG_DEBUG << this << " from write(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 642 | return; |
| 643 | } |
| 644 | |
| 645 | serializer.reset(); |
| 646 | BMCWEB_LOG_DEBUG << this << " Clearing response"; |
| 647 | res.clear(); |
| 648 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Gunnar Mills | ded2a1e | 2020-07-24 09:46:33 -0500 | [diff] [blame] | 649 | parser->body_limit(httpReqBodyLimit); // reset body limit for |
| 650 | // newly created parser |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 651 | buffer.consume(buffer.size()); |
| 652 | |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 653 | // If the session was built from the transport, we don't need to |
| 654 | // clear it. All other sessions are generated per request. |
| 655 | if (!sessionIsFromTransport) |
| 656 | { |
| 657 | userSession = nullptr; |
| 658 | } |
| 659 | |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 660 | // Destroy the Request via the std::optional |
| 661 | req.reset(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 662 | doReadHeaders(); |
| 663 | }); |
| 664 | } |
| 665 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 666 | void cancelDeadlineTimer() |
| 667 | { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 668 | timer.cancel(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 671 | void startDeadline() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 672 | { |
| 673 | cancelDeadlineTimer(); |
| 674 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 675 | std::chrono::seconds timeout(15); |
| 676 | // allow slow uploads for logged in users |
| 677 | bool loggedIn = req && req->session; |
| 678 | if (loggedIn) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 679 | { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 680 | timeout = std::chrono::seconds(60); |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 681 | return; |
| 682 | } |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 683 | |
| 684 | std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this(); |
| 685 | timer.expires_after(timeout); |
| 686 | timer.async_wait([weakSelf](const boost::system::error_code ec) { |
| 687 | // Note, we are ignoring other types of errors here; If the timer |
| 688 | // failed for any reason, we should still close the connection |
| 689 | |
| 690 | std::shared_ptr<Connection<Adaptor, Handler>> self = |
| 691 | weakSelf.lock(); |
| 692 | if (!self) |
| 693 | { |
| 694 | BMCWEB_LOG_CRITICAL << self << " Failed to capture connection"; |
| 695 | return; |
| 696 | } |
| 697 | if (ec == boost::asio::error::operation_aborted) |
| 698 | { |
| 699 | // Canceled wait means the path succeeeded. |
| 700 | return; |
| 701 | } |
| 702 | if (ec) |
| 703 | { |
| 704 | BMCWEB_LOG_CRITICAL << self << " timer failed " << ec; |
| 705 | } |
| 706 | |
| 707 | BMCWEB_LOG_WARNING << self << "Connection timed out, closing"; |
| 708 | |
| 709 | self->close(); |
| 710 | }); |
| 711 | |
| 712 | BMCWEB_LOG_DEBUG << this << " timer started"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | private: |
| 716 | Adaptor adaptor; |
| 717 | Handler* handler; |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 718 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 719 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 720 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 721 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 722 | parser; |
| 723 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 724 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 725 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 726 | std::optional<boost::beast::http::response_serializer< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 727 | boost::beast::http::string_body>> |
| 728 | serializer; |
| 729 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 730 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 731 | crow::Response res; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 732 | |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 733 | bool sessionIsFromTransport = false; |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 734 | std::shared_ptr<persistent_data::UserSession> userSession; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 735 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 736 | boost::asio::steady_timer timer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 737 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 738 | std::function<std::string()>& getCachedDateStr; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 739 | |
| 740 | using std::enable_shared_from_this< |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 741 | Connection<Adaptor, Handler>>::shared_from_this; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 742 | |
| 743 | using std::enable_shared_from_this< |
| 744 | Connection<Adaptor, Handler>>::weak_from_this; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 745 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 746 | } // namespace crow |