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