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. 920fd29865Swukaihua-fii-na if (req.session != nullptr && !session->username.empty() && 930fd29865Swukaihua-fii-na session->username != req.session->username) 94900f9497SJoseph Reynolds { 956c51eab1SEd Tanous Privileges effectiveUserPrivileges = 966c51eab1SEd Tanous redfish::getUserPrivileges(req.userRole); 976c51eab1SEd Tanous 98724340d7SEd Tanous if (!effectiveUserPrivileges.isSupersetOf({"ConfigureUsers"})) 99900f9497SJoseph Reynolds { 1008d1b46d7Szhanghch05 messages::insufficientPrivilege(asyncResp->res); 101900f9497SJoseph Reynolds return; 102900f9497SJoseph Reynolds } 103900f9497SJoseph Reynolds } 104900f9497SJoseph Reynolds 105724340d7SEd Tanous persistent_data::SessionStore::getInstance().removeSession(session); 1065cc148afSEd Tanous messages::success(asyncResp->res); 107724340d7SEd Tanous } 108f4c4dcf4SKowalski, Kamil 109724340d7SEd Tanous inline nlohmann::json getSessionCollectionMembers() 110724340d7SEd Tanous { 11155c7b7a2SEd Tanous std::vector<const std::string*> sessionIds = 11252cc112dSEd Tanous persistent_data::SessionStore::getInstance().getUniqueIds( 11352cc112dSEd Tanous false, persistent_data::PersistenceType::TIMEOUT); 114724340d7SEd Tanous nlohmann::json ret = nlohmann::json::array(); 1151abe55efSEd Tanous for (const std::string* uid : sessionIds) 1161abe55efSEd Tanous { 1171476687dSEd Tanous nlohmann::json::object_t session; 1181476687dSEd Tanous session["@odata.id"] = "/redfish/v1/SessionService/Sessions/" + *uid; 1191476687dSEd Tanous ret.push_back(std::move(session)); 1202b7981f6SKowalski, Kamil } 121724340d7SEd Tanous return ret; 122724340d7SEd Tanous } 123724340d7SEd Tanous 124724340d7SEd Tanous inline void handleSessionCollectionGet( 12545ca1b86SEd Tanous crow::App& app, const crow::Request& req, 126724340d7SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 127724340d7SEd Tanous { 12845ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 12945ca1b86SEd Tanous { 13045ca1b86SEd Tanous return; 13145ca1b86SEd Tanous } 132724340d7SEd Tanous asyncResp->res.jsonValue["Members"] = getSessionCollectionMembers(); 133faa34ccfSEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 134724340d7SEd Tanous asyncResp->res.jsonValue["Members"].size(); 1358d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 1368d1b46d7Szhanghch05 "#SessionCollection.SessionCollection"; 1378d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 1388d1b46d7Szhanghch05 "/redfish/v1/SessionService/Sessions/"; 1398d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Session Collection"; 1408d1b46d7Szhanghch05 asyncResp->res.jsonValue["Description"] = "Session Collection"; 141724340d7SEd Tanous } 1422b7981f6SKowalski, Kamil 143724340d7SEd Tanous inline void handleSessionCollectionMembersGet( 14445ca1b86SEd Tanous crow::App& app, const crow::Request& req, 145724340d7SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 146724340d7SEd Tanous { 14745ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 14845ca1b86SEd Tanous { 14945ca1b86SEd Tanous return; 15045ca1b86SEd Tanous } 151724340d7SEd Tanous asyncResp->res.jsonValue = getSessionCollectionMembers(); 152724340d7SEd Tanous } 153724340d7SEd Tanous 154*4ee8e211SEd Tanous inline void handleSessionCollectionPost( 15545ca1b86SEd Tanous crow::App& app, const crow::Request& req, 156724340d7SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 157724340d7SEd Tanous { 15845ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 15945ca1b86SEd Tanous { 16045ca1b86SEd Tanous return; 16145ca1b86SEd Tanous } 1629712f8acSEd Tanous std::string username; 1639712f8acSEd Tanous std::string password; 16408bdcc71SSunitha Harish std::optional<nlohmann::json> oemObject; 16508bdcc71SSunitha Harish std::string clientId; 166724340d7SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "UserName", username, 167724340d7SEd Tanous "Password", password, "Oem", oemObject)) 1681abe55efSEd Tanous { 1692b7981f6SKowalski, Kamil return; 1702b7981f6SKowalski, Kamil } 1712b7981f6SKowalski, Kamil 172820ce598SEd Tanous if (password.empty() || username.empty() || 1738d1b46d7Szhanghch05 asyncResp->res.result() != boost::beast::http::status::ok) 1741abe55efSEd Tanous { 1751abe55efSEd Tanous if (username.empty()) 1761abe55efSEd Tanous { 1778d1b46d7Szhanghch05 messages::propertyMissing(asyncResp->res, "UserName"); 178f4c4dcf4SKowalski, Kamil } 179f4c4dcf4SKowalski, Kamil 1801abe55efSEd Tanous if (password.empty()) 1811abe55efSEd Tanous { 1828d1b46d7Szhanghch05 messages::propertyMissing(asyncResp->res, "Password"); 183820ce598SEd Tanous } 184820ce598SEd Tanous 185820ce598SEd Tanous return; 186f4c4dcf4SKowalski, Kamil } 1872b7981f6SKowalski, Kamil 1883bf4e632SJoseph Reynolds int pamrc = pamAuthenticateUser(username, password); 1893bf4e632SJoseph Reynolds bool isConfigureSelfOnly = pamrc == PAM_NEW_AUTHTOK_REQD; 1903bf4e632SJoseph Reynolds if ((pamrc != PAM_SUCCESS) && !isConfigureSelfOnly) 1911abe55efSEd Tanous { 192724340d7SEd Tanous messages::resourceAtUriUnauthorized(asyncResp->res, req.urlView, 193f12894f8SJason M. Bills "Invalid username or password"); 194820ce598SEd Tanous return; 1952b7981f6SKowalski, Kamil } 19608bdcc71SSunitha Harish #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE 19708bdcc71SSunitha Harish if (oemObject) 19808bdcc71SSunitha Harish { 19908bdcc71SSunitha Harish std::optional<nlohmann::json> bmcOem; 200724340d7SEd Tanous if (!json_util::readJson(*oemObject, asyncResp->res, "OpenBMC", bmcOem)) 20108bdcc71SSunitha Harish { 20208bdcc71SSunitha Harish return; 20308bdcc71SSunitha Harish } 204724340d7SEd Tanous if (!json_util::readJson(*bmcOem, asyncResp->res, "ClientID", clientId)) 20508bdcc71SSunitha Harish { 20608bdcc71SSunitha Harish BMCWEB_LOG_ERROR << "Could not read ClientId"; 20708bdcc71SSunitha Harish return; 20808bdcc71SSunitha Harish } 20908bdcc71SSunitha Harish } 21008bdcc71SSunitha Harish #endif 2116f115bbbSManojkiran Eda 212820ce598SEd Tanous // User is authenticated - create session 21352cc112dSEd Tanous std::shared_ptr<persistent_data::UserSession> session = 214724340d7SEd Tanous persistent_data::SessionStore::getInstance().generateUserSession( 21541d61c82SJiaqing Zhao username, req.ipAddress, clientId, 216724340d7SEd Tanous persistent_data::PersistenceType::TIMEOUT, isConfigureSelfOnly); 2178d1b46d7Szhanghch05 asyncResp->res.addHeader("X-Auth-Token", session->sessionToken); 218faa34ccfSEd Tanous asyncResp->res.addHeader( 219724340d7SEd Tanous "Location", "/redfish/v1/SessionService/Sessions/" + session->uniqueId); 2208d1b46d7Szhanghch05 asyncResp->res.result(boost::beast::http::status::created); 2213bf4e632SJoseph Reynolds if (session->isConfigureSelfOnly) 2223bf4e632SJoseph Reynolds { 2233bf4e632SJoseph Reynolds messages::passwordChangeRequired( 224724340d7SEd Tanous asyncResp->res, 225724340d7SEd Tanous crow::utility::urlFromPieces("redfish", "v1", "AccountService", 226ace85d60SEd Tanous "Accounts", req.session->username)); 2272b7981f6SKowalski, Kamil } 2282b7981f6SKowalski, Kamil 229faa34ccfSEd Tanous fillSessionObject(asyncResp->res, *session); 230724340d7SEd Tanous } 231724340d7SEd Tanous inline void 23245ca1b86SEd Tanous handleSessionServiceGet(crow::App& app, const crow::Request& req, 233724340d7SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 2342b7981f6SKowalski, Kamil 235724340d7SEd Tanous { 23645ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 23745ca1b86SEd Tanous { 23845ca1b86SEd Tanous return; 23945ca1b86SEd Tanous } 2408d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 2418d1b46d7Szhanghch05 "#SessionService.v1_0_2.SessionService"; 242724340d7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/SessionService/"; 2438d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Session Service"; 2448d1b46d7Szhanghch05 asyncResp->res.jsonValue["Id"] = "SessionService"; 2458d1b46d7Szhanghch05 asyncResp->res.jsonValue["Description"] = "Session Service"; 2468d1b46d7Szhanghch05 asyncResp->res.jsonValue["SessionTimeout"] = 247724340d7SEd Tanous persistent_data::SessionStore::getInstance().getTimeoutInSeconds(); 2488d1b46d7Szhanghch05 asyncResp->res.jsonValue["ServiceEnabled"] = true; 2490f74e643SEd Tanous 2501476687dSEd Tanous asyncResp->res.jsonValue["Sessions"]["@odata.id"] = 2511476687dSEd Tanous "/redfish/v1/SessionService/Sessions"; 252724340d7SEd Tanous } 253f2a4a606SManojkiran Eda 254724340d7SEd Tanous inline void handleSessionServicePatch( 25545ca1b86SEd Tanous crow::App& app, const crow::Request& req, 256724340d7SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 257724340d7SEd Tanous { 25845ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 25945ca1b86SEd Tanous { 26045ca1b86SEd Tanous return; 26145ca1b86SEd Tanous } 262f2a4a606SManojkiran Eda std::optional<int64_t> sessionTimeout; 263724340d7SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "SessionTimeout", 264724340d7SEd Tanous sessionTimeout)) 265f2a4a606SManojkiran Eda { 266f2a4a606SManojkiran Eda return; 267f2a4a606SManojkiran Eda } 268f2a4a606SManojkiran Eda 269f2a4a606SManojkiran Eda if (sessionTimeout) 270f2a4a606SManojkiran Eda { 271faa34ccfSEd Tanous // The mininum & maximum allowed values for session timeout 272faa34ccfSEd Tanous // are 30 seconds and 86400 seconds respectively as per the 273faa34ccfSEd Tanous // session service schema mentioned at 274f2a4a606SManojkiran Eda // https://redfish.dmtf.org/schemas/v1/SessionService.v1_1_7.json 275f2a4a606SManojkiran Eda 276f2a4a606SManojkiran Eda if (*sessionTimeout <= 86400 && *sessionTimeout >= 30) 277f2a4a606SManojkiran Eda { 278724340d7SEd Tanous std::chrono::seconds sessionTimeoutInseconds(*sessionTimeout); 279724340d7SEd Tanous persistent_data::SessionStore::getInstance().updateSessionTimeout( 280724340d7SEd Tanous sessionTimeoutInseconds); 281724340d7SEd Tanous messages::propertyValueModified(asyncResp->res, "SessionTimeOut", 282f2a4a606SManojkiran Eda std::to_string(*sessionTimeout)); 283f2a4a606SManojkiran Eda } 284f2a4a606SManojkiran Eda else 285f2a4a606SManojkiran Eda { 286724340d7SEd Tanous messages::propertyValueNotInList(asyncResp->res, 287724340d7SEd Tanous std::to_string(*sessionTimeout), 2888d1b46d7Szhanghch05 "SessionTimeOut"); 289f2a4a606SManojkiran Eda } 290f2a4a606SManojkiran Eda } 291724340d7SEd Tanous } 292724340d7SEd Tanous 293724340d7SEd Tanous inline void requestRoutesSession(App& app) 294724340d7SEd Tanous { 295724340d7SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/") 296724340d7SEd Tanous .privileges(redfish::privileges::getSession) 29745ca1b86SEd Tanous .methods(boost::beast::http::verb::get)( 29845ca1b86SEd Tanous std::bind_front(handleSessionGet, std::ref(app))); 299724340d7SEd Tanous 300724340d7SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/<str>/") 301724340d7SEd Tanous .privileges(redfish::privileges::deleteSession) 30245ca1b86SEd Tanous .methods(boost::beast::http::verb::delete_)( 30345ca1b86SEd Tanous std::bind_front(handleSessionDelete, std::ref(app))); 304724340d7SEd Tanous 305724340d7SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/") 306724340d7SEd Tanous .privileges(redfish::privileges::getSessionCollection) 30745ca1b86SEd Tanous .methods(boost::beast::http::verb::get)( 30845ca1b86SEd Tanous std::bind_front(handleSessionCollectionGet, std::ref(app))); 309724340d7SEd Tanous 310e76cd868SEd Tanous // Note, the next two routes technically don't match the privilege 311724340d7SEd Tanous // registry given the way login mechanisms work. The base privilege 312724340d7SEd Tanous // registry lists this endpoint as requiring login privilege, but because 313724340d7SEd Tanous // this is the endpoint responsible for giving the login privilege, and it 314724340d7SEd Tanous // is itself its own route, it needs to not require Login 315724340d7SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/") 316724340d7SEd Tanous .privileges({}) 31745ca1b86SEd Tanous .methods(boost::beast::http::verb::post)( 31845ca1b86SEd Tanous std::bind_front(handleSessionCollectionPost, std::ref(app))); 319724340d7SEd Tanous 320e76cd868SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/SessionService/Sessions/Members/") 321e76cd868SEd Tanous .privileges({}) 32245ca1b86SEd Tanous .methods(boost::beast::http::verb::post)( 32345ca1b86SEd Tanous std::bind_front(handleSessionCollectionPost, std::ref(app))); 324e76cd868SEd Tanous 325724340d7SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/SessionService/") 326724340d7SEd Tanous .privileges(redfish::privileges::getSessionService) 32745ca1b86SEd Tanous .methods(boost::beast::http::verb::get)( 32845ca1b86SEd Tanous std::bind_front(handleSessionServiceGet, std::ref(app))); 329724340d7SEd Tanous 330724340d7SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/SessionService/") 331724340d7SEd Tanous .privileges(redfish::privileges::patchSessionService) 33245ca1b86SEd Tanous .methods(boost::beast::http::verb::patch)( 33345ca1b86SEd Tanous std::bind_front(handleSessionServicePatch, std::ref(app))); 334f2a4a606SManojkiran Eda } 3355d27b854SBorawski.Lukasz 3362b7981f6SKowalski, Kamil } // namespace redfish 337