1 package org.metricshub.ipmi.core.coding.commands.session;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import org.metricshub.ipmi.core.coding.commands.CommandCodes;
26 import org.metricshub.ipmi.core.coding.commands.IpmiCommandCoder;
27 import org.metricshub.ipmi.core.coding.commands.IpmiVersion;
28 import org.metricshub.ipmi.core.coding.commands.PrivilegeLevel;
29 import org.metricshub.ipmi.core.coding.commands.ResponseData;
30 import org.metricshub.ipmi.core.coding.payload.CompletionCode;
31 import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
32 import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
33 import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
34 import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanResponse;
35 import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
36 import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
37 import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
38 import org.metricshub.ipmi.core.coding.security.CipherSuite;
39 import org.metricshub.ipmi.core.common.TypeConverter;
40
41 import java.security.InvalidKeyException;
42 import java.security.NoSuchAlgorithmException;
43
44
45
46
47 public class SetSessionPrivilegeLevel extends IpmiCommandCoder {
48
49 private PrivilegeLevel privilegeLevel;
50
51
52
53
54
55
56
57
58
59
60
61
62 public SetSessionPrivilegeLevel(IpmiVersion version, CipherSuite cipherSuite,
63 AuthenticationType authenticationType, PrivilegeLevel privilegeLevel) {
64 super(version, cipherSuite, authenticationType);
65 this.privilegeLevel = privilegeLevel;
66 }
67
68 @Override
69 public byte getCommandCode() {
70 return CommandCodes.SET_SESSION_PRIVILEGE_LEVEL;
71 }
72
73 @Override
74 public NetworkFunction getNetworkFunction() {
75 return NetworkFunction.ApplicationRequest;
76 }
77
78 @Override
79 protected IpmiPayload preparePayload(int sequenceNumber) throws NoSuchAlgorithmException, InvalidKeyException {
80 byte[] requestData = new byte[1];
81
82 requestData[0] = TypeConverter.intToByte(getRequestedPrivilegeLevelEncoded());
83
84 return new IpmiLanRequest(getNetworkFunction(), getCommandCode(), requestData,
85 TypeConverter.intToByte(sequenceNumber));
86 }
87
88 @Override
89 public ResponseData getResponseData(IpmiMessage message) throws IPMIException,
90 NoSuchAlgorithmException, InvalidKeyException {
91 if (!isCommandResponse(message)) {
92 throw new IllegalArgumentException("This is not a response for Get SEL Entry command");
93 }
94 if (!(message.getPayload() instanceof IpmiLanResponse)) {
95 throw new IllegalArgumentException("Invalid response payload");
96 }
97 if (((IpmiLanResponse) message.getPayload()).getCompletionCode() != CompletionCode.Ok) {
98 throw new IPMIException(((IpmiLanResponse) message.getPayload()).getCompletionCode());
99 }
100
101 return new SetSessionPrivilegeLevelResponseData();
102 }
103
104 private byte getRequestedPrivilegeLevelEncoded() {
105 switch (privilegeLevel) {
106 case MaximumAvailable:
107 return 0;
108 case Callback:
109 return TypeConverter.intToByte(0x1);
110 case User:
111 return TypeConverter.intToByte(0x2);
112 case Operator:
113 return TypeConverter.intToByte(0x3);
114 case Administrator:
115 return TypeConverter.intToByte(0x4);
116 default:
117 throw new IllegalArgumentException("Invalid privilege level");
118 }
119 }
120 }