Use once_cell to allow for global static immutable options

So they don't have to be passed around all over the place
This commit is contained in:
Barry Pederson
2022-03-01 10:03:38 -08:00
parent a244e714ea
commit b9c091f71f
6 changed files with 27 additions and 23 deletions
Generated
+7
View File
@@ -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",
]
+1
View File
@@ -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"
+11 -13
View File
@@ -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<Listener, Error> {
if options.socket.exists() {
std::fs::remove_file(&options.socket)?;
pub fn new() -> Result<Listener, Error> {
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");
}
}
}
+1 -5
View File
@@ -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()
}
+3
View File
@@ -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<Opt> = Lazy::new(Opt::from_args);
+4 -5
View File
@@ -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<Server, Error> {
Ok(Server { options, stream })
pub fn new(stream: UnixStream) -> Result<Server, Error> {
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)?;