HostServ: normalise vhosts + enforce uniqueness

Canonicalise a requested vhost the way the ircd displays it (a disallowed
underscore becomes a hyphen) before storing and applying, so what we keep
matches what shows on the network. A vhost can no longer be assigned to two
accounts: prepare_vhost normalises, validates, and checks vhost_owner at
every assignment point (SET/REQUEST/ACTIVATE/TAKE/DEFAULT), so two inputs
that collapse to the same host are caught as one.

Approach recommended by siniStar (the ns_nethost normalisation logic).
This commit is contained in:
Jean Chevronnet 2026-07-13 23:05:41 +00:00
parent 271e06fa77
commit 31d23cf6b3
No known key found for this signature in database
9 changed files with 130 additions and 18 deletions

View file

@ -26,6 +26,14 @@ 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;
}
// Re-check the requested host now (another account may have taken it since).
let host = match super::prepare_vhost(&host, account, db) {
Ok(h) => h,
Err(msg) => {
ctx.notice(me, from.uid, format!("Can't activate: {msg}"));
return;
}
};
match db.set_vhost(account, &host, from.nick, None) {
Ok(()) => {
for uid in net.uids_logged_into(account) {

View file

@ -17,11 +17,14 @@ pub fn handle(me: &str, from: &Sender, ctx: &mut ServiceCtx, db: &mut dyn Store)
ctx.notice(me, from.uid, "Your account name has no usable characters for a vhost.");
return;
}
let host = template.replace("$account", &label);
if !super::valid_vhost(&host) || db.vhost_is_forbidden(&host) {
ctx.notice(me, from.uid, "Sorry, a vhost couldn't be generated for your account.");
return;
}
let generated = template.replace("$account", &label);
let host = match super::prepare_vhost(&generated, account, db) {
Ok(h) if !db.vhost_is_forbidden(&h) => h,
_ => {
ctx.notice(me, from.uid, "Sorry, a vhost couldn't be generated for your account.");
return;
}
};
match db.set_vhost(account, &host, "template", None) {
Ok(()) => {
ctx.apply_vhost(from.uid, &host);

View file

@ -86,6 +86,37 @@ fn require_oper(me: &str, from: &Sender, ctx: &mut ServiceCtx) -> bool {
false
}
// Canonicalise a vhost the way the ircd will actually display it: the host part
// only allows letters, digits, dots and hyphens, so a disallowed character it
// would rewrite (an underscore becomes a hyphen) is rewritten here first. This
// keeps what we store identical to what shows on the network, so two inputs
// that would collapse to the same host are detected as one.
// (Recommended by siniStar — the ns_nethost normalisation approach.)
pub(crate) fn normalize_vhost(spec: &str) -> String {
let (ident, host) = match spec.split_once('@') {
Some((i, h)) => (Some(i), h),
None => (None, spec),
};
let norm_host: String = host.chars().map(|c| if c == '_' { '-' } else { c }).collect();
match ident {
Some(i) => format!("{i}@{norm_host}"),
None => norm_host,
}
}
// Normalise a requested vhost and check it's valid and not already another
// account's. Returns the canonical spec to store, or a message to show.
pub(crate) fn prepare_vhost(spec: &str, account: &str, db: &dyn Store) -> Result<String, String> {
let host = normalize_vhost(spec);
if !valid_vhost(&host) {
return Err(format!("\x02{host}\x02 isn't a valid host (letters, digits, hyphens and dots)."));
}
if db.vhost_owner(&host).is_some_and(|owner| !owner.eq_ignore_ascii_case(account)) {
return Err(format!("\x02{host}\x02 is already in use. Please choose another."));
}
Ok(host)
}
// Whether `spec` is a valid vhost: an optional `ident@` (letters, digits, a few
// punctuation) followed by a hostname of dot-separated alphanumeric/hyphen labels.
pub(crate) fn valid_vhost(spec: &str) -> bool {

View file

@ -10,11 +10,14 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
ctx.notice(me, from.uid, "Syntax: REQUEST <host>");
return;
};
if !super::valid_vhost(host) {
ctx.notice(me, from.uid, format!("\x02{host}\x02 isn't a valid host (letters, digits, hyphens and dots)."));
return;
}
if db.vhost_is_forbidden(host) {
let host = match super::prepare_vhost(host, account, db) {
Ok(h) => h,
Err(msg) => {
ctx.notice(me, from.uid, msg);
return;
}
};
if db.vhost_is_forbidden(&host) {
ctx.notice(me, from.uid, format!("\x02{host}\x02 isn't allowed here. Please choose another."));
return;
}
@ -23,7 +26,7 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
ctx.notice(me, from.uid, format!("Please wait \x02{wait}\x02s before requesting another vhost."));
return;
}
match db.request_vhost(account, host) {
match db.request_vhost(account, &host) {
Ok(()) => ctx.notice(me, from.uid, format!("Requested vhost \x02{host}\x02 — an operator will review it.")),
Err(_) => ctx.notice(me, from.uid, "Sorry, that didn't work. Please try again in a moment."),
}

View file

@ -11,19 +11,22 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, net:
ctx.notice(me, from.uid, "Syntax: SET <account> <host> [duration]");
return;
};
if !super::valid_vhost(host) {
ctx.notice(me, from.uid, format!("\x02{host}\x02 isn't a valid host (letters, digits, hyphens and dots)."));
return;
}
if db.account(account).is_none() {
ctx.notice(me, from.uid, format!("\x02{account}\x02 isn't registered."));
return;
}
let host = match super::prepare_vhost(host, account, db) {
Ok(h) => h,
Err(msg) => {
ctx.notice(me, from.uid, msg);
return;
}
};
let ttl = args.get(3).and_then(|s| parse_duration(s));
match db.set_vhost(account, host, from.nick, ttl) {
match db.set_vhost(account, &host, from.nick, ttl) {
Ok(()) => {
for uid in net.uids_logged_into(account) {
ctx.apply_vhost(&uid, host);
ctx.apply_vhost(&uid, &host);
}
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}."));

View file

@ -10,10 +10,17 @@ pub fn handle(me: &str, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, db:
ctx.notice(me, from.uid, "Syntax: TAKE <number> (see OFFERLIST)");
return;
};
let Some(host) = n.checked_sub(1).and_then(|i| db.vhost_offers().into_iter().nth(i)) else {
let Some(offer) = n.checked_sub(1).and_then(|i| db.vhost_offers().into_iter().nth(i)) else {
ctx.notice(me, from.uid, format!("There's no offer #\x02{n}\x02. See \x02OFFERLIST\x02."));
return;
};
let host = match super::prepare_vhost(&offer, account, db) {
Ok(h) => h,
Err(msg) => {
ctx.notice(me, from.uid, msg);
return;
}
};
match db.set_vhost(account, &host, "offer", None) {
Ok(()) => {
ctx.apply_vhost(from.uid, &host);