use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;
use serde::{Deserialize, Serialize};
use crate::config::is_ssh_alias;
const INCLUDE_DEPTH: usize = 16;
pub fn ssh_dir() -> Option<PathBuf> {
std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".ssh"))
}
pub fn user_config() -> Option<PathBuf> {
ssh_dir().map(|dir| dir.join("config"))
}
pub fn list(config: &Path) -> io::Result<Vec<String>> {
let text = match fs::read_to_string(config) {
Ok(text) => text,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(error) => return Err(error),
};
let base = config.parent().unwrap_or(Path::new("/"));
let mut aliases = Vec::new();
collect(&text, base, 0, &mut aliases);
Ok(aliases)
}
fn collect(text: &str, base: &Path, depth: usize, aliases: &mut Vec<String>) {
for line in text.lines() {
let Some((keyword, args)) = directive(line) else {
continue;
};
if keyword.eq_ignore_ascii_case("host") {
for name in args {
if is_ssh_alias(&name) && !aliases.contains(&name) {
aliases.push(name);
}
}
} else if keyword.eq_ignore_ascii_case("include") && depth < INCLUDE_DEPTH {
for pattern in args {
for file in expand(&pattern, base) {
if let Ok(text) = fs::read_to_string(&file) {
collect(&text, base, depth + 1, aliases);
}
}
}
}
}
}
fn directive(line: &str) -> Option<(String, Vec<String>)> {
let line = line.trim();
let split = line.find(|c: char| c.is_whitespace() || c == '=')?;
let keyword = &line[..split];
let rest = line[split..].trim_start();
let rest = rest.strip_prefix('=').unwrap_or(rest);
let mut args = Vec::new();
let mut chars = rest.chars().peekable();
loop {
while chars.peek().is_some_and(|c| c.is_whitespace()) {
chars.next();
}
match chars.peek() {
None | Some('#') => break,
Some('"') => {
chars.next();
args.push(chars.by_ref().take_while(|&c| c != '"').collect());
}
Some(_) => {
let mut arg = String::new();
while let Some(&c) = chars.peek().filter(|c| !c.is_whitespace()) {
arg.push(c);
chars.next();
}
args.push(arg);
}
}
}
(!keyword.starts_with('#')).then(|| (keyword.to_string(), args))
}
fn expand(pattern: &str, base: &Path) -> Vec<PathBuf> {
let path = match pattern.strip_prefix("~/") {
Some(rest) => match std::env::var_os("HOME") {
Some(home) => PathBuf::from(home).join(rest),
None => return Vec::new(),
},
None => base.join(pattern),
};
let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
return Vec::new();
};
if !name.contains(['*', '?']) {
return vec![path];
}
let Some(dir) = path.parent() else {
return Vec::new();
};
let Ok(entries) = fs::read_dir(dir) else {
return Vec::new();
};
let mut files: Vec<PathBuf> = entries
.filter_map(Result::ok)
.filter(|entry| entry.file_name().to_str().is_some_and(|n| glob(name, n)))
.map(|entry| entry.path())
.filter(|path| path.is_file())
.collect();
files.sort();
files
}
fn glob(pattern: &str, name: &str) -> bool {
fn at(p: &[char], n: &[char]) -> bool {
match p.split_first() {
None => n.is_empty(),
Some(('*', rest)) => (0..=n.len()).any(|skip| at(rest, &n[skip..])),
Some((&c, rest)) => n
.split_first()
.is_some_and(|(&first, tail)| (c == '?' || c == first) && at(rest, tail)),
}
}
let p: Vec<char> = pattern.chars().collect();
let n: Vec<char> = name.chars().collect();
at(&p, &n)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Resolved {
pub host_name: String,
pub user: String,
pub port: u16,
}
#[derive(Debug, thiserror::Error)]
pub enum ResolveError {
#[error("not an ssh alias")]
NotAnAlias,
#[error("could not run ssh: {0}")]
Spawn(io::Error),
#[error("ssh -G failed: {0}")]
Failed(String),
#[error("ssh -G printed no {0}")]
Missing(&'static str),
}
pub fn resolve(alias: &str, config: Option<&Path>) -> Result<Resolved, ResolveError> {
if !is_ssh_alias(alias) {
return Err(ResolveError::NotAnAlias);
}
let mut command = Command::new("ssh");
if let Some(config) = config {
command.arg("-F").arg(config);
}
let output = command
.args(["-G", "--", alias])
.output()
.map_err(ResolveError::Spawn)?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(ResolveError::Failed(stderr.trim().to_string()));
}
let stdout = String::from_utf8_lossy(&output.stdout);
let value = |key: &'static str| {
stdout
.lines()
.find_map(|line| line.strip_prefix(key)?.strip_prefix(' '))
.map(str::to_string)
.ok_or(ResolveError::Missing(key))
};
Ok(Resolved {
host_name: value("hostname")?,
user: value("user")?,
port: value("port")?
.parse()
.map_err(|_| ResolveError::Missing("port"))?,
})
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HostDraft {
pub alias: String,
pub host_name: String,
pub user: String,
pub identity_file: String,
pub port: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum HostField {
Alias,
HostName,
User,
IdentityFile,
Port,
}
impl HostDraft {
pub fn block(&self) -> Result<String, Vec<(HostField, &'static str)>> {
let problems = self.problems();
if !problems.is_empty() {
return Err(problems);
}
let mut block = format!(
"Host {}\n HostName {}\n",
self.alias.trim(),
self.host_name.trim()
);
if !self.user.trim().is_empty() {
block.push_str(&format!(" User {}\n", self.user.trim()));
}
if !self.identity_file.trim().is_empty() {
block.push_str(&format!(" IdentityFile {}\n", self.identity_file.trim()));
}
let port = self.port_number().unwrap_or(22);
if port != 22 {
block.push_str(&format!(" Port {port}\n"));
}
Ok(block)
}
pub fn matches(&self, resolved: &Resolved) -> bool {
let user = self.user.trim();
resolved
.host_name
.eq_ignore_ascii_case(self.host_name.trim())
&& (user.is_empty() || resolved.user == user)
&& Some(resolved.port) == self.port_number()
}
fn port_number(&self) -> Option<u16> {
let port = self.port.trim();
if port.is_empty() {
return Some(22);
}
port.parse().ok().filter(|&port| port != 0)
}
fn problems(&self) -> Vec<(HostField, &'static str)> {
let word = |value: &str, extra: &[char]| {
value.chars().all(|c| {
c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-') || extra.contains(&c)
}) && !value.starts_with('-')
};
let mut problems = Vec::new();
if !is_ssh_alias(self.alias.trim()) {
problems.push((
HostField::Alias,
"Use only letters, digits, `.`, `-` and `_`.",
));
}
let host_name = self.host_name.trim();
if host_name.is_empty() {
problems.push((
HostField::HostName,
"Enter the server's host name or IP address.",
));
} else if !word(host_name, &[':']) {
problems.push((
HostField::HostName,
"Use a host name or IP address, such as 203.0.113.7.",
));
}
if !word(self.user.trim(), &[]) {
problems.push((
HostField::User,
"Use only letters, digits, `.`, `-` and `_`.",
));
}
let identity = self.identity_file.trim();
if !identity.is_empty()
&& !(identity.starts_with("~/.ssh/id_") && word(&identity["~/.ssh/".len()..], &[]))
{
problems.push((HostField::IdentityFile, "Pick a key from ~/.ssh."));
}
if self.port_number().is_none() {
problems.push((HostField::Port, "Use a port from 1 to 65535."));
}
problems
}
}
pub fn identity_files(ssh_dir: &Path) -> Vec<String> {
let Ok(entries) = fs::read_dir(ssh_dir) else {
return Vec::new();
};
let mut names: Vec<String> = entries
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_file()))
.filter_map(|entry| entry.file_name().into_string().ok())
.filter(|name| name.starts_with("id_") && !name.ends_with(".pub"))
.map(|name| format!("~/.ssh/{name}"))
.collect();
names.sort();
names
}
#[cfg(test)]
mod tests;