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.PrivilegeLevel;
28 import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
29 import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
30 import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
31 import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
32 import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
33 import org.metricshub.ipmi.core.common.TypeConverter;
34
35 import java.security.InvalidKeyException;
36 import java.security.NoSuchAlgorithmException;
37
38
39
40
41 public class SetSessionPrivilegeLevel extends IpmiCommandCoder<SetSessionPrivilegeLevelResponseData> {
42
43
44 public static final int LEVEL_NOT_AVAILABLE = 0x80;
45
46
47 public static final int LEVEL_ABOVE_LIMIT = 0x81;
48
49 private PrivilegeLevel privilegeLevel;
50
51
52
53
54
55
56
57
58 public static String describeRefusal(IPMIException refusal) {
59 switch (refusal.getRawCode()) {
60 case LEVEL_NOT_AVAILABLE:
61 return IPMIException.describe("the level is not available for this user", LEVEL_NOT_AVAILABLE);
62 case LEVEL_ABOVE_LIMIT:
63 return IPMIException
64 .describe("the level exceeds the privilege limit of the channel or of the user", LEVEL_ABOVE_LIMIT);
65 default:
66 return refusal.describe();
67 }
68 }
69
70
71
72
73
74
75
76
77 public SetSessionPrivilegeLevel(PrivilegeLevel privilegeLevel) {
78 this.privilegeLevel = privilegeLevel;
79 }
80
81
82
83
84
85
86
87 public PrivilegeLevel getPrivilegeLevel() {
88 return privilegeLevel;
89 }
90
91 @Override
92 public byte getCommandCode() {
93 return CommandCodes.SET_SESSION_PRIVILEGE_LEVEL;
94 }
95
96 @Override
97 public NetworkFunction getNetworkFunction() {
98 return NetworkFunction.ApplicationRequest;
99 }
100
101 @Override
102 protected IpmiPayload preparePayload(int sequenceNumber) throws NoSuchAlgorithmException, InvalidKeyException {
103 byte[] requestData = new byte[1];
104
105 requestData[0] = TypeConverter.intToByte(getRequestedPrivilegeLevelEncoded());
106
107 return new IpmiLanRequest(
108 getNetworkFunction(),
109 getCommandCode(),
110 requestData,
111 TypeConverter.intToByte(sequenceNumber));
112 }
113
114 @Override
115 public SetSessionPrivilegeLevelResponseData getResponseData(IpmiMessage message)
116 throws IPMIException,
117 NoSuchAlgorithmException,
118 InvalidKeyException {
119 validateResponse(message);
120
121 return new SetSessionPrivilegeLevelResponseData();
122 }
123
124 private byte getRequestedPrivilegeLevelEncoded() {
125 switch (privilegeLevel) {
126 case MaximumAvailable:
127 return 0;
128 case Callback:
129 return TypeConverter.intToByte(0x1);
130 case User:
131 return TypeConverter.intToByte(0x2);
132 case Operator:
133 return TypeConverter.intToByte(0x3);
134 case Administrator:
135 return TypeConverter.intToByte(0x4);
136 default:
137 throw new IllegalArgumentException("Invalid privilege level");
138 }
139 }
140 }