virNetLibsshAuthenticateKeyboardInteractive: Use virAuthAskCredential

Rework the code to use the new helper instead of open coding the auth
callback interaction.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
This commit is contained in:
Peter Krempa 2022-12-08 16:04:13 +01:00
parent 95f4879e97
commit d9bdfe4e21

View File

@ -647,26 +647,17 @@ virNetLibsshAuthenticateKeyboardInteractive(virNetLibsshSession *sess,
virBufferAddChar(&buff, '\n'); virBufferAddChar(&buff, '\n');
for (iprompt = 0; iprompt < nprompts; ++iprompt) { for (iprompt = 0; iprompt < nprompts; ++iprompt) {
virConnectCredential retr_passphrase;
const char *promptStr; const char *promptStr;
int promptStrLen; int promptStrLen;
char echo; char echo;
char *prompt = NULL; g_autofree char *prompt = NULL;
int cred_type; g_autoptr(virConnectCredential) cred = NULL;
/* get the prompt */ /* get the prompt */
promptStr = ssh_userauth_kbdint_getprompt(sess->session, iprompt, promptStr = ssh_userauth_kbdint_getprompt(sess->session, iprompt,
&echo); &echo);
promptStrLen = virLengthForPromptString(promptStr); promptStrLen = virLengthForPromptString(promptStr);
cred_type = virCredTypeForPrompt(sess->cred, echo);
if (cred_type == -1) {
virReportError(VIR_ERR_LIBSSH, "%s",
_("no suitable callback for input of keyboard "
"response"));
goto prompt_error;
}
/* create the prompt for the user, using the instruction /* create the prompt for the user, using the instruction
* buffer if specified * buffer if specified
*/ */
@ -681,42 +672,18 @@ virNetLibsshAuthenticateKeyboardInteractive(virNetLibsshSession *sess,
prompt = g_strndup(promptStr, promptStrLen); prompt = g_strndup(promptStr, promptStrLen);
} }
memset(&retr_passphrase, 0, sizeof(virConnectCredential)); if (!(cred = virAuthAskCredential(sess->cred, prompt, echo)))
retr_passphrase.type = cred_type; return SSH_AUTH_ERROR;
retr_passphrase.prompt = prompt;
if (retr_passphrase.type == -1) { if (ssh_userauth_kbdint_setanswer(sess->session, iprompt,
virReportError(VIR_ERR_LIBSSH, "%s", cred->result) < 0) {
_("no suitable callback for input of key "
"passphrase"));
goto prompt_error;
}
if (sess->cred->cb(&retr_passphrase, 1, sess->cred->cbdata)) {
virReportError(VIR_ERR_LIBSSH, "%s",
_("failed to retrieve keyboard interactive "
"result: callback has failed"));
goto prompt_error;
}
VIR_FREE(prompt);
ret = ssh_userauth_kbdint_setanswer(sess->session, iprompt,
retr_passphrase.result);
virSecureEraseString(retr_passphrase.result);
g_free(retr_passphrase.result);
if (ret < 0) {
errmsg = ssh_get_error(sess->session); errmsg = ssh_get_error(sess->session);
virReportError(VIR_ERR_AUTH_FAILED, virReportError(VIR_ERR_AUTH_FAILED,
_("authentication failed: %s"), errmsg); _("authentication failed: %s"), errmsg);
goto prompt_error; return SSH_AUTH_ERROR;
} }
continue; continue;
prompt_error:
VIR_FREE(prompt);
return SSH_AUTH_ERROR;
} }
ret = ssh_userauth_kbdint(sess->session, NULL, NULL); ret = ssh_userauth_kbdint(sess->session, NULL, NULL);