blob: 4dbb64daba077c8ce5f6685fd44e829ea4fe4de4 [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"
Borawski.Lukasz4b1b8682018-04-04 12:50:16 +020020#include "persistent_data_middleware.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:
30 Sessions(CrowApp& app) :
31 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:
44 void doGet(crow::Response& res, const crow::Request& req,
45 const std::vector<std::string>& params) override
46 {
Joseph Reynolds900f9492019-11-25 15:37:29 -060047 // Note that control also reaches here via doPost and doDelete.
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 auto session =
49 crow::persistent_data::SessionStore::getInstance().getSessionByUid(
50 params[0]);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010051
Ed Tanous1abe55e2018-09-05 08:30:59 -070052 if (session == nullptr)
53 {
Jason M. Billsf12894f2018-10-09 12:45:45 -070054 messages::resourceNotFound(res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 res.end();
56 return;
57 }
Kowalski, Kamilf4c4dcf2018-01-29 14:55:35 +010058
Ed Tanous0f74e642018-11-12 15:17:05 -080059 res.jsonValue["Id"] = session->uniqueId;
60 res.jsonValue["UserName"] = session->username;
61 res.jsonValue["@odata.id"] =
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 "/redfish/v1/SessionService/Sessions/" + session->uniqueId;
Ed Tanous0f74e642018-11-12 15:17:05 -080063 res.jsonValue["@odata.type"] = "#Session.v1_0_2.Session";
Ed Tanous0f74e642018-11-12 15:17:05 -080064 res.jsonValue["Name"] = "User Session";
65 res.jsonValue["Description"] = "Manager User Session";
Sunitha Harish08bdcc72020-05-12 05:17:57 -050066#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
67 res.jsonValue["Oem"]["OpenBMC"]["@odata.type"] =
68 "#OemSession.v1_0_0.Session";
69 res.jsonValue["Oem"]["OpenBMC"]["ClientID"] = session->clientId;
70#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010072 }
73
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 void doDelete(crow::Response& res, const crow::Request& req,
75 const std::vector<std::string>& params) override
76 {
77 // Need only 1 param which should be id of session to be deleted
78 if (params.size() != 1)
79 {
80 // This should be handled by crow and never happen
81 BMCWEB_LOG_ERROR << "Session DELETE has been called with invalid "
82 "number of params";
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010083
Jason M. Billsf12894f2018-10-09 12:45:45 -070084 messages::generalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 res.end();
86 return;
87 }
88
89 auto session =
90 crow::persistent_data::SessionStore::getInstance().getSessionByUid(
91 params[0]);
92
93 if (session == nullptr)
94 {
Jason M. Billsf12894f2018-10-09 12:45:45 -070095 messages::resourceNotFound(res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 res.end();
97 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";
110 messages::insufficientPrivilege(res);
111 res.end();
112 return;
113 }
114 }
115
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 // DELETE should return representation of object that will be removed
117 doGet(res, req, params);
118
119 crow::persistent_data::SessionStore::getInstance().removeSession(
120 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
126 * return data for created member which should match member's doGet result
127 * in 100%
128 */
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:
135 SessionCollection(CrowApp& app) :
136 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:
148 void doGet(crow::Response& res, const crow::Request& req,
149 const std::vector<std::string>& params) override
150 {
151 std::vector<const std::string*> sessionIds =
152 crow::persistent_data::SessionStore::getInstance().getUniqueIds(
153 false, crow::persistent_data::PersistenceType::TIMEOUT);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100154
Ed Tanous0f74e642018-11-12 15:17:05 -0800155 res.jsonValue["Members@odata.count"] = sessionIds.size();
156 res.jsonValue["Members"] = nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700157 for (const std::string* uid : sessionIds)
158 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800159 res.jsonValue["Members"].push_back(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160 {{"@odata.id", "/redfish/v1/SessionService/Sessions/" + *uid}});
161 }
Tanousf00032d2018-11-05 01:18:10 -0300162 res.jsonValue["Members@odata.count"] = sessionIds.size();
Ed Tanous0f74e642018-11-12 15:17:05 -0800163 res.jsonValue["@odata.type"] = "#SessionCollection.SessionCollection";
164 res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/Sessions/";
Ed Tanous0f74e642018-11-12 15:17:05 -0800165 res.jsonValue["Name"] = "Session Collection";
166 res.jsonValue["Description"] = "Session Collection";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700167 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100168 }
169
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170 void doPost(crow::Response& res, const crow::Request& req,
171 const std::vector<std::string>& params) override
172 {
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;
Ed Tanous9712f8a2018-09-21 13:38:49 -0700177 if (!json_util::readJson(req, res, "UserName", username, "Password",
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500178 password, "Oem", oemObject))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179 {
180 res.end();
181 return;
182 }
Ed Tanousb9845d92018-07-24 14:38:06 -0700183
Ed Tanous820ce592018-10-04 15:54:21 -0700184 if (password.empty() || username.empty() ||
185 res.result() != boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700187 if (username.empty())
188 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800189 messages::propertyMissing(res, "UserName");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700190 }
191
192 if (password.empty())
193 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800194 messages::propertyMissing(res, "Password");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 }
Ed Tanous820ce592018-10-04 15:54:21 -0700196 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700197
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 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700205 messages::resourceAtUriUnauthorized(res, std::string(req.url),
206 "Invalid username or password");
Ed Tanous820ce592018-10-04 15:54:21 -0700207 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208
Ed Tanous820ce592018-10-04 15:54:21 -0700209 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700210 }
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500211#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
212 if (oemObject)
213 {
214 std::optional<nlohmann::json> bmcOem;
215 if (!json_util::readJson(*oemObject, res, "OpenBMC", bmcOem))
216 {
217 res.end();
218 return;
219 }
220 if (!json_util::readJson(*bmcOem, res, "ClientID", clientId))
221 {
222 BMCWEB_LOG_ERROR << "Could not read ClientId";
223 res.end();
224 return;
225 }
226 }
227#endif
Ed Tanous820ce592018-10-04 15:54:21 -0700228 // User is authenticated - create session
229 std::shared_ptr<crow::persistent_data::UserSession> session =
230 crow::persistent_data::SessionStore::getInstance()
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600231 .generateUserSession(
232 username, crow::persistent_data::PersistenceType::TIMEOUT,
Sunitha Harish08bdcc72020-05-12 05:17:57 -0500233 isConfigureSelfOnly, clientId);
Ed Tanous820ce592018-10-04 15:54:21 -0700234 res.addHeader("X-Auth-Token", session->sessionToken);
235 res.addHeader("Location", "/redfish/v1/SessionService/Sessions/" +
236 session->uniqueId);
237 res.result(boost::beast::http::status::created);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600238 if (session->isConfigureSelfOnly)
239 {
240 messages::passwordChangeRequired(
241 res,
242 "/redfish/v1/AccountService/Accounts/" + session->username);
243 }
Ed Tanous820ce592018-10-04 15:54:21 -0700244 memberSession.doGet(res, 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:
257 SessionService(CrowApp& app) : Node(app, "/redfish/v1/SessionService/")
258 {
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:
270 void doGet(crow::Response& res, const crow::Request& req,
271 const std::vector<std::string>& params) override
272 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800273 res.jsonValue["@odata.type"] = "#SessionService.v1_0_2.SessionService";
274 res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
Ed Tanous0f74e642018-11-12 15:17:05 -0800275 res.jsonValue["Name"] = "Session Service";
276 res.jsonValue["Id"] = "SessionService";
277 res.jsonValue["Description"] = "Session Service";
278 res.jsonValue["SessionTimeout"] =
279 crow::persistent_data::SessionStore::getInstance()
280 .getTimeoutInSeconds();
281 res.jsonValue["ServiceEnabled"] = true;
282
Ed Tanous0f261532019-02-08 11:13:29 -0800283 res.jsonValue["Sessions"] = {
284 {"@odata.id", "/redfish/v1/SessionService/Sessions"}};
285
Ed Tanous1abe55e2018-09-05 08:30:59 -0700286 res.end();
287 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100288};
289
Ed Tanous1abe55e2018-09-05 08:30:59 -0700290} // namespace redfish