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