blob: af01e05af7be7fa84f19577f8b1478f2682555b6 [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;
37 res.jsonValue["@odata.type"] = "#Session.v1_3_0.Session";
38 res.jsonValue["Name"] = "User Session";
39 res.jsonValue["Description"] = "Manager User Session";
40 res.jsonValue["ClientOriginIPAddress"] = session.clientIp;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050041#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
Ed Tanousfaa34cc2021-06-03 13:27:02 -070042 res.jsonValue["Oem"]["OpenBMC"]["@odata.type"] =
43 "#OemSession.v1_0_0.Session";
44 res.jsonValue["Oem"]["OpenBMC"]["ClientID"] = session.clientId;
Sunitha Harish08bdcc72020-05-12 05:17:57 -050045#endif
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 Tanous45ca1b82022-03-25 13:07:27 -070049 handleSessionGet(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -070050 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
51 const std::string& sessionId)
52{
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 Tanous724340d2022-03-14 09:10:07 -070057 // Note that control also reaches here via doPost and doDelete.
58 auto session =
59 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
60
61 if (session == nullptr)
62 {
63 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
64 return;
65 }
66
67 fillSessionObject(asyncResp->res, *session);
68}
69
70inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -070071 handleSessionDelete(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -070072 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
73 const std::string& sessionId)
74{
Carson Labrado3ba00072022-06-06 19:40:56 +000075 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -070076 {
77 return;
78 }
Ed Tanous724340d2022-03-14 09:10:07 -070079 auto session =
80 persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
81
82 if (session == nullptr)
83 {
84 messages::resourceNotFound(asyncResp->res, "Session", sessionId);
85 return;
86 }
87
88 // Perform a proper ConfigureSelf authority check. If a
89 // session is being used to DELETE some other user's session,
90 // then the ConfigureSelf privilege does not apply. In that
91 // case, perform the authority check again without the user's
92 // ConfigureSelf privilege.
wukaihua-fii-na0fd29862022-05-18 09:19:16 +080093 if (req.session != nullptr && !session->username.empty() &&
94 session->username != req.session->username)
Ed Tanous724340d2022-03-14 09:10:07 -070095 {
96 Privileges effectiveUserPrivileges =
97 redfish::getUserPrivileges(req.userRole);
98
99 if (!effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"}))
100 {
101 messages::insufficientPrivilege(asyncResp->res);
102 return;
103 }
104 }
105
106 persistent_data::SessionStore::getInstance().removeSession(session);
107 messages::success(asyncResp->res);
108}
109
110inline nlohmann::json getSessionCollectionMembers()
111{
112 std::vector<const std::string*> sessionIds =
113 persistent_data::SessionStore::getInstance().getUniqueIds(
114 false, persistent_data::PersistenceType::TIMEOUT);
115 nlohmann::json ret = nlohmann::json::array();
116 for (const std::string* uid : sessionIds)
117 {
Ed Tanous14766872022-03-15 10:44:42 -0700118 nlohmann::json::object_t session;
119 session["@odata.id"] = "/redfish/v1/SessionService/Sessions/" + *uid;
120 ret.push_back(std::move(session));
Ed Tanous724340d2022-03-14 09:10:07 -0700121 }
122 return ret;
123}
124
125inline void handleSessionCollectionGet(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700126 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700127 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
128{
Carson Labrado3ba00072022-06-06 19:40:56 +0000129 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700130 {
131 return;
132 }
Ed Tanous724340d2022-03-14 09:10:07 -0700133 asyncResp->res.jsonValue["Members"] = getSessionCollectionMembers();
134 asyncResp->res.jsonValue["Members@odata.count"] =
135 asyncResp->res.jsonValue["Members"].size();
136 asyncResp->res.jsonValue["@odata.type"] =
137 "#SessionCollection.SessionCollection";
138 asyncResp->res.jsonValue["@odata.id"] =
139 "/redfish/v1/SessionService/Sessions/";
140 asyncResp->res.jsonValue["Name"] = "Session Collection";
141 asyncResp->res.jsonValue["Description"] = "Session Collection";
142}
143
144inline void handleSessionCollectionMembersGet(
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 Tanous724340d2022-03-14 09:10:07 -0700152 asyncResp->res.jsonValue = getSessionCollectionMembers();
153}
154
Ed Tanous4ee8e212022-05-28 09:42:51 -0700155inline void handleSessionCollectionPost(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700156 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700157 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
158{
Carson Labrado3ba00072022-06-06 19:40:56 +0000159 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700160 {
161 return;
162 }
Ed Tanous724340d2022-03-14 09:10:07 -0700163 std::string username;
164 std::string password;
165 std::optional<nlohmann::json> oemObject;
166 std::string clientId;
167 if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
168 "Password", password, "Oem", oemObject))
169 {
170 return;
171 }
172
173 if (password.empty() || username.empty() ||
174 asyncResp->res.result() != boost::beast::http::status::ok)
175 {
176 if (username.empty())
177 {
178 messages::propertyMissing(asyncResp->res, "UserName");
179 }
180
181 if (password.empty())
182 {
183 messages::propertyMissing(asyncResp->res, "Password");
184 }
185
186 return;
187 }
188
189 int pamrc = pamAuthenticateUser(username, password);
190 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
191 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
192 {
193 messages::resourceAtUriUnauthorized(asyncResp->res, req.urlView,
194 "Invalid username or password");
195 return;
196 }
197#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
198 if (oemObject)
199 {
200 std::optional<nlohmann::json> bmcOem;
201 if (!json_util::readJson(*oemObject, asyncResp->res, "OpenBMC", bmcOem))
202 {
203 return;
204 }
205 if (!json_util::readJson(*bmcOem, asyncResp->res, "ClientID", clientId))
206 {
207 BMCWEB_LOG_ERROR << "Could not read ClientId";
208 return;
209 }
210 }
211#endif
212
213 // User is authenticated - create session
214 std::shared_ptr<persistent_data::UserSession> session =
215 persistent_data::SessionStore::getInstance().generateUserSession(
216 username, req.ipAddress, clientId,
217 persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly);
Brad Bishop02e53ae2022-07-29 14:38:40 -0400218 if (session == nullptr)
219 {
220 messages::internalError(asyncResp->res);
221 return;
222 }
223
Ed Tanous724340d2022-03-14 09:10:07 -0700224 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
225 asyncResp->res.addHeader(
226 "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId);
227 asyncResp->res.result(boost::beast::http::status::created);
228 if (session->isConfigureSelfOnly)
229 {
230 messages::passwordChangeRequired(
231 asyncResp->res,
232 crow::utility::urlFromPieces("redfish", "v1", "AccountService",
233 "Accounts", req.session->username));
234 }
235
236 fillSessionObject(asyncResp->res, *session);
237}
238inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -0700239 handleSessionServiceGet(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700240 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
241
242{
Carson Labrado3ba00072022-06-06 19:40:56 +0000243 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700244 {
245 return;
246 }
Ed Tanous724340d2022-03-14 09:10:07 -0700247 asyncResp->res.jsonValue["@odata.type"] =
248 "#SessionService.v1_0_2.SessionService";
249 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
250 asyncResp->res.jsonValue["Name"] = "Session Service";
251 asyncResp->res.jsonValue["Id"] = "SessionService";
252 asyncResp->res.jsonValue["Description"] = "Session Service";
253 asyncResp->res.jsonValue["SessionTimeout"] =
254 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
255 asyncResp->res.jsonValue["ServiceEnabled"] = true;
256
Ed Tanous14766872022-03-15 10:44:42 -0700257 asyncResp->res.jsonValue["Sessions"]["@odata.id"] =
258 "/redfish/v1/SessionService/Sessions";
Ed Tanous724340d2022-03-14 09:10:07 -0700259}
260
261inline void handleSessionServicePatch(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700262 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700263 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
264{
Carson Labrado3ba00072022-06-06 19:40:56 +0000265 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700266 {
267 return;
268 }
Ed Tanous724340d2022-03-14 09:10:07 -0700269 std::optional<int64_t> sessionTimeout;
270 if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout",
271 sessionTimeout))
272 {
273 return;
274 }
275
276 if (sessionTimeout)
277 {
278 // The mininum & maximum allowed values for session timeout
279 // are 30 seconds and 86400 seconds respectively as per the
280 // session service schema mentioned at
281 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
282
283 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
284 {
285 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
286 persistent_data::SessionStore::getInstance().updateSessionTimeout(
287 sessionTimeoutInseconds);
288 messages::propertyValueModified(asyncResp->res, "SessionTimeOut",
289 std::to_string(*sessionTimeout));
290 }
291 else
292 {
293 messages::propertyValueNotInList(asyncResp->res,
294 std::to_string(*sessionTimeout),
295 "SessionTimeOut");
296 }
297 }
298}
299
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700300inline void requestRoutesSession(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700301{
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700302 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700303 .privileges(redfish::privileges::getSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700304 .methods(boost::beast::http::verb::get)(
305 std::bind_front(handleSessionGet, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100306
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700307 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700308 .privileges(redfish::privileges::deleteSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700309 .methods(boost::beast::http::verb::delete_)(
310 std::bind_front(handleSessionDelete, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700311
312 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanoused398212021-06-09 17:05:54 -0700313 .privileges(redfish::privileges::getSessionCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700314 .methods(boost::beast::http::verb::get)(
315 std::bind_front(handleSessionCollectionGet, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700316
Ed Tanouse76cd862022-03-14 09:12:00 -0700317 // Note, the next two routes technically don't match the privilege
Ed Tanous724340d2022-03-14 09:10:07 -0700318 // registry given the way login mechanisms work. The base privilege
319 // registry lists this endpoint as requiring login privilege, but because
320 // this is the endpoint responsible for giving the login privilege, and it
321 // is itself its own route, it needs to not require Login
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700322 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
323 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700324 .methods(boost::beast::http::verb::post)(
325 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100326
Ed Tanouse76cd862022-03-14 09:12:00 -0700327 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/")
328 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700329 .methods(boost::beast::http::verb::post)(
330 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Ed Tanouse76cd862022-03-14 09:12:00 -0700331
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700332 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700333 .privileges(redfish::privileges::getSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700334 .methods(boost::beast::http::verb::get)(
335 std::bind_front(handleSessionServiceGet, std::ref(app)));
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100336
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700337 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700338 .privileges(redfish::privileges::patchSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700339 .methods(boost::beast::http::verb::patch)(
340 std::bind_front(handleSessionServicePatch, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700341}
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100342
Ed Tanous1abe55e2018-09-05 08:30:59 -0700343} // namespace redfish