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