use std::net::ToSocketAddrs;
use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct HostInfo {
pub canonical: String,
pub ip: String,
}
pub trait DaemonPlatform: Send + Sync {
fn home_dir(&self, user: &str) -> Option<PathBuf>;
fn lookup_host(&self, host: &str) -> Option<HostInfo>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct StdPlatform;
impl DaemonPlatform for StdPlatform {
fn home_dir(&self, user: &str) -> Option<PathBuf> {
if cfg!(windows) {
return None;
}
let text = std::fs::read_to_string("/etc/passwd").ok()?;
passwd_home(&text, user)
}
fn lookup_host(&self, host: &str) -> Option<HostInfo> {
if host.is_empty() {
return None;
}
let addr = (host, 0u16).to_socket_addrs().ok()?.next()?;
Some(HostInfo {
canonical: host.to_string(),
ip: addr.ip().to_string(),
})
}
}
pub(super) fn passwd_home(text: &str, user: &str) -> Option<PathBuf> {
text.lines().find_map(|line| {
let fields: Vec<&str> = line.split(':').collect();
(fields.len() >= 7 && fields[0] == user).then(|| PathBuf::from(fields[5]))
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn passwd_home_reads_sixth_field() {
let text = "root:x:0:0:root:/root:/bin/sh\nalice:x:1000:1000::/srv/users/alice:/bin/sh\n";
assert_eq!(
passwd_home(text, "alice"),
Some(PathBuf::from("/srv/users/alice"))
);
assert_eq!(passwd_home(text, "bob"), None);
}
#[test]
fn std_lookup_resolves_numeric_hosts() {
let info = StdPlatform.lookup_host("127.0.0.1").unwrap();
assert_eq!(info.ip, "127.0.0.1");
assert!(StdPlatform.lookup_host("").is_none());
}
}