diff options
author | Andreas Schneider <asn@cynapses.org> | 2011-01-08 10:17:37 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cynapses.org> | 2011-01-08 10:18:55 +0100 |
commit | d1e1103198f111a0f501c15480b81162b9f77a42 (patch) | |
tree | 77feba0d725a9fcbc84408e8d0b851d3946d62b1 /examples | |
parent | 4f83918109df303da8487e1aa30ce82a04f91793 (diff) | |
download | libssh-d1e1103198f111a0f501c15480b81162b9f77a42.tar.gz libssh-d1e1103198f111a0f501c15480b81162b9f77a42.tar.xz libssh-d1e1103198f111a0f501c15480b81162b9f77a42.zip |
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.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/authentication.c | 99 | ||||
-rw-r--r-- | examples/examples_common.h | 2 |
2 files changed, 64 insertions, 37 deletions
diff --git a/examples/authentication.c b/examples/authentication.c index 3add1b2..eb75504 100644 --- a/examples/authentication.c +++ b/examples/authentication.c @@ -25,45 +25,72 @@ clients must be made or how a client should react. #include <libssh/libssh.h> #include "examples_common.h" -int authenticate_kbdint(ssh_session session){ - int err=ssh_userauth_kbdint(session,NULL,NULL); - const char *name, *instruction, *prompt; - char *ptr; - char buffer[128]; - int i,n; - char echo; - while (err==SSH_AUTH_INFO){ - name=ssh_userauth_kbdint_getname(session); - instruction=ssh_userauth_kbdint_getinstruction(session); - n=ssh_userauth_kbdint_getnprompts(session); - if(strlen(name)>0) - printf("%s\n",name); - if(strlen(instruction)>0) - printf("%s\n",instruction); - for(i=0;i<n;++i){ - prompt=ssh_userauth_kbdint_getprompt(session,i,&echo); - if(echo){ - printf("%s",prompt); - if (fgets(buffer,sizeof(buffer),stdin) == NULL) { - return SSH_AUTH_ERROR; +int authenticate_kbdint(ssh_session session, const char *password) { + int err; + + err = ssh_userauth_kbdint(session, NULL, NULL); + while (err == SSH_AUTH_INFO) { + const char *instruction; + const char *name; + char buffer[128]; + 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); } - buffer[sizeof(buffer)-1]=0; - if((ptr=strchr(buffer,'\n'))) - *ptr=0; - if (ssh_userauth_kbdint_setanswer(session,i,buffer) < 0) { - return SSH_AUTH_ERROR; + + if (instruction && strlen(instruction) > 0) { + printf("%s\n", instruction); } - memset(buffer,0,strlen(buffer)); - } else { - ptr=getpass(prompt); - if (ssh_userauth_kbdint_setanswer(session,i,ptr) < 0) { - return SSH_AUTH_ERROR; + + for (i = 0; i < n; i++) { + const char *answer; + const char *prompt; + 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){ @@ -99,7 +126,7 @@ int authenticate_console(ssh_session session){ // Try to authenticate with keyboard interactive"; if (method & SSH_AUTH_METHOD_INTERACTIVE) { - rc = authenticate_kbdint(session); + rc = authenticate_kbdint(session, NULL); if (rc == SSH_AUTH_ERROR) { error(session); return rc; diff --git a/examples/examples_common.h b/examples/examples_common.h index bc30c93..13eb455 100644 --- a/examples/examples_common.h +++ b/examples/examples_common.h @@ -15,7 +15,7 @@ clients must be made or how a client should react. #include <libssh/libssh.h> 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); ssh_session connect_ssh(const char *hostname, const char *user, int verbosity); |