| 1 |
//! SSH connection support for the Rust transport layer. |
| 2 |
//! |
| 3 |
//! This module mirrors `dromedary/ssh/__init__.py`. It exposes vendor |
| 4 |
//! implementations (subprocess-based and library-based) that produce an |
| 5 |
//! [`SSHConnection`] or an SFTP channel usable by the `sftp` submodule. |
| 6 |
//! |
| 7 |
//! The `SshLibrary` / `SshSession` traits below are an internal abstraction |
| 8 |
//! that lets us plug in different crypto backends (russh today; libssh2 or |
| 9 |
//! ssh2-rs in the future) without rewriting the PyO3-facing vendor layer. |
| 10 |
//! They are deliberately not exposed to Python. |
| 11 |
|
| 12 |
use pyo3::prelude::*; |
| 13 |
use std::ffi::OsString; |
| 14 |
|
| 15 |
#[cfg(feature = "russh")] |
| 16 |
mod russh_vendor; |
| 17 |
mod subprocess; |
| 18 |
// TODO: add `libssh2` backend module gated on a future `libssh2` feature. |
| 19 |
// TODO: add `ssh2-rs` backend module gated on a future `ssh2-rs` feature. |
| 20 |
|
| 21 |
/// Parameters used to establish an SSH connection. |
| 22 |
#[allow(dead_code)] |
| 23 |
pub(crate) struct ConnectConfig { |
| 24 |
pub host: String, |
| 25 |
pub port: u16, |
| 26 |
pub username: Option<String>, |
| 27 |
pub password: Option<String>, |
| 28 |
} |
| 29 |
|
| 30 |
/// Backend-agnostic SSH session. Each crypto library (russh, libssh2, …) |
| 31 |
/// provides its own implementation. |
| 32 |
#[allow(dead_code)] |
| 33 |
pub(crate) trait SshSession: Send { |
| 34 |
/// Open the `sftp` subsystem and return a bidirectional stream suitable |
| 35 |
/// for feeding into `sftp::SftpClient::from_stream` (to be added). |
| 36 |
fn open_sftp(&mut self) -> std::io::Result<Box<dyn ReadWrite>>; |
| 37 |
|
| 38 |
/// Execute a command on the remote host, returning its stdio as a stream. |
| 39 |
fn exec(&mut self, command: &str) -> std::io::Result<Box<dyn ReadWrite>>; |
| 40 |
} |
| 41 |
|
| 42 |
/// Marker trait combining `Read + Write + Send` so we can hand a trait object |
| 43 |
/// to the SFTP client. |
| 44 |
pub(crate) trait ReadWrite: std::io::Read + std::io::Write + Send {} |
| 45 |
impl<T: std::io::Read + std::io::Write + Send> ReadWrite for T {} |
| 46 |
|
| 47 |
/// Library-level entry point. Each backend implements this to hand back a |
| 48 |
/// fresh [`SshSession`] for the given connection parameters. |
| 49 |
#[allow(dead_code)] |
| 50 |
pub(crate) trait SshLibrary { |
| 51 |
fn connect(cfg: &ConnectConfig) -> std::io::Result<Box<dyn SshSession>>; |
| 52 |
} |
| 53 |
|
| 54 |
/// Classify an `ssh -V` version string into a vendor registry key. |
| 55 |
/// Mirrors `dromedary.ssh.SSHVendorManager._get_vendor_by_version_string`. |
| 56 |
#[pyfunction] |
| 57 |
#[pyo3(signature = (version, progname))] |
| 58 |
fn classify_ssh_version(version: &str, progname: &str) -> Option<&'static str> { |
| 59 |
dromedary::ssh::classify_ssh_version(version, progname) |
| 60 |
} |
| 61 |
|
| 62 |
/// Run `executable -V` and return the vendor registry key, or `None` if |
| 63 |
/// the binary can't be run or the output isn't recognized. Mirrors the |
| 64 |
/// combination of `_get_ssh_version_string` + `_get_vendor_from_path`. |
| 65 |
#[pyfunction] |
| 66 |
#[pyo3(signature = (executable))] |
| 67 |
fn detect_ssh_vendor(py: Python, executable: OsString) -> Option<&'static str> { |
| 68 |
py.detach(|| dromedary::ssh::detect_ssh_vendor(&executable)) |
| 69 |
} |
| 70 |
|
| 71 |
pub(crate) fn register(py: Python, m: &Bound<PyModule>) -> PyResult<()> { |
| 72 |
subprocess::register(py, m)?; |
| 73 |
#[cfg(feature = "russh")] |
| 74 |
russh_vendor::register(py, m)?; |
| 75 |
m.add_function(wrap_pyfunction!(classify_ssh_version, m)?)?; |
| 76 |
m.add_function(wrap_pyfunction!(detect_ssh_vendor, m)?)?; |
| 77 |
Ok(()) |
| 78 |
} |