From 271e06fa77a869d076fc436adbd3f6a461ff3aaa Mon Sep 17 00:00:00 2001 From: Jean Date: Mon, 13 Jul 2026 22:53:04 +0000 Subject: [PATCH] HostServ: temporary vhosts (expiry) SET [duration] assigns a vhost that lapses after the duration (e.g. 30d); a Vhost carries an optional expiry and the store hides an expired one lazily (like a suspension), so it stops applying on identify/ ON without a sweep. LIST flags temporary vhosts. --- api/src/lib.rs | 3 ++- hostserv/src/approve.rs | 2 +- hostserv/src/default.rs | 2 +- hostserv/src/list.rs | 3 ++- hostserv/src/set.rs | 15 ++++++++------ hostserv/src/take.rs | 2 +- src/engine/db.rs | 37 +++++++++++++++++++-------------- src/engine/mod.rs | 45 +++++++++++++++++++++++++++++++++++++++-- 8 files changed, 81 insertions(+), 28 deletions(-) diff --git a/api/src/lib.rs b/api/src/lib.rs index 9f90ad6..4637f57 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -383,6 +383,7 @@ pub struct VhostView { pub account: String, pub host: String, pub setter: String, + pub expires: Option, } #[derive(Debug, Clone)] @@ -596,7 +597,7 @@ pub trait Store { fn set_email(&mut self, account: &str, email: Option) -> Result<(), RegError>; fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError>; // HostServ vhosts. - fn set_vhost(&mut self, account: &str, host: &str, setter: &str) -> Result<(), RegError>; + fn set_vhost(&mut self, account: &str, host: &str, setter: &str, ttl: Option) -> Result<(), RegError>; fn del_vhost(&mut self, account: &str) -> Result; fn vhost(&self, account: &str) -> Option; fn vhosts(&self) -> Vec; diff --git a/hostserv/src/approve.rs b/hostserv/src/approve.rs index 1624b72..1046d64 100644 --- a/hostserv/src/approve.rs +++ b/hostserv/src/approve.rs @@ -26,7 +26,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: ctx.notice(me, from.uid, format!("Rejected \x02{account}\x02's vhost request.")); return; } - match db.set_vhost(account, &host, from.nick) { + match db.set_vhost(account, &host, from.nick, None) { Ok(()) => { for uid in net.uids_logged_into(account) { ctx.apply_vhost(&uid, &host); diff --git a/hostserv/src/default.rs b/hostserv/src/default.rs index 3418c65..15e68a4 100644 --- a/hostserv/src/default.rs +++ b/hostserv/src/default.rs @@ -22,7 +22,7 @@ pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &mut dyn Store) ctx.notice(me, from.uid, "Sorry, a vhost couldn't be generated for your account."); return; } - match db.set_vhost(account, &host, "template") { + match db.set_vhost(account, &host, "template", None) { Ok(()) => { ctx.apply_vhost(from.uid, &host); ctx.notice(me, from.uid, format!("You now have the vhost \x02{host}\x02.")); diff --git a/hostserv/src/list.rs b/hostserv/src/list.rs index 10d88a8..16034fa 100644 --- a/hostserv/src/list.rs +++ b/hostserv/src/list.rs @@ -12,6 +12,7 @@ pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &dyn Store) { } ctx.notice(me, from.uid, format!("Assigned vhosts ({}):", vhosts.len())); for v in &vhosts { - ctx.notice(me, from.uid, format!(" \x02{}\x02 — {} (by {})", v.account, v.host, v.setter)); + let temp = if v.expires.is_some() { ", temporary" } else { "" }; + ctx.notice(me, from.uid, format!(" \x02{}\x02 — {} (by {}{temp})", v.account, v.host, v.setter)); } } diff --git a/hostserv/src/set.rs b/hostserv/src/set.rs index f789d9d..041f218 100644 --- a/hostserv/src/set.rs +++ b/hostserv/src/set.rs @@ -1,13 +1,14 @@ -use fedserv_api::{NetView, Sender, ServiceCtx, Store}; +use fedserv_api::{parse_duration, NetView, Sender, ServiceCtx, Store}; -// SET : assign a vhost to an account, applying it at once to any -// online sessions. Operators only. +// SET [duration]: assign a vhost to an account, applying it at +// once to online sessions. An optional duration (e.g. 30d) makes it temporary. +// Operators only. pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: &dyn NetView, db: &mut dyn Store) { if !super::require_oper(me, from, ctx) { return; } let (Some(&account), Some(&host)) = (args.get(1), args.get(2)) else { - ctx.notice(me, from.uid, "Syntax: SET "); + ctx.notice(me, from.uid, "Syntax: SET [duration]"); return; }; if !super::valid_vhost(host) { @@ -18,12 +19,14 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net: ctx.notice(me, from.uid, format!("\x02{account}\x02 isn't registered.")); return; } - match db.set_vhost(account, host, from.nick) { + let ttl = args.get(3).and_then(|s| parse_duration(s)); + match db.set_vhost(account, host, from.nick, ttl) { Ok(()) => { for uid in net.uids_logged_into(account) { ctx.apply_vhost(&uid, host); } - ctx.notice(me, from.uid, format!("Vhost \x02{host}\x02 assigned to \x02{account}\x02.")); + let when = args.get(3).filter(|_| ttl.is_some()).map(|d| format!(" (expires in {d})")).unwrap_or_default(); + ctx.notice(me, from.uid, format!("Vhost \x02{host}\x02 assigned to \x02{account}\x02{when}.")); } Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."), } diff --git a/hostserv/src/take.rs b/hostserv/src/take.rs index 71d5fd9..303b9f5 100644 --- a/hostserv/src/take.rs +++ b/hostserv/src/take.rs @@ -14,7 +14,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db: ctx.notice(me, from.uid, format!("There's no offer #\x02{n}\x02. See \x02OFFERLIST\x02.")); return; }; - match db.set_vhost(account, &host, "offer") { + match db.set_vhost(account, &host, "offer", None) { Ok(()) => { ctx.apply_vhost(from.uid, &host); ctx.notice(me, from.uid, format!("You now have the vhost \x02{host}\x02.")); diff --git a/src/engine/db.rs b/src/engine/db.rs index 4fbfda5..53d5456 100644 --- a/src/engine/db.rs +++ b/src/engine/db.rs @@ -94,6 +94,9 @@ pub struct Vhost { pub host: String, pub setter: String, pub ts: u64, + // Absolute unix-seconds expiry, or None for a permanent vhost. + #[serde(default)] + pub expires: Option, } fn verified_default() -> bool { @@ -116,7 +119,7 @@ pub enum Event { AccountVerified { account: String }, AjoinAdded { account: String, channel: String, key: String }, AjoinRemoved { account: String, channel: String }, - VhostSet { account: String, host: String, setter: String, ts: u64 }, + VhostSet { account: String, host: String, setter: String, ts: u64, expires: Option }, VhostDeleted { account: String }, VhostRequested { account: String, host: String, ts: u64 }, VhostRequestCleared { account: String }, @@ -1206,15 +1209,17 @@ impl Db { Ok(()) } - /// Assign a vhost to `account`. `setter` is who did it, for the record. - pub fn set_vhost(&mut self, account: &str, host: &str, setter: &str) -> Result<(), RegError> { + /// Assign a vhost to `account`. `setter` is who did it; `ttl` is seconds until + /// it expires, or None for a permanent vhost. + pub fn set_vhost(&mut self, account: &str, host: &str, setter: &str, ttl: Option) -> Result<(), RegError> { let k = key(account); if !self.accounts.contains_key(&k) { return Err(RegError::Internal); } let ts = now(); - self.log.append(Event::VhostSet { account: account.to_string(), host: host.to_string(), setter: setter.to_string(), ts }).map_err(|_| RegError::Internal)?; - self.accounts.get_mut(&k).unwrap().vhost = Some(Vhost { host: host.to_string(), setter: setter.to_string(), ts }); + let expires = ttl.map(|t| ts + t); + self.log.append(Event::VhostSet { account: account.to_string(), host: host.to_string(), setter: setter.to_string(), ts, expires }).map_err(|_| RegError::Internal)?; + self.accounts.get_mut(&k).unwrap().vhost = Some(Vhost { host: host.to_string(), setter: setter.to_string(), ts, expires }); Ok(()) } @@ -1351,12 +1356,12 @@ impl Db { self.host_cfg.template.as_deref() } - /// Every account that has a vhost, as (account, host, setter). - pub fn vhosts(&self) -> Vec<(String, String, String)> { - let mut out: Vec<(String, String, String)> = self + /// Every account that has a vhost, as (account, host, setter, expires). + pub fn vhosts(&self) -> Vec<(String, String, String, Option)> { + let mut out: Vec<(String, String, String, Option)> = self .accounts .values() - .filter_map(|a| a.vhost.as_ref().map(|v| (a.name.clone(), v.host.clone(), v.setter.clone()))) + .filter_map(|a| a.vhost.as_ref().map(|v| (a.name.clone(), v.host.clone(), v.setter.clone(), v.expires))) .collect(); out.sort_by(|a, b| a.0.cmp(&b.0)); out @@ -2208,9 +2213,9 @@ fn apply(accounts: &mut HashMap, channels: &mut HashMap { + Event::VhostSet { account, host, setter, ts, expires } => { if let Some(a) = accounts.get_mut(&key(&account)) { - a.vhost = Some(Vhost { host, setter, ts }); + a.vhost = Some(Vhost { host, setter, ts, expires }); } } Event::VhostDeleted { account } => { @@ -2513,17 +2518,19 @@ impl Store for Db { fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> { Db::set_greet(self, account, greet) } - fn set_vhost(&mut self, account: &str, host: &str, setter: &str) -> Result<(), RegError> { - Db::set_vhost(self, account, host, setter) + fn set_vhost(&mut self, account: &str, host: &str, setter: &str, ttl: Option) -> Result<(), RegError> { + Db::set_vhost(self, account, host, setter, ttl) } fn del_vhost(&mut self, account: &str) -> Result { Db::del_vhost(self, account) } fn vhost(&self, account: &str) -> Option { - Db::account(self, account).and_then(|a| a.vhost.as_ref().map(|v| VhostView { account: a.name.clone(), host: v.host.clone(), setter: v.setter.clone() })) + Db::account(self, account).and_then(|a| { + a.vhost.as_ref().filter(|v| v.expires.is_none_or(|e| e > now())).map(|v| VhostView { account: a.name.clone(), host: v.host.clone(), setter: v.setter.clone(), expires: v.expires }) + }) } fn vhosts(&self) -> Vec { - Db::vhosts(self).into_iter().map(|(account, host, setter)| VhostView { account, host, setter }).collect() + Db::vhosts(self).into_iter().map(|(account, host, setter, expires)| VhostView { account, host, setter, expires }).collect() } fn request_vhost(&mut self, account: &str, host: &str) -> Result<(), RegError> { Db::request_vhost(self, account, host) diff --git a/src/engine/mod.rs b/src/engine/mod.rs index cf191d9..6ba0658 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -2650,7 +2650,7 @@ mod tests { db.scram_iterations = 4096; db.register("boss", "password1", None).unwrap(); db.register("alice", "password1", None).unwrap(); - db.set_vhost("alice", "alice.vhost.example", "system").unwrap(); // pre-assigned + db.set_vhost("alice", "alice.vhost.example", "system", None).unwrap(); // pre-assigned let mut e = Engine::new( vec![ Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }), @@ -2686,6 +2686,47 @@ mod tests { assert!(hs(&mut e, "000AAAAAB", "SET alice not a host").iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("isn't a valid host"))), "host validated"); } + // A temporary vhost applies while valid; an already-expired one is ignored. + #[test] + fn hostserv_vhost_expiry() { + use fedserv_hostserv::HostServ; + use fedserv_nickserv::NickServ; + let path = std::env::temp_dir().join("fedserv-hsexpiry.jsonl"); + let _ = std::fs::remove_file(&path); + let mut db = Db::open(&path, "42S"); + db.scram_iterations = 4096; + db.register("boss", "password1", None).unwrap(); + db.register("alice", "password1", None).unwrap(); + db.register("bob", "password1", None).unwrap(); + db.set_vhost("bob", "old.host.example", "system", Some(0)).unwrap(); // already expired + let mut e = Engine::new( + vec![ + Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }), + Box::new(HostServ { uid: "42SAAAAAG".into() }), + ], + db, + ); + e.set_sid("42S".into()); + let mut opers = std::collections::HashMap::new(); + opers.insert("boss".to_string(), Privs::default().with(fedserv_api::Priv::Admin)); + e.set_opers(opers); + let ns = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAA".into(), text: t.into() }); + let hs = |e: &mut Engine, uid: &str, t: &str| e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAG".into(), text: t.into() }); + e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "boss".into(), host: "realhost".into() }); + e.handle(NetEvent::UserConnect { uid: "000AAAAAV".into(), nick: "alice".into(), host: "realhost".into() }); + e.handle(NetEvent::UserConnect { uid: "000AAAAAW".into(), nick: "bob".into(), host: "realhost".into() }); + ns(&mut e, "000AAAAAB", "IDENTIFY password1"); + ns(&mut e, "000AAAAAV", "IDENTIFY password1"); + + // bob's expired vhost is not applied on identify. + assert!(!ns(&mut e, "000AAAAAW", "IDENTIFY password1").iter().any(|a| matches!(a, NetAction::SetHost { .. })), "expired vhost not applied"); + // A temporary vhost with a duration applies now and lists as temporary. + let out = hs(&mut e, "000AAAAAB", "SET alice temp.host.example 1h"); + assert!(out.iter().any(|a| matches!(a, NetAction::SetHost { uid, host } if uid == "000AAAAAV" && host == "temp.host.example")), "temporary vhost applied: {out:?}"); + assert!(out.iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("expires in 1h"))), "expiry announced"); + assert!(hs(&mut e, "000AAAAAB", "LIST").iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("alice") && text.contains("temporary"))), "listed as temporary"); + } + // Forbidden patterns block impersonating requests; the template + DEFAULT // gives a user a $account-based vhost. #[test] @@ -2783,7 +2824,7 @@ mod tests { db.scram_iterations = 4096; db.register("boss", "password1", None).unwrap(); db.register("alice", "password1", None).unwrap(); - db.set_vhost("alice", "web@cloak.example", "system").unwrap(); + db.set_vhost("alice", "web@cloak.example", "system", None).unwrap(); let mut e = Engine::new( vec![ Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }),