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 | |
Nan Zhou | d055a34 | 2022-05-25 01:15:34 +0000 | [diff] [blame] | 4 | #include "authentication.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" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 8 | #include "utility.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 10 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 11 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 12 | #include <boost/asio/ip/tcp.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 13 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 14 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 15 | #include <boost/beast/core/flat_static_buffer.hpp> |
Ed Tanous | 918ef25 | 2022-05-25 10:40:41 -0700 | [diff] [blame] | 16 | #include <boost/beast/http/parser.hpp> |
| 17 | #include <boost/beast/http/read.hpp> |
| 18 | #include <boost/beast/http/serializer.hpp> |
| 19 | #include <boost/beast/http/write.hpp> |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 20 | #include <boost/beast/ssl/ssl_stream.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 21 | #include <boost/beast/websocket.hpp> |
Ed Tanous | d32c4fa | 2021-09-14 13:16:51 -0700 | [diff] [blame] | 22 | #include <boost/url/url_view.hpp> |
Ed Tanous | 57fce80 | 2019-05-21 13:00:34 -0700 | [diff] [blame] | 23 | #include <json_html_serializer.hpp> |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 24 | #include <security_headers.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 25 | #include <ssl_key_handler.hpp> |
| 26 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 27 | #include <atomic> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 28 | #include <chrono> |
| 29 | #include <vector> |
| 30 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | namespace crow |
| 32 | { |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | inline void prettyPrintJson(crow::Response& res) |
| 35 | { |
Ed Tanous | 57fce80 | 2019-05-21 13:00:34 -0700 | [diff] [blame] | 36 | json_html_util::dumpHtml(res.body(), res.jsonValue); |
| 37 | |
Ed Tanous | 93ef580 | 2019-01-03 10:15:41 -0800 | [diff] [blame] | 38 | res.addHeader("Content-Type", "text/html;charset=UTF-8"); |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 41 | static int connectionCount = 0; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 42 | |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 43 | // request body limit size set by the bmcwebHttpReqBodyLimitMb option |
Ed Tanous | 6de264c | 2022-01-06 12:47:59 -0800 | [diff] [blame] | 44 | constexpr uint64_t httpReqBodyLimit = |
| 45 | 1024UL * 1024UL * bmcwebHttpReqBodyLimitMb; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 46 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 47 | constexpr uint64_t loggedOutPostBodyLimit = 4096; |
| 48 | |
| 49 | constexpr uint32_t httpHeaderLimit = 8192; |
| 50 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 51 | template <typename Adaptor, typename Handler> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 52 | class Connection : |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 53 | public std::enable_shared_from_this<Connection<Adaptor, Handler>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 54 | { |
| 55 | public: |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 56 | Connection(Handler* handlerIn, boost::asio::steady_timer&& timerIn, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 57 | std::function<std::string()>& getCachedDateStrF, |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 58 | Adaptor adaptorIn) : |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 59 | adaptor(std::move(adaptorIn)), |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 60 | handler(handlerIn), timer(std::move(timerIn)), |
| 61 | getCachedDateStr(getCachedDateStrF) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | { |
| 63 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | parser->body_limit(httpReqBodyLimit); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 65 | parser->header_limit(httpHeaderLimit); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 66 | |
| 67 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 68 | prepareMutualTls(); |
| 69 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 70 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 71 | connectionCount++; |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 72 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 73 | BMCWEB_LOG_DEBUG << this << " Connection open, total " |
| 74 | << connectionCount; |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | ~Connection() |
| 78 | { |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 79 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 80 | cancelDeadlineTimer(); |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 81 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 82 | connectionCount--; |
| 83 | BMCWEB_LOG_DEBUG << this << " Connection closed, total " |
| 84 | << connectionCount; |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 87 | Connection(const Connection&) = delete; |
| 88 | Connection(Connection&&) = delete; |
| 89 | Connection& operator=(const Connection&) = delete; |
| 90 | Connection& operator=(Connection&&) = delete; |
| 91 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 92 | void prepareMutualTls() |
| 93 | { |
Jonathan Doman | 83deb7d | 2020-11-16 17:00:22 -0800 | [diff] [blame] | 94 | std::error_code error; |
| 95 | std::filesystem::path caPath(ensuressl::trustStorePath); |
| 96 | auto caAvailable = !std::filesystem::is_empty(caPath, error); |
| 97 | caAvailable = caAvailable && !error; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 98 | if (caAvailable && persistent_data::SessionStore::getInstance() |
| 99 | .getAuthMethodsConfig() |
| 100 | .tls) |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 101 | { |
| 102 | adaptor.set_verify_mode(boost::asio::ssl::verify_peer); |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 103 | std::string id = "bmcweb"; |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 104 | |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 105 | const char* cStr = id.c_str(); |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 106 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 107 | const auto* idC = reinterpret_cast<const unsigned char*>(cStr); |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 108 | int ret = SSL_set_session_id_context( |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 109 | adaptor.native_handle(), idC, |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 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 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 117 | adaptor.set_verify_callback( |
| 118 | [this](bool preverified, boost::asio::ssl::verify_context& ctx) { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 119 | // do nothing if TLS is disabled |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 120 | if (!persistent_data::SessionStore::getInstance() |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 121 | .getAuthMethodsConfig() |
| 122 | .tls) |
| 123 | { |
| 124 | BMCWEB_LOG_DEBUG << this << " TLS auth_config is disabled"; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 125 | return true; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // We always return true to allow full auth flow |
| 129 | if (!preverified) |
| 130 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 131 | BMCWEB_LOG_DEBUG << this << " TLS preverification failed."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
| 135 | X509_STORE_CTX* cts = ctx.native_handle(); |
| 136 | if (cts == nullptr) |
| 137 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 138 | BMCWEB_LOG_DEBUG << this << " Cannot get native TLS handle."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 139 | return true; |
| 140 | } |
| 141 | |
| 142 | // Get certificate |
| 143 | X509* peerCert = |
| 144 | X509_STORE_CTX_get_current_cert(ctx.native_handle()); |
| 145 | if (peerCert == nullptr) |
| 146 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 147 | BMCWEB_LOG_DEBUG << this |
| 148 | << " Cannot get current TLS certificate."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
| 152 | // Check if certificate is OK |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 153 | int ctxError = X509_STORE_CTX_get_error(cts); |
| 154 | if (ctxError != X509_V_OK) |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 155 | { |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 156 | BMCWEB_LOG_INFO << this << " Last TLS error is: " << ctxError; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | // Check that we have reached final certificate in chain |
| 160 | int32_t depth = X509_STORE_CTX_get_error_depth(cts); |
| 161 | if (depth != 0) |
| 162 | |
| 163 | { |
| 164 | BMCWEB_LOG_DEBUG |
| 165 | << this << " Certificate verification in progress (depth " |
| 166 | << depth << "), waiting to reach final depth"; |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | BMCWEB_LOG_DEBUG << this |
| 171 | << " Certificate verification of final depth"; |
| 172 | |
| 173 | // Verify KeyUsage |
| 174 | bool isKeyUsageDigitalSignature = false; |
| 175 | bool isKeyUsageKeyAgreement = false; |
| 176 | |
| 177 | ASN1_BIT_STRING* usage = static_cast<ASN1_BIT_STRING*>( |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 178 | X509_get_ext_d2i(peerCert, NID_key_usage, nullptr, nullptr)); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 179 | |
| 180 | if (usage == nullptr) |
| 181 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 182 | BMCWEB_LOG_DEBUG << this << " TLS usage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | |
| 186 | for (int i = 0; i < usage->length; i++) |
| 187 | { |
Ed Tanous | ca45aa3 | 2022-01-07 09:28:45 -0800 | [diff] [blame] | 188 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 189 | unsigned char usageChar = usage->data[i]; |
| 190 | if (KU_DIGITAL_SIGNATURE & usageChar) |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 191 | { |
| 192 | isKeyUsageDigitalSignature = true; |
| 193 | } |
Ed Tanous | ca45aa3 | 2022-01-07 09:28:45 -0800 | [diff] [blame] | 194 | if (KU_KEY_AGREEMENT & usageChar) |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 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 | { |
Ed Tanous | 4bac4a8 | 2022-01-07 09:37:55 -0800 | [diff] [blame] | 225 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) |
| 226 | int nid = OBJ_obj2nid(sk_ASN1_OBJECT_value(extUsage, i)); |
| 227 | if (NID_client_auth == nid) |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 228 | { |
| 229 | isExKeyUsageClientAuth = true; |
| 230 | break; |
| 231 | } |
| 232 | } |
Zbigniew Kurzynski | 09d02f8 | 2020-03-30 13:41:42 +0200 | [diff] [blame] | 233 | sk_ASN1_OBJECT_free(extUsage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 234 | |
| 235 | // Certificate has to have proper key usages set |
| 236 | if (!isExKeyUsageClientAuth) |
| 237 | { |
| 238 | BMCWEB_LOG_DEBUG << this |
| 239 | << " Certificate ExtendedKeyUsage does " |
| 240 | "not allow provided certificate to " |
| 241 | "be used for user authentication"; |
| 242 | return true; |
| 243 | } |
| 244 | std::string sslUser; |
| 245 | // Extract username contained in CommonName |
| 246 | sslUser.resize(256, '\0'); |
| 247 | |
| 248 | int status = X509_NAME_get_text_by_NID( |
| 249 | X509_get_subject_name(peerCert), NID_commonName, sslUser.data(), |
| 250 | static_cast<int>(sslUser.size())); |
| 251 | |
| 252 | if (status == -1) |
| 253 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 254 | BMCWEB_LOG_DEBUG |
| 255 | << this << " TLS cannot get username to create session"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 256 | return true; |
| 257 | } |
| 258 | |
| 259 | size_t lastChar = sslUser.find('\0'); |
| 260 | if (lastChar == std::string::npos || lastChar == 0) |
| 261 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 262 | BMCWEB_LOG_DEBUG << this << " Invalid TLS user name"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | sslUser.resize(lastChar); |
Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 266 | std::string unsupportedClientId; |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 267 | sessionIsFromTransport = true; |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 268 | userSession = persistent_data::SessionStore::getInstance() |
| 269 | .generateUserSession( |
Jiaqing Zhao | 41d61c8 | 2021-12-07 13:21:47 +0800 | [diff] [blame] | 270 | sslUser, req->ipAddress, unsupportedClientId, |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 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 | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 289 | if (connectionCount >= 100) |
| 290 | { |
| 291 | BMCWEB_LOG_CRITICAL << this << "Max connection count exceeded."; |
| 292 | return; |
| 293 | } |
| 294 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 295 | startDeadline(); |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 296 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 297 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 298 | // asynchronous "start" |
| 299 | if constexpr (std::is_same_v<Adaptor, |
| 300 | boost::beast::ssl_stream< |
| 301 | boost::asio::ip::tcp::socket>>) |
| 302 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 303 | adaptor.async_handshake(boost::asio::ssl::stream_base::server, |
| 304 | [this, self(shared_from_this())]( |
| 305 | const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 306 | if (ec) |
| 307 | { |
| 308 | return; |
| 309 | } |
| 310 | doReadHeaders(); |
| 311 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 312 | } |
| 313 | else |
| 314 | { |
| 315 | doReadHeaders(); |
| 316 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 317 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 318 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 319 | void handle() |
| 320 | { |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 321 | std::error_code reqEc; |
| 322 | crow::Request& thisReq = req.emplace(parser->release(), reqEc); |
| 323 | if (reqEc) |
| 324 | { |
| 325 | BMCWEB_LOG_DEBUG << "Request failed to construct" << reqEc; |
| 326 | return; |
| 327 | } |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 328 | thisReq.session = userSession; |
| 329 | |
Ivan Mikhaylov | f65b0be | 2021-04-19 10:05:30 +0000 | [diff] [blame] | 330 | // Fetch the client IP address |
| 331 | readClientIp(); |
| 332 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 333 | // Check for HTTP version 1.1. |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 334 | if (thisReq.version() == 11) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 335 | { |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 336 | if (thisReq.getHeaderValue(boost::beast::http::field::host).empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 337 | { |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 338 | res.result(boost::beast::http::status::bad_request); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 339 | completeRequest(res); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 340 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 343 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 344 | BMCWEB_LOG_INFO << "Request: " |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 345 | << " " << this << " HTTP/" << thisReq.version() / 10 |
| 346 | << "." << thisReq.version() % 10 << ' ' |
| 347 | << thisReq.methodString() << " " << thisReq.target() |
Ed Tanous | 8cc8ede | 2022-02-28 10:20:59 -0800 | [diff] [blame] | 348 | << " " << thisReq.ipAddress.to_string(); |
Ed Tanous | d32c4fa | 2021-09-14 13:16:51 -0700 | [diff] [blame] | 349 | |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 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 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 357 | completeRequest(res); |
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 | } |
Nan Zhou | a43ea82 | 2022-05-27 00:42:44 +0000 | [diff] [blame] | 360 | #ifndef BMCWEB_INSECURE_DISABLE_AUTHX |
Nan Zhou | d055a34 | 2022-05-25 01:15:34 +0000 | [diff] [blame] | 361 | if (!crow::authentication::isOnAllowlist(req->url, req->method()) && |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 362 | thisReq.session == nullptr) |
| 363 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 364 | BMCWEB_LOG_WARNING << "Authentication failed"; |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 365 | forward_unauthorized::sendUnauthorized( |
Ed Tanous | c127a0f | 2022-05-11 15:23:59 -0700 | [diff] [blame] | 366 | req->url, req->getHeaderValue("X-Requested-With"), |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 367 | req->getHeaderValue("Accept"), res); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 368 | completeRequest(res); |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 369 | return; |
| 370 | } |
Nan Zhou | a43ea82 | 2022-05-27 00:42:44 +0000 | [diff] [blame] | 371 | #endif // BMCWEB_INSECURE_DISABLE_AUTHX |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 372 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); |
| 373 | BMCWEB_LOG_DEBUG << "Setting completion handler"; |
| 374 | asyncResp->res.setCompleteRequestHandler( |
| 375 | [self(shared_from_this())](crow::Response& thisRes) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 376 | self->completeRequest(thisRes); |
| 377 | }); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 378 | |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 379 | if (thisReq.isUpgrade() && |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 380 | boost::iequals( |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 381 | thisReq.getHeaderValue(boost::beast::http::field::upgrade), |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 382 | "websocket")) |
| 383 | { |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 384 | handler->handleUpgrade(thisReq, res, std::move(adaptor)); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 385 | // delete lambda with self shared_ptr |
| 386 | // to enable connection destruction |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 387 | asyncResp->res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 388 | return; |
| 389 | } |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 390 | handler->handle(thisReq, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 391 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 392 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 393 | bool isAlive() |
| 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 | return adaptor.next_layer().is_open(); |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | return adaptor.is_open(); |
| 404 | } |
| 405 | } |
| 406 | void close() |
| 407 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 408 | if constexpr (std::is_same_v<Adaptor, |
| 409 | boost::beast::ssl_stream< |
| 410 | boost::asio::ip::tcp::socket>>) |
| 411 | { |
| 412 | adaptor.next_layer().close(); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 413 | if (sessionIsFromTransport && userSession != nullptr) |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 414 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 415 | BMCWEB_LOG_DEBUG |
| 416 | << this |
| 417 | << " Removing TLS session: " << userSession->uniqueId; |
| 418 | persistent_data::SessionStore::getInstance().removeSession( |
| 419 | userSession); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 420 | } |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 421 | } |
| 422 | else |
| 423 | { |
| 424 | adaptor.close(); |
| 425 | } |
| 426 | } |
| 427 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 428 | void completeRequest(crow::Response& thisRes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 429 | { |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 430 | if (!req) |
| 431 | { |
| 432 | return; |
| 433 | } |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 434 | res = std::move(thisRes); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 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 | |
Nan Zhou | d055a34 | 2022-05-25 01:15:34 +0000 | [diff] [blame] | 440 | crow::authentication::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 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 487 | doWrite(res); |
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 | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 535 | BMCWEB_LOG_DEBUG << this << " async_read_header " |
| 536 | << bytesTransferred << " Bytes"; |
| 537 | bool errorWhileReading = false; |
| 538 | if (ec) |
| 539 | { |
| 540 | errorWhileReading = true; |
| 541 | if (ec == boost::asio::error::eof) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 542 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 543 | BMCWEB_LOG_WARNING |
| 544 | << this << " Error while reading: " << ec.message(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 545 | } |
| 546 | else |
| 547 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 548 | BMCWEB_LOG_ERROR |
| 549 | << this << " Error while reading: " << ec.message(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 550 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 551 | } |
| 552 | else |
| 553 | { |
| 554 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 555 | // websocket, treat as an error |
| 556 | if (!isAlive() && |
| 557 | !boost::beast::websocket::is_upgrade(parser->get())) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 558 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 559 | errorWhileReading = true; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | cancelDeadlineTimer(); |
| 564 | |
| 565 | if (errorWhileReading) |
| 566 | { |
| 567 | close(); |
| 568 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | readClientIp(); |
| 573 | |
| 574 | boost::asio::ip::address ip; |
| 575 | if (getClientIp(ip)) |
| 576 | { |
| 577 | BMCWEB_LOG_DEBUG << "Unable to get client IP"; |
| 578 | } |
| 579 | sessionIsFromTransport = false; |
| 580 | #ifndef BMCWEB_INSECURE_DISABLE_AUTHX |
| 581 | boost::beast::http::verb method = parser->get().method(); |
| 582 | userSession = crow::authentication::authenticate( |
| 583 | ip, res, method, parser->get().base(), userSession); |
| 584 | |
| 585 | bool loggedIn = userSession != nullptr; |
| 586 | if (!loggedIn) |
| 587 | { |
| 588 | const boost::optional<uint64_t> contentLength = |
| 589 | parser->content_length(); |
| 590 | if (contentLength && *contentLength > loggedOutPostBodyLimit) |
| 591 | { |
| 592 | BMCWEB_LOG_DEBUG << "Content length greater than limit " |
| 593 | << *contentLength; |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 594 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 598 | BMCWEB_LOG_DEBUG << "Starting quick deadline"; |
| 599 | } |
Nan Zhou | a43ea82 | 2022-05-27 00:42:44 +0000 | [diff] [blame] | 600 | #endif // BMCWEB_INSECURE_DISABLE_AUTHX |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 601 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 602 | doRead(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 603 | }); |
| 604 | } |
| 605 | |
| 606 | void doRead() |
| 607 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 608 | BMCWEB_LOG_DEBUG << this << " doRead"; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 609 | startDeadline(); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 610 | boost::beast::http::async_read(adaptor, buffer, *parser, |
| 611 | [this, self(shared_from_this())]( |
| 612 | const boost::system::error_code& ec, |
| 613 | std::size_t bytesTransferred) { |
| 614 | BMCWEB_LOG_DEBUG << this << " async_read " << bytesTransferred |
| 615 | << " Bytes"; |
| 616 | cancelDeadlineTimer(); |
| 617 | if (ec) |
| 618 | { |
| 619 | BMCWEB_LOG_ERROR << this |
| 620 | << " Error while reading: " << ec.message(); |
| 621 | close(); |
| 622 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
| 623 | return; |
| 624 | } |
| 625 | handle(); |
| 626 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 629 | void doWrite(crow::Response& thisRes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 630 | { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 631 | BMCWEB_LOG_DEBUG << this << " doWrite"; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 632 | thisRes.preparePayload(); |
| 633 | serializer.emplace(*thisRes.stringResponse); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 634 | startDeadline(); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 635 | boost::beast::http::async_write(adaptor, *serializer, |
| 636 | [this, self(shared_from_this())]( |
| 637 | const boost::system::error_code& ec, |
| 638 | std::size_t bytesTransferred) { |
| 639 | BMCWEB_LOG_DEBUG << this << " async_write " << bytesTransferred |
| 640 | << " bytes"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 641 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 642 | cancelDeadlineTimer(); |
James Feist | 54d8bb1 | 2020-07-20 13:28:59 -0700 | [diff] [blame] | 643 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 644 | if (ec) |
| 645 | { |
| 646 | BMCWEB_LOG_DEBUG << this << " from write(2)"; |
| 647 | return; |
| 648 | } |
| 649 | if (!res.keepAlive()) |
| 650 | { |
| 651 | close(); |
| 652 | BMCWEB_LOG_DEBUG << this << " from write(1)"; |
| 653 | return; |
| 654 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 655 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 656 | serializer.reset(); |
| 657 | BMCWEB_LOG_DEBUG << this << " Clearing response"; |
| 658 | res.clear(); |
| 659 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 660 | parser->body_limit(httpReqBodyLimit); // reset body limit for |
| 661 | // newly created parser |
| 662 | buffer.consume(buffer.size()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 663 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 664 | // If the session was built from the transport, we don't need to |
| 665 | // clear it. All other sessions are generated per request. |
| 666 | if (!sessionIsFromTransport) |
| 667 | { |
| 668 | userSession = nullptr; |
| 669 | } |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 670 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 671 | // Destroy the Request via the std::optional |
| 672 | req.reset(); |
| 673 | doReadHeaders(); |
| 674 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 677 | void cancelDeadlineTimer() |
| 678 | { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 679 | timer.cancel(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 682 | void startDeadline() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 683 | { |
| 684 | cancelDeadlineTimer(); |
| 685 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 686 | std::chrono::seconds timeout(15); |
| 687 | // allow slow uploads for logged in users |
Lei YU | 638e239 | 2021-12-28 18:14:13 +0800 | [diff] [blame] | 688 | bool loggedIn = userSession != nullptr; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 689 | if (loggedIn) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 690 | { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 691 | timeout = std::chrono::seconds(60); |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 692 | return; |
| 693 | } |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 694 | |
| 695 | std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this(); |
| 696 | timer.expires_after(timeout); |
| 697 | timer.async_wait([weakSelf](const boost::system::error_code ec) { |
| 698 | // Note, we are ignoring other types of errors here; If the timer |
| 699 | // failed for any reason, we should still close the connection |
| 700 | |
| 701 | std::shared_ptr<Connection<Adaptor, Handler>> self = |
| 702 | weakSelf.lock(); |
| 703 | if (!self) |
| 704 | { |
| 705 | BMCWEB_LOG_CRITICAL << self << " Failed to capture connection"; |
| 706 | return; |
| 707 | } |
| 708 | if (ec == boost::asio::error::operation_aborted) |
| 709 | { |
| 710 | // Canceled wait means the path succeeeded. |
| 711 | return; |
| 712 | } |
| 713 | if (ec) |
| 714 | { |
| 715 | BMCWEB_LOG_CRITICAL << self << " timer failed " << ec; |
| 716 | } |
| 717 | |
| 718 | BMCWEB_LOG_WARNING << self << "Connection timed out, closing"; |
| 719 | |
| 720 | self->close(); |
| 721 | }); |
| 722 | |
| 723 | BMCWEB_LOG_DEBUG << this << " timer started"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 726 | Adaptor adaptor; |
| 727 | Handler* handler; |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 728 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 729 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 730 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 731 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 732 | parser; |
| 733 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 734 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 735 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 736 | std::optional<boost::beast::http::response_serializer< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 737 | boost::beast::http::string_body>> |
| 738 | serializer; |
| 739 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 740 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 741 | crow::Response res; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 742 | |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 743 | bool sessionIsFromTransport = false; |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 744 | std::shared_ptr<persistent_data::UserSession> userSession; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 745 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 746 | boost::asio::steady_timer timer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 747 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 748 | std::function<std::string()>& getCachedDateStr; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 749 | |
| 750 | using std::enable_shared_from_this< |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 751 | Connection<Adaptor, Handler>>::shared_from_this; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 752 | |
| 753 | using std::enable_shared_from_this< |
| 754 | Connection<Adaptor, Handler>>::weak_from_this; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 755 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 756 | } // namespace crow |