blob: e4eae68c33e13847a4a8c50f516be5dcc8200e2e [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);
218 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
219 asyncResp->res.addHeader(
220 "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId);
221 asyncResp->res.result(boost::beast::http::status::created);
222 if (session->isConfigureSelfOnly)
223 {
224 messages::passwordChangeRequired(
225 asyncResp->res,
226 crow::utility::urlFromPieces("redfish", "v1", "AccountService",
227 "Accounts", req.session->username));
228 }
229
230 fillSessionObject(asyncResp->res, *session);
231}
232inline void
Ed Tanous45ca1b82022-03-25 13:07:27 -0700233 handleSessionServiceGet(crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700234 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
235
236{
Carson Labrado3ba00072022-06-06 19:40:56 +0000237 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700238 {
239 return;
240 }
Ed Tanous724340d2022-03-14 09:10:07 -0700241 asyncResp->res.jsonValue["@odata.type"] =
242 "#SessionService.v1_0_2.SessionService";
243 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
244 asyncResp->res.jsonValue["Name"] = "Session Service";
245 asyncResp->res.jsonValue["Id"] = "SessionService";
246 asyncResp->res.jsonValue["Description"] = "Session Service";
247 asyncResp->res.jsonValue["SessionTimeout"] =
248 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
249 asyncResp->res.jsonValue["ServiceEnabled"] = true;
250
Ed Tanous14766872022-03-15 10:44:42 -0700251 asyncResp->res.jsonValue["Sessions"]["@odata.id"] =
252 "/redfish/v1/SessionService/Sessions";
Ed Tanous724340d2022-03-14 09:10:07 -0700253}
254
255inline void handleSessionServicePatch(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700256 crow::App& app, const crow::Request& req,
Ed Tanous724340d2022-03-14 09:10:07 -0700257 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
258{
Carson Labrado3ba00072022-06-06 19:40:56 +0000259 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700260 {
261 return;
262 }
Ed Tanous724340d2022-03-14 09:10:07 -0700263 std::optional<int64_t> sessionTimeout;
264 if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout",
265 sessionTimeout))
266 {
267 return;
268 }
269
270 if (sessionTimeout)
271 {
272 // The mininum & maximum allowed values for session timeout
273 // are 30 seconds and 86400 seconds respectively as per the
274 // session service schema mentioned at
275 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
276
277 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
278 {
279 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
280 persistent_data::SessionStore::getInstance().updateSessionTimeout(
281 sessionTimeoutInseconds);
282 messages::propertyValueModified(asyncResp->res, "SessionTimeOut",
283 std::to_string(*sessionTimeout));
284 }
285 else
286 {
287 messages::propertyValueNotInList(asyncResp->res,
288 std::to_string(*sessionTimeout),
289 "SessionTimeOut");
290 }
291 }
292}
293
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700294inline void requestRoutesSession(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700295{
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700296 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700297 .privileges(redfish::privileges::getSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700298 .methods(boost::beast::http::verb::get)(
299 std::bind_front(handleSessionGet, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100300
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700301 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700302 .privileges(redfish::privileges::deleteSession)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700303 .methods(boost::beast::http::verb::delete_)(
304 std::bind_front(handleSessionDelete, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700305
306 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
Ed Tanoused398212021-06-09 17:05:54 -0700307 .privileges(redfish::privileges::getSessionCollection)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700308 .methods(boost::beast::http::verb::get)(
309 std::bind_front(handleSessionCollectionGet, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700310
Ed Tanouse76cd862022-03-14 09:12:00 -0700311 // Note, the next two routes technically don't match the privilege
Ed Tanous724340d2022-03-14 09:10:07 -0700312 // registry given the way login mechanisms work. The base privilege
313 // registry lists this endpoint as requiring login privilege, but because
314 // this is the endpoint responsible for giving the login privilege, and it
315 // is itself its own route, it needs to not require Login
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700316 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
317 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700318 .methods(boost::beast::http::verb::post)(
319 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100320
Ed Tanouse76cd862022-03-14 09:12:00 -0700321 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/")
322 .privileges({})
Ed Tanous45ca1b82022-03-25 13:07:27 -0700323 .methods(boost::beast::http::verb::post)(
324 std::bind_front(handleSessionCollectionPost, std::ref(app)));
Ed Tanouse76cd862022-03-14 09:12:00 -0700325
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700326 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700327 .privileges(redfish::privileges::getSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700328 .methods(boost::beast::http::verb::get)(
329 std::bind_front(handleSessionServiceGet, std::ref(app)));
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100330
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700331 BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
Ed Tanoused398212021-06-09 17:05:54 -0700332 .privileges(redfish::privileges::patchSessionService)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700333 .methods(boost::beast::http::verb::patch)(
334 std::bind_front(handleSessionServicePatch, std::ref(app)));
Ed Tanousfaa34cc2021-06-03 13:27:02 -0700335}
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100336
Ed Tanous1abe55e2018-09-05 08:30:59 -0700337} // namespace redfish