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