blob: d1314a576752556383196fe1ce6d71e5b90be1f1 [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;
Ed Tanousbb759e32022-08-02 17:07:54 -070037 res.jsonValue["@odata.type"] = "#Session.v1_5_0.Session";
Ed Tanousfaa34cc2021-06-03 13:27:02 -070038 res.jsonValue["Name"] = "User Session";
39 res.jsonValue["Description"] = "Manager User Session";
40 res.jsonValue["ClientOriginIPAddress"] = session.clientIp;
Ed Tanousbb759e32022-08-02 17:07:54 -070041 if (session.clientId)
42 {
43 res.jsonValue["Context"] = *session.clientId;
44 }
45// The below implementation is deprecated in leiu of Session.Context
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050046#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
Ed Tanousfaa34cc2021-06-03 13:27:02 -070047 res.jsonValue["Oem"]["OpenBMC"]["@odata.type"] =
48 "#OemSession.v1_0_0.Session";
Ed Tanousbb759e32022-08-02 17:07:54 -070049 res.jsonValue["Oem"]["OpenBMC"]["ClientID"] = session.clientId.value_or("");
Sunitha Harish08bdcc72020-05-12 05:17:57 -050050#endif
Ed Tanousfaa34cc2021-06-03 13:27:02 -070051}
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010052
Ed Tanous724340d2022-03-14 09:10:07 -070053inline void
Ed Tanousa1e08712022-07-07 16:10:39 -070054 handleSessionHead(crow::App& app, const crow::Request& req,
55 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
56 const std::string& /*sessionId*/)
Ed Tanous724340d2022-03-14 09:10:07 -070057{
Ed Tanousa1e08712022-07-07 16:10:39 -070058
Carson Labrado3ba00072022-06-06 19:40:56 +000059 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070060 {
61 return;
62 }
Ed Tanousa1e08712022-07-07 16:10:39 -070063 asyncResp->res.addHeader(
64 boost::beast::http::field::link,
65 "</redfish/v1/JsonSchemas/Session/Session.json>; rel=describedby");
66}
67
68inline void
69 handleSessionGet(crow::App& app, const crow::Request& req,
70 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
71 const std::string& sessionId)
72{
73 handleSessionHead(app, req, asyncResp, sessionId);
74
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;
137 session["@odata.id"] = "/redfish/v1/SessionService/Sessions/" + *uid;
138 ret.push_back(std::move(session));
Ed Tanous724340d2022-03-14 09:10:07 -0700139 }
140 return ret;
141}
142
Ed Tanousa1e08712022-07-07 16:10:39 -0700143inline void handleSessionCollectionHead(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700144 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700145 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
146{
Ed Tanousa1e08712022-07-07 16:10:39 -0700147
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;
194 std::optional<nlohmann::json> oemObject;
Ed Tanousbb759e32022-08-02 17:07:54 -0700195 std::optional<std::string> clientId;
Ed Tanous724340d2022-03-14 09:10:07 -0700196 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
Ed Tanousbb759e32022-08-02 17:07:54 -0700197 "Password", password, "Context", clientId,
198 "Oem", oemObject))
Ed Tanous724340d2022-03-14 09:10:07 -0700199 {
200 return;
201 }
202
203 if (password.empty() || username.empty() ||
204 asyncResp->res.result() != boost::beast::http::status::ok)
205 {
206 if (username.empty())
207 {
208 messages::propertyMissing(asyncResp->res, "UserName");
209 }
210
211 if (password.empty())
212 {
213 messages::propertyMissing(asyncResp->res, "Password");
214 }
215
216 return;
217 }
218
219 int pamrc = pamAuthenticateUser(username, password);
220 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
221 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
222 {
223 messages::resourceAtUriUnauthorized(asyncResp->res, req.urlView,
224 "Invalid username or password");
225 return;
226 }
227#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
228 if (oemObject)
229 {
230 std::optional<nlohmann::json> bmcOem;
231 if (!json_util::readJson(*oemObject, asyncResp->res, "OpenBMC", bmcOem))
232 {
233 return;
234 }
Ed Tanousbb759e32022-08-02 17:07:54 -0700235
236 std::optional<std::string> oemClientId;
237 if (!json_util::readJson(*bmcOem, asyncResp->res, "ClientID",
238 oemClientId))
Ed Tanous724340d2022-03-14 09:10:07 -0700239 {
240 BMCWEB_LOG_ERROR << "Could not read ClientId";
241 return;
242 }
Ed Tanousbb759e32022-08-02 17:07:54 -0700243 if (oemClientId)
244 {
245 if (clientId)
246 {
247 messages::propertyValueConflict(*oemClientId, *clientId);
248 return;
249 }
250 clientId = *oemClientId;
251 }
Ed Tanous724340d2022-03-14 09:10:07 -0700252 }
253#endif
254
255 // User is authenticated - create session
256 std::shared_ptr<persistent_data::UserSession> session =
257 persistent_data::SessionStore::getInstance().generateUserSession(
258 username, req.ipAddress, clientId,
259 persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly);
Brad Bishop02e53ae2022-07-29 14:38:40 -0400260 if (session == nullptr)
261 {
262 messages::internalError(asyncResp->res);
263 return;
264 }
265
Ed Tanous724340d2022-03-14 09:10:07 -0700266 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
267 asyncResp->res.addHeader(
268 "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId);
269 asyncResp->res.result(boost::beast::http::status::created);
270 if (session->isConfigureSelfOnly)
271 {
272 messages::passwordChangeRequired(
273 asyncResp->res,
274 crow::utility::urlFromPieces("redfish", "v1", "AccountService",
Brad Bishop85e64712022-07-29 12:59:07 -0400275 "Accounts", session->username));
Ed Tanous724340d2022-03-14 09:10:07 -0700276 }
277
278 fillSessionObject(asyncResp->res, *session);
279}
Ed Tanousa1e08712022-07-07 16:10:39 -0700280inline void handleSessionServiceHead(
281 crow::App& app, const crow::Request& req,
282 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
283{
284
285 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
286 {
287 return;
288 }
289 asyncResp->res.addHeader(
290 boost::beast::http::field::link,
291 "</redfish/v1/JsonSchemas/SessionService/SessionService.json>; rel=describedby");
292}
Ed Tanous724340d2022-03-14 09:10:07 -0700293inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -0700294 handleSessionServiceGet(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700295 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
296
297{
Ed Tanousa1e08712022-07-07 16:10:39 -0700298 handleSessionServiceHead(app, req, asyncResp);
Ed Tanous724340d2022-03-14 09:10:07 -0700299 asyncResp->res.jsonValue["@odata.type"] =
300 "#SessionService.v1_0_2.SessionService";
301 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
302 asyncResp->res.jsonValue["Name"] = "Session Service";
303 asyncResp->res.jsonValue["Id"] = "SessionService";
304 asyncResp->res.jsonValue["Description"] = "Session Service";
305 asyncResp->res.jsonValue["SessionTimeout"] =
306 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
307 asyncResp->res.jsonValue["ServiceEnabled"] = true;
308
Ed Tanous14766872022-03-15 10:44:42 -0700309 asyncResp->res.jsonValue["Sessions"]["@odata.id"] =
310 "/redfish/v1/SessionService/Sessions";
Ed Tanous724340d2022-03-14 09:10:07 -0700311}
312
313inline void handleSessionServicePatch(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700314 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700315 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
316{
Carson Labrado3ba00072022-06-06 19:40:56 +0000317 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700318 {
319 return;
320 }
Ed Tanous724340d2022-03-14 09:10:07 -0700321 std::optional<int64_t> sessionTimeout;
322 if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout",
323 sessionTimeout))
324 {
325 return;
326 }
327
328 if (sessionTimeout)
329 {
330 // The mininum & maximum allowed values for session timeout
331 // are 30 seconds and 86400 seconds respectively as per the
332 // session service schema mentioned at
333 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
334
335 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
336 {
337 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
338 persistent_data::SessionStore::getInstance().updateSessionTimeout(
339 sessionTimeoutInseconds);
340 messages::propertyValueModified(asyncResp->res, "SessionTimeOut",
341 std::to_string(*sessionTimeout));
342 }
343 else
344 {
345 messages::propertyValueNotInList(asyncResp->res,
346 std::to_string(*sessionTimeout),
347 "SessionTimeOut");
348 }
349 }
350}
351
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700352inline void requestRoutesSession(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700353{
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700354 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700355 .privileges(redfish::privileges::headSession)
356 .methods(boost::beast::http::verb::head)(
357 std::bind_front(handleSessionHead, std::ref(app)));
358
359 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700360 .privileges(redfish::privileges::getSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700361 .methods(boost::beast::http::verb::get)(
362 std::bind_front(handleSessionGet, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100363
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700364 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700365 .privileges(redfish::privileges::deleteSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700366 .methods(boost::beast::http::verb::delete_)(
367 std::bind_front(handleSessionDelete, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700368
369 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700370 .privileges(redfish::privileges::headSessionCollection)
371 .methods(boost::beast::http::verb::head)(
372 std::bind_front(handleSessionCollectionHead, std::ref(app)));
373
374 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanoused398212021-06-09 17:05:54 -0700375 .privileges(redfish::privileges::getSessionCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700376 .methods(boost::beast::http::verb::get)(
377 std::bind_front(handleSessionCollectionGet, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700378
Ed Tanouse76cd862022-03-14 09:12:00 -0700379 // Note, the next two routes technically don't match the privilege
Ed Tanous724340d2022-03-14 09:10:07 -0700380 // registry given the way login mechanisms work. The base privilege
381 // registry lists this endpoint as requiring login privilege, but because
382 // this is the endpoint responsible for giving the login privilege, and it
383 // is itself its own route, it needs to not require Login
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700384 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
385 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700386 .methods(boost::beast::http::verb::post)(
387 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100388
Ed Tanouse76cd862022-03-14 09:12:00 -0700389 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/")
390 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700391 .methods(boost::beast::http::verb::post)(
392 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Ed Tanouse76cd862022-03-14 09:12:00 -0700393
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700394 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanousa1e08712022-07-07 16:10:39 -0700395 .privileges(redfish::privileges::headSessionService)
396 .methods(boost::beast::http::verb::head)(
397 std::bind_front(handleSessionServiceHead, std::ref(app)));
398
399 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700400 .privileges(redfish::privileges::getSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700401 .methods(boost::beast::http::verb::get)(
402 std::bind_front(handleSessionServiceGet, std::ref(app)));
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100403
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700404 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700405 .privileges(redfish::privileges::patchSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700406 .methods(boost::beast::http::verb::patch)(
407 std::bind_front(handleSessionServicePatch, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700408}
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100409
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410} // namespace redfish