use std::collections::HashSet;
use std::ffi::OsString;
use std::fs;
use std::io::{Read, Write};
use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Mutex, MutexGuard, PoisonError};
use anyhow::{Context, Result};
use second_brain_core::keys::valid_machine_id;
use second_brain_core::machine::MachineIdentity;
use crate::protocol::Hello;
use crate::session::{Exit, Remote, Transport};
use crate::ssh_identity::EphemeralIdentity;
static KNOWN_HOSTS_LOCK: Mutex<()> = Mutex::new(());
static TEMP_SEQ: AtomicU64 = AtomicU64::new(0);
fn known_hosts_guard() -> MutexGuard<'static, ()> {
KNOWN_HOSTS_LOCK
.lock()
.unwrap_or_else(PoisonError::into_inner)
}
pub fn known_hosts_path_in(config_dir: &Path) -> PathBuf {
config_dir.join("known_hosts")
}
pub fn known_hosts_path() -> Result<PathBuf> {
Ok(known_hosts_path_in(&MachineIdentity::config_dir()?))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyscanLine {
pub host: String,
pub key_type: String,
pub public_key: String,
}
pub fn parse_keyscan_output(raw: &str) -> Vec<KeyscanLine> {
raw.lines()
.filter_map(|line| {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
return None;
}
let mut parts = line.split_whitespace();
let host = parts.next()?;
let key_type = parts.next()?;
let public_key = parts.next()?;
Some(KeyscanLine {
host: host.to_string(),
key_type: key_type.to_string(),
public_key: public_key.to_string(),
})
})
.collect()
}
pub fn compose_known_host_entry(line: &KeyscanLine) -> String {
format!("{} {} {}\n", line.host, line.key_type, line.public_key)
}
pub fn append_known_host(path: &Path, lines: &[KeyscanLine]) -> Result<()> {
if let Some(parent) = path.parent()
&& !parent.as_os_str().is_empty()
{
fs::create_dir_all(parent).context("creating known_hosts parent dir")?;
}
let _guard = known_hosts_guard();
let existing = match fs::read_to_string(path) {
Ok(s) => s,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(e) => return Err(e).context("reading known_hosts"),
};
let already: HashSet<&str> = existing
.lines()
.map(str::trim)
.filter(|l| !l.is_empty())
.collect();
let mut file = fs::OpenOptions::new()
.create(true)
.append(true)
.mode(0o600)
.open(path)
.context("opening known_hosts for append")?;
for line in lines {
let entry = compose_known_host_entry(line);
if already.contains(entry.trim_end()) {
continue;
}
file.write_all(entry.as_bytes())
.context("writing known_hosts entry")?;
}
fs::set_permissions(path, fs::Permissions::from_mode(0o600))
.context("setting known_hosts mode 0600")?;
Ok(())
}
const HOST_KEY_FILES: &[&str] = &[
"ssh_host_ed25519_key.pub",
"ssh_host_ecdsa_key.pub",
"ssh_host_rsa_key.pub",
];
pub fn read_host_keys(dir: &Path) -> Vec<String> {
HOST_KEY_FILES
.iter()
.filter_map(|name| fs::read_to_string(dir.join(name)).ok())
.filter_map(|text| {
let mut parts = text.split_whitespace();
let key_type = parts.next()?;
let key = parts.next()?;
(key_type.starts_with("ssh-") || key_type.starts_with("ecdsa-"))
.then(|| format!("{key_type} {key}"))
})
.collect()
}
pub fn known_host_name(host: &str, port: u16) -> String {
if port == 22 {
host.to_string()
} else {
format!("[{host}]:{port}")
}
}
fn is_plain_host(host: &str) -> bool {
!host.is_empty()
&& host
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'-' | b'_' | b':' | b'%'))
}
pub fn pin_host_keys(
path: &Path,
hosts: &[String],
port: u16,
host_keys: &[String],
) -> Result<Vec<String>> {
let mut lines = Vec::new();
for host in hosts.iter().filter(|host| is_plain_host(host)) {
let name = known_host_name(host, port);
for key in host_keys {
let mut parts = key.split_whitespace();
let (Some(key_type), Some(public_key)) = (parts.next(), parts.next()) else {
continue;
};
lines.push(KeyscanLine {
host: name.clone(),
key_type: key_type.to_string(),
public_key: public_key.to_string(),
});
}
}
lines.dedup();
append_known_host(path, &lines)?;
Ok(lines
.iter()
.map(|line| compose_known_host_entry(line).trim_end().to_string())
.collect())
}
pub fn device_alias(machine_id: &str) -> String {
format!("sb-{machine_id}")
}
fn read_known_hosts(path: &Path) -> Result<String> {
match fs::read_to_string(path) {
Ok(text) => Ok(text),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(String::new()),
Err(e) => Err(e).context("reading known_hosts"),
}
}
pub fn pin_alias(path: &Path, alias: &str, host_keys: &[String]) -> Result<bool> {
anyhow::ensure!(
valid_machine_id(alias),
"{alias:?} cannot be a known_hosts host name"
);
let _guard = known_hosts_guard();
let existing = read_known_hosts(path)?;
let pinned: HashSet<(&str, &str)> = existing
.lines()
.filter_map(|line| {
let mut parts = line.split_whitespace();
(parts.next()? == alias).then_some(())?;
Some((parts.next()?, parts.next()?))
})
.collect();
let mut fresh: Vec<(&str, &str)> = Vec::new();
for key in host_keys {
let mut parts = key.split_whitespace();
let (Some(key_type), Some(public_key)) = (parts.next(), parts.next()) else {
continue;
};
let pair = (key_type, public_key);
if !pinned.contains(&pair) && !fresh.contains(&pair) {
fresh.push(pair);
}
}
if fresh.is_empty() {
return Ok(false);
}
let rotated: HashSet<&str> = fresh.iter().map(|(key_type, _)| *key_type).collect();
let mut out: String = existing
.split_inclusive('\n')
.filter(|line| match pinned_line(line) {
Some((host, (key_type, _))) => host != alias || !rotated.contains(key_type),
None => true,
})
.collect();
if !out.is_empty() && !out.ends_with('\n') {
out.push('\n');
}
for (key_type, public_key) in fresh {
out.push_str(&format!("{alias} {key_type} {public_key}\n"));
}
write_atomic(path, out.as_bytes(), Some(0o600))?;
Ok(true)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DevicePin {
pub alias: String,
pub signed_host_key: Option<String>,
}
fn key_pair(key: &str) -> Option<(&str, &str)> {
let mut parts = key.split_whitespace();
Some((parts.next()?, parts.next()?))
}
fn pinned_line(line: &str) -> Option<(&str, (&str, &str))> {
let mut parts = line.split_whitespace();
let host = parts
.next()
.filter(|h| !h.starts_with('#') && !h.starts_with('@'))?;
Some((host, (parts.next()?, parts.next()?)))
}
pub fn unpin_devices(path: &Path, cut: &[DevicePin], kept: &[DevicePin]) -> Result<bool> {
if cut.is_empty() {
return Ok(false);
}
let _guard = known_hosts_guard();
let existing = read_known_hosts(path)?;
let keys_of = |pins: &[DevicePin]| -> HashSet<(String, String)> {
let aliases: HashSet<&str> = pins.iter().map(|p| p.alias.as_str()).collect();
let mut keys: HashSet<(String, String)> = existing
.lines()
.filter_map(pinned_line)
.filter(|(host, _)| aliases.contains(host))
.map(|(_, (t, k))| (t.to_string(), k.to_string()))
.collect();
keys.extend(
pins.iter()
.filter_map(|p| p.signed_host_key.as_deref().and_then(key_pair))
.map(|(t, k)| (t.to_string(), k.to_string())),
);
keys
};
let kept_keys = keys_of(kept);
let cut_keys: HashSet<(String, String)> =
keys_of(cut).difference(&kept_keys).cloned().collect();
let cut_aliases: HashSet<&str> = cut.iter().map(|p| p.alias.as_str()).collect();
let kept_lines: String = existing
.split_inclusive('\n')
.filter(|line| match pinned_line(line) {
Some((host, (t, k))) => {
!cut_aliases.contains(host) && !cut_keys.contains(&(t.to_string(), k.to_string()))
}
None => true,
})
.collect();
if kept_lines == existing {
return Ok(false);
}
write_atomic(path, kept_lines.as_bytes(), Some(0o600))?;
Ok(true)
}
pub(crate) fn write_atomic(path: &Path, bytes: &[u8], mode: Option<u32>) -> Result<()> {
let target = if fs::symlink_metadata(path).is_ok_and(|m| m.file_type().is_symlink()) {
fs::canonicalize(path).with_context(|| format!("resolving symlink {}", path.display()))?
} else {
path.to_path_buf()
};
let dir = match target.parent() {
Some(p) if !p.as_os_str().is_empty() => p.to_path_buf(),
_ => PathBuf::from("."),
};
fs::DirBuilder::new()
.recursive(true)
.mode(0o700)
.create(&dir)
.with_context(|| format!("creating {}", dir.display()))?;
let mode = mode.unwrap_or_else(|| {
fs::metadata(&target)
.map(|m| m.permissions().mode() & 0o7777)
.unwrap_or(0o600)
});
let name = target
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
let tmp = dir.join(format!(
".{name}.sb-{}-{}.tmp",
std::process::id(),
TEMP_SEQ.fetch_add(1, Ordering::Relaxed)
));
let written = (|| -> Result<()> {
let mut file = fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o600)
.open(&tmp)
.with_context(|| format!("creating {}", tmp.display()))?;
file.write_all(bytes)?;
file.set_permissions(fs::Permissions::from_mode(mode))?;
file.sync_all()?;
fs::rename(&tmp, &target).with_context(|| format!("replacing {}", target.display()))?;
fs::File::open(&dir).and_then(|d| d.sync_all())?;
Ok(())
})();
if written.is_err() {
let _ = fs::remove_file(&tmp);
}
written
}
#[derive(Debug, Clone, Default)]
struct ConnectOptions {
port: Option<u16>,
host_key_alias: Option<String>,
identity: Option<PathBuf>,
}
pub fn untrusted_host_hint(host: &str) -> String {
format!(
"if ssh reported a host-key error, the host is not in ~/.second-brain/known_hosts yet — \
run `sb sync trust {host}` to verify and pin its fingerprint."
)
}
pub fn ssh_args(known_hosts: &Path) -> Vec<OsString> {
let mut user_kh = OsString::from("UserKnownHostsFile=");
user_kh.push(known_hosts);
vec![
OsString::from("-o"),
OsString::from("StrictHostKeyChecking=yes"),
OsString::from("-o"),
user_kh,
OsString::from("-o"),
OsString::from("GlobalKnownHostsFile=/dev/null"),
OsString::from("-o"),
OsString::from("ServerAliveInterval=30"),
]
}
pub struct SshTransport {
host: String,
sb: String,
known_hosts: PathBuf,
unattended: bool,
options: ConnectOptions,
ephemeral: Option<EphemeralIdentity>,
}
impl SshTransport {
pub fn new(host: &str, sb: &str) -> Result<Self> {
Ok(Self {
host: host.to_string(),
sb: sb.to_string(),
known_hosts: known_hosts_path()?,
unattended: false,
options: ConnectOptions::default(),
ephemeral: None,
})
}
pub fn unattended(mut self) -> Self {
self.unattended = true;
self
}
pub fn known_hosts(mut self, path: PathBuf) -> Self {
self.known_hosts = path;
self
}
pub fn port(mut self, port: u16) -> Self {
self.options.port = Some(port);
self
}
pub fn host_key_alias(mut self, alias: &str) -> Self {
self.options.host_key_alias = Some(alias.to_string());
self
}
pub fn identity(mut self, key_file: PathBuf) -> Self {
self.options.identity = Some(key_file);
self
}
pub fn ephemeral_identity(mut self, identity: EphemeralIdentity) -> Self {
self.options.identity = Some(identity.path().to_path_buf());
self.ephemeral = Some(identity);
self
}
fn ssh_options(&self) -> Vec<OsString> {
let mut args = ssh_args(&self.known_hosts);
if self.unattended {
args.extend(
["-o", "BatchMode=yes", "-o", "ConnectTimeout=15"]
.into_iter()
.map(OsString::from),
);
}
if let Some(port) = self.options.port {
args.extend([OsString::from("-p"), OsString::from(port.to_string())]);
}
if let Some(alias) = &self.options.host_key_alias {
args.extend([
OsString::from("-o"),
OsString::from(format!("HostKeyAlias={alias}")),
]);
}
if let Some(identity) = &self.options.identity {
args.extend([
OsString::from("-i"),
identity.clone().into_os_string(),
OsString::from("-o"),
OsString::from("IdentitiesOnly=yes"),
]);
}
args
}
fn ssh(&self) -> Command {
let mut command = Command::new("ssh");
command.args(self.ssh_options());
command
}
fn remote(&self, options: &[&str], args: &[&str]) -> Command {
let mut command = self.ssh();
command
.args(options)
.arg("--")
.arg(&self.host)
.arg(&self.sb)
.args(args);
command
}
}
impl Transport for SshTransport {
fn host(&self) -> &str {
&self.host
}
fn whoami(&self) -> Result<String> {
let output = self
.remote(&[], &["whoami"])
.output()
.context("spawning ssh to resolve peer machine id")?;
if !output.status.success() {
anyhow::bail!(
"ssh whoami on {} exited with {}: {}",
self.host,
output.status,
String::from_utf8_lossy(&output.stderr).trim()
);
}
let id = String::from_utf8_lossy(&output.stdout).trim().to_string();
if id.is_empty() {
anyhow::bail!("peer {} returned an empty machine id", self.host);
}
Ok(id)
}
fn export(
&self,
after_seq: u64,
hello: &Hello,
) -> Result<(Box<dyn Read + Send>, Box<dyn Remote>)> {
let mut child = self
.remote(&[], &["sync-export", "--after-seq", &after_seq.to_string()])
.arg("--hello")
.arg(hello.to_arg())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.context("spawning ssh for pull")?;
let output = child
.stdout
.take()
.expect("ssh export spawned with piped stdout");
Ok((Box::new(output), Box::new(SshRemote(child))))
}
fn import(&self) -> Result<(Box<dyn Write + Send>, Box<dyn Read + Send>, Box<dyn Remote>)> {
self.spawn_push("sync-import", &[])
}
fn snapshot_export(&self, hello: &Hello) -> Result<(Box<dyn Read + Send>, Box<dyn Remote>)> {
let mut child = self
.remote(&[], &["sync-snapshot", "--hello"])
.arg(hello.to_arg())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.context("spawning ssh for snapshot export")?;
let output = child
.stdout
.take()
.expect("ssh snapshot export spawned with piped stdout");
Ok((Box::new(output), Box::new(SshRemote(child))))
}
fn snapshot_import(
&self,
) -> Result<(Box<dyn Write + Send>, Box<dyn Read + Send>, Box<dyn Remote>)> {
self.spawn_push("sync-snapshot", &["--apply"])
}
}
impl SshTransport {
fn spawn_push(
&self,
command: &str,
extra: &[&str],
) -> Result<(Box<dyn Write + Send>, Box<dyn Read + Send>, Box<dyn Remote>)> {
let mut child = self
.remote(
&[
"-o",
"ServerAliveInterval=60",
"-o",
"ServerAliveCountMax=60",
],
&[command],
)
.args(extra)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.context("spawning ssh for push")?;
let input = child
.stdin
.take()
.expect("ssh push spawned with piped stdin");
let output = child
.stdout
.take()
.expect("ssh push spawned with piped stdout");
Ok((
Box::new(input),
Box::new(output),
Box::new(SshRemote(child)),
))
}
}
struct SshRemote(Child);
impl Remote for SshRemote {
fn wait(&mut self) -> Result<Exit> {
let status = self.0.wait()?;
Ok(Exit {
success: status.success(),
status: status.to_string(),
})
}
fn stop(&mut self) -> Result<()> {
if let Err(e) = self.0.kill() {
tracing::debug!("ssh already exited: {e}");
}
self.0.wait()?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
const KEY_1: &str =
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl";
const KEY_2: &str = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC0";
fn transport() -> SshTransport {
SshTransport {
host: "box".to_string(),
sb: "sb".to_string(),
known_hosts: PathBuf::from("/tmp/kh"),
unattended: false,
options: ConnectOptions::default(),
ephemeral: None,
}
}
fn args_of(command: &Command) -> Vec<String> {
command
.get_args()
.map(|a| a.to_string_lossy().into_owned())
.collect()
}
fn has_pair(args: &[String], a: &str, b: &str) -> bool {
args.windows(2).any(|w| w[0] == a && w[1] == b)
}
#[test]
fn an_ephemeral_identity_reaches_ssh_and_goes_with_the_transport() {
let base = tempfile::tempdir().unwrap();
let data_dir = tempfile::tempdir().unwrap();
let key = ed25519_dalek::SigningKey::from_bytes(&[3; 32]);
let identity =
EphemeralIdentity::create_in(base.path(), data_dir.path(), &key, "sb").unwrap();
let path = identity.path().to_string_lossy().into_owned();
let t = transport().ephemeral_identity(identity);
let args = args_of(&t.ssh());
assert!(has_pair(&args, "-i", &path));
assert!(has_pair(&args, "-o", "IdentitiesOnly=yes"));
drop(t);
assert!(!Path::new(&path).exists());
}
#[test]
fn device_alias_prefixes_machine_id() {
assert_eq!(device_alias("m-1"), "sb-m-1");
}
#[test]
fn builder_options_reach_the_ssh_command() {
let t = transport()
.known_hosts(PathBuf::from("/x/known"))
.port(2222)
.host_key_alias("sb-m-1")
.identity(PathBuf::from("/x/id"))
.unattended();
let args = args_of(&t.ssh());
assert!(has_pair(&args, "-o", "UserKnownHostsFile=/x/known"));
assert!(has_pair(&args, "-p", "2222"));
assert!(has_pair(&args, "-o", "HostKeyAlias=sb-m-1"));
assert!(has_pair(&args, "-i", "/x/id"));
assert!(has_pair(&args, "-o", "IdentitiesOnly=yes"));
assert!(has_pair(&args, "-o", "BatchMode=yes"));
}
#[test]
fn the_host_is_never_read_as_an_option() {
let mut t = transport();
t.host = "-oProxyCommand=touch /tmp/x".to_string();
let args = args_of(&t.remote(&["-o", "ServerAliveInterval=60"], &["whoami"]));
let end = args.len();
assert_eq!(
args[end - 4..],
["--", "-oProxyCommand=touch /tmp/x", "sb", "whoami"]
);
assert!(has_pair(&args, "-o", "ServerAliveInterval=60"));
}
#[test]
fn default_transport_adds_no_extra_options() {
let args = args_of(&transport().ssh());
assert!(!args.iter().any(|a| a == "-p" || a == "-i"));
assert!(!args.iter().any(|a| a.starts_with("HostKeyAlias")));
}
#[test]
fn pin_alias_adds_missing_keys_once() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("known_hosts");
std::fs::write(&path, "other ssh-rsa ZZZ").unwrap();
let keys = vec![format!("{KEY_1} comment"), KEY_2.to_string()];
assert!(pin_alias(&path, "sb-m-1", &keys).unwrap());
let text = std::fs::read_to_string(&path).unwrap();
assert_eq!(
text,
format!("other ssh-rsa ZZZ\nsb-m-1 {KEY_1}\nsb-m-1 {KEY_2}\n")
);
assert!(!pin_alias(&path, "sb-m-1", &keys).unwrap());
assert_eq!(std::fs::read_to_string(&path).unwrap(), text);
assert!(pin_alias(&path, "sb-m-2", &keys[..1]).unwrap());
assert_eq!(
std::fs::metadata(&path).unwrap().permissions().mode() & 0o777,
0o600
);
}
#[test]
fn unpinning_a_device_drops_its_alias_and_its_keys_under_other_names() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("known_hosts");
let original = format!(
"# note\nhost {KEY_1}\n[host]:2222 {KEY_2}\nsb-m-1 {KEY_1}\nsb-m-10 {KEY_2}\nsb-m-1 {KEY_2}\n"
);
std::fs::write(&path, &original).unwrap();
let pin = |alias: &str, key: Option<&str>| DevicePin {
alias: alias.to_string(),
signed_host_key: key.map(str::to_string),
};
assert!(unpin_devices(&path, &[pin("sb-m-1", None)], &[pin("sb-m-10", None)]).unwrap());
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
format!("# note\n[host]:2222 {KEY_2}\nsb-m-10 {KEY_2}\n")
);
let before = std::fs::metadata(&path).unwrap().modified().unwrap();
assert!(!unpin_devices(&path, &[pin("sb-m-1", None)], &[pin("sb-m-10", None)]).unwrap());
assert_eq!(
std::fs::metadata(&path).unwrap().modified().unwrap(),
before
);
assert!(unpin_devices(&path, &[pin("sb-m-10", Some(KEY_2))], &[]).unwrap());
assert_eq!(std::fs::read_to_string(&path).unwrap(), "# note\n");
assert!(!unpin_devices(&dir.path().join("none"), &[pin("sb-m-1", None)], &[]).unwrap());
}
#[test]
fn a_rotated_host_key_replaces_the_old_one_of_its_type() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("known_hosts");
let rotated = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIRotatedHostKeyOfTheDevice";
pin_alias(&path, "sb-m-1", &[KEY_1.to_string(), KEY_2.to_string()]).unwrap();
pin_alias(&path, "sb-m-2", &[KEY_1.to_string()]).unwrap();
assert!(pin_alias(&path, "sb-m-1", &[rotated.to_string()]).unwrap());
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
format!("sb-m-1 {KEY_2}\nsb-m-2 {KEY_1}\nsb-m-1 {rotated}\n")
);
}
#[test]
fn an_alias_that_could_add_a_known_hosts_line_is_refused() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("known_hosts");
let keys = [KEY_1.to_string()];
for alias in [
"sb-x\n@cert-authority *",
"sb-x y",
"sb-x\0",
"@revoked",
"",
] {
assert!(pin_alias(&path, alias, &keys).is_err(), "{alias:?}");
}
assert!(!path.exists());
}
#[test]
fn concurrent_pins_of_one_file_all_land() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("known_hosts");
let threads: Vec<_> = (0..8)
.map(|t| {
let path = path.clone();
std::thread::spawn(move || {
for i in 0..25 {
pin_alias(&path, &format!("sb-{t}-{i}"), &[KEY_1.to_string()]).unwrap();
}
})
})
.collect();
for thread in threads {
thread.join().unwrap();
}
assert_eq!(std::fs::read_to_string(&path).unwrap().lines().count(), 200);
}
}