use std::fs;
use std::os::unix::fs::{MetadataExt as _, PermissionsExt as _};
use std::path::Path;
use super::Found;
const OTHERS_WRITE: u32 = 0o022;
const OTHERS_READ: u32 = 0o044;
pub(super) const NOT_CHECKED: &str = "state files modes and owners not checked: could not tell which \
user this is (the temp directory is not writable)";
pub(super) fn current_user() -> Option<u32> {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_nanos());
let probe =
std::env::temp_dir().join(format!(".openmoat-owner-{}-{nanos}", std::process::id()));
let file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&probe)
.ok()?;
let uid = file.metadata().ok().map(|m| m.uid());
drop(file);
let _ = fs::remove_file(&probe);
uid
}
pub(super) fn inspect(path: &Path, me: u32) -> Option<Found> {
let meta = fs::symlink_metadata(path).ok()?;
Some(judge(
path,
meta.permissions().mode(),
meta.uid(),
me,
meta.file_type().is_symlink(),
))
}
fn judge(path: &Path, mode: u32, uid: u32, me: u32, link: bool) -> Found {
let bits = mode & 0o7777;
let owner = if uid == me {
"you".to_owned()
} else {
format!("uid {uid}")
};
let shown_path = path.display();
if link {
return Found {
shown: format!("symbolic link, owner {owner}"),
problem: None,
readable: Some(
"is a symbolic link: whoever may change where it points chooses this file; the \
lock notices a new target, but keep the file itself here"
.to_owned(),
),
};
}
let shown = format!("mode {bits:03o}, owner {owner}");
let problem = if uid != me {
Some(format!(
"is owned by uid {uid}, not by you (uid {me}): that user can change it under the \
lock; `sudo chown {me} {shown_path}`, then check what it holds"
))
} else if bits & OTHERS_WRITE != 0 {
Some(format!(
"has mode {bits:03o}: other users may change it under the lock; \
`chmod go-rwx {shown_path}`, then check what it holds"
))
} else {
None
};
let readable = (bits & OTHERS_READ != 0).then(|| {
format!("has mode {bits:03o}: other users may read it; `chmod go-rwx {shown_path}`")
});
Found {
shown,
problem,
readable,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn owner_only_files_are_fine() {
let found = judge(
Path::new("/h/.moat/policy.lock"),
0o100_600,
501,
501,
false,
);
assert_eq!(found.shown, "mode 600, owner you");
assert_eq!(found.problem, None);
assert_eq!(found.readable, None);
let dir = judge(Path::new("/h/.moat"), 0o040_700, 501, 501, false);
assert_eq!(dir.problem, None);
}
#[test]
fn others_writing_or_owning_is_a_problem_and_reading_a_warning() {
let path = Path::new("/h/.moat/policy.yaml");
let writable = judge(path, 0o100_666, 501, 501, false);
assert!(writable.problem.unwrap().contains("chmod go-rwx"));
assert!(writable.readable.is_some());
let readable = judge(path, 0o100_644, 501, 501, false);
assert_eq!(readable.problem, None);
assert!(readable.readable.unwrap().contains("may read"));
let theirs = judge(path, 0o100_600, 0, 501, false);
assert_eq!(theirs.shown, "mode 600, owner uid 0");
assert!(theirs.problem.unwrap().contains("owned by uid 0"));
let link = judge(path, 0o120_777, 501, 501, true);
assert_eq!(link.problem, None);
assert!(link.readable.unwrap().contains("symbolic link"));
}
#[test]
fn this_process_owns_what_it_creates() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("f");
fs::write(&file, b"").unwrap();
let me = current_user().unwrap();
assert_eq!(fs::metadata(&file).unwrap().uid(), me);
let found = inspect(&file, me).unwrap();
assert!(found.shown.ends_with("owner you"), "{found:?}");
assert_eq!(inspect(&dir.path().join("missing"), me), None);
}
}