use std::os::windows::ffi::OsStrExt as _;
use std::path::{Path, PathBuf};
use anyhow::{Context as _, Result, bail};
mod win32;
const DEFAULT_ROOT: &str = r"C:\Windows";
const NO_ACCESS_LISTS: &str =
"the drive may not keep access lists (FAT or exFAT): put MOAT_HOME on an NTFS drive";
pub fn icacls() -> PathBuf {
let root = std::env::var_os("SystemRoot")
.map(PathBuf::from)
.filter(|root| root.is_absolute())
.unwrap_or_else(|| PathBuf::from(DEFAULT_ROOT));
root.join(r"System32\icacls.exe")
}
fn wide(dir: &Path) -> Result<Vec<u16>> {
let mut units: Vec<u16> = dir.as_os_str().encode_wide().collect();
if units.contains(&0) {
bail!("{} holds a NUL character", dir.display());
}
units.push(0);
Ok(units)
}
pub fn read(dir: &Path) -> Result<(String, String)> {
let me = win32::current_sid()?;
let descriptor = win32::read(&wide(dir)?)
.with_context(|| format!("reading the access list of {}", dir.display()))?;
Ok((me, super::sddl_of(&descriptor)))
}
pub fn descriptor(path: &Path) -> Result<super::Descriptor> {
win32::read(&wide(path)?)
.with_context(|| format!("reading the access list of {}", path.display()))
}
pub fn protect(dir: &Path, existed: bool) -> Result<()> {
let me = win32::current_sid()?;
let path = wide(dir)?;
if existed {
let sddl = super::sddl_of(
&win32::read(&path)
.with_context(|| format!("reading the access list of {}", dir.display()))?,
);
if let Some(owner) = super::judge(&sddl, &me).foreign_owner {
bail!(
"the state directory {} is owned by {owner}, not by you; OpenMoat does not take \
it over: move it away, or set MOAT_HOME to a folder of your own",
dir.display()
);
}
}
win32::set_two(&path, &me).with_context(|| {
format!(
"setting the access list of {}; {NO_ACCESS_LISTS}",
dir.display()
)
})?;
let after = super::sddl_of(
&win32::read(&path)
.with_context(|| format!("reading the access list of {}", dir.display()))?,
);
if let Err(stayed) = super::verify(&after, &me) {
bail!(
"the access list of {} still holds more than your account and SYSTEM after \
`openmoat init` set it: {stayed}",
dir.display()
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn protect_leaves_only_this_account_and_system() {
let dir = tempfile::tempdir().unwrap();
let state = dir.path().join("state");
std::fs::create_dir(&state).unwrap();
let before = state.join("policy.lock");
std::fs::write(&before, "x").unwrap();
protect(&state, false).unwrap();
let (me, sddl) = read(&state).unwrap();
assert_eq!(super::super::verify(&sddl, &me), Ok(()), "{sddl}");
let widened = std::process::Command::new(icacls())
.arg(&state)
.args(["/grant", "*S-1-1-0:(OI)(CI)M", "/q"])
.status()
.unwrap();
assert!(widened.success());
let (_, wide) = read(&state).unwrap();
assert_eq!(
super::super::judge(&wide, &me).writers,
["Everyone"],
"{wide}"
);
protect(&state, true).unwrap();
let (_, sddl) = read(&state).unwrap();
assert_eq!(super::super::verify(&sddl, &me), Ok(()), "{sddl}");
std::fs::write(state.join("audit.db"), "y").unwrap();
std::fs::write(&before, "z").unwrap();
}
}