aboutsummaryrefslogtreecommitdiff
path: root/tests/authentication.c
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2008-03-17 02:31:22 +0000
committerAris Adamantiadis <aris@0xbadc0de.be>2008-03-17 02:31:22 +0000
commit46b96a145cde14b7ecf4a9608173508aa877b9fa (patch)
treee8ae1288f955d0d09fd5ebb88b2e5edd4487e841 /tests/authentication.c
parentebbca06b045cec6367deb4eb7a947cde2090dedc (diff)
downloadlibssh-46b96a145cde14b7ecf4a9608173508aa877b9fa.tar.gz
libssh-46b96a145cde14b7ecf4a9608173508aa877b9fa.tar.xz
libssh-46b96a145cde14b7ecf4a9608173508aa877b9fa.zip
preliminary test suite
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@153 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'tests/authentication.c')
-rw-r--r--tests/authentication.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/authentication.c b/tests/authentication.c
new file mode 100644
index 00000000..00abd052
--- /dev/null
+++ b/tests/authentication.c
@@ -0,0 +1,74 @@
+/*
+This file is distributed in public domain. You can do whatever you want
+with its content.
+*/
+
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libssh/libssh.h>
+
+#include "tests.h"
+static int auth_kbdint(SSH_SESSION *session){
+ int err=ssh_userauth_kbdint(session,NULL,NULL);
+ char *name,*instruction,*prompt,*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;
+ ssh_userauth_kbdint_setanswer(session,i,buffer);
+ memset(buffer,0,strlen(buffer));
+ } else {
+ ptr=getpass(prompt);
+ ssh_userauth_kbdint_setanswer(session,i,ptr);
+ }
+ }
+ err=ssh_userauth_kbdint(session,NULL,NULL);
+ }
+ return err;
+}
+
+int authenticate (SSH_SESSION *session){
+ int auth=ssh_userauth_autopubkey(session);
+ char *password;
+ if(auth==SSH_AUTH_ERROR){
+ fprintf(stderr,"Authenticating with pubkey: %s\n",ssh_get_error(session));
+ return auth;
+ }
+ 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));
+ return auth;
+ }
+ }
+ if(auth!=SSH_AUTH_SUCCESS){
+ password=getpass("Password : ");
+ auth = ssh_userauth_password(session,NULL,password);
+ memset(password,0,strlen(password));
+ if (auth==SSH_AUTH_ERROR){
+ fprintf(stderr,"Authentication with password failed: %s\n",ssh_get_error(session));
+ return auth;
+ }
+ }
+ return auth;
+}