[control] Files retry: only in the apply worker under the lock, reloads edge to what it writes, drops only the journal of the apply that caused the pause; swallowed aborts and pauses logged; CLI owner check before creating the state dir
This commit is contained in:
@@ -85,19 +85,34 @@ fn state_dir(args: &[String]) -> Result<PathBuf, String> {
|
||||
#[cfg(unix)]
|
||||
fn same_owner(dir: &std::path::Path) -> Result<(), String> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let owner = std::fs::metadata(dir)
|
||||
.map_err(|e| format!("cannot read {}: {e}", dir.display()))?
|
||||
let existing = dir
|
||||
.ancestors()
|
||||
.find(|p| p.is_dir())
|
||||
.ok_or_else(|| format!("no part of {} exists", dir.display()))?;
|
||||
let owner = std::fs::metadata(existing)
|
||||
.map_err(|e| format!("cannot read {}: {e}", existing.display()))?
|
||||
.uid();
|
||||
let probe = dir.join(format!(".owner-probe-{}", std::process::id()));
|
||||
let me = std::fs::File::create(&probe)
|
||||
// Who we are, without libc: the owner of a file we create (in the temp dir, so nothing is
|
||||
// created in the state dir by the wrong user).
|
||||
let probe = std::env::temp_dir().join(format!(".ianitor-owner-probe-{}", std::process::id()));
|
||||
// create_new: never follows or reuses a file someone else put there.
|
||||
let me = std::fs::File::create_new(&probe)
|
||||
.and_then(|f| f.metadata())
|
||||
.map(|m| m.uid());
|
||||
let _ = std::fs::remove_file(&probe);
|
||||
let me = me.map_err(|e| format!("cannot write in {}: {e}", dir.display()))?;
|
||||
if me.is_ok() {
|
||||
let _ = std::fs::remove_file(&probe);
|
||||
}
|
||||
let me = me.map_err(|e| format!("cannot create a probe file to learn who runs this: {e}"))?;
|
||||
if me == 0 && !dir.is_dir() {
|
||||
return Err(format!(
|
||||
"{} does not exist; create it as the user control runs as (or run this as that user), not as root",
|
||||
dir.display()
|
||||
));
|
||||
}
|
||||
if me != owner {
|
||||
return Err(format!(
|
||||
"{} belongs to uid {owner}, but this runs as uid {me}; run it as that user (e.g. sudo -u ianitor ianitor-control token ...)",
|
||||
dir.display()
|
||||
existing.display()
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
@@ -110,8 +125,10 @@ fn same_owner(_: &std::path::Path) -> Result<(), String> {
|
||||
|
||||
fn token(args: &[String]) -> Result<(), String> {
|
||||
let dir = state_dir(args)?;
|
||||
std::fs::create_dir_all(&dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
|
||||
// Check the owner before creating anything: the nearest existing ancestor must belong to
|
||||
// whoever runs this (so root does not create a state dir control cannot use).
|
||||
same_owner(&dir)?;
|
||||
std::fs::create_dir_all(&dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
|
||||
let tokens = Tokens::new(&dir);
|
||||
match args.first().map(String::as_str) {
|
||||
Some("create") => {
|
||||
|
||||
Reference in New Issue
Block a user