init: fix segfaul in iscsi_parse_url

We allowed iscsi to be NULL in iscsi_parse_url. Especially
qemu does this and currently segfaults at start. Change the
usage guidelines for target username/password to be the same
as for chap username/password.

Signed-off-by: Peter Lieven <pl@kamp.de>
This commit is contained in:
Peter Lieven
2015-03-26 10:07:52 +01:00
parent b55ce5cc09
commit a45094b7a7
3 changed files with 23 additions and 30 deletions

View File

@@ -91,6 +91,8 @@ struct iscsi_url {
char target[MAX_STRING_SIZE + 1]; char target[MAX_STRING_SIZE + 1];
char user[MAX_STRING_SIZE + 1]; char user[MAX_STRING_SIZE + 1];
char passwd[MAX_STRING_SIZE + 1]; char passwd[MAX_STRING_SIZE + 1];
char target_user[MAX_STRING_SIZE + 1];
char target_passwd[MAX_STRING_SIZE + 1];
int lun; int lun;
struct iscsi_context *iscsi; struct iscsi_context *iscsi;
}; };

View File

@@ -282,12 +282,8 @@ try_again:
iscsi_set_header_digest(iscsi, old_iscsi->want_header_digest); iscsi_set_header_digest(iscsi, old_iscsi->want_header_digest);
if (old_iscsi->user[0]) { iscsi_set_initiator_username_pwd(iscsi, old_iscsi->user, old_iscsi->passwd);
iscsi_set_initiator_username_pwd(iscsi, old_iscsi->user, old_iscsi->passwd); iscsi_set_target_username_pwd(iscsi, old_iscsi->target_user, old_iscsi->target_passwd);
}
if (old_iscsi->target_user[0]) {
iscsi_set_target_username_pwd(iscsi, old_iscsi->target_user, old_iscsi->target_passwd);
}
iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL); iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);

View File

@@ -453,6 +453,8 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
char *portal; char *portal;
char *user = NULL; char *user = NULL;
char *passwd = NULL; char *passwd = NULL;
char *target_user = NULL;
char *target_passwd = NULL;
char *target = NULL; char *target = NULL;
char *lun; char *lun;
char *tmp; char *tmp;
@@ -474,12 +476,10 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
strncpy(str,url + 8, MAX_STRING_SIZE); strncpy(str,url + 8, MAX_STRING_SIZE);
portal = str; portal = str;
iscsi_set_target_username_pwd(iscsi, user = getenv("LIBISCSI_CHAP_USERNAME");
getenv("LIBISCSI_CHAP_TARGET_USERNAME"), passwd = getenv("LIBISCSI_CHAP_PASSWORD");
getenv("LIBISCSI_CHAP_TARGET_PASSWORD")); target_user = getenv("LIBISCSI_CHAP_TARGET_USERNAME");
target_passwd = getenv("LIBISCSI_CHAP_TARGET_PASSWORD");
user = getenv("LIBISCSI_CHAP_USERNAME");
passwd = getenv("LIBISCSI_CHAP_PASSWORD");
tmp = strchr(portal, '?'); tmp = strchr(portal, '?');
if (tmp) { if (tmp) {
@@ -487,7 +487,6 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
while (tmp && *tmp) { while (tmp && *tmp) {
char *next = strchr(tmp, '&'); char *next = strchr(tmp, '&');
char *key, *value; char *key, *value;
if (next != NULL) { if (next != NULL) {
*next++ = 0; *next++ = 0;
} }
@@ -497,15 +496,9 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
*value++ = 0; *value++ = 0;
} }
if (!strcmp(key, "target_user")) { if (!strcmp(key, "target_user")) {
if (value) { target_user = value;
strncpy(iscsi->target_user,
value, MAX_STRING_SIZE);
}
} else if (!strcmp(key, "target_password")) { } else if (!strcmp(key, "target_password")) {
if (value) { target_passwd = value;
strncpy(iscsi->target_passwd,
value, MAX_STRING_SIZE);
}
} }
tmp = next; tmp = next;
} }
@@ -587,19 +580,16 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE); strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE);
if (!iscsi->target_user[0] || !iscsi->target_passwd[0]) { if (user && passwd && user[0] && passwd[0]) {
iscsi->target_user[0] = 0;
iscsi->target_passwd[0] = 0;
}
if (user != NULL && passwd != NULL) {
strncpy(iscsi_url->user, user, MAX_STRING_SIZE); strncpy(iscsi_url->user, user, MAX_STRING_SIZE);
strncpy(iscsi_url->passwd, passwd, MAX_STRING_SIZE); strncpy(iscsi_url->passwd, passwd, MAX_STRING_SIZE);
} else {
/* if we do not have normal CHAP, that means we do not have /* if we do not have normal CHAP, that means we do not have
* bidirectional either. * bidirectional either.
*/ */
iscsi->target_user[0] = 0; if (target_user && target_passwd && target_user[0] && target_passwd[0]) {
iscsi->target_passwd[0] = 0; strncpy(iscsi_url->target_user, target_user, MAX_STRING_SIZE);
strncpy(iscsi_url->target_passwd, target_passwd, MAX_STRING_SIZE);
}
} }
if (full) { if (full) {
@@ -640,6 +630,11 @@ int
iscsi_set_initiator_username_pwd(struct iscsi_context *iscsi, iscsi_set_initiator_username_pwd(struct iscsi_context *iscsi,
const char *user, const char *passwd) const char *user, const char *passwd)
{ {
if (!user || !passwd || !user[0] || !passwd[0]) {
iscsi->user[0] = 0;
iscsi->passwd[0] = 0;
return 0;
}
strncpy(iscsi->user,user,MAX_STRING_SIZE); strncpy(iscsi->user,user,MAX_STRING_SIZE);
strncpy(iscsi->passwd,passwd,MAX_STRING_SIZE); strncpy(iscsi->passwd,passwd,MAX_STRING_SIZE);
return 0; return 0;
@@ -650,7 +645,7 @@ int
iscsi_set_target_username_pwd(struct iscsi_context *iscsi, iscsi_set_target_username_pwd(struct iscsi_context *iscsi,
const char *user, const char *passwd) const char *user, const char *passwd)
{ {
if (!user || !passwd) { if (!user || !passwd || !user[0] || !passwd[0]) {
iscsi->target_user[0] = 0; iscsi->target_user[0] = 0;
iscsi->target_passwd[0] = 0; iscsi->target_passwd[0] = 0;
return 0; return 0;