xref: /openbmc/bmcweb/features/redfish/lib/redfish_sessions.hpp (revision 1476687deb1697d865b20458a0097c9ab5fd44e2)
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>
22ace85d60SEd Tanous #include <http/utility.hpp>
2345ca1b86SEd Tanous #include <query.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
257e860f15SJohn Edward Broadbent 
261abe55efSEd Tanous namespace redfish
271abe55efSEd Tanous {
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 
47724340d7SEd Tanous inline void
4845ca1b86SEd Tanous     handleSessionGet(crow::App& app, const crow::Request& req,
49faa34ccfSEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
50724340d7SEd Tanous                      const std::string& sessionId)
51724340d7SEd Tanous {
5245ca1b86SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
5345ca1b86SEd Tanous     {
5445ca1b86SEd Tanous         return;
5545ca1b86SEd Tanous     }
56faa34ccfSEd Tanous     // Note that control also reaches here via doPost and doDelete.
57724340d7SEd Tanous     auto session =
58724340d7SEd Tanous         persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
592b7981f6SKowalski, Kamil 
601abe55efSEd Tanous     if (session == nullptr)
611abe55efSEd Tanous     {
62724340d7SEd Tanous         messages::resourceNotFound(asyncResp->res, "Session", sessionId);
63faa34ccfSEd Tanous         return;
64faa34ccfSEd Tanous     }
65faa34ccfSEd Tanous 
66faa34ccfSEd Tanous     fillSessionObject(asyncResp->res, *session);
67724340d7SEd Tanous }
68faa34ccfSEd Tanous 
69724340d7SEd Tanous inline void
7045ca1b86SEd Tanous     handleSessionDelete(crow::App& app, const crow::Request& req,
71faa34ccfSEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
72724340d7SEd Tanous                         const std::string& sessionId)
73724340d7SEd Tanous {
7445ca1b86SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
7545ca1b86SEd Tanous     {
7645ca1b86SEd Tanous         return;
7745ca1b86SEd Tanous     }
78724340d7SEd Tanous     auto session =
79724340d7SEd Tanous         persistent_data::SessionStore::getInstance().getSessionByUid(sessionId);
80faa34ccfSEd Tanous 
81faa34ccfSEd Tanous     if (session == nullptr)
82faa34ccfSEd Tanous     {
83724340d7SEd Tanous         messages::resourceNotFound(asyncResp->res, "Session", sessionId);
842b7981f6SKowalski, Kamil         return;
852b7981f6SKowalski, Kamil     }
862b7981f6SKowalski, Kamil 
87900f9497SJoseph Reynolds     // Perform a proper ConfigureSelf authority check.  If a
88900f9497SJoseph Reynolds     // session is being used to DELETE some other user's session,
89900f9497SJoseph Reynolds     // then the ConfigureSelf privilege does not apply.  In that
90900f9497SJoseph Reynolds     // case, perform the authority check again without the user's
91900f9497SJoseph Reynolds     // ConfigureSelf privilege.
92900f9497SJoseph Reynolds     if (session->username != req.session->username)
93900f9497SJoseph Reynolds     {
946c51eab1SEd Tanous         Privileges effectiveUserPrivileges =
956c51eab1SEd Tanous             redfish::getUserPrivileges(req.userRole);
966c51eab1SEd Tanous 
97724340d7SEd Tanous         if (!effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"}))
98900f9497SJoseph Reynolds         {
998d1b46d7Szhanghch05             messages::insufficientPrivilege(asyncResp->res);
100900f9497SJoseph Reynolds             return;
101900f9497SJoseph Reynolds         }
102900f9497SJoseph Reynolds     }
103900f9497SJoseph Reynolds 
104724340d7SEd Tanous     persistent_data::SessionStore::getInstance().removeSession(session);
1055cc148afSEd Tanous     messages::success(asyncResp->res);
106724340d7SEd Tanous }
107f4c4dcf4SKowalski, Kamil 
108724340d7SEd Tanous inline nlohmann::json getSessionCollectionMembers()
109724340d7SEd Tanous {
11055c7b7a2SEd Tanous     std::vector<const std::string*> sessionIds =
11152cc112dSEd Tanous         persistent_data::SessionStore::getInstance().getUniqueIds(
11252cc112dSEd Tanous             false, persistent_data::PersistenceType::TIMEOUT);
113724340d7SEd Tanous     nlohmann::json ret = nlohmann::json::array();
1141abe55efSEd Tanous     for (const std::string* uid : sessionIds)
1151abe55efSEd Tanous     {
116*1476687dSEd Tanous         nlohmann::json::object_t session;
117*1476687dSEd Tanous         session["@odata.id"] = "/redfish/v1/SessionService/Sessions/" + *uid;
118*1476687dSEd Tanous         ret.push_back(std::move(session));
1192b7981f6SKowalski, Kamil     }
120724340d7SEd Tanous     return ret;
121724340d7SEd Tanous }
122724340d7SEd Tanous 
123724340d7SEd Tanous inline void handleSessionCollectionGet(
12445ca1b86SEd Tanous     crow::App& app, const crow::Request& req,
125724340d7SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
126724340d7SEd Tanous {
12745ca1b86SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
12845ca1b86SEd Tanous     {
12945ca1b86SEd Tanous         return;
13045ca1b86SEd Tanous     }
131724340d7SEd Tanous     asyncResp->res.jsonValue["Members"] = getSessionCollectionMembers();
132faa34ccfSEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] =
133724340d7SEd Tanous         asyncResp->res.jsonValue["Members"].size();
1348d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
1358d1b46d7Szhanghch05         "#SessionCollection.SessionCollection";
1368d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.id"] =
1378d1b46d7Szhanghch05         "/redfish/v1/SessionService/Sessions/";
1388d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Session Collection";
1398d1b46d7Szhanghch05     asyncResp->res.jsonValue["Description"] = "Session Collection";
140724340d7SEd Tanous }
1412b7981f6SKowalski, Kamil 
142724340d7SEd Tanous inline void handleSessionCollectionMembersGet(
14345ca1b86SEd Tanous     crow::App& app, const crow::Request& req,
144724340d7SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
145724340d7SEd Tanous {
14645ca1b86SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
14745ca1b86SEd Tanous     {
14845ca1b86SEd Tanous         return;
14945ca1b86SEd Tanous     }
150724340d7SEd Tanous     asyncResp->res.jsonValue = getSessionCollectionMembers();
151724340d7SEd Tanous }
152724340d7SEd Tanous 
153724340d7SEd Tanous void handleSessionCollectionPost(
15445ca1b86SEd Tanous     crow::App& app, const crow::Request& req,
155724340d7SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
156724340d7SEd Tanous {
15745ca1b86SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
15845ca1b86SEd Tanous     {
15945ca1b86SEd Tanous         return;
16045ca1b86SEd Tanous     }
1619712f8acSEd Tanous     std::string username;
1629712f8acSEd Tanous     std::string password;
16308bdcc71SSunitha Harish     std::optional<nlohmann::json> oemObject;
16408bdcc71SSunitha Harish     std::string clientId;
165724340d7SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username,
166724340d7SEd Tanous                                   "Password", password, "Oem", oemObject))
1671abe55efSEd Tanous     {
1682b7981f6SKowalski, Kamil         return;
1692b7981f6SKowalski, Kamil     }
1702b7981f6SKowalski, Kamil 
171820ce598SEd Tanous     if (password.empty() || username.empty() ||
1728d1b46d7Szhanghch05         asyncResp->res.result() != boost::beast::http::status::ok)
1731abe55efSEd Tanous     {
1741abe55efSEd Tanous         if (username.empty())
1751abe55efSEd Tanous         {
1768d1b46d7Szhanghch05             messages::propertyMissing(asyncResp->res, "UserName");
177f4c4dcf4SKowalski, Kamil         }
178f4c4dcf4SKowalski, Kamil 
1791abe55efSEd Tanous         if (password.empty())
1801abe55efSEd Tanous         {
1818d1b46d7Szhanghch05             messages::propertyMissing(asyncResp->res, "Password");
182820ce598SEd Tanous         }
183820ce598SEd Tanous 
184820ce598SEd Tanous         return;
185f4c4dcf4SKowalski, Kamil     }
1862b7981f6SKowalski, Kamil 
1873bf4e632SJoseph Reynolds     int pamrc = pamAuthenticateUser(username, password);
1883bf4e632SJoseph Reynolds     bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD;
1893bf4e632SJoseph Reynolds     if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly)
1901abe55efSEd Tanous     {
191724340d7SEd Tanous         messages::resourceAtUriUnauthorized(asyncResp->res, req.urlView,
192f12894f8SJason M. Bills                                             "Invalid username or password");
193820ce598SEd Tanous         return;
1942b7981f6SKowalski, Kamil     }
19508bdcc71SSunitha Harish #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
19608bdcc71SSunitha Harish     if (oemObject)
19708bdcc71SSunitha Harish     {
19808bdcc71SSunitha Harish         std::optional<nlohmann::json> bmcOem;
199724340d7SEd Tanous         if (!json_util::readJson(*oemObject, asyncResp->res, "OpenBMC", bmcOem))
20008bdcc71SSunitha Harish         {
20108bdcc71SSunitha Harish             return;
20208bdcc71SSunitha Harish         }
203724340d7SEd Tanous         if (!json_util::readJson(*bmcOem, asyncResp->res, "ClientID", clientId))
20408bdcc71SSunitha Harish         {
20508bdcc71SSunitha Harish             BMCWEB_LOG_ERROR << "Could not read ClientId";
20608bdcc71SSunitha Harish             return;
20708bdcc71SSunitha Harish         }
20808bdcc71SSunitha Harish     }
20908bdcc71SSunitha Harish #endif
2106f115bbbSManojkiran Eda 
211820ce598SEd Tanous     // User is authenticated - create session
21252cc112dSEd Tanous     std::shared_ptr<persistent_data::UserSession> session =
213724340d7SEd Tanous         persistent_data::SessionStore::getInstance().generateUserSession(
21441d61c82SJiaqing Zhao             username, req.ipAddress, clientId,
215724340d7SEd Tanous             persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly);
2168d1b46d7Szhanghch05     asyncResp->res.addHeader("X-Auth-Token", session->sessionToken);
217faa34ccfSEd Tanous     asyncResp->res.addHeader(
218724340d7SEd Tanous         "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId);
2198d1b46d7Szhanghch05     asyncResp->res.result(boost::beast::http::status::created);
2203bf4e632SJoseph Reynolds     if (session->isConfigureSelfOnly)
2213bf4e632SJoseph Reynolds     {
2223bf4e632SJoseph Reynolds         messages::passwordChangeRequired(
223724340d7SEd Tanous             asyncResp->res,
224724340d7SEd Tanous             crow::utility::urlFromPieces("redfish", "v1", "AccountService",
225ace85d60SEd Tanous                                          "Accounts", req.session->username));
2262b7981f6SKowalski, Kamil     }
2272b7981f6SKowalski, Kamil 
228faa34ccfSEd Tanous     fillSessionObject(asyncResp->res, *session);
229724340d7SEd Tanous }
230724340d7SEd Tanous inline void
23145ca1b86SEd Tanous     handleSessionServiceGet(crow::App& app, const crow::Request& req,
232724340d7SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2332b7981f6SKowalski, Kamil 
234724340d7SEd Tanous {
23545ca1b86SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
23645ca1b86SEd Tanous     {
23745ca1b86SEd Tanous         return;
23845ca1b86SEd Tanous     }
2398d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
2408d1b46d7Szhanghch05         "#SessionService.v1_0_2.SessionService";
241724340d7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/";
2428d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Session Service";
2438d1b46d7Szhanghch05     asyncResp->res.jsonValue["Id"] = "SessionService";
2448d1b46d7Szhanghch05     asyncResp->res.jsonValue["Description"] = "Session Service";
2458d1b46d7Szhanghch05     asyncResp->res.jsonValue["SessionTimeout"] =
246724340d7SEd Tanous         persistent_data::SessionStore::getInstance().getTimeoutInSeconds();
2478d1b46d7Szhanghch05     asyncResp->res.jsonValue["ServiceEnabled"] = true;
2480f74e643SEd Tanous 
249*1476687dSEd Tanous     asyncResp->res.jsonValue["Sessions"]["@odata.id"] =
250*1476687dSEd Tanous         "/redfish/v1/SessionService/Sessions";
251724340d7SEd Tanous }
252f2a4a606SManojkiran Eda 
253724340d7SEd Tanous inline void handleSessionServicePatch(
25445ca1b86SEd Tanous     crow::App& app, const crow::Request& req,
255724340d7SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
256724340d7SEd Tanous {
25745ca1b86SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
25845ca1b86SEd Tanous     {
25945ca1b86SEd Tanous         return;
26045ca1b86SEd Tanous     }
261f2a4a606SManojkiran Eda     std::optional<int64_t> sessionTimeout;
262724340d7SEd Tanous     if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout",
263724340d7SEd Tanous                                   sessionTimeout))
264f2a4a606SManojkiran Eda     {
265f2a4a606SManojkiran Eda         return;
266f2a4a606SManojkiran Eda     }
267f2a4a606SManojkiran Eda 
268f2a4a606SManojkiran Eda     if (sessionTimeout)
269f2a4a606SManojkiran Eda     {
270faa34ccfSEd Tanous         // The mininum & maximum allowed values for session timeout
271faa34ccfSEd Tanous         // are 30 seconds and 86400 seconds respectively as per the
272faa34ccfSEd Tanous         // session service schema mentioned at
273f2a4a606SManojkiran Eda         // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json
274f2a4a606SManojkiran Eda 
275f2a4a606SManojkiran Eda         if (*sessionTimeout <= 86400 && *sessionTimeout >= 30)
276f2a4a606SManojkiran Eda         {
277724340d7SEd Tanous             std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout);
278724340d7SEd Tanous             persistent_data::SessionStore::getInstance().updateSessionTimeout(
279724340d7SEd Tanous                 sessionTimeoutInseconds);
280724340d7SEd Tanous             messages::propertyValueModified(asyncResp->res, "SessionTimeOut",
281f2a4a606SManojkiran Eda                                             std::to_string(*sessionTimeout));
282f2a4a606SManojkiran Eda         }
283f2a4a606SManojkiran Eda         else
284f2a4a606SManojkiran Eda         {
285724340d7SEd Tanous             messages::propertyValueNotInList(asyncResp->res,
286724340d7SEd Tanous                                              std::to_string(*sessionTimeout),
2878d1b46d7Szhanghch05                                              "SessionTimeOut");
288f2a4a606SManojkiran Eda         }
289f2a4a606SManojkiran Eda     }
290724340d7SEd Tanous }
291724340d7SEd Tanous 
292724340d7SEd Tanous inline void requestRoutesSession(App& app)
293724340d7SEd Tanous {
294724340d7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
295724340d7SEd Tanous         .privileges(redfish::privileges::getSession)
29645ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)(
29745ca1b86SEd Tanous             std::bind_front(handleSessionGet, std::ref(app)));
298724340d7SEd Tanous 
299724340d7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/")
300724340d7SEd Tanous         .privileges(redfish::privileges::deleteSession)
30145ca1b86SEd Tanous         .methods(boost::beast::http::verb::delete_)(
30245ca1b86SEd Tanous             std::bind_front(handleSessionDelete, std::ref(app)));
303724340d7SEd Tanous 
304724340d7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
305724340d7SEd Tanous         .privileges(redfish::privileges::getSessionCollection)
30645ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)(
30745ca1b86SEd Tanous             std::bind_front(handleSessionCollectionGet, std::ref(app)));
308724340d7SEd Tanous 
309e76cd868SEd Tanous     // Note, the next two routes technically don't match the privilege
310724340d7SEd Tanous     // registry given the way login mechanisms work.  The base privilege
311724340d7SEd Tanous     // registry lists this endpoint as requiring login privilege, but because
312724340d7SEd Tanous     // this is the endpoint responsible for giving the login privilege, and it
313724340d7SEd Tanous     // is itself its own route, it needs to not require Login
314724340d7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/")
315724340d7SEd Tanous         .privileges({})
31645ca1b86SEd Tanous         .methods(boost::beast::http::verb::post)(
31745ca1b86SEd Tanous             std::bind_front(handleSessionCollectionPost, std::ref(app)));
318724340d7SEd Tanous 
319e76cd868SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/")
320e76cd868SEd Tanous         .privileges({})
32145ca1b86SEd Tanous         .methods(boost::beast::http::verb::post)(
32245ca1b86SEd Tanous             std::bind_front(handleSessionCollectionPost, std::ref(app)));
323e76cd868SEd Tanous 
324724340d7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
325724340d7SEd Tanous         .privileges(redfish::privileges::getSessionService)
32645ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)(
32745ca1b86SEd Tanous             std::bind_front(handleSessionServiceGet, std::ref(app)));
328724340d7SEd Tanous 
329724340d7SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/SessionService/")
330724340d7SEd Tanous         .privileges(redfish::privileges::patchSessionService)
33145ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
33245ca1b86SEd Tanous             std::bind_front(handleSessionServicePatch, std::ref(app)));
333f2a4a606SManojkiran Eda }
3345d27b854SBorawski.Lukasz 
3352b7981f6SKowalski, Kamil } // namespace redfish
336