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