blob: 3aecae09feede90d592af46ed9ab6e1ef2414cbe [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous911ac312017-08-15 09:37:42 -07003#include <dbus/connection.hpp>
4#include <dbus/endpoint.hpp>
5#include <dbus/filter.hpp>
6#include <dbus/match.hpp>
7#include <dbus/message.hpp>
Ed Tanousba9f9a62017-10-11 16:40:35 -07008#include <persistent_data_middleware.hpp>
9#include <token_authorization_middleware.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070010#include <fstream>
Ed Tanousba9f9a62017-10-11 16:40:35 -070011#include <streambuf>
12#include <string>
Ed Tanous2a866f82017-10-25 17:46:24 -070013#include <crow/app.h>
14#include <boost/algorithm/string.hpp>
Ed Tanous3dac7492017-08-02 13:46:20 -070015namespace crow {
16namespace redfish {
17
18template <typename... Middlewares>
Ed Tanousba9f9a62017-10-11 16:40:35 -070019void get_redfish_sub_routes(Crow<Middlewares...>& app, const std::string& url,
20 nlohmann::json& j) {
Ed Tanous1c74de82017-10-26 13:58:28 -070021 std::vector<const std::string*> routes = app.get_routes(url);
22 for (auto route : routes) {
Ed Tanousba9f9a62017-10-11 16:40:35 -070023 auto redfish_sub_route =
Ed Tanous1c74de82017-10-26 13:58:28 -070024 route->substr(url.size(), route->size() - url.size() - 1);
Ed Tanousba9f9a62017-10-11 16:40:35 -070025 // check if the route is at this level, and we didn't find and exact match
26 // also, filter out resources that start with $ to remove $metadata
27 if (!redfish_sub_route.empty() && redfish_sub_route[0] != '$' &&
28 redfish_sub_route.find('/') == std::string::npos) {
Ed Tanous1c74de82017-10-26 13:58:28 -070029 j[redfish_sub_route] = nlohmann::json{{"@odata.id", *route}};
Ed Tanous911ac312017-08-15 09:37:42 -070030 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070031 }
32}
Ed Tanous911ac312017-08-15 09:37:42 -070033
Ed Tanous2a866f82017-10-25 17:46:24 -070034std::string execute_process(const char* cmd) {
35 std::array<char, 128> buffer;
36 std::string result;
37 std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
38 if (!pipe) throw std::runtime_error("popen() failed!");
39 while (!feof(pipe.get())) {
40 if (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
41 result += buffer.data();
42 }
43 return result;
44}
45
Ed Tanousba9f9a62017-10-11 16:40:35 -070046template <typename... Middlewares>
47void request_routes(Crow<Middlewares...>& app) {
48 CROW_ROUTE(app, "/redfish/")
Ed Tanous911ac312017-08-15 09:37:42 -070049 .methods("GET"_method)([](const crow::request& req, crow::response& res) {
Ed Tanousba9f9a62017-10-11 16:40:35 -070050 res.json_value = {{"v1", "/redfish/v1/"}};
51 res.end();
52 });
53
Ed Tanousba9f9a62017-10-11 16:40:35 -070054 CROW_ROUTE(app, "/redfish/v1/Chassis/")
55 .methods("GET"_method)(
56 [&](const crow::request& req, crow::response& res) {
57 std::vector<std::string> entities;
58 /*std::ifstream f("~/system.json");
59
60 nlohmann::json input = nlohmann::json::parse(f);
61 for (auto it = input.begin(); it != input.end(); it++) {
62 auto value = it.value();
63 if (value["type"] == "Chassis") {
64 std::string str = value["name"];
65 entities.emplace_back(str);
66 }
67 }
68 */
Ed Tanous1c74de82017-10-26 13:58:28 -070069
Ed Tanousba9f9a62017-10-11 16:40:35 -070070 res.json_value = {
71 {"@odata.context",
72 "/redfish/v1/$metadata#ChassisCollection.ChassisCollection"},
73 {"@odata.id", "/redfish/v1/Chassis"},
74 {"@odata.type", "#ChassisCollection.ChassisCollection"},
75 {"Name", "Chassis Collection"},
76 {"Members@odata.count", entities.size()}};
77
78 get_redfish_sub_routes(app, "/redfish/v1/Chassis", res.json_value);
79 res.end();
80 });
81
82 CROW_ROUTE(app, "/redfish/v1/AccountService/")
83 .methods(
84 "GET"_method)([&](const crow::request& req, crow::response& res) {
85 res.json_value = {
86 {"@odata.context",
87 "/redfish/v1/$metadata#AccountService.AccountService"},
88 {"@odata.id", "/redfish/v1/AccountService"},
89 {"@odata.type", "#AccountService.v1_1_0.AccountService"},
90 {"Id", "AccountService"},
91 {"Name", "Account Service"},
92 {"Description", "BMC User Accounts"},
93 {"Status",
94 // TODO(ed) health rollup
95 {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}},
96 {"ServiceEnabled", true},
97 {"MinPasswordLength", 1},
98 {"MaxPasswordLength", 20},
99 };
100 get_redfish_sub_routes(app, "/redfish/v1/AccountService",
101 res.json_value);
102 res.end();
103 });
104
105 CROW_ROUTE(app, "/redfish/v1/AccountService/Roles/")
106 .methods("GET"_method)(
107 [&](const crow::request& req, crow::response& res) {
108 res.json_value = {
109 {"@odata.context",
110 "/redfish/v1/$metadata#RoleCollection.RoleCollection"},
111 {"@odata.id", "/redfish/v1/AccountService/Roles"},
112 {"@odata.type", "#RoleCollection.RoleCollection"},
113 {"Name", "Account Service"},
114 {"Description", "BMC User Roles"},
115 {"Members@odata.count", 1},
116 {"Members",
117 {{"@odata.id",
118 "/redfish/v1/AccountService/Roles/Administrator"}}}};
119 get_redfish_sub_routes(app, "/redfish/v1/AccountService",
120 res.json_value);
121 res.end();
122 });
123
124 CROW_ROUTE(app, "/redfish/v1/AccountService/Roles/Administrator/")
125 .methods("GET"_method)(
126 [&](const crow::request& req, crow::response& res) {
127 res.json_value = {
128 {"@odata.context", "/redfish/v1/$metadata#Role.Role"},
129 {"@odata.id", "/redfish/v1/AccountService/Roles/Administrator"},
130 {"@odata.type", "#Role.v1_0_2.Role"},
131 {"Id", "Administrator"},
132 {"Name", "User Role"},
133 {"Description", "Administrator User Role"},
134 {"IsPredefined", true},
135 {"AssignedPrivileges",
136 {"Login", "ConfigureManager", "ConfigureUsers",
137 "ConfigureSelf", "ConfigureComponents"}}};
138 res.end();
139 });
140
141 CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
142 .methods(
143 "GET"_method)([&](const crow::request& req, crow::response& res) {
Ed Tanous911ac312017-08-15 09:37:42 -0700144 boost::asio::io_service io;
145 auto bus = std::make_shared<dbus::connection>(io, dbus::bus::session);
146 dbus::endpoint user_list("org.openbmc.UserManager",
147 "/org/openbmc/UserManager/Users",
148 "org.openbmc.Enrol", "UserList");
149 bus->async_method_call(
150 [&](const boost::system::error_code ec,
Ed Tanousba9f9a62017-10-11 16:40:35 -0700151 const std::vector<std::string>& users) {
Ed Tanous911ac312017-08-15 09:37:42 -0700152 if (ec) {
153 res.code = 500;
154 } else {
Ed Tanousba9f9a62017-10-11 16:40:35 -0700155 res.json_value = {
Ed Tanous911ac312017-08-15 09:37:42 -0700156 {"@odata.context",
157 "/redfish/v1/"
158 "$metadata#ManagerAccountCollection."
159 "ManagerAccountCollection"},
160 {"@odata.id", "/redfish/v1/AccountService/Accounts"},
161 {"@odata.type",
162 "#ManagerAccountCollection.ManagerAccountCollection"},
163 {"Name", "Accounts Collection"},
164 {"Description", "BMC User Accounts"},
165 {"Members@odata.count", users.size()}};
Ed Tanousba9f9a62017-10-11 16:40:35 -0700166 nlohmann::json member_array = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -0700167 int user_index = 0;
Ed Tanousc963aa42017-10-27 16:00:19 -0700168 for (int user_index = 0; user_index < users.size();
169 user_index++) {
Ed Tanous911ac312017-08-15 09:37:42 -0700170 member_array.push_back(
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100171 {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
172 std::to_string(user_index)}});
Ed Tanous911ac312017-08-15 09:37:42 -0700173 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700174 res.json_value["Members"] = member_array;
Ed Tanous911ac312017-08-15 09:37:42 -0700175 }
176 res.end();
Ed Tanousba9f9a62017-10-11 16:40:35 -0700177 },
178 user_list);
Ed Tanous3dac7492017-08-02 13:46:20 -0700179 });
180
181 CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/<int>/")
Ed Tanousba9f9a62017-10-11 16:40:35 -0700182 .methods("GET"_method)([](const crow::request& req, crow::response& res,
183 int account_index) {
184 res.json_value = {
Ed Tanous3dac7492017-08-02 13:46:20 -0700185 {"@odata.context",
186 "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
187 {"@odata.id", "/redfish/v1/AccountService/Accounts/1"},
188 {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"},
189 {"Id", "1"},
190 {"Name", "User Account"},
191 {"Description", "User Account"},
192 {"Enabled", false},
193 {"Password", nullptr},
194 {"UserName", "anonymous"},
195 {"RoleId", "NoAccess"},
196 {"Links",
197 {{"Role",
198 {{"@odata.id", "/redfish/v1/AccountService/Roles/NoAccess"}}}}}};
Ed Tanousba9f9a62017-10-11 16:40:35 -0700199 res.end();
200 });
201
202 CROW_ROUTE(app, "/redfish/v1/SessionService/")
203 .methods(
204 "GET"_method)([&](const crow::request& req, crow::response& res) {
205 res.json_value = {
206 {"@odata.context",
207 "/redfish/v1/$metadata#SessionService.SessionService"},
208 {"@odata.id", "/redfish/v1/SessionService"},
209 {"@odata.type", "#SessionService.v1_1_1.SessionService"},
210 {"Id", "SessionService"},
211 {"Name", "SessionService"},
212 {"Description", "SessionService"},
213 {"Status",
214 {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}},
215 {"ServiceEnabled", true},
216 // TODO(ed) converge with session timeouts once they exist
217 // Bogus number for now
218 {"SessionTimeout", 1800}};
219 get_redfish_sub_routes(app, "/redfish/v1/AccountService",
220 res.json_value);
221 res.end();
222 });
223
Ed Tanous2a866f82017-10-25 17:46:24 -0700224 CROW_ROUTE(app, "/redfish/v1/Managers/")
225 .methods("GET"_method)(
226 [&](const crow::request& req, crow::response& res) {
227 res.json_value = {
228 {"@odata.context",
229 "/redfish/v1/$metadata#ManagerCollection.ManagerCollection"},
230 {"@odata.id", "/redfish/v1/Managers"},
231 {"@odata.type", "#ManagerCollection.ManagerCollection"},
232 {"Name", "Manager Collection"},
233 {"Members@odata.count", 1},
234 {"Members", {{{"@odata.id", "/redfish/v1/Managers/openbmc"}}}}};
235 res.end();
236 });
237
238 CROW_ROUTE(app, "/redfish/v1/Managers/openbmc/")
239 .methods(
240 "GET"_method)([&](const crow::request& req, crow::response& res) {
241 time_t t = time(NULL);
242 tm* mytime = std::localtime(&t);
243 if (mytime == nullptr) {
244 res.code = 500;
245 res.end();
246 return;
247 }
248 std::array<char, 100> time_buffer;
249 std::size_t len = std::strftime(time_buffer.data(), time_buffer.size(),
250 "%FT%TZ", mytime);
251 if (len == 0) {
252 res.code = 500;
253 res.end();
254 return;
255 }
256 res.json_value = {
257 {"@odata.context", "/redfish/v1/$metadata#Manager.Manager"},
258 {"@odata.id", "/redfish/v1/Managers/openbmc"},
259 {"@odata.type", "#Manager.v1_3_0.Manager"},
260 {"Id", "openbmc"},
261 {"Name", "OpenBmc Manager"},
262 {"Description", "Baseboard Management Controller"},
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100263 {"UUID", app.template get_middleware<PersistentData::Middleware>()
264 .system_uuid},
Ed Tanous2a866f82017-10-25 17:46:24 -0700265 {"Model", "OpenBmc"}, // TODO(ed), get model
266 {"DateTime", time_buffer.data()},
267 {"Status",
268 {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}},
269 {"FirmwareVersion", "1234456789"}, // TODO(ed) get fwversion
270 {"PowerState", "On"}};
271 get_redfish_sub_routes(app, "/redfish/v1/Managers/openbmc/",
272 res.json_value);
273 res.end();
274 });
275
276 CROW_ROUTE(app, "/redfish/v1/Managers/NetworkProtocol/")
277 .methods(
278 "GET"_method)([&](const crow::request& req, crow::response& res) {
279 std::array<char, HOST_NAME_MAX> hostname;
280 if (gethostname(hostname.data(), hostname.size()) != 0) {
281 res.code = 500;
282 res.end();
283 return;
284 }
285 res.json_value = {
286 {"@odata.context",
287 "/redfish/v1/"
288 "$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol"},
289 {"@odata.id", "/redfish/v1/Managers/BMC/NetworkProtocol"},
290 {"@odata.type",
291 "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol"},
292 {"Id", "NetworkProtocol"},
293 {"Name", "Manager Network Protocol"},
294 {"Description", "Manager Network Service"},
295 {"Status",
296 {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}},
297 {"HostName", hostname.data()}}; // TODO(ed) get hostname
298 std::string netstat_out = execute_process("netstat -tuln");
299
300 std::map<int, const char*> service_types{{22, "SSH"},
301 {443, "HTTPS"},
302 {1900, "SSDP"},
303 {623, "IPMI"},
304 {427, "SLP"}};
305
306 std::vector<std::string> lines;
307 boost::split(lines, netstat_out, boost::is_any_of("\n"));
308 auto lines_it = lines.begin();
309 lines_it++; // skip the netstat header
310 lines_it++;
311 while (lines_it != lines.end()) {
312 std::vector<std::string> columns;
313 boost::split(columns, *lines_it, boost::is_any_of("\t "),
314 boost::token_compress_on);
315 if (columns.size() >= 5) {
316 std::size_t found = columns[3].find_last_of(":");
317 if (found != std::string::npos) {
318 std::string port_str = columns[3].substr(found + 1);
319 int port = std::stoi(port_str.c_str());
320 auto type_it = service_types.find(port);
321 if (type_it != service_types.end()) {
Ed Tanous2a866f82017-10-25 17:46:24 -0700322 res.json_value[type_it->second] = {{"ProtocolEnabled", true},
323 {"Port", port}};
324 }
325 }
326 }
327 lines_it++;
328 }
329
330 get_redfish_sub_routes(app, "/redfish/v1/", res.json_value);
331 res.end();
332 });
Ed Tanous3dac7492017-08-02 13:46:20 -0700333}
Ed Tanous911ac312017-08-15 09:37:42 -0700334} // namespace redfish
335} // namespace crow