diff --git a/lib/iscsi/iscsi.c b/lib/iscsi/iscsi.c index 258be6dced..6ae0515908 100644 --- a/lib/iscsi/iscsi.c +++ b/lib/iscsi/iscsi.c @@ -40,7 +40,6 @@ #include "spdk/trace.h" #include "spdk/string.h" #include "spdk/queue.h" -#include "spdk/conf.h" #include "spdk/net.h" #include "iscsi/md5.h" @@ -677,91 +676,6 @@ spdk_iscsi_append_param(struct spdk_iscsi_conn *conn, const char *key, return rc; } -static int -spdk_iscsi_chap_get_authinfo(struct iscsi_chap_auth *auth, const char *authfile, - const char *authuser, int ag_tag) -{ - struct spdk_conf *config = NULL; - struct spdk_conf_section *sp; - const char *val; - const char *user, *muser; - const char *secret, *msecret; - int rc; - int i; - - if (auth->user != NULL) { - free(auth->user); - free(auth->secret); - free(auth->muser); - free(auth->msecret); - auth->user = auth->secret = NULL; - auth->muser = auth->msecret = NULL; - } - - /* read config files */ - config = spdk_conf_allocate(); - if (config == NULL) { - SPDK_ERRLOG("allocate config fail\n"); - return -1; - } - rc = spdk_conf_read(config, authfile); - if (rc < 0) { - SPDK_ERRLOG("auth conf error\n"); - spdk_conf_free(config); - return -1; - } - //spdk_conf_print(config); - - sp = spdk_conf_first_section(config); - while (sp != NULL) { - if (spdk_conf_section_match_prefix(sp, "AuthGroup")) { - int group = spdk_conf_section_get_num(sp); - if (group == 0) { - SPDK_ERRLOG("Group 0 is invalid\n"); - spdk_conf_free(config); - return -1; - } - if (ag_tag != group) { - goto skip_ag_tag; - } - - val = spdk_conf_section_get_val(sp, "Comment"); - if (val != NULL) { - SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Comment %s\n", val); - } - for (i = 0; ; i++) { - val = spdk_conf_section_get_nval(sp, "Auth", i); - if (val == NULL) { - break; - } - user = spdk_conf_section_get_nmval(sp, "Auth", i, 0); - secret = spdk_conf_section_get_nmval(sp, "Auth", i, 1); - muser = spdk_conf_section_get_nmval(sp, "Auth", i, 2); - msecret = spdk_conf_section_get_nmval(sp, "Auth", i, 3); - if (user != NULL) { - if (strcasecmp(authuser, user) == 0) { - /* match user */ - auth->user = xstrdup(user); - auth->secret = xstrdup(secret); - auth->muser = xstrdup(muser); - auth->msecret = xstrdup(msecret); - spdk_conf_free(config); - return 0; - } - } else { - SPDK_ERRLOG("Invalid Auth format, skip this line\n"); - continue; - } - } - } -skip_ag_tag: - sp = spdk_conf_next_section(sp); - } - - spdk_conf_free(config); - return 0; -} - static int spdk_iscsi_get_authinfo(struct spdk_iscsi_conn *conn, const char *authuser) { diff --git a/lib/iscsi/iscsi.h b/lib/iscsi/iscsi.h index f3fe55c66e..7a72b0a29e 100644 --- a/lib/iscsi/iscsi.h +++ b/lib/iscsi/iscsi.h @@ -366,6 +366,8 @@ struct spdk_iscsi_opts *spdk_iscsi_opts_copy(struct spdk_iscsi_opts *src); void spdk_iscsi_opts_info_json(struct spdk_json_write_ctx *w); int spdk_iscsi_set_discovery_auth(bool disable_chap, bool require_chap, bool mutual_chap, int32_t chap_group); +int spdk_iscsi_chap_get_authinfo(struct iscsi_chap_auth *auth, const char *authfile, + const char *authuser, int ag_tag); void spdk_iscsi_send_nopin(struct spdk_iscsi_conn *conn); void spdk_iscsi_task_response(struct spdk_iscsi_conn *conn, diff --git a/lib/iscsi/iscsi_subsystem.c b/lib/iscsi/iscsi_subsystem.c index 23c051e8ad..f1264807e7 100644 --- a/lib/iscsi/iscsi_subsystem.c +++ b/lib/iscsi/iscsi_subsystem.c @@ -774,6 +774,92 @@ spdk_iscsi_set_discovery_auth(bool disable_chap, bool require_chap, bool mutual_ return 0; } +int +spdk_iscsi_chap_get_authinfo(struct iscsi_chap_auth *auth, const char *authfile, + const char *authuser, int ag_tag) +{ + struct spdk_conf *config = NULL; + struct spdk_conf_section *sp; + const char *val; + const char *user, *muser; + const char *secret, *msecret; + int rc; + int i; + + if (auth->user != NULL) { + free(auth->user); + free(auth->secret); + free(auth->muser); + free(auth->msecret); + auth->user = auth->secret = NULL; + auth->muser = auth->msecret = NULL; + } + + /* read config files */ + config = spdk_conf_allocate(); + if (config == NULL) { + SPDK_ERRLOG("allocate config fail\n"); + return -1; + } + + rc = spdk_conf_read(config, authfile); + if (rc < 0) { + SPDK_ERRLOG("auth conf error\n"); + spdk_conf_free(config); + return -1; + } + //spdk_conf_print(config); + + sp = spdk_conf_first_section(config); + while (sp != NULL) { + if (spdk_conf_section_match_prefix(sp, "AuthGroup")) { + int group = spdk_conf_section_get_num(sp); + if (group == 0) { + SPDK_ERRLOG("Group 0 is invalid\n"); + spdk_conf_free(config); + return -1; + } + if (ag_tag != group) { + goto skip_ag_tag; + } + + val = spdk_conf_section_get_val(sp, "Comment"); + if (val != NULL) { + SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "Comment %s\n", val); + } + for (i = 0; ; i++) { + val = spdk_conf_section_get_nval(sp, "Auth", i); + if (val == NULL) { + break; + } + user = spdk_conf_section_get_nmval(sp, "Auth", i, 0); + secret = spdk_conf_section_get_nmval(sp, "Auth", i, 1); + muser = spdk_conf_section_get_nmval(sp, "Auth", i, 2); + msecret = spdk_conf_section_get_nmval(sp, "Auth", i, 3); + if (user != NULL) { + if (strcasecmp(authuser, user) == 0) { + /* match user */ + auth->user = xstrdup(user); + auth->secret = xstrdup(secret); + auth->muser = xstrdup(muser); + auth->msecret = xstrdup(msecret); + spdk_conf_free(config); + return 0; + } + } else { + SPDK_ERRLOG("Invalid Auth format, skip this line\n"); + continue; + } + } + } +skip_ag_tag: + sp = spdk_conf_next_section(sp); + } + + spdk_conf_free(config); + return 0; +} + static int spdk_iscsi_initialize_global_params(void) { diff --git a/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c b/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c index 310b6d20b3..ae930ef201 100644 --- a/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c +++ b/test/unit/lib/iscsi/iscsi.c/iscsi_ut.c @@ -96,6 +96,13 @@ spdk_iscsi_conn_free_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pd { } +int +spdk_iscsi_chap_get_authinfo(struct iscsi_chap_auth *auth, const char *authfile, + const char *authuser, int ag_tag) +{ + return 0; +} + int spdk_scsi_lun_get_id(const struct spdk_scsi_lun *lun) {