blob: 3ae75eaed1a9043350f92f41c9d5fd49dc2e4e43 [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"
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010019#include "node.hpp"
Ed Tanous52cc1122020-07-18 13:51:21 -070020#include "persistent_data.hpp"
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010021
John Edward Broadbent7e860f12021-04-08 15:57:16 -070022#include <app.hpp>
23
Ed Tanous1abe55e2018-09-05 08:30:59 -070024namespace redfish
25{
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010026
27class SessionCollection;
28
Ed Tanous1abe55e2018-09-05 08:30:59 -070029class Sessions : public Node
30{
31 public:
Ed Tanous52cc1122020-07-18 13:51:21 -070032 Sessions(App& app) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070033 Node(app, "/redfish/v1/SessionService/Sessions/<str>/", std::string())
34 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070035 entityPrivileges = {
36 {boost::beast::http::verb::get, {{"Login"}}},
37 {boost::beast::http::verb::head, {{"Login"}}},
38 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
39 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
Joseph Reynolds900f9492019-11-25 15:37:29 -060040 {boost::beast::http::verb::delete_,
41 {{"ConfigureManager"}, {"ConfigureSelf"}}},
Ed Tanous1abe55e2018-09-05 08:30:59 -070042 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010043 }
44
Ed Tanous1abe55e2018-09-05 08:30:59 -070045 private:
zhanghch058d1b46d2021-04-01 11:18:24 +080046 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
47 const crow::Request&,
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 const std::vector<std::string>& params) override
49 {
Joseph Reynolds900f9492019-11-25 15:37:29 -060050 // Note that control also reaches here via doPost and doDelete.
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 auto session =
Ed Tanous52cc1122020-07-18 13:51:21 -070052 persistent_data::SessionStore::getInstance().getSessionByUid(
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 params[0]);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010054
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 if (session == nullptr)
56 {
zhanghch058d1b46d2021-04-01 11:18:24 +080057 messages::resourceNotFound(asyncResp->res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 return;
59 }
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010060
zhanghch058d1b46d2021-04-01 11:18:24 +080061 asyncResp->res.jsonValue["Id"] = session->uniqueId;
62 asyncResp->res.jsonValue["UserName"] = session->username;
63 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 "/redfish/v1/SessionService/Sessions/" + session->uniqueId;
zhanghch058d1b46d2021-04-01 11:18:24 +080065 asyncResp->res.jsonValue["@odata.type"] = "#Session.v1_3_0.Session";
66 asyncResp->res.jsonValue["Name"] = "User Session";
67 asyncResp->res.jsonValue["Description"] = "Manager User Session";
68 asyncResp->res.jsonValue["ClientOriginIPAddress"] = session->clientIp;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050069#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
zhanghch058d1b46d2021-04-01 11:18:24 +080070 asyncResp->res.jsonValue["Oem"]["OpenBMC"]["@odata.type"] =
Sunitha Harish08bdcc72020-05-12 05:17:57 -050071 "#OemSession.v1_0_0.Session";
zhanghch058d1b46d2021-04-01 11:18:24 +080072 asyncResp->res.jsonValue["Oem"]["OpenBMC"]["ClientID"] =
73 session->clientId;
Sunitha Harish08bdcc72020-05-12 05:17:57 -050074#endif
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010075 }
76
zhanghch058d1b46d2021-04-01 11:18:24 +080077 void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
78 const crow::Request& req,
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 const std::vector<std::string>& params) override
80 {
81 // Need only 1 param which should be id of session to be deleted
82 if (params.size() != 1)
83 {
84 // This should be handled by crow and never happen
85 BMCWEB_LOG_ERROR << "Session DELETE has been called with invalid "
86 "number of params";
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010087
zhanghch058d1b46d2021-04-01 11:18:24 +080088 messages::generalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -070089 return;
90 }
91
92 auto session =
Ed Tanous52cc1122020-07-18 13:51:21 -070093 persistent_data::SessionStore::getInstance().getSessionByUid(
Ed Tanous1abe55e2018-09-05 08:30:59 -070094 params[0]);
95
96 if (session == nullptr)
97 {
zhanghch058d1b46d2021-04-01 11:18:24 +080098 messages::resourceNotFound(asyncResp->res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 return;
100 }
101
Joseph Reynolds900f9492019-11-25 15:37:29 -0600102 // Perform a proper ConfigureSelf authority check. If a
103 // session is being used to DELETE some other user's session,
104 // then the ConfigureSelf privilege does not apply. In that
105 // case, perform the authority check again without the user's
106 // ConfigureSelf privilege.
107 if (session->username != req.session->username)
108 {
109 if (!isAllowedWithoutConfigureSelf(req))
110 {
111 BMCWEB_LOG_WARNING << "DELETE Session denied access";
zhanghch058d1b46d2021-04-01 11:18:24 +0800112 messages::insufficientPrivilege(asyncResp->res);
Joseph Reynolds900f9492019-11-25 15:37:29 -0600113 return;
114 }
115 }
116
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 // DELETE should return representation of object that will be removed
zhanghch058d1b46d2021-04-01 11:18:24 +0800118 doGet(asyncResp, req, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119
Ed Tanous52cc1122020-07-18 13:51:21 -0700120 persistent_data::SessionStore::getInstance().removeSession(session);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100121 }
122
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123 /**
124 * This allows SessionCollection to reuse this class' doGet method, to
125 * maintain consistency of returned data, as Collection's doPost should
Sunitha Harish92f68222020-05-28 05:09:09 -0500126 * return data for created member which should match member's doGet
127 * result in 100%
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128 */
129 friend SessionCollection;
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100130};
131
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132class SessionCollection : public Node
133{
134 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700135 SessionCollection(App& app) :
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 Node(app, "/redfish/v1/SessionService/Sessions/"), memberSession(app)
137 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138 entityPrivileges = {
139 {boost::beast::http::verb::get, {{"Login"}}},
140 {boost::beast::http::verb::head, {{"Login"}}},
141 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
142 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
143 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
144 {boost::beast::http::verb::post, {}}};
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100145 }
146
Ed Tanous1abe55e2018-09-05 08:30:59 -0700147 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800148 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
149 const crow::Request&, const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700150 {
151 std::vector<const std::string*> sessionIds =
Ed Tanous52cc1122020-07-18 13:51:21 -0700152 persistent_data::SessionStore::getInstance().getUniqueIds(
153 false, persistent_data::PersistenceType::TIMEOUT);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100154
zhanghch058d1b46d2021-04-01 11:18:24 +0800155 asyncResp->res.jsonValue["Members@odata.count"] = sessionIds.size();
156 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700157 for (const std::string* uid : sessionIds)
158 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800159 asyncResp->res.jsonValue["Members"].push_back(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160 {{"@odata.id", "/redfish/v1/SessionService/Sessions/" + *uid}});
161 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800162 asyncResp->res.jsonValue["Members@odata.count"] = sessionIds.size();
163 asyncResp->res.jsonValue["@odata.type"] =
164 "#SessionCollection.SessionCollection";
165 asyncResp->res.jsonValue["@odata.id"] =
166 "/redfish/v1/SessionService/Sessions/";
167 asyncResp->res.jsonValue["Name"] = "Session Collection";
168 asyncResp->res.jsonValue["Description"] = "Session Collection";
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100169 }
170
zhanghch058d1b46d2021-04-01 11:18:24 +0800171 void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
172 const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +0000173 const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700174 {
Ed Tanous9712f8a2018-09-21 13:38:49 -0700175 std::string username;
176 std::string password;
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500177 std::optional<nlohmann::json> oemObject;
178 std::string clientId;
zhanghch058d1b46d2021-04-01 11:18:24 +0800179 if (!json_util::readJson(req, asyncResp->res, "UserName", username,
180 "Password", password, "Oem", oemObject))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700182 return;
183 }
Ed Tanousb9845d92018-07-24 14:38:06 -0700184
Ed Tanous820ce592018-10-04 15:54:21 -0700185 if (password.empty() || username.empty() ||
zhanghch058d1b46d2021-04-01 11:18:24 +0800186 asyncResp->res.result() != boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700187 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700188 if (username.empty())
189 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800190 messages::propertyMissing(asyncResp->res, "UserName");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700191 }
192
193 if (password.empty())
194 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800195 messages::propertyMissing(asyncResp->res, "Password");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700196 }
197
Ed Tanous820ce592018-10-04 15:54:21 -0700198 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700199 }
200
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600201 int pamrc = pamAuthenticateUser(username, password);
202 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
203 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800205 messages::resourceAtUriUnauthorized(asyncResp->res,
206 std::string(req.url),
Jason M. Billsf12894f2018-10-09 12:45:45 -0700207 "Invalid username or password");
Ed Tanous820ce592018-10-04 15:54:21 -0700208 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700209 }
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500210#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
211 if (oemObject)
212 {
213 std::optional<nlohmann::json> bmcOem;
zhanghch058d1b46d2021-04-01 11:18:24 +0800214 if (!json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
215 bmcOem))
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500216 {
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500217 return;
218 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800219 if (!json_util::readJson(*bmcOem, asyncResp->res, "ClientID",
220 clientId))
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500221 {
222 BMCWEB_LOG_ERROR << "Could not read ClientId";
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500223 return;
224 }
225 }
226#endif
Manojkiran Eda6f115bb2020-06-27 09:52:23 +0530227
Ed Tanous820ce592018-10-04 15:54:21 -0700228 // User is authenticated - create session
Ed Tanous52cc1122020-07-18 13:51:21 -0700229 std::shared_ptr<persistent_data::UserSession> session =
230 persistent_data::SessionStore::getInstance().generateUserSession(
Sunitha Harishd3239222021-02-24 15:33:29 +0530231 username, req.ipAddress.to_string(), clientId,
232 persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly);
zhanghch058d1b46d2021-04-01 11:18:24 +0800233 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
234 asyncResp->res.addHeader("Location",
235 "/redfish/v1/SessionService/Sessions/" +
236 session->uniqueId);
237 asyncResp->res.result(boost::beast::http::status::created);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600238 if (session->isConfigureSelfOnly)
239 {
240 messages::passwordChangeRequired(
zhanghch058d1b46d2021-04-01 11:18:24 +0800241 asyncResp->res,
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600242 "/redfish/v1/AccountService/Accounts/" + session->username);
243 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800244 memberSession.doGet(asyncResp, req, {session->uniqueId});
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100245 }
246
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247 /**
248 * Member session to ensure consistency between collection's doPost and
249 * member's doGet, as they should return 100% matching data
250 */
251 Sessions memberSession;
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100252};
253
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254class SessionService : public Node
255{
256 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700257 SessionService(App& app) : Node(app, "/redfish/v1/SessionService/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700258 {
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800259
Ed Tanous1abe55e2018-09-05 08:30:59 -0700260 entityPrivileges = {
261 {boost::beast::http::verb::get, {{"Login"}}},
262 {boost::beast::http::verb::head, {{"Login"}}},
263 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
264 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
265 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
266 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
267 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100268
Ed Tanous1abe55e2018-09-05 08:30:59 -0700269 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800270 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
271 const crow::Request&, const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700272 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800273 asyncResp->res.jsonValue["@odata.type"] =
274 "#SessionService.v1_0_2.SessionService";
275 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
276 asyncResp->res.jsonValue["Name"] = "Session Service";
277 asyncResp->res.jsonValue["Id"] = "SessionService";
278 asyncResp->res.jsonValue["Description"] = "Session Service";
279 asyncResp->res.jsonValue["SessionTimeout"] =
Ed Tanous52cc1122020-07-18 13:51:21 -0700280 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
zhanghch058d1b46d2021-04-01 11:18:24 +0800281 asyncResp->res.jsonValue["ServiceEnabled"] = true;
Ed Tanous0f74e642018-11-12 15:17:05 -0800282
zhanghch058d1b46d2021-04-01 11:18:24 +0800283 asyncResp->res.jsonValue["Sessions"] = {
Ed Tanous0f261532019-02-08 11:13:29 -0800284 {"@odata.id", "/redfish/v1/SessionService/Sessions"}};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700285 }
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530286
zhanghch058d1b46d2021-04-01 11:18:24 +0800287 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
288 const crow::Request& req,
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530289 const std::vector<std::string>&) override
290 {
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530291 std::optional<int64_t> sessionTimeout;
zhanghch058d1b46d2021-04-01 11:18:24 +0800292 if (!json_util::readJson(req, asyncResp->res, "SessionTimeout",
293 sessionTimeout))
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530294 {
295 return;
296 }
297
298 if (sessionTimeout)
299 {
300 // The mininum & maximum allowed values for session timeout are 30
301 // seconds and 86400 seconds respectively as per the session service
302 // schema mentioned at
303 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
304
305 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
306 {
307 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
308 persistent_data::SessionStore::getInstance()
309 .updateSessionTimeout(sessionTimeoutInseconds);
310 messages::propertyValueModified(
311 asyncResp->res, "SessionTimeOut",
312 std::to_string(*sessionTimeout));
313 }
314 else
315 {
316 messages::propertyValueNotInList(
zhanghch058d1b46d2021-04-01 11:18:24 +0800317 asyncResp->res, std::to_string(*sessionTimeout),
318 "SessionTimeOut");
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530319 }
320 }
321 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100322};
323
Ed Tanous1abe55e2018-09-05 08:30:59 -0700324} // namespace redfish