Skip to content

Commit

Permalink
fix: cli print & rm unimplemented command
Browse files Browse the repository at this point in the history
  • Loading branch information
34N0 committed Jan 19, 2024
1 parent d3fbf6b commit f1dcac6
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 36 deletions.
38 changes: 29 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [ "crates/lib", "crates/cli", "crates/util", "crates/xtask-test-integr

[workspace.package]
edition = "2021"
version = "0.9.0"
version = "0.9.1"
description = "The AuthRamp PAM module provides an account lockout mechanism based on the number of authentication failures."
authors = ["34n0 <34n0@immerda.ch>"]
license = "GPL-3.0"
Expand All @@ -26,6 +26,7 @@ anyhow = "1.0.75"
cli-xtask = { version = "0.8.0", features = ["main", "lib-crate"] }
xshell = "0.2.5"
clap = { version = "4.4.16", features = ["derive"] }
colored = "2.1.0"

[workspace.lints.clippy]
pedantic = { level = "deny" }
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ The AuthRamp PAM (Pluggable Authentication Modules) module provides an account l
### RPM
If you're a RPM distribution user, then then pam-authramp can be installed using a binary .rpm file provided in each [release](https://github.com/34N0/pam-authramp/releases).
```bash
curl -LO https://github.com/34N0/pam-authramp/releases/download/v0.3.0-alpha/pam-authramp-0.3.0-1.x86_64.rpm
sudo rpm -i pam-authramp-0.3.0-1.x86_64.rpm
curl -LO https://github.com/34N0/pam-authramp/releases/download/v0.9.1-beta/pam-authramp-0.9.1-1.x86_64.rpm
sudo rpm -i pam-authramp-0.9.1-1.x86_64.rpm
```
### Debian
If you're a Debian user (or a user of a Debian derivative like Ubuntu), then pam-authramp can be installed using a binary .deb file provided in each [release](https://github.com/34N0/pam-authramp/releases).
```bash
curl -LO https://github.com/34N0/pam-authramp/releases/download/v0.3.0-alpha/pam-authramp_0.3.0-1_amd64.deb
sudo dpkg -i pam-authramp_0.3.0-1_amd64.deb
curl -LO https://github.com/34N0/pam-authramp/releases/download/v0.9.1-beta/pam-authramp_0.9.1-1_amd64.deb
sudo dpkg -i pam-authramp_0.9.1-1_amd64.deb
```
### Manually
1. Download the latest [release](https://github.com/34N0/pam-authramp/releases).
Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ doc = false
[dependencies]
clap = { workspace = true, features = ["derive"] }
log.workspace = true
colored.workspace = true
util = { path = "../util" }

[dev-dependencies]
Expand Down
30 changes: 20 additions & 10 deletions crates/cli/src/cmd/reset.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
use colored::Colorize;
use std::{fs, path::PathBuf};
use util::config::Config;

use crate::{ArCliError, ArCliResult};
use crate::{ArCliError, ArCliInfo, ArCliResult as Acr, ArCliSuccess};

pub fn user(user: &str) -> ArCliResult {
pub fn user(user: &str) -> Acr {
let config = Config::load_file(None);

let tally_path = config.tally_dir.join(user);

delete_tally(&tally_path)
delete_tally(&tally_path, user)
}

fn delete_tally(path: &PathBuf) -> ArCliResult {
fn delete_tally(path: &PathBuf, user: &str) -> Acr {
match fs::remove_file(path) {
Ok(()) => Ok(Some(String::from("File successfully deleted"))),
Err(e) => Err(ArCliError {
message: format!("Error deleting file: {e}").to_string(),
}),
Ok(()) => Acr::Success(Some(ArCliSuccess {
message: format!("tally reset for user: '{}'", user.yellow()),
})),
Err(e) => {
if e.kind().eq(&std::io::ErrorKind::NotFound) {
Acr::Info(ArCliInfo {
message: format!("No tally found for user: '{}'", user.yellow()),
})
} else {
Acr::Error(ArCliError {
message: format!("{e}").to_string(),
})
}
}
}
}

Expand All @@ -36,10 +47,9 @@ mod tests {
fs::write(&temp_tally_path, "test tally").expect("Failed to create temporary file");

// Load the Config into the reset_user function
let result = delete_tally(&temp_tally_path);
let _result = delete_tally(&temp_tally_path, "test");

// Assert that the file is deleted successfully
assert!(result.is_ok());
assert!(!temp_tally_path.exists(), "Tally File not deleted!");
}
}
71 changes: 59 additions & 12 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::{Parser, Subcommand};
use cmd::reset;
use colored::Colorize;
use std::fmt;
use util::{log_error, log_info, syslog};
mod cmd;

Expand All @@ -9,14 +11,60 @@ const BANNER: &str = r"
██ ████ ██ ██ ██ ████ ████ ██████ ██████ ██
█████████ ██ ██ █████████████ █████████ ████ ████████
██ ████ ██ ██ ██ ████ ████ ████ ██ ████
██ ██ ██████ ██ ██ ████ ████ ████ ████";
██ ██ ██████ ██ ██ ████ ████ ████ ████
by 34n0@immerda.ch";

#[derive(Debug)]
struct ArCliError {
message: String,
}

type ArCliResult = Result<Option<String>, ArCliError>;
impl fmt::Display for ArCliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", "error:".red().bold(), self.message)
}
}

#[derive(Debug)]
struct ArCliSuccess {
message: String,
}

impl fmt::Display for ArCliSuccess {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", "success:".green().bold(), self.message)
}
}

#[derive(Debug)]
struct ArCliInfo {
message: String,
}

impl fmt::Display for ArCliInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", "info:".yellow().bold(), self.message)
}
}

#[derive(Debug)]
enum ArCliResult {
Success(Option<ArCliSuccess>),
Info(ArCliInfo),
Error(ArCliError),
}

impl fmt::Display for ArCliResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ArCliResult::Success(Some(ref success)) => write!(f, "{success}"),
ArCliResult::Success(None) => Ok(()),
ArCliResult::Error(ref error) => write!(f, "{error}"),
ArCliResult::Info(ref info) => write!(f, "{info}"),
}
}
}

#[derive(Parser, Debug)]
#[command(
Expand All @@ -32,8 +80,6 @@ struct Cli {

#[derive(Subcommand, Debug)]
enum Command {
#[command(about = "Audit the PAM authramp log")]
Audit,
#[command(about = "Reset a locked PAM user")]
Reset {
#[clap(long, short)]
Expand All @@ -46,19 +92,20 @@ fn main() {

let cli_res = match Cli::parse().command {
Some(Command::Reset { user }) => reset::user(&user),
_ => Ok(None),
_ => ArCliResult::Success(None),
};

match cli_res {
Ok(res) => {
match &cli_res {
ArCliResult::Success(res) => {
if let Some(res) = res {
log_info!("{}", &res);
println!("{}", &res);
log_info!("{}", &res.message);
}
}
Err(e) => {
log_error!("{}", &e.message);
println!("{}", &e.message);
ArCliResult::Error(res) => {
log_error!("{}", &res.message);
}
ArCliResult::Info(_) => (),
}

println!("{cli_res}");
}

0 comments on commit f1dcac6

Please sign in to comment.