blob: 494f15ccf9b2d824d8f1fbe84d26e1d531269283 [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 Tanousef4c65b2023-04-24 15:28:50 -070026#include <boost/url/format.hpp>
27
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace redfish
29{
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010030
Ed Tanous4f48d5f2021-06-21 08:27:45 -070031inline void fillSessionObject(crow::Response& res,
32 const persistent_data::UserSession& session)
Ed Tanous1abe55e2018-09-05 08:30:59 -070033{
Ed Tanousfaa34cc2021-06-03 13:27:02 -070034 res.jsonValue["Id"] = session.uniqueId;
35 res.jsonValue["UserName"] = session.username;
Ed Tanousef4c65b2023-04-24 15:28:50 -070036 res.jsonValue["@odata.id"] = boost::urls::format(
37 "/redfish/v1/SessionService/Sessions/{}", session.uniqueId);
Ed Tanousbb759e32022-08-02 17:07:54 -070038 res.jsonValue["@odata.type"] = "#Session.v1_5_0.Session";
Ed Tanousfaa34cc2021-06-03 13:27:02 -070039 res.jsonValue["Name"] = "User Session";
40 res.jsonValue["Description"] = "Manager User Session";
41 res.jsonValue["ClientOriginIPAddress"] = session.clientIp;
Ed Tanousbb759e32022-08-02 17:07:54 -070042 if (session.clientId)
43 {
44 res.jsonValue["Context"] = *session.clientId;
45 }
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{
Carson Labrado3ba00072022-06-06 19:40:56 +000053 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070054 {
55 return;
56 }
Ed Tanousa1e08712022-07-07 16:10:39 -070057 asyncResp->res.addHeader(
58 boost::beast::http::field::link,
59 "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby");
60}
61
62inline void
63 handleSessionGet(crow::App& app, const crow::Request& req,
64 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
65 const std::string& sessionId)
66{
Ed Tanous65ffbcb2023-05-16 08:54:11 -070067 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
68 {
69 return;
70 }
71 asyncResp->res.addHeader(
72 boost::beast::http::field::link,
73 "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby");
Ed Tanousa1e08712022-07-07 16:10:39 -070074
Ed Tanous724340d2022-03-14 09:10:07 -070075 // Note that control also reaches here via doPost and doDelete.
76 auto session =
77 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
78
79 if (session == nullptr)
80 {
81 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
82 return;
83 }
84
85 fillSessionObject(asyncResp->res, *session);
86}
87
88inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -070089 handleSessionDelete(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -070090 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
91 const std::string& sessionId)
92{
Carson Labrado3ba00072022-06-06 19:40:56 +000093 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070094 {
95 return;
96 }
Ed Tanous724340d2022-03-14 09:10:07 -070097 auto session =
98 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
99
100 if (session == nullptr)
101 {
102 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
103 return;
104 }
105
106 // Perform a proper ConfigureSelf authority check. If a
107 // session is being used to DELETE some other user's session,
108 // then the ConfigureSelf privilege does not apply. In that
109 // case, perform the authority check again without the user's
110 // ConfigureSelf privilege.
wukaihua-fii-na0fd29862022-05-18 09:19:16 +0800111 if (req.session != nullptr && !session->username.empty() &&
112 session->username != req.session->username)
Ed Tanous724340d2022-03-14 09:10:07 -0700113 {
114 Privileges effectiveUserPrivileges =
115 redfish::getUserPrivileges(req.userRole);
116
117 if (!effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"}))
118 {
119 messages::insufficientPrivilege(asyncResp->res);
120 return;
121 }
122 }
123
124 persistent_data::SessionStore::getInstance().removeSession(session);
125 messages::success(asyncResp->res);
126}
127
128inline nlohmann::json getSessionCollectionMembers()
129{
130 std::vector<const std::string*> sessionIds =
131 persistent_data::SessionStore::getInstance().getUniqueIds(
132 false, persistent_data::PersistenceType::TIMEOUT);
133 nlohmann::json ret = nlohmann::json::array();
134 for (const std::string* uid : sessionIds)
135 {
Ed Tanous14766872022-03-15 10:44:42 -0700136 nlohmann::json::object_t session;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700137 session["@odata.id"] =
138 boost::urls::format("/redfish/v1/SessionService/Sessions/{}", *uid);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500139 ret.emplace_back(std::move(session));
Ed Tanous724340d2022-03-14 09:10:07 -0700140 }
141 return ret;
142}
143
Ed Tanousa1e08712022-07-07 16:10:39 -0700144inline void handleSessionCollectionHead(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700145 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700146 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
147{
Carson Labrado3ba00072022-06-06 19:40:56 +0000148 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700149 {
150 return;
151 }
Ed Tanousa1e08712022-07-07 16:10:39 -0700152 asyncResp->res.addHeader(
153 boost::beast::http::field::link,
154 "</redfish/v1/JsonSchemas/SessionCollection.json>; rel=describedby");
155}
156
157inline void handleSessionCollectionGet(
158 crow::App& app, const crow::Request& req,
159 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
160{
161 handleSessionCollectionHead(app, req, asyncResp);
Ed Tanous724340d2022-03-14 09:10:07 -0700162 asyncResp->res.jsonValue["Members"] = getSessionCollectionMembers();
163 asyncResp->res.jsonValue["Members@odata.count"] =
164 asyncResp->res.jsonValue["Members"].size();
165 asyncResp->res.jsonValue["@odata.type"] =
166 "#SessionCollection.SessionCollection";
167 asyncResp->res.jsonValue["@odata.id"] =
168 "/redfish/v1/SessionService/Sessions/";
169 asyncResp->res.jsonValue["Name"] = "Session Collection";
170 asyncResp->res.jsonValue["Description"] = "Session Collection";
171}
172
173inline void handleSessionCollectionMembersGet(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700174 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700175 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
176{
Carson Labrado3ba00072022-06-06 19:40:56 +0000177 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700178 {
179 return;
180 }
Ed Tanous724340d2022-03-14 09:10:07 -0700181 asyncResp->res.jsonValue = getSessionCollectionMembers();
182}
183
Ed Tanous4ee8e212022-05-28 09:42:51 -0700184inline void handleSessionCollectionPost(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700185 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700186 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
187{
Carson Labrado3ba00072022-06-06 19:40:56 +0000188 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700189 {
190 return;
191 }
Ed Tanous724340d2022-03-14 09:10:07 -0700192 std::string username;
193 std::string password;
Ed Tanousbb759e32022-08-02 17:07:54 -0700194 std::optional<std::string> clientId;
Ed Tanous724340d2022-03-14 09:10:07 -0700195 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
Ed Tanousd678d4f2023-01-07 14:40:40 -0800196 "Password", password, "Context", clientId))
Ed Tanous724340d2022-03-14 09:10:07 -0700197 {
198 return;
199 }
200
201 if (password.empty() || username.empty() ||
202 asyncResp->res.result() != boost::beast::http::status::ok)
203 {
204 if (username.empty())
205 {
206 messages::propertyMissing(asyncResp->res, "UserName");
207 }
208
209 if (password.empty())
210 {
211 messages::propertyMissing(asyncResp->res, "Password");
212 }
213
214 return;
215 }
216
217 int pamrc = pamAuthenticateUser(username, password);
218 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
219 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
220 {
Ed Tanous39662a32023-02-06 15:09:46 -0800221 messages::resourceAtUriUnauthorized(asyncResp->res, req.url(),
Ed Tanous724340d2022-03-14 09:10:07 -0700222 "Invalid username or password");
223 return;
224 }
Ed Tanous724340d2022-03-14 09:10:07 -0700225
226 // User is authenticated - create session
227 std::shared_ptr<persistent_data::UserSession> session =
228 persistent_data::SessionStore::getInstance().generateUserSession(
229 username, req.ipAddress, clientId,
230 persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly);
Brad Bishop02e53ae2022-07-29 14:38:40 -0400231 if (session == nullptr)
232 {
233 messages::internalError(asyncResp->res);
234 return;
235 }
236
Ed Tanous724340d2022-03-14 09:10:07 -0700237 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
238 asyncResp->res.addHeader(
239 "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId);
240 asyncResp->res.result(boost::beast::http::status::created);
241 if (session->isConfigureSelfOnly)
242 {
243 messages::passwordChangeRequired(
244 asyncResp->res,
Ed Tanousef4c65b2023-04-24 15:28:50 -0700245 boost::urls::format("/redfish/v1/AccountService/Accounts/{}",
246 session->username));
Ed Tanous724340d2022-03-14 09:10:07 -0700247 }
248
249 fillSessionObject(asyncResp->res, *session);
250}
Ed Tanousa1e08712022-07-07 16:10:39 -0700251inline void handleSessionServiceHead(
252 crow::App& app, const crow::Request& req,
253 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
254{
Ed Tanousa1e08712022-07-07 16:10:39 -0700255 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
256 {
257 return;
258 }
259 asyncResp->res.addHeader(
260 boost::beast::http::field::link,
261 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby");
262}
Ed Tanous724340d2022-03-14 09:10:07 -0700263inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -0700264 handleSessionServiceGet(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700265 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
266
267{
Ed Tanousa1e08712022-07-07 16:10:39 -0700268 handleSessionServiceHead(app, req, asyncResp);
Ed Tanous724340d2022-03-14 09:10:07 -0700269 asyncResp->res.jsonValue["@odata.type"] =
270 "#SessionService.v1_0_2.SessionService";
271 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
272 asyncResp->res.jsonValue["Name"] = "Session Service";
273 asyncResp->res.jsonValue["Id"] = "SessionService";
274 asyncResp->res.jsonValue["Description"] = "Session Service";
275 asyncResp->res.jsonValue["SessionTimeout"] =
276 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
277 asyncResp->res.jsonValue["ServiceEnabled"] = true;
278
Ed Tanous14766872022-03-15 10:44:42 -0700279 asyncResp->res.jsonValue["Sessions"]["@odata.id"] =
280 "/redfish/v1/SessionService/Sessions";
Ed Tanous724340d2022-03-14 09:10:07 -0700281}
282
283inline void handleSessionServicePatch(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700284 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700285 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
286{
Carson Labrado3ba00072022-06-06 19:40:56 +0000287 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700288 {
289 return;
290 }
Ed Tanous724340d2022-03-14 09:10:07 -0700291 std::optional<int64_t> sessionTimeout;
292 if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout",
293 sessionTimeout))
294 {
295 return;
296 }
297
298 if (sessionTimeout)
299 {
300 // The mininum & maximum allowed values for session timeout
301 // are 30 seconds and 86400 seconds respectively as per the
302 // session service schema mentioned at
303 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
304
305 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
306 {
307 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
308 persistent_data::SessionStore::getInstance().updateSessionTimeout(
309 sessionTimeoutInseconds);
310 messages::propertyValueModified(asyncResp->res, "SessionTimeOut",
311 std::to_string(*sessionTimeout));
312 }
313 else
314 {
315 messages::propertyValueNotInList(asyncResp->res,
316 std::to_string(*sessionTimeout),
317 "SessionTimeOut");
318 }
319 }
320}
321
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700322inline void requestRoutesSession(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700323{
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700324 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700325 .privileges(redfish::privileges::headSession)
326 .methods(boost::beast::http::verb::head)(
327 std::bind_front(handleSessionHead, std::ref(app)));
328
329 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700330 .privileges(redfish::privileges::getSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700331 .methods(boost::beast::http::verb::get)(
332 std::bind_front(handleSessionGet, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100333
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700334 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700335 .privileges(redfish::privileges::deleteSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700336 .methods(boost::beast::http::verb::delete_)(
337 std::bind_front(handleSessionDelete, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700338
339 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700340 .privileges(redfish::privileges::headSessionCollection)
341 .methods(boost::beast::http::verb::head)(
342 std::bind_front(handleSessionCollectionHead, std::ref(app)));
343
344 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanoused398212021-06-09 17:05:54 -0700345 .privileges(redfish::privileges::getSessionCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700346 .methods(boost::beast::http::verb::get)(
347 std::bind_front(handleSessionCollectionGet, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700348
Ed Tanouse76cd862022-03-14 09:12:00 -0700349 // Note, the next two routes technically don't match the privilege
Ed Tanous724340d2022-03-14 09:10:07 -0700350 // registry given the way login mechanisms work. The base privilege
351 // registry lists this endpoint as requiring login privilege, but because
352 // this is the endpoint responsible for giving the login privilege, and it
353 // is itself its own route, it needs to not require Login
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700354 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
355 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700356 .methods(boost::beast::http::verb::post)(
357 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100358
Ed Tanouse76cd862022-03-14 09:12:00 -0700359 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/")
360 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700361 .methods(boost::beast::http::verb::post)(
362 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Ed Tanouse76cd862022-03-14 09:12:00 -0700363
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700364 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700365 .privileges(redfish::privileges::headSessionService)
366 .methods(boost::beast::http::verb::head)(
367 std::bind_front(handleSessionServiceHead, std::ref(app)));
368
369 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700370 .privileges(redfish::privileges::getSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700371 .methods(boost::beast::http::verb::get)(
372 std::bind_front(handleSessionServiceGet, std::ref(app)));
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100373
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700374 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700375 .privileges(redfish::privileges::patchSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700376 .methods(boost::beast::http::verb::patch)(
377 std::bind_front(handleSessionServicePatch, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700378}
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100379
Ed Tanous1abe55e2018-09-05 08:30:59 -0700380} // namespace redfish