Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "http_connection.hpp" |
| 4 | #include "logging.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "ssl_key_handler.hpp" |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 6 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 7 | #include <boost/asio/ip/address.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 8 | #include <boost/asio/ip/tcp.hpp> |
| 9 | #include <boost/asio/signal_set.hpp> |
Ed Tanous | 2f1ebcd | 2019-02-13 19:39:07 -0800 | [diff] [blame] | 10 | #include <boost/asio/ssl/context.hpp> |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 11 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 12 | #include <boost/asio/steady_timer.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 13 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 14 | #include <atomic> |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 15 | #include <chrono> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 16 | #include <cstdint> |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 17 | #include <filesystem> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 18 | #include <future> |
| 19 | #include <memory> |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame^] | 20 | #include <string> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 21 | #include <utility> |
| 22 | #include <vector> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | namespace crow |
| 25 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 26 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 27 | template <typename Handler, typename Adaptor = boost::asio::ip::tcp::socket> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | class Server |
| 29 | { |
| 30 | public: |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 31 | Server(Handler* handlerIn, boost::asio::ip::tcp::acceptor&& acceptorIn, |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 32 | std::shared_ptr<boost::asio::ssl::context> adaptorCtxIn, |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 33 | std::shared_ptr<boost::asio::io_context> io) : |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | ioService(std::move(io)), |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 35 | acceptor(std::move(acceptorIn)), |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 36 | signals(*ioService, SIGINT, SIGTERM, SIGHUP), handler(handlerIn), |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 37 | adaptorCtx(std::move(adaptorCtxIn)) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 38 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 39 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | void updateDateStr() |
| 41 | { |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 42 | time_t lastTimeT = time(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | tm myTm{}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 44 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | gmtime_r(&lastTimeT, &myTm); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 46 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | dateStr.resize(100); |
Ed Tanous | 0f83707 | 2023-06-30 09:57:26 -0700 | [diff] [blame] | 48 | size_t dateStrSz = strftime(dateStr.data(), dateStr.size() - 1, |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 49 | "%a, %d %b %Y %H:%M:%S GMT", &myTm); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | dateStr.resize(dateStrSz); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 51 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 52 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | void run() |
| 54 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 55 | loadCertificate(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | updateDateStr(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 57 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | getCachedDateStr = [this]() -> std::string { |
| 59 | static std::chrono::time_point<std::chrono::steady_clock> |
| 60 | lastDateUpdate = std::chrono::steady_clock::now(); |
| 61 | if (std::chrono::steady_clock::now() - lastDateUpdate >= |
| 62 | std::chrono::seconds(10)) |
| 63 | { |
| 64 | lastDateUpdate = std::chrono::steady_clock::now(); |
| 65 | updateDateStr(); |
| 66 | } |
Ed Tanous | 21b4aba | 2023-06-05 11:42:43 -0700 | [diff] [blame] | 67 | return dateStr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | }; |
Ed Tanous | 9e6e1b2 | 2018-03-16 13:08:50 -0700 | [diff] [blame] | 69 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 70 | BMCWEB_LOG_INFO("bmcweb server is running, local endpoint {}", |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 71 | acceptor.local_endpoint().address().to_string()); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 72 | startAsyncWaitForSignal(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | doAccept(); |
| 74 | } |
| 75 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 76 | void loadCertificate() |
| 77 | { |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 78 | if constexpr (BMCWEB_INSECURE_DISABLE_SSL) |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 79 | { |
| 80 | return; |
| 81 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 82 | namespace fs = std::filesystem; |
| 83 | // Cleanup older certificate file existing in the system |
| 84 | fs::path oldCert = "/home/root/server.pem"; |
| 85 | if (fs::exists(oldCert)) |
| 86 | { |
| 87 | fs::remove("/home/root/server.pem"); |
| 88 | } |
| 89 | fs::path certPath = "/etc/ssl/certs/https/"; |
| 90 | // if path does not exist create the path so that |
| 91 | // self signed certificate can be created in the |
| 92 | // path |
| 93 | if (!fs::exists(certPath)) |
| 94 | { |
| 95 | fs::create_directories(certPath); |
| 96 | } |
| 97 | fs::path certFile = certPath / "server.pem"; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 98 | BMCWEB_LOG_INFO("Building SSL Context file={}", certFile.string()); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 99 | std::string sslPemFile(certFile); |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame^] | 100 | std::string cert = |
| 101 | ensuressl::ensureOpensslKeyPresentAndValid(sslPemFile); |
| 102 | if (cert.empty()) |
| 103 | { |
| 104 | throw std::runtime_error("Failed to load string"); |
| 105 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 106 | std::shared_ptr<boost::asio::ssl::context> sslContext = |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame^] | 107 | ensuressl::getSslContext(cert); |
| 108 | if (sslContext == nullptr) |
| 109 | { |
| 110 | throw std::runtime_error("Failed to load certificate"); |
| 111 | } |
| 112 | BMCWEB_LOG_DEBUG("Replaced certificate"); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 113 | adaptorCtx = sslContext; |
| 114 | handler->ssl(std::move(sslContext)); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void startAsyncWaitForSignal() |
| 118 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 119 | signals.async_wait( |
| 120 | [this](const boost::system::error_code& ec, int signalNo) { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 121 | if (ec) |
| 122 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 123 | BMCWEB_LOG_INFO("Error in signal handler{}", ec.message()); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 124 | } |
| 125 | else |
| 126 | { |
| 127 | if (signalNo == SIGHUP) |
| 128 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 129 | BMCWEB_LOG_INFO("Receivied reload signal"); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 130 | loadCertificate(); |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 131 | boost::system::error_code ec2; |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 132 | acceptor.cancel(ec2); |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 133 | if (ec2) |
Zbigniew Lukwinski | 7d0120b | 2019-10-15 09:12:45 +0200 | [diff] [blame] | 134 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 135 | BMCWEB_LOG_ERROR( |
| 136 | "Error while canceling async operations:{}", |
| 137 | ec2.message()); |
Zbigniew Lukwinski | 7d0120b | 2019-10-15 09:12:45 +0200 | [diff] [blame] | 138 | } |
Ed Tanous | 21b4aba | 2023-06-05 11:42:43 -0700 | [diff] [blame] | 139 | startAsyncWaitForSignal(); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 140 | } |
| 141 | else |
| 142 | { |
| 143 | stop(); |
| 144 | } |
| 145 | } |
| 146 | }); |
| 147 | } |
| 148 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | void stop() |
| 150 | { |
| 151 | ioService->stop(); |
| 152 | } |
| 153 | |
| 154 | void doAccept() |
| 155 | { |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 156 | if (ioService == nullptr) |
| 157 | { |
| 158 | BMCWEB_LOG_CRITICAL("IoService was null"); |
| 159 | return; |
| 160 | } |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 161 | boost::asio::steady_timer timer(*ioService); |
Ed Tanous | 88e1612 | 2021-12-06 14:14:43 -0800 | [diff] [blame] | 162 | std::shared_ptr<Connection<Adaptor, Handler>> connection; |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 163 | if constexpr (std::is_same<Adaptor, |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 164 | boost::asio::ssl::stream< |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 165 | boost::asio::ip::tcp::socket>>::value) |
| 166 | { |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 167 | if (adaptorCtx == nullptr) |
| 168 | { |
| 169 | BMCWEB_LOG_CRITICAL( |
| 170 | "Asked to lauch TLS socket but no context available"); |
| 171 | return; |
| 172 | } |
Ed Tanous | 88e1612 | 2021-12-06 14:14:43 -0800 | [diff] [blame] | 173 | connection = std::make_shared<Connection<Adaptor, Handler>>( |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 174 | handler, std::move(timer), getCachedDateStr, |
Ed Tanous | 88e1612 | 2021-12-06 14:14:43 -0800 | [diff] [blame] | 175 | Adaptor(*ioService, *adaptorCtx)); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
Ed Tanous | 88e1612 | 2021-12-06 14:14:43 -0800 | [diff] [blame] | 179 | connection = std::make_shared<Connection<Adaptor, Handler>>( |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 180 | handler, std::move(timer), getCachedDateStr, |
Ed Tanous | 88e1612 | 2021-12-06 14:14:43 -0800 | [diff] [blame] | 181 | Adaptor(*ioService)); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 182 | } |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 183 | acceptor.async_accept( |
Ed Tanous | 88e1612 | 2021-12-06 14:14:43 -0800 | [diff] [blame] | 184 | boost::beast::get_lowest_layer(connection->socket()), |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 185 | [this, connection](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 186 | if (!ec) |
| 187 | { |
Ed Tanous | 21b4aba | 2023-06-05 11:42:43 -0700 | [diff] [blame] | 188 | boost::asio::post(*ioService, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 189 | [connection] { connection->start(); }); |
| 190 | } |
| 191 | doAccept(); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 192 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | private: |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 196 | std::shared_ptr<boost::asio::io_context> ioService; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 197 | std::function<std::string()> getCachedDateStr; |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 198 | boost::asio::ip::tcp::acceptor acceptor; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 199 | boost::asio::signal_set signals; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 200 | |
| 201 | std::string dateStr; |
| 202 | |
| 203 | Handler* handler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 204 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 205 | std::shared_ptr<boost::asio::ssl::context> adaptorCtx; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 206 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 207 | } // namespace crow |