Reputation: 317
I am using the libssh library for creating a connection to an ssh server created on a remote server. Everything goes well with the connection until the code gets the public key from the server and asks user if he wants to save it or not. There the code crashes for some unknown reason. I am using the tutorial given at this link.
Here is the code that I've written so far. Any help regarding this would be great.
#include <libssh/libssh.h>
#include <iostream>
using namespace std;
int verifyServer(ssh_session);
int main(){
ssh_session my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
int verbosity = SSH_LOG_PROTOCOL;
int port = 22;
int connect;
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "hostname");
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "username");
connect = ssh_connect(my_ssh_session);
if(connect != SSH_OK){
cout<<"Connection error!"<<endl;
ssh_free(my_ssh_session);
exit(-1);
}
int result = verifyServer(my_ssh_session);
if(result < 0){
ssh_disconnect(my_ssh_session);
exit(-1);
}
connect = ssh_userauth_password(my_ssh_session, NULL, "password");
if(connect != SSH_AUTH_SUCCESS){
cout<<"Password authentication failed!"<<endl;
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
exit(-1);
}
ssh_free(my_ssh_session);
}
int verifyServer(ssh_session session){
int state, hlen;
unsigned char* hash = NULL;
char* hexa;
char buffer[10];
state = ssh_is_server_known(session);
hlen = ssh_get_pubkey_hash(session, &hash);
if(hlen < 0){
cout<<"Server public key hash error!"<<endl;
return -1;
}
switch(state){
case SSH_SERVER_KNOWN_OK:
break;
case SSH_SERVER_KNOWN_CHANGED:
cout<<"Host key for the server has been changed to: \n";
ssh_print_hexa("Public Key Hash", hash, hlen);
cout<<"For security reasons, connection will now be stopped.\n";
free(hash);
return -1;
case SSH_SERVER_FOUND_OTHER:
cout<< "The host key for this server was not found but an other"
"type of key exists.\n";
cout<<"An attacker might change the default server key to"
"confuse your client into thinking the key does not exist\n";
free(hash);
return -1;
case SSH_SERVER_FILE_NOT_FOUND:
cout<<"Could not find known host file.\n";
cout<<"If you accept the host key here, the file will be"
"automatically created.\n";
case SSH_SERVER_NOT_KNOWN:
hexa = ssh_get_hexa(hash, hlen);
cout<<"The server is unknown. Do you trust the host key?\n";
cout<<"Public key hash : "<<hexa<<endl;
free(hexa);
if (fgets(buffer, sizeof(buffer), stdin) == NULL)
{
free(hash);
return -1;
}
if (_strnicmp(buffer, "yes", 3) != 0)
{
free(hash);
return -1;
}
if (ssh_write_knownhost(session) < 0)
{
fprintf(stderr, "Error %s\n", strerror(errno));
free(hash);
return -1;
}
break;
case SSH_SERVER_ERROR:
cout<<ssh_get_error(session);
free(hash);
return -1;
}
free(hash);
return 0;
}
Here is the output window that gives the error.
Upvotes: 0
Views: 947
Reputation: 838
I don't see you calling ssh_init() which is important. Did you look at the tutorial on http://api.libssh.org and the examples in the source code?
Upvotes: 1