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