blob: 3b9fa0ec633a9c9b2d380496cc4feef07c751ccf [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
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +05308#include <user_channel/channel_layer.hpp>
9#include <user_channel/user_layer.hpp>
Vernon Mauery9e801a22018-10-12 13:20:49 -070010
Tom Joseph9662c3a2016-12-06 17:52:16 +053011namespace command
12{
13
Vernon Mauery9e801a22018-10-12 13:20:49 -070014std::vector<uint8_t>
15 setSessionPrivilegeLevel(const std::vector<uint8_t>& inPayload,
16 const message::Handler& handler)
Tom Joseph9662c3a2016-12-06 17:52:16 +053017{
Tom Joseph9662c3a2016-12-06 17:52:16 +053018
19 std::vector<uint8_t> outPayload(sizeof(SetSessionPrivLevelResp));
Vernon Mauery9e801a22018-10-12 13:20:49 -070020 auto request =
21 reinterpret_cast<const SetSessionPrivLevelReq*>(inPayload.data());
22 auto response =
23 reinterpret_cast<SetSessionPrivLevelResp*>(outPayload.data());
Tom Joseph9662c3a2016-12-06 17:52:16 +053024 response->completionCode = IPMI_CC_OK;
25 uint8_t reqPrivilegeLevel = request->reqPrivLevel;
26
Vernon Maueryae1fda42018-10-15 12:55:34 -070027 auto session = std::get<session::Manager&>(singletonPool)
28 .getSession(handler.sessionID);
Tom Joseph9662c3a2016-12-06 17:52:16 +053029
30 if (reqPrivilegeLevel == 0) // Just return present privilege level
31 {
32 response->newPrivLevel = static_cast<uint8_t>(session->curPrivLevel);
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053033 return outPayload;
Tom Joseph9662c3a2016-12-06 17:52:16 +053034 }
Tom Joseph4021b1f2019-02-12 10:10:12 +053035 if (reqPrivilegeLevel > (static_cast<uint8_t>(session->reqMaxPrivLevel) &
36 session::reqMaxPrivMask))
Tom Joseph9662c3a2016-12-06 17:52:16 +053037 {
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053038 // 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 Joseph9662c3a2016-12-06 17:52:16 +053063 }
64 else
65 {
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053066 minPriv = userAccess.privilege;
67 }
68 if (reqPrivilegeLevel > minPriv)
69 {
Tom Joseph9662c3a2016-12-06 17:52:16 +053070 // Requested level exceeds Channel and/or User Privilege Limit
71 response->completionCode = IPMI_CC_EXCEEDS_USER_PRIV;
72 }
Richard Marian Thomaiyar127748a2018-09-06 07:08:51 +053073 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 Joseph9662c3a2016-12-06 17:52:16 +053081 return outPayload;
82}
83
Tom Joseph18a45e92017-04-11 11:30:44 +053084std::vector<uint8_t> closeSession(const std::vector<uint8_t>& inPayload,
Tom Joseph9662c3a2016-12-06 17:52:16 +053085 const message::Handler& handler)
86{
Tom Joseph9662c3a2016-12-06 17:52:16 +053087 std::vector<uint8_t> outPayload(sizeof(CloseSessionResponse));
Vernon Mauery9e801a22018-10-12 13:20:49 -070088 auto request =
89 reinterpret_cast<const CloseSessionRequest*>(inPayload.data());
Tom Joseph9662c3a2016-12-06 17:52:16 +053090 auto response = reinterpret_cast<CloseSessionResponse*>(outPayload.data());
Vernon Mauery9e801a22018-10-12 13:20:49 -070091 response->completionCode = IPMI_CC_OK;
Tom Joseph9662c3a2016-12-06 17:52:16 +053092
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 Mauery9e801a22018-10-12 13:20:49 -0700103 auto status = std::get<session::Manager&>(singletonPool)
104 .stopSession(bmcSessionID);
105 if (!status)
Tom Joseph9662c3a2016-12-06 17:52:16 +0530106 {
107 response->completionCode = IPMI_CC_INVALID_SESSIONID;
108 }
109 }
Tom Joseph9662c3a2016-12-06 17:52:16 +0530110 return outPayload;
111}
112
113} // namespace command