Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: referenced tally_dir instead of String #26

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Actions>,
// PAM user
Expand All @@ -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.
///
Expand All @@ -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<User>,
args: Vec<&CStr>,
_flags: PamFlag,
config_file: Option<PathBuf>,
pam_hook: &str,
) -> Result<Settings, PamResultCode> {
pam_hook: &'a str,
) -> Result<Settings<'a>, PamResultCode> {
// Load INI file.
let mut settings = Self::load_conf_file(config_file);

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<PathBuf>) -> Settings {
fn load_conf_file(config_file: Option<PathBuf>) -> 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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/tally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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,
};

Expand Down