Blame SOURCES/0003-ipmitool-1.8.11-set-kg-key.patch

b6e1a9
diff -urNp old/doc/ipmitool.1 new/doc/ipmitool.1
b6e1a9
--- old/doc/ipmitool.1	2017-02-06 10:20:02.254362909 +0100
b6e1a9
+++ new/doc/ipmitool.1	2017-02-06 10:33:41.729294474 +0100
b6e1a9
@@ -372,6 +372,20 @@ Configure user access information on the
b6e1a9
 
b6e1a9
 Displays the list of cipher suites supported for the given
b6e1a9
 application (ipmi or sol) on the given channel.
b6e1a9
+.TP
b6e1a9
+\fIsetkg\fP <\fIhex\fP|\fIplain\fP> <\fBkey\fP> [<\fBchannel\fR>]
b6e1a9
+.br
b6e1a9
+
b6e1a9
+Sets K_g key to given value. Use \fIplain\fP to specify \fBkey\fR as simple ASCII string.
b6e1a9
+Use \fIhex\fP to specify \fBkey\fR as sequence of hexadecimal codes of ASCII charactes.
b6e1a9
+I.e. following two examples are equivalent:
b6e1a9
+
b6e1a9
+.RS
b6e1a9
+ipmitool channel setkg plain PASSWORD
b6e1a9
+
b6e1a9
+ipmitool channel setkg hex 50415353574F5244
b6e1a9
+.RE
b6e1a9
+
b6e1a9
 .RE
b6e1a9
 .RE
b6e1a9
 .TP 
b6e1a9
diff -urNp old/include/ipmitool/helper.h new/include/ipmitool/helper.h
b6e1a9
--- old/include/ipmitool/helper.h	2017-02-06 10:20:02.254362909 +0100
b6e1a9
+++ new/include/ipmitool/helper.h	2017-02-06 10:40:07.336136844 +0100
b6e1a9
@@ -58,6 +58,8 @@
b6e1a9
 # define IPMI_UID_MAX 63
b6e1a9
 #endif
b6e1a9
 
b6e1a9
+#define IPMI_KG_BUFFER_SIZE       21 /* key plus null byte */
b6e1a9
+
b6e1a9
 struct ipmi_intf;
b6e1a9
 
b6e1a9
 struct valstr {
b6e1a9
diff -urNp old/include/ipmitool/ipmi_channel.h new/include/ipmitool/ipmi_channel.h
b6e1a9
--- old/include/ipmitool/ipmi_channel.h	2017-02-06 10:20:02.253316684 +0100
b6e1a9
+++ new/include/ipmitool/ipmi_channel.h	2017-02-06 10:58:15.291287621 +0100
b6e1a9
@@ -49,6 +49,10 @@
b6e1a9
 #define IPMI_GET_USER_NAME             0x46
b6e1a9
 #define IPMI_SET_USER_PASSWORD         0x47
b6e1a9
 #define IPMI_GET_CHANNEL_CIPHER_SUITES 0x54
b6e1a9
+#define IPMI_SET_CHANNEL_SECURITY_KEYS 0x56
b6e1a9
+
b6e1a9
+#define IPMI_KG_KEY_ID  1
b6e1a9
+#define IPMI_SET_CHANNEL_SECURITY_KEYS_OP_SET 1
b6e1a9
 
b6e1a9
 /* These are for channel_info_t.session_support */
b6e1a9
 #define IPMI_CHANNEL_SESSION_LESS 0x00
b6e1a9
@@ -137,6 +141,40 @@ int _ipmi_set_channel_access(struct ipmi
b6e1a9
 		struct channel_access_t channel_access, uint8_t access_option,
b6e1a9
 		uint8_t privilege_option);
b6e1a9
 
b6e1a9
+struct set_channel_security_keys_req {
b6e1a9
+#if WORDS_BIGENDIAN
b6e1a9
+	uint8_t __reserved1			:4;
b6e1a9
+	uint8_t channel				:4;
b6e1a9
+	
b6e1a9
+	uint8_t __reserved2			:6;
b6e1a9
+	uint8_t operation			:2;
b6e1a9
+	
b6e1a9
+	uint8_t key_id;
b6e1a9
+	unsigned char key_value[IPMI_KG_BUFFER_SIZE-1]; /* we don't want space for '\0' at the end */
b6e1a9
+#else
b6e1a9
+	uint8_t channel				:4;
b6e1a9
+	uint8_t __reserved1			:4;
b6e1a9
+	
b6e1a9
+	uint8_t operation			:2;
b6e1a9
+	uint8_t __reserved2			:6;
b6e1a9
+	
b6e1a9
+	uint8_t key_id;
b6e1a9
+	unsigned char key_value[IPMI_KG_BUFFER_SIZE-1]; /* we don't want space for '\0' at the end */
b6e1a9
+#endif
b6e1a9
+} __attribute__ ((packed));
b6e1a9
+
b6e1a9
+struct set_channel_security_keys_rsp {
b6e1a9
+#if WORDS_BIGENDIAN
b6e1a9
+	uint8_t __reserved1			:6;
b6e1a9
+	uint8_t lock_status			:2;
b6e1a9
+	unsigned char key_value; /* just the first character, use &key_value to explore the rest */
b6e1a9
+#else
b6e1a9
+	uint8_t lock_status			:2;
b6e1a9
+	uint8_t __reserved1			:6;
b6e1a9
+	unsigned char key_value; /* just the first character, use &key_value to explore the rest */
b6e1a9
+#endif
b6e1a9
+} __attribute__ ((packed));
b6e1a9
+
b6e1a9
 uint8_t ipmi_get_channel_medium(struct ipmi_intf * intf, uint8_t channel);
b6e1a9
 uint8_t ipmi_current_channel_medium(struct ipmi_intf * intf);
b6e1a9
 int ipmi_channel_main(struct ipmi_intf * intf, int argc, char ** argv);
b6e1a9
diff -urNp old/include/ipmitool/ipmi_intf.h new/include/ipmitool/ipmi_intf.h
b6e1a9
--- old/include/ipmitool/ipmi_intf.h	2017-02-06 10:20:02.254362909 +0100
b6e1a9
+++ new/include/ipmitool/ipmi_intf.h	2017-02-06 10:40:40.264577602 +0100
b6e1a9
@@ -60,7 +60,6 @@ enum LANPLUS_SESSION_STATE {
b6e1a9
 
b6e1a9
 #define IPMI_AUTHCODE_BUFFER_SIZE 20
b6e1a9
 #define IPMI_SIK_BUFFER_SIZE      IPMI_MAX_MD_SIZE
b6e1a9
-#define IPMI_KG_BUFFER_SIZE       21 /* key plus null byte */
b6e1a9
 
b6e1a9
 struct ipmi_session_params {
b6e1a9
 	char * hostname;
b6e1a9
diff -urNp old/lib/ipmi_channel.c new/lib/ipmi_channel.c
b6e1a9
--- old/lib/ipmi_channel.c	2017-02-06 10:20:02.255409134 +0100
b6e1a9
+++ new/lib/ipmi_channel.c	2017-02-06 12:32:14.222282317 +0100
b6e1a9
@@ -821,6 +821,92 @@ ipmi_set_user_access(struct ipmi_intf *i
b6e1a9
 	return 0;
b6e1a9
 }
b6e1a9
 
b6e1a9
+int 
b6e1a9
+ipmi_set_channel_security_keys (struct ipmi_intf *intf, uint8_t channel, const char *method, const char *key)
b6e1a9
+{
b6e1a9
+	uint8_t kgkey[IPMI_KG_BUFFER_SIZE];
b6e1a9
+	struct ipmi_rs *rsp;
b6e1a9
+	struct ipmi_rq req;
b6e1a9
+	struct set_channel_security_keys_req req_data;
b6e1a9
+	int rc = -1;
b6e1a9
+	
b6e1a9
+	/* convert provided key to array of bytes */
b6e1a9
+	if (strcmp(method, "hex") == 0) {
b6e1a9
+		if (strlen(key) > (IPMI_KG_BUFFER_SIZE-1)*2) {
b6e1a9
+			lprintf(LOG_ERR, "Provided key is too long, max. length is %d bytes", (IPMI_KG_BUFFER_SIZE-1));
b6e1a9
+			printf_channel_usage();
b6e1a9
+			return -1;
b6e1a9
+		}
b6e1a9
+
b6e1a9
+		rc = ipmi_parse_hex(key, kgkey, sizeof(kgkey)-1);
b6e1a9
+		if (rc == -1) {
b6e1a9
+			lprintf(LOG_ERR, "Number of Kg key characters is not even");
b6e1a9
+			return rc;
b6e1a9
+		} else if (rc == -3) {
b6e1a9
+			lprintf(LOG_ERR, "Kg key is not hexadecimal number");
b6e1a9
+			return rc;
b6e1a9
+		} else if (rc > (IPMI_KG_BUFFER_SIZE-1)) {
b6e1a9
+			lprintf(LOG_ERR, "Kg key is too long");
b6e1a9
+			return rc;
b6e1a9
+		}
b6e1a9
+		
b6e1a9
+	} else if (strcmp(method, "plain") == 0) {
b6e1a9
+		if (strlen(key) > IPMI_KG_BUFFER_SIZE-1) {
b6e1a9
+			lprintf(LOG_ERR, "Provided key is too long, max. length is %d bytes", (IPMI_KG_BUFFER_SIZE -1));
b6e1a9
+			printf_channel_usage();
b6e1a9
+			return rc;
b6e1a9
+		}
b6e1a9
+		
b6e1a9
+		strncpy(kgkey, key, IPMI_KG_BUFFER_SIZE-1);
b6e1a9
+	} else {
b6e1a9
+		printf_channel_usage();
b6e1a9
+		return rc;
b6e1a9
+	}
b6e1a9
+	
b6e1a9
+	/* assemble and send request to set kg key */
b6e1a9
+	memset(&req_data, 0, sizeof(req_data));
b6e1a9
+	req_data.channel = channel;
b6e1a9
+	req_data.operation = IPMI_SET_CHANNEL_SECURITY_KEYS_OP_SET;
b6e1a9
+	req_data.key_id = IPMI_KG_KEY_ID;
b6e1a9
+	memcpy(req_data.key_value, kgkey, IPMI_KG_BUFFER_SIZE-1);
b6e1a9
+	
b6e1a9
+	memset(&req, 0, sizeof(req));
b6e1a9
+	req.msg.netfn = IPMI_NETFN_APP;
b6e1a9
+	req.msg.cmd = IPMI_SET_CHANNEL_SECURITY_KEYS;
b6e1a9
+	req.msg.data = (uint8_t*) &req_data;
b6e1a9
+	req.msg.data_len = sizeof(req_data);
b6e1a9
+
b6e1a9
+	rsp = intf->sendrecv(intf, &req;;
b6e1a9
+	if (rsp == NULL) {
b6e1a9
+		lprintf(LOG_ERR, "Set Channel Security Keys command failed");
b6e1a9
+		return rc;
b6e1a9
+	}
b6e1a9
+	if (rsp->ccode > 0) {
b6e1a9
+		const char *error = NULL;
b6e1a9
+		switch (rsp->ccode) {
b6e1a9
+		case 0x80:
b6e1a9
+			error = "Key is locked";
b6e1a9
+			break;
b6e1a9
+		case 0x81:
b6e1a9
+			error = "Insufficient key bytes";
b6e1a9
+			break;
b6e1a9
+		case 0x82:
b6e1a9
+			error = "Too many key bytes";
b6e1a9
+			break;
b6e1a9
+		case 0x83:
b6e1a9
+			error = "Key value does not meet criteria for K_g key";
b6e1a9
+			break;
b6e1a9
+		default:
b6e1a9
+			error = val2str(rsp->ccode, completion_code_vals);
b6e1a9
+		}
b6e1a9
+		lprintf(LOG_ERR, "Error setting security key: %X (%s)", rsp->ccode, error);
b6e1a9
+		return rc;
b6e1a9
+	}
b6e1a9
+	
b6e1a9
+	lprintf(LOG_NOTICE, "Set Channel Security Keys command succeeded");
b6e1a9
+	return 0;
b6e1a9
+}
b6e1a9
+
b6e1a9
 int
b6e1a9
 ipmi_channel_main(struct ipmi_intf *intf, int argc, char **argv)
b6e1a9
 {
b6e1a9
@@ -890,6 +976,19 @@ ipmi_channel_main(struct ipmi_intf *intf
b6e1a9
 		retval = ipmi_get_channel_cipher_suites(intf,
b6e1a9
 							argv[1], /* ipmi | sol */
b6e1a9
 							channel);
b6e1a9
+	} else if (strncmp(argv[0], "setkg", 5) == 0) {
b6e1a9
+		if (argc < 3 || argc > 4)
b6e1a9
+			printf_channel_usage();
b6e1a9
+		else {
b6e1a9
+			uint8_t ch = 0xe;
b6e1a9
+			char *method = argv[1];
b6e1a9
+			char *key = argv[2];
b6e1a9
+			if (argc == 4) {
b6e1a9
+				ch = (uint8_t)strtol(argv[3], NULL, 0);
b6e1a9
+			}
b6e1a9
+				
b6e1a9
+			retval = ipmi_set_channel_security_keys(intf, ch, method, key);
b6e1a9
+		}
b6e1a9
 	} else {
b6e1a9
 		lprintf(LOG_ERR, "Invalid CHANNEL command: %s\n", argv[0]);
b6e1a9
 		printf_channel_usage();
b6e1a9
@@ -916,6 +1015,10 @@ printf_channel_usage()
b6e1a9
 	lprintf(LOG_NOTICE,
b6e1a9
 "");
b6e1a9
 	lprintf(LOG_NOTICE,
b6e1a9
+"                  setkg hex|plain <key> [channel]");
b6e1a9
+	lprintf(LOG_NOTICE,
b6e1a9
+"");
b6e1a9
+	lprintf(LOG_NOTICE,
b6e1a9
 "Possible privilege levels are:");
b6e1a9
 	lprintf(LOG_NOTICE,
b6e1a9
 "   1   Callback level");
b6e1a9
diff -urNp old/src/plugins/ipmi_intf.c new/src/plugins/ipmi_intf.c
b6e1a9
--- old/src/plugins/ipmi_intf.c	2017-02-06 10:20:02.257501584 +0100
b6e1a9
+++ new/src/plugins/ipmi_intf.c	2017-02-06 10:42:12.585257810 +0100
b6e1a9
@@ -55,6 +55,7 @@
b6e1a9
 #include <ipmitool/ipmi.h>
b6e1a9
 #include <ipmitool/ipmi_sdr.h>
b6e1a9
 #include <ipmitool/log.h>
b6e1a9
+#include <ipmitool/helper.h>
b6e1a9
 
b6e1a9
 #define IPMI_DEFAULT_PAYLOAD_SIZE   25
b6e1a9