Blame SOURCES/ipmitool-1.8.13-set-kg-key1.patch

00a85e
diff -up ./doc/ipmitool.1.kg1 ./doc/ipmitool.1
00a85e
--- ./doc/ipmitool.1.kg1	2014-03-05 09:04:19.334742088 +0100
00a85e
+++ ./doc/ipmitool.1	2014-03-05 09:02:53.011524309 +0100
00a85e
@@ -371,6 +371,20 @@ Configure user access information on the
00a85e
 
00a85e
 Displays the list of cipher suites supported for the given
00a85e
 application (ipmi or sol) on the given channel.
00a85e
+.TP
00a85e
+\fIsetkg\fP <\fIhex\fP|\fIplain\fP> <\fBkey\fP> [<\fBchannel\fR>]
00a85e
+.br
00a85e
+
00a85e
+Sets K_g key to given value. Use \fIplain\fP to specify \fBkey\fR as simple ASCII string.
00a85e
+Use \fIhex\fP to specify \fBkey\fR as sequence of hexadecimal codes of ASCII charactes.
00a85e
+I.e. following two examples are equivalent:
00a85e
+
00a85e
+.RS
00a85e
+ipmitool channel setkg plain PASSWORD
00a85e
+
00a85e
+ipmitool channel setkg hex 50415353574F5244
00a85e
+.RE
00a85e
+
00a85e
 .RE
00a85e
 .RE
00a85e
 .TP 
00a85e
diff -up ./include/ipmitool/helper.h.kg1 ./include/ipmitool/helper.h
00a85e
--- ./include/ipmitool/helper.h.kg1	2014-03-05 09:42:21.546499783 +0100
00a85e
+++ ./include/ipmitool/helper.h	2014-03-05 09:43:28.095667676 +0100
00a85e
@@ -58,6 +58,8 @@
00a85e
 # define IPMI_UID_MAX 63
00a85e
 #endif
00a85e
 
00a85e
+#define IPMI_KG_BUFFER_SIZE       21 /* key plus null byte */
00a85e
+
00a85e
 struct ipmi_intf;
00a85e
 
00a85e
 struct valstr {
00a85e
@@ -100,6 +102,8 @@ uint8_t ipmi_csum(uint8_t * d, int s);
00a85e
 FILE * ipmi_open_file(const char * file, int rw);
00a85e
 void ipmi_start_daemon(struct ipmi_intf *intf);
00a85e
 
00a85e
+unsigned char *ipmi_parse_hex(const char *str);
00a85e
+
00a85e
 #define ipmi_open_file_read(file)	ipmi_open_file(file, 0)
00a85e
 #define ipmi_open_file_write(file)	ipmi_open_file(file, 1)
00a85e
 
00a85e
diff -up ./include/ipmitool/ipmi_channel.h.kg1 ./include/ipmitool/ipmi_channel.h
00a85e
--- ./include/ipmitool/ipmi_channel.h.kg1	2014-03-05 09:04:41.707798532 +0100
00a85e
+++ ./include/ipmitool/ipmi_channel.h	2014-03-05 09:10:00.770603481 +0100
00a85e
@@ -48,7 +48,10 @@
00a85e
 #define IPMI_GET_USER_NAME             0x46
00a85e
 #define IPMI_SET_USER_PASSWORD         0x47
00a85e
 #define IPMI_GET_CHANNEL_CIPHER_SUITES 0x54
00a85e
+#define IPMI_SET_CHANNEL_SECURITY_KEYS 0x56
00a85e
 
00a85e
+#define IPMI_KG_KEY_ID  1
00a85e
+#define IPMI_SET_CHANNEL_SECURITY_KEYS_OP_SET 1
00a85e
 
00a85e
 /*
00a85e
  * The Get Authentication Capabilities response structure
00a85e
@@ -246,6 +249,48 @@ struct set_user_access_data {
00a85e
 #endif
00a85e
 } ATTRIBUTE_PACKING;
00a85e
 #ifdef HAVE_PRAGMA_PACK
00a85e
+#pragma pack(0)
00a85e
+#endif
00a85e
+
00a85e
+#ifdef HAVE_PRAGMA_PACK
00a85e
+#pragma pack(1)
00a85e
+#endif
00a85e
+struct set_channel_security_keys_req {
00a85e
+#if WORDS_BIGENDIAN
00a85e
+	uint8_t __reserved1                     :4;
00a85e
+	uint8_t channel                         :4;
00a85e
+	uint8_t __reserved2                     :6;
00a85e
+	uint8_t operation                       :2;
00a85e
+	uint8_t key_id;
00a85e
+	unsigned char key_value[IPMI_KG_BUFFER_SIZE-1]; /* we don't want space for '\0' at the end */
00a85e
+#else
00a85e
+	uint8_t channel                         :4;
00a85e
+	uint8_t __reserved1                     :4;
00a85e
+	uint8_t operation                       :2;
00a85e
+	uint8_t __reserved2                     :6;
00a85e
+	uint8_t key_id;
00a85e
+	unsigned char key_value[IPMI_KG_BUFFER_SIZE-1]; /* we don't want space for '\0' at the end */
00a85e
+#endif
00a85e
+} ATTRIBUTE_PACKING;
00a85e
+#ifdef HAVE_PRAGMA_PACK
00a85e
+#pragma pack(0)
00a85e
+#endif
00a85e
+
00a85e
+#ifdef HAVE_PRAGMA_PACK
00a85e
+#pragma pack(1)
00a85e
+#endif
00a85e
+struct set_channel_security_keys_rsp {
00a85e
+#if WORDS_BIGENDIAN
00a85e
+	uint8_t __reserved1                     :6;
00a85e
+	uint8_t lock_status                     :2;
00a85e
+	unsigned char key_value; /* just the first character, use &key_value to explore the rest */
00a85e
+#else
00a85e
+	uint8_t lock_status                     :2;
00a85e
+	uint8_t __reserved1                     :6;
00a85e
+	unsigned char key_value; /* just the first character, use &key_value to explore the rest */
00a85e
+#endif
00a85e
+} ATTRIBUTE_PACKING;
00a85e
+#ifdef HAVE_PRAGMA_PACK
00a85e
 #pragma pack(0)
00a85e
 #endif
00a85e
 
00a85e
diff -up ./include/ipmitool/ipmi_intf.h.kg1 ./include/ipmitool/ipmi_intf.h
00a85e
--- ./include/ipmitool/ipmi_intf.h.kg1	2014-03-05 09:10:45.363715984 +0100
00a85e
+++ ./include/ipmitool/ipmi_intf.h	2014-03-05 09:11:27.483822244 +0100
00a85e
@@ -60,7 +60,7 @@ enum LANPLUS_SESSION_STATE {
00a85e
 
00a85e
 #define IPMI_AUTHCODE_BUFFER_SIZE 20
00a85e
 #define IPMI_SIK_BUFFER_SIZE      20
00a85e
-#define IPMI_KG_BUFFER_SIZE       21 /* key plus null byte */
00a85e
+/* XXX: remove or modify  # d e fine IPMI_KG_BUFFER_SIZE       21 /* key plus null byte */
00a85e
 
00a85e
 struct ipmi_session {
00a85e
 	uint8_t hostname[64];
00a85e
diff -up ./lib/helper.c.kg1 ./lib/helper.c
00a85e
--- ./lib/helper.c.kg1	2014-03-05 09:11:41.417857400 +0100
00a85e
+++ ./lib/helper.c	2014-03-05 09:15:11.318386949 +0100
00a85e
@@ -667,6 +667,68 @@ ipmi_start_daemon(struct ipmi_intf *intf
00a85e
 	dup(0);
00a85e
 }
00a85e
 
00a85e
+/* ipmi_parse_hex - convert hexadecimal numbers to ascii string
00a85e
+ *                  Input string must be composed of two-characer hexadecimal numbers.
00a85e
+ *                  There is no separator between the numbers. Each number results in one character
00a85e
+ *                  of the converted string.
00a85e
+ *
00a85e
+ *                  Example: ipmi_parse_hex("50415353574F5244") returns 'PASSWORD'
00a85e
+ *
00a85e
+ * @param str:  input string. It must contain only even number of '0'-'9','a'-'f' and 'A-F' characters.
00a85e
+ * @returns converted ascii string
00a85e
+ * @returns NULL on error
00a85e
+ */
00a85e
+unsigned char *
00a85e
+ipmi_parse_hex(const char *str)
00a85e
+{
00a85e
+	const char * p;
00a85e
+	unsigned char * out, *q;
00a85e
+	unsigned char b = 0;
00a85e
+	int shift = 4;
00a85e
+
00a85e
+	if (strlen(str) == 0)
00a85e
+		return NULL;
00a85e
+
00a85e
+	if (strlen(str) % 2 != 0) {
00a85e
+		lprintf(LOG_ERR, "Number of hex_kg characters is not even");
00a85e
+		return NULL;
00a85e
+	}
00a85e
+
00a85e
+	if (strlen(str) > (IPMI_KG_BUFFER_SIZE-1)*2) {
00a85e
+		lprintf(LOG_ERR, "Kg key is too long");
00a85e
+		return NULL;
00a85e
+	}
00a85e
+
00a85e
+	out = calloc(IPMI_KG_BUFFER_SIZE, sizeof(unsigned char));
00a85e
+	if (out == NULL) {
00a85e
+		lprintf(LOG_ERR, "malloc failure");
00a85e
+		return NULL;
00a85e
+	}
00a85e
+
00a85e
+	for (p = str, q = out; *p; p++) {
00a85e
+		if (!isxdigit(*p)) {
00a85e
+			lprintf(LOG_ERR, "Kg_hex is not hexadecimal number");
00a85e
+			free(out);
00a85e
+			return NULL;
00a85e
+		}
00a85e
+
00a85e
+		if (*p < 'A') /* it must be 0-9 */
00a85e
+			b = *p - '0';
00a85e
+		else /* it's A-F or a-f */
00a85e
+			b = (*p | 0x20) - 'a' + 10; /* convert to lowercase and to 10-15 */
00a85e
+
00a85e
+		*q = *q + b << shift;
00a85e
+		if (shift)
00a85e
+			shift = 0;
00a85e
+		else {
00a85e
+			shift = 4;
00a85e
+			q++;
00a85e
+		}
00a85e
+	}
00a85e
+
00a85e
+	return out;
00a85e
+}
00a85e
+
00a85e
 /* is_fru_id - wrapper for str-2-int FRU ID conversion. Message is printed
00a85e
  * on error.
00a85e
  * FRU ID range: <0..255>
00a85e
diff -up ./lib/ipmi_channel.c.kg1 ./lib/ipmi_channel.c
00a85e
--- ./lib/ipmi_channel.c.kg1	2014-03-05 09:15:28.655430688 +0100
00a85e
+++ ./lib/ipmi_channel.c	2014-03-05 09:37:19.367737427 +0100
00a85e
@@ -786,6 +786,90 @@ ipmi_current_channel_medium(struct ipmi_
00a85e
 	return ipmi_get_channel_medium(intf, 0xE);
00a85e
 }
00a85e
 
00a85e
+int 
00a85e
+ipmi_set_channel_security_keys (struct ipmi_intf *intf, uint8_t channel,
00a85e
+				const char *method, const char *key)
00a85e
+{
00a85e
+	unsigned char* decoded_key = NULL;
00a85e
+	struct ipmi_rs *rsp;
00a85e
+	struct ipmi_rq req;
00a85e
+	struct set_channel_security_keys_req req_data;
00a85e
+
00a85e
+	/* convert provided key to array of bytes */
00a85e
+	if (strcmp(method, "hex") == 0) {
00a85e
+		if (strlen(key) > (IPMI_KG_BUFFER_SIZE-1)*2) {
00a85e
+			lprintf(LOG_ERR, "Provided key is too long, max. length is 20 bytes");
00a85e
+			printf_channel_usage();
00a85e
+			return -1;
00a85e
+		}
00a85e
+		decoded_key = ipmi_parse_hex(key);
00a85e
+		if (decoded_key == NULL) {
00a85e
+			/* something went bad, ipmi_parse_hex already reported the error */
00a85e
+			return -1;
00a85e
+		}
00a85e
+	} else if (strcmp(method, "plain") == 0) {
00a85e
+		if (strlen(key) > IPMI_KG_BUFFER_SIZE-1) {
00a85e
+			lprintf(LOG_ERR, "Provided key is too long, max. length is 20 bytes");
00a85e
+			printf_channel_usage();
00a85e
+			return -1;
00a85e
+		}
00a85e
+
00a85e
+		decoded_key = calloc(IPMI_KG_BUFFER_SIZE, sizeof(unsigned char));
00a85e
+		if (decoded_key == NULL) {
00a85e
+			lprintf(LOG_ERR, "Cannot allocate memory");
00a85e
+			return -1;
00a85e
+		}
00a85e
+		strncpy(decoded_key, key, IPMI_KG_BUFFER_SIZE-1);
00a85e
+	} else {
00a85e
+		printf_channel_usage();
00a85e
+		return -1;
00a85e
+	}
00a85e
+
00a85e
+	/* assemble and send request to set kg key */
00a85e
+	memset(&req_data, 0, sizeof(req_data));
00a85e
+	req_data.channel = channel;
00a85e
+	req_data.operation = IPMI_SET_CHANNEL_SECURITY_KEYS_OP_SET;
00a85e
+	req_data.key_id = IPMI_KG_KEY_ID;
00a85e
+	memcpy(req_data.key_value, decoded_key, IPMI_KG_BUFFER_SIZE-1);
00a85e
+	free(decoded_key);
00a85e
+
00a85e
+	memset(&req, 0, sizeof(req));
00a85e
+	req.msg.netfn = IPMI_NETFN_APP;
00a85e
+	req.msg.cmd = IPMI_SET_CHANNEL_SECURITY_KEYS;
00a85e
+	req.msg.data = (uint8_t*) &req_data;
00a85e
+	req.msg.data_len = sizeof(req_data);
00a85e
+
00a85e
+	rsp = intf->sendrecv(intf, &req;;
00a85e
+	if (rsp == NULL) {
00a85e
+		lprintf(LOG_ERR, "Set Channel Security Keys command failed");
00a85e
+		return -1;
00a85e
+	}
00a85e
+	if (rsp->ccode > 0) {
00a85e
+		const char *error = NULL;
00a85e
+		switch (rsp->ccode) {
00a85e
+			case 0x80:
00a85e
+				error = "Key is locked";
00a85e
+				break;
00a85e
+			case 0x81:
00a85e
+				error = "Insufficient key bytes";
00a85e
+				break;
00a85e
+			case 0x82:
00a85e
+				error = "Too many key bytes";
00a85e
+				break;
00a85e
+			case 0x83:
00a85e
+				error = "Key value does not meet criteria for K_g key";
00a85e
+				break;
00a85e
+			default:
00a85e
+				error = val2str(rsp->ccode, completion_code_vals);
00a85e
+		}
00a85e
+		lprintf(LOG_ERR, "Error setting security key: %X (%s)", rsp->ccode, error);
00a85e
+		return -1;
00a85e
+	}
00a85e
+
00a85e
+	lprintf(LOG_NOTICE, "Set Channel Security Keys command succeeded");
00a85e
+	return 0;
00a85e
+}
00a85e
+
00a85e
 void
00a85e
 printf_channel_usage()
00a85e
 {
00a85e
@@ -795,6 +879,7 @@ printf_channel_usage()
00a85e
 		"<user id> [callin=on|off] [ipmi=on|off] [link=on|off] [privilege=level]");
00a85e
 	lprintf(LOG_NOTICE, "                  info      [channel number]");
00a85e
 	lprintf(LOG_NOTICE, "                  getciphers <ipmi | sol> [channel]\n");
00a85e
+	lprintf(LOG_NOTICE, "                  setkg hex|plain <key> [channel]\n");
00a85e
 	lprintf(LOG_NOTICE, "Possible privilege levels are:");
00a85e
 	lprintf(LOG_NOTICE, "   1   Callback level");
00a85e
 	lprintf(LOG_NOTICE, "   2   User level");
00a85e
@@ -892,6 +977,21 @@ ipmi_channel_main(struct ipmi_intf * int
00a85e
 								ch);
00a85e
 		}
00a85e
 	}
00a85e
+	else if (strcmp(argv[0], "setkg") == 0)
00a85e
+	{
00a85e
+		if (argc < 3 || argc > 4)
00a85e
+			printf_channel_usage();
00a85e
+		else {
00a85e
+			uint8_t ch = 0xe;
00a85e
+			char *method = argv[1];
00a85e
+			char *key = argv[2];
00a85e
+			if (argc == 4) {
00a85e
+				ch = (uint8_t)strtol(argv[3], NULL, 0);
00a85e
+			}
00a85e
+
00a85e
+			retval = ipmi_set_channel_security_keys(intf, ch, method, key);
00a85e
+		}
00a85e
+	}
00a85e
 	else
00a85e
 	{
00a85e
 		printf("Invalid CHANNEL command: %s\n", argv[0]);
00a85e
diff -up ./lib/ipmi_main.c.kg1 ./lib/ipmi_main.c
00a85e
--- ./lib/ipmi_main.c.kg1	2014-03-05 09:50:30.905734365 +0100
00a85e
+++ ./lib/ipmi_main.c	2014-03-05 10:47:04.857296819 +0100
00a85e
@@ -275,69 +275,6 @@ void ipmi_catch_sigint()
00a85e
 	exit(-1);
00a85e
 }
00a85e
 
00a85e
-/* ipmi_parse_hex - convert hexadecimal numbers to ascii string
00a85e
- *                  Input string must be composed of two-characer hexadecimal numbers.
00a85e
- *                  There is no separator between the numbers. Each number results in one character
00a85e
- *                  of the converted string.
00a85e
- *
00a85e
- *                  Example: ipmi_parse_hex("50415353574F5244") returns 'PASSWORD'
00a85e
- *
00a85e
- * @param str:  input string. It must contain only even number of '0'-'9','a'-'f' and 'A-F' characters.
00a85e
- * @returns converted ascii string
00a85e
- * @returns NULL on error
00a85e
- */
00a85e
-static unsigned char *
00a85e
-ipmi_parse_hex(const char *str)
00a85e
-{
00a85e
-	const char * p;
00a85e
-	unsigned char * out, *q;
00a85e
-	unsigned char b = 0;
00a85e
-	int shift = 4;
00a85e
-
00a85e
-	if (strlen(str) == 0)
00a85e
-		return NULL;
00a85e
-
00a85e
-	if (strlen(str) % 2 != 0) {
00a85e
-		lprintf(LOG_ERR, "Number of hex_kg characters is not even");
00a85e
-		return NULL;
00a85e
-	}
00a85e
-
00a85e
-	if (strlen(str) > (IPMI_KG_BUFFER_SIZE-1)*2) {
00a85e
-		lprintf(LOG_ERR, "Kg key is too long");
00a85e
-		return NULL;
00a85e
-	}
00a85e
-
00a85e
-	out = calloc(IPMI_KG_BUFFER_SIZE, sizeof(unsigned char));
00a85e
-	if (out == NULL) {
00a85e
-		lprintf(LOG_ERR, "malloc failure");
00a85e
-		return NULL;
00a85e
-	}
00a85e
-
00a85e
-	for (p = str, q = out; *p; p++) {
00a85e
-		if (!isxdigit(*p)) {
00a85e
-			lprintf(LOG_ERR, "Kg_hex is not hexadecimal number");
00a85e
-			free(out);
00a85e
-			out = NULL;
00a85e
-			return NULL;
00a85e
-		}
00a85e
-
00a85e
-		if (*p < 'A') /* it must be 0-9 */
00a85e
-			b = *p - '0';
00a85e
-		else /* it's A-F or a-f */
00a85e
-			b = (*p | 0x20) - 'a' + 10; /* convert to lowercase and to 10-15 */
00a85e
-
00a85e
-		*q = *q + b << shift;
00a85e
-		if (shift)
00a85e
-			shift = 0;
00a85e
-		else {
00a85e
-			shift = 4;
00a85e
-			q++;
00a85e
-		}
00a85e
-	}
00a85e
-
00a85e
-	return out;
00a85e
-}
00a85e
-
00a85e
 /* ipmi_parse_options  -  helper function to handle parsing command line options
00a85e
  *
00a85e
  * @argc:	count of options
00a85e
@@ -521,11 +458,12 @@ ipmi_main(int argc, char ** argv,
00a85e
 				free(kgkey);
00a85e
 				kgkey = NULL;
00a85e
 			}
00a85e
-			kgkey = strdup(optarg);
00a85e
+			kgkey = calloc(IPMI_KG_BUFFER_SIZE, 1);
00a85e
 			if (kgkey == NULL) {
00a85e
 				lprintf(LOG_ERR, "%s: malloc failure", progname);
00a85e
 				goto out_free;
00a85e
 			}
00a85e
+			strncpy(kgkey, optarg, IPMI_KG_BUFFER_SIZE);
00a85e
 			break;
00a85e
 		case 'K':
00a85e
 			if ((tmp_env = getenv("IPMI_KGKEY"))) {
00a85e
@@ -533,11 +471,12 @@ ipmi_main(int argc, char ** argv,
00a85e
 					free(kgkey);
00a85e
 					kgkey = NULL;
00a85e
 				}
00a85e
-				kgkey = strdup(tmp_env);
00a85e
+				kgkey = calloc(IPMI_KG_BUFFER_SIZE, 1);
00a85e
 				if (kgkey == NULL) {
00a85e
 					lprintf(LOG_ERR, "%s: malloc failure", progname);
00a85e
 					goto out_free;
00a85e
 				}
00a85e
+				strncpy(kgkey, tmp_env, IPMI_KG_BUFFER_SIZE);
00a85e
 			} else {
00a85e
 				lprintf(LOG_WARN, "Unable to read kgkey from environment");
00a85e
 			}
00a85e
@@ -564,11 +503,14 @@ ipmi_main(int argc, char ** argv,
00a85e
 					kgkey = NULL;
00a85e
 				}
00a85e
 				kgkey = strdup(tmp_pass);
00a85e
-				tmp_pass = NULL;
00a85e
+				kgkey = calloc(IPMI_KG_BUFFER_SIZE, 1);
00a85e
 				if (kgkey == NULL) {
00a85e
 					lprintf(LOG_ERR, "%s: malloc failure", progname);
00a85e
+					tmp_pass = NULL;
00a85e
 					goto out_free;
00a85e
 				}
00a85e
+				strncpy(kgkey, tmp_pass, IPMI_KG_BUFFER_SIZE);
00a85e
+				tmp_pass = NULL;
00a85e
 			}
00a85e
 			break;
00a85e
 		case 'U':