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