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