mod filters;
mod logon;
mod manage;
mod registry;
mod system;
use std::path::Path;
use anyhow::{Context as _, Result, bail};
use zeroize::Zeroizing;
use super::acl;
use super::win32::Job;
use crate::agent_account::{self, AclEntry, Observed, Record};
use crate::sandbox::write_token::account::AccountPlan;
use crate::sandbox::write_token::{self, Entry};
pub use logon::AsAccount;
pub use manage::{remove, require_elevated, setup, status, supported};
fn wide(text: &str) -> Result<Vec<u16>> {
let mut units: Vec<u16> = text.encode_utf16().collect();
if units.contains(&0) {
bail!("{text} holds a NUL character");
}
units.push(0);
Ok(units)
}
fn native(path: &str) -> Result<Vec<u16>> {
wide(&path.replace('/', "\\"))
}
const USERS: &str = "S-1-5-32-545";
pub struct Active {
pub record: Record,
state: std::path::PathBuf,
pub filters: bool,
}
pub fn proxy_ports(state: &Path) -> Option<[u16; 2]> {
Record::load(state).ok().flatten()?.wfp.map(|w| w.ports)
}
pub fn active(state: &Path) -> Option<Active> {
let record = match Record::load(state) {
Ok(record) => record?,
Err(error) => {
eprintln!(
"moat run: the agent account's record cannot be read, so this run uses the \
write-restricted token: {error:#}"
);
return None;
}
};
let seen = observe(state, &record);
let drift = agent_account::drift(&record, &seen);
if !drift.is_empty() {
eprintln!(
"moat run: the agent account {} no longer matches its record ({}), so this run uses \
the write-restricted token; `openmoat agent-account status` shows it",
record.account,
drift.join("; ")
);
return None;
}
if seen.wfp == agent_account::wfp::Read::Unread {
eprintln!(
"moat run: the agent account's network filters cannot be read back from this \
terminal, so this run does not count them (`wfp_network`); they still refuse \
what they name"
);
}
Some(Active {
filters: matches!(seen.wfp, agent_account::wfp::Read::Seen(_)),
record,
state: state.to_path_buf(),
})
}
fn observe(state: &Path, record: &Record) -> Observed {
use agent_account::wfp::Read;
let wfp = match record.wfp.as_ref().map(filters::observe) {
None => Read::NotAsked,
Some(Ok(seen)) => Read::Seen(seen),
Some(Err(_)) => Read::Unread,
};
Observed {
sid: system::sid_of(&record.account).ok().flatten(),
groups: system::local_groups(&record.account).ok(),
rights: system::rights(&record.sid).ok(),
hidden: registry::hidden(&record.account),
relay: registry::registered(&record.sid),
profile_entry: has_entry(&record.profile_entry, &record.sid),
password: state
.join(agent_account::DIR)
.join(agent_account::PASSWORD_FILE)
.is_file(),
wfp,
}
}
fn as_entry(entry: &AclEntry, sid: &str) -> Entry {
Entry {
path: entry.path.clone(),
sid: sid.to_owned(),
allows: entry.allows,
mask: entry.mask,
inherit: entry.inherit,
}
}
fn has_entry(entry: &AclEntry, sid: &str) -> bool {
crate::state_acl::descriptor(Path::new(&entry.path))
.ok()
.and_then(|d| d.aces)
.is_some_and(|aces| !write_token::needs_change(&aces, &as_entry(entry, sid)))
}
fn set(entry: &AclEntry, sid: &str) -> Result<()> {
if has_entry(entry, sid) {
return Ok(());
}
acl::add_entry(
&native(&entry.path)?,
sid,
(entry.allows, entry.mask, entry.inherit),
)
.with_context(|| format!("setting the access list of {}", entry.path))
}
fn unset(entry: &AclEntry, sid: &str) -> Result<()> {
let (allows, mask) = (entry.allows, entry.mask);
acl::remove(&native(&entry.path)?, sid, &|a, m| a == allows && m == mask)
.with_context(|| format!("taking the agent account's entry off {}", entry.path))
.map(|_| ())
}
pub fn current_is_agent_account() -> bool {
logon::current_user().is_ok_and(|sid| registry::registered(&sid).is_some())
}
impl Active {
pub fn apply(&self, plan: &AccountPlan) -> Result<()> {
let sid = &self.record.sid;
let mut record =
Record::load(&self.state)?.context("the agent account's record is gone")?;
for old in agent_account::stale(&record.read_entries, &plan.reads) {
unset(&old, sid)?;
}
for entry in &plan.writes {
if entry.allows && !has_entry(entry, sid) {
eprintln!(
"moat run: granting the agent account write on {} (once per folder)",
entry.path
);
}
set(entry, sid)?;
}
for entry in &plan.reads {
set(entry, sid)?;
}
record.read_entries.clone_from(&plan.reads);
record.add_writes(&plan.writes);
crate::home::write_private(
&self.state.join(agent_account::RECORD_FILE),
record.to_json()?.as_bytes(),
)
}
pub fn start(
&self,
job: &Job,
application: &[u16],
line: Vec<u16>,
env: &[u16],
) -> Result<AsAccount> {
agent_account::command_line_fits(line.len().saturating_sub(1))?;
let file = self
.state
.join(agent_account::DIR)
.join(agent_account::PASSWORD_FILE);
let blob = std::fs::read(&file).with_context(|| format!("reading {}", file.display()))?;
let plain = logon::unprotect(&blob)?;
let Ok(text) = std::str::from_utf8(&plain) else {
bail!("the agent account's password file does not decrypt to text");
};
let password: Zeroizing<Vec<u16>> =
Zeroizing::new(text.encode_utf16().chain(std::iter::once(0)).collect());
let record = &self.record;
logon::start(
(&record.account, &record.sid, &password),
job,
application,
line,
env,
)
}
}