Improve error messages when parsing of iscsi url fails
Always print the correct url syntax when parsing has failed. Test if the user forgot to specify a <target-iqn> at all and log this as a missing target-iqn error. Not as a missing <lun> error. Remove \n from the error strings in init.c
This commit is contained in:
52
lib/init.c
52
lib/init.c
@@ -29,6 +29,8 @@
|
|||||||
#include "iscsi-private.h"
|
#include "iscsi-private.h"
|
||||||
#include "slist.h"
|
#include "slist.h"
|
||||||
|
|
||||||
|
#define ISCSI_URL_SYNTAX "\"iscsi://[<username>[%<password>]@]" \
|
||||||
|
"<host>[:<port>]/<target-iqn>/<lun>\""
|
||||||
|
|
||||||
struct iscsi_context *
|
struct iscsi_context *
|
||||||
iscsi_create_context(const char *initiator_name)
|
iscsi_create_context(const char *initiator_name)
|
||||||
@@ -240,15 +242,15 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
|
|||||||
|
|
||||||
if (strncmp(url, "iscsi://", 8)) {
|
if (strncmp(url, "iscsi://", 8)) {
|
||||||
iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must be of "
|
iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must be of "
|
||||||
"the form "
|
"the form: %s",
|
||||||
"\"iscsi://[<username>[%%<password>]@]"
|
url,
|
||||||
"<host>[:<port>]/<target-iqn>/<lun>\"\n", url);
|
ISCSI_URL_SYNTAX);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
str = strdup(url + 8);
|
str = strdup(url + 8);
|
||||||
if (str == NULL) {
|
if (str == NULL) {
|
||||||
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s\n", url);
|
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s", url);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
portal = str;
|
portal = str;
|
||||||
@@ -269,19 +271,31 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
|
|||||||
target = index(portal, '/');
|
target = index(portal, '/');
|
||||||
if (target == NULL) {
|
if (target == NULL) {
|
||||||
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse "
|
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse "
|
||||||
"'<target-iqn>'\niSCSI URL must be of the form "
|
"'<target-iqn>'\niSCSI URL must be of the "
|
||||||
"\"iscsi://[<username>[%%<password>]@]"
|
"form: %s",
|
||||||
"<host>[:<port>]/<target-iqn>/<lun>\"\n", url);
|
url,
|
||||||
|
ISCSI_URL_SYNTAX);
|
||||||
free(str);
|
free(str);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
*target++ = 0;
|
*target++ = 0;
|
||||||
|
|
||||||
|
if (*target == 0) {
|
||||||
|
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse "
|
||||||
|
"<target-iqn>\n"
|
||||||
|
"iSCSI URL must be of the form: %s",
|
||||||
|
url,
|
||||||
|
ISCSI_URL_SYNTAX);
|
||||||
|
free(str);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
lun = index(target, '/');
|
lun = index(target, '/');
|
||||||
if (lun == NULL) {
|
if (lun == NULL) {
|
||||||
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
|
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
|
||||||
"iSCSI URL must be of the form \"iscsi://"
|
"iSCSI URL must be of the form: %s",
|
||||||
"<host>[:<port>]/<target-iqn>/<lun>\"\n", url);
|
url,
|
||||||
|
ISCSI_URL_SYNTAX);
|
||||||
free(str);
|
free(str);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -290,16 +304,16 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
|
|||||||
l = strtol(lun, &tmp, 10);
|
l = strtol(lun, &tmp, 10);
|
||||||
if (*lun == 0 || *tmp != 0) {
|
if (*lun == 0 || *tmp != 0) {
|
||||||
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
|
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
|
||||||
"iSCSI URL must be of the form \"iscsi://"
|
"iSCSI URL must be of the form: %s",
|
||||||
"<host>[:<port>]/<target-iqn>/<lun>\"\n",
|
url,
|
||||||
url);
|
ISCSI_URL_SYNTAX);
|
||||||
free(str);
|
free(str);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
iscsi_url = malloc(sizeof(struct iscsi_url));
|
iscsi_url = malloc(sizeof(struct iscsi_url));
|
||||||
if (iscsi_url == NULL) {
|
if (iscsi_url == NULL) {
|
||||||
iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure\n");
|
iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure");
|
||||||
free(str);
|
free(str);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -307,7 +321,7 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
|
|||||||
|
|
||||||
iscsi_url->portal = strdup(portal);
|
iscsi_url->portal = strdup(portal);
|
||||||
if (iscsi_url->portal == NULL) {
|
if (iscsi_url->portal == NULL) {
|
||||||
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup portal string\n");
|
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup portal string");
|
||||||
iscsi_destroy_url(iscsi_url);
|
iscsi_destroy_url(iscsi_url);
|
||||||
free(str);
|
free(str);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -315,7 +329,7 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
|
|||||||
|
|
||||||
iscsi_url->target = strdup(target);
|
iscsi_url->target = strdup(target);
|
||||||
if (iscsi_url->target == NULL) {
|
if (iscsi_url->target == NULL) {
|
||||||
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup target string\n");
|
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup target string");
|
||||||
iscsi_destroy_url(iscsi_url);
|
iscsi_destroy_url(iscsi_url);
|
||||||
free(str);
|
free(str);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -324,7 +338,7 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
|
|||||||
if (user != NULL) {
|
if (user != NULL) {
|
||||||
iscsi_url->user = strdup(user);
|
iscsi_url->user = strdup(user);
|
||||||
if (iscsi_url->user == NULL) {
|
if (iscsi_url->user == NULL) {
|
||||||
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup username string\n");
|
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup username string");
|
||||||
iscsi_destroy_url(iscsi_url);
|
iscsi_destroy_url(iscsi_url);
|
||||||
free(str);
|
free(str);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -334,7 +348,7 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
|
|||||||
if (passwd != NULL) {
|
if (passwd != NULL) {
|
||||||
iscsi_url->passwd = strdup(passwd);
|
iscsi_url->passwd = strdup(passwd);
|
||||||
if (iscsi_url->passwd == NULL) {
|
if (iscsi_url->passwd == NULL) {
|
||||||
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup password string\n");
|
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup password string");
|
||||||
iscsi_destroy_url(iscsi_url);
|
iscsi_destroy_url(iscsi_url);
|
||||||
free(str);
|
free(str);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -369,14 +383,14 @@ iscsi_set_initiator_username_pwd(struct iscsi_context *iscsi,
|
|||||||
free(discard_const(iscsi->user));
|
free(discard_const(iscsi->user));
|
||||||
iscsi->user = strdup(user);
|
iscsi->user = strdup(user);
|
||||||
if (iscsi->user == NULL) {
|
if (iscsi->user == NULL) {
|
||||||
iscsi_set_error(iscsi, "Out-of-memory: failed to strdup username\n");
|
iscsi_set_error(iscsi, "Out-of-memory: failed to strdup username");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(discard_const(iscsi->passwd));
|
free(discard_const(iscsi->passwd));
|
||||||
iscsi->passwd = strdup(passwd);
|
iscsi->passwd = strdup(passwd);
|
||||||
if (iscsi->passwd == NULL) {
|
if (iscsi->passwd == NULL) {
|
||||||
iscsi_set_error(iscsi, "Out-of-memory: failed to strdup password\n");
|
iscsi_set_error(iscsi, "Out-of-memory: failed to strdup password");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user