View Javadoc
1   package org.metricshub.ipmi.core.coding.commands.session;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 Verax Systems, MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * Wrapper class for Set Session Privilege Level command
46   */
47  public class SetSessionPrivilegeLevel extends IpmiCommandCoder {
48  
49      private PrivilegeLevel privilegeLevel;
50  
51      /**
52       * Initiates {@link SetSessionPrivilegeLevel} for encoding and decoding
53       * @param version
54       * - IPMI version of the command.
55       * @param cipherSuite
56       * - {@link CipherSuite} containing authentication, confidentiality and integrity algorithms for this session.
57       * @param authenticationType
58       * - Type of authentication used. Must be RMCPPlus for IPMI v2.0.
59       * @param privilegeLevel
60       * - Requested {@link PrivilegeLevel} to acquire. Can not be higher than level declared during starting session.
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 }