blob: 52eb02f28d5aca90b9ae3ab5a50c2a2f860fd7e7 [file] [log] [blame]
Tom Joseph9662c3a2016-12-06 17:52:16 +05301#include "session_cmds.hpp"
2
Tom Joseph9662c3a2016-12-06 17:52:16 +05303#include "endian.hpp"
4#include "main.hpp"
Vernon Mauery9e801a22018-10-12 13:20:49 -07005
William A. Kennington III4f09eae2019-02-12 17:10:35 -08006#include <ipmid/api.h>
Tom Joseph9662c3a2016-12-06 17:52:16 +05307
8namespace command
9{
10
Vernon Mauery9e801a22018-10-12 13:20:49 -070011std::vector<uint8_t>
12 setSessionPrivilegeLevel(const std::vector<uint8_t>& inPayload,
13 const message::Handler& handler)
Tom Joseph9662c3a2016-12-06 17:52:16 +053014{
Tom Joseph9662c3a2016-12-06 17:52:16 +053015
16 std::vector<uint8_t> outPayload(sizeof(SetSessionPrivLevelResp));
Vernon Mauery9e801a22018-10-12 13:20:49 -070017 auto request =
18 reinterpret_cast<const SetSessionPrivLevelReq*>(inPayload.data());
19 auto response =
20 reinterpret_cast<SetSessionPrivLevelResp*>(outPayload.data());
Tom Joseph9662c3a2016-12-06 17:52:16 +053021 response->completionCode = IPMI_CC_OK;
22 uint8_t reqPrivilegeLevel = request->reqPrivLevel;
23
Vernon Maueryae1fda42018-10-15 12:55:34 -070024 auto session = std::get<session::Manager&>(singletonPool)
25 .getSession(handler.sessionID);
Tom Joseph9662c3a2016-12-06 17:52:16 +053026
27 if (reqPrivilegeLevel == 0) // Just return present privilege level
28 {
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053029 response->newPrivLevel = session->currentPrivilege();
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053030 return outPayload;
Tom Joseph9662c3a2016-12-06 17:52:16 +053031 }
Tom Joseph4021b1f2019-02-12 10:10:12 +053032 if (reqPrivilegeLevel > (static_cast<uint8_t>(session->reqMaxPrivLevel) &
33 session::reqMaxPrivMask))
Tom Joseph9662c3a2016-12-06 17:52:16 +053034 {
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053035 // Requested level exceeds Channel and/or User Privilege Limit
36 response->completionCode = IPMI_CC_EXCEEDS_USER_PRIV;
37 return outPayload;
38 }
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053039 // Use the minimum privilege of user or channel
40 uint8_t minPriv = 0;
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +053041 if (session->sessionChannelAccess.privLimit <
42 session->sessionUserPrivAccess.privilege)
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053043 {
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +053044 minPriv = session->sessionChannelAccess.privLimit;
Tom Joseph9662c3a2016-12-06 17:52:16 +053045 }
46 else
47 {
Richard Marian Thomaiyar992e53c2019-03-03 13:30:46 +053048 minPriv = session->sessionUserPrivAccess.privilege;
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053049 }
50 if (reqPrivilegeLevel > minPriv)
51 {
Tom Joseph9662c3a2016-12-06 17:52:16 +053052 // Requested level exceeds Channel and/or User Privilege Limit
53 response->completionCode = IPMI_CC_EXCEEDS_USER_PRIV;
54 }
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053055 else
56 {
57 // update current privilege of the session.
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053058 session->currentPrivilege(static_cast<uint8_t>(reqPrivilegeLevel));
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053059 response->newPrivLevel = reqPrivilegeLevel;
60 }
61
Tom Joseph9662c3a2016-12-06 17:52:16 +053062 return outPayload;
63}
64
Tom Joseph18a45e92017-04-11 11:30:44 +053065std::vector<uint8_t> closeSession(const std::vector<uint8_t>& inPayload,
Tom Joseph9662c3a2016-12-06 17:52:16 +053066 const message::Handler& handler)
67{
Tom Joseph9662c3a2016-12-06 17:52:16 +053068 std::vector<uint8_t> outPayload(sizeof(CloseSessionResponse));
Vernon Mauery9e801a22018-10-12 13:20:49 -070069 auto request =
70 reinterpret_cast<const CloseSessionRequest*>(inPayload.data());
Tom Joseph9662c3a2016-12-06 17:52:16 +053071 auto response = reinterpret_cast<CloseSessionResponse*>(outPayload.data());
Vernon Mauery9e801a22018-10-12 13:20:49 -070072 response->completionCode = IPMI_CC_OK;
Tom Joseph9662c3a2016-12-06 17:52:16 +053073
74 auto bmcSessionID = endian::from_ipmi(request->sessionID);
75
76 // Session 0 is needed to handle session setup, so session zero is never
77 // closed
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053078 if (bmcSessionID == session::sessionZero)
Tom Joseph9662c3a2016-12-06 17:52:16 +053079 {
80 response->completionCode = IPMI_CC_INVALID_SESSIONID;
81 }
82 else
83 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070084 auto status = std::get<session::Manager&>(singletonPool)
85 .stopSession(bmcSessionID);
86 if (!status)
Tom Joseph9662c3a2016-12-06 17:52:16 +053087 {
88 response->completionCode = IPMI_CC_INVALID_SESSIONID;
89 }
90 }
Tom Joseph9662c3a2016-12-06 17:52:16 +053091 return outPayload;
92}
93
94} // namespace command