aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2009-11-07 03:45:27 +0100
committerAris Adamantiadis <aris@0xbadc0de.be>2009-11-07 03:45:27 +0100
commit3a95ecfe74ee770f6c0045768abe16faf29ca1aa (patch)
tree76f6170621f8a1bc090d3e49c6bee1547458a42a /examples
parent14ae9a7f0c7ce7a9fa2972ef29c1c8cb86242d4d (diff)
downloadlibssh-3a95ecfe74ee770f6c0045768abe16faf29ca1aa.tar.gz
libssh-3a95ecfe74ee770f6c0045768abe16faf29ca1aa.tar.xz
libssh-3a95ecfe74ee770f6c0045768abe16faf29ca1aa.zip
move authentication out of sampe.c
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt2
-rw-r--r--examples/sample.c82
2 files changed, 7 insertions, 77 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index c62935c..3e7b6dc 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -13,7 +13,7 @@ include_directories(
add_executable(libssh_scp libssh_scp.c ${examples_SRCS})
add_executable(scp_download scp_download.c ${examples_SRCS})
-add_executable(samplessh sample.c)
+add_executable(samplessh sample.c ${examples_SRCS})
target_link_libraries(libssh_scp ${LIBSSH_SHARED_LIBRARY})
target_link_libraries(scp_download ${LIBSSH_SHARED_LIBRARY})
diff --git a/examples/sample.c b/examples/sample.c
index 1768351..3f827f0 100644
--- a/examples/sample.c
+++ b/examples/sample.c
@@ -33,6 +33,7 @@ clients must be made or how a client should react.
#include <fcntl.h>
+#include "examples_common.h"
#define MAXCMD 10
char *host;
char *user;
@@ -305,49 +306,9 @@ static void batch_shell(ssh_session session){
select_loop(session,channel);
}
-static int auth_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);
- fgets(buffer,sizeof(buffer),stdin);
- 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;
- }
- memset(buffer,0,strlen(buffer));
- } else {
- ptr=getpass(prompt);
- if (ssh_userauth_kbdint_setanswer(session,i,ptr) < 0) {
- return SSH_AUTH_ERROR;
- }
- }
- }
- err=ssh_userauth_kbdint(session,NULL,NULL);
- }
- return err;
-}
-
int main(int argc, char **argv){
ssh_session session;
int auth=0;
- char *password;
char *banner;
char *hexa;
int state;
@@ -449,47 +410,16 @@ int main(int argc, char **argv){
free(hash);
ssh_userauth_none(session, NULL);
-
- auth = ssh_auth_list(session);
- printf("supported auth methods: ");
- if (auth & SSH_AUTH_METHOD_PUBLICKEY) {
- printf("publickey");
- }
- if (auth & SSH_AUTH_METHOD_INTERACTIVE) {
- printf(", keyboard-interactive");
- }
- printf("\n");
-
- /* no ? you should :) */
- auth=ssh_userauth_autopubkey(session, NULL);
- if(auth==SSH_AUTH_ERROR){
- fprintf(stderr,"Authenticating with pubkey: %s\n",ssh_get_error(session));
- ssh_finalize();
- return -1;
- }
banner=ssh_get_issue_banner(session);
if(banner){
printf("%s\n",banner);
free(banner);
}
- if(auth!=SSH_AUTH_SUCCESS){
- auth=auth_kbdint(session);
- if(auth==SSH_AUTH_ERROR){
- fprintf(stderr,"authenticating with keyb-interactive: %s\n",
- ssh_get_error(session));
- ssh_finalize();
- return -1;
- }
- }
- if(auth!=SSH_AUTH_SUCCESS){
- password=getpass("Password: ");
- if(ssh_userauth_password(session,NULL,password) != SSH_AUTH_SUCCESS){
- fprintf(stderr,"Authentication failed: %s\n",ssh_get_error(session));
- ssh_disconnect(session);
- ssh_finalize();
- return -1;
- }
- memset(password,0,strlen(password));
+ auth=authenticate_console(session);
+ if(auth != SSH_AUTH_SUCCESS){
+ ssh_disconnect(session);
+ ssh_free(session);
+ return EXIT_FAILURE;
}
ssh_log(session, SSH_LOG_FUNCTIONS, "Authentication success");
if(!cmds[0])