Commit 052b45f3 authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
Browse files

WIP. starting on remote execution.

parent 60894bfb
Pipeline #86747 failed
......@@ -6,6 +6,10 @@
#include <cassert>
#include <sstream>
#include <QByteArray>
#include <QString>
namespace rsm
{
void assert_ssh_session(ssh_session session, const std::string& error_message)
......@@ -20,6 +24,7 @@ class SessionWorkerImpl
{
public:
ssh_session session = nullptr;
QByteArray output_buffer;
SessionWorkerImpl()
{
session = ssh_new();
......@@ -102,6 +107,8 @@ void SessionWorker::disconnect()
{
radix_tagged_line("Disconnecting session.");
ssh_disconnect(p->session);
ssh_free(p->session);
ssh_finalize();
}
}
......@@ -253,4 +260,52 @@ void SessionWorker::authenticateWithPassword(QString pswd)
}
}
void SessionWorker::requestExec(QString command)
{
assert_ssh_session(p->session, "request_exec() -- Session is not allocated.");
char buffer[256];
// clear any previous buffer
p->output_buffer.clear();
ssh_channel channel = ssh_channel_new(p->session);
if (channel == nullptr)
{
radix_tagged_line("TODO: Handle failed new channel");
return;
}
int rc = ssh_channel_open_session(channel);
if (rc < 0)
{
radix_tagged_line("TODO: Handle failed new channel");
return;
}
rc = ssh_channel_request_exec(channel, command.toStdString().c_str());
if (rc < 0)
{
radix_tagged_line("TODO: Handle failed new channel");
ssh_channel_close(channel);
ssh_channel_free(channel);
return;
}
int nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
while (nbytes > 0)
{
p->output_buffer.append(buffer, nbytes);
emit execOutputReady();
radix(nbytes);
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}
radix_tagged_line();
if (nbytes == 0)
{
ssh_channel_send_eof(channel);
}
ssh_channel_close(channel);
ssh_channel_free(channel);
}
} // namespace rsm
\ No newline at end of file
......@@ -25,22 +25,78 @@ class RSM_PUBLIC SessionWorker : public QObject
Functions
};
/**
* Basic Constructor
*/
SessionWorker(QObject* parent = nullptr);
/**
* Contructs with host name
*/
SessionWorker(QString host, QObject* parent = nullptr);
~SessionWorker();
public slots:
/**
* Set the remote host to connect to
*/
void setHost(QString host);
/**
* Set the Log verbosity
*/
void setLogVerbosity(Verbosity level);
/**
* Set the ssh port number
*/
void setPort(int port);
/**
* Set the user
* Defaults to system user
*/
void setUser(QString name);
/**
* Perform hand-shake to connect to host.
*/
void connect();
/**
* Disconnect ssh session
*/
void disconnect();
/**
* Perform known host verification.
* Checks known_host file to validate remote host SHA
* Can emit hostUnknown(QString host_hash).
* Can emit hostPublicKeyChanged(QString host_hash).
* Can emit hostPublicKeyUnavailable().
* Can emit knownHostError().
*/
void verifyKnownHost();
/**
* Performs a request to accept/save the host's public key,
* updating the known_hosts
*/
void acceptHostPublicKeyUpdate();
/**
* Attempts to authenticate using public key or password.
* Can emit authenticationError(QString message).
* Can emit authenticationSucceeded() upon acceptance of public key.
* Can emit passworedRequested().
*/
void authenticate();
/**
* Attempts to authenticate with username and password.
* Can emit authenticationError(QString message).
* Can emit authenticationSuccessed() upon acceptance of password.
* Can emit loginBannerIssued(QString message) give availability.
*/
void authenticateWithPassword(QString pswd);
/**
* Requests remote execution of command
* @param command - QString command to execute
*/
void requestExec(QString command);
signals:
void getServerPublicKeyFailed();
void hostUnknown(QString host_hash);
......@@ -52,6 +108,7 @@ class RSM_PUBLIC SessionWorker : public QObject
void authenticationSucceeded();
void passwordRequested();
void loginBannerIssued(QString message);
void execOutputReady();
}; // class SessionWorker
} // namespace rsm
......
......@@ -5,12 +5,13 @@
using namespace rsm;
TEST(RSM, Session)
{
SessionWorker session("localhost");
SessionWorker session("warroom4");
session.setLogVerbosity(SessionWorker::Verbosity::Functions);
session.setPort(22);
session.setUser("jap");
session.connect(); // 1) hand-shake with host
session.verifyKnownHost(); // 2) verify host is known/acceptable
session.authenticate(); // 3) process authentication
session.requestExec("ls -la");
session.disconnect();
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment