use super::*;
#[derive(Clone)]
pub struct AdoptedMaster {
pub host: String,
pub socket_dir: PathBuf,
pub socket_path: PathBuf,
}
pub fn close_master(a: AdoptedMaster) {
drop(SshControl::adopt(a));
}
impl SshControl {
pub fn adopt(a: AdoptedMaster) -> Self {
Self {
host: a.host,
socket_dir: a.socket_dir,
socket_path: a.socket_path,
}
}
pub(super) fn start(host: &str) -> Result<Self> {
let socket_dir = ssh_control_dir()?;
let socket_path = socket_dir.join("ctl");
let status = Command::new("ssh")
.arg("-M")
.arg("-S")
.arg(&socket_path)
.arg("-f")
.arg("-N")
.arg("-T")
.arg("-o")
.arg("ConnectTimeout=10")
.arg("-o")
.arg("ServerAliveInterval=10")
.arg("-o")
.arg("ServerAliveCountMax=3")
.arg(host)
.status()
.context("starting SSH control connection")?;
if !status.success() {
anyhow::bail!("ssh control connection exited with {status}");
}
Ok(Self {
host: host.to_string(),
socket_dir,
socket_path,
})
}
pub(super) fn command(&self) -> Command {
ssh_socket_command(&self.socket_path, false)
}
pub(super) fn background_command(&self) -> Command {
ssh_socket_command(&self.socket_path, true)
}
pub(super) fn background_shell(&self, script: &str) -> Command {
let mut command = self.background_command();
command.arg(&self.host).arg(script);
command
}
}
pub(super) fn ssh_program() -> OsString {
#[cfg(test)]
if let Some(p) = TEST_SSH.with(|p| p.borrow().clone()) {
return p.into_os_string();
}
OsString::from("ssh")
}
pub(super) fn ssh_socket_read(socket_path: &Path, host: &str, script: &str) -> Command {
let mut command = ssh_socket_command(socket_path, true);
command.arg(host).arg(script).stdout(Stdio::piped());
command
}
pub(super) fn ssh_socket_command(socket_path: &Path, background: bool) -> Command {
let mut command = Command::new(ssh_program());
command
.arg("-S")
.arg(socket_path)
.arg("-o")
.arg("ControlMaster=no")
.arg("-o")
.arg("ConnectTimeout=10")
.arg("-o")
.arg("ServerAliveInterval=10")
.arg("-o")
.arg("ServerAliveCountMax=3");
if background {
command
.arg("-n")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
}
command
}
impl Drop for SshControl {
fn drop(&mut self) {
let _ = Command::new("ssh")
.arg("-S")
.arg(&self.socket_path)
.arg("-O")
.arg("exit")
.arg(&self.host)
.status();
let _ = std::fs::remove_dir_all(&self.socket_dir);
}
}
pub(super) fn ssh_exec(host: &str, socket: &Path, cmd: &str) -> bool {
Command::new("ssh")
.arg("-S")
.arg(socket)
.arg("-o")
.arg("ControlMaster=no")
.arg(host)
.arg(cmd)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
}
pub fn ssh_control_dir() -> Result<PathBuf> {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.context("system clock before unix epoch")?
.as_millis();
static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!("croft-ssh-{}-{now}-{seq}", std::process::id()));
let mut builder = std::fs::DirBuilder::new();
#[cfg(unix)]
{
use std::os::unix::fs::DirBuilderExt;
builder.mode(0o700);
}
builder
.create(&dir)
.with_context(|| format!("creating {}", dir.display()))?;
Ok(dir)
}
#[cfg(test)]
pub(super) fn ssh_control_socket_path_for_test(dir: &Path) -> PathBuf {
dir.join("ctl")
}