OperServ: temporary OPER grants that auto-expire

OPER ADD <account> <priv[,priv]> [+duration] now takes an optional window,
after which the grant lazily stops conferring privileges — no timer, the
oper_privs merge just hides an expired grant (evaluated on the engine's
clock so it's testable). Permanent grants are unchanged; LIST flags the
temporary ones and hides expired entries. Runtime opers now carry an
OperGrant{privs, expires} instead of a bare priv list.
This commit is contained in:
Jean Chevronnet 2026-07-14 02:29:03 +00:00
parent 90824513a7
commit 85d01b3ebf
No known key found for this signature in database
4 changed files with 113 additions and 39 deletions

View file

@ -1,24 +1,26 @@
use fedserv_api::{Priv, Sender, ServiceCtx, Store};
use fedserv_api::{parse_duration, Priv, Sender, ServiceCtx, Store};
use std::time::{SystemTime, UNIX_EPOCH};
// OPER ADD <account> <priv[,priv…]> | OPER DEL <account> | OPER LIST: manage
// runtime services operators (merged with the declarative config opers). The
// privileges are auspex, suspend, admin. Admin-only — only an admin grants oper.
// OPER ADD <account> <priv[,priv…]> [+duration] | OPER DEL <account> | OPER LIST:
// manage runtime services operators (merged with the declarative config opers).
// The privileges are auspex, suspend, admin; a +duration makes the grant expire.
// Admin-only — only an admin grants oper.
pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) {
if !from.privs.has(Priv::Admin) {
ctx.notice(me, from.uid, "Access denied — OPER needs the \x02admin\x02 privilege.");
return;
}
match args.get(1).map(|s| s.to_ascii_uppercase()).as_deref() {
Some("ADD") | Some("SET") => add(me, from, args.get(2).copied(), args.get(3).copied(), ctx, db),
Some("ADD") | Some("SET") => add(me, from, args.get(2).copied(), args.get(3).copied(), args.get(4).copied(), ctx, db),
Some("DEL") | Some("REMOVE") => del(me, from, args.get(2).copied(), ctx, db),
Some("LIST") | Some("VIEW") => list(me, from, ctx, db),
_ => ctx.notice(me, from.uid, "Syntax: OPER ADD <account> <priv[,priv]> | OPER DEL <account> | OPER LIST — privs: auspex, suspend, admin"),
_ => ctx.notice(me, from.uid, "Syntax: OPER ADD <account> <priv[,priv]> [+duration] | OPER DEL <account> | OPER LIST — privs: auspex, suspend, admin"),
}
}
fn add(me: &str, from: &Sender, account: Option<&str>, privs: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) {
fn add(me: &str, from: &Sender, account: Option<&str>, privs: Option<&str>, dur: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) {
let (Some(account), Some(privs)) = (account, privs) else {
ctx.notice(me, from.uid, "Syntax: OPER ADD <account> <priv[,priv]>");
ctx.notice(me, from.uid, "Syntax: OPER ADD <account> <priv[,priv]> [+duration]");
return;
};
let Some(account) = db.resolve_account(account).map(str::to_string) else {
@ -35,8 +37,14 @@ fn add(me: &str, from: &Sender, account: Option<&str>, privs: Option<&str>, ctx:
ctx.notice(me, from.uid, "No valid privileges given (auspex, suspend, admin).");
return;
}
db.oper_grant(&account, names.clone());
ctx.notice(me, from.uid, format!("\x02{account}\x02 is now an operator ({}).", names.join(", ")));
let expires = dur.and_then(|d| d.strip_prefix('+')).and_then(parse_duration).map(|secs| now() + secs);
db.oper_grant(&account, names.clone(), expires);
let window = if expires.is_some() { " (temporary)" } else { "" };
ctx.notice(me, from.uid, format!("\x02{account}\x02 is now an operator ({}){window}.", names.join(", ")));
}
fn now() -> u64 {
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0)
}
fn del(me: &str, from: &Sender, account: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) {
@ -58,8 +66,9 @@ fn list(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &mut dyn Store) {
ctx.notice(me, from.uid, "No runtime operators (config opers are set in the daemon config).");
return;
}
for (account, privs) in &opers {
ctx.notice(me, from.uid, format!(" \x02{account}\x02{}", privs.join(", ")));
for (account, privs, expires) in &opers {
let window = if expires.is_some() { " (temporary)" } else { "" };
ctx.notice(me, from.uid, format!(" \x02{account}\x02{}{window}", privs.join(", ")));
}
ctx.notice(me, from.uid, format!("End of runtime operator list ({} shown).", opers.len()));
}