diff --git a/Cargo.lock b/Cargo.lock index 37051cb..b766437 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,6 +129,12 @@ dependencies = [ "libc", ] +[[package]] +name = "once_cell" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -175,6 +181,7 @@ dependencies = [ name = "saslauthd" version = "0.1.0" dependencies = [ + "once_cell", "structopt", "syslog", ] diff --git a/Cargo.toml b/Cargo.toml index 3870116..8719967 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +once_cell = "^1.9.0" structopt = "^0.3.26" syslog = "^6.0" \ No newline at end of file diff --git a/src/listener.rs b/src/listener.rs index 851ab17..336985d 100644 --- a/src/listener.rs +++ b/src/listener.rs @@ -4,27 +4,26 @@ use std::os::unix::fs::PermissionsExt; use std::os::unix::net::UnixListener; use std::thread; -use crate::options::Opt; +use crate::options::OPTIONS; use crate::server::Server; pub struct Listener { - options: Opt, listener: UnixListener, } impl Listener { - pub fn new(options: Opt) -> Result { - if options.socket.exists() { - std::fs::remove_file(&options.socket)?; + pub fn new() -> Result { + if OPTIONS.socket.exists() { + std::fs::remove_file(&OPTIONS.socket)?; } - let listener = UnixListener::bind(&options.socket)?; + let listener = UnixListener::bind(&OPTIONS.socket)?; - let mut perms = metadata(&options.socket)?.permissions(); + let mut perms = metadata(&OPTIONS.socket)?.permissions(); perms.set_mode(0o0777); - set_permissions(&options.socket, perms)?; + set_permissions(&OPTIONS.socket, perms)?; - Ok(Listener { options, listener }) + Ok(Listener { listener }) } pub fn run(&mut self) -> Result<(), Error> { @@ -33,8 +32,7 @@ impl Listener { match stream { Ok(stream) => { /* connection succeeded */ - let options = self.options.clone(); - thread::spawn(|| Server::new(options, stream).unwrap().handle_client()); + thread::spawn(|| Server::new(stream).unwrap().handle_client()); } Err(_err) => { /* connection failed */ @@ -48,7 +46,7 @@ impl Listener { impl Drop for Listener { fn drop(&mut self) { - let _result = std::fs::remove_file(&self.options.socket); + let _result = std::fs::remove_file(&OPTIONS.socket); println!("Shutting down"); } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 03868bd..62c9a7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,13 +2,9 @@ mod listener; mod options; mod server; -use structopt::StructOpt; - use crate::listener::Listener; -use crate::options::Opt; - fn main() -> std::io::Result<()> { - let mut server = Listener::new(Opt::from_args())?; + let mut server = Listener::new()?; server.run() } diff --git a/src/options.rs b/src/options.rs index 374528a..2677da3 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,3 +1,4 @@ +use once_cell::sync::Lazy; use std::path::PathBuf; use structopt::StructOpt; @@ -21,3 +22,5 @@ pub struct Opt { )] pub passwords: PathBuf, } + +pub static OPTIONS: Lazy = Lazy::new(Opt::from_args); diff --git a/src/server.rs b/src/server.rs index 02d391c..874d491 100644 --- a/src/server.rs +++ b/src/server.rs @@ -3,19 +3,18 @@ use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Write}; use std::net::Shutdown; use std::os::unix::net::UnixStream; -use crate::options::Opt; +use crate::options::OPTIONS; const RESPONSE_NO: [u8; 4] = [0x0, 0x2, b'N', b'O']; const RESPONSE_OK: [u8; 4] = [0x0, 0x2, b'O', b'K']; pub struct Server { - options: Opt, stream: UnixStream, } impl Server { - pub fn new(options: Opt, stream: UnixStream) -> Result { - Ok(Server { options, stream }) + pub fn new(stream: UnixStream) -> Result { + Ok(Server { stream }) } /// The saslauthd protocol transmits strings as @@ -60,7 +59,7 @@ impl Server { return Ok(false); } - let mut password_file = self.options.passwords.clone(); + let mut password_file = OPTIONS.passwords.clone(); password_file.push(&userid); let file = File::open(password_file)?;