use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum Target {
#[default]
Active,
Local,
Tab(u32),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum WriteMode {
Create,
Truncate,
CreateNew,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Fs {
Read {
path: String,
offset: u64,
len: Option<u64>,
},
Write {
path: String,
#[serde(with = "serde_bytes")]
data: Vec<u8>,
mode: WriteMode,
perm: Option<u32>,
atomic: bool,
},
Append {
path: String,
#[serde(with = "serde_bytes")]
data: Vec<u8>,
},
Stat {
path: String,
follow: bool,
},
List {
path: String,
glob: Option<String>,
hidden: bool,
cursor: Option<String>,
},
Mkdir {
path: String,
parents: bool,
perm: Option<u32>,
},
Remove {
path: String,
recursive: bool,
to_trash: bool,
},
Rename {
from: String,
to: String,
overwrite: bool,
},
Chmod {
path: String,
mode: u32,
},
Watch {
path: String,
recursive: bool,
},
Unwatch {
id: u64,
},
}
impl Fs {
pub fn mutates(&self) -> bool {
!matches!(self, Fs::Read { .. } | Fs::Stat { .. } | Fs::List { .. } | Fs::Watch { .. } | Fs::Unwatch { .. })
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Io {
Pipes,
Pty {
cols: u16,
rows: u16,
},
VisiblePty {
new: bool,
},
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Spawn {
pub program: String,
pub args: Vec<String>,
pub env_inherit: bool,
pub env_set: Vec<(String, String)>,
pub cwd: Option<String>,
pub io: Io,
pub timeout_ms: Option<u64>,
}
pub const ESCALATORS: &[&str] = &["sudo", "doas", "pkexec", "su", "run0"];
impl Spawn {
pub fn check_escalation(&self) -> Result<(), String> {
let base = self.program.rsplit(['/', '\\']).next().unwrap_or(&self.program);
let base = base.strip_suffix(".exe").unwrap_or(base);
if ESCALATORS.contains(&base) && !matches!(self.io, Io::VisiblePty { .. }) {
return Err(format!("{base} must run in the visible terminal (EscalationNeedsVisiblePty)"));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Signal {
Term,
Kill,
Int,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Call {
Fs {
target: Target,
op: Fs,
},
Spawn {
target: Target,
spec: Spawn,
},
Stdin {
proc: u64,
#[serde(with = "serde_bytes")]
data: Vec<u8>,
close: bool,
},
Kill {
proc: u64,
signal: Signal,
},
Resize {
proc: u64,
cols: u16,
rows: u16,
},
Escalate {
target: Target,
cmdline: String,
},
Service {
method: String,
params: Value,
},
Approve {
title: String,
detail: String,
options: Vec<String>,
},
TimerSet {
id: String,
every_ms: Option<u64>,
at_ms: Option<u64>,
},
TimerClear {
id: String,
},
ClipboardGet,
ClipboardSet(String),
}
pub const DENY_LIST: &[&str] =
&["$HOME/.ssh/**", "$HOME/.gnupg/**", "$MOUSER_HOME/secrets/**", "$MOUSER_HOME/config.toml", "$HOME/Library/Keychains/**"];