Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 1 | #include "session_cmds.hpp" |
| 2 | |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 3 | #include "endian.hpp" |
| 4 | #include "main.hpp" |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 5 | |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 6 | #include <host-ipmid/ipmid-api.h> |
| 7 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | #include <iostream> |
| 9 | |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 10 | namespace command |
| 11 | { |
| 12 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 13 | std::vector<uint8_t> |
| 14 | setSessionPrivilegeLevel(const std::vector<uint8_t>& inPayload, |
| 15 | const message::Handler& handler) |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 16 | { |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 17 | |
| 18 | std::vector<uint8_t> outPayload(sizeof(SetSessionPrivLevelResp)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 19 | auto request = |
| 20 | reinterpret_cast<const SetSessionPrivLevelReq*>(inPayload.data()); |
| 21 | auto response = |
| 22 | reinterpret_cast<SetSessionPrivLevelResp*>(outPayload.data()); |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 23 | response->completionCode = IPMI_CC_OK; |
| 24 | uint8_t reqPrivilegeLevel = request->reqPrivLevel; |
| 25 | |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 26 | auto session = std::get<session::Manager&>(singletonPool) |
| 27 | .getSession(handler.sessionID); |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 28 | |
| 29 | if (reqPrivilegeLevel == 0) // Just return present privilege level |
| 30 | { |
| 31 | response->newPrivLevel = static_cast<uint8_t>(session->curPrivLevel); |
| 32 | } |
| 33 | else if (reqPrivilegeLevel <= static_cast<uint8_t>(session->maxPrivLevel)) |
| 34 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 35 | session->curPrivLevel = |
| 36 | static_cast<session::Privilege>(reqPrivilegeLevel); |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 37 | response->newPrivLevel = reqPrivilegeLevel; |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | // Requested level exceeds Channel and/or User Privilege Limit |
| 42 | response->completionCode = IPMI_CC_EXCEEDS_USER_PRIV; |
| 43 | } |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 44 | return outPayload; |
| 45 | } |
| 46 | |
Tom Joseph | 18a45e9 | 2017-04-11 11:30:44 +0530 | [diff] [blame] | 47 | std::vector<uint8_t> closeSession(const std::vector<uint8_t>& inPayload, |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 48 | const message::Handler& handler) |
| 49 | { |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 50 | std::vector<uint8_t> outPayload(sizeof(CloseSessionResponse)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 51 | auto request = |
| 52 | reinterpret_cast<const CloseSessionRequest*>(inPayload.data()); |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 53 | auto response = reinterpret_cast<CloseSessionResponse*>(outPayload.data()); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 54 | response->completionCode = IPMI_CC_OK; |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 55 | |
| 56 | auto bmcSessionID = endian::from_ipmi(request->sessionID); |
| 57 | |
| 58 | // Session 0 is needed to handle session setup, so session zero is never |
| 59 | // closed |
| 60 | if (bmcSessionID == session::SESSION_ZERO) |
| 61 | { |
| 62 | response->completionCode = IPMI_CC_INVALID_SESSIONID; |
| 63 | } |
| 64 | else |
| 65 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 66 | auto status = std::get<session::Manager&>(singletonPool) |
| 67 | .stopSession(bmcSessionID); |
| 68 | if (!status) |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 69 | { |
| 70 | response->completionCode = IPMI_CC_INVALID_SESSIONID; |
| 71 | } |
| 72 | } |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 73 | return outPayload; |
| 74 | } |
| 75 | |
| 76 | } // namespace command |