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 | |
William A. Kennington III | 4f09eae | 2019-02-12 17:10:35 -0800 | [diff] [blame^] | 6 | #include <ipmid/api.h> |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 7 | |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 8 | #include <user_channel/channel_layer.hpp> |
| 9 | #include <user_channel/user_layer.hpp> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 10 | |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 11 | namespace command |
| 12 | { |
| 13 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 14 | std::vector<uint8_t> |
| 15 | setSessionPrivilegeLevel(const std::vector<uint8_t>& inPayload, |
| 16 | const message::Handler& handler) |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 17 | { |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 18 | |
| 19 | std::vector<uint8_t> outPayload(sizeof(SetSessionPrivLevelResp)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 20 | auto request = |
| 21 | reinterpret_cast<const SetSessionPrivLevelReq*>(inPayload.data()); |
| 22 | auto response = |
| 23 | reinterpret_cast<SetSessionPrivLevelResp*>(outPayload.data()); |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 24 | response->completionCode = IPMI_CC_OK; |
| 25 | uint8_t reqPrivilegeLevel = request->reqPrivLevel; |
| 26 | |
Vernon Mauery | ae1fda4 | 2018-10-15 12:55:34 -0700 | [diff] [blame] | 27 | auto session = std::get<session::Manager&>(singletonPool) |
| 28 | .getSession(handler.sessionID); |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 29 | |
| 30 | if (reqPrivilegeLevel == 0) // Just return present privilege level |
| 31 | { |
| 32 | response->newPrivLevel = static_cast<uint8_t>(session->curPrivLevel); |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 33 | return outPayload; |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 34 | } |
Tom Joseph | 4021b1f | 2019-02-12 10:10:12 +0530 | [diff] [blame] | 35 | if (reqPrivilegeLevel > (static_cast<uint8_t>(session->reqMaxPrivLevel) & |
| 36 | session::reqMaxPrivMask)) |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 37 | { |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 38 | // Requested level exceeds Channel and/or User Privilege Limit |
| 39 | response->completionCode = IPMI_CC_EXCEEDS_USER_PRIV; |
| 40 | return outPayload; |
| 41 | } |
| 42 | |
| 43 | uint8_t userId = ipmi::ipmiUserGetUserId(session->userName); |
| 44 | if (userId == ipmi::invalidUserId) |
| 45 | { |
| 46 | response->completionCode = IPMI_CC_UNSPECIFIED_ERROR; |
| 47 | return outPayload; |
| 48 | } |
| 49 | ipmi::PrivAccess userAccess{}; |
| 50 | ipmi::ChannelAccess chAccess{}; |
| 51 | if ((ipmi::ipmiUserGetPrivilegeAccess(userId, session->chNum, userAccess) != |
| 52 | IPMI_CC_OK) || |
| 53 | (ipmi::getChannelAccessData(session->chNum, chAccess) != IPMI_CC_OK)) |
| 54 | { |
| 55 | response->completionCode = IPMI_CC_INVALID_PRIV_LEVEL; |
| 56 | return outPayload; |
| 57 | } |
| 58 | // Use the minimum privilege of user or channel |
| 59 | uint8_t minPriv = 0; |
| 60 | if (chAccess.privLimit < userAccess.privilege) |
| 61 | { |
| 62 | minPriv = chAccess.privLimit; |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 63 | } |
| 64 | else |
| 65 | { |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 66 | minPriv = userAccess.privilege; |
| 67 | } |
| 68 | if (reqPrivilegeLevel > minPriv) |
| 69 | { |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 70 | // Requested level exceeds Channel and/or User Privilege Limit |
| 71 | response->completionCode = IPMI_CC_EXCEEDS_USER_PRIV; |
| 72 | } |
Richard Marian Thomaiyar | 127748a | 2018-09-06 07:08:51 +0530 | [diff] [blame] | 73 | else |
| 74 | { |
| 75 | // update current privilege of the session. |
| 76 | session->curPrivLevel = |
| 77 | static_cast<session::Privilege>(reqPrivilegeLevel); |
| 78 | response->newPrivLevel = reqPrivilegeLevel; |
| 79 | } |
| 80 | |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 81 | return outPayload; |
| 82 | } |
| 83 | |
Tom Joseph | 18a45e9 | 2017-04-11 11:30:44 +0530 | [diff] [blame] | 84 | std::vector<uint8_t> closeSession(const std::vector<uint8_t>& inPayload, |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 85 | const message::Handler& handler) |
| 86 | { |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 87 | std::vector<uint8_t> outPayload(sizeof(CloseSessionResponse)); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 88 | auto request = |
| 89 | reinterpret_cast<const CloseSessionRequest*>(inPayload.data()); |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 90 | auto response = reinterpret_cast<CloseSessionResponse*>(outPayload.data()); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 91 | response->completionCode = IPMI_CC_OK; |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 92 | |
| 93 | auto bmcSessionID = endian::from_ipmi(request->sessionID); |
| 94 | |
| 95 | // Session 0 is needed to handle session setup, so session zero is never |
| 96 | // closed |
| 97 | if (bmcSessionID == session::SESSION_ZERO) |
| 98 | { |
| 99 | response->completionCode = IPMI_CC_INVALID_SESSIONID; |
| 100 | } |
| 101 | else |
| 102 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 103 | auto status = std::get<session::Manager&>(singletonPool) |
| 104 | .stopSession(bmcSessionID); |
| 105 | if (!status) |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 106 | { |
| 107 | response->completionCode = IPMI_CC_INVALID_SESSIONID; |
| 108 | } |
| 109 | } |
Tom Joseph | 9662c3a | 2016-12-06 17:52:16 +0530 | [diff] [blame] | 110 | return outPayload; |
| 111 | } |
| 112 | |
| 113 | } // namespace command |