#include "mainwindow.h"#include "ui_mainwindow.h"#include <QDebug>#include "libssh/legacy.h"#include "libssh/callbacks.h"#include "libssh/server.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::onConnected(){ qDebug() << "SecureUploader: Connected";}void MainWindow::on_pushButton_clicked(){ ssh_session my_ssh_session; int rc; char *password; char *login = "rebrov"; char *host = "192.168.1.1"; int port = 2432; // Open session and set options my_ssh_session = ssh_new(); if (my_ssh_session == NULL) exit(-1); ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port); ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, &login); ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, &host); // Connect to server rc = ssh_connect(my_ssh_session); if (rc != SSH_OK) { fprintf(stderr, "Error connecting to localhost: %s\n", ssh_get_error(my_ssh_session)); ssh_free(my_ssh_session); qDebug() << "Не то пальто"; return;//exit(-1); } // Verify the server's identity // For the source code of verify_knowhost(), check previous example if (verify_knownhost(my_ssh_session) < 0) { ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); qDebug() << "Не тот плющ"; return;//exit(-1); } // Authenticate ourselves password = "Fa#tme7"; rc = ssh_userauth_password(my_ssh_session, NULL, password); if (rc != SSH_AUTH_SUCCESS) { fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(my_ssh_session)); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); qDebug() << "Не там высадили"; return;//exit(-1); } qDebug()<< "success!"; ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); /* QSsh::SshConnectionParameters params; params.host = QString("192.168.1.1"); params.userName = "rebrov"; params.password = QString("Xz29#dm7"); params.authenticationType = QSsh::SshConnectionParameters::AuthenticationByPassword; params.proxyType = QSsh::SshConnectionParameters::NoProxy; params.timeout = 30; params.port = 2432; m_connection = new QSsh::SshConnection(params, this); // TODO free this pointer! qDebug() << m_connection->connectionParameters().userName; connect(m_connection, SIGNAL(connected()), SLOT(onConnected())); connect(m_connection, SIGNAL(error(QSsh::SshError)), SLOT(onConnectionError(QSsh::SshError))); qDebug() << "SecureUploader: Connecting to host" << "192.168.1.150"; m_connection->connectToHost();*/}int MainWindow::verify_knownhost(ssh_session session){ int state, hlen; unsigned char *hash = NULL; char *hexa; char buf[10]; state = ssh_is_server_known(session); hlen = ssh_get_pubkey_hash(session, &hash); if (hlen < 0) return -1; switch (state) { case SSH_SERVER_KNOWN_OK: break; /* ok */ case SSH_SERVER_KNOWN_CHANGED: fprintf(stderr, "Host key for server changed: it is now:\n"); ssh_print_hexa("Public key hash", hash, hlen); fprintf(stderr, "For security reasons, connection will be stopped\n"); free(hash); return -1; case SSH_SERVER_FOUND_OTHER: fprintf(stderr, "The host key for this server was not found but an other" "type of key exists.\n"); fprintf(stderr, "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: fprintf(stderr, "Could not find known host file.\n"); fprintf(stderr, "If you accept the host key here, the file will be" "automatically created.\n"); /* fallback to SSH_SERVER_NOT_KNOWN behavior */ case SSH_SERVER_NOT_KNOWN: hexa = ssh_get_hexa(hash, hlen); fprintf(stderr,"The server is unknown. Do you trust the host key?\n"); fprintf(stderr, "Public key hash: %s\n", hexa); free(hexa); if (fgets(buf, sizeof(buf), stdin) == NULL) { free(hash); return -1; } if (strncasecmp(buf, "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: fprintf(stderr, "Error %s", ssh_get_error(session)); free(hash); return -1; } free(hash); return 0;}
C++ (Qt)ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, &login);ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, &host);
LIBSSH_API int ssh_options_get(ssh_session session, enum ssh_options_e type, char **value);