use std::collections::BTreeMap;
use std::net::{IpAddr, Ipv4Addr};
use std::path::PathBuf;
use isoloom_manifest::Manifest;
use serde::{Deserialize, Serialize};
use super::{DETACH_TIMEOUT, Vagrant, status};
use crate::Result;
use crate::driver::{ADHOC_NAME, Attached};
use crate::error::Error;
use crate::executor::{Cx, Echo, call_ok};
use crate::state::Op;
use crate::status::MachineStatus;
const DIR: &str = "adhoc";
const HOOK: &str = "IsoloomAdHoc";
const PROVIDERS: [&str; 5] = ["virtualbox", "vmware_desktop", "parallels", "libvirt", "qemu"];
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Node {
network: String,
address: Ipv4Addr,
#[serde(default, skip_serializing_if = "Option::is_none")]
r#box: Option<String>,
}
fn dir(v: &Vagrant, cx: &Cx<'_>) -> Result<PathBuf> {
Ok(v.dotfile(cx)?.join(DIR))
}
fn files(v: &Vagrant, cx: &Cx<'_>) -> Result<(PathBuf, PathBuf)> {
let dir = dir(v, cx)?;
Ok((dir.join(format!("{ADHOC_NAME}.rb")), dir.join(format!("{ADHOC_NAME}.json"))))
}
pub(super) fn present(v: &Vagrant, cx: &Cx<'_>) -> bool {
files(v, cx).is_ok_and(|(rb, _)| rb.is_file())
}
fn node(v: &Vagrant, cx: &Cx<'_>) -> Option<Node> {
let (_, json) = files(v, cx).ok()?;
serde_json::from_str(&std::fs::read_to_string(json).ok()?).ok()
}
fn ruby_str(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 2);
out.push('"');
for c in s.chars() {
match c {
'\\' | '"' | '#' => {
out.push('\\');
out.push(c);
}
c => out.push(c),
}
}
out.push('"');
out
}
fn definition(node: &Node) -> String {
let r#box = node.r#box.as_deref().map_or_else(|| "nil".to_owned(), ruby_str);
format!(
"# The attack VM the runner added to this running lab (`isothrum attach`); `isothrum detach` removes it.\n{HOOK}.define(config, {}, network: {}, ip: {}, box: {box})\n",
ruby_str(ADHOC_NAME),
ruby_str(&node.network),
ruby_str(&node.address.to_string())
)
}
fn network(manifest: &Manifest, chosen: Option<&str>) -> Result<String> {
let names: Vec<&str> = manifest.networks.iter().map(|n| n.name.as_str()).collect();
match chosen {
Some(n) if names.contains(&n) => Ok(n.to_owned()),
Some(n) => Err(Error::Unsupported {
what: format!("an ad-hoc attack VM on network `{n}` (this lab's networks: {})", names.join(", ")),
}),
None => names.first().map(|n| (*n).to_owned()).ok_or_else(|| Error::Unsupported {
what: "an ad-hoc attack VM (this lab has no network)".into(),
}),
}
}
fn free_address(manifest: &Manifest, network: &str) -> Result<Ipv4Addr> {
let cidr = manifest
.networks
.iter()
.find(|n| n.name.as_str() == network)
.map(|n| n.cidr.to_string())
.unwrap_or_default();
let block = || Error::Unsupported {
what: format!("an ad-hoc attack VM on network `{network}` ({cidr}: not an IPv4 block of /24 to /29)"),
};
let (base, len) = cidr.split_once('/').ok_or_else(block)?;
let base: Ipv4Addr = base.parse().map_err(|_| block())?;
let len: u8 = len.parse().map_err(|_| block())?;
if !(24..=29).contains(&len) {
return Err(block());
}
let taken: Vec<Ipv4Addr> = manifest
.machines
.iter()
.filter_map(|m| m.addresses.iter().find(|(n, _)| n.as_str() == network))
.filter_map(|(_, a)| match a {
IpAddr::V4(v4) => Some(*v4),
IpAddr::V6(_) => None,
})
.collect();
let size = 1u32 << (32 - len);
let (lowest, mut ip) = (u32::from(base) + 2, u32::from(base) + size - 4);
while taken.contains(&Ipv4Addr::from(ip)) {
if ip <= lowest {
return Err(Error::Unsupported {
what: format!("an ad-hoc attack VM on network `{network}` (no address left)"),
});
}
ip -= 1;
}
Ok(Ipv4Addr::from(ip))
}
fn refusal(provider: &str, vagrantfile: &str) -> Option<String> {
if !PROVIDERS.contains(&provider) {
return Some(format!(
"an ad-hoc attack VM on `{provider}` (it joins VirtualBox, VMware, Parallels, libvirt and QEMU networks; declare an `access: true` machine for the others)"
));
}
(!vagrantfile.contains(HOOK))
.then(|| "an ad-hoc attack VM (this lab's Vagrantfile predates the hook: generate it again with Isoloom 1.0.9 or later)".to_owned())
}
fn check(cx: &Cx<'_>) -> Result<()> {
let vagrantfile = cx.run.dir().join("Vagrantfile");
let text = std::fs::read_to_string(&vagrantfile).map_err(Error::io(&vagrantfile, "read"))?;
match refusal(cx.scope.provider.as_deref().unwrap_or_default(), &text) {
Some(what) => Err(Error::Unsupported { what }),
None => Ok(()),
}
}
pub(super) async fn attach(v: &Vagrant, cx: &Cx<'_>, image: Option<&str>, network: Option<&str>) -> Result<Attached> {
check(cx)?;
let manifest = cx.run.manifest();
let network = self::network(manifest, network)?;
let node = Node {
address: free_address(manifest, &network)?,
network,
r#box: image.map(str::to_owned),
};
let (rb, json) = files(v, cx)?;
let dir = dir(v, cx)?;
std::fs::create_dir_all(&dir).map_err(Error::io(&dir, "created"))?;
std::fs::write(&rb, definition(&node)).map_err(Error::io(&rb, "written"))?;
let record = serde_json::to_string(&node).unwrap_or_default();
std::fs::write(&json, record).map_err(Error::io(&json, "written"))?;
v.boot(cx, ADHOC_NAME, image).await?;
Ok(Attached {
name: Some(ADHOC_NAME.to_owned()),
network: Some(node.network),
address: Some(node.address.to_string()),
})
}
pub(super) async fn detach(v: &Vagrant, cx: &Cx<'_>) -> Result<()> {
if !present(v, cx) {
return Ok(());
}
let spec = v.vagrant(cx, Op::Detach.as_str(), &["destroy", "-f", ADHOC_NAME])?;
call_ok(cx, Op::Detach.as_str(), &spec, DETACH_TIMEOUT, Echo::Shown).await?;
let (rb, json) = files(v, cx)?;
drop(std::fs::remove_file(rb));
drop(std::fs::remove_file(json));
Ok(())
}
pub(super) fn status(v: &Vagrant, cx: &Cx<'_>, read: &status::Reading) -> Option<MachineStatus> {
if !present(v, cx) {
return None;
}
let state = read
.states
.get(ADHOC_NAME)
.map_or(crate::status::MachineState::Absent, |s| status::machine_state(s));
let addresses: BTreeMap<String, String> = node(v, cx).map(|n| [(n.network, n.address.to_string())].into()).unwrap_or_default();
Some(MachineStatus {
name: ADHOC_NAME.to_owned(),
role: isoloom_manifest::Role::Access,
state,
addresses,
published: Vec::new(),
})
}
#[cfg(test)]
mod tests {
use super::*;
fn manifest() -> Manifest {
isoloom_manifest::parse(crate::testing::FLAG_VM).unwrap()
}
#[test]
fn the_definition_is_one_hook_call_with_its_strings_kept_literal() {
let node = Node {
network: "lab".into(),
address: "10.61.0.252".parse().unwrap(),
r#box: Some("me/kali \"2026\" #1".into()),
};
let text = definition(&node);
assert!(text.starts_with("# The attack VM the runner added"), "{text}");
assert!(
text.ends_with("IsoloomAdHoc.define(config, \"isoloom-access\", network: \"lab\", ip: \"10.61.0.252\", box: \"me/kali \\\"2026\\\" \\#1\")\n"),
"{text}"
);
let default = definition(&Node { r#box: None, ..node });
assert!(default.ends_with("box: nil)\n"), "the host's default box: {default}");
}
#[test]
fn the_address_is_the_highest_free_one_below_isolooms_own() {
assert_eq!(free_address(&manifest(), "lab").unwrap().to_string(), "10.61.0.252");
let mut m = manifest();
let taken: isoloom_manifest::Name = "lab".parse().unwrap();
m.machines[0].addresses.insert(taken, "10.61.0.252".parse().unwrap());
assert_eq!(free_address(&m, "lab").unwrap().to_string(), "10.61.0.251");
assert_eq!(free_address(&manifest(), "nope").unwrap_err().code(), crate::ErrorCode::Unsupported);
}
#[test]
fn the_hook_serves_the_desktop_hypervisors_libvirt_and_qemu_from_a_vagrantfile_that_has_it() {
let hooked = "# generated\nmodule IsoloomAdHoc\nend\n";
for p in PROVIDERS {
assert_eq!(refusal(p, hooked), None, "{p}");
}
for p in ["hyperv", "vmware_esxi", "utm", ""] {
assert!(refusal(p, hooked).is_some_and(|why| why.contains(&format!("on `{p}`"))), "{p}");
}
assert!(refusal("qemu", "# generated before the hook\n").is_some_and(|why| why.contains("predates the hook")));
}
#[test]
fn the_network_is_the_chosen_one_else_the_first() {
let m = manifest();
assert_eq!(network(&m, None).unwrap(), "lab");
assert_eq!(network(&m, Some("lab")).unwrap(), "lab");
let err = network(&m, Some("dmz")).unwrap_err().to_string();
assert!(err.contains("network `dmz`") && err.contains("lab"), "{err}");
}
}