Проверка подлинности на сервере Libssh в VS2008 идет не очень хорошо

Я использую библиотеку libssh для создания соединения с ssh-сервером, созданным на удаленном сервере. Все идет хорошо с соединением, пока код не получит открытый ключ от сервера и не спросит пользователя, хочет ли он сохранить его или нет. Там код вылетает по неизвестной причине. Я использую учебник, данный на этом ссылка на сайт.

Вот код, который я написал до сих пор. Любая помощь по этому поводу будет здорово.

#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;
}

Вот окно вывода, которое выдает ошибку.

введите описание изображения здесь

0

Решение

Я не вижу, чтобы вы вызывали ssh_init (), что важно. Вы смотрели на учебник по http://api.libssh.org а примеры в исходном коде?

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector