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