use core::fmt;
use std::io;
use std::path::Path;
use std::sync::Arc;
use serde::Deserialize;
use super::srt::{Call, Output, Srt};
pub(crate) struct Held {
pub(crate) srt: Arc<dyn Srt>,
pub(crate) sid: String,
pub(crate) holder: u32,
}
impl fmt::Debug for Held {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Held")
.field("sid", &self.sid)
.field("holder", &self.holder)
.finish_non_exhaustive()
}
}
impl Held {
fn call(&self, verb: &str) -> Call {
Call::new([
"acl",
verb,
"--holder-pid",
&self.holder.to_string(),
"--sandbox-user-sid",
&self.sid,
"--json",
])
}
}
impl Drop for Held {
fn drop(&mut self) {
if revoked(&self.srt.run(&self.call("revoke"))) {
let _ = self.srt.run(&self.call("restore"));
} else {
eprintln!(
"crabpot: the sandbox's grants could not all be revoked, so its denies stay in place; `crabpot --srt-win acl recover` clears them once this process has exited"
);
}
}
}
#[derive(Deserialize)]
struct Outcome {
path: String,
status: String,
}
impl Outcome {
fn released(&self) -> bool {
match self.status.as_str() {
"revoked" | "stillHeld" | "downgraded" | "relocated" | "mismatch" | "noRow"
| "gone" => true,
"missing" => matches!(
Path::new(&self.path).symlink_metadata(),
Err(e) if e.kind() == io::ErrorKind::NotFound
),
_ => false,
}
}
}
fn revoked(output: &io::Result<Output>) -> bool {
let Ok(output) = output else {
return false;
};
output.succeeded()
&& serde_json::from_str::<Vec<Outcome>>(&output.stdout)
.is_ok_and(|outcomes| outcomes.iter().all(Outcome::released))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::windows::srt::fake::{Fake, exit};
fn held(srt: Fake) -> Held {
Held {
srt: Arc::new(srt),
sid: "S-1-5-21-7".to_owned(),
holder: 4242,
}
}
fn release(verb: &str) -> Vec<String> {
["acl", verb, "--holder-pid", "4242"]
.into_iter()
.chain(["--sandbox-user-sid", "S-1-5-21-7", "--json"])
.map(str::to_owned)
.collect()
}
#[test]
fn held_grants_are_revoked_then_restored() {
let (srt, calls) = Fake::passing();
drop(held(srt));
let all = calls.all();
let [revoke, restore] = all.as_slice() else {
panic!("calls: {all:?}");
};
assert_eq!(revoke.args, release("revoke"));
assert_eq!(restore.args, release("restore"));
}
#[test]
fn a_revoke_that_did_not_finish_leaves_the_denies_in_place() {
let (srt, calls) = Fake::answering(|call| match call.args[1].as_str() {
"revoke" => exit(1, r#"[{"path":"C:\\work","status":"pending"}]"#),
_ => exit(0, "[]"),
});
drop(held(srt));
assert!(calls.starting(&["acl", "restore"]).is_empty());
}
#[test]
fn a_revoke_that_found_a_substituted_path_is_finished() {
let (srt, calls) = Fake::answering(|call| match call.args[1].as_str() {
"revoke" => exit(0, r#"[{"path":"C:\\work","status":"mismatch"}]"#),
_ => exit(0, "[]"),
});
drop(held(srt));
assert_eq!(calls.starting(&["acl", "restore"]).len(), 1);
}
#[test]
fn a_revoke_that_printed_an_unknown_status_is_not_finished() {
let (srt, calls) = Fake::answering(|call| match call.args[1].as_str() {
"revoke" => exit(0, r#"[{"path":"C:\\work","status":"somethingNew"}]"#),
_ => exit(0, "[]"),
});
drop(held(srt));
assert!(calls.starting(&["acl", "restore"]).is_empty());
}
}