[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Need Help in creating SSH Server using LIBSSH


Hello Andreas,

Thanks for your suggestion. I have generated the public and private keys using GITBASH Tool.  The below commands 

ssh-keygen -t dsa -f ssh_host_dsa_key -N ''				--> Worked with GIT BASH
ssh-keygen -t rsa -b 2048 -f ssh_host_rsa_key -N ''.,			-->  It didn't worked with GIT BASH tool

Let me know if you are using any other tools, I set the options as you explained in the mail.  Below is the code .,

#include<iostream>
using namespace std;

#include"libssh/libssh.h"
#include"libssh/server.h"
#include"libssh/callbacks.h"
#include"libssh/legacy.h"
#include"libssh/sftp.h"
#include"libssh/ssh2.h"

#define SSHD_USER		"libssh"
#define SSHD_PASSWORD	"libssh"

#ifndef KEYS_FOLDER
#ifdef _WIN32
#define KEYS_FOLDER "C:\\Users\\inrasun3\\.ssh\\"
#else
#define KEYS_FOLDER "/etc/ssh/"
#endif
#endif

const int port = 22;

static int authenticate(ssh_session session); static int auth_password(const char *user, const char *password); void printerror(const char *pText, const char *pErrorInfo);

int main(void)
{
	int iError = -1;
	int iAuth = 0;
	ssh_session pSshSession = NULL;
	ssh_bind    pSshBind = NULL;
	ssh_message pSshMsg = NULL;
	ssh_channel pSshChannel = NULL;

	iError = ssh_init();
	
	if(iError == -1)
		printerror("SSH init failed", "Reason Unknown");

	pSshSession = ssh_new();		//Create SSH session

	if(pSshSession == NULL)
		printerror("SSH session creation failed", ssh_get_error(pSshBind));

	pSshBind    = ssh_bind_new();   //create a bind object

	if(pSshBind == NULL)
		printerror("SSH session creation failed reason ", ssh_get_error(pSshBind));

	iError = ssh_bind_options_set(pSshBind, SSH_BIND_OPTIONS_BINDPORT, &port);
	iError = ssh_bind_options_set(pSshBind, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key");
	iError = ssh_bind_options_set(pSshBind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "id_rsa");

	if(iError < 0 )
		printerror("Bind options set failed ", ssh_get_error(pSshBind));

	iError = ssh_bind_listen(pSshBind);

	if(iError < 0 )
        printerror("Error listening to socket ", ssh_get_error(pSshBind));

    cout<<"Started sample libssh sshd on port "<<port<<endl;
    cout<<"You can login as the user "<<SSHD_USER<<" with the password "<<SSHD_PASSWORD<<endl;

	iError = ssh_bind_accept(pSshBind, pSshSession);
	//ssh_bind_fd_toaccept(pSshBind);

    if(iError==SSH_ERROR)
	{
		printerror("Error accepting a connection " , ssh_get_error(pSshBind) );
	}

    if (ssh_handle_key_exchange(pSshSession)) {
        printf("ssh_handle_key_exchange: %s\n", ssh_get_error(pSshSession));
        return 1;
    }

	 /* proceed to authentication */
	iAuth = authenticate(pSshSession);
    if(!iAuth)
	{
		printerror("Authentication error: ", ssh_get_error(pSshSession));
        ssh_disconnect(pSshSession);
    }


	ssh_disconnect(pSshSession);
    ssh_bind_free(pSshBind);

    ssh_finalize();
    return 0;
}

Please let me know what I am missing.

Regards
Ashish

-----Original Message-----
From: Andreas Schneider [mailto:asn@xxxxxxxxxxxxxx] 
Sent: Sunday, June 02, 2013 11:32 PM
To: libssh@xxxxxxxxxx
Cc: Ashish Mangla
Subject: Re: Need Help in creating SSH Server using LIBSSH

On Thursday 30 May 2013 09:15:07 Ashish Mangla wrote:
> Hi
> 
> I have created a SSH server using latest release of LIBSSH library the 
> targeted environment is Windows 7 64-bit, Visual Studio 2010, but I am 
> not successful in getting it work. Despite of many trails still I am 
> getting a error "Could n't create BIO". I browsed through internet and 
> come to know that the key folder must have read access. This I checked it and there is
> no problem in reading the problem ( I wrote a sample C++ program   whether
> I can open file or not. The program is successful in reading the file 
> (of course in Binary )). Now I am helpless in finding the reason for the error.
> Here is what options I have set please let me know where I am doing wrong.
> 
> If any sample program for SSH server on windows is really helpful.

First you need to create a rsa and dsa host key.

ssh-keygen -t dsa -f ssh_host_dsa_key -N ''
ssh-keygen -t rsa -b 2048 -f ssh_host_rsa_key -N ''

After that you need to set them with

ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, "/path/to/ssh_host_dsa_key"); ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, "/path/to/ssh_host_rsa_key");

Have you done that?

> I have set the bind options with DSA and RSA key and the folder name 
> given as "C:\Users\ravikumarS\.ssh\ida_rsa.pub".

Well it expects a private key not a public key ...

-- 
Andreas Schneider                   GPG-ID: F33E3FC6
www.cryptomilk.org                asn@xxxxxxxxxxxxxx



Follow-Ups:
Re: Need Help in creating SSH Server using LIBSSHAndreas Schneider <asn@xxxxxxxxxxxxxx>
References:
Re: Need Help in creating SSH Server using LIBSSHAndreas Schneider <asn@xxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org