From 1f679a9b9e9debc8e7181bc4d2ffd5da4300d9fd Mon Sep 17 00:00:00 2001 From: Dastan-glitch Date: Wed, 17 Aug 2022 21:19:39 +0300 Subject: [PATCH] bin/ircd: add support for password registration --- bin/ircd/src/main.rs | 7 +++++++ bin/ircd/src/server.rs | 27 ++++++++++++++++++++++++++- bin/ircd/src/settings.rs | 4 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/bin/ircd/src/main.rs b/bin/ircd/src/main.rs index d0906c6ca..af0dc714f 100644 --- a/bin/ircd/src/main.rs +++ b/bin/ircd/src/main.rs @@ -60,6 +60,7 @@ struct Ircd { // p2p p2p: net::P2pPtr, senders: SubscriberPtr, + password: String, } impl Ircd { @@ -67,6 +68,7 @@ impl Ircd { seen_msg_ids: SeenMsgIds, privmsgs_buffer: PrivmsgsBuffer, autojoin_chans: Vec, + password: String, configured_chans: FxHashMap, configured_contacts: FxHashMap, p2p: net::P2pPtr, @@ -76,6 +78,7 @@ impl Ircd { seen_msg_ids, privmsgs_buffer, autojoin_chans, + password, configured_chans, configured_contacts, p2p, @@ -114,6 +117,7 @@ impl Ircd { self.seen_msg_ids.clone(), self.privmsgs_buffer.clone(), self.autojoin_chans.clone(), + self.password.clone(), self.configured_chans.clone(), self.configured_contacts.clone(), self.p2p.clone(), @@ -174,6 +178,8 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { return Ok(()) } + let password = settings.password.unwrap_or(String::default()); + // Pick up channel settings from the TOML configuration let cfg_path = get_config_path(settings.config, CONFIG_FILE)?; let toml_contents = std::fs::read_to_string(cfg_path)?; @@ -271,6 +277,7 @@ async fn realmain(settings: Args, executor: Arc>) -> Result<()> { seen_msg_ids.clone(), privmsgs_buffer.clone(), settings.autojoin.clone(), + password.clone(), configured_chans.clone(), configured_contacts.clone(), p2p.clone(), diff --git a/bin/ircd/src/server.rs b/bin/ircd/src/server.rs index d095e935d..176fe8e85 100644 --- a/bin/ircd/src/server.rs +++ b/bin/ircd/src/server.rs @@ -31,6 +31,7 @@ pub struct IrcServerConnection, pub configured_chans: FxHashMap, @@ -40,6 +41,7 @@ pub struct IrcServerConnection, subscriber_id: u64, + password: String, } impl IrcServerConnection { @@ -50,6 +52,7 @@ impl IrcServerConnection seen_msg_ids: SeenMsgIds, privmsgs_buffer: PrivmsgsBuffer, auto_channels: Vec, + password: String, configured_chans: FxHashMap, configured_contacts: FxHashMap, p2p: P2pPtr, @@ -67,8 +70,10 @@ impl IrcServerConnection is_user_init: false, is_registered: false, is_cap_end: true, + is_pass_init: false, nickname: "anon".to_string(), auto_channels, + password, configured_chans, configured_contacts, capabilities, @@ -83,6 +88,10 @@ impl IrcServerConnection return Err(Error::MalformedPacket) } + if self.password.is_empty() { + self.is_pass_init = true + } + let mut tokens = line.split_ascii_whitespace(); // Commands can begin with :garbage but we will reject clients doing // that for now to keep the protocol simple and focused. @@ -91,10 +100,26 @@ impl IrcServerConnection info!("IRC server received command: {}", command.to_uppercase()); match command.to_uppercase().as_str() { + "PASS" => { + let password = tokens.next().ok_or(Error::MalformedPacket)?; + if self.password == password.to_string() { + self.is_pass_init = true + } else { + // Close the connection + warn!("Password is not correct!"); + return Err(Error::NetworkServiceStopped) + } + } "USER" => { // We can stuff any extra things like public keys in here. // Ignore it for now. - self.is_user_init = true; + if self.is_pass_init { + self.is_user_init = true; + } else { + // Close the connection + warn!("Password is required"); + return Err(Error::NetworkServiceStopped) + } } "NAMES" => { let channels = tokens.next().ok_or(Error::MalformedPacket)?; diff --git a/bin/ircd/src/settings.rs b/bin/ircd/src/settings.rs index 5c4a22926..c00398632 100644 --- a/bin/ircd/src/settings.rs +++ b/bin/ircd/src/settings.rs @@ -43,6 +43,10 @@ pub struct Args { #[structopt(long)] pub autojoin: Vec, + /// Password + #[structopt(long)] + pub password: Option, + #[structopt(flatten)] pub net: SettingsOpt,