blob: 555e7f32ac9a07f6c3b224c5f3fc880729e018c6 [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
Paul Fertser478c5a52024-06-26 22:27:59 +0000260 crow::getUserInfo(asyncResp, username, session, [asyncResp, session]() {
261 fillSessionObject(asyncResp->res, *session);
262 });
Ed Tanous724340d2022-03-14 09:10:07 -0700263}
Ed Tanousa1e08712022-07-07 16:10:39 -0700264inline void handleSessionServiceHead(
265 crow::App& app, const crow::Request& req,
266 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
267{
Ed Tanousa1e08712022-07-07 16:10:39 -0700268 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
269 {
270 return;
271 }
272 asyncResp->res.addHeader(
273 boost::beast::http::field::link,
274 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby");
275}
Ed Tanous724340d2022-03-14 09:10:07 -0700276inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -0700277 handleSessionServiceGet(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700278 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
279
280{
Gunnar Mills78e39002023-05-17 11:52:44 -0500281 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
282 {
283 return;
284 }
285 asyncResp->res.addHeader(
286 boost::beast::http::field::link,
287 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby");
288
Ed Tanous724340d2022-03-14 09:10:07 -0700289 asyncResp->res.jsonValue["@odata.type"] =
290 "#SessionService.v1_0_2.SessionService";
Gunnar Mills7a859ff2024-03-04 23:04:45 -0700291 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService";
Ed Tanous724340d2022-03-14 09:10:07 -0700292 asyncResp->res.jsonValue["Name"] = "Session Service";
293 asyncResp->res.jsonValue["Id"] = "SessionService";
294 asyncResp->res.jsonValue["Description"] = "Session Service";
295 asyncResp->res.jsonValue["SessionTimeout"] =
296 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
297 asyncResp->res.jsonValue["ServiceEnabled"] = true;
298
Ed Tanous14766872022-03-15 10:44:42 -0700299 asyncResp->res.jsonValue["Sessions"]["@odata.id"] =
300 "/redfish/v1/SessionService/Sessions";
Ed Tanous724340d2022-03-14 09:10:07 -0700301}
302
303inline void handleSessionServicePatch(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700304 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700305 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
306{
Carson Labrado3ba00072022-06-06 19:40:56 +0000307 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700308 {
309 return;
310 }
Ed Tanous724340d2022-03-14 09:10:07 -0700311 std::optional<int64_t> sessionTimeout;
312 if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout",
313 sessionTimeout))
314 {
315 return;
316 }
317
318 if (sessionTimeout)
319 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800320 // The minimum & maximum allowed values for session timeout
Ed Tanous724340d2022-03-14 09:10:07 -0700321 // are 30 seconds and 86400 seconds respectively as per the
322 // session service schema mentioned at
323 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
324
325 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
326 {
327 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
328 persistent_data::SessionStore::getInstance().updateSessionTimeout(
329 sessionTimeoutInseconds);
330 messages::propertyValueModified(asyncResp->res, "SessionTimeOut",
331 std::to_string(*sessionTimeout));
332 }
333 else
334 {
Ed Tanouse2616cc2022-06-27 12:45:55 -0700335 messages::propertyValueNotInList(asyncResp->res, *sessionTimeout,
Ed Tanous724340d2022-03-14 09:10:07 -0700336 "SessionTimeOut");
337 }
338 }
339}
340
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700341inline void requestRoutesSession(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700342{
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700343 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700344 .privileges(redfish::privileges::headSession)
345 .methods(boost::beast::http::verb::head)(
346 std::bind_front(handleSessionHead, std::ref(app)));
347
348 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700349 .privileges(redfish::privileges::getSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700350 .methods(boost::beast::http::verb::get)(
351 std::bind_front(handleSessionGet, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100352
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700353 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700354 .privileges(redfish::privileges::deleteSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700355 .methods(boost::beast::http::verb::delete_)(
356 std::bind_front(handleSessionDelete, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700357
358 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700359 .privileges(redfish::privileges::headSessionCollection)
360 .methods(boost::beast::http::verb::head)(
361 std::bind_front(handleSessionCollectionHead, std::ref(app)));
362
363 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanoused398212021-06-09 17:05:54 -0700364 .privileges(redfish::privileges::getSessionCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700365 .methods(boost::beast::http::verb::get)(
366 std::bind_front(handleSessionCollectionGet, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700367
Ed Tanouse76cd862022-03-14 09:12:00 -0700368 // Note, the next two routes technically don't match the privilege
Ed Tanous724340d2022-03-14 09:10:07 -0700369 // registry given the way login mechanisms work. The base privilege
370 // registry lists this endpoint as requiring login privilege, but because
371 // this is the endpoint responsible for giving the login privilege, and it
372 // is itself its own route, it needs to not require Login
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700373 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
374 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700375 .methods(boost::beast::http::verb::post)(
376 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100377
Ed Tanouse76cd862022-03-14 09:12:00 -0700378 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/")
379 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700380 .methods(boost::beast::http::verb::post)(
381 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Ed Tanouse76cd862022-03-14 09:12:00 -0700382
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700383 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700384 .privileges(redfish::privileges::headSessionService)
385 .methods(boost::beast::http::verb::head)(
386 std::bind_front(handleSessionServiceHead, std::ref(app)));
387
388 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700389 .privileges(redfish::privileges::getSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700390 .methods(boost::beast::http::verb::get)(
391 std::bind_front(handleSessionServiceGet, std::ref(app)));
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100392
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700393 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700394 .privileges(redfish::privileges::patchSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700395 .methods(boost::beast::http::verb::patch)(
396 std::bind_front(handleSessionServicePatch, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700397}
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100398
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399} // namespace redfish