xref: /openbmc/bmcweb/features/redfish/lib/redfish_sessions.hpp (revision 41d61c8281ed9af50630c78f0a646e726139fb25)
12b7981f6SKowalski, Kamil /*
22b7981f6SKowalski, Kamil // Copyright (c) 2018 Intel Corporation
32b7981f6SKowalski, Kamil //
42b7981f6SKowalski, Kamil // Licensed under the Apache License, Version 2.0 (the "License");
52b7981f6SKowalski, Kamil // you may not use this file except in compliance with the License.
62b7981f6SKowalski, Kamil // You may obtain a copy of the License at
72b7981f6SKowalski, Kamil //
82b7981f6SKowalski, Kamil //      http://www.apache.org/licenses/LICENSE-2.0
92b7981f6SKowalski, Kamil //
102b7981f6SKowalski, Kamil // Unless required by applicable law or agreed to in writing, software
112b7981f6SKowalski, Kamil // distributed under the License is distributed on an "AS IS" BASIS,
122b7981f6SKowalski, Kamil // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132b7981f6SKowalski, Kamil // See the License for the specific language governing permissions and
142b7981f6SKowalski, Kamil // limitations under the License.
152b7981f6SKowalski, Kamil */
162b7981f6SKowalski, Kamil #pragma once
1743a095abSBorawski.Lukasz 
18f4c4dcf4SKowalski, Kamil #include "error_messages.hpp"
1952cc112dSEd Tanous #include "persistent_data.hpp"
202b7981f6SKowalski, Kamil 
217e860f15SJohn Edward Broadbent #include <app.hpp>
22ed398213SEd Tanous #include <registries/privilege_registry.hpp>
237e860f15SJohn Edward Broadbent 
241abe55efSEd Tanous namespace redfish
251abe55efSEd Tanous {
262b7981f6SKowalski, Kamil 
272b7981f6SKowalski, Kamil class SessionCollection;
282b7981f6SKowalski, Kamil 
294f48d5f6SEd Tanous inline void fillSessionObject(crow::Response& res,
30faa34ccfSEd Tanous                               const persistent_data::UserSession& session)
311abe55efSEd Tanous {
32faa34ccfSEd Tanous     res.jsonValue["Id"] = session.uniqueId;
33faa34ccfSEd Tanous     res.jsonValue["UserName"] = session.username;
34faa34ccfSEd Tanous     res.jsonValue["@odata.id"] =
35faa34ccfSEd Tanous         "/redfish/v1/SessionService/Sessions/" + session.uniqueId;
36faa34ccfSEd Tanous     res.jsonValue["@odata.type"] = "#Session.v1_3_0.Session";
37faa34ccfSEd Tanous     res.jsonValue["Name"] = "User Session";
38faa34ccfSEd Tanous     res.jsonValue["Description"] = "Manager User Session";
39faa34ccfSEd Tanous     res.jsonValue["ClientOriginIPAddress"] = session.clientIp;
40c0ea7ae1SSunitha Harish #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
41faa34ccfSEd Tanous     res.jsonValue["Oem"]["OpenBMC"]["@odata.type"] =
4208bdcc71SSunitha Harish         "#OemSession.v1_0_0.Session";
43faa34ccfSEd Tanous     res.jsonValue["Oem"]["OpenBMC"]["ClientID"] = session.clientId;
4408bdcc71SSunitha Harish #endif
452b7981f6SKowalski, Kamil }
462b7981f6SKowalski, Kamil 
47faa34ccfSEd Tanous inline void requestRoutesSession(App& app)
481abe55efSEd Tanous {
49faa34ccfSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
50ed398213SEd Tanous         .privileges(redfish::privileges::getSession)
51faa34ccfSEd Tanous         .methods(boost::beast::http::verb::get)(
52faa34ccfSEd Tanous             [](const crow::Request& /*req*/,
53faa34ccfSEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
54faa34ccfSEd Tanous                const std::string& sessionId) -> void {
55faa34ccfSEd Tanous                 // Note that control also reaches here via doPost and doDelete.
56faa34ccfSEd Tanous                 auto session = persistent_data::SessionStore::getInstance()
57faa34ccfSEd Tanous                                    .getSessionByUid(sessionId);
582b7981f6SKowalski, Kamil 
591abe55efSEd Tanous                 if (session == nullptr)
601abe55efSEd Tanous                 {
61faa34ccfSEd Tanous                     messages::resourceNotFound(asyncResp->res, "Session",
62faa34ccfSEd Tanous                                                sessionId);
63faa34ccfSEd Tanous                     return;
64faa34ccfSEd Tanous                 }
65faa34ccfSEd Tanous 
66faa34ccfSEd Tanous                 fillSessionObject(asyncResp->res, *session);
67faa34ccfSEd Tanous             });
68faa34ccfSEd Tanous 
69faa34ccfSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
70ed398213SEd Tanous         .privileges(redfish::privileges::deleteSession)
71faa34ccfSEd Tanous         .methods(boost::beast::http::verb::delete_)(
72faa34ccfSEd Tanous             [](const crow::Request& req,
73faa34ccfSEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
74faa34ccfSEd Tanous                const std::string& sessionId) -> void {
75faa34ccfSEd Tanous                 auto session = persistent_data::SessionStore::getInstance()
76faa34ccfSEd Tanous                                    .getSessionByUid(sessionId);
77faa34ccfSEd Tanous 
78faa34ccfSEd Tanous                 if (session == nullptr)
79faa34ccfSEd Tanous                 {
80faa34ccfSEd Tanous                     messages::resourceNotFound(asyncResp->res, "Session",
81faa34ccfSEd Tanous                                                sessionId);
822b7981f6SKowalski, Kamil                     return;
832b7981f6SKowalski, Kamil                 }
842b7981f6SKowalski, Kamil 
85900f9497SJoseph Reynolds                 // Perform a proper ConfigureSelf authority check.  If a
86900f9497SJoseph Reynolds                 // session is being used to DELETE some other user's session,
87900f9497SJoseph Reynolds                 // then the ConfigureSelf privilege does not apply.  In that
88900f9497SJoseph Reynolds                 // case, perform the authority check again without the user's
89900f9497SJoseph Reynolds                 // ConfigureSelf privilege.
90900f9497SJoseph Reynolds                 if (session->username != req.session->username)
91900f9497SJoseph Reynolds                 {
926c51eab1SEd Tanous                     Privileges effectiveUserPrivileges =
936c51eab1SEd Tanous                         redfish::getUserPrivileges(req.userRole);
946c51eab1SEd Tanous 
95faa34ccfSEd Tanous                     if (!effectiveUserPrivileges.isSupersetOf(
964f48d5f6SEd Tanous                             {"ConfigureUsers"}))
97900f9497SJoseph Reynolds                     {
988d1b46d7Szhanghch05                         messages::insufficientPrivilege(asyncResp->res);
99900f9497SJoseph Reynolds                         return;
100900f9497SJoseph Reynolds                     }
101900f9497SJoseph Reynolds                 }
102900f9497SJoseph Reynolds 
103faa34ccfSEd Tanous                 persistent_data::SessionStore::getInstance().removeSession(
104faa34ccfSEd Tanous                     session);
1055cc148afSEd Tanous                 messages::success(asyncResp->res);
106faa34ccfSEd Tanous             });
107f4c4dcf4SKowalski, Kamil 
108faa34ccfSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
109ed398213SEd Tanous         .privileges(redfish::privileges::getSessionCollection)
110faa34ccfSEd Tanous         .methods(boost::beast::http::verb::get)(
111faa34ccfSEd Tanous             [](const crow::Request& /*req*/,
112faa34ccfSEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
11355c7b7a2SEd Tanous                 std::vector<const std::string*> sessionIds =
11452cc112dSEd Tanous                     persistent_data::SessionStore::getInstance().getUniqueIds(
11552cc112dSEd Tanous                         false, persistent_data::PersistenceType::TIMEOUT);
1162b7981f6SKowalski, Kamil 
117faa34ccfSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
118faa34ccfSEd Tanous                     sessionIds.size();
1198d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
1201abe55efSEd Tanous                 for (const std::string* uid : sessionIds)
1211abe55efSEd Tanous                 {
1228d1b46d7Szhanghch05                     asyncResp->res.jsonValue["Members"].push_back(
123faa34ccfSEd Tanous                         {{"@odata.id",
124faa34ccfSEd Tanous                           "/redfish/v1/SessionService/Sessions/" + *uid}});
1252b7981f6SKowalski, Kamil                 }
126faa34ccfSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
127faa34ccfSEd Tanous                     sessionIds.size();
1288d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
1298d1b46d7Szhanghch05                     "#SessionCollection.SessionCollection";
1308d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
1318d1b46d7Szhanghch05                     "/redfish/v1/SessionService/Sessions/";
1328d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Session Collection";
1338d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Description"] = "Session Collection";
134faa34ccfSEd Tanous             });
1352b7981f6SKowalski, Kamil 
136faa34ccfSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
137ed398213SEd Tanous         // Note, this technically doesn't match the privilege registry given the
138ed398213SEd Tanous         // way login mechanisms work.  The base privilege registry lists this
139ed398213SEd Tanous         // endpoint as requiring login privilege, but because this is the
140ed398213SEd Tanous         // endpoint responsible for giving the login privilege, and it is itself
141ed398213SEd Tanous         // its own route, it needs to not require Login
142faa34ccfSEd Tanous         .privileges({})
143faa34ccfSEd Tanous         .methods(boost::beast::http::verb::post)(
144faa34ccfSEd Tanous             [](const crow::Request& req,
145faa34ccfSEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
1469712f8acSEd Tanous                 std::string username;
1479712f8acSEd Tanous                 std::string password;
14808bdcc71SSunitha Harish                 std::optional<nlohmann::json> oemObject;
14908bdcc71SSunitha Harish                 std::string clientId;
150faa34ccfSEd Tanous                 if (!json_util::readJson(req, asyncResp->res, "UserName",
151faa34ccfSEd Tanous                                          username, "Password", password, "Oem",
152faa34ccfSEd Tanous                                          oemObject))
1531abe55efSEd Tanous                 {
1542b7981f6SKowalski, Kamil                     return;
1552b7981f6SKowalski, Kamil                 }
1562b7981f6SKowalski, Kamil 
157820ce598SEd Tanous                 if (password.empty() || username.empty() ||
1588d1b46d7Szhanghch05                     asyncResp->res.result() != boost::beast::http::status::ok)
1591abe55efSEd Tanous                 {
1601abe55efSEd Tanous                     if (username.empty())
1611abe55efSEd Tanous                     {
1628d1b46d7Szhanghch05                         messages::propertyMissing(asyncResp->res, "UserName");
163f4c4dcf4SKowalski, Kamil                     }
164f4c4dcf4SKowalski, Kamil 
1651abe55efSEd Tanous                     if (password.empty())
1661abe55efSEd Tanous                     {
1678d1b46d7Szhanghch05                         messages::propertyMissing(asyncResp->res, "Password");
168820ce598SEd Tanous                     }
169820ce598SEd Tanous 
170820ce598SEd Tanous                     return;
171f4c4dcf4SKowalski, Kamil                 }
1722b7981f6SKowalski, Kamil 
1733bf4e632SJoseph Reynolds                 int pamrc = pamAuthenticateUser(username, password);
1743bf4e632SJoseph Reynolds                 bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
1753bf4e632SJoseph Reynolds                 if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
1761abe55efSEd Tanous                 {
177faa34ccfSEd Tanous                     messages::resourceAtUriUnauthorized(
178faa34ccfSEd Tanous                         asyncResp->res, std::string(req.url),
179f12894f8SJason M. Bills                         "Invalid username or password");
180820ce598SEd Tanous                     return;
1812b7981f6SKowalski, Kamil                 }
18208bdcc71SSunitha Harish #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
18308bdcc71SSunitha Harish                 if (oemObject)
18408bdcc71SSunitha Harish                 {
18508bdcc71SSunitha Harish                     std::optional<nlohmann::json> bmcOem;
186faa34ccfSEd Tanous                     if (!json_util::readJson(*oemObject, asyncResp->res,
187faa34ccfSEd Tanous                                              "OpenBMC", bmcOem))
18808bdcc71SSunitha Harish                     {
18908bdcc71SSunitha Harish                         return;
19008bdcc71SSunitha Harish                     }
191faa34ccfSEd Tanous                     if (!json_util::readJson(*bmcOem, asyncResp->res,
192faa34ccfSEd Tanous                                              "ClientID", clientId))
19308bdcc71SSunitha Harish                     {
19408bdcc71SSunitha Harish                         BMCWEB_LOG_ERROR << "Could not read ClientId";
19508bdcc71SSunitha Harish                         return;
19608bdcc71SSunitha Harish                     }
19708bdcc71SSunitha Harish                 }
19808bdcc71SSunitha Harish #endif
1996f115bbbSManojkiran Eda 
200820ce598SEd Tanous                 // User is authenticated - create session
20152cc112dSEd Tanous                 std::shared_ptr<persistent_data::UserSession> session =
202faa34ccfSEd Tanous                     persistent_data::SessionStore::getInstance()
203faa34ccfSEd Tanous                         .generateUserSession(
204*41d61c82SJiaqing Zhao                             username, req.ipAddress, clientId,
205faa34ccfSEd Tanous                             persistent_data::PersistenceType::TIMEOUT,
206faa34ccfSEd Tanous                             isConfigureSelfOnly);
2078d1b46d7Szhanghch05                 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
208faa34ccfSEd Tanous                 asyncResp->res.addHeader(
209faa34ccfSEd Tanous                     "Location",
210faa34ccfSEd Tanous                     "/redfish/v1/SessionService/Sessions/" + session->uniqueId);
2118d1b46d7Szhanghch05                 asyncResp->res.result(boost::beast::http::status::created);
2123bf4e632SJoseph Reynolds                 if (session->isConfigureSelfOnly)
2133bf4e632SJoseph Reynolds                 {
2143bf4e632SJoseph Reynolds                     messages::passwordChangeRequired(
215faa34ccfSEd Tanous                         asyncResp->res, "/redfish/v1/AccountService/Accounts/" +
216faa34ccfSEd Tanous                                             session->username);
2172b7981f6SKowalski, Kamil                 }
2182b7981f6SKowalski, Kamil 
219faa34ccfSEd Tanous                 fillSessionObject(asyncResp->res, *session);
220faa34ccfSEd Tanous             });
2212b7981f6SKowalski, Kamil 
222faa34ccfSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
223ed398213SEd Tanous         .privileges(redfish::privileges::getSessionService)
224faa34ccfSEd Tanous         .methods(boost::beast::http::verb::get)(
225faa34ccfSEd Tanous             [](const crow::Request& /* req */,
226faa34ccfSEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
2278d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
2288d1b46d7Szhanghch05                     "#SessionService.v1_0_2.SessionService";
229faa34ccfSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
230faa34ccfSEd Tanous                     "/redfish/v1/SessionService/";
2318d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Session Service";
2328d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Id"] = "SessionService";
2338d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Description"] = "Session Service";
2348d1b46d7Szhanghch05                 asyncResp->res.jsonValue["SessionTimeout"] =
235faa34ccfSEd Tanous                     persistent_data::SessionStore::getInstance()
236faa34ccfSEd Tanous                         .getTimeoutInSeconds();
2378d1b46d7Szhanghch05                 asyncResp->res.jsonValue["ServiceEnabled"] = true;
2380f74e643SEd Tanous 
2398d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Sessions"] = {
2400f261533SEd Tanous                     {"@odata.id", "/redfish/v1/SessionService/Sessions"}};
241faa34ccfSEd Tanous             });
242f2a4a606SManojkiran Eda 
243faa34ccfSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
244ed398213SEd Tanous         .privileges(redfish::privileges::patchSessionService)
245faa34ccfSEd Tanous         .methods(boost::beast::http::verb::patch)(
246faa34ccfSEd Tanous             [](const crow::Request& req,
247faa34ccfSEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) -> void {
248f2a4a606SManojkiran Eda                 std::optional<int64_t> sessionTimeout;
2498d1b46d7Szhanghch05                 if (!json_util::readJson(req, asyncResp->res, "SessionTimeout",
2508d1b46d7Szhanghch05                                          sessionTimeout))
251f2a4a606SManojkiran Eda                 {
252f2a4a606SManojkiran Eda                     return;
253f2a4a606SManojkiran Eda                 }
254f2a4a606SManojkiran Eda 
255f2a4a606SManojkiran Eda                 if (sessionTimeout)
256f2a4a606SManojkiran Eda                 {
257faa34ccfSEd Tanous                     // The mininum & maximum allowed values for session timeout
258faa34ccfSEd Tanous                     // are 30 seconds and 86400 seconds respectively as per the
259faa34ccfSEd Tanous                     // session service schema mentioned at
260f2a4a606SManojkiran Eda                     // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
261f2a4a606SManojkiran Eda 
262f2a4a606SManojkiran Eda                     if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
263f2a4a606SManojkiran Eda                     {
264faa34ccfSEd Tanous                         std::chrono::seconds sessionTimeoutInseconds(
265faa34ccfSEd Tanous                             *sessionTimeout);
266f2a4a606SManojkiran Eda                         persistent_data::SessionStore::getInstance()
267f2a4a606SManojkiran Eda                             .updateSessionTimeout(sessionTimeoutInseconds);
268f2a4a606SManojkiran Eda                         messages::propertyValueModified(
269f2a4a606SManojkiran Eda                             asyncResp->res, "SessionTimeOut",
270f2a4a606SManojkiran Eda                             std::to_string(*sessionTimeout));
271f2a4a606SManojkiran Eda                     }
272f2a4a606SManojkiran Eda                     else
273f2a4a606SManojkiran Eda                     {
274f2a4a606SManojkiran Eda                         messages::propertyValueNotInList(
2758d1b46d7Szhanghch05                             asyncResp->res, std::to_string(*sessionTimeout),
2768d1b46d7Szhanghch05                             "SessionTimeOut");
277f2a4a606SManojkiran Eda                     }
278f2a4a606SManojkiran Eda                 }
279faa34ccfSEd Tanous             });
280f2a4a606SManojkiran Eda }
2815d27b854SBorawski.Lukasz 
2822b7981f6SKowalski, Kamil } // namespace redfish
283