Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 5 | #include "bmcweb_config.h" |
| 6 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 7 | #include "http_connect_types.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 8 | #include "http_connection.hpp" |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 9 | #include "io_context_singleton.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 10 | #include "logging.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 11 | #include "ssl_key_handler.hpp" |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 12 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 13 | #include <boost/asio/ip/address.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 14 | #include <boost/asio/ip/tcp.hpp> |
| 15 | #include <boost/asio/signal_set.hpp> |
Ed Tanous | 2f1ebcd | 2019-02-13 19:39:07 -0800 | [diff] [blame] | 16 | #include <boost/asio/ssl/context.hpp> |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 17 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 18 | #include <boost/asio/steady_timer.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 19 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 20 | #include <chrono> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 21 | #include <csignal> |
| 22 | #include <cstddef> |
| 23 | #include <ctime> |
| 24 | #include <functional> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 25 | #include <memory> |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 26 | #include <string> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 27 | #include <utility> |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 28 | #include <vector> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | namespace crow |
| 31 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 32 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 33 | struct Acceptor |
| 34 | { |
| 35 | boost::asio::ip::tcp::acceptor acceptor; |
| 36 | HttpType httpType; |
| 37 | }; |
| 38 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 39 | template <typename Handler, typename Adaptor = boost::asio::ip::tcp::socket> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | class Server |
| 41 | { |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 42 | using self_t = Server<Handler, Adaptor>; |
| 43 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 44 | public: |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 45 | Server(Handler* handlerIn, std::vector<Acceptor>&& acceptorsIn) : |
| 46 | acceptors(std::move(acceptorsIn)), |
| 47 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 48 | // NOLINTNEXTLINE(misc-include-cleaner) |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 49 | signals(getIoContext(), SIGINT, SIGTERM, SIGHUP), handler(handlerIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 50 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 51 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | void updateDateStr() |
| 53 | { |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 54 | time_t lastTimeT = time(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | tm myTm{}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 56 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | gmtime_r(&lastTimeT, &myTm); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 58 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | dateStr.resize(100); |
Ed Tanous | 0f83707 | 2023-06-30 09:57:26 -0700 | [diff] [blame] | 60 | size_t dateStrSz = strftime(dateStr.data(), dateStr.size() - 1, |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 61 | "%a, %d %b %Y %H:%M:%S GMT", &myTm); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | dateStr.resize(dateStrSz); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 63 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 64 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | void run() |
| 66 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 67 | loadCertificate(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | updateDateStr(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 69 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | getCachedDateStr = [this]() -> std::string { |
| 71 | static std::chrono::time_point<std::chrono::steady_clock> |
| 72 | lastDateUpdate = std::chrono::steady_clock::now(); |
| 73 | if (std::chrono::steady_clock::now() - lastDateUpdate >= |
| 74 | std::chrono::seconds(10)) |
| 75 | { |
| 76 | lastDateUpdate = std::chrono::steady_clock::now(); |
| 77 | updateDateStr(); |
| 78 | } |
Ed Tanous | 21b4aba | 2023-06-05 11:42:43 -0700 | [diff] [blame] | 79 | return dateStr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | }; |
Ed Tanous | 9e6e1b2 | 2018-03-16 13:08:50 -0700 | [diff] [blame] | 81 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 82 | for (const Acceptor& accept : acceptors) |
| 83 | { |
| 84 | BMCWEB_LOG_INFO( |
| 85 | "bmcweb server is running, local endpoint {}", |
| 86 | accept.acceptor.local_endpoint().address().to_string()); |
| 87 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 88 | startAsyncWaitForSignal(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 89 | doAccept(); |
| 90 | } |
| 91 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 92 | void loadCertificate() |
| 93 | { |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 94 | if constexpr (BMCWEB_INSECURE_DISABLE_SSL) |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 95 | { |
| 96 | return; |
| 97 | } |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 98 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 99 | adaptorCtx = ensuressl::getSslServerContext(); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void startAsyncWaitForSignal() |
| 103 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 104 | signals.async_wait( |
| 105 | [this](const boost::system::error_code& ec, int signalNo) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 106 | if (ec) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 107 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 108 | BMCWEB_LOG_INFO("Error in signal handler{}", ec.message()); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 109 | } |
| 110 | else |
| 111 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 112 | if (signalNo == SIGHUP) |
| 113 | { |
| 114 | BMCWEB_LOG_INFO("Receivied reload signal"); |
| 115 | loadCertificate(); |
| 116 | startAsyncWaitForSignal(); |
| 117 | } |
| 118 | else |
| 119 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 120 | getIoContext().stop(); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 121 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 122 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 123 | }); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 124 | } |
| 125 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 126 | using SocketPtr = std::unique_ptr<Adaptor>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 127 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 128 | void afterAccept(SocketPtr socket, HttpType httpType, |
| 129 | const boost::system::error_code& ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | { |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 131 | if (ec) |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 132 | { |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 133 | BMCWEB_LOG_ERROR("Failed to accept socket {}", ec); |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 134 | return; |
| 135 | } |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 136 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 137 | boost::asio::steady_timer timer(getIoContext()); |
| 138 | if (adaptorCtx == nullptr) |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 139 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 140 | adaptorCtx = std::make_shared<boost::asio::ssl::context>( |
| 141 | boost::asio::ssl::context::tls_server); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 142 | } |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 143 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 144 | boost::asio::ssl::stream<Adaptor> stream(std::move(*socket), |
| 145 | *adaptorCtx); |
| 146 | using ConnectionType = Connection<Adaptor, Handler>; |
| 147 | auto connection = std::make_shared<ConnectionType>( |
| 148 | handler, httpType, std::move(timer), getCachedDateStr, |
| 149 | std::move(stream)); |
| 150 | |
| 151 | boost::asio::post(getIoContext(), |
| 152 | [connection] { connection->start(); }); |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 153 | |
| 154 | doAccept(); |
| 155 | } |
| 156 | |
| 157 | void doAccept() |
| 158 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 159 | SocketPtr socket = std::make_unique<Adaptor>(getIoContext()); |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 160 | // Keep a raw pointer so when the socket is moved, the pointer is still |
| 161 | // valid |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 162 | Adaptor* socketPtr = socket.get(); |
| 163 | for (Acceptor& accept : acceptors) |
| 164 | { |
| 165 | accept.acceptor.async_accept( |
| 166 | *socketPtr, |
| 167 | std::bind_front(&self_t::afterAccept, this, std::move(socket), |
| 168 | accept.httpType)); |
| 169 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | private: |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 173 | std::function<std::string()> getCachedDateStr; |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 174 | std::vector<Acceptor> acceptors; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 175 | boost::asio::signal_set signals; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 176 | |
| 177 | std::string dateStr; |
| 178 | |
| 179 | Handler* handler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 180 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 181 | std::shared_ptr<boost::asio::ssl::context> adaptorCtx; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 182 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | } // namespace crow |