HostServ: temporary vhosts (expiry)
SET <account> <host> [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.
This commit is contained in:
parent
87bad7fbcc
commit
271e06fa77
8 changed files with 81 additions and 28 deletions
|
|
@ -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<u64>,
|
||||
}
|
||||
|
||||
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<u64> },
|
||||
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<u64>) -> 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<u64>)> {
|
||||
let mut out: Vec<(String, String, String, Option<u64>)> = 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<String, Account>, channels: &mut HashMap<String,
|
|||
a.ajoin.retain(|e| !e.channel.eq_ignore_ascii_case(&channel));
|
||||
}
|
||||
}
|
||||
Event::VhostSet { account, host, setter, ts } => {
|
||||
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<u64>) -> Result<(), RegError> {
|
||||
Db::set_vhost(self, account, host, setter, ttl)
|
||||
}
|
||||
fn del_vhost(&mut self, account: &str) -> Result<bool, RegError> {
|
||||
Db::del_vhost(self, account)
|
||||
}
|
||||
fn vhost(&self, account: &str) -> Option<VhostView> {
|
||||
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<VhostView> {
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -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 }),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue