Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions include/multipass/ssh/libssh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include <multipass/singleton.h>

#include <libssh/callbacks.h>
#include <libssh/libssh.h>
#include <libssh/sftp.h>

#include <sys/types.h>

#include <cstddef>
#include <cstdint>

#define MP_LIBSSH multipass::Libssh::instance()

namespace multipass
{
class Libssh : public Singleton<Libssh>
{
public:
Libssh(const Singleton<Libssh>::PrivatePass&) noexcept;

// --- init / finalize -----------------------------------------------------
virtual int ssh_init() const;
virtual int ssh_finalize() const;

// --- session -------------------------------------------------------------
virtual ssh_session ssh_new() const;
virtual void ssh_free(ssh_session session) const;
virtual void ssh_disconnect(ssh_session session) const;
virtual int ssh_is_connected(ssh_session session) const;
virtual int ssh_options_set(ssh_session session,
enum ssh_options_e type,
const void* value) const;
virtual const char* ssh_get_error(void* error) const;
virtual socket_t ssh_get_fd(ssh_session session) const;
virtual int ssh_connect(ssh_session session) const;
virtual int ssh_userauth_publickey(ssh_session session,
const char* username,
const ssh_key privkey) const;

// --- channel -------------------------------------------------------------
virtual ssh_channel ssh_channel_new(ssh_session session) const;
virtual void ssh_channel_free(ssh_channel channel) const;
virtual int ssh_channel_is_closed(ssh_channel channel) const;
virtual int ssh_channel_is_eof(ssh_channel channel) const;
virtual int ssh_channel_is_open(ssh_channel channel) const;
virtual int ssh_channel_close(ssh_channel channel) const;
virtual ssh_session ssh_channel_get_session(ssh_channel channel) const;
virtual int ssh_channel_read_timeout(ssh_channel channel,
void* dest,
uint32_t count,
int is_stderr,
int timeout_ms) const;
virtual int ssh_channel_read_nonblocking(ssh_channel channel,
void* dest,
uint32_t count,
int is_stderr) const;
virtual int ssh_channel_request_pty_size(ssh_channel channel,
const char* term,
int cols,
int rows) const;
virtual int ssh_channel_change_pty_size(ssh_channel channel, int cols, int rows) const;
virtual int ssh_channel_write(ssh_channel channel, const void* data, uint32_t len) const;
virtual int ssh_channel_open_session(ssh_channel channel) const;
virtual int ssh_channel_request_exec(ssh_channel channel, const char* cmd) const;
virtual int ssh_channel_request_shell(ssh_channel channel) const;
virtual int ssh_channel_get_exit_state(ssh_channel channel,
uint32_t* exit_code,
char** exit_signal,
int* core_dumped) const;

// --- channel callbacks ---------------------------------------------------
virtual int ssh_add_channel_callbacks(ssh_channel channel, ssh_channel_callbacks cb) const;
virtual int ssh_remove_channel_callbacks(ssh_channel channel, ssh_channel_callbacks cb) const;

// --- event ---------------------------------------------------------------
virtual ssh_event ssh_event_new() const;
virtual void ssh_event_free(ssh_event event) const;
virtual int ssh_event_add_session(ssh_event event, ssh_session session) const;
virtual int ssh_event_add_connector(ssh_event event, ssh_connector connector) const;
virtual int ssh_event_remove_connector(ssh_event event, ssh_connector connector) const;
virtual int ssh_event_dopoll(ssh_event event, int timeout) const;

// --- connector -----------------------------------------------------------
virtual ssh_connector ssh_connector_new(ssh_session session) const;
virtual void ssh_connector_free(ssh_connector connector) const;
virtual int ssh_connector_set_in_channel(ssh_connector connector,
ssh_channel channel,
enum ssh_connector_flags_e flags) const;
virtual void ssh_connector_set_in_fd(ssh_connector connector, socket_t fd) const;
virtual int ssh_connector_set_out_channel(ssh_connector connector,
ssh_channel channel,
enum ssh_connector_flags_e flags) const;
virtual void ssh_connector_set_out_fd(ssh_connector connector, socket_t fd) const;

// --- pki / keys ----------------------------------------------------------
virtual int ssh_pki_generate(enum ssh_keytypes_e type, int parameter, ssh_key* pkey) const;
virtual int ssh_pki_export_privkey_file(const ssh_key privkey,
const char* passphrase,
ssh_auth_callback auth_fn,
void* auth_data,
const char* filename) const;
virtual int ssh_pki_import_privkey_file(const char* filename,
const char* passphrase,
ssh_auth_callback auth_fn,
void* auth_data,
ssh_key* pkey) const;
virtual int ssh_pki_export_pubkey_base64(const ssh_key key, char** b64_key) const;
virtual int ssh_pki_import_privkey_base64(const char* b64_key,
const char* passphrase,
ssh_auth_callback auth_fn,
void* auth_data,
ssh_key* pkey) const;
virtual void ssh_key_free(ssh_key key) const;

// --- strings -------------------------------------------------------------
virtual void ssh_string_free(ssh_string str) const;
virtual void ssh_string_free_char(char* s) const;
virtual const char* ssh_string_get_char(ssh_string str) const;
virtual size_t ssh_string_len(ssh_string str) const;

// --- sftp client ---------------------------------------------------------
virtual sftp_session sftp_new(ssh_session session) const;
virtual void sftp_free(sftp_session sftp) const;
virtual int sftp_init(sftp_session sftp) const;
virtual sftp_file sftp_open(sftp_session session,
const char* file,
int accesstype,
mode_t mode) const;
virtual int sftp_close(sftp_file file) const;
virtual ssize_t sftp_read(sftp_file file, void* buf, size_t count) const;
virtual ssize_t sftp_write(sftp_file file, const void* buf, size_t count) const;
virtual int sftp_chmod(sftp_session sftp, const char* file, mode_t mode) const;
virtual int sftp_mkdir(sftp_session sftp, const char* directory, mode_t mode) const;
virtual int sftp_unlink(sftp_session sftp, const char* file) const;
virtual int sftp_symlink(sftp_session sftp, const char* target, const char* dest) const;
virtual sftp_attributes sftp_stat(sftp_session session, const char* path) const;
virtual sftp_attributes sftp_lstat(sftp_session session, const char* path) const;
virtual sftp_dir sftp_opendir(sftp_session session, const char* path) const;
virtual int sftp_closedir(sftp_dir dir) const;
virtual sftp_attributes sftp_readdir(sftp_session session, sftp_dir dir) const;
virtual char* sftp_readlink(sftp_session sftp, const char* path) const;
virtual int sftp_dir_eof(sftp_dir dir) const;
virtual int sftp_get_error(sftp_session sftp) const;
virtual void sftp_attributes_free(sftp_attributes file) const;
virtual sftp_limits_t sftp_limits(sftp_session sftp) const;
// libssh >= 0.11
virtual void sftp_limits_free(sftp_limits_t limits) const;

// --- sftp server ---------------------------------------------------------
virtual sftp_session sftp_server_new(ssh_session session, ssh_channel chan) const;
virtual void sftp_server_free(sftp_session sftp) const;
virtual void sftp_client_message_free(sftp_client_message msg) const;
virtual void* sftp_handle(sftp_session sftp, ssh_string handle) const;
virtual ssh_string sftp_handle_alloc(sftp_session sftp, void* info) const;
virtual void sftp_handle_remove(sftp_session sftp, void* handle) const;
virtual sftp_client_message sftp_get_client_message(sftp_session sftp) const;
virtual const char* sftp_client_message_get_data(sftp_client_message msg) const;
virtual const char* sftp_client_message_get_filename(sftp_client_message msg) const;
virtual uint32_t sftp_client_message_get_flags(sftp_client_message msg) const;
virtual const char* sftp_client_message_get_submessage(sftp_client_message msg) const;
virtual uint8_t sftp_client_message_get_type(sftp_client_message msg) const;
virtual int sftp_reply_attr(sftp_client_message msg, sftp_attributes attr) const;
virtual int sftp_reply_data(sftp_client_message msg, const void* data, int len) const;
virtual int sftp_reply_handle(sftp_client_message msg, ssh_string handle) const;
virtual int sftp_reply_name(sftp_client_message msg,
const char* name,
sftp_attributes attr) const;
virtual int sftp_reply_names(sftp_client_message msg) const;
virtual int sftp_reply_names_add(sftp_client_message msg,
const char* file,
const char* longname,
sftp_attributes attr) const;
virtual int sftp_reply_status(sftp_client_message msg,
uint32_t status,
const char* message) const;
// Not exposed in libssh's public headers; forward-declared with C linkage and
// implemented by libssh.
virtual int sftp_reply_version(sftp_client_message msg) const;
};
} // namespace multipass
24 changes: 21 additions & 3 deletions include/multipass/ssh/sftp_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,32 @@

#include <multipass/format.h>
#include <multipass/singleton.h>
#include <multipass/ssh/libssh.h>

// Routes both the allocating call and the deleter through MP_LIBSSH so SFTP code paths
// remain mockable. The deleter type stays a std::function (matching the libssh signature)
// so the returned smart pointers remain interchangeable with the public type aliases.
// `close` must be a libssh function exposed on the Libssh singleton.
#define MP_SFTP_UNIQUE_PTR(open, close) \
template <typename... Args> \
auto mp_##open(Args&&... args) \
{ \
return std::unique_ptr<std::remove_pointer_t<decltype(std::function{open})::result_type>, \
decltype(std::function{close})>{open(std::forward<Args>(args)...), \
close}; \
decltype(std::function{close})>{ \
MP_LIBSSH.open(std::forward<Args>(args)...), \
[](auto* ptr) { return MP_LIBSSH.close(ptr); }}; \
}
Comment thread
tobe2098 marked this conversation as resolved.

// Variant for deleters that are not libssh functions (e.g. the C library `free`) and so
// are not routed through MP_LIBSSH.
#define MP_SFTP_UNIQUE_PTR_RAW(open, close) \
template <typename... Args> \
auto mp_##open(Args&&... args) \
{ \
return std::unique_ptr<std::remove_pointer_t<decltype(std::function{open})::result_type>, \
decltype(std::function{close})>{ \
MP_LIBSSH.open(std::forward<Args>(args)...), \
close}; \
}

namespace multipass
Expand All @@ -50,7 +68,7 @@ MP_SFTP_UNIQUE_PTR(sftp_stat, sftp_attributes_free)
MP_SFTP_UNIQUE_PTR(sftp_lstat, sftp_attributes_free)
MP_SFTP_UNIQUE_PTR(sftp_opendir, sftp_closedir)
MP_SFTP_UNIQUE_PTR(sftp_readdir, sftp_attributes_free)
MP_SFTP_UNIQUE_PTR(sftp_readlink, free)
MP_SFTP_UNIQUE_PTR_RAW(sftp_readlink, free)

#define MP_SFTPUTILS multipass::SFTPUtils::instance()

Expand Down
4 changes: 3 additions & 1 deletion include/multipass/ssh/throw_on_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#pragma once

#include <multipass/ssh/libssh.h>

#include <libssh/libssh.h>

#include <fmt/format.h>
Expand All @@ -39,7 +41,7 @@ void throw_on_error(Handle&& h,
const auto ret = f(h.get(), std::forward<Args>(args)...);
if (ret != SSH_OK)
{
throw SSHException(fmt::format("{}: '{}'", error_msg, ssh_get_error(session)));
throw SSHException(fmt::format("{}: '{}'", error_msg, MP_LIBSSH.ssh_get_error(session)));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/platform/backends/hyperv/hyperv_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <multipass/ip_address.h>
#include <multipass/logging/log.h>
#include <multipass/platform.h>
#include <multipass/ssh/libssh.h>
#include <multipass/ssh/plain_ssh_session.h>
#include <multipass/top_catch_all.h>
#include <multipass/utils.h>
Expand Down Expand Up @@ -62,7 +63,7 @@ try

sockaddr_in addr{};
int size = sizeof(addr);
auto socket = ssh_get_fd(session);
auto socket = MP_LIBSSH.ssh_get_fd(session);
const auto failed = getpeername(socket, reinterpret_cast<sockaddr*>(&addr), &size);

if (failed)
Expand Down
15 changes: 8 additions & 7 deletions src/platform/console/unix_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "unix_console.h"

#include <multipass/platform_unix.h>
#include <multipass/ssh/libssh.h>

#include <sys/ioctl.h>

Expand Down Expand Up @@ -54,9 +55,9 @@ static void sigwinch_handler(int sig)
{
if (update_local_pty_size(global_cout_fd))
{
ssh_channel_change_pty_size(global_channel,
local_pty_size.columns,
local_pty_size.rows);
MP_LIBSSH.ssh_channel_change_pty_size(global_channel,
local_pty_size.columns,
local_pty_size.rows);
Comment thread
tobe2098 marked this conversation as resolved.
}
}
}
Expand All @@ -78,10 +79,10 @@ mp::UnixConsole::UnixConsole(ssh_channel channel, UnixTerminal* term) : term{ter
term_type = (term_type == nullptr) ? "xterm" : term_type;

update_local_pty_size(term->cout_fd());
ssh_channel_request_pty_size(channel,
term_type,
local_pty_size.columns,
local_pty_size.rows);
MP_LIBSSH.ssh_channel_request_pty_size(channel,
term_type,
local_pty_size.columns,
local_pty_size.rows);

// set stdin to Raw Mode after libssh inherits sane settings from stdin.
setup_console();
Expand Down
23 changes: 14 additions & 9 deletions src/platform/console/windows_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <multipass/cli/client_platform.h>
#include <multipass/format.h>
#include <multipass/logging/log.h>
#include <multipass/ssh/libssh.h>

#include <winsock2.h>

Expand Down Expand Up @@ -54,7 +55,7 @@ mp::WindowsConsole::WindowsConsole(ssh_channel channel, WindowsTerminal* term)
output_handle{term->cout_handle()},
error_handle{term->cerr_handle()},
channel{channel},
session_socket_fd{ssh_get_fd(ssh_channel_get_session(channel))},
session_socket_fd{MP_LIBSSH.ssh_get_fd(MP_LIBSSH.ssh_channel_get_session(channel))},
last_geometry{get_console_size(output_handle)}
{
setup_console();
Expand All @@ -71,7 +72,10 @@ void mp::WindowsConsole::setup_console()
GetConsoleMode(output_handle, &console_output_mode);
SetConsoleMode(output_handle, console_output_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

ssh_channel_request_pty_size(channel, term_type, last_geometry.columns, last_geometry.rows);
MP_LIBSSH.ssh_channel_request_pty_size(channel,
term_type,
last_geometry.columns,
last_geometry.rows);
}
}

Expand Down Expand Up @@ -111,9 +115,10 @@ void mp::WindowsConsole::read_console()
}

std::lock_guard<std::mutex> lock(ssh_mutex);
ssh_channel_write(channel,
text_buffer.data(),
static_cast<uint32_t>(text_buffer.size() * sizeof(text_buffer.front())));
MP_LIBSSH.ssh_channel_write(
channel,
text_buffer.data(),
static_cast<uint32_t>(text_buffer.size() * sizeof(text_buffer.front())));
}
}

Expand All @@ -133,12 +138,12 @@ void mp::WindowsConsole::write_console()

{
std::lock_guard<std::mutex> lock(ssh_mutex);
num_bytes = ssh_channel_read_nonblocking(channel, buffer.data(), chunk, 0);
num_bytes = MP_LIBSSH.ssh_channel_read_nonblocking(channel, buffer.data(), chunk, 0);

// Try reading from stderr if nothing is returned from stdout
if (num_bytes == 0)
{
num_bytes = ssh_channel_read_nonblocking(channel, buffer.data(), chunk, 1);
num_bytes = MP_LIBSSH.ssh_channel_read_nonblocking(channel, buffer.data(), chunk, 1);
current_handle = error_handle;
}
}
Expand All @@ -147,7 +152,7 @@ void mp::WindowsConsole::write_console()
{
// Force the channel to close if EOF is detected from the channel_read
if (num_bytes == SSH_EOF)
ssh_channel_close(channel);
MP_LIBSSH.ssh_channel_close(channel);

return;
}
Expand Down Expand Up @@ -187,6 +192,6 @@ void mp::WindowsConsole::update_ssh_pty_size()
last_geometry.columns = columns;
last_geometry.rows = rows;
std::lock_guard<std::mutex> lock(ssh_mutex);
ssh_channel_change_pty_size(channel, columns, rows);
MP_LIBSSH.ssh_channel_change_pty_size(channel, columns, rows);
}
}
Loading
Loading