1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-07 08:02:55 +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:
Andreas Schneider
2011-01-08 10:17:37 +01:00
parent 4f83918109
commit d1e1103198
2 changed files with 64 additions and 37 deletions

View File

@@ -25,44 +25,71 @@ 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);
while (err == SSH_AUTH_INFO) {
const char *instruction;
const char *name;
char buffer[128]; char buffer[128];
int i,n; int i, n;
name = ssh_userauth_kbdint_getname(session);
instruction = ssh_userauth_kbdint_getinstruction(session);
n = ssh_userauth_kbdint_getnprompts(session);
if (name && strlen(name) > 0) {
printf("%s\n", name);
}
if (instruction && strlen(instruction) > 0) {
printf("%s\n", instruction);
}
for (i = 0; i < n; i++) {
const char *answer;
const char *prompt;
char echo; char echo;
while (err==SSH_AUTH_INFO){
name=ssh_userauth_kbdint_getname(session); prompt = ssh_userauth_kbdint_getprompt(session, i, &echo);
instruction=ssh_userauth_kbdint_getinstruction(session); if (prompt == NULL) {
n=ssh_userauth_kbdint_getnprompts(session); break;
if(strlen(name)>0) }
printf("%s\n",name);
if(strlen(instruction)>0) if (echo) {
printf("%s\n",instruction); char *p;
for(i=0;i<n;++i){
prompt=ssh_userauth_kbdint_getprompt(session,i,&echo); printf("%s", prompt);
if(echo){
printf("%s",prompt); if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
if (fgets(buffer,sizeof(buffer),stdin) == NULL) {
return SSH_AUTH_ERROR; return SSH_AUTH_ERROR;
} }
buffer[sizeof(buffer)-1]=0;
if((ptr=strchr(buffer,'\n'))) buffer[sizeof(buffer) - 1] = '\0';
*ptr=0; if ((p = strchr(buffer, '\n'))) {
if (ssh_userauth_kbdint_setanswer(session,i,buffer) < 0) { *p = '\0';
}
if (ssh_userauth_kbdint_setanswer(session, i, buffer) < 0) {
return SSH_AUTH_ERROR; return SSH_AUTH_ERROR;
} }
memset(buffer,0,strlen(buffer));
memset(buffer, 0, strlen(buffer));
} else { } else {
ptr=getpass(prompt); if (password && strstr(prompt, "Password:")) {
if (ssh_userauth_kbdint_setanswer(session,i,ptr) < 0) { answer = password;
} else {
answer = getpass(prompt);
}
if (ssh_userauth_kbdint_setanswer(session, i, answer) < 0) {
return SSH_AUTH_ERROR; return SSH_AUTH_ERROR;
} }
} }
} }
err=ssh_userauth_kbdint(session,NULL,NULL); err=ssh_userauth_kbdint(session,NULL,NULL);
} }
return err; return err;
} }
@@ -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;

View File

@@ -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);