# Fail to login with libssh2



## SIFE (Oct 27, 2010)

I write this piece of code:

```
#include<stdio.h>
#include<stdlib.h>
#include<libssh2.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>

#define PORT 22

main()
 {
  int ss, sshsock, lstat; 
  char *ulogin = "tst";
  char *password = "pass";
  char *ADDR = "127.0.0.1";
  struct sockaddr_in sin;
  LIBSSH2_SESSION *session;
  sshsock = socket(AF_INET, SOCK_STREAM, 0);
  sin.sin_family = AF_INET;
  sin.sin_port = htons(PORT);
  sin.sin_addr.s_addr = inet_addr(ADDR);
  connect(sshsock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in));
  session = libssh2_session_init();
  if(session == NULL)
   {
    printf("Fail to initial SSH session\n");
    exit(0);
   }
  ss = libssh2_session_startup(session, sshsock);
  if(ss < 0)
   {
    printf("Fail to startup SSH session\n");
     exit(0);
   }
  lstat = libssh2_userauth_password_ex(session, ulogin, strlen(ulogin), password, strlen(password), NULL);
  if(lstat == 0)
   printf("Login successed\n");
  else
   printf("Invalid password\n");
  /* disconnect from ssh server */ 
  libssh2_session_disconnect(session, 0);
  /* free ssh session */
  libssh2_session_free(session);
  /* exit from libssh2 */
  libssh2_exit();
  /* close descriptor of socket */
  close(sshsock);
  return 0;
 }
```
I compile it like this:

```
gcc -o sshc sshc.c -I/usr/local/include -L/usr/local/lib -lssh2
```
Now I  execute it I get this error:

```
Invalid password
```
I had already setup account *tst* with password *pass*, I also I enabled ssh server.


----------



## SIFE (Nov 4, 2010)

After reading RFC 4256, I get to know a new method of authentication to me called "keyboard-interactive", I change my code to:

```
#include<stdio.h>
#include<stdlib.h>
#include<libssh2.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>

#define PORT 22

const char *ulogin = "test";
const char *password = NULL;

static void kbd_callback(const char *name, int name_len,
                         const char *instruction, int instruction_len,
                         int num_prompts,
                         const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
                         LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
                         void **abstract)
{
    (void)name;
    (void)name_len;
    (void)instruction;
    (void)instruction_len;
    if (num_prompts == 1) {
        responses[0].text = strdup(password);
        responses[0].length = strlen(password);
    }
    (void)prompts;
    (void)abstract;
}
main(int argc, char **argv)
 {
  int ss, sshsock, lstat;
  char *ADDR = "127.0.0.1";
  char *lauth=NULL;
  struct sockaddr_in sin;
  LIBSSH2_SESSION *session;
  password=argv[1];
  sshsock = socket(AF_INET, SOCK_STREAM, 0);
  sin.sin_family = AF_INET;
  sin.sin_port = htons(PORT);
  sin.sin_addr.s_addr = inet_addr(ADDR);
  connect(sshsock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in));
  session = libssh2_session_init();
  if(session == NULL)
   {
    printf("Fail to initial SSH session\n");
    exit(0);
   }
  ss = libssh2_session_startup(session, sshsock);
  if(ss < 0)
   {
    printf("Fail to startup SSH session\n");
     exit(0);
   }
  lauth = libssh2_userauth_list(session, ulogin, strlen(ulogin));
  printf("List of authentication list: %s\n", lauth);
  lstat = libssh2_userauth_keyboard_interactive_ex(session, ulogin, strlen(ulogin), &kbd_callback);
  if(lstat == 0)
   printf("Login successed\n");
  else
   printf("Invalid password\n");
  /* disconnect from ssh server */ 
  libssh2_session_disconnect(session, 0);
  /* free ssh session */
  libssh2_session_free(session);
  /* exit from libssh2 */
  libssh2_exit();
  /* close descriptor of socket */
  close(sshsock);
  return 0;
 }
```
Note: FreeBSD and Mac-OS using keyboard-interactive as method of authentication with SSH pam, most Linux use password as method of authentication.
Now its work.


----------

