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 | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 10 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 11 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 12 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 13 | #include <boost/asio/ip/tcp.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 14 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 15 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 16 | #include <boost/beast/core/flat_static_buffer.hpp> |
Ed Tanous | 918ef25 | 2022-05-25 10:40:41 -0700 | [diff] [blame] | 17 | #include <boost/beast/http/parser.hpp> |
| 18 | #include <boost/beast/http/read.hpp> |
| 19 | #include <boost/beast/http/serializer.hpp> |
| 20 | #include <boost/beast/http/write.hpp> |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 21 | #include <boost/beast/ssl/ssl_stream.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 22 | #include <boost/beast/websocket.hpp> |
Ed Tanous | d32c4fa | 2021-09-14 13:16:51 -0700 | [diff] [blame] | 23 | #include <boost/url/url_view.hpp> |
Ed Tanous | 57fce80 | 2019-05-21 13:00:34 -0700 | [diff] [blame] | 24 | #include <json_html_serializer.hpp> |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 25 | #include <security_headers.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 26 | #include <ssl_key_handler.hpp> |
| 27 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 28 | #include <atomic> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 29 | #include <chrono> |
| 30 | #include <vector> |
| 31 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | namespace crow |
| 33 | { |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 34 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | inline void prettyPrintJson(crow::Response& res) |
| 36 | { |
Ed Tanous | 57fce80 | 2019-05-21 13:00:34 -0700 | [diff] [blame] | 37 | json_html_util::dumpHtml(res.body(), res.jsonValue); |
| 38 | |
Ed Tanous | 93ef580 | 2019-01-03 10:15:41 -0800 | [diff] [blame] | 39 | res.addHeader("Content-Type", "text/html;charset=UTF-8"); |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 42 | static int connectionCount = 0; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 44 | // request body limit size set by the bmcwebHttpReqBodyLimitMb option |
Ed Tanous | 6de264c | 2022-01-06 12:47:59 -0800 | [diff] [blame] | 45 | constexpr uint64_t httpReqBodyLimit = |
| 46 | 1024UL * 1024UL * bmcwebHttpReqBodyLimitMb; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 47 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 48 | constexpr uint64_t loggedOutPostBodyLimit = 4096; |
| 49 | |
| 50 | constexpr uint32_t httpHeaderLimit = 8192; |
| 51 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 52 | template <typename Adaptor, typename Handler> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 53 | class Connection : |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 54 | public std::enable_shared_from_this<Connection<Adaptor, Handler>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | { |
| 56 | public: |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 57 | Connection(Handler* handlerIn, boost::asio::steady_timer&& timerIn, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 58 | std::function<std::string()>& getCachedDateStrF, |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 59 | Adaptor adaptorIn) : |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 60 | adaptor(std::move(adaptorIn)), |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 61 | handler(handlerIn), timer(std::move(timerIn)), |
| 62 | getCachedDateStr(getCachedDateStrF) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 63 | { |
| 64 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | parser->body_limit(httpReqBodyLimit); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 66 | parser->header_limit(httpHeaderLimit); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 67 | |
| 68 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 69 | prepareMutualTls(); |
| 70 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 71 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 72 | connectionCount++; |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 73 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 74 | BMCWEB_LOG_DEBUG << this << " Connection open, total " |
| 75 | << connectionCount; |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | ~Connection() |
| 79 | { |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 80 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 81 | cancelDeadlineTimer(); |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 82 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 83 | connectionCount--; |
| 84 | BMCWEB_LOG_DEBUG << this << " Connection closed, total " |
| 85 | << connectionCount; |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 88 | Connection(const Connection&) = delete; |
| 89 | Connection(Connection&&) = delete; |
| 90 | Connection& operator=(const Connection&) = delete; |
| 91 | Connection& operator=(Connection&&) = delete; |
| 92 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 93 | void prepareMutualTls() |
| 94 | { |
Jonathan Doman | 83deb7d | 2020-11-16 17:00:22 -0800 | [diff] [blame] | 95 | std::error_code error; |
| 96 | std::filesystem::path caPath(ensuressl::trustStorePath); |
| 97 | auto caAvailable = !std::filesystem::is_empty(caPath, error); |
| 98 | caAvailable = caAvailable && !error; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 99 | if (caAvailable && persistent_data::SessionStore::getInstance() |
| 100 | .getAuthMethodsConfig() |
| 101 | .tls) |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 102 | { |
| 103 | adaptor.set_verify_mode(boost::asio::ssl::verify_peer); |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 104 | std::string id = "bmcweb"; |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 105 | |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 106 | const char* cStr = id.c_str(); |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 107 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 108 | const auto* idC = reinterpret_cast<const unsigned char*>(cStr); |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 109 | int ret = SSL_set_session_id_context( |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 110 | adaptor.native_handle(), idC, |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 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 | { |
Ed Tanous | ca45aa3 | 2022-01-07 09:28:45 -0800 | [diff] [blame] | 190 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 191 | unsigned char usageChar = usage->data[i]; |
| 192 | if (KU_DIGITAL_SIGNATURE & usageChar) |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 193 | { |
| 194 | isKeyUsageDigitalSignature = true; |
| 195 | } |
Ed Tanous | ca45aa3 | 2022-01-07 09:28:45 -0800 | [diff] [blame] | 196 | if (KU_KEY_AGREEMENT & usageChar) |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 197 | { |
| 198 | isKeyUsageKeyAgreement = true; |
| 199 | } |
| 200 | } |
Vernon Mauery | b937830 | 2021-06-16 14:06:57 -0700 | [diff] [blame] | 201 | ASN1_BIT_STRING_free(usage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 202 | |
| 203 | if (!isKeyUsageDigitalSignature || !isKeyUsageKeyAgreement) |
| 204 | { |
| 205 | BMCWEB_LOG_DEBUG << this |
| 206 | << " Certificate ExtendedKeyUsage does " |
| 207 | "not allow provided certificate to " |
| 208 | "be used for user authentication"; |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | // Determine that ExtendedKeyUsage includes Client Auth |
| 213 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 214 | stack_st_ASN1_OBJECT* extUsage = |
| 215 | static_cast<stack_st_ASN1_OBJECT*>(X509_get_ext_d2i( |
| 216 | peerCert, NID_ext_key_usage, nullptr, nullptr)); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 217 | |
| 218 | if (extUsage == nullptr) |
| 219 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 220 | BMCWEB_LOG_DEBUG << this << " TLS extUsage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
| 224 | bool isExKeyUsageClientAuth = false; |
| 225 | for (int i = 0; i < sk_ASN1_OBJECT_num(extUsage); i++) |
| 226 | { |
Ed Tanous | 4bac4a8 | 2022-01-07 09:37:55 -0800 | [diff] [blame] | 227 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) |
| 228 | int nid = OBJ_obj2nid(sk_ASN1_OBJECT_value(extUsage, i)); |
| 229 | if (NID_client_auth == nid) |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 230 | { |
| 231 | isExKeyUsageClientAuth = true; |
| 232 | break; |
| 233 | } |
| 234 | } |
Zbigniew Kurzynski | 09d02f8 | 2020-03-30 13:41:42 +0200 | [diff] [blame] | 235 | sk_ASN1_OBJECT_free(extUsage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 236 | |
| 237 | // Certificate has to have proper key usages set |
| 238 | if (!isExKeyUsageClientAuth) |
| 239 | { |
| 240 | BMCWEB_LOG_DEBUG << this |
| 241 | << " Certificate ExtendedKeyUsage does " |
| 242 | "not allow provided certificate to " |
| 243 | "be used for user authentication"; |
| 244 | return true; |
| 245 | } |
| 246 | std::string sslUser; |
| 247 | // Extract username contained in CommonName |
| 248 | sslUser.resize(256, '\0'); |
| 249 | |
| 250 | int status = X509_NAME_get_text_by_NID( |
| 251 | X509_get_subject_name(peerCert), NID_commonName, sslUser.data(), |
| 252 | static_cast<int>(sslUser.size())); |
| 253 | |
| 254 | if (status == -1) |
| 255 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 256 | BMCWEB_LOG_DEBUG |
| 257 | << this << " TLS cannot get username to create session"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | |
| 261 | size_t lastChar = sslUser.find('\0'); |
| 262 | if (lastChar == std::string::npos || lastChar == 0) |
| 263 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 264 | BMCWEB_LOG_DEBUG << this << " Invalid TLS user name"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 265 | return true; |
| 266 | } |
| 267 | sslUser.resize(lastChar); |
Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 268 | std::string unsupportedClientId; |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 269 | sessionIsFromTransport = true; |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 270 | userSession = persistent_data::SessionStore::getInstance() |
| 271 | .generateUserSession( |
Jiaqing Zhao | 41d61c8 | 2021-12-07 13:21:47 +0800 | [diff] [blame] | 272 | sslUser, req->ipAddress, unsupportedClientId, |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 273 | persistent_data::PersistenceType::TIMEOUT); |
| 274 | if (userSession != nullptr) |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 275 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 276 | BMCWEB_LOG_DEBUG |
| 277 | << this |
| 278 | << " Generating TLS session: " << userSession->uniqueId; |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 279 | } |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 280 | return true; |
| 281 | }); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 284 | Adaptor& socket() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 285 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 286 | return adaptor; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 289 | void start() |
| 290 | { |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 291 | if (connectionCount >= 100) |
| 292 | { |
| 293 | BMCWEB_LOG_CRITICAL << this << "Max connection count exceeded."; |
| 294 | return; |
| 295 | } |
| 296 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 297 | startDeadline(); |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 298 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 299 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 300 | // asynchronous "start" |
| 301 | if constexpr (std::is_same_v<Adaptor, |
| 302 | boost::beast::ssl_stream< |
| 303 | boost::asio::ip::tcp::socket>>) |
| 304 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 305 | adaptor.async_handshake(boost::asio::ssl::stream_base::server, |
| 306 | [this, self(shared_from_this())]( |
| 307 | const boost::system::error_code& ec) { |
| 308 | if (ec) |
| 309 | { |
| 310 | return; |
| 311 | } |
| 312 | doReadHeaders(); |
| 313 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 314 | } |
| 315 | else |
| 316 | { |
| 317 | doReadHeaders(); |
| 318 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 319 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 320 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 321 | void handle() |
| 322 | { |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 323 | std::error_code reqEc; |
| 324 | crow::Request& thisReq = req.emplace(parser->release(), reqEc); |
| 325 | if (reqEc) |
| 326 | { |
| 327 | BMCWEB_LOG_DEBUG << "Request failed to construct" << reqEc; |
| 328 | return; |
| 329 | } |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 330 | thisReq.session = userSession; |
| 331 | |
Ivan Mikhaylov | f65b0be | 2021-04-19 10:05:30 +0000 | [diff] [blame] | 332 | // Fetch the client IP address |
| 333 | readClientIp(); |
| 334 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 335 | // Check for HTTP version 1.1. |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 336 | if (thisReq.version() == 11) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 337 | { |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 338 | if (thisReq.getHeaderValue(boost::beast::http::field::host).empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 339 | { |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 340 | res.result(boost::beast::http::status::bad_request); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 341 | completeRequest(res); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 342 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 345 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 346 | BMCWEB_LOG_INFO << "Request: " |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 347 | << " " << this << " HTTP/" << thisReq.version() / 10 |
| 348 | << "." << thisReq.version() % 10 << ' ' |
| 349 | << thisReq.methodString() << " " << thisReq.target() |
Ed Tanous | 8cc8ede | 2022-02-28 10:20:59 -0800 | [diff] [blame] | 350 | << " " << thisReq.ipAddress.to_string(); |
Ed Tanous | d32c4fa | 2021-09-14 13:16:51 -0700 | [diff] [blame] | 351 | |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 352 | res.isAliveHelper = [this]() -> bool { return isAlive(); }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 353 | |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 354 | thisReq.ioService = static_cast<decltype(thisReq.ioService)>( |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 355 | &adaptor.get_executor().context()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 356 | |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 357 | if (res.completed) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 358 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 359 | completeRequest(res); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 360 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 361 | } |
Nan Zhou | a43ea82 | 2022-05-27 00:42:44 +0000 | [diff] [blame^] | 362 | #ifndef BMCWEB_INSECURE_DISABLE_AUTHX |
Nan Zhou | d055a34 | 2022-05-25 01:15:34 +0000 | [diff] [blame] | 363 | if (!crow::authentication::isOnAllowlist(req->url, req->method()) && |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 364 | thisReq.session == nullptr) |
| 365 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 366 | BMCWEB_LOG_WARNING << "Authentication failed"; |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 367 | forward_unauthorized::sendUnauthorized( |
| 368 | req->url, req->getHeaderValue("User-Agent"), |
| 369 | req->getHeaderValue("Accept"), res); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 370 | completeRequest(res); |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 371 | return; |
| 372 | } |
Nan Zhou | a43ea82 | 2022-05-27 00:42:44 +0000 | [diff] [blame^] | 373 | #endif // BMCWEB_INSECURE_DISABLE_AUTHX |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 374 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); |
| 375 | BMCWEB_LOG_DEBUG << "Setting completion handler"; |
| 376 | asyncResp->res.setCompleteRequestHandler( |
| 377 | [self(shared_from_this())](crow::Response& thisRes) { |
| 378 | self->completeRequest(thisRes); |
| 379 | }); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 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 |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 389 | asyncResp->res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 392 | handler->handle(thisReq, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 393 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 394 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 395 | bool isAlive() |
| 396 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 397 | if constexpr (std::is_same_v<Adaptor, |
| 398 | boost::beast::ssl_stream< |
| 399 | boost::asio::ip::tcp::socket>>) |
| 400 | { |
| 401 | return adaptor.next_layer().is_open(); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | return adaptor.is_open(); |
| 406 | } |
| 407 | } |
| 408 | void close() |
| 409 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 410 | if constexpr (std::is_same_v<Adaptor, |
| 411 | boost::beast::ssl_stream< |
| 412 | boost::asio::ip::tcp::socket>>) |
| 413 | { |
| 414 | adaptor.next_layer().close(); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 415 | if (sessionIsFromTransport && userSession != nullptr) |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 416 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 417 | BMCWEB_LOG_DEBUG |
| 418 | << this |
| 419 | << " Removing TLS session: " << userSession->uniqueId; |
| 420 | persistent_data::SessionStore::getInstance().removeSession( |
| 421 | userSession); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 422 | } |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 423 | } |
| 424 | else |
| 425 | { |
| 426 | adaptor.close(); |
| 427 | } |
| 428 | } |
| 429 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 430 | void completeRequest(crow::Response& thisRes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 431 | { |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 432 | if (!req) |
| 433 | { |
| 434 | return; |
| 435 | } |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 436 | res = std::move(thisRes); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 437 | BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' |
| 438 | << res.resultInt() << " keepalive=" << req->keepAlive(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 439 | |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 440 | addSecurityHeaders(*req, res); |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 441 | |
Nan Zhou | d055a34 | 2022-05-25 01:15:34 +0000 | [diff] [blame] | 442 | crow::authentication::cleanupTempSession(*req); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 443 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 444 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 445 | { |
| 446 | // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << |
| 447 | // isReading |
| 448 | // << ' ' << isWriting; |
| 449 | // delete this; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 450 | |
| 451 | // delete lambda with self shared_ptr |
| 452 | // to enable connection destruction |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 453 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | if (res.body().empty() && !res.jsonValue.empty()) |
| 457 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 458 | if (http_helpers::requestPrefersHtml(req->getHeaderValue("Accept"))) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 459 | { |
| 460 | prettyPrintJson(res); |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | res.jsonMode(); |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 465 | res.body() = res.jsonValue.dump( |
| 466 | 2, ' ', true, nlohmann::json::error_handler_t::replace); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 469 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 470 | if (res.resultInt() >= 400 && res.body().empty()) |
| 471 | { |
| 472 | res.body() = std::string(res.reason()); |
| 473 | } |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 474 | |
| 475 | if (res.result() == boost::beast::http::status::no_content) |
| 476 | { |
| 477 | // Boost beast throws if content is provided on a no-content |
| 478 | // response. Ideally, this would never happen, but in the case that |
| 479 | // it does, we don't want to throw. |
| 480 | BMCWEB_LOG_CRITICAL |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 481 | << this << " Response content provided but code was no-content"; |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 482 | res.body().clear(); |
| 483 | } |
| 484 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 485 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
| 486 | |
| 487 | res.keepAlive(req->keepAlive()); |
| 488 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 489 | doWrite(res); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 490 | |
| 491 | // delete lambda with self shared_ptr |
| 492 | // to enable connection destruction |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 493 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 496 | void readClientIp() |
| 497 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 498 | boost::asio::ip::address ip; |
| 499 | boost::system::error_code ec = getClientIp(ip); |
| 500 | if (ec) |
| 501 | { |
| 502 | return; |
| 503 | } |
| 504 | req->ipAddress = ip; |
| 505 | } |
| 506 | |
| 507 | boost::system::error_code getClientIp(boost::asio::ip::address& ip) |
| 508 | { |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 509 | boost::system::error_code ec; |
| 510 | BMCWEB_LOG_DEBUG << "Fetch the client IP address"; |
| 511 | boost::asio::ip::tcp::endpoint endpoint = |
| 512 | boost::beast::get_lowest_layer(adaptor).remote_endpoint(ec); |
| 513 | |
| 514 | if (ec) |
| 515 | { |
| 516 | // If remote endpoint fails keep going. "ClientOriginIPAddress" |
| 517 | // will be empty. |
| 518 | BMCWEB_LOG_ERROR << "Failed to get the client's IP Address. ec : " |
| 519 | << ec; |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 520 | return ec; |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 521 | } |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 522 | ip = endpoint.address(); |
| 523 | return ec; |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 524 | } |
| 525 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 526 | private: |
| 527 | void doReadHeaders() |
| 528 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 529 | BMCWEB_LOG_DEBUG << this << " doReadHeaders"; |
| 530 | |
| 531 | // Clean up any previous Connection. |
| 532 | boost::beast::http::async_read_header( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 533 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 534 | [this, |
| 535 | self(shared_from_this())](const boost::system::error_code& ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 536 | std::size_t bytesTransferred) { |
Andrew Geissler | 1c801e1 | 2021-10-08 14:36:01 -0500 | [diff] [blame] | 537 | BMCWEB_LOG_DEBUG << this << " async_read_header " |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 538 | << bytesTransferred << " Bytes"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 539 | bool errorWhileReading = false; |
| 540 | if (ec) |
| 541 | { |
| 542 | errorWhileReading = true; |
Andrew Geissler | 1c801e1 | 2021-10-08 14:36:01 -0500 | [diff] [blame] | 543 | if (ec == boost::asio::error::eof) |
| 544 | { |
| 545 | BMCWEB_LOG_WARNING |
| 546 | << this << " Error while reading: " << ec.message(); |
| 547 | } |
| 548 | else |
| 549 | { |
| 550 | BMCWEB_LOG_ERROR |
| 551 | << this << " Error while reading: " << ec.message(); |
| 552 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 553 | } |
| 554 | else |
| 555 | { |
| 556 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 557 | // websocket, treat as an error |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 558 | if (!isAlive() && |
| 559 | !boost::beast::websocket::is_upgrade(parser->get())) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 560 | { |
| 561 | errorWhileReading = true; |
| 562 | } |
| 563 | } |
| 564 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 565 | cancelDeadlineTimer(); |
| 566 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 567 | if (errorWhileReading) |
| 568 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 569 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 570 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 574 | readClientIp(); |
Ed Tanous | 92ccb88 | 2020-08-18 10:36:33 -0700 | [diff] [blame] | 575 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 576 | boost::asio::ip::address ip; |
| 577 | if (getClientIp(ip)) |
| 578 | { |
| 579 | BMCWEB_LOG_DEBUG << "Unable to get client IP"; |
| 580 | } |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 581 | sessionIsFromTransport = false; |
Nan Zhou | a43ea82 | 2022-05-27 00:42:44 +0000 | [diff] [blame^] | 582 | #ifndef BMCWEB_INSECURE_DISABLE_AUTHX |
Nan Zhou | 8682c5a | 2021-11-13 11:00:07 -0800 | [diff] [blame] | 583 | boost::beast::http::verb method = parser->get().method(); |
Nan Zhou | d055a34 | 2022-05-25 01:15:34 +0000 | [diff] [blame] | 584 | userSession = crow::authentication::authenticate( |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 585 | ip, res, method, parser->get().base(), userSession); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 586 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 587 | bool loggedIn = userSession != nullptr; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 588 | if (!loggedIn) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 589 | { |
| 590 | const boost::optional<uint64_t> contentLength = |
| 591 | parser->content_length(); |
| 592 | if (contentLength && |
| 593 | *contentLength > loggedOutPostBodyLimit) |
| 594 | { |
| 595 | BMCWEB_LOG_DEBUG << "Content length greater than limit " |
| 596 | << *contentLength; |
| 597 | close(); |
| 598 | return; |
| 599 | } |
| 600 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 601 | BMCWEB_LOG_DEBUG << "Starting quick deadline"; |
| 602 | } |
Nan Zhou | a43ea82 | 2022-05-27 00:42:44 +0000 | [diff] [blame^] | 603 | #endif // BMCWEB_INSECURE_DISABLE_AUTHX |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 604 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 605 | doRead(); |
| 606 | }); |
| 607 | } |
| 608 | |
| 609 | void doRead() |
| 610 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 611 | BMCWEB_LOG_DEBUG << this << " doRead"; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 612 | startDeadline(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 613 | boost::beast::http::async_read( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 614 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 615 | [this, |
| 616 | self(shared_from_this())](const boost::system::error_code& ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 617 | std::size_t bytesTransferred) { |
| 618 | BMCWEB_LOG_DEBUG << this << " async_read " << bytesTransferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 619 | << " Bytes"; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 620 | cancelDeadlineTimer(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 621 | if (ec) |
| 622 | { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 623 | BMCWEB_LOG_ERROR |
| 624 | << this << " Error while reading: " << ec.message(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 625 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 626 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | handle(); |
| 630 | }); |
| 631 | } |
| 632 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 633 | void doWrite(crow::Response& thisRes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 634 | { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 635 | BMCWEB_LOG_DEBUG << this << " doWrite"; |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 636 | thisRes.preparePayload(); |
| 637 | serializer.emplace(*thisRes.stringResponse); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 638 | startDeadline(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 639 | boost::beast::http::async_write( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 640 | adaptor, *serializer, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 641 | [this, |
| 642 | self(shared_from_this())](const boost::system::error_code& ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 643 | std::size_t bytesTransferred) { |
| 644 | BMCWEB_LOG_DEBUG << this << " async_write " << bytesTransferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 645 | << " bytes"; |
| 646 | |
James Feist | 54d8bb1 | 2020-07-20 13:28:59 -0700 | [diff] [blame] | 647 | cancelDeadlineTimer(); |
| 648 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 649 | if (ec) |
| 650 | { |
| 651 | BMCWEB_LOG_DEBUG << this << " from write(2)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 652 | return; |
| 653 | } |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 654 | if (!res.keepAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 655 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 656 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 657 | BMCWEB_LOG_DEBUG << this << " from write(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 658 | return; |
| 659 | } |
| 660 | |
| 661 | serializer.reset(); |
| 662 | BMCWEB_LOG_DEBUG << this << " Clearing response"; |
| 663 | res.clear(); |
| 664 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Gunnar Mills | ded2a1e | 2020-07-24 09:46:33 -0500 | [diff] [blame] | 665 | parser->body_limit(httpReqBodyLimit); // reset body limit for |
| 666 | // newly created parser |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 667 | buffer.consume(buffer.size()); |
| 668 | |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 669 | // If the session was built from the transport, we don't need to |
| 670 | // clear it. All other sessions are generated per request. |
| 671 | if (!sessionIsFromTransport) |
| 672 | { |
| 673 | userSession = nullptr; |
| 674 | } |
| 675 | |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 676 | // Destroy the Request via the std::optional |
| 677 | req.reset(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 678 | doReadHeaders(); |
| 679 | }); |
| 680 | } |
| 681 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 682 | void cancelDeadlineTimer() |
| 683 | { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 684 | timer.cancel(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 687 | void startDeadline() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 688 | { |
| 689 | cancelDeadlineTimer(); |
| 690 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 691 | std::chrono::seconds timeout(15); |
| 692 | // allow slow uploads for logged in users |
Lei YU | 638e239 | 2021-12-28 18:14:13 +0800 | [diff] [blame] | 693 | bool loggedIn = userSession != nullptr; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 694 | if (loggedIn) |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 695 | { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 696 | timeout = std::chrono::seconds(60); |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 697 | return; |
| 698 | } |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 699 | |
| 700 | std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this(); |
| 701 | timer.expires_after(timeout); |
| 702 | timer.async_wait([weakSelf](const boost::system::error_code ec) { |
| 703 | // Note, we are ignoring other types of errors here; If the timer |
| 704 | // failed for any reason, we should still close the connection |
| 705 | |
| 706 | std::shared_ptr<Connection<Adaptor, Handler>> self = |
| 707 | weakSelf.lock(); |
| 708 | if (!self) |
| 709 | { |
| 710 | BMCWEB_LOG_CRITICAL << self << " Failed to capture connection"; |
| 711 | return; |
| 712 | } |
| 713 | if (ec == boost::asio::error::operation_aborted) |
| 714 | { |
| 715 | // Canceled wait means the path succeeeded. |
| 716 | return; |
| 717 | } |
| 718 | if (ec) |
| 719 | { |
| 720 | BMCWEB_LOG_CRITICAL << self << " timer failed " << ec; |
| 721 | } |
| 722 | |
| 723 | BMCWEB_LOG_WARNING << self << "Connection timed out, closing"; |
| 724 | |
| 725 | self->close(); |
| 726 | }); |
| 727 | |
| 728 | BMCWEB_LOG_DEBUG << this << " timer started"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 731 | Adaptor adaptor; |
| 732 | Handler* handler; |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 733 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 734 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 735 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 736 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 737 | parser; |
| 738 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 739 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 740 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 741 | std::optional<boost::beast::http::response_serializer< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 742 | boost::beast::http::string_body>> |
| 743 | serializer; |
| 744 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 745 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 746 | crow::Response res; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 747 | |
Ed Tanous | 9a69d5a | 2021-09-13 10:23:51 -0700 | [diff] [blame] | 748 | bool sessionIsFromTransport = false; |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 749 | std::shared_ptr<persistent_data::UserSession> userSession; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 750 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 751 | boost::asio::steady_timer timer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 752 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 753 | std::function<std::string()>& getCachedDateStr; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 754 | |
| 755 | using std::enable_shared_from_this< |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 756 | Connection<Adaptor, Handler>>::shared_from_this; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 757 | |
| 758 | using std::enable_shared_from_this< |
| 759 | Connection<Adaptor, Handler>>::weak_from_this; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 760 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 761 | } // namespace crow |