From bb65edd766e3567429b72af886334750bb32a63c Mon Sep 17 00:00:00 2001 From: Barry Pederson Date: Mon, 28 Feb 2022 19:14:54 -0800 Subject: [PATCH] Add structopt option parsing, use a plain textfile for each user rather than YAML --- Cargo.lock | 198 +++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 4 +- passwords/bp | 3 + src/main.rs | 216 +++++++++++++++++++++++++------------------------ src/options.rs | 23 ++++++ 5 files changed, 319 insertions(+), 125 deletions(-) create mode 100644 passwords/bp create mode 100644 src/options.rs diff --git a/Cargo.lock b/Cargo.lock index 986d131..37051cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,53 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "error-chain" version = "0.12.4" @@ -17,6 +58,24 @@ dependencies = [ "version_check", ] +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hostname" version = "0.3.1" @@ -34,18 +93,18 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.119" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" -[[package]] -name = "linked-hash-map" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" - [[package]] name = "log" version = "0.4.14" @@ -70,12 +129,95 @@ dependencies = [ "libc", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +dependencies = [ + "proc-macro2", +] + [[package]] name = "saslauthd" version = "0.1.0" dependencies = [ + "structopt", "syslog", - "yaml-rust", +] + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "structopt" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] @@ -91,6 +233,15 @@ dependencies = [ "time", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "time" version = "0.3.7" @@ -102,6 +253,30 @@ dependencies = [ "num_threads", ] +[[package]] +name = "unicode-segmentation" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" + +[[package]] +name = "unicode-width" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.4" @@ -129,12 +304,3 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "yaml-rust" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" -dependencies = [ - "linked-hash-map", -] diff --git a/Cargo.toml b/Cargo.toml index 912a845..3870116 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -syslog = "^6.0" -yaml-rust = "0.4" \ No newline at end of file +structopt = "^0.3.26" +syslog = "^6.0" \ No newline at end of file diff --git a/passwords/bp b/passwords/bp new file mode 100644 index 0000000..8446d27 --- /dev/null +++ b/passwords/bp @@ -0,0 +1,3 @@ +# Demo password file + +some-rad-password \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b757b1f..78bd9ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,155 +1,157 @@ -use std::fs::{metadata, set_permissions}; -use std::io::{Error, ErrorKind, Read, Write}; +use std::fs::{metadata, set_permissions, File}; +use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Write}; use std::net::Shutdown; use std::os::unix::fs::PermissionsExt; use std::os::unix::net::{UnixListener, UnixStream}; -use std::path::PathBuf; -use std::{fs, thread}; +use std::thread; +use structopt::StructOpt; -extern crate yaml_rust; +use crate::options::Opt; -use yaml_rust::{yaml::Hash, Yaml, YamlLoader}; +mod options; const RESPONSE_NO: [u8; 4] = [0x0, 0x2, b'N', b'O']; const RESPONSE_OK: [u8; 4] = [0x0, 0x2, b'O', b'K']; -struct SaslSock { - name: PathBuf, +struct Listener { + options: Opt, listener: UnixListener, } -impl SaslSock { - pub fn new(name: &str) -> Result { - let path = PathBuf::from(&name); - if path.exists() { - std::fs::remove_file(&path)?; +struct Server { + options: Opt, + stream: UnixStream, +} + +impl Listener { + pub fn new(options: Opt) -> Result { + if options.socket.exists() { + std::fs::remove_file(&options.socket)?; } - let listener = UnixListener::bind(&name)?; + let listener = UnixListener::bind(&options.socket)?; - let mut perms = metadata(&path)?.permissions(); + let mut perms = metadata(&options.socket)?.permissions(); perms.set_mode(0o0777); - set_permissions(&path, perms)?; + set_permissions(&options.socket, perms)?; - Ok(SaslSock { - name: PathBuf::from(&name), - listener, - }) + Ok(Listener { options, listener }) + } + + pub fn run(&mut self) -> Result<(), Error> { + // accept connections and process them, spawning a new thread for each one + for stream in self.listener.incoming() { + match stream { + Ok(stream) => { + /* connection succeeded */ + let options = self.options.clone(); + thread::spawn(|| Server::new(options, stream).unwrap().handle_client()); + } + Err(_err) => { + /* connection failed */ + break; + } + } + } + Ok(()) } } -impl Drop for SaslSock { +impl Drop for Listener { fn drop(&mut self) { - let _result = std::fs::remove_file(&self.name); + let _result = std::fs::remove_file(&self.options.socket); println!("Shutting down"); } } -/// The saslauthd protocol transmits strings as -/// 16-bit unsigned network-byte-order lengths followed -/// by the string itself. -/// -fn read_string(stream: &mut UnixStream) -> Result { - let mut length_buffer = [0u8, 0u8]; - - stream.read_exact(&mut length_buffer)?; - let length = u16::from_be_bytes(length_buffer); - - let mut string_buffer = vec![0u8; length as usize]; - - stream.read_exact(&mut string_buffer)?; - - match String::from_utf8(string_buffer) { - Ok(str) => Ok(str), - Err(_err) => Err(Error::new(ErrorKind::Other, "Invalid UTF-8 sent")), - } -} - -fn write_string(s: &str, stream: &mut UnixStream) -> Result<(), Error> { - let length = s.len(); - - if length > 0xffff { - return Err(Error::new(ErrorKind::Other, "String too long to write")); +impl Server { + pub fn new(options: Opt, stream: UnixStream) -> Result { + Ok(Server { options, stream }) } - let length_buffer = [(length >> 8) as u8, (length & 0xff) as u8]; + /// The saslauthd protocol transmits strings as + /// 16-bit unsigned network-byte-order lengths followed + /// by the string itself. + /// + fn read_string(&mut self) -> Result { + let mut length_buffer = [0u8, 0u8]; - stream.write_all(&length_buffer)?; - stream.write_all(s.as_bytes())?; + self.stream.read_exact(&mut length_buffer)?; + let length = u16::from_be_bytes(length_buffer); - Ok(()) -} + let mut string_buffer = vec![0u8; length as usize]; -fn check_auth(userid: &str, password: &str) -> Result { - let yaml = fs::read_to_string("test.yml")?; + self.stream.read_exact(&mut string_buffer)?; - let docs = - YamlLoader::load_from_str(&yaml) - .map_err(|_| Error::new(ErrorKind::Other, "Can't parse YAML"))?; + match String::from_utf8(string_buffer) { + Ok(str) => Ok(str), + Err(_err) => Err(Error::new(ErrorKind::Other, "Invalid UTF-8 sent")), + } + } - let doc = &docs[0]; - let users = doc["users"] - .as_hash() - .ok_or(Error::new(ErrorKind::Other, "Can't find 'users' in YAML"))?; + fn write_string(&mut self, s: &str) -> Result<(), Error> { + let length = s.len(); - let passwords = users[&Yaml::from_str(&userid)].as_vec(); + if length > 0xffff { + return Err(Error::new(ErrorKind::Other, "String too long to write")); + } - if let Some(passwords) = passwords { - // Found the passwords for the user, look for any matches + let length_buffer = [(length >> 8) as u8, (length & 0xff) as u8]; - for p in passwords { - if let Some(str) = p.as_str() { - if str == password { - return Ok(true); - } + self.stream.write_all(&length_buffer)?; + self.stream.write_all(s.as_bytes())?; + + Ok(()) + } + + fn check_auth(&mut self, userid: &str, password: &str) -> Result { + // Don't allow empty userids or passwords + // + if userid.is_empty() || password.is_empty() { + return Ok(false); + } + + let mut password_file = self.options.passwords.clone(); + password_file.push(&userid); + + let file = File::open(password_file)?; + + for line in BufReader::new(file).lines().flatten() { + if !line.starts_with('#') && line.trim() == password { + return Ok(true); } } + + Ok(false) } - Ok(false) -} + fn communicate(&mut self) -> Result<(), Error> { + let userid = self.read_string()?; + let password = self.read_string()?; + let service = self.read_string()?; + let _realm = self.read_string()?; - -fn communicate(mut stream: UnixStream) -> Result<(), Error> { - let userid = read_string(&mut stream)?; - let password = read_string(&mut stream)?; - let service = read_string(&mut stream)?; - let _realm = read_string(&mut stream)?; - - match check_auth(&userid, &password) { - Ok(true) => stream.write_all(&RESPONSE_OK)?, - Ok(false) => stream.write_all(&RESPONSE_NO)?, - Err(e) => { - stream.write_all(&RESPONSE_NO)?; - stream.shutdown(Shutdown::Both)?; - return Err(e) + match self.check_auth(&userid, &password) { + Ok(true) => self.stream.write_all(&RESPONSE_OK)?, + Ok(false) => self.stream.write_all(&RESPONSE_NO)?, + Err(e) => { + self.stream.write_all(&RESPONSE_NO)?; + self.stream.shutdown(Shutdown::Both)?; + return Err(e); + } } + + Ok(()) } - Ok(()) -} - -fn handle_client(stream: UnixStream) { - if let Err(error) = communicate(stream) { - eprint!("{}", error); + fn handle_client(&mut self) { + if let Err(error) = self.communicate() { + eprint!("{}", error); + } } } fn main() -> std::io::Result<()> { - let sock = SaslSock::new("/tmp/saslauthd.sock")?; - - // accept connections and process them, spawning a new thread for each one - for stream in sock.listener.incoming() { - match stream { - Ok(stream) => { - /* connection succeeded */ - thread::spawn(|| handle_client(stream)); - } - Err(_err) => { - /* connection failed */ - break; - } - } - } - Ok(()) + let mut server = Listener::new(Opt::from_args())?; + server.run() } diff --git a/src/options.rs b/src/options.rs new file mode 100644 index 0000000..374528a --- /dev/null +++ b/src/options.rs @@ -0,0 +1,23 @@ +use std::path::PathBuf; +use structopt::StructOpt; + +#[derive(StructOpt, Clone, Debug)] +#[structopt(name = "simple_saslauthd")] +/// Rust-based web crawler +pub struct Opt { + #[structopt( + short = "f", + long = "socket", + default_value = "/tmp/saslauthd.sock", + parse(from_os_str) + )] + pub socket: PathBuf, + + #[structopt( + short = "p", + long = "passwords", + default_value = "./passwords", + parse(from_os_str) + )] + pub passwords: PathBuf, +}