blob: e55314a67fdbe26c3b53c2ea30c88d22b102ec5f [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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010019#include "error_messages.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "http/utility.hpp"
Ed Tanous52cc1122020-07-18 13:51:21 -070021#include "persistent_data.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include "query.hpp"
23#include "registries/privilege_registry.hpp"
24#include "utils/json_utils.hpp"
John Edward Broadbent7e860f12021-04-08 15:57:16 -070025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026namespace redfish
27{
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010028
Ed Tanous4f48d5f2021-06-21 08:27:45 -070029inline void fillSessionObject(crow::Response& res,
30 const persistent_data::UserSession& session)
Ed Tanous1abe55e2018-09-05 08:30:59 -070031{
Ed Tanousfaa34cc2021-06-03 13:27:02 -070032 res.jsonValue["Id"] = session.uniqueId;
33 res.jsonValue["UserName"] = session.username;
Willy Tueddfc432022-09-26 16:46:38 +000034 res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
Gunnar Mills03457a92023-02-23 13:41:08 -060035 "redfish", "v1", "SessionService", "Sessions", session.uniqueId);
Ed Tanousbb759e32022-08-02 17:07:54 -070036 res.jsonValue["@odata.type"] = "#Session.v1_5_0.Session";
Ed Tanousfaa34cc2021-06-03 13:27:02 -070037 res.jsonValue["Name"] = "User Session";
38 res.jsonValue["Description"] = "Manager User Session";
39 res.jsonValue["ClientOriginIPAddress"] = session.clientIp;
Ed Tanousbb759e32022-08-02 17:07:54 -070040 if (session.clientId)
41 {
42 res.jsonValue["Context"] = *session.clientId;
43 }
Ed Tanousfaa34cc2021-06-03 13:27:02 -070044}
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010045
Ed Tanous724340d2022-03-14 09:10:07 -070046inline void
Ed Tanousa1e08712022-07-07 16:10:39 -070047 handleSessionHead(crow::App& app, const crow::Request& req,
48 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
49 const std::string& /*sessionId*/)
Ed Tanous724340d2022-03-14 09:10:07 -070050{
Carson Labrado3ba00072022-06-06 19:40:56 +000051 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070052 {
53 return;
54 }
Ed Tanousa1e08712022-07-07 16:10:39 -070055 asyncResp->res.addHeader(
56 boost::beast::http::field::link,
57 "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby");
58}
59
60inline void
61 handleSessionGet(crow::App& app, const crow::Request& req,
62 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
63 const std::string& sessionId)
64{
65 handleSessionHead(app, req, asyncResp, sessionId);
66
Ed Tanous724340d2022-03-14 09:10:07 -070067 // Note that control also reaches here via doPost and doDelete.
68 auto session =
69 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
70
71 if (session == nullptr)
72 {
73 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
74 return;
75 }
76
77 fillSessionObject(asyncResp->res, *session);
78}
79
80inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -070081 handleSessionDelete(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -070082 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
83 const std::string& sessionId)
84{
Carson Labrado3ba00072022-06-06 19:40:56 +000085 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070086 {
87 return;
88 }
Ed Tanous724340d2022-03-14 09:10:07 -070089 auto session =
90 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
91
92 if (session == nullptr)
93 {
94 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
95 return;
96 }
97
98 // Perform a proper ConfigureSelf authority check. If a
99 // session is being used to DELETE some other user's session,
100 // then the ConfigureSelf privilege does not apply. In that
101 // case, perform the authority check again without the user's
102 // ConfigureSelf privilege.
wukaihua-fii-na0fd29862022-05-18 09:19:16 +0800103 if (req.session != nullptr && !session->username.empty() &&
104 session->username != req.session->username)
Ed Tanous724340d2022-03-14 09:10:07 -0700105 {
106 Privileges effectiveUserPrivileges =
107 redfish::getUserPrivileges(req.userRole);
108
109 if (!effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"}))
110 {
111 messages::insufficientPrivilege(asyncResp->res);
112 return;
113 }
114 }
115
116 persistent_data::SessionStore::getInstance().removeSession(session);
117 messages::success(asyncResp->res);
118}
119
120inline nlohmann::json getSessionCollectionMembers()
121{
122 std::vector<const std::string*> sessionIds =
123 persistent_data::SessionStore::getInstance().getUniqueIds(
124 false, persistent_data::PersistenceType::TIMEOUT);
125 nlohmann::json ret = nlohmann::json::array();
126 for (const std::string* uid : sessionIds)
127 {
Ed Tanous14766872022-03-15 10:44:42 -0700128 nlohmann::json::object_t session;
Willy Tueddfc432022-09-26 16:46:38 +0000129 session["@odata.id"] = crow::utility::urlFromPieces(
Gunnar Mills03457a92023-02-23 13:41:08 -0600130 "redfish", "v1", "SessionService", "Sessions", *uid);
Ed Tanous14766872022-03-15 10:44:42 -0700131 ret.push_back(std::move(session));
Ed Tanous724340d2022-03-14 09:10:07 -0700132 }
133 return ret;
134}
135
Ed Tanousa1e08712022-07-07 16:10:39 -0700136inline void handleSessionCollectionHead(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700137 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700138 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
139{
Carson Labrado3ba00072022-06-06 19:40:56 +0000140 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700141 {
142 return;
143 }
Ed Tanousa1e08712022-07-07 16:10:39 -0700144 asyncResp->res.addHeader(
145 boost::beast::http::field::link,
146 "</redfish/v1/JsonSchemas/SessionCollection.json>; rel=describedby");
147}
148
149inline void handleSessionCollectionGet(
150 crow::App& app, const crow::Request& req,
151 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
152{
153 handleSessionCollectionHead(app, req, asyncResp);
Ed Tanous724340d2022-03-14 09:10:07 -0700154 asyncResp->res.jsonValue["Members"] = getSessionCollectionMembers();
155 asyncResp->res.jsonValue["Members@odata.count"] =
156 asyncResp->res.jsonValue["Members"].size();
157 asyncResp->res.jsonValue["@odata.type"] =
158 "#SessionCollection.SessionCollection";
159 asyncResp->res.jsonValue["@odata.id"] =
160 "/redfish/v1/SessionService/Sessions/";
161 asyncResp->res.jsonValue["Name"] = "Session Collection";
162 asyncResp->res.jsonValue["Description"] = "Session Collection";
163}
164
165inline void handleSessionCollectionMembersGet(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700166 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700167 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
168{
Carson Labrado3ba00072022-06-06 19:40:56 +0000169 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700170 {
171 return;
172 }
Ed Tanous724340d2022-03-14 09:10:07 -0700173 asyncResp->res.jsonValue = getSessionCollectionMembers();
174}
175
Ed Tanous4ee8e212022-05-28 09:42:51 -0700176inline void handleSessionCollectionPost(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700177 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700178 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
179{
Carson Labrado3ba00072022-06-06 19:40:56 +0000180 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700181 {
182 return;
183 }
Ed Tanous724340d2022-03-14 09:10:07 -0700184 std::string username;
185 std::string password;
Ed Tanousbb759e32022-08-02 17:07:54 -0700186 std::optional<std::string> clientId;
Ed Tanous724340d2022-03-14 09:10:07 -0700187 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
Ed Tanousd678d4f2023-01-07 14:40:40 -0800188 "Password", password, "Context", clientId))
Ed Tanous724340d2022-03-14 09:10:07 -0700189 {
190 return;
191 }
192
193 if (password.empty() || username.empty() ||
194 asyncResp->res.result() != boost::beast::http::status::ok)
195 {
196 if (username.empty())
197 {
198 messages::propertyMissing(asyncResp->res, "UserName");
199 }
200
201 if (password.empty())
202 {
203 messages::propertyMissing(asyncResp->res, "Password");
204 }
205
206 return;
207 }
208
209 int pamrc = pamAuthenticateUser(username, password);
210 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
211 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
212 {
Ed Tanous39662a32023-02-06 15:09:46 -0800213 messages::resourceAtUriUnauthorized(asyncResp->res, req.url(),
Ed Tanous724340d2022-03-14 09:10:07 -0700214 "Invalid username or password");
215 return;
216 }
Ed Tanous724340d2022-03-14 09:10:07 -0700217
218 // User is authenticated - create session
219 std::shared_ptr<persistent_data::UserSession> session =
220 persistent_data::SessionStore::getInstance().generateUserSession(
221 username, req.ipAddress, clientId,
222 persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly);
Brad Bishop02e53ae2022-07-29 14:38:40 -0400223 if (session == nullptr)
224 {
225 messages::internalError(asyncResp->res);
226 return;
227 }
228
Ed Tanous724340d2022-03-14 09:10:07 -0700229 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
230 asyncResp->res.addHeader(
231 "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId);
232 asyncResp->res.result(boost::beast::http::status::created);
233 if (session->isConfigureSelfOnly)
234 {
235 messages::passwordChangeRequired(
236 asyncResp->res,
237 crow::utility::urlFromPieces("redfish", "v1", "AccountService",
Brad Bishop85e64712022-07-29 12:59:07 -0400238 "Accounts", session->username));
Ed Tanous724340d2022-03-14 09:10:07 -0700239 }
240
241 fillSessionObject(asyncResp->res, *session);
242}
Ed Tanousa1e08712022-07-07 16:10:39 -0700243inline void handleSessionServiceHead(
244 crow::App& app, const crow::Request& req,
245 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
246{
Ed Tanousa1e08712022-07-07 16:10:39 -0700247 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
248 {
249 return;
250 }
251 asyncResp->res.addHeader(
252 boost::beast::http::field::link,
253 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby");
254}
Ed Tanous724340d2022-03-14 09:10:07 -0700255inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -0700256 handleSessionServiceGet(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700257 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
258
259{
Ed Tanousa1e08712022-07-07 16:10:39 -0700260 handleSessionServiceHead(app, req, asyncResp);
Ed Tanous724340d2022-03-14 09:10:07 -0700261 asyncResp->res.jsonValue["@odata.type"] =
262 "#SessionService.v1_0_2.SessionService";
263 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
264 asyncResp->res.jsonValue["Name"] = "Session Service";
265 asyncResp->res.jsonValue["Id"] = "SessionService";
266 asyncResp->res.jsonValue["Description"] = "Session Service";
267 asyncResp->res.jsonValue["SessionTimeout"] =
268 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
269 asyncResp->res.jsonValue["ServiceEnabled"] = true;
270
Ed Tanous14766872022-03-15 10:44:42 -0700271 asyncResp->res.jsonValue["Sessions"]["@odata.id"] =
272 "/redfish/v1/SessionService/Sessions";
Ed Tanous724340d2022-03-14 09:10:07 -0700273}
274
275inline void handleSessionServicePatch(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700276 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700277 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
278{
Carson Labrado3ba00072022-06-06 19:40:56 +0000279 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700280 {
281 return;
282 }
Ed Tanous724340d2022-03-14 09:10:07 -0700283 std::optional<int64_t> sessionTimeout;
284 if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout",
285 sessionTimeout))
286 {
287 return;
288 }
289
290 if (sessionTimeout)
291 {
292 // The mininum & maximum allowed values for session timeout
293 // are 30 seconds and 86400 seconds respectively as per the
294 // session service schema mentioned at
295 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
296
297 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
298 {
299 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
300 persistent_data::SessionStore::getInstance().updateSessionTimeout(
301 sessionTimeoutInseconds);
302 messages::propertyValueModified(asyncResp->res, "SessionTimeOut",
303 std::to_string(*sessionTimeout));
304 }
305 else
306 {
307 messages::propertyValueNotInList(asyncResp->res,
308 std::to_string(*sessionTimeout),
309 "SessionTimeOut");
310 }
311 }
312}
313
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700314inline void requestRoutesSession(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700315{
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700316 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700317 .privileges(redfish::privileges::headSession)
318 .methods(boost::beast::http::verb::head)(
319 std::bind_front(handleSessionHead, std::ref(app)));
320
321 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700322 .privileges(redfish::privileges::getSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700323 .methods(boost::beast::http::verb::get)(
324 std::bind_front(handleSessionGet, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100325
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700326 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700327 .privileges(redfish::privileges::deleteSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700328 .methods(boost::beast::http::verb::delete_)(
329 std::bind_front(handleSessionDelete, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700330
331 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700332 .privileges(redfish::privileges::headSessionCollection)
333 .methods(boost::beast::http::verb::head)(
334 std::bind_front(handleSessionCollectionHead, std::ref(app)));
335
336 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanoused398212021-06-09 17:05:54 -0700337 .privileges(redfish::privileges::getSessionCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700338 .methods(boost::beast::http::verb::get)(
339 std::bind_front(handleSessionCollectionGet, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700340
Ed Tanouse76cd862022-03-14 09:12:00 -0700341 // Note, the next two routes technically don't match the privilege
Ed Tanous724340d2022-03-14 09:10:07 -0700342 // registry given the way login mechanisms work. The base privilege
343 // registry lists this endpoint as requiring login privilege, but because
344 // this is the endpoint responsible for giving the login privilege, and it
345 // is itself its own route, it needs to not require Login
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700346 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
347 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700348 .methods(boost::beast::http::verb::post)(
349 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100350
Ed Tanouse76cd862022-03-14 09:12:00 -0700351 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/")
352 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700353 .methods(boost::beast::http::verb::post)(
354 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Ed Tanouse76cd862022-03-14 09:12:00 -0700355
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700356 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700357 .privileges(redfish::privileges::headSessionService)
358 .methods(boost::beast::http::verb::head)(
359 std::bind_front(handleSessionServiceHead, std::ref(app)));
360
361 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700362 .privileges(redfish::privileges::getSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700363 .methods(boost::beast::http::verb::get)(
364 std::bind_front(handleSessionServiceGet, std::ref(app)));
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100365
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700366 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700367 .privileges(redfish::privileges::patchSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700368 .methods(boost::beast::http::verb::patch)(
369 std::bind_front(handleSessionServicePatch, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700370}
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100371
Ed Tanous1abe55e2018-09-05 08:30:59 -0700372} // namespace redfish