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
|
|
@ -383,6 +383,7 @@ pub struct VhostView {
|
||||||
pub account: String,
|
pub account: String,
|
||||||
pub host: String,
|
pub host: String,
|
||||||
pub setter: String,
|
pub setter: String,
|
||||||
|
pub expires: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
@ -596,7 +597,7 @@ pub trait Store {
|
||||||
fn set_email(&mut self, account: &str, email: Option<String>) -> Result<(), RegError>;
|
fn set_email(&mut self, account: &str, email: Option<String>) -> Result<(), RegError>;
|
||||||
fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError>;
|
fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError>;
|
||||||
// HostServ vhosts.
|
// 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<u64>) -> Result<(), RegError>;
|
||||||
fn del_vhost(&mut self, account: &str) -> Result<bool, RegError>;
|
fn del_vhost(&mut self, account: &str) -> Result<bool, RegError>;
|
||||||
fn vhost(&self, account: &str) -> Option<VhostView>;
|
fn vhost(&self, account: &str) -> Option<VhostView>;
|
||||||
fn vhosts(&self) -> Vec<VhostView>;
|
fn vhosts(&self) -> Vec<VhostView>;
|
||||||
|
|
|
||||||
|
|
@ -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."));
|
ctx.notice(me, from.uid, format!("Rejected \x02{account}\x02's vhost request."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match db.set_vhost(account, &host, from.nick) {
|
match db.set_vhost(account, &host, from.nick, None) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
for uid in net.uids_logged_into(account) {
|
for uid in net.uids_logged_into(account) {
|
||||||
ctx.apply_vhost(&uid, &host);
|
ctx.apply_vhost(&uid, &host);
|
||||||
|
|
|
||||||
|
|
@ -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.");
|
ctx.notice(me, from.uid, "Sorry, a vhost couldn't be generated for your account.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match db.set_vhost(account, &host, "template") {
|
match db.set_vhost(account, &host, "template", None) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
ctx.apply_vhost(from.uid, &host);
|
ctx.apply_vhost(from.uid, &host);
|
||||||
ctx.notice(me, from.uid, format!("You now have the vhost \x02{host}\x02."));
|
ctx.notice(me, from.uid, format!("You now have the vhost \x02{host}\x02."));
|
||||||
|
|
|
||||||
|
|
@ -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()));
|
ctx.notice(me, from.uid, format!("Assigned vhosts ({}):", vhosts.len()));
|
||||||
for v in &vhosts {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
use fedserv_api::{NetView, Sender, ServiceCtx, Store};
|
use fedserv_api::{parse_duration, NetView, Sender, ServiceCtx, Store};
|
||||||
|
|
||||||
// SET <account> <host>: assign a vhost to an account, applying it at once to any
|
// SET <account> <host> [duration]: assign a vhost to an account, applying it at
|
||||||
// online sessions. Operators only.
|
// 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) {
|
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) {
|
if !super::require_oper(me, from, ctx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let (Some(&account), Some(&host)) = (args.get(1), args.get(2)) else {
|
let (Some(&account), Some(&host)) = (args.get(1), args.get(2)) else {
|
||||||
ctx.notice(me, from.uid, "Syntax: SET <account> <host>");
|
ctx.notice(me, from.uid, "Syntax: SET <account> <host> [duration]");
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if !super::valid_vhost(host) {
|
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."));
|
ctx.notice(me, from.uid, format!("\x02{account}\x02 isn't registered."));
|
||||||
return;
|
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(()) => {
|
Ok(()) => {
|
||||||
for uid in net.uids_logged_into(account) {
|
for uid in net.uids_logged_into(account) {
|
||||||
ctx.apply_vhost(&uid, host);
|
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."),
|
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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."));
|
ctx.notice(me, from.uid, format!("There's no offer #\x02{n}\x02. See \x02OFFERLIST\x02."));
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
match db.set_vhost(account, &host, "offer") {
|
match db.set_vhost(account, &host, "offer", None) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
ctx.apply_vhost(from.uid, &host);
|
ctx.apply_vhost(from.uid, &host);
|
||||||
ctx.notice(me, from.uid, format!("You now have the vhost \x02{host}\x02."));
|
ctx.notice(me, from.uid, format!("You now have the vhost \x02{host}\x02."));
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,9 @@ pub struct Vhost {
|
||||||
pub host: String,
|
pub host: String,
|
||||||
pub setter: String,
|
pub setter: String,
|
||||||
pub ts: u64,
|
pub ts: u64,
|
||||||
|
// Absolute unix-seconds expiry, or None for a permanent vhost.
|
||||||
|
#[serde(default)]
|
||||||
|
pub expires: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn verified_default() -> bool {
|
fn verified_default() -> bool {
|
||||||
|
|
@ -116,7 +119,7 @@ pub enum Event {
|
||||||
AccountVerified { account: String },
|
AccountVerified { account: String },
|
||||||
AjoinAdded { account: String, channel: String, key: String },
|
AjoinAdded { account: String, channel: String, key: String },
|
||||||
AjoinRemoved { account: String, channel: 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 },
|
VhostDeleted { account: String },
|
||||||
VhostRequested { account: String, host: String, ts: u64 },
|
VhostRequested { account: String, host: String, ts: u64 },
|
||||||
VhostRequestCleared { account: String },
|
VhostRequestCleared { account: String },
|
||||||
|
|
@ -1206,15 +1209,17 @@ impl Db {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Assign a vhost to `account`. `setter` is who did it, for the record.
|
/// Assign a vhost to `account`. `setter` is who did it; `ttl` is seconds until
|
||||||
pub fn set_vhost(&mut self, account: &str, host: &str, setter: &str) -> Result<(), RegError> {
|
/// 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);
|
let k = key(account);
|
||||||
if !self.accounts.contains_key(&k) {
|
if !self.accounts.contains_key(&k) {
|
||||||
return Err(RegError::Internal);
|
return Err(RegError::Internal);
|
||||||
}
|
}
|
||||||
let ts = now();
|
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)?;
|
let expires = ttl.map(|t| ts + t);
|
||||||
self.accounts.get_mut(&k).unwrap().vhost = Some(Vhost { host: host.to_string(), setter: setter.to_string(), ts });
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1351,12 +1356,12 @@ impl Db {
|
||||||
self.host_cfg.template.as_deref()
|
self.host_cfg.template.as_deref()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Every account that has a vhost, as (account, host, setter).
|
/// Every account that has a vhost, as (account, host, setter, expires).
|
||||||
pub fn vhosts(&self) -> Vec<(String, String, String)> {
|
pub fn vhosts(&self) -> Vec<(String, String, String, Option<u64>)> {
|
||||||
let mut out: Vec<(String, String, String)> = self
|
let mut out: Vec<(String, String, String, Option<u64>)> = self
|
||||||
.accounts
|
.accounts
|
||||||
.values()
|
.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();
|
.collect();
|
||||||
out.sort_by(|a, b| a.0.cmp(&b.0));
|
out.sort_by(|a, b| a.0.cmp(&b.0));
|
||||||
out
|
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));
|
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)) {
|
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 } => {
|
Event::VhostDeleted { account } => {
|
||||||
|
|
@ -2513,17 +2518,19 @@ impl Store for Db {
|
||||||
fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> {
|
fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> {
|
||||||
Db::set_greet(self, account, greet)
|
Db::set_greet(self, account, greet)
|
||||||
}
|
}
|
||||||
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<u64>) -> Result<(), RegError> {
|
||||||
Db::set_vhost(self, account, host, setter)
|
Db::set_vhost(self, account, host, setter, ttl)
|
||||||
}
|
}
|
||||||
fn del_vhost(&mut self, account: &str) -> Result<bool, RegError> {
|
fn del_vhost(&mut self, account: &str) -> Result<bool, RegError> {
|
||||||
Db::del_vhost(self, account)
|
Db::del_vhost(self, account)
|
||||||
}
|
}
|
||||||
fn vhost(&self, account: &str) -> Option<VhostView> {
|
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> {
|
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> {
|
fn request_vhost(&mut self, account: &str, host: &str) -> Result<(), RegError> {
|
||||||
Db::request_vhost(self, account, host)
|
Db::request_vhost(self, account, host)
|
||||||
|
|
|
||||||
|
|
@ -2650,7 +2650,7 @@ mod tests {
|
||||||
db.scram_iterations = 4096;
|
db.scram_iterations = 4096;
|
||||||
db.register("boss", "password1", None).unwrap();
|
db.register("boss", "password1", None).unwrap();
|
||||||
db.register("alice", "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(
|
let mut e = Engine::new(
|
||||||
vec![
|
vec![
|
||||||
Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }),
|
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");
|
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
|
// Forbidden patterns block impersonating requests; the template + DEFAULT
|
||||||
// gives a user a $account-based vhost.
|
// gives a user a $account-based vhost.
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -2783,7 +2824,7 @@ mod tests {
|
||||||
db.scram_iterations = 4096;
|
db.scram_iterations = 4096;
|
||||||
db.register("boss", "password1", None).unwrap();
|
db.register("boss", "password1", None).unwrap();
|
||||||
db.register("alice", "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(
|
let mut e = Engine::new(
|
||||||
vec![
|
vec![
|
||||||
Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }),
|
Box::new(NickServ { uid: "42SAAAAAA".into(), guest_nick: "Guest".into(), guest_seq: 0 }),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue