-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple ban types and actions
Bans can match at registration (as for K:lines), immediately on new connections (as D:lines), or when SASL authentication is attempted. They can also require SASL, block SASL, or disconnect the user.
- Loading branch information
Showing
14 changed files
with
317 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,4 @@ async-trait = "0.1.57" | |
structopt = "0.3" | ||
base64 = "0.21" | ||
anyhow = "1.0" | ||
serde_json = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use super::*; | ||
use sable_network::{chert, network::ban::*}; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all = "snake_case")] | ||
enum NewBanAction { | ||
RefuseConnection, | ||
RequireSasl, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct NewBanArguments { | ||
#[serde(rename = "type")] | ||
match_type: Option<BanMatchType>, | ||
action: Option<NewBanAction>, | ||
apply_existing: Option<bool>, | ||
pattern: String, | ||
duration: i64, | ||
reason: String, | ||
oper_reason: Option<String>, | ||
} | ||
|
||
#[command_handler("BAN")] | ||
fn handle_ban( | ||
server: &ClientServer, | ||
source: UserSource, | ||
response: &dyn CommandResponse, | ||
new_ban_str: &str, | ||
) -> CommandResult { | ||
server.policy().require_oper(&source)?; | ||
|
||
let new_ban_details: NewBanArguments = match serde_json::from_str(new_ban_str) { | ||
Ok(ban) => ban, | ||
Err(e) => { | ||
response.send(message::Fail::new("BAN", "INVALID_BAN", "", &e.to_string())); | ||
return Ok(()); | ||
} | ||
}; | ||
|
||
let match_type = new_ban_details | ||
.match_type | ||
.unwrap_or(BanMatchType::PreRegistration); | ||
|
||
let action = match match_type { | ||
BanMatchType::PreSasl => { | ||
// Only valid action here is DenySasl | ||
NetworkBanAction::DenySasl | ||
} | ||
_ => match new_ban_details.action { | ||
Some(NewBanAction::RefuseConnection) => { | ||
NetworkBanAction::RefuseConnection(new_ban_details.apply_existing.unwrap_or(true)) | ||
} | ||
Some(NewBanAction::RequireSasl) => { | ||
NetworkBanAction::RequireSasl(new_ban_details.apply_existing.unwrap_or(true)) | ||
} | ||
None => NetworkBanAction::RefuseConnection(true), | ||
}, | ||
}; | ||
|
||
let pattern_parsed = match match_type { | ||
BanMatchType::PreRegistration => { | ||
chert::parse::<PreRegistrationBanSettings>(&new_ban_details.pattern) | ||
.map(|ast| ast.get_root().clone()) | ||
} | ||
BanMatchType::NewConnection => { | ||
chert::parse::<NewConnectionBanSettings>(&new_ban_details.pattern) | ||
.map(|ast| ast.get_root().clone()) | ||
} | ||
BanMatchType::PreSasl => chert::parse::<PreSaslBanSettings>(&new_ban_details.pattern) | ||
.map(|ast| ast.get_root().clone()), | ||
}; | ||
|
||
let pattern = match pattern_parsed { | ||
Ok(node) => node, | ||
Err(e) => { | ||
response.send(message::Fail::new( | ||
"BAN", | ||
"INVALID_BAN_PATTERN", | ||
"", | ||
&format!("{:?}", e), | ||
)); | ||
return Ok(()); | ||
} | ||
}; | ||
|
||
let timestamp = sable_network::utils::now(); | ||
let expires = timestamp + new_ban_details.duration * 60; | ||
|
||
let new_ban_id = server.ids().next_network_ban(); | ||
|
||
let new_ban = event::details::NewNetworkBan { | ||
match_type, | ||
pattern, | ||
action, | ||
timestamp, | ||
expires, | ||
reason: new_ban_details.reason, | ||
oper_reason: new_ban_details.oper_reason, | ||
setter_info: source.0.nuh(), | ||
}; | ||
|
||
server.node().submit_event(new_ban_id, new_ban); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ mod handlers { | |
|
||
mod admin; | ||
mod away; | ||
mod ban; | ||
mod cap; | ||
mod chathistory; | ||
mod invite; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.