Blame SOURCES/0007-URL-encoded-Targetnames.patch

dc245c
From e061cba1b91650ab08ae8fa50e8cadb13ac3d97d Mon Sep 17 00:00:00 2001
dc245c
From: Ronnie Sahlberg <ronniesahlberg@gmail.com>
dc245c
Date: Sun, 16 Jun 2013 11:35:14 -0700
dc245c
Subject: [RHEL7 libiscsi PATCH 07/18] URL encoded Targetnames
dc245c
dc245c
Assume target names are URL encoded with '%' as the special character.
dc245c
dc245c
Any sequence of '%' followed by two bytes in the target name will be replaced
dc245c
with the byte that the second two bytes represent in hexadecimal.
dc245c
dc245c
Example
dc245c
iqn.ronnie.test%3A1234
dc245c
will be translated to iqn.ronnie.test:1234
dc245c
dc245c
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
dc245c
(cherry-picked from upstream commit e061cba1b91650ab08ae8fa50e8cadb13ac3d97d)
dc245c
dc245c
[The right thing to do here is probably to not use this simple-minded code
dc245c
 and use QEMU's URI parser.  I need to find a testcase, then I will create
dc245c
 a bug. - Paolo]
dc245c
---
dc245c
 include/iscsi.h |  12 ++++--
dc245c
 lib/init.c      | 114 +++++++++++++++++++++++++++++++++++++++-----------------
dc245c
 lib/login.c     |   1 +
dc245c
 3 files changed, 88 insertions(+), 39 deletions(-)
dc245c
dc245c
diff --git a/include/iscsi.h b/include/iscsi.h
dc245c
index f14d404..a4ed932 100644
dc245c
--- a/include/iscsi.h
dc245c
+++ b/include/iscsi.h
dc245c
@@ -125,6 +125,10 @@ iscsi_set_initial_r2t(struct iscsi_context *iscsi, enum iscsi_initial_r2t initia
dc245c
  * iSCSI URL format :
dc245c
  * iscsi://[<username>[%<password>]@]<host>[:<port>]/<target-iqn>/<lun>
dc245c
  *
dc245c
+ * Target names are url encoded with '%' as a special character.
dc245c
+ * Example:
dc245c
+ * "iqn.ronnie.test%3A1234" will be translated to "iqn.ronnie.test:1234"
dc245c
+ *
dc245c
  * Function will return a pointer to an iscsi url structure if successful,
dc245c
  * or it will return NULL and set iscsi_get_error() accrodingly if there was a problem
dc245c
  * with the URL.
dc245c
diff --git a/lib/init.c b/lib/init.c
dc245c
index 18f3fb2..60a1b6d 100644
dc245c
--- a/lib/init.c
dc245c
+++ b/lib/init.c
dc245c
@@ -358,6 +358,45 @@ iscsi_is_logged_in(struct iscsi_context *iscsi)
dc245c
 	return iscsi->is_loggedin;
dc245c
 }
dc245c
 
dc245c
+static int
dc245c
+h2i(int h)
dc245c
+{
dc245c
+	if (h >= 'a' && h <= 'f') {
dc245c
+		return h - 'a' + 10;
dc245c
+	}
dc245c
+	if (h >= 'A' && h <= 'F') {
dc245c
+		return h - 'A' + 10;
dc245c
+	}
dc245c
+	return h - '0';
dc245c
+}
dc245c
+
dc245c
+static void
dc245c
+iscsi_decode_url_string(char *str)
dc245c
+{
dc245c
+	while (*str) {
dc245c
+		char *tmp = str;
dc245c
+		char c;
dc245c
+
dc245c
+		if (*str++ != '%') {
dc245c
+			continue;
dc245c
+		}
dc245c
+
dc245c
+		if (*str == 0) {
dc245c
+			return;
dc245c
+		}
dc245c
+		c = h2i(*str++) << 4;
dc245c
+
dc245c
+		if (*str == 0) {
dc245c
+			return;
dc245c
+		}
dc245c
+		c |= h2i(*str++);
dc245c
+
dc245c
+		*tmp++ = c;
dc245c
+		memmove(tmp, str, strlen(str));
dc245c
+		tmp[strlen(str)] = 0;
dc245c
+	}
dc245c
+}
dc245c
+
dc245c
 struct iscsi_url *
dc245c
 iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
dc245c
 {
dc245c
@@ -373,15 +412,18 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
dc245c
 
dc245c
 	if (strncmp(url, "iscsi://", 8)) {
dc245c
 		if (full) {
dc245c
-		iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must be of "
dc245c
-				"the form: %s",url,ISCSI_URL_SYNTAX); }
dc245c
-		else {
dc245c
-		iscsi_set_error(iscsi, "Invalid URL %s\niSCSI Portal URL must be of "
dc245c
-				"the form: %s",url,ISCSI_PORTAL_URL_SYNTAX); }
dc245c
+			iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must "
dc245c
+				"be of the form: %s",
dc245c
+				url, ISCSI_URL_SYNTAX);
dc245c
+		} else {
dc245c
+			iscsi_set_error(iscsi, "Invalid URL %s\niSCSI Portal "
dc245c
+				"URL must be of the form: %s",
dc245c
+				url, ISCSI_PORTAL_URL_SYNTAX);
dc245c
+		}
dc245c
 		return NULL;
dc245c
 	}
dc245c
 
dc245c
-	strncpy(str,url + 8,MAX_STRING_SIZE);
dc245c
+	strncpy(str,url + 8, MAX_STRING_SIZE);
dc245c
 	portal = str;
dc245c
 
dc245c
 	user   = getenv("LIBISCSI_CHAP_USERNAME");
dc245c
@@ -406,56 +448,56 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
dc245c
 	if (full) {
dc245c
 		target = strchr(portal, '/');
dc245c
 		if (target == NULL) {
dc245c
-			iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse "
dc245c
-				"'<target-iqn>'\niSCSI URL must be of the "
dc245c
-				"form: %s",
dc245c
-				url,
dc245c
-				ISCSI_URL_SYNTAX);
dc245c
+			iscsi_set_error(iscsi, "Invalid URL %s\nCould not "
dc245c
+				"parse '<target-iqn>'\niSCSI URL must be of "
dc245c
+				"the form: %s",
dc245c
+				url, ISCSI_URL_SYNTAX);
dc245c
 			return NULL;
dc245c
 		}
dc245c
 		*target++ = 0;
dc245c
 
dc245c
 		if (*target == 0) {
dc245c
-			iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse "
dc245c
-				"<target-iqn>\n"
dc245c
-				"iSCSI URL must be of the form: %s",
dc245c
-				url,
dc245c
-				ISCSI_URL_SYNTAX);
dc245c
+			iscsi_set_error(iscsi, "Invalid URL %s\nCould not "
dc245c
+				"parse <target-iqn>\niSCSI URL must be of the "
dc245c
+				"form: %s",
dc245c
+				url, ISCSI_URL_SYNTAX);
dc245c
 			return NULL;
dc245c
 		}
dc245c
 
dc245c
 		lun = strchr(target, '/');
dc245c
 		if (lun == NULL) {
dc245c
-			iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
dc245c
-				"iSCSI URL must be of the form: %s",
dc245c
-				url,
dc245c
-				ISCSI_URL_SYNTAX);
dc245c
+			iscsi_set_error(iscsi, "Invalid URL %s\nCould not "
dc245c
+				"parse <lun>\niSCSI URL must be of the form: "
dc245c
+				"%s",
dc245c
+				url, ISCSI_URL_SYNTAX);
dc245c
 			return NULL;
dc245c
 		}
dc245c
 		*lun++ = 0;
dc245c
 
dc245c
 		l = strtol(lun, &tmp, 10);
dc245c
 		if (*lun == 0 || *tmp != 0) {
dc245c
-			iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
dc245c
-				"iSCSI URL must be of the form: %s",
dc245c
-				url,
dc245c
-				ISCSI_URL_SYNTAX);
dc245c
+			iscsi_set_error(iscsi, "Invalid URL %s\nCould not "
dc245c
+				"parse <lun>\niSCSI URL must be of the form: "
dc245c
+				"%s",
dc245c
+				url, ISCSI_URL_SYNTAX);
dc245c
 			return NULL;
dc245c
 		}
dc245c
-	}
dc245c
-	else
dc245c
-	{
dc245c
+	} else {
dc245c
 		tmp=strchr(portal,'/');
dc245c
-		if (tmp) *tmp=0;
dc245c
+		if (tmp) {
dc245c
+			*tmp=0;
dc245c
+		}
dc245c
 	}
dc245c
 	
dc245c
-	if (iscsi != NULL)
dc245c
+	if (iscsi != NULL) {
dc245c
 		iscsi_url = iscsi_malloc(iscsi, sizeof(struct iscsi_url));
dc245c
-	else
dc245c
+	} else {
dc245c
 		iscsi_url = malloc(sizeof(struct iscsi_url));
dc245c
-	
dc245c
+	}
dc245c
+
dc245c
 	if (iscsi_url == NULL) {
dc245c
-		iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure");
dc245c
+		iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate "
dc245c
+			"iscsi_url structure");
dc245c
 		return NULL;
dc245c
 	}
dc245c
 	memset(iscsi_url, 0, sizeof(struct iscsi_url));
dc245c
@@ -464,15 +506,17 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
dc245c
 	strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE);
dc245c
 
dc245c
 	if (user != NULL && passwd != NULL) {
dc245c
-		strncpy(iscsi_url->user,user,MAX_STRING_SIZE);
dc245c
-		strncpy(iscsi_url->passwd,passwd,MAX_STRING_SIZE);
dc245c
+		strncpy(iscsi_url->user, user, MAX_STRING_SIZE);
dc245c
+		strncpy(iscsi_url->passwd, passwd, MAX_STRING_SIZE);
dc245c
 	}
dc245c
 
dc245c
 	if (full) {
dc245c
-		strncpy(iscsi_url->target,target,MAX_STRING_SIZE);
dc245c
+		strncpy(iscsi_url->target, target, MAX_STRING_SIZE);
dc245c
 		iscsi_url->lun = l;
dc245c
 	}
dc245c
 
dc245c
+	iscsi_decode_url_string(&iscsi_url->target[0]);
dc245c
+
dc245c
 	return iscsi_url;
dc245c
 }
dc245c
 
dc245c
diff --git a/lib/login.c b/lib/login.c
dc245c
index 29fe4b3..0448ce2 100644
dc245c
--- a/lib/login.c
dc245c
+++ b/lib/login.c
dc245c
@@ -622,6 +622,7 @@ h2i(int h)
dc245c
 	}
dc245c
 	return h - '0';
dc245c
 }
dc245c
+
dc245c
 static int
dc245c
 i2h(int i)
dc245c
 {
dc245c
-- 
dc245c
1.8.1.4
dc245c