blob: 515f5bea633b7b82f49acfc36dc8ebb1468075af [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";
Ed Tanouse0d918b2018-03-27 17:41:04 -070066
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010068 }
69
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 void doDelete(crow::Response& res, const crow::Request& req,
71 const std::vector<std::string>& params) override
72 {
73 // Need only 1 param which should be id of session to be deleted
74 if (params.size() != 1)
75 {
76 // This should be handled by crow and never happen
77 BMCWEB_LOG_ERROR << "Session DELETE has been called with invalid "
78 "number of params";
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010079
Jason M. Billsf12894f2018-10-09 12:45:45 -070080 messages::generalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 res.end();
82 return;
83 }
84
85 auto session =
86 crow::persistent_data::SessionStore::getInstance().getSessionByUid(
87 params[0]);
88
89 if (session == nullptr)
90 {
Jason M. Billsf12894f2018-10-09 12:45:45 -070091 messages::resourceNotFound(res, "Session", params[0]);
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 res.end();
93 return;
94 }
95
Joseph Reynolds900f9492019-11-25 15:37:29 -060096 // Perform a proper ConfigureSelf authority check. If a
97 // session is being used to DELETE some other user's session,
98 // then the ConfigureSelf privilege does not apply. In that
99 // case, perform the authority check again without the user's
100 // ConfigureSelf privilege.
101 if (session->username != req.session->username)
102 {
103 if (!isAllowedWithoutConfigureSelf(req))
104 {
105 BMCWEB_LOG_WARNING << "DELETE Session denied access";
106 messages::insufficientPrivilege(res);
107 res.end();
108 return;
109 }
110 }
111
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 // DELETE should return representation of object that will be removed
113 doGet(res, req, params);
114
115 crow::persistent_data::SessionStore::getInstance().removeSession(
116 session);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100117 }
118
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 /**
120 * This allows SessionCollection to reuse this class' doGet method, to
121 * maintain consistency of returned data, as Collection's doPost should
122 * return data for created member which should match member's doGet result
123 * in 100%
124 */
125 friend SessionCollection;
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100126};
127
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128class SessionCollection : public Node
129{
130 public:
131 SessionCollection(CrowApp& app) :
132 Node(app, "/redfish/v1/SessionService/Sessions/"), memberSession(app)
133 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700134 entityPrivileges = {
135 {boost::beast::http::verb::get, {{"Login"}}},
136 {boost::beast::http::verb::head, {{"Login"}}},
137 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
138 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
139 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
140 {boost::beast::http::verb::post, {}}};
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100141 }
142
Ed Tanous1abe55e2018-09-05 08:30:59 -0700143 private:
144 void doGet(crow::Response& res, const crow::Request& req,
145 const std::vector<std::string>& params) override
146 {
147 std::vector<const std::string*> sessionIds =
148 crow::persistent_data::SessionStore::getInstance().getUniqueIds(
149 false, crow::persistent_data::PersistenceType::TIMEOUT);
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100150
Ed Tanous0f74e642018-11-12 15:17:05 -0800151 res.jsonValue["Members@odata.count"] = sessionIds.size();
152 res.jsonValue["Members"] = nlohmann::json::array();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 for (const std::string* uid : sessionIds)
154 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800155 res.jsonValue["Members"].push_back(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700156 {{"@odata.id", "/redfish/v1/SessionService/Sessions/" + *uid}});
157 }
Tanousf00032d2018-11-05 01:18:10 -0300158 res.jsonValue["Members@odata.count"] = sessionIds.size();
Ed Tanous0f74e642018-11-12 15:17:05 -0800159 res.jsonValue["@odata.type"] = "#SessionCollection.SessionCollection";
160 res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/Sessions/";
Ed Tanous0f74e642018-11-12 15:17:05 -0800161 res.jsonValue["Name"] = "Session Collection";
162 res.jsonValue["Description"] = "Session Collection";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700163 res.end();
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100164 }
165
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 void doPost(crow::Response& res, const crow::Request& req,
167 const std::vector<std::string>& params) override
168 {
Ed Tanous9712f8a2018-09-21 13:38:49 -0700169 std::string username;
170 std::string password;
171 if (!json_util::readJson(req, res, "UserName", username, "Password",
172 password))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700173 {
174 res.end();
175 return;
176 }
Ed Tanousb9845d92018-07-24 14:38:06 -0700177
Ed Tanous820ce592018-10-04 15:54:21 -0700178 if (password.empty() || username.empty() ||
179 res.result() != boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 if (username.empty())
182 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800183 messages::propertyMissing(res, "UserName");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184 }
185
186 if (password.empty())
187 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800188 messages::propertyMissing(res, "Password");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700189 }
Ed Tanous820ce592018-10-04 15:54:21 -0700190 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700191
Ed Tanous820ce592018-10-04 15:54:21 -0700192 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700193 }
194
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600195 int pamrc = pamAuthenticateUser(username, password);
196 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
197 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700198 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700199 messages::resourceAtUriUnauthorized(res, std::string(req.url),
200 "Invalid username or password");
Ed Tanous820ce592018-10-04 15:54:21 -0700201 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202
Ed Tanous820ce592018-10-04 15:54:21 -0700203 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 }
205
Ed Tanous820ce592018-10-04 15:54:21 -0700206 // User is authenticated - create session
207 std::shared_ptr<crow::persistent_data::UserSession> session =
208 crow::persistent_data::SessionStore::getInstance()
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600209 .generateUserSession(
210 username, crow::persistent_data::PersistenceType::TIMEOUT,
211 isConfigureSelfOnly);
Ed Tanous820ce592018-10-04 15:54:21 -0700212 res.addHeader("X-Auth-Token", session->sessionToken);
213 res.addHeader("Location", "/redfish/v1/SessionService/Sessions/" +
214 session->uniqueId);
215 res.result(boost::beast::http::status::created);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600216 if (session->isConfigureSelfOnly)
217 {
218 messages::passwordChangeRequired(
219 res,
220 "/redfish/v1/AccountService/Accounts/" + session->username);
221 }
Ed Tanous820ce592018-10-04 15:54:21 -0700222 memberSession.doGet(res, req, {session->uniqueId});
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100223 }
224
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 /**
226 * Member session to ensure consistency between collection's doPost and
227 * member's doGet, as they should return 100% matching data
228 */
229 Sessions memberSession;
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100230};
231
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232class SessionService : public Node
233{
234 public:
235 SessionService(CrowApp& app) : Node(app, "/redfish/v1/SessionService/")
236 {
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800237
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 entityPrivileges = {
239 {boost::beast::http::verb::get, {{"Login"}}},
240 {boost::beast::http::verb::head, {{"Login"}}},
241 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
242 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
243 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
244 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
245 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100246
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247 private:
248 void doGet(crow::Response& res, const crow::Request& req,
249 const std::vector<std::string>& params) override
250 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800251 res.jsonValue["@odata.type"] = "#SessionService.v1_0_2.SessionService";
252 res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
Ed Tanous0f74e642018-11-12 15:17:05 -0800253 res.jsonValue["Name"] = "Session Service";
254 res.jsonValue["Id"] = "SessionService";
255 res.jsonValue["Description"] = "Session Service";
256 res.jsonValue["SessionTimeout"] =
257 crow::persistent_data::SessionStore::getInstance()
258 .getTimeoutInSeconds();
259 res.jsonValue["ServiceEnabled"] = true;
260
Ed Tanous0f261532019-02-08 11:13:29 -0800261 res.jsonValue["Sessions"] = {
262 {"@odata.id", "/redfish/v1/SessionService/Sessions"}};
263
Ed Tanous1abe55e2018-09-05 08:30:59 -0700264 res.end();
265 }
Borawski.Lukasz5d27b852018-02-08 13:24:24 +0100266};
267
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268} // namespace redfish