1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-11-18 15:20:56 +03:00
Files
libssh2/tests/test_keyboard_interactive_auth_fails_with_wrong_response.c
Viktor Szakats 2efdb6747a tidy-up: example, tests continued
- fix skip auth if `userauthlist` is NULL.
  Closes #836 (Reported-by: @sudipm-mukherjee on github)
- fix most silenced `checksrc` warnings.
- sync examples/tests code between each other.
  (output messages, error handling, declaration order, comments)
- stop including unnecessary headers.
- always deinitialize in case of error.
- drop some redundant variables.
- add error handling where missing.
- show more error codes.
- switch `perror()` to `fprintf()`.
- fix some `printf()`s to be `fprintf()`.
- formatting.

Closes #960
2023-04-14 11:07:53 +00:00

57 lines
1.7 KiB
C

#include "runner.h"
static const char *USERNAME = "libssh2"; /* set in Dockerfile */
static const char *WRONG_PASSWORD = "i'm not the password";
static void kbd_callback(const char *name, int name_len,
const char *instruct, int instruct_len,
int num_prompts,
const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
void **abstract)
{
int i;
(void)abstract;
fprintf(stdout, "Kb-int name: %.*s\n", name_len, name);
fprintf(stdout, "Kb-int instruction: %.*s\n", instruct_len, instruct);
for(i = 0; i < num_prompts; ++i) {
fprintf(stdout, "Kb-int prompt %d: %.*s\n", i,
(int)prompts[i].length, prompts[i].text);
}
if(num_prompts == 1) {
responses[0].text = strdup(WRONG_PASSWORD);
responses[0].length = (unsigned int)strlen(WRONG_PASSWORD);
}
}
int test(LIBSSH2_SESSION *session)
{
int rc;
const char *userauth_list =
libssh2_userauth_list(session, USERNAME,
(unsigned int)strlen(USERNAME));
if(!userauth_list) {
print_last_session_error("libssh2_userauth_list");
return 1;
}
if(!strstr(userauth_list, "keyboard-interactive")) {
fprintf(stderr,
"'keyboard-interactive' was expected in userauth list: %s\n",
userauth_list);
return 1;
}
rc = libssh2_userauth_keyboard_interactive_ex(
session, USERNAME, (unsigned int)strlen(USERNAME), kbd_callback);
if(rc == 0) {
fprintf(stderr,
"Keyboard-interactive auth succeeded with wrong response\n");
return 1;
}
return 0;
}