blob: 67e2aaef046ba5dbb57ca859e15f1a7a97f03864 [file] [log] [blame]
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08001#include "bmcweb_config.h"
2
3#include "app.hpp"
4#include "cors_preflight.hpp"
5#include "dbus_monitor.hpp"
6#include "dbus_singleton.hpp"
Ed Tanousfaf100f2023-05-25 10:03:14 -07007#include "google/google_service_root.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#include "hostname_monitor.hpp"
9#include "ibm/management_console_rest.hpp"
10#include "image_upload.hpp"
11#include "kvm_websocket.hpp"
12#include "login_routes.hpp"
13#include "nbd_proxy.hpp"
14#include "obmc_console.hpp"
15#include "openbmc_dbus_rest.hpp"
16#include "redfish.hpp"
17#include "redfish_aggregator.hpp"
18#include "security_headers.hpp"
19#include "ssl_key_handler.hpp"
Xie Ning9fa06f12022-06-29 18:27:47 +080020#include "user_monitor.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080021#include "vm_websocket.hpp"
22#include "webassets.hpp"
23
Ed Tanous9e6e1b22018-03-16 13:08:50 -070024#include <systemd/sd-daemon.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -070025
Ed Tanous8f626352018-12-19 14:51:54 -080026#include <boost/asio/io_context.hpp>
Ed Tanousaa2e59c2018-04-12 12:17:20 -070027#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/bus.hpp>
29#include <sdbusplus/server.hpp>
Ed Tanouscc5a37f2017-05-11 10:27:23 -070030
Josh Lehan45248ba2022-07-14 14:03:29 -070031#include <exception>
Ed Tanous02379d32020-09-15 21:15:44 -070032#include <memory>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033#include <string>
34
Vernon Mauery168792a2018-01-26 13:42:54 -080035constexpr int defaultPort = 18080;
36
Ed Tanous23a21a12020-07-25 04:45:05 +000037inline void setupSocket(crow::App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070038{
39 int listenFd = sd_listen_fds(0);
40 if (1 == listenFd)
41 {
Ed Tanous62598e32023-07-17 17:06:25 -070042 BMCWEB_LOG_INFO("attempting systemd socket activation");
Ed Tanous1abe55e2018-09-05 08:30:59 -070043 if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1,
Ed Tanouse662eae2022-01-25 10:39:19 -080044 0) != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070045 {
Ed Tanous62598e32023-07-17 17:06:25 -070046 BMCWEB_LOG_INFO("Starting webserver on socket handle {}",
47 SD_LISTEN_FDS_START);
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 app.socket(SD_LISTEN_FDS_START);
49 }
50 else
51 {
Ed Tanous62598e32023-07-17 17:06:25 -070052 BMCWEB_LOG_INFO(
53 "bad incoming socket, starting webserver on port {}",
54 defaultPort);
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 app.port(defaultPort);
56 }
Vernon Mauery168792a2018-01-26 13:42:54 -080057 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 else
59 {
Ed Tanous62598e32023-07-17 17:06:25 -070060 BMCWEB_LOG_INFO("Starting webserver on port {}", defaultPort);
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 app.port(defaultPort);
62 }
Vernon Mauery168792a2018-01-26 13:42:54 -080063}
64
Ed Tanous2119e272022-02-17 18:07:14 -080065static int run()
Ed Tanous1abe55e2018-09-05 08:30:59 -070066{
Ed Tanous8f626352018-12-19 14:51:54 -080067 auto io = std::make_shared<boost::asio::io_context>();
Ed Tanous52cc1122020-07-18 13:51:21 -070068 App app(io);
Ed Tanousb4d29f42017-03-24 16:39:25 -070069
Nan Zhou6384e322022-06-23 22:28:25 +000070 sdbusplus::asio::connection systemBus(*io);
71 crow::connections::systemBus = &systemBus;
AppaRao Pulif6f97192021-03-23 23:36:40 +000072
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 // Static assets need to be initialized before Authorization, because auth
74 // needs to build the whitelist from the static routes
Ed Tanousf0226cd2017-05-16 12:35:38 -070075
Andrew Geisslerd529ee22018-08-10 13:49:54 -070076#ifdef BMCWEB_ENABLE_STATIC_HOSTING
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 crow::webassets::requestRoutes(app);
Ed Tanous1e439872018-05-18 11:48:52 -070078#endif
79
80#ifdef BMCWEB_ENABLE_KVM
Ed Tanous3eb2f352018-12-20 12:30:45 -080081 crow::obmc_kvm::requestRoutes(app);
Ed Tanous1e439872018-05-18 11:48:52 -070082#endif
83
84#ifdef BMCWEB_ENABLE_REDFISH
P Dheeraj Srujan Kumarb614e2b2021-02-05 01:51:15 +053085 redfish::RedfishService redfish(app);
86
87 // Create EventServiceManager instance and initialize Config
Ed Tanousf8ca6d72022-06-28 12:12:03 -070088 redfish::EventServiceManager::getInstance(&*io);
Carson Labrado7fb33562022-04-18 23:26:56 +000089
90#ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
91 // Create RedfishAggregator instance and initialize Config
Ed Tanousf8ca6d72022-06-28 12:12:03 -070092 redfish::RedfishAggregator::getInstance(&*io);
Carson Labrado7fb33562022-04-18 23:26:56 +000093#endif
Ed Tanous1e439872018-05-18 11:48:52 -070094#endif
95
96#ifdef BMCWEB_ENABLE_DBUS_REST
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 crow::dbus_monitor::requestRoutes(app);
98 crow::image_upload::requestRoutes(app);
99 crow::openbmc_mapper::requestRoutes(app);
Ed Tanous1e439872018-05-18 11:48:52 -0700100#endif
101
Ed Tanous77295132018-10-12 11:11:17 -0700102#ifdef BMCWEB_ENABLE_HOST_SERIAL_WEBSOCKET
103 crow::obmc_console::requestRoutes(app);
104#endif
105
Adriana Kobylak1bfbe0e2019-01-17 12:08:38 -0600106#ifdef BMCWEB_ENABLE_VM_WEBSOCKET
107 crow::obmc_vm::requestRoutes(app);
108#endif
109
Ratan Gupta453fed02019-12-14 09:39:47 +0530110#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
111 crow::ibm_mc::requestRoutes(app);
Ratan Gupta07386c62019-12-14 14:06:09 +0530112 crow::ibm_mc_lock::Lock::getInstance();
Ratan Gupta453fed02019-12-14 09:39:47 +0530113#endif
114
Feras Aldahlawi735ef6d2021-03-19 14:01:46 -0700115#ifdef BMCWEB_ENABLE_GOOGLE_API
116 crow::google_api::requestRoutes(app);
117#endif
118
Ed Tanouse662eae2022-01-25 10:39:19 -0800119 if (bmcwebInsecureDisableXssPrevention != 0)
Ed Tanous0260d9d2021-02-07 19:31:07 +0000120 {
121 cors_preflight::requestRoutes(app);
122 }
Ed Tanous52cc1122020-07-18 13:51:21 -0700123
James Feist3909dc82020-04-03 10:58:55 -0700124 crow::login_routes::requestRoutes(app);
Ed Tanous911ac312017-08-15 09:37:42 -0700125
Ed Tanous1abe55e2018-09-05 08:30:59 -0700126 setupSocket(app);
Ed Tanous3dac7492017-08-02 13:46:20 -0700127
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200128#ifdef BMCWEB_ENABLE_VM_NBDPROXY
129 crow::nbd_proxy::requestRoutes(app);
130#endif
131
AppaRao Puli7f4eb582020-06-13 19:01:29 +0530132#ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
133 int rc = redfish::EventServiceManager::startEventLogMonitor(*io);
Ed Tanouse662eae2022-01-25 10:39:19 -0800134 if (rc != 0)
AppaRao Puli7f4eb582020-06-13 19:01:29 +0530135 {
Ed Tanous62598e32023-07-17 17:06:25 -0700136 BMCWEB_LOG_ERROR("Redfish event handler setup failed...");
AppaRao Puli7f4eb582020-06-13 19:01:29 +0530137 return rc;
138 }
139#endif
140
Alan Kuoa8220702020-11-26 11:15:29 +0800141#ifdef BMCWEB_ENABLE_SSL
Ed Tanous62598e32023-07-17 17:06:25 -0700142 BMCWEB_LOG_INFO("Start Hostname Monitor Service...");
Alan Kuoa8220702020-11-26 11:15:29 +0800143 crow::hostname_monitor::registerHostnameSignal();
144#endif
145
Xie Ning9fa06f12022-06-29 18:27:47 +0800146 bmcweb::registerUserRemovedSignal();
147
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 app.run();
149 io->run();
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700150
Nan Zhou6384e322022-06-23 22:28:25 +0000151 crow::connections::systemBus = nullptr;
152
P Dheeraj Srujan Kumar85bf8902021-07-12 21:07:36 +0530153 return 0;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800154}
Ed Tanous24b2fe82022-01-06 12:45:54 -0800155
156int main(int /*argc*/, char** /*argv*/)
157{
158 try
159 {
160 return run();
161 }
Josh Lehan45248ba2022-07-14 14:03:29 -0700162 catch (const std::exception& e)
163 {
Ed Tanous62598e32023-07-17 17:06:25 -0700164 BMCWEB_LOG_CRITICAL("Threw exception to main: {}", e.what());
Josh Lehan45248ba2022-07-14 14:03:29 -0700165 return -1;
166 }
Ed Tanous24b2fe82022-01-06 12:45:54 -0800167 catch (...)
168 {
Ed Tanous62598e32023-07-17 17:06:25 -0700169 BMCWEB_LOG_CRITICAL("Threw exception to main");
Ed Tanous6f2cd972022-06-30 18:09:06 -0700170 return -1;
Ed Tanous24b2fe82022-01-06 12:45:54 -0800171 }
172}