mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-08-05 20:55:46 +03:00
examples: Improve the authenticate_kbdint function.
The function excepts a predefined password now. It will try to use it if the prompt is a Password prompt. This works in 80% of the cases.
This commit is contained in:
@@ -25,45 +25,72 @@ clients must be made or how a client should react.
|
|||||||
#include <libssh/libssh.h>
|
#include <libssh/libssh.h>
|
||||||
#include "examples_common.h"
|
#include "examples_common.h"
|
||||||
|
|
||||||
int authenticate_kbdint(ssh_session session){
|
int authenticate_kbdint(ssh_session session, const char *password) {
|
||||||
int err=ssh_userauth_kbdint(session,NULL,NULL);
|
int err;
|
||||||
const char *name, *instruction, *prompt;
|
|
||||||
char *ptr;
|
err = ssh_userauth_kbdint(session, NULL, NULL);
|
||||||
char buffer[128];
|
while (err == SSH_AUTH_INFO) {
|
||||||
int i,n;
|
const char *instruction;
|
||||||
char echo;
|
const char *name;
|
||||||
while (err==SSH_AUTH_INFO){
|
char buffer[128];
|
||||||
name=ssh_userauth_kbdint_getname(session);
|
int i, n;
|
||||||
instruction=ssh_userauth_kbdint_getinstruction(session);
|
|
||||||
n=ssh_userauth_kbdint_getnprompts(session);
|
name = ssh_userauth_kbdint_getname(session);
|
||||||
if(strlen(name)>0)
|
instruction = ssh_userauth_kbdint_getinstruction(session);
|
||||||
printf("%s\n",name);
|
n = ssh_userauth_kbdint_getnprompts(session);
|
||||||
if(strlen(instruction)>0)
|
|
||||||
printf("%s\n",instruction);
|
if (name && strlen(name) > 0) {
|
||||||
for(i=0;i<n;++i){
|
printf("%s\n", name);
|
||||||
prompt=ssh_userauth_kbdint_getprompt(session,i,&echo);
|
|
||||||
if(echo){
|
|
||||||
printf("%s",prompt);
|
|
||||||
if (fgets(buffer,sizeof(buffer),stdin) == NULL) {
|
|
||||||
return SSH_AUTH_ERROR;
|
|
||||||
}
|
}
|
||||||
buffer[sizeof(buffer)-1]=0;
|
|
||||||
if((ptr=strchr(buffer,'\n')))
|
if (instruction && strlen(instruction) > 0) {
|
||||||
*ptr=0;
|
printf("%s\n", instruction);
|
||||||
if (ssh_userauth_kbdint_setanswer(session,i,buffer) < 0) {
|
|
||||||
return SSH_AUTH_ERROR;
|
|
||||||
}
|
}
|
||||||
memset(buffer,0,strlen(buffer));
|
|
||||||
} else {
|
for (i = 0; i < n; i++) {
|
||||||
ptr=getpass(prompt);
|
const char *answer;
|
||||||
if (ssh_userauth_kbdint_setanswer(session,i,ptr) < 0) {
|
const char *prompt;
|
||||||
return SSH_AUTH_ERROR;
|
char echo;
|
||||||
|
|
||||||
|
prompt = ssh_userauth_kbdint_getprompt(session, i, &echo);
|
||||||
|
if (prompt == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (echo) {
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
printf("%s", prompt);
|
||||||
|
|
||||||
|
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
|
||||||
|
return SSH_AUTH_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[sizeof(buffer) - 1] = '\0';
|
||||||
|
if ((p = strchr(buffer, '\n'))) {
|
||||||
|
*p = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ssh_userauth_kbdint_setanswer(session, i, buffer) < 0) {
|
||||||
|
return SSH_AUTH_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(buffer, 0, strlen(buffer));
|
||||||
|
} else {
|
||||||
|
if (password && strstr(prompt, "Password:")) {
|
||||||
|
answer = password;
|
||||||
|
} else {
|
||||||
|
answer = getpass(prompt);
|
||||||
|
}
|
||||||
|
if (ssh_userauth_kbdint_setanswer(session, i, answer) < 0) {
|
||||||
|
return SSH_AUTH_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
err=ssh_userauth_kbdint(session,NULL,NULL);
|
||||||
}
|
}
|
||||||
err=ssh_userauth_kbdint(session,NULL,NULL);
|
|
||||||
}
|
return err;
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void error(ssh_session session){
|
static void error(ssh_session session){
|
||||||
@@ -99,7 +126,7 @@ int authenticate_console(ssh_session session){
|
|||||||
|
|
||||||
// Try to authenticate with keyboard interactive";
|
// Try to authenticate with keyboard interactive";
|
||||||
if (method & SSH_AUTH_METHOD_INTERACTIVE) {
|
if (method & SSH_AUTH_METHOD_INTERACTIVE) {
|
||||||
rc = authenticate_kbdint(session);
|
rc = authenticate_kbdint(session, NULL);
|
||||||
if (rc == SSH_AUTH_ERROR) {
|
if (rc == SSH_AUTH_ERROR) {
|
||||||
error(session);
|
error(session);
|
||||||
return rc;
|
return rc;
|
||||||
|
@@ -15,7 +15,7 @@ clients must be made or how a client should react.
|
|||||||
|
|
||||||
#include <libssh/libssh.h>
|
#include <libssh/libssh.h>
|
||||||
int authenticate_console(ssh_session session);
|
int authenticate_console(ssh_session session);
|
||||||
int authenticate_kbdint(ssh_session session);
|
int authenticate_kbdint(ssh_session session, const char *password);
|
||||||
int verify_knownhost(ssh_session session);
|
int verify_knownhost(ssh_session session);
|
||||||
ssh_session connect_ssh(const char *hostname, const char *user, int verbosity);
|
ssh_session connect_ssh(const char *hostname, const char *user, int verbosity);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user