34 lines
1.1 KiB
Rust
34 lines
1.1 KiB
Rust
use std::io::stdin;
|
|
|
|
use crate::user_info::get_username;
|
|
|
|
const AUTH_SERVICE: &'static str = "system-auth";
|
|
const RETRIES: u32 = 3;
|
|
|
|
pub fn check_auth(from_stdin: bool) -> Result<(), String> {
|
|
let login = get_username().unwrap();
|
|
for i in 0..RETRIES {
|
|
let password = if from_stdin {
|
|
eprintln!["password for {login}: "];
|
|
let mut buf = String::new();
|
|
if let Err(e) = stdin().read_line(&mut buf) {
|
|
return Err(format!["Error: Rudo: Failed to read stdin: {e}"]);
|
|
}
|
|
buf
|
|
} else {
|
|
rpassword::prompt_password(format!["password for {login}: "]).unwrap()
|
|
};
|
|
let mut auth = pam::Authenticator::with_password(AUTH_SERVICE).unwrap();
|
|
auth.get_handler().set_credentials(&login, password);
|
|
match auth.authenticate() {
|
|
Ok(()) => return Ok(()),
|
|
Err(_) => {
|
|
if i < RETRIES - 1 {
|
|
eprintln!["incorrect password, try again"]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Err(String::from("Error: Authentication error."))
|
|
}
|