[control] Share and token fixes: settle the epoch before minting, mint in its own task so a dropped client frees its key, token checks off the async threads, CLI refuses to run as another user than the state dir's owner
This commit is contained in:
@@ -80,9 +80,38 @@ fn state_dir(args: &[String]) -> Result<PathBuf, String> {
|
||||
Err("give --config <control.toml> or --state-dir <dir>".into())
|
||||
}
|
||||
|
||||
/// The token files must stay owned by the user control runs as: refuse to touch them as
|
||||
/// anyone else (typically root), instead of leaving root-owned files control cannot open.
|
||||
#[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()))?
|
||||
.uid();
|
||||
let probe = dir.join(format!(".owner-probe-{}", std::process::id()));
|
||||
let me = std::fs::File::create(&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 != 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()
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn same_owner(_: &std::path::Path) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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()))?;
|
||||
same_owner(&dir)?;
|
||||
let tokens = Tokens::new(&dir);
|
||||
match args.first().map(String::as_str) {
|
||||
Some("create") => {
|
||||
|
||||
Reference in New Issue
Block a user