2025-02-25 02:22:05 -05:00

66 lines
1.9 KiB
Rust

use std::net::Ipv4Addr;
use sparse_actions::{
adapter::{BeaconAdapter, BeaconInterface, BeaconNetworkingInfo, BeaconRoute},
error,
};
#[derive(thiserror::Error, Debug)]
pub enum FreeBsdAdapterError {}
impl sparse_actions::error::AdapterError for FreeBsdAdapterError {}
#[derive(Clone)]
pub struct FreeBsdAdapter;
#[async_trait::async_trait]
impl BeaconAdapter for FreeBsdAdapter {
type Error = FreeBsdAdapterError;
const OPERATING_SYSTEM: &'static str = "Windows";
fn interface_name_from_interface(interface: &BeaconInterface) -> Vec<u8> {
interface.name.clone()
}
fn networking_info(&self) -> Result<BeaconNetworkingInfo, error::BeaconError<Self::Error>> {
Ok(BeaconNetworkingInfo {
routes: vec![],
interfaces: vec![],
})
}
async fn get_username(&self) -> Result<String, error::BeaconError<Self::Error>> {
let passwd = tokio::fs::read_to_string("/etc/passwd").await?;
let uid = unsafe { libc::getuid() };
Ok(passwd
.split("\n")
.find_map(|row| -> Option<String> {
let mut entries = row.split(":");
let name = entries.next()?;
entries.next()?;
let euid = entries.next()?.parse::<u32>().ok()?;
if euid == uid {
Some(name.to_string())
} else {
None
}
})
.unwrap_or("(unknown)".to_string()))
}
async fn get_hostname(&self) -> Result<String, error::BeaconError<Self::Error>> {
Ok(tokio::fs::read_to_string("/etc/rc.conf")
.await?
.split("\n")
.map(|line| line.split("=").collect::<Vec<_>>())
.find(|line| line.get(0) == Some(&"hostname"))
.map(|line| line.get(1).map(|name| name.to_string()))
.flatten()
.unwrap_or("(unknown)".to_string()))
}
}