blob: bef2753bb3e386928633b376e1bacf71ef56cfdd [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
Ed Tanous1abe55e2018-09-05 08:30:59 -070022namespace redfish
23{
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010024
25class SessionCollection;
26
Ed Tanous1abe55e2018-09-05 08:30:59 -070027class Sessions : public Node
28{
29 public:
Ed Tanous52cc1122020-07-18 13:51:21 -070030 Sessions(App& app) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070031 Node(app, "/redfish/v1/SessionService/Sessions/<str>/", std::string())
32 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070033 entityPrivileges = {
34 {boost::beast::http::verb::get, {{"Login"}}},
35 {boost::beast::http::verb::head, {{"Login"}}},
36 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
37 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
Joseph Reynolds900f9492019-11-25 15:37:29 -060038 {boost::beast::http::verb::delete_,
39 {{"ConfigureManager"}, {"ConfigureSelf"}}},
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010041 }
42
Ed Tanous1abe55e2018-09-05 08:30:59 -070043 private:
zhanghch058d1b46d2021-04-01 11:18:24 +080044 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
45 const crow::Request&,
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 const std::vector<std::string>& params) override
47 {
Joseph Reynolds900f9492019-11-25 15:37:29 -060048 // Note that control also reaches here via doPost and doDelete.
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 auto session =
Ed Tanous52cc1122020-07-18 13:51:21 -070050 persistent_data::SessionStore::getInstance().getSessionByUid(
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 params[0]);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010052
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 if (session == nullptr)
54 {
zhanghch058d1b46d2021-04-01 11:18:24 +080055 messages::resourceNotFound(asyncResp->res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 return;
57 }
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010058
zhanghch058d1b46d2021-04-01 11:18:24 +080059 asyncResp->res.jsonValue["Id"] = session->uniqueId;
60 asyncResp->res.jsonValue["UserName"] = session->username;
61 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 "/redfish/v1/SessionService/Sessions/" + session->uniqueId;
zhanghch058d1b46d2021-04-01 11:18:24 +080063 asyncResp->res.jsonValue["@odata.type"] = "#Session.v1_3_0.Session";
64 asyncResp->res.jsonValue["Name"] = "User Session";
65 asyncResp->res.jsonValue["Description"] = "Manager User Session";
66 asyncResp->res.jsonValue["ClientOriginIPAddress"] = session->clientIp;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050067#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
zhanghch058d1b46d2021-04-01 11:18:24 +080068 asyncResp->res.jsonValue["Oem"]["OpenBMC"]["@odata.type"] =
Sunitha Harish08bdcc72020-05-12 05:17:57 -050069 "#OemSession.v1_0_0.Session";
zhanghch058d1b46d2021-04-01 11:18:24 +080070 asyncResp->res.jsonValue["Oem"]["OpenBMC"]["ClientID"] =
71 session->clientId;
Sunitha Harish08bdcc72020-05-12 05:17:57 -050072#endif
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010073 }
74
zhanghch058d1b46d2021-04-01 11:18:24 +080075 void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
76 const crow::Request& req,
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 const std::vector<std::string>& params) override
78 {
79 // Need only 1 param which should be id of session to be deleted
80 if (params.size() != 1)
81 {
82 // This should be handled by crow and never happen
83 BMCWEB_LOG_ERROR << "Session DELETE has been called with invalid "
84 "number of params";
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010085
zhanghch058d1b46d2021-04-01 11:18:24 +080086 messages::generalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -070087 return;
88 }
89
90 auto session =
Ed Tanous52cc1122020-07-18 13:51:21 -070091 persistent_data::SessionStore::getInstance().getSessionByUid(
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 params[0]);
93
94 if (session == nullptr)
95 {
zhanghch058d1b46d2021-04-01 11:18:24 +080096 messages::resourceNotFound(asyncResp->res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 return;
98 }
99
Joseph Reynolds900f9492019-11-25 15:37:29 -0600100 // Perform a proper ConfigureSelf authority check. If a
101 // session is being used to DELETE some other user's session,
102 // then the ConfigureSelf privilege does not apply. In that
103 // case, perform the authority check again without the user's
104 // ConfigureSelf privilege.
105 if (session->username != req.session->username)
106 {
107 if (!isAllowedWithoutConfigureSelf(req))
108 {
109 BMCWEB_LOG_WARNING << "DELETE Session denied access";
zhanghch058d1b46d2021-04-01 11:18:24 +0800110 messages::insufficientPrivilege(asyncResp->res);
Joseph Reynolds900f9492019-11-25 15:37:29 -0600111 return;
112 }
113 }
114
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 // DELETE should return representation of object that will be removed
zhanghch058d1b46d2021-04-01 11:18:24 +0800116 doGet(asyncResp, req, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117
Ed Tanous52cc1122020-07-18 13:51:21 -0700118 persistent_data::SessionStore::getInstance().removeSession(session);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100119 }
120
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 /**
122 * This allows SessionCollection to reuse this class' doGet method, to
123 * maintain consistency of returned data, as Collection's doPost should
Sunitha Harish92f68222020-05-28 05:09:09 -0500124 * return data for created member which should match member's doGet
125 * result in 100%
Ed Tanous1abe55e2018-09-05 08:30:59 -0700126 */
127 friend SessionCollection;
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100128};
129
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130class SessionCollection : public Node
131{
132 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700133 SessionCollection(App& app) :
Ed Tanous1abe55e2018-09-05 08:30:59 -0700134 Node(app, "/redfish/v1/SessionService/Sessions/"), memberSession(app)
135 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 entityPrivileges = {
137 {boost::beast::http::verb::get, {{"Login"}}},
138 {boost::beast::http::verb::head, {{"Login"}}},
139 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
140 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
141 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
142 {boost::beast::http::verb::post, {}}};
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100143 }
144
Ed Tanous1abe55e2018-09-05 08:30:59 -0700145 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800146 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
147 const crow::Request&, const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 {
149 std::vector<const std::string*> sessionIds =
Ed Tanous52cc1122020-07-18 13:51:21 -0700150 persistent_data::SessionStore::getInstance().getUniqueIds(
151 false, persistent_data::PersistenceType::TIMEOUT);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100152
zhanghch058d1b46d2021-04-01 11:18:24 +0800153 asyncResp->res.jsonValue["Members@odata.count"] = sessionIds.size();
154 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700155 for (const std::string* uid : sessionIds)
156 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800157 asyncResp->res.jsonValue["Members"].push_back(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158 {{"@odata.id", "/redfish/v1/SessionService/Sessions/" + *uid}});
159 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800160 asyncResp->res.jsonValue["Members@odata.count"] = sessionIds.size();
161 asyncResp->res.jsonValue["@odata.type"] =
162 "#SessionCollection.SessionCollection";
163 asyncResp->res.jsonValue["@odata.id"] =
164 "/redfish/v1/SessionService/Sessions/";
165 asyncResp->res.jsonValue["Name"] = "Session Collection";
166 asyncResp->res.jsonValue["Description"] = "Session Collection";
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100167 }
168
zhanghch058d1b46d2021-04-01 11:18:24 +0800169 void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
170 const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +0000171 const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172 {
Ed Tanous9712f8a2018-09-21 13:38:49 -0700173 std::string username;
174 std::string password;
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500175 std::optional<nlohmann::json> oemObject;
176 std::string clientId;
zhanghch058d1b46d2021-04-01 11:18:24 +0800177 if (!json_util::readJson(req, asyncResp->res, "UserName", username,
178 "Password", password, "Oem", oemObject))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 return;
181 }
Ed Tanousb9845d92018-07-24 14:38:06 -0700182
Ed Tanous820ce592018-10-04 15:54:21 -0700183 if (password.empty() || username.empty() ||
zhanghch058d1b46d2021-04-01 11:18:24 +0800184 asyncResp->res.result() != boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700185 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186 if (username.empty())
187 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800188 messages::propertyMissing(asyncResp->res, "UserName");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700189 }
190
191 if (password.empty())
192 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800193 messages::propertyMissing(asyncResp->res, "Password");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700194 }
195
Ed Tanous820ce592018-10-04 15:54:21 -0700196 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700197 }
198
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600199 int pamrc = pamAuthenticateUser(username, password);
200 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
201 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800203 messages::resourceAtUriUnauthorized(asyncResp->res,
204 std::string(req.url),
Jason M. Billsf12894f2018-10-09 12:45:45 -0700205 "Invalid username or password");
Ed Tanous820ce592018-10-04 15:54:21 -0700206 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700207 }
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500208#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
209 if (oemObject)
210 {
211 std::optional<nlohmann::json> bmcOem;
zhanghch058d1b46d2021-04-01 11:18:24 +0800212 if (!json_util::readJson(*oemObject, asyncResp->res, "OpenBMC",
213 bmcOem))
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500214 {
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500215 return;
216 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800217 if (!json_util::readJson(*bmcOem, asyncResp->res, "ClientID",
218 clientId))
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500219 {
220 BMCWEB_LOG_ERROR << "Could not read ClientId";
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500221 return;
222 }
223 }
224#endif
Manojkiran Eda6f115bb2020-06-27 09:52:23 +0530225
Ed Tanous820ce592018-10-04 15:54:21 -0700226 // User is authenticated - create session
Ed Tanous52cc1122020-07-18 13:51:21 -0700227 std::shared_ptr<persistent_data::UserSession> session =
228 persistent_data::SessionStore::getInstance().generateUserSession(
Sunitha Harishd3239222021-02-24 15:33:29 +0530229 username, req.ipAddress.to_string(), clientId,
230 persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly);
zhanghch058d1b46d2021-04-01 11:18:24 +0800231 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
232 asyncResp->res.addHeader("Location",
233 "/redfish/v1/SessionService/Sessions/" +
234 session->uniqueId);
235 asyncResp->res.result(boost::beast::http::status::created);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600236 if (session->isConfigureSelfOnly)
237 {
238 messages::passwordChangeRequired(
zhanghch058d1b46d2021-04-01 11:18:24 +0800239 asyncResp->res,
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600240 "/redfish/v1/AccountService/Accounts/" + session->username);
241 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800242 memberSession.doGet(asyncResp, req, {session->uniqueId});
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100243 }
244
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 /**
246 * Member session to ensure consistency between collection's doPost and
247 * member's doGet, as they should return 100% matching data
248 */
249 Sessions memberSession;
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100250};
251
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252class SessionService : public Node
253{
254 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700255 SessionService(App& app) : Node(app, "/redfish/v1/SessionService/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700256 {
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800257
Ed Tanous1abe55e2018-09-05 08:30:59 -0700258 entityPrivileges = {
259 {boost::beast::http::verb::get, {{"Login"}}},
260 {boost::beast::http::verb::head, {{"Login"}}},
261 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
262 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
263 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
264 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
265 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100266
Ed Tanous1abe55e2018-09-05 08:30:59 -0700267 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800268 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
269 const crow::Request&, const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700270 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800271 asyncResp->res.jsonValue["@odata.type"] =
272 "#SessionService.v1_0_2.SessionService";
273 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
274 asyncResp->res.jsonValue["Name"] = "Session Service";
275 asyncResp->res.jsonValue["Id"] = "SessionService";
276 asyncResp->res.jsonValue["Description"] = "Session Service";
277 asyncResp->res.jsonValue["SessionTimeout"] =
Ed Tanous52cc1122020-07-18 13:51:21 -0700278 persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
zhanghch058d1b46d2021-04-01 11:18:24 +0800279 asyncResp->res.jsonValue["ServiceEnabled"] = true;
Ed Tanous0f74e642018-11-12 15:17:05 -0800280
zhanghch058d1b46d2021-04-01 11:18:24 +0800281 asyncResp->res.jsonValue["Sessions"] = {
Ed Tanous0f261532019-02-08 11:13:29 -0800282 {"@odata.id", "/redfish/v1/SessionService/Sessions"}};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700283 }
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530284
zhanghch058d1b46d2021-04-01 11:18:24 +0800285 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
286 const crow::Request& req,
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530287 const std::vector<std::string>&) override
288 {
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530289 std::optional<int64_t> sessionTimeout;
zhanghch058d1b46d2021-04-01 11:18:24 +0800290 if (!json_util::readJson(req, asyncResp->res, "SessionTimeout",
291 sessionTimeout))
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530292 {
293 return;
294 }
295
296 if (sessionTimeout)
297 {
298 // The mininum & maximum allowed values for session timeout are 30
299 // seconds and 86400 seconds respectively as per the session service
300 // schema mentioned at
301 // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
302
303 if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
304 {
305 std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
306 persistent_data::SessionStore::getInstance()
307 .updateSessionTimeout(sessionTimeoutInseconds);
308 messages::propertyValueModified(
309 asyncResp->res, "SessionTimeOut",
310 std::to_string(*sessionTimeout));
311 }
312 else
313 {
314 messages::propertyValueNotInList(
zhanghch058d1b46d2021-04-01 11:18:24 +0800315 asyncResp->res, std::to_string(*sessionTimeout),
316 "SessionTimeOut");
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530317 }
318 }
319 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100320};
321
Ed Tanous1abe55e2018-09-05 08:30:59 -0700322} // namespace redfish