Add HostServ: virtual hosts
New HostServ pseudo-service assigns and applies vhosts. A vhost is a typed
Option<Vhost>{host,setter,ts} on the account (VhostSet/VhostDeleted events),
applied to the displayed host on identify and re-applied by ON; OFF restores
the normal host for the session. Operators SET/DEL (applied at once to
online sessions) and LIST; hosts are validated as dot-separated labels.
New SetHost NetAction -> InspIRCd ENCAP <target-sid> CHGHOST <uid> <host>.
This commit is contained in:
parent
540d2f39d1
commit
1227e26b95
17 changed files with 378 additions and 6 deletions
|
|
@ -73,7 +73,7 @@ impl Default for Modules {
|
|||
}
|
||||
|
||||
fn default_services() -> Vec<String> {
|
||||
vec!["nickserv".to_string(), "chanserv".to_string(), "botserv".to_string(), "memoserv".to_string(), "statserv".to_string()]
|
||||
vec!["nickserv".to_string(), "chanserv".to_string(), "botserv".to_string(), "memoserv".to_string(), "statserv".to_string(), "hostserv".to_string()]
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ use super::scram::{self, Hash};
|
|||
// fedserv-api SDK crate; re-exported so the engine keeps naming them locally and
|
||||
// modules importing `crate::engine::db::{ChanError, ...}` are unaffected.
|
||||
pub use fedserv_api::{
|
||||
AccountView, AjoinView, BotView, MemoView, SuspensionView, ChanAccessView, ChanAkickView, ChanError, ChanSetting, ChannelView, CertError, CodeKind, Kicker, RegError, Store, TriggerView,
|
||||
AccountView, AjoinView, BotView, MemoView, SuspensionView, ChanAccessView, ChanAkickView, ChanError, ChanSetting, ChannelView, CertError, CodeKind, Kicker, RegError, Store, TriggerView, VhostView,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
|
@ -73,6 +73,17 @@ pub struct Account {
|
|||
// Personal greet a bot shows when this account joins a greet-enabled channel.
|
||||
#[serde(default)]
|
||||
pub greet: String,
|
||||
// Assigned vhost (HostServ), applied to the displayed host on identify.
|
||||
#[serde(default)]
|
||||
pub vhost: Option<Vhost>,
|
||||
}
|
||||
|
||||
// A HostServ virtual host: the displayed host, who assigned it, and when.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Vhost {
|
||||
pub host: String,
|
||||
pub setter: String,
|
||||
pub ts: u64,
|
||||
}
|
||||
|
||||
fn verified_default() -> bool {
|
||||
|
|
@ -95,6 +106,8 @@ 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 },
|
||||
VhostDeleted { account: String },
|
||||
AccountSuspended { account: String, by: String, reason: String, ts: u64, expires: Option<u64> },
|
||||
AccountUnsuspended { account: String },
|
||||
MemoSent { account: String, from: String, text: String, ts: u64 },
|
||||
|
|
@ -148,6 +161,8 @@ impl Event {
|
|||
| Event::AccountVerified { .. }
|
||||
| Event::AjoinAdded { .. }
|
||||
| Event::AjoinRemoved { .. }
|
||||
| Event::VhostSet { .. }
|
||||
| Event::VhostDeleted { .. }
|
||||
| Event::AccountSuspended { .. }
|
||||
| Event::AccountUnsuspended { .. }
|
||||
| Event::MemoSent { .. }
|
||||
|
|
@ -970,6 +985,7 @@ impl Db {
|
|||
suspension: None,
|
||||
memos: Vec::new(),
|
||||
greet: String::new(),
|
||||
vhost: None,
|
||||
};
|
||||
self.log.append(Event::AccountRegistered(account.clone())).map_err(|_| RegError::Internal)?;
|
||||
self.accounts.insert(key(name), account);
|
||||
|
|
@ -1139,6 +1155,47 @@ 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> {
|
||||
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 });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove `account`'s vhost. Returns whether one was set.
|
||||
pub fn del_vhost(&mut self, account: &str) -> Result<bool, RegError> {
|
||||
let k = key(account);
|
||||
match self.accounts.get(&k) {
|
||||
Some(a) if a.vhost.is_some() => {}
|
||||
Some(_) => return Ok(false),
|
||||
None => return Err(RegError::Internal),
|
||||
}
|
||||
self.log.append(Event::VhostDeleted { account: account.to_string() }).map_err(|_| RegError::Internal)?;
|
||||
self.accounts.get_mut(&k).unwrap().vhost = None;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// `account`'s vhost, if any.
|
||||
pub fn vhost(&self, account: &str) -> Option<&Vhost> {
|
||||
self.accounts.get(&key(account)).and_then(|a| a.vhost.as_ref())
|
||||
}
|
||||
|
||||
/// 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
|
||||
.accounts
|
||||
.values()
|
||||
.filter_map(|a| a.vhost.as_ref().map(|v| (a.name.clone(), v.host.clone(), v.setter.clone())))
|
||||
.collect();
|
||||
out.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
out
|
||||
}
|
||||
|
||||
/// Set (or clear, when empty) `account`'s personal greet.
|
||||
pub fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> {
|
||||
let k = key(account);
|
||||
|
|
@ -1985,6 +2042,16 @@ 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 } => {
|
||||
if let Some(a) = accounts.get_mut(&key(&account)) {
|
||||
a.vhost = Some(Vhost { host, setter, ts });
|
||||
}
|
||||
}
|
||||
Event::VhostDeleted { account } => {
|
||||
if let Some(a) = accounts.get_mut(&key(&account)) {
|
||||
a.vhost = None;
|
||||
}
|
||||
}
|
||||
Event::AccountSuspended { account, by, reason, ts, expires } => {
|
||||
if let Some(a) = accounts.get_mut(&key(&account)) {
|
||||
a.suspension = Some(Suspension { by, reason, ts, expires });
|
||||
|
|
@ -2251,6 +2318,18 @@ 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 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() }))
|
||||
}
|
||||
fn vhosts(&self) -> Vec<VhostView> {
|
||||
Db::vhosts(self).into_iter().map(|(account, host, setter)| VhostView { account, host, setter }).collect()
|
||||
}
|
||||
fn group_nick(&mut self, nick: &str, account: &str) -> Result<(), RegError> {
|
||||
Db::group_nick(self, nick, account)
|
||||
}
|
||||
|
|
@ -2490,7 +2569,7 @@ mod tests {
|
|||
fn account_conflict_resolves_deterministically() {
|
||||
let alice = |hash: &str, ts: u64, home: &str| Account {
|
||||
name: "alice".into(), password_hash: hash.into(), email: None,
|
||||
ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
|
||||
ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(), vhost: None,
|
||||
};
|
||||
let converge = |first: &Account, second: &Account| {
|
||||
let (mut acc, mut ch, mut gr, mut bo) = (HashMap::new(), HashMap::new(), HashMap::new(), HashMap::new());
|
||||
|
|
@ -2766,7 +2845,7 @@ mod tests {
|
|||
db.register("alice", "pw", None).unwrap();
|
||||
let bob = Account {
|
||||
name: "bob".into(), password_hash: "x".into(), email: None,
|
||||
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
|
||||
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(), vhost: None,
|
||||
};
|
||||
let entry = LogEntry { origin: "peer".into(), seq: 0, lamport: 1, event: Event::AccountRegistered(bob) };
|
||||
db.ingest(entry).unwrap();
|
||||
|
|
|
|||
|
|
@ -1914,7 +1914,7 @@ mod tests {
|
|||
// An earlier claim from another node wins and takes the name over.
|
||||
let winner = db::Account {
|
||||
name: "alice".into(), password_hash: "OTHER".into(), email: None,
|
||||
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
|
||||
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(), vhost: None,
|
||||
};
|
||||
let entry = LogEntry::for_test("peer", 0, 1, db::Event::AccountRegistered(winner));
|
||||
e.gossip_ingest(entry).unwrap();
|
||||
|
|
@ -2638,6 +2638,54 @@ mod tests {
|
|||
assert!(kicked(&vote(&mut e, "000AAAAAC", "!votekick victim")), "2 distinct votes: kicked");
|
||||
}
|
||||
|
||||
// HostServ: a vhost is applied on identify and by SET, listed and removed by
|
||||
// operators, and its administration is oper-gated with host validation.
|
||||
#[test]
|
||||
fn hostserv_assigns_and_applies_vhosts() {
|
||||
use fedserv_hostserv::HostServ;
|
||||
use fedserv_nickserv::NickServ;
|
||||
let path = std::env::temp_dir().join("fedserv-hostserv.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.set_vhost("alice", "alice.vhost.example", "system").unwrap(); // pre-assigned
|
||||
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() });
|
||||
let sethost = |out: &[NetAction], uid: &str, host: &str| out.iter().any(|a| matches!(a, NetAction::SetHost { uid: u, host: h } if u == uid && h == host));
|
||||
|
||||
// Identifying applies the pre-assigned vhost.
|
||||
assert!(sethost(&ns(&mut e, "000AAAAAV", "IDENTIFY password1"), "000AAAAAV", "alice.vhost.example"), "vhost applied on identify");
|
||||
ns(&mut e, "000AAAAAB", "IDENTIFY password1");
|
||||
|
||||
// An operator SET applies at once to the online session.
|
||||
assert!(sethost(&hs(&mut e, "000AAAAAB", "SET alice new.host.example"), "000AAAAAV", "new.host.example"), "SET applies online");
|
||||
// ON re-activates the account's vhost.
|
||||
assert!(sethost(&hs(&mut e, "000AAAAAV", "ON"), "000AAAAAV", "new.host.example"), "ON re-applies");
|
||||
// LIST shows it.
|
||||
assert!(hs(&mut e, "000AAAAAB", "LIST").iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("alice") && text.contains("new.host.example"))), "listed");
|
||||
// DEL restores the real host on the online session.
|
||||
assert!(sethost(&hs(&mut e, "000AAAAAB", "DEL alice"), "000AAAAAV", "realhost"), "DEL restores real host");
|
||||
|
||||
// Non-operators can't SET; invalid hosts are rejected.
|
||||
assert!(hs(&mut e, "000AAAAAV", "SET boss x.y").iter().any(|a| matches!(a, NetAction::Notice { text, .. } if text.contains("Access denied"))), "oper-gated");
|
||||
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");
|
||||
}
|
||||
|
||||
// StatServ reports per-channel activity (#channel) and the global registry
|
||||
// (SERVER, operators only).
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -127,6 +127,8 @@ fn to_wire(entry: &LogEntry) -> Option<ReplicationEvent> {
|
|||
| Event::AccountGreetSet { .. }
|
||||
| Event::AjoinAdded { .. }
|
||||
| Event::AjoinRemoved { .. }
|
||||
| Event::VhostSet { .. }
|
||||
| Event::VhostDeleted { .. }
|
||||
| Event::AccountSuspended { .. }
|
||||
| Event::AccountUnsuspended { .. }
|
||||
| Event::MemoSent { .. }
|
||||
|
|
@ -397,6 +399,7 @@ mod tests {
|
|||
suspension: None,
|
||||
memos: vec![],
|
||||
greet: String::new(),
|
||||
vhost: None,
|
||||
};
|
||||
let registered = LogEntry::for_test("A", 0, 1, Event::AccountRegistered(acct));
|
||||
let wire = to_wire(®istered).expect("account registration replicates");
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ use fedserv_botserv::BotServ;
|
|||
use fedserv_chanserv::ChanServ;
|
||||
use fedserv_memoserv::MemoServ;
|
||||
use fedserv_statserv::StatServ;
|
||||
use fedserv_hostserv::HostServ;
|
||||
use fedserv_example::ExampleServ;
|
||||
use fedserv_inspircd::InspIrcd;
|
||||
use fedserv_nickserv::NickServ;
|
||||
|
|
@ -76,6 +77,11 @@ async fn main() -> Result<()> {
|
|||
uid: format!("{}AAAAAF", cfg.server.sid),
|
||||
}));
|
||||
}
|
||||
if enabled("hostserv") {
|
||||
services.push(Box::new(HostServ {
|
||||
uid: format!("{}AAAAAG", cfg.server.sid),
|
||||
}));
|
||||
}
|
||||
if enabled("example") {
|
||||
services.push(Box::new(ExampleServ {
|
||||
uid: format!("{}AAAAAC", cfg.server.sid),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue