From 757991b8a05f6f2e89641ed48504ca2f0e558013 Mon Sep 17 00:00:00 2001 From: 34n0 <34n0@immerda.ch> Date: Sat, 13 Jan 2024 20:52:25 +0100 Subject: [PATCH] feat: referenced tally_dir instead of String --- src/settings.rs | 30 ++++++++++++++++-------------- src/tally.rs | 4 ++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index ee52bf7..ff93861 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -37,12 +37,11 @@ use std::path::PathBuf; use users::User; -const DEFAULT_TALLY_DIR: &str = "/var/run/authramp"; const DEFAULT_CONFIG_FILE_PATH: &str = "/etc/security/authramp.conf"; // Settings struct represents the configuration loaded from default values, configuration file and parameters #[derive(Debug)] -pub struct Settings { +pub struct Settings<'a> { // Directory where tally information is stored. pub tally_dir: PathBuf, // Number of allowed free authentication attempts before applying delays. @@ -52,7 +51,7 @@ pub struct Settings { // Multiplier for the delay calculation based on the number of failures. pub ramp_multiplier: i32, // PAM Hook - pub pam_hook: String, + pub pam_hook: &'a str, // PAM action pub action: Option, // PAM user @@ -61,23 +60,23 @@ pub struct Settings { pub even_deny_root: bool, } -impl Default for Settings { +impl Default for Settings<'_> { /// Creates a default 'Settings' struct. Default configruation values are set here. fn default() -> Self { Settings { - tally_dir: PathBuf::from(DEFAULT_TALLY_DIR), + tally_dir: PathBuf::from("/var/run/authramp"), action: Some(Actions::AUTHSUCC), user: None, free_tries: 6, base_delay_seconds: 30, ramp_multiplier: 50, - pam_hook: String::from("auth"), + pam_hook: "auth", even_deny_root: false, } } } -impl Settings { +impl Settings<'_> { /// Constructs a `Settings` instance based on input parameters, including user /// information, PAM flags, and an optional configuration file path. /// @@ -94,13 +93,13 @@ impl Settings { /// /// A `Result` containing the constructed `Settings` instance or a `PamResultCode` /// indicating an error during the construction process. - pub fn build( + pub fn build<'a>( user: Option, args: Vec<&CStr>, _flags: PamFlag, config_file: Option, - pam_hook: &str, - ) -> Result { + pam_hook: &'a str, + ) -> Result, PamResultCode> { // Load INI file. let mut settings = Self::load_conf_file(config_file); @@ -128,7 +127,7 @@ impl Settings { settings.user = Some(user.ok_or(PamResultCode::PAM_SYSTEM_ERR)?); // pam hook - settings.pam_hook = String::from(pam_hook); + settings.pam_hook = pam_hook; Ok(settings) } @@ -167,7 +166,7 @@ impl Settings { /// /// A `Settings` instance populated with values from the configuration file, or the /// default values if the file is not present or cannot be loaded. - fn load_conf_file(config_file: Option) -> Settings { + fn load_conf_file(config_file: Option) -> Settings<'static> { // Read TOML file using the toml crate let content = fs::read_to_string(config_file.unwrap_or(PathBuf::from(DEFAULT_CONFIG_FILE_PATH))).ok(); @@ -222,7 +221,10 @@ mod tests { #[test] fn test_default_settings() { let default_settings = Settings::default(); - assert_eq!(default_settings.tally_dir, PathBuf::from(DEFAULT_TALLY_DIR)); + assert_eq!( + default_settings.tally_dir, + PathBuf::from("/var/run/authramp") + ); assert_eq!(default_settings.action, Some(Actions::AUTHSUCC)); assert!(default_settings.user.is_none()); assert_eq!(default_settings.free_tries, 6); @@ -321,8 +323,8 @@ mod tests { let settings = result.unwrap(); println!("{:?}", settings); assert_eq!(settings.action, Some(Actions::PREAUTH)); - assert_eq!(settings.tally_dir, PathBuf::from(DEFAULT_TALLY_DIR)); assert_eq!(settings.free_tries, 6); + assert_eq!(settings.tally_dir, PathBuf::from("/var/run/authramp")); assert_eq!(settings.base_delay_seconds, 30); assert_eq!(settings.ramp_multiplier, 50); assert!(!settings.even_deny_root); diff --git a/src/tally.rs b/src/tally.rs index 3ec3fef..3bb4926 100644 --- a/src/tally.rs +++ b/src/tally.rs @@ -394,7 +394,7 @@ mod tests { free_tries: 6, ramp_multiplier: 50, base_delay_seconds: 30, - pam_hook: String::from("test"), + pam_hook: "test", even_deny_root: false, }; @@ -435,7 +435,7 @@ mod tests { free_tries: 6, ramp_multiplier: 50, base_delay_seconds: 30, - pam_hook: String::from("test"), + pam_hook: "test", even_deny_root: false, };