Improve the parsing of iscsi URLs and validate the input.

Generate better error messages when a badly formed URL is given
and an error string that points to the field there is a problem with.
This commit is contained in:
Ronnie Sahlberg
2010-12-31 09:44:14 +11:00
parent 2b30e3a7fe
commit 46199d2f59
2 changed files with 26 additions and 6 deletions

View File

@@ -232,9 +232,13 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
char *target;
char *lun;
char *tmp;
int l;
if (strncmp(url, "iscsi://", 8)) {
iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must be of the form \"iscsi://[<username>[%%<password>]@]<host>[:<port>]/<target-iqn>/<lun>\"\n", url);
iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must be of "
"the form "
"\"iscsi://[<username>[%%<password>]@]"
"<host>[:<port>]/<target-iqn>/<lun>\"\n", url);
return NULL;
}
@@ -260,7 +264,10 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
target = index(portal, '/');
if (target == NULL) {
iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must be of the form \"iscsi://<host>[:<port>]/<target-iqn>/<lun>\"\n", url);
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse "
"'<target-iqn>'\niSCSI URL must be of the form "
"\"iscsi://[<username>[%%<password>]@]"
"<host>[:<port>]/<target-iqn>/<lun>\"\n", url);
free(str);
return NULL;
}
@@ -268,12 +275,23 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
lun = index(target, '/');
if (lun == NULL) {
iscsi_set_error(iscsi, "Invalid URL %s\niSCSI URL must be of the form \"iscsi://<host>[:<port>]/<target-iqn>/<lun>\"\n", url);
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
"iSCSI URL must be of the form \"iscsi://"
"<host>[:<port>]/<target-iqn>/<lun>\"\n", url);
free(str);
return NULL;
}
*lun++ = 0;
l = strtol(lun, &tmp, 10);
if (*lun == 0 || *tmp != 0) {
iscsi_set_error(iscsi, "Invalid URL %s\nCould not parse <lun>\n"
"iSCSI URL must be of the form \"iscsi://"
"<host>[:<port>]/<target-iqn>/<lun>\"\n",
url);
free(str);
return NULL;
}
iscsi_url = malloc(sizeof(struct iscsi_url));
if (iscsi_url == NULL) {
@@ -319,7 +337,7 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
}
}
iscsi_url->lun = atoi(lun);
iscsi_url->lun = l;
free(str);
return iscsi_url;
}

View File

@@ -194,12 +194,14 @@ int main(int argc, const char *argv[])
if (url == NULL) {
fprintf(stderr, "You must specify the URL\n");
fprintf(stderr, " iscsi://[<username>[%%<password>]@]<host>[:<port>]/<target-iqn>/<lun>\n");
fprintf(stderr, " iscsi://[<username>[%%<password>]@]<host>"
"[:<port>]/<target-iqn>/<lun>\n");
exit(10);
}
iscsi_url = iscsi_parse_full_url(iscsi, url);
if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL : %s %s\n", url, iscsi_get_error(iscsi));
fprintf(stderr, "Failed to parse URL: %s\n",
iscsi_get_error(iscsi));
exit(10);
}