Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
LEFEBVREJP email
rsm
Commits
052b45f3
Commit
052b45f3
authored
Jan 16, 2020
by
LEFEBVREJP email
Browse files
WIP. starting on remote execution.
parent
60894bfb
Pipeline
#86747
failed
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
rsmcore/sessionworker.cc
View file @
052b45f3
...
@@ -6,6 +6,10 @@
...
@@ -6,6 +6,10 @@
#include <cassert>
#include <cassert>
#include <sstream>
#include <sstream>
#include <QByteArray>
#include <QString>
namespace
rsm
namespace
rsm
{
{
void
assert_ssh_session
(
ssh_session
session
,
const
std
::
string
&
error_message
)
void
assert_ssh_session
(
ssh_session
session
,
const
std
::
string
&
error_message
)
...
@@ -20,6 +24,7 @@ class SessionWorkerImpl
...
@@ -20,6 +24,7 @@ class SessionWorkerImpl
{
{
public:
public:
ssh_session
session
=
nullptr
;
ssh_session
session
=
nullptr
;
QByteArray
output_buffer
;
SessionWorkerImpl
()
SessionWorkerImpl
()
{
{
session
=
ssh_new
();
session
=
ssh_new
();
...
@@ -102,6 +107,8 @@ void SessionWorker::disconnect()
...
@@ -102,6 +107,8 @@ void SessionWorker::disconnect()
{
{
radix_tagged_line
(
"Disconnecting session."
);
radix_tagged_line
(
"Disconnecting session."
);
ssh_disconnect
(
p
->
session
);
ssh_disconnect
(
p
->
session
);
ssh_free
(
p
->
session
);
ssh_finalize
();
}
}
}
}
...
@@ -253,4 +260,52 @@ void SessionWorker::authenticateWithPassword(QString pswd)
...
@@ -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
}
// namespace rsm
\ No newline at end of file
rsmcore/sessionworker.hh
View file @
052b45f3
...
@@ -25,22 +25,78 @@ class RSM_PUBLIC SessionWorker : public QObject
...
@@ -25,22 +25,78 @@ class RSM_PUBLIC SessionWorker : public QObject
Functions
Functions
};
};
/**
* Basic Constructor
*/
SessionWorker
(
QObject
*
parent
=
nullptr
);
SessionWorker
(
QObject
*
parent
=
nullptr
);
/**
* Contructs with host name
*/
SessionWorker
(
QString
host
,
QObject
*
parent
=
nullptr
);
SessionWorker
(
QString
host
,
QObject
*
parent
=
nullptr
);
~
SessionWorker
();
~
SessionWorker
();
public
slots
:
public
slots
:
/**
* Set the remote host to connect to
*/
void
setHost
(
QString
host
);
void
setHost
(
QString
host
);
/**
* Set the Log verbosity
*/
void
setLogVerbosity
(
Verbosity
level
);
void
setLogVerbosity
(
Verbosity
level
);
/**
* Set the ssh port number
*/
void
setPort
(
int
port
);
void
setPort
(
int
port
);
/**
* Set the user
* Defaults to system user
*/
void
setUser
(
QString
name
);
void
setUser
(
QString
name
);
/**
* Perform hand-shake to connect to host.
*/
void
connect
();
void
connect
();
/**
* Disconnect ssh session
*/
void
disconnect
();
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
();
void
verifyKnownHost
();
/**
* Performs a request to accept/save the host's public key,
* updating the known_hosts
*/
void
acceptHostPublicKeyUpdate
();
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
();
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
);
void
authenticateWithPassword
(
QString
pswd
);
/**
* Requests remote execution of command
* @param command - QString command to execute
*/
void
requestExec
(
QString
command
);
signals:
signals:
void
getServerPublicKeyFailed
();
void
getServerPublicKeyFailed
();
void
hostUnknown
(
QString
host_hash
);
void
hostUnknown
(
QString
host_hash
);
...
@@ -52,6 +108,7 @@ class RSM_PUBLIC SessionWorker : public QObject
...
@@ -52,6 +108,7 @@ class RSM_PUBLIC SessionWorker : public QObject
void
authenticationSucceeded
();
void
authenticationSucceeded
();
void
passwordRequested
();
void
passwordRequested
();
void
loginBannerIssued
(
QString
message
);
void
loginBannerIssued
(
QString
message
);
void
execOutputReady
();
};
// class SessionWorker
};
// class SessionWorker
}
// namespace rsm
}
// namespace rsm
...
...
rsmcore/tests/tstSession.cc
View file @
052b45f3
...
@@ -5,12 +5,13 @@
...
@@ -5,12 +5,13 @@
using
namespace
rsm
;
using
namespace
rsm
;
TEST
(
RSM
,
Session
)
TEST
(
RSM
,
Session
)
{
{
SessionWorker
session
(
"
localhost
"
);
SessionWorker
session
(
"
warroom4
"
);
session
.
setLogVerbosity
(
SessionWorker
::
Verbosity
::
Functions
);
session
.
setLogVerbosity
(
SessionWorker
::
Verbosity
::
Functions
);
session
.
setPort
(
22
);
session
.
setPort
(
22
);
session
.
setUser
(
"jap"
);
session
.
setUser
(
"jap"
);
session
.
connect
();
// 1) hand-shake with host
session
.
connect
();
// 1) hand-shake with host
session
.
verifyKnownHost
();
// 2) verify host is known/acceptable
session
.
verifyKnownHost
();
// 2) verify host is known/acceptable
session
.
authenticate
();
// 3) process authentication
session
.
authenticate
();
// 3) process authentication
session
.
requestExec
(
"ls -la"
);
session
.
disconnect
();
session
.
disconnect
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment