Basic PAM auth

This commit is contained in:
Candifloss 2026-01-17 23:05:26 +05:30
parent 2650b2621f
commit ca27c091f2
4 changed files with 56 additions and 2 deletions

7
.gitignore vendored
View File

@ -19,3 +19,10 @@ Cargo.lock
# Added by cargo
/target
# Added by cargo
#
# already existing elements were commented out
#/target

View File

@ -1,7 +1,11 @@
[package]
name = "breaDM"
version = "0.1.0"
edition = "2021"
edition = "2024"
[[bin]]
name = "breadm"
path = "src/main.rs"
[dependencies]
pam = "0.8.0"

24
src/auth.rs Normal file
View File

@ -0,0 +1,24 @@
use pam::Client;
use std::error::Error;
/// Authenticate a user using PAM.
///
/// Arguments
/// username: login name as typed
/// password: secret provided by user
///
/// Uses the `login` PAM service for now.
pub fn authenticate(username: &str, password: &str) -> Result<(), Box<dyn Error>> {
// Initialize PAM with the login service
let mut client = Client::with_password("login")?;
// Provide credentials to PAM conversation
client
.conversation_mut()
.set_credentials(username.to_owned(), password.to_owned());
// Authenticate
client.authenticate()?;
Ok(())
}

View File

@ -1,3 +1,22 @@
mod auth;
use std::io;
fn main() {
println!("Hello, world!");
let mut username = String::new();
let mut password = String::new();
println!("username:");
io::stdin().read_line(&mut username).unwrap();
println!("password:");
io::stdin().read_line(&mut password).unwrap();
let username = username.trim();
let password = password.trim();
match auth::authenticate(username, password) {
Ok(()) => println!("authentication successful"),
Err(e) => eprintln!("authentication failed: {e}"),
}
}