blob: 37527dd05945da07ef078679c25584466848c4bb [file] [log] [blame]
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010017
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010018#include "error_messages.hpp"
Ed Tanous52cc1122020-07-18 13:51:21 -070019#include "persistent_data.hpp"
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010020
John Edward Broadbent7e860f12021-04-08 15:57:16 -070021#include <app.hpp>
Ed Tanousace85d62021-10-26 12:45:59 -070022#include <http/utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070023#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070024#include <registries/privilege_registry.hpp>
Ed Tanous840098b2022-06-28 12:06:17 -070025#include <utils/json_utils.hpp>
John Edward Broadbent7e860f12021-04-08 15:57:16 -070026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027namespace redfish
28{
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010029
Ed Tanous4f48d5f2021-06-21 08:27:45 -070030inline void fillSessionObject(crow::Response& res,
31 const persistent_data::UserSession& session)
Ed Tanous1abe55e2018-09-05 08:30:59 -070032{
Ed Tanousfaa34cc2021-06-03 13:27:02 -070033 res.jsonValue["Id"] = session.uniqueId;
34 res.jsonValue["UserName"] = session.username;
35 res.jsonValue["@odata.id"] =
36 "/redfish/v1/SessionService/Sessions/" + session.uniqueId;
37 res.jsonValue["@odata.type"] = "#Session.v1_3_0.Session";
38 res.jsonValue["Name"] = "User Session";
39 res.jsonValue["Description"] = "Manager User Session";
40 res.jsonValue["ClientOriginIPAddress"] = session.clientIp;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050041#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
Ed Tanousfaa34cc2021-06-03 13:27:02 -070042 res.jsonValue["Oem"]["OpenBMC"]["@odata.type"] =
43 "#OemSession.v1_0_0.Session";
44 res.jsonValue["Oem"]["OpenBMC"]["ClientID"] = session.clientId;
Sunitha Harish08bdcc72020-05-12 05:17:57 -050045#endif
Ed Tanousfaa34cc2021-06-03 13:27:02 -070046}
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010047
Ed Tanous724340d2022-03-14 09:10:07 -070048inline void
Ed Tanousa1e08712022-07-07 16:10:39 -070049 handleSessionHead(crow::App& app, const crow::Request& req,
50 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
51 const std::string& /*sessionId*/)
Ed Tanous724340d2022-03-14 09:10:07 -070052{
Ed Tanousa1e08712022-07-07 16:10:39 -070053
Carson Labrado3ba00072022-06-06 19:40:56 +000054 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070055 {
56 return;
57 }
Ed Tanousa1e08712022-07-07 16:10:39 -070058 asyncResp->res.addHeader(
59 boost::beast::http::field::link,
60 "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby");
61}
62
63inline void
64 handleSessionGet(crow::App& app, const crow::Request& req,
65 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
66 const std::string& sessionId)
67{
68 handleSessionHead(app, req, asyncResp, sessionId);
69
Ed Tanous724340d2022-03-14 09:10:07 -070070 // Note that control also reaches here via doPost and doDelete.
71 auto session =
72 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
73
74 if (session == nullptr)
75 {
76 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
77 return;
78 }
79
80 fillSessionObject(asyncResp->res, *session);
81}
82
83inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -070084 handleSessionDelete(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -070085 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
86 const std::string& sessionId)
87{
Carson Labrado3ba00072022-06-06 19:40:56 +000088 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070089 {
90 return;
91 }
Ed Tanous724340d2022-03-14 09:10:07 -070092 auto session =
93 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
94
95 if (session == nullptr)
96 {
97 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
98 return;
99 }
100
101 // Perform a proper ConfigureSelf authority check. If a
102 // session is being used to DELETE some other user's session,
103 // then the ConfigureSelf privilege does not apply. In that
104 // case, perform the authority check again without the user's
105 // ConfigureSelf privilege.
wukaihua-fii-na0fd29862022-05-18 09:19:16 +0800106 if (req.session != nullptr && !session->username.empty() &&
107 session->username != req.session->username)
Ed Tanous724340d2022-03-14 09:10:07 -0700108 {
109 Privileges effectiveUserPrivileges =
110 redfish::getUserPrivileges(req.userRole);
111
112 if (!effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"}))
113 {
114 messages::insufficientPrivilege(asyncResp->res);
115 return;
116 }
117 }
118
119 persistent_data::SessionStore::getInstance().removeSession(session);
120 messages::success(asyncResp->res);
121}
122
123inline nlohmann::json getSessionCollectionMembers()
124{
125 std::vector<const std::string*> sessionIds =
126 persistent_data::SessionStore::getInstance().getUniqueIds(
127 false, persistent_data::PersistenceType::TIMEOUT);
128 nlohmann::json ret = nlohmann::json::array();
129 for (const std::string* uid : sessionIds)
130 {
Ed Tanous14766872022-03-15 10:44:42 -0700131 nlohmann::json::object_t session;
132 session["@odata.id"] = "/redfish/v1/SessionService/Sessions/" + *uid;
133 ret.push_back(std::move(session));
Ed Tanous724340d2022-03-14 09:10:07 -0700134 }
135 return ret;
136}
137
Ed Tanousa1e08712022-07-07 16:10:39 -0700138inline void handleSessionCollectionHead(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700139 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700140 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
141{
Ed Tanousa1e08712022-07-07 16:10:39 -0700142
Carson Labrado3ba00072022-06-06 19:40:56 +0000143 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700144 {
145 return;
146 }
Ed Tanousa1e08712022-07-07 16:10:39 -0700147 asyncResp->res.addHeader(
148 boost::beast::http::field::link,
149 "</redfish/v1/JsonSchemas/SessionCollection.json>; rel=describedby");
150}
151
152inline void handleSessionCollectionGet(
153 crow::App& app, const crow::Request& req,
154 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
155{
156 handleSessionCollectionHead(app, req, asyncResp);
Ed Tanous724340d2022-03-14 09:10:07 -0700157 asyncResp->res.jsonValue["Members"] = getSessionCollectionMembers();
158 asyncResp->res.jsonValue["Members@odata.count"] =
159 asyncResp->res.jsonValue["Members"].size();
160 asyncResp->res.jsonValue["@odata.type"] =
161 "#SessionCollection.SessionCollection";
162 asyncResp->res.jsonValue["@odata.id"] =
163 "/redfish/v1/SessionService/Sessions/";
164 asyncResp->res.jsonValue["Name"] = "Session Collection";
165 asyncResp->res.jsonValue["Description"] = "Session Collection";
166}
167
168inline void handleSessionCollectionMembersGet(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700169 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700170 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
171{
Carson Labrado3ba00072022-06-06 19:40:56 +0000172 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700173 {
174 return;
175 }
Ed Tanous724340d2022-03-14 09:10:07 -0700176 asyncResp->res.jsonValue = getSessionCollectionMembers();
177}
178
Ed Tanous4ee8e212022-05-28 09:42:51 -0700179inline void handleSessionCollectionPost(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700180 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700181 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
182{
Carson Labrado3ba00072022-06-06 19:40:56 +0000183 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700184 {
185 return;
186 }
Ed Tanous724340d2022-03-14 09:10:07 -0700187 std::string username;
188 std::string password;
189 std::optional<nlohmann::json> oemObject;
190 std::string clientId;
191 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
192 "Password", password, "Oem", oemObject))
193 {
194 return;
195 }
196
197 if (password.empty() || username.empty() ||
198 asyncResp->res.result() != boost::beast::http::status::ok)
199 {
200 if (username.empty())
201 {
202 messages::propertyMissing(asyncResp->res, "UserName");
203 }
204
205 if (password.empty())
206 {
207 messages::propertyMissing(asyncResp->res, "Password");
208 }
209
210 return;
211 }
212
213 int pamrc = pamAuthenticateUser(username, password);
214 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
215 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
216 {
217 messages::resourceAtUriUnauthorized(asyncResp->res, req.urlView,
218 "Invalid username or password");
219 return;
220 }
221#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
222 if (oemObject)
223 {
224 std::optional<nlohmann::json> bmcOem;
225 if (!json_util::readJson(*oemObject, asyncResp->res, "OpenBMC", bmcOem))
226 {
227 return;
228 }
229 if (!json_util::readJson(*bmcOem, asyncResp->res, "ClientID", clientId))
230 {
231 BMCWEB_LOG_ERROR << "Could not read ClientId";
232 return;
233 }
234 }
235#endif
236
237 // User is authenticated - create session
238 std::shared_ptr<persistent_data::UserSession> session =
239 persistent_data::SessionStore::getInstance().generateUserSession(
240 username, req.ipAddress, clientId,
241 persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly);
Brad Bishop02e53ae2022-07-29 14:38:40 -0400242 if (session == nullptr)
243 {
244 messages::internalError(asyncResp->res);
245 return;
246 }
247
Ed Tanous724340d2022-03-14 09:10:07 -0700248 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
249 asyncResp->res.addHeader(
250 "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId);
251 asyncResp->res.result(boost::beast::http::status::created);
252 if (session->isConfigureSelfOnly)
253 {
254 messages::passwordChangeRequired(
255 asyncResp->res,
256 crow::utility::urlFromPieces("redfish", "v1", "AccountService",
Brad Bishop85e64712022-07-29 12:59:07 -0400257 "Accounts", session->username));
Ed Tanous724340d2022-03-14 09:10:07 -0700258 }
259
260 fillSessionObject(asyncResp->res, *session);
261}
Ed Tanousa1e08712022-07-07 16:10:39 -0700262inline void handleSessionServiceHead(
263 crow::App& app, const crow::Request& req,
264 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
265{
266
267 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
268 {
269 return;
270 }
271 asyncResp->res.addHeader(
272 boost::beast::http::field::link,
273 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby");
274}
Ed Tanous724340d2022-03-14 09:10:07 -0700275inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -0700276 handleSessionServiceGet(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700277 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
278
279{
Ed Tanousa1e08712022-07-07 16:10:39 -0700280 handleSessionServiceHead(app, req, asyncResp);
Ed Tanous724340d2022-03-14 09:10:07 -0700281 asyncResp->res.jsonValue["@odata.type"] =
282 "#SessionService.v1_0_2.SessionService";
283 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
284 asyncResp->res.jsonValue["Name"] = "Session Service";
285 asyncResp->res.jsonValue["Id"] = "SessionService";
286 asyncResp->res.jsonValue["Description"] = "Session Service";
287 asyncResp->res.jsonValue["SessionTimeout"] =
288 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
289 asyncResp->res.jsonValue["ServiceEnabled"] = true;
290
Ed Tanous14766872022-03-15 10:44:42 -0700291 asyncResp->res.jsonValue["Sessions"]["@odata.id"] =
292 "/redfish/v1/SessionService/Sessions";
Ed Tanous724340d2022-03-14 09:10:07 -0700293}
294
295inline void handleSessionServicePatch(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700296 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700297 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
298{
Carson Labrado3ba00072022-06-06 19:40:56 +0000299 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700300 {
301 return;
302 }
Ed Tanous724340d2022-03-14 09:10:07 -0700303 std::optional<int64_t> sessionTimeout;
304 if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout",
305 sessionTimeout))
306 {
307 return;
308 }
309
310 if (sessionTimeout)
311 {
312 // The mininum & maximum allowed values for session timeout
313 // are 30 seconds and 86400 seconds respectively as per the
314 // session service schema mentioned at
315 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
316
317 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
318 {
319 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
320 persistent_data::SessionStore::getInstance().updateSessionTimeout(
321 sessionTimeoutInseconds);
322 messages::propertyValueModified(asyncResp->res, "SessionTimeOut",
323 std::to_string(*sessionTimeout));
324 }
325 else
326 {
327 messages::propertyValueNotInList(asyncResp->res,
328 std::to_string(*sessionTimeout),
329 "SessionTimeOut");
330 }
331 }
332}
333
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700334inline void requestRoutesSession(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700335{
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700336 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700337 .privileges(redfish::privileges::headSession)
338 .methods(boost::beast::http::verb::head)(
339 std::bind_front(handleSessionHead, std::ref(app)));
340
341 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700342 .privileges(redfish::privileges::getSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700343 .methods(boost::beast::http::verb::get)(
344 std::bind_front(handleSessionGet, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100345
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700346 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700347 .privileges(redfish::privileges::deleteSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700348 .methods(boost::beast::http::verb::delete_)(
349 std::bind_front(handleSessionDelete, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700350
351 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700352 .privileges(redfish::privileges::headSessionCollection)
353 .methods(boost::beast::http::verb::head)(
354 std::bind_front(handleSessionCollectionHead, std::ref(app)));
355
356 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanoused398212021-06-09 17:05:54 -0700357 .privileges(redfish::privileges::getSessionCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700358 .methods(boost::beast::http::verb::get)(
359 std::bind_front(handleSessionCollectionGet, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700360
Ed Tanouse76cd862022-03-14 09:12:00 -0700361 // Note, the next two routes technically don't match the privilege
Ed Tanous724340d2022-03-14 09:10:07 -0700362 // registry given the way login mechanisms work. The base privilege
363 // registry lists this endpoint as requiring login privilege, but because
364 // this is the endpoint responsible for giving the login privilege, and it
365 // is itself its own route, it needs to not require Login
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700366 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
367 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700368 .methods(boost::beast::http::verb::post)(
369 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100370
Ed Tanouse76cd862022-03-14 09:12:00 -0700371 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/")
372 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700373 .methods(boost::beast::http::verb::post)(
374 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Ed Tanouse76cd862022-03-14 09:12:00 -0700375
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700376 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700377 .privileges(redfish::privileges::headSessionService)
378 .methods(boost::beast::http::verb::head)(
379 std::bind_front(handleSessionServiceHead, std::ref(app)));
380
381 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700382 .privileges(redfish::privileges::getSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700383 .methods(boost::beast::http::verb::get)(
384 std::bind_front(handleSessionServiceGet, std::ref(app)));
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100385
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700386 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700387 .privileges(redfish::privileges::patchSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700388 .methods(boost::beast::http::verb::patch)(
389 std::bind_front(handleSessionServicePatch, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700390}
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100391
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392} // namespace redfish