blob: b38f9249a6b8177699154c29761f32536375198a [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
Paul Fertserce22f602024-06-03 20:53:16 +000018#include "account_service.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080019#include "app.hpp"
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010020#include "error_messages.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080021#include "http/utility.hpp"
Ed Tanous52cc1122020-07-18 13:51:21 -070022#include "persistent_data.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "query.hpp"
24#include "registries/privilege_registry.hpp"
25#include "utils/json_utils.hpp"
John Edward Broadbent7e860f12021-04-08 15:57:16 -070026
Ed Tanousef4c65b2023-04-24 15:28:50 -070027#include <boost/url/format.hpp>
28
Ed Tanous1abe55e2018-09-05 08:30:59 -070029namespace redfish
30{
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010031
Ed Tanous4f48d5f2021-06-21 08:27:45 -070032inline void fillSessionObject(crow::Response& res,
33 const persistent_data::UserSession& session)
Ed Tanous1abe55e2018-09-05 08:30:59 -070034{
Ed Tanousfaa34cc2021-06-03 13:27:02 -070035 res.jsonValue["Id"] = session.uniqueId;
36 res.jsonValue["UserName"] = session.username;
Paul Fertserce22f602024-06-03 20:53:16 +000037 nlohmann::json::array_t roles;
38 roles.emplace_back(redfish::getRoleIdFromPrivilege(session.userRole));
39 res.jsonValue["Roles"] = std::move(roles);
Ed Tanousef4c65b2023-04-24 15:28:50 -070040 res.jsonValue["@odata.id"] = boost::urls::format(
41 "/redfish/v1/SessionService/Sessions/{}", session.uniqueId);
Paul Fertserce22f602024-06-03 20:53:16 +000042 res.jsonValue["@odata.type"] = "#Session.v1_7_0.Session";
Ed Tanousfaa34cc2021-06-03 13:27:02 -070043 res.jsonValue["Name"] = "User Session";
44 res.jsonValue["Description"] = "Manager User Session";
45 res.jsonValue["ClientOriginIPAddress"] = session.clientIp;
Ed Tanousbb759e32022-08-02 17:07:54 -070046 if (session.clientId)
47 {
48 res.jsonValue["Context"] = *session.clientId;
49 }
Ed Tanousfaa34cc2021-06-03 13:27:02 -070050}
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010051
Ed Tanous724340d2022-03-14 09:10:07 -070052inline void
Ed Tanousa1e08712022-07-07 16:10:39 -070053 handleSessionHead(crow::App& app, const crow::Request& req,
54 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
55 const std::string& /*sessionId*/)
Ed Tanous724340d2022-03-14 09:10:07 -070056{
Carson Labrado3ba00072022-06-06 19:40:56 +000057 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070058 {
59 return;
60 }
Ed Tanousa1e08712022-07-07 16:10:39 -070061 asyncResp->res.addHeader(
62 boost::beast::http::field::link,
63 "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby");
64}
65
66inline void
67 handleSessionGet(crow::App& app, const crow::Request& req,
68 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
69 const std::string& sessionId)
70{
Ed Tanous65ffbcb2023-05-16 08:54:11 -070071 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
72 {
73 return;
74 }
75 asyncResp->res.addHeader(
76 boost::beast::http::field::link,
77 "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby");
Ed Tanousa1e08712022-07-07 16:10:39 -070078
Ed Tanous724340d2022-03-14 09:10:07 -070079 // Note that control also reaches here via doPost and doDelete.
80 auto session =
81 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
82
83 if (session == nullptr)
84 {
85 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
86 return;
87 }
88
89 fillSessionObject(asyncResp->res, *session);
90}
91
92inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -070093 handleSessionDelete(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -070094 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
95 const std::string& sessionId)
96{
Carson Labrado3ba00072022-06-06 19:40:56 +000097 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070098 {
99 return;
100 }
Ed Tanous724340d2022-03-14 09:10:07 -0700101 auto session =
102 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
103
104 if (session == nullptr)
105 {
106 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
107 return;
108 }
109
110 // Perform a proper ConfigureSelf authority check. If a
111 // session is being used to DELETE some other user's session,
112 // then the ConfigureSelf privilege does not apply. In that
113 // case, perform the authority check again without the user's
114 // ConfigureSelf privilege.
wukaihua-fii-na0fd29862022-05-18 09:19:16 +0800115 if (req.session != nullptr && !session->username.empty() &&
116 session->username != req.session->username)
Ed Tanous724340d2022-03-14 09:10:07 -0700117 {
118 Privileges effectiveUserPrivileges =
Ninad Palsule3e72c202023-03-27 17:19:55 -0500119 redfish::getUserPrivileges(*req.session);
Ed Tanous724340d2022-03-14 09:10:07 -0700120
121 if (!effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"}))
122 {
123 messages::insufficientPrivilege(asyncResp->res);
124 return;
125 }
126 }
127
128 persistent_data::SessionStore::getInstance().removeSession(session);
129 messages::success(asyncResp->res);
130}
131
132inline nlohmann::json getSessionCollectionMembers()
133{
134 std::vector<const std::string*> sessionIds =
135 persistent_data::SessionStore::getInstance().getUniqueIds(
136 false, persistent_data::PersistenceType::TIMEOUT);
137 nlohmann::json ret = nlohmann::json::array();
138 for (const std::string* uid : sessionIds)
139 {
Ed Tanous14766872022-03-15 10:44:42 -0700140 nlohmann::json::object_t session;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700141 session["@odata.id"] =
142 boost::urls::format("/redfish/v1/SessionService/Sessions/{}", *uid);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500143 ret.emplace_back(std::move(session));
Ed Tanous724340d2022-03-14 09:10:07 -0700144 }
145 return ret;
146}
147
Ed Tanousa1e08712022-07-07 16:10:39 -0700148inline void handleSessionCollectionHead(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700149 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700150 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
151{
Carson Labrado3ba00072022-06-06 19:40:56 +0000152 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700153 {
154 return;
155 }
Ed Tanousa1e08712022-07-07 16:10:39 -0700156 asyncResp->res.addHeader(
157 boost::beast::http::field::link,
158 "</redfish/v1/JsonSchemas/SessionCollection.json>; rel=describedby");
159}
160
161inline void handleSessionCollectionGet(
162 crow::App& app, const crow::Request& req,
163 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
164{
Ed Tanous01a89a12022-08-05 09:18:54 -0700165 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
166 {
167 return;
168 }
169 asyncResp->res.addHeader(
170 boost::beast::http::field::link,
171 "</redfish/v1/JsonSchemas/SessionCollection.json>; rel=describedby");
172
Ed Tanous724340d2022-03-14 09:10:07 -0700173 asyncResp->res.jsonValue["Members"] = getSessionCollectionMembers();
174 asyncResp->res.jsonValue["Members@odata.count"] =
175 asyncResp->res.jsonValue["Members"].size();
176 asyncResp->res.jsonValue["@odata.type"] =
177 "#SessionCollection.SessionCollection";
178 asyncResp->res.jsonValue["@odata.id"] =
Gunnar Mills7a859ff2024-03-04 23:04:45 -0700179 "/redfish/v1/SessionService/Sessions";
Ed Tanous724340d2022-03-14 09:10:07 -0700180 asyncResp->res.jsonValue["Name"] = "Session Collection";
181 asyncResp->res.jsonValue["Description"] = "Session Collection";
182}
183
184inline void handleSessionCollectionMembersGet(
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 asyncResp->res.jsonValue = getSessionCollectionMembers();
193}
194
Ed Tanous4ee8e212022-05-28 09:42:51 -0700195inline void handleSessionCollectionPost(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700196 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700197 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
198{
Carson Labrado3ba00072022-06-06 19:40:56 +0000199 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700200 {
201 return;
202 }
Ed Tanous724340d2022-03-14 09:10:07 -0700203 std::string username;
204 std::string password;
Ed Tanousbb759e32022-08-02 17:07:54 -0700205 std::optional<std::string> clientId;
Ed Tanous724340d2022-03-14 09:10:07 -0700206 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
Ed Tanousd678d4f2023-01-07 14:40:40 -0800207 "Password", password, "Context", clientId))
Ed Tanous724340d2022-03-14 09:10:07 -0700208 {
209 return;
210 }
211
212 if (password.empty() || username.empty() ||
213 asyncResp->res.result() != boost::beast::http::status::ok)
214 {
215 if (username.empty())
216 {
217 messages::propertyMissing(asyncResp->res, "UserName");
218 }
219
220 if (password.empty())
221 {
222 messages::propertyMissing(asyncResp->res, "Password");
223 }
224
225 return;
226 }
227
228 int pamrc = pamAuthenticateUser(username, password);
229 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
230 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
231 {
Ed Tanous39662a32023-02-06 15:09:46 -0800232 messages::resourceAtUriUnauthorized(asyncResp->res, req.url(),
Ed Tanous724340d2022-03-14 09:10:07 -0700233 "Invalid username or password");
234 return;
235 }
Ed Tanous724340d2022-03-14 09:10:07 -0700236
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,
Ed Tanousef4c65b2023-04-24 15:28:50 -0700256 boost::urls::format("/redfish/v1/AccountService/Accounts/{}",
257 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{
Ed Tanousa1e08712022-07-07 16:10:39 -0700266 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
267 {
268 return;
269 }
270 asyncResp->res.addHeader(
271 boost::beast::http::field::link,
272 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby");
273}
Ed Tanous724340d2022-03-14 09:10:07 -0700274inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -0700275 handleSessionServiceGet(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700276 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
277
278{
Gunnar Mills78e39002023-05-17 11:52:44 -0500279 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
280 {
281 return;
282 }
283 asyncResp->res.addHeader(
284 boost::beast::http::field::link,
285 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby");
286
Ed Tanous724340d2022-03-14 09:10:07 -0700287 asyncResp->res.jsonValue["@odata.type"] =
288 "#SessionService.v1_0_2.SessionService";
Gunnar Mills7a859ff2024-03-04 23:04:45 -0700289 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService";
Ed Tanous724340d2022-03-14 09:10:07 -0700290 asyncResp->res.jsonValue["Name"] = "Session Service";
291 asyncResp->res.jsonValue["Id"] = "SessionService";
292 asyncResp->res.jsonValue["Description"] = "Session Service";
293 asyncResp->res.jsonValue["SessionTimeout"] =
294 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
295 asyncResp->res.jsonValue["ServiceEnabled"] = true;
296
Ed Tanous14766872022-03-15 10:44:42 -0700297 asyncResp->res.jsonValue["Sessions"]["@odata.id"] =
298 "/redfish/v1/SessionService/Sessions";
Ed Tanous724340d2022-03-14 09:10:07 -0700299}
300
301inline void handleSessionServicePatch(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700302 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700303 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
304{
Carson Labrado3ba00072022-06-06 19:40:56 +0000305 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700306 {
307 return;
308 }
Ed Tanous724340d2022-03-14 09:10:07 -0700309 std::optional<int64_t> sessionTimeout;
310 if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout",
311 sessionTimeout))
312 {
313 return;
314 }
315
316 if (sessionTimeout)
317 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800318 // The minimum & maximum allowed values for session timeout
Ed Tanous724340d2022-03-14 09:10:07 -0700319 // are 30 seconds and 86400 seconds respectively as per the
320 // session service schema mentioned at
321 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
322
323 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
324 {
325 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
326 persistent_data::SessionStore::getInstance().updateSessionTimeout(
327 sessionTimeoutInseconds);
328 messages::propertyValueModified(asyncResp->res, "SessionTimeOut",
329 std::to_string(*sessionTimeout));
330 }
331 else
332 {
Ed Tanouse2616cc2022-06-27 12:45:55 -0700333 messages::propertyValueNotInList(asyncResp->res, *sessionTimeout,
Ed Tanous724340d2022-03-14 09:10:07 -0700334 "SessionTimeOut");
335 }
336 }
337}
338
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700339inline void requestRoutesSession(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700340{
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700341 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700342 .privileges(redfish::privileges::headSession)
343 .methods(boost::beast::http::verb::head)(
344 std::bind_front(handleSessionHead, std::ref(app)));
345
346 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700347 .privileges(redfish::privileges::getSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700348 .methods(boost::beast::http::verb::get)(
349 std::bind_front(handleSessionGet, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100350
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700351 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700352 .privileges(redfish::privileges::deleteSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700353 .methods(boost::beast::http::verb::delete_)(
354 std::bind_front(handleSessionDelete, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700355
356 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700357 .privileges(redfish::privileges::headSessionCollection)
358 .methods(boost::beast::http::verb::head)(
359 std::bind_front(handleSessionCollectionHead, std::ref(app)));
360
361 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanoused398212021-06-09 17:05:54 -0700362 .privileges(redfish::privileges::getSessionCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700363 .methods(boost::beast::http::verb::get)(
364 std::bind_front(handleSessionCollectionGet, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700365
Ed Tanouse76cd862022-03-14 09:12:00 -0700366 // Note, the next two routes technically don't match the privilege
Ed Tanous724340d2022-03-14 09:10:07 -0700367 // registry given the way login mechanisms work. The base privilege
368 // registry lists this endpoint as requiring login privilege, but because
369 // this is the endpoint responsible for giving the login privilege, and it
370 // is itself its own route, it needs to not require Login
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700371 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
372 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700373 .methods(boost::beast::http::verb::post)(
374 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100375
Ed Tanouse76cd862022-03-14 09:12:00 -0700376 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/")
377 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700378 .methods(boost::beast::http::verb::post)(
379 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Ed Tanouse76cd862022-03-14 09:12:00 -0700380
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700381 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700382 .privileges(redfish::privileges::headSessionService)
383 .methods(boost::beast::http::verb::head)(
384 std::bind_front(handleSessionServiceHead, std::ref(app)));
385
386 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700387 .privileges(redfish::privileges::getSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700388 .methods(boost::beast::http::verb::get)(
389 std::bind_front(handleSessionServiceGet, std::ref(app)));
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100390
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700391 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700392 .privileges(redfish::privileges::patchSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700393 .methods(boost::beast::http::verb::patch)(
394 std::bind_front(handleSessionServicePatch, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700395}
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100396
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397} // namespace redfish