Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Adriana Kobylak | 0e1cf26 | 2019-12-05 13:57:57 -0600 | [diff] [blame] | 2 | #include "config.h" |
| 3 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 4 | #include "http_response.h" |
| 5 | #include "logging.h" |
| 6 | #include "middleware_context.h" |
| 7 | #include "timer_queue.h" |
| 8 | #include "utility.h" |
| 9 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 10 | #include "authorization.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 11 | #include "http_utility.hpp" |
| 12 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 13 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 14 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 15 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 16 | #include <boost/asio/ip/tcp.hpp> |
Ed Tanous | 2f1ebcd | 2019-02-13 19:39:07 -0800 | [diff] [blame] | 17 | #include <boost/asio/ssl.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 18 | #include <boost/beast/core/flat_static_buffer.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 19 | #include <boost/beast/http.hpp> |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 20 | #include <boost/beast/ssl/ssl_stream.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 21 | #include <boost/beast/websocket.hpp> |
| 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 | { |
Jason M. Bills | 193ad2f | 2018-09-26 15:08:52 -0700 | [diff] [blame] | 33 | std::string value = res.jsonValue.dump(4, ' ', true); |
Ed Tanous | a29c997 | 2018-11-29 15:54:32 -0800 | [diff] [blame] | 34 | utility::escapeHtml(value); |
| 35 | utility::convertToLinks(value); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 36 | res.body() = "<html>\n" |
| 37 | "<head>\n" |
| 38 | "<title>Redfish API</title>\n" |
| 39 | "<link rel=\"stylesheet\" type=\"text/css\" " |
| 40 | "href=\"/styles/default.css\">\n" |
| 41 | "<script src=\"/highlight.pack.js\"></script>" |
| 42 | "<script>hljs.initHighlightingOnLoad();</script>" |
| 43 | "</head>\n" |
| 44 | "<body>\n" |
| 45 | "<div style=\"max-width: 576px;margin:0 auto;\">\n" |
| 46 | "<img src=\"/DMTF_Redfish_logo_2017.svg\" alt=\"redfish\" " |
| 47 | "height=\"406px\" " |
| 48 | "width=\"576px\">\n" |
| 49 | "<br>\n" |
| 50 | "<pre>\n" |
| 51 | "<code class=\"json\">" + |
| 52 | value + |
| 53 | "</code>\n" |
| 54 | "</pre>\n" |
| 55 | "</div>\n" |
| 56 | "</body>\n" |
| 57 | "</html>\n"; |
Ed Tanous | 93ef580 | 2019-01-03 10:15:41 -0800 | [diff] [blame] | 58 | res.addHeader("Content-Type", "text/html;charset=UTF-8"); |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 61 | using namespace boost; |
| 62 | using tcp = asio::ip::tcp; |
| 63 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | namespace detail |
| 65 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 66 | template <typename MW> |
| 67 | struct CheckBeforeHandleArity3Const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | { |
| 69 | template <typename T, |
| 70 | void (T::*)(Request&, Response&, typename MW::Context&) const = |
| 71 | &T::beforeHandle> |
| 72 | struct Get |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 73 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 76 | template <typename MW> |
| 77 | struct CheckBeforeHandleArity3 |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 78 | { |
| 79 | template <typename T, void (T::*)(Request&, Response&, |
| 80 | typename MW::Context&) = &T::beforeHandle> |
| 81 | struct Get |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 82 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 85 | template <typename MW> |
| 86 | struct CheckAfterHandleArity3Const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | { |
| 88 | template <typename T, |
| 89 | void (T::*)(Request&, Response&, typename MW::Context&) const = |
| 90 | &T::afterHandle> |
| 91 | struct Get |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 92 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 95 | template <typename MW> |
| 96 | struct CheckAfterHandleArity3 |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 97 | { |
| 98 | template <typename T, void (T::*)(Request&, Response&, |
| 99 | typename MW::Context&) = &T::afterHandle> |
| 100 | struct Get |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 101 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 104 | template <typename T> |
| 105 | struct IsBeforeHandleArity3Impl |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | { |
| 107 | template <typename C> |
| 108 | static std::true_type |
| 109 | f(typename CheckBeforeHandleArity3Const<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 110 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | template <typename C> |
| 112 | static std::true_type |
| 113 | f(typename CheckBeforeHandleArity3<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 114 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 115 | template <typename C> |
| 116 | static std::false_type f(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 117 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | public: |
Ed Tanous | 0c838cf | 2019-10-24 10:01:46 -0700 | [diff] [blame] | 119 | static constexpr bool value = decltype(f<T>(nullptr))::value; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 122 | template <typename T> |
| 123 | struct IsAfterHandleArity3Impl |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 124 | { |
| 125 | template <typename C> |
| 126 | static std::true_type |
| 127 | f(typename CheckAfterHandleArity3Const<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 128 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 129 | template <typename C> |
| 130 | static std::true_type |
| 131 | f(typename CheckAfterHandleArity3<T>::template Get<C>*); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 132 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 133 | template <typename C> |
| 134 | static std::false_type f(...); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 135 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 136 | public: |
Ed Tanous | 0c838cf | 2019-10-24 10:01:46 -0700 | [diff] [blame] | 137 | static constexpr bool value = decltype(f<T>(nullptr))::value; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 141 | typename std::enable_if<!IsBeforeHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 142 | beforeHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 143 | ParentContext& /*parent_ctx*/) |
| 144 | { |
| 145 | mw.beforeHandle(req, res, ctx.template get<MW>(), ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 149 | typename std::enable_if<IsBeforeHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 150 | beforeHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 151 | ParentContext& /*parent_ctx*/) |
| 152 | { |
| 153 | mw.beforeHandle(req, res, ctx.template get<MW>()); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 157 | typename std::enable_if<!IsAfterHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | afterHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 159 | ParentContext& /*parent_ctx*/) |
| 160 | { |
| 161 | mw.afterHandle(req, res, ctx.template get<MW>(), ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | template <typename MW, typename Context, typename ParentContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 165 | typename std::enable_if<IsAfterHandleArity3Impl<MW>::value>::type |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 166 | afterHandlerCall(MW& mw, Request& req, Response& res, Context& ctx, |
| 167 | ParentContext& /*parent_ctx*/) |
| 168 | { |
| 169 | mw.afterHandle(req, res, ctx.template get<MW>()); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 172 | template <size_t N, typename Context, typename Container, typename CurrentMW, |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 173 | typename... Middlewares> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 174 | bool middlewareCallHelper(Container& middlewares, Request& req, Response& res, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 175 | Context& ctx) |
| 176 | { |
| 177 | using parent_context_t = typename Context::template partial<N - 1>; |
| 178 | beforeHandlerCall<CurrentMW, Context, parent_context_t>( |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 179 | std::get<N>(middlewares), req, res, ctx, |
| 180 | static_cast<parent_context_t&>(ctx)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 181 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | if (res.isCompleted()) |
| 183 | { |
| 184 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 185 | std::get<N>(middlewares), req, res, ctx, |
| 186 | static_cast<parent_context_t&>(ctx)); |
| 187 | return true; |
| 188 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 189 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 190 | if (middlewareCallHelper<N + 1, Context, Container, Middlewares...>( |
| 191 | middlewares, req, res, ctx)) |
| 192 | { |
| 193 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 194 | std::get<N>(middlewares), req, res, ctx, |
| 195 | static_cast<parent_context_t&>(ctx)); |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | return false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 202 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 203 | bool middlewareCallHelper(Container& /*middlewares*/, Request& /*req*/, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 204 | Response& /*res*/, Context& /*ctx*/) |
| 205 | { |
| 206 | return false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 209 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 210 | typename std::enable_if<(N < 0)>::type |
| 211 | afterHandlersCallHelper(Container& /*middlewares*/, Context& /*Context*/, |
| 212 | Request& /*req*/, Response& /*res*/) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 213 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 214 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 215 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 216 | typename std::enable_if<(N == 0)>::type |
| 217 | afterHandlersCallHelper(Container& middlewares, Context& ctx, Request& req, |
| 218 | Response& res) |
| 219 | { |
| 220 | using parent_context_t = typename Context::template partial<N - 1>; |
| 221 | using CurrentMW = typename std::tuple_element< |
| 222 | N, typename std::remove_reference<Container>::type>::type; |
| 223 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 224 | std::get<N>(middlewares), req, res, ctx, |
| 225 | static_cast<parent_context_t&>(ctx)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 226 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 228 | template <size_t N, typename Context, typename Container> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 229 | typename std::enable_if<(N > 0)>::type |
| 230 | afterHandlersCallHelper(Container& middlewares, Context& ctx, Request& req, |
| 231 | Response& res) |
| 232 | { |
| 233 | using parent_context_t = typename Context::template partial<N - 1>; |
| 234 | using CurrentMW = typename std::tuple_element< |
| 235 | N, typename std::remove_reference<Container>::type>::type; |
| 236 | afterHandlerCall<CurrentMW, Context, parent_context_t>( |
| 237 | std::get<N>(middlewares), req, res, ctx, |
| 238 | static_cast<parent_context_t&>(ctx)); |
| 239 | afterHandlersCallHelper<N - 1, Context, Container>(middlewares, ctx, req, |
| 240 | res); |
| 241 | } |
| 242 | } // namespace detail |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 243 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 244 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 245 | static std::atomic<int> connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 246 | #endif |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 247 | |
Adriana Kobylak | 0e1cf26 | 2019-12-05 13:57:57 -0600 | [diff] [blame] | 248 | // request body limit size set by the BMCWEB_HTTP_REQ_BODY_LIMIT_MB option |
| 249 | constexpr unsigned int httpReqBodyLimit = |
| 250 | 1024 * 1024 * BMCWEB_HTTP_REQ_BODY_LIMIT_MB; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 251 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 252 | constexpr uint64_t loggedOutPostBodyLimit = 4096; |
| 253 | |
| 254 | constexpr uint32_t httpHeaderLimit = 8192; |
| 255 | |
| 256 | // drop all connections after 1 minute, this time limit was chosen |
| 257 | // arbitrarily and can be adjusted later if needed |
| 258 | static constexpr const size_t loggedInAttempts = |
| 259 | (60 / timerQueueTimeoutSeconds); |
| 260 | |
| 261 | static constexpr const size_t loggedOutAttempts = |
| 262 | (15 / timerQueueTimeoutSeconds); |
| 263 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 264 | template <typename Adaptor, typename Handler, typename... Middlewares> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 265 | class Connection : |
| 266 | public std::enable_shared_from_this< |
| 267 | Connection<Adaptor, Handler, Middlewares...>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 268 | { |
| 269 | public: |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 270 | Connection(boost::asio::io_context& ioService, Handler* handlerIn, |
| 271 | const std::string& ServerNameIn, |
| 272 | std::tuple<Middlewares...>* middlewaresIn, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 273 | std::function<std::string()>& get_cached_date_str_f, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 274 | detail::TimerQueue& timerQueueIn, Adaptor adaptorIn) : |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 275 | adaptor(std::move(adaptorIn)), |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 276 | handler(handlerIn), serverName(ServerNameIn), |
| 277 | middlewares(middlewaresIn), getCachedDateStr(get_cached_date_str_f), |
| 278 | timerQueue(timerQueueIn) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 279 | { |
| 280 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 281 | parser->body_limit(httpReqBodyLimit); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 282 | parser->header_limit(httpHeaderLimit); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 283 | req.emplace(parser->get()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 284 | |
| 285 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 286 | auto ca_available = !std::filesystem::is_empty( |
| 287 | std::filesystem::path(ensuressl::trustStorePath)); |
| 288 | if (ca_available && crow::persistent_data::SessionStore::getInstance() |
| 289 | .getAuthMethodsConfig() |
| 290 | .tls) |
| 291 | { |
| 292 | adaptor.set_verify_mode(boost::asio::ssl::verify_peer); |
| 293 | SSL_set_session_id_context( |
| 294 | adaptor.native_handle(), |
| 295 | reinterpret_cast<const unsigned char*>(serverName.c_str()), |
Zbigniew Kurzynski | cac94c5 | 2019-11-07 12:55:04 +0100 | [diff] [blame] | 296 | static_cast<unsigned int>(serverName.length())); |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 297 | BMCWEB_LOG_DEBUG << this << " TLS is enabled on this connection."; |
| 298 | } |
| 299 | |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 300 | adaptor.set_verify_callback([this]( |
| 301 | bool preverified, |
| 302 | boost::asio::ssl::verify_context& ctx) { |
| 303 | // do nothing if TLS is disabled |
| 304 | if (!crow::persistent_data::SessionStore::getInstance() |
| 305 | .getAuthMethodsConfig() |
| 306 | .tls) |
| 307 | { |
| 308 | BMCWEB_LOG_DEBUG << this << " TLS auth_config is disabled"; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 309 | return true; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // We always return true to allow full auth flow |
| 313 | if (!preverified) |
| 314 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 315 | BMCWEB_LOG_DEBUG << this << " TLS preverification failed."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 316 | return true; |
| 317 | } |
| 318 | |
| 319 | X509_STORE_CTX* cts = ctx.native_handle(); |
| 320 | if (cts == nullptr) |
| 321 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 322 | BMCWEB_LOG_DEBUG << this << " Cannot get native TLS handle."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 323 | return true; |
| 324 | } |
| 325 | |
| 326 | // Get certificate |
| 327 | X509* peerCert = |
| 328 | X509_STORE_CTX_get_current_cert(ctx.native_handle()); |
| 329 | if (peerCert == nullptr) |
| 330 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 331 | BMCWEB_LOG_DEBUG << this |
| 332 | << " Cannot get current TLS certificate."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 333 | return true; |
| 334 | } |
| 335 | |
| 336 | // Check if certificate is OK |
| 337 | int error = X509_STORE_CTX_get_error(cts); |
| 338 | if (error != X509_V_OK) |
| 339 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 340 | BMCWEB_LOG_INFO << this << " Last TLS error is: " << error; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | // Check that we have reached final certificate in chain |
| 344 | int32_t depth = X509_STORE_CTX_get_error_depth(cts); |
| 345 | if (depth != 0) |
| 346 | |
| 347 | { |
| 348 | BMCWEB_LOG_DEBUG |
| 349 | << this << " Certificate verification in progress (depth " |
| 350 | << depth << "), waiting to reach final depth"; |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | BMCWEB_LOG_DEBUG << this |
| 355 | << " Certificate verification of final depth"; |
| 356 | |
| 357 | // Verify KeyUsage |
| 358 | bool isKeyUsageDigitalSignature = false; |
| 359 | bool isKeyUsageKeyAgreement = false; |
| 360 | |
| 361 | ASN1_BIT_STRING* usage = static_cast<ASN1_BIT_STRING*>( |
| 362 | X509_get_ext_d2i(peerCert, NID_key_usage, NULL, NULL)); |
| 363 | |
| 364 | if (usage == nullptr) |
| 365 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 366 | BMCWEB_LOG_DEBUG << this << " TLS usage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
| 370 | for (int i = 0; i < usage->length; i++) |
| 371 | { |
| 372 | if (KU_DIGITAL_SIGNATURE & usage->data[i]) |
| 373 | { |
| 374 | isKeyUsageDigitalSignature = true; |
| 375 | } |
| 376 | if (KU_KEY_AGREEMENT & usage->data[i]) |
| 377 | { |
| 378 | isKeyUsageKeyAgreement = true; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (!isKeyUsageDigitalSignature || !isKeyUsageKeyAgreement) |
| 383 | { |
| 384 | BMCWEB_LOG_DEBUG << this |
| 385 | << " Certificate ExtendedKeyUsage does " |
| 386 | "not allow provided certificate to " |
| 387 | "be used for user authentication"; |
| 388 | return true; |
| 389 | } |
Zbigniew Kurzynski | 09d02f8 | 2020-03-30 13:41:42 +0200 | [diff] [blame] | 390 | ASN1_BIT_STRING_free(usage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 391 | |
| 392 | // Determine that ExtendedKeyUsage includes Client Auth |
| 393 | |
| 394 | stack_st_ASN1_OBJECT* extUsage = static_cast<stack_st_ASN1_OBJECT*>( |
| 395 | X509_get_ext_d2i(peerCert, NID_ext_key_usage, NULL, NULL)); |
| 396 | |
| 397 | if (extUsage == nullptr) |
| 398 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 399 | BMCWEB_LOG_DEBUG << this << " TLS extUsage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
| 403 | bool isExKeyUsageClientAuth = false; |
| 404 | for (int i = 0; i < sk_ASN1_OBJECT_num(extUsage); i++) |
| 405 | { |
| 406 | if (NID_client_auth == |
| 407 | OBJ_obj2nid(sk_ASN1_OBJECT_value(extUsage, i))) |
| 408 | { |
| 409 | isExKeyUsageClientAuth = true; |
| 410 | break; |
| 411 | } |
| 412 | } |
Zbigniew Kurzynski | 09d02f8 | 2020-03-30 13:41:42 +0200 | [diff] [blame] | 413 | sk_ASN1_OBJECT_free(extUsage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 414 | |
| 415 | // Certificate has to have proper key usages set |
| 416 | if (!isExKeyUsageClientAuth) |
| 417 | { |
| 418 | BMCWEB_LOG_DEBUG << this |
| 419 | << " Certificate ExtendedKeyUsage does " |
| 420 | "not allow provided certificate to " |
| 421 | "be used for user authentication"; |
| 422 | return true; |
| 423 | } |
| 424 | std::string sslUser; |
| 425 | // Extract username contained in CommonName |
| 426 | sslUser.resize(256, '\0'); |
| 427 | |
| 428 | int status = X509_NAME_get_text_by_NID( |
| 429 | X509_get_subject_name(peerCert), NID_commonName, sslUser.data(), |
| 430 | static_cast<int>(sslUser.size())); |
| 431 | |
| 432 | if (status == -1) |
| 433 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 434 | BMCWEB_LOG_DEBUG |
| 435 | << this << " TLS cannot get username to create session"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
| 439 | size_t lastChar = sslUser.find('\0'); |
| 440 | if (lastChar == std::string::npos || lastChar == 0) |
| 441 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 442 | BMCWEB_LOG_DEBUG << this << " Invalid TLS user name"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 443 | return true; |
| 444 | } |
| 445 | sslUser.resize(lastChar); |
| 446 | |
| 447 | session = persistent_data::SessionStore::getInstance() |
| 448 | .generateUserSession( |
| 449 | sslUser, |
| 450 | crow::persistent_data::PersistenceType::TIMEOUT); |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 451 | if (auto sp = session.lock()) |
| 452 | { |
| 453 | BMCWEB_LOG_DEBUG << this |
| 454 | << " Generating TLS session: " << sp->uniqueId; |
| 455 | } |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 456 | return true; |
| 457 | }); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 458 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 459 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 460 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 461 | connectionCount++; |
| 462 | BMCWEB_LOG_DEBUG << this << " Connection open, total " |
| 463 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 464 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 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 | ~Connection() |
| 468 | { |
| 469 | res.completeRequestHandler = nullptr; |
| 470 | cancelDeadlineTimer(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 471 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 472 | connectionCount--; |
| 473 | BMCWEB_LOG_DEBUG << this << " Connection closed, total " |
| 474 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 475 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 478 | Adaptor& socket() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 479 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 480 | return adaptor; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 483 | void start() |
| 484 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 485 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 486 | startDeadline(0); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 487 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 488 | // asynchronous "start" |
| 489 | if constexpr (std::is_same_v<Adaptor, |
| 490 | boost::beast::ssl_stream< |
| 491 | boost::asio::ip::tcp::socket>>) |
| 492 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 493 | adaptor.async_handshake(boost::asio::ssl::stream_base::server, |
| 494 | [this, self(shared_from_this())]( |
| 495 | const boost::system::error_code& ec) { |
| 496 | if (ec) |
| 497 | { |
| 498 | return; |
| 499 | } |
| 500 | doReadHeaders(); |
| 501 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 502 | } |
| 503 | else |
| 504 | { |
| 505 | doReadHeaders(); |
| 506 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 507 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 508 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 509 | void handle() |
| 510 | { |
| 511 | cancelDeadlineTimer(); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 512 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 513 | bool isInvalidRequest = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 514 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 515 | // Check for HTTP version 1.1. |
| 516 | if (req->version() == 11) |
| 517 | { |
| 518 | if (req->getHeaderValue(boost::beast::http::field::host).empty()) |
| 519 | { |
| 520 | isInvalidRequest = true; |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 521 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 524 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 525 | BMCWEB_LOG_INFO << "Request: " |
| 526 | << " " << this << " HTTP/" << req->version() / 10 << "." |
| 527 | << req->version() % 10 << ' ' << req->methodString() |
| 528 | << " " << req->target(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 529 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 530 | needToCallAfterHandlers = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 531 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 532 | if (!isInvalidRequest) |
| 533 | { |
raviteja-b | 4722efe | 2020-02-03 12:23:18 -0600 | [diff] [blame] | 534 | req->socket = [this, self = shared_from_this()]() -> Adaptor& { |
| 535 | return self->socket(); |
| 536 | }; |
| 537 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 538 | res.completeRequestHandler = [] {}; |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 539 | res.isAliveHelper = [this]() -> bool { return isAlive(); }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 540 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 541 | ctx = detail::Context<Middlewares...>(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 542 | req->middlewareContext = static_cast<void*>(&ctx); |
| 543 | req->ioService = static_cast<decltype(req->ioService)>( |
| 544 | &adaptor.get_executor().context()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 545 | |
| 546 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 547 | if (auto sp = session.lock()) |
| 548 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 549 | // set cookie only if this is req from the browser. |
| 550 | if (req->getHeaderValue("User-Agent").empty()) |
| 551 | { |
| 552 | BMCWEB_LOG_DEBUG << this << " TLS session: " << sp->uniqueId |
| 553 | << " will be used for this request."; |
| 554 | req->session = sp; |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | std::string_view cookieValue = |
| 559 | req->getHeaderValue("Cookie"); |
| 560 | if (cookieValue.empty() || |
| 561 | cookieValue.find("SESSION=") == std::string::npos) |
| 562 | { |
| 563 | res.addHeader("Set-Cookie", |
| 564 | "XSRF-TOKEN=" + sp->csrfToken + |
| 565 | "; Secure\r\nSet-Cookie: SESSION=" + |
| 566 | sp->sessionToken + |
Zbigniew Kurzynski | 26139a5 | 2019-12-11 19:11:18 +0100 | [diff] [blame] | 567 | "; Secure; HttpOnly\r\nSet-Cookie: " |
| 568 | "IsAuthenticated=true; Secure"); |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 569 | BMCWEB_LOG_DEBUG |
| 570 | << this << " TLS session: " << sp->uniqueId |
| 571 | << " with cookie will be used for this request."; |
| 572 | req->session = sp; |
| 573 | } |
| 574 | } |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 575 | } |
| 576 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 577 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 578 | detail::middlewareCallHelper< |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 579 | 0U, decltype(ctx), decltype(*middlewares), Middlewares...>( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 580 | *middlewares, *req, res, ctx); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 581 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 582 | if (!res.completed) |
| 583 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 584 | needToCallAfterHandlers = true; |
| 585 | res.completeRequestHandler = [self(shared_from_this())] { |
| 586 | self->completeRequest(); |
| 587 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 588 | if (req->isUpgrade() && |
| 589 | boost::iequals( |
| 590 | req->getHeaderValue(boost::beast::http::field::upgrade), |
| 591 | "websocket")) |
| 592 | { |
| 593 | handler->handleUpgrade(*req, res, std::move(adaptor)); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 594 | // delete lambda with self shared_ptr |
| 595 | // to enable connection destruction |
| 596 | res.completeRequestHandler = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 597 | return; |
| 598 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 599 | handler->handle(*req, res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 600 | } |
| 601 | else |
| 602 | { |
| 603 | completeRequest(); |
| 604 | } |
| 605 | } |
| 606 | else |
| 607 | { |
| 608 | completeRequest(); |
| 609 | } |
| 610 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 611 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 612 | bool isAlive() |
| 613 | { |
| 614 | |
| 615 | if constexpr (std::is_same_v<Adaptor, |
| 616 | boost::beast::ssl_stream< |
| 617 | boost::asio::ip::tcp::socket>>) |
| 618 | { |
| 619 | return adaptor.next_layer().is_open(); |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | return adaptor.is_open(); |
| 624 | } |
| 625 | } |
| 626 | void close() |
| 627 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 628 | if constexpr (std::is_same_v<Adaptor, |
| 629 | boost::beast::ssl_stream< |
| 630 | boost::asio::ip::tcp::socket>>) |
| 631 | { |
| 632 | adaptor.next_layer().close(); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 633 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 634 | if (auto sp = session.lock()) |
| 635 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 636 | BMCWEB_LOG_DEBUG << this |
| 637 | << " Removing TLS session: " << sp->uniqueId; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 638 | persistent_data::SessionStore::getInstance().removeSession(sp); |
| 639 | } |
| 640 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 641 | } |
| 642 | else |
| 643 | { |
| 644 | adaptor.close(); |
| 645 | } |
| 646 | } |
| 647 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 648 | void completeRequest() |
| 649 | { |
| 650 | BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' |
| 651 | << res.resultInt() << " keepalive=" << req->keepAlive(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 652 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 653 | if (needToCallAfterHandlers) |
| 654 | { |
| 655 | needToCallAfterHandlers = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 656 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 657 | // call all afterHandler of middlewares |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 658 | detail::afterHandlersCallHelper<sizeof...(Middlewares) - 1, |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 659 | decltype(ctx), |
| 660 | decltype(*middlewares)>( |
| 661 | *middlewares, ctx, *req, res); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 662 | |
| 663 | crow::authorization::cleanupTempSession(*req); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 664 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 665 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 666 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 667 | { |
| 668 | // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << |
| 669 | // isReading |
| 670 | // << ' ' << isWriting; |
| 671 | // delete this; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 672 | |
| 673 | // delete lambda with self shared_ptr |
| 674 | // to enable connection destruction |
| 675 | res.completeRequestHandler = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 676 | return; |
| 677 | } |
| 678 | if (res.body().empty() && !res.jsonValue.empty()) |
| 679 | { |
| 680 | if (http_helpers::requestPrefersHtml(*req)) |
| 681 | { |
| 682 | prettyPrintJson(res); |
| 683 | } |
| 684 | else |
| 685 | { |
| 686 | res.jsonMode(); |
Jason M. Bills | 193ad2f | 2018-09-26 15:08:52 -0700 | [diff] [blame] | 687 | res.body() = res.jsonValue.dump(2, ' ', true); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 690 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 691 | if (res.resultInt() >= 400 && res.body().empty()) |
| 692 | { |
| 693 | res.body() = std::string(res.reason()); |
| 694 | } |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 695 | |
| 696 | if (res.result() == boost::beast::http::status::no_content) |
| 697 | { |
| 698 | // Boost beast throws if content is provided on a no-content |
| 699 | // response. Ideally, this would never happen, but in the case that |
| 700 | // it does, we don't want to throw. |
| 701 | BMCWEB_LOG_CRITICAL |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 702 | << this << " Response content provided but code was no-content"; |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 703 | res.body().clear(); |
| 704 | } |
| 705 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 706 | res.addHeader(boost::beast::http::field::server, serverName); |
| 707 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
| 708 | |
| 709 | res.keepAlive(req->keepAlive()); |
| 710 | |
| 711 | doWrite(); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 712 | |
| 713 | // delete lambda with self shared_ptr |
| 714 | // to enable connection destruction |
| 715 | res.completeRequestHandler = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | private: |
| 719 | void doReadHeaders() |
| 720 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 721 | BMCWEB_LOG_DEBUG << this << " doReadHeaders"; |
| 722 | |
| 723 | // Clean up any previous Connection. |
| 724 | boost::beast::http::async_read_header( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 725 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 726 | [this, |
| 727 | self(shared_from_this())](const boost::system::error_code& ec, |
| 728 | std::size_t bytes_transferred) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 729 | BMCWEB_LOG_ERROR << this << " async_read_header " |
| 730 | << bytes_transferred << " Bytes"; |
| 731 | bool errorWhileReading = false; |
| 732 | if (ec) |
| 733 | { |
| 734 | errorWhileReading = true; |
| 735 | BMCWEB_LOG_ERROR |
| 736 | << this << " Error while reading: " << ec.message(); |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 741 | // websocket, treat as an error |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 742 | if (!isAlive() && !req->isUpgrade()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 743 | { |
| 744 | errorWhileReading = true; |
| 745 | } |
| 746 | } |
| 747 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 748 | cancelDeadlineTimer(); |
| 749 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 750 | if (errorWhileReading) |
| 751 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 752 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 753 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 754 | return; |
| 755 | } |
| 756 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 757 | if (!req) |
| 758 | { |
| 759 | close(); |
| 760 | return; |
| 761 | } |
| 762 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 763 | // Compute the url parameters for the request |
| 764 | req->url = req->target(); |
| 765 | std::size_t index = req->url.find("?"); |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 766 | if (index != std::string_view::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 767 | { |
Jason M. Bills | 43fcbe5 | 2018-10-16 15:19:20 -0700 | [diff] [blame] | 768 | req->url = req->url.substr(0, index); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 769 | } |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 770 | crow::authorization::authenticate(*req, res); |
| 771 | |
| 772 | bool loggedIn = req && req->session; |
| 773 | if (loggedIn) |
| 774 | { |
| 775 | startDeadline(loggedInAttempts); |
| 776 | BMCWEB_LOG_DEBUG << "Starting slow deadline"; |
| 777 | |
| 778 | req->urlParams = QueryString(std::string(req->target())); |
| 779 | } |
| 780 | else |
| 781 | { |
| 782 | const boost::optional<uint64_t> contentLength = |
| 783 | parser->content_length(); |
| 784 | if (contentLength && |
| 785 | *contentLength > loggedOutPostBodyLimit) |
| 786 | { |
| 787 | BMCWEB_LOG_DEBUG << "Content length greater than limit " |
| 788 | << *contentLength; |
| 789 | close(); |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | startDeadline(loggedOutAttempts); |
| 794 | BMCWEB_LOG_DEBUG << "Starting quick deadline"; |
| 795 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 796 | doRead(); |
| 797 | }); |
| 798 | } |
| 799 | |
| 800 | void doRead() |
| 801 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 802 | BMCWEB_LOG_DEBUG << this << " doRead"; |
| 803 | |
| 804 | boost::beast::http::async_read( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 805 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 806 | [this, |
| 807 | self(shared_from_this())](const boost::system::error_code& ec, |
| 808 | std::size_t bytes_transferred) { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 809 | BMCWEB_LOG_DEBUG << this << " async_read " << bytes_transferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 810 | << " Bytes"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 811 | |
| 812 | bool errorWhileReading = false; |
| 813 | if (ec) |
| 814 | { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 815 | BMCWEB_LOG_ERROR |
| 816 | << this << " Error while reading: " << ec.message(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 817 | errorWhileReading = true; |
| 818 | } |
| 819 | else |
| 820 | { |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 821 | if (isAlive()) |
| 822 | { |
| 823 | cancelDeadlineTimer(); |
| 824 | bool loggedIn = req && req->session; |
| 825 | if (loggedIn) |
| 826 | { |
| 827 | startDeadline(loggedInAttempts); |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | startDeadline(loggedOutAttempts); |
| 832 | } |
| 833 | } |
| 834 | else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 835 | { |
| 836 | errorWhileReading = true; |
| 837 | } |
| 838 | } |
| 839 | if (errorWhileReading) |
| 840 | { |
| 841 | cancelDeadlineTimer(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 842 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 843 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 844 | return; |
| 845 | } |
| 846 | handle(); |
| 847 | }); |
| 848 | } |
| 849 | |
| 850 | void doWrite() |
| 851 | { |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 852 | bool loggedIn = req && req->session; |
| 853 | if (loggedIn) |
| 854 | { |
| 855 | startDeadline(loggedInAttempts); |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | startDeadline(loggedOutAttempts); |
| 860 | } |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 861 | BMCWEB_LOG_DEBUG << this << " doWrite"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 862 | res.preparePayload(); |
| 863 | serializer.emplace(*res.stringResponse); |
| 864 | boost::beast::http::async_write( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 865 | adaptor, *serializer, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 866 | [this, |
| 867 | self(shared_from_this())](const boost::system::error_code& ec, |
| 868 | std::size_t bytes_transferred) { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 869 | BMCWEB_LOG_DEBUG << this << " async_write " << bytes_transferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 870 | << " bytes"; |
| 871 | |
| 872 | if (ec) |
| 873 | { |
| 874 | BMCWEB_LOG_DEBUG << this << " from write(2)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 875 | return; |
| 876 | } |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 877 | if (!res.keepAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 878 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 879 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 880 | BMCWEB_LOG_DEBUG << this << " from write(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | |
| 884 | serializer.reset(); |
| 885 | BMCWEB_LOG_DEBUG << this << " Clearing response"; |
| 886 | res.clear(); |
| 887 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 888 | buffer.consume(buffer.size()); |
| 889 | |
| 890 | req.emplace(parser->get()); |
| 891 | doReadHeaders(); |
| 892 | }); |
| 893 | } |
| 894 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 895 | void cancelDeadlineTimer() |
| 896 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 897 | if (timerCancelKey) |
| 898 | { |
| 899 | BMCWEB_LOG_DEBUG << this << " timer cancelled: " << &timerQueue |
| 900 | << ' ' << *timerCancelKey; |
| 901 | timerQueue.cancel(*timerCancelKey); |
| 902 | timerCancelKey.reset(); |
| 903 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 904 | } |
| 905 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 906 | void startDeadline(size_t timerIterations) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 907 | { |
| 908 | cancelDeadlineTimer(); |
| 909 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 910 | if (timerIterations) |
| 911 | { |
| 912 | timerIterations--; |
| 913 | } |
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 914 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 915 | timerCancelKey = |
| 916 | timerQueue.add([self(shared_from_this()), timerIterations] { |
| 917 | // Mark timer as not active to avoid canceling it during |
| 918 | // Connection destructor which leads to double free issue |
| 919 | self->timerCancelKey.reset(); |
| 920 | if (!self->isAlive()) |
| 921 | { |
| 922 | return; |
| 923 | } |
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 924 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame^] | 925 | // Threshold can be used to drop slow connections |
| 926 | // to protect against slow-rate DoS attack |
| 927 | if (timerIterations) |
| 928 | { |
| 929 | BMCWEB_LOG_DEBUG << self.get() |
| 930 | << " restart timer - read in progress"; |
| 931 | self->startDeadline(timerIterations); |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | self->close(); |
| 936 | }); |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 937 | |
| 938 | if (!timerCancelKey) |
| 939 | { |
| 940 | close(); |
| 941 | return; |
| 942 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 943 | BMCWEB_LOG_DEBUG << this << " timer added: " << &timerQueue << ' ' |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 944 | << *timerCancelKey; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | private: |
| 948 | Adaptor adaptor; |
| 949 | Handler* handler; |
| 950 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 951 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 952 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 953 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 954 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 955 | parser; |
| 956 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 957 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 958 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 959 | std::optional<boost::beast::http::response_serializer< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 960 | boost::beast::http::string_body>> |
| 961 | serializer; |
| 962 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 963 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 964 | crow::Response res; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 965 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 966 | std::weak_ptr<crow::persistent_data::UserSession> session; |
| 967 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 968 | |
| 969 | const std::string& serverName; |
| 970 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 971 | std::optional<size_t> timerCancelKey; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 972 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 973 | bool needToCallAfterHandlers{}; |
| 974 | bool needToStartReadAfterComplete{}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 975 | |
| 976 | std::tuple<Middlewares...>* middlewares; |
| 977 | detail::Context<Middlewares...> ctx; |
| 978 | |
| 979 | std::function<std::string()>& getCachedDateStr; |
| 980 | detail::TimerQueue& timerQueue; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 981 | |
| 982 | using std::enable_shared_from_this< |
| 983 | Connection<Adaptor, Handler, Middlewares...>>::shared_from_this; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 984 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 985 | } // namespace crow |