intial commit
This commit is contained in:
40
src/auth/pamsock.rs
Normal file
40
src/auth/pamsock.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
//! Horrible error handling here btw
|
||||
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
use pamsock::prot::ServerResponse;
|
||||
|
||||
use super::AuthenticateResponse;
|
||||
|
||||
#[allow(clippy::from_over_into)]
|
||||
impl Into<AuthenticateResponse<&'static str>> for ServerResponse {
|
||||
fn into(self) -> AuthenticateResponse<&'static str> {
|
||||
use AuthenticateResponse as AR;
|
||||
|
||||
match self {
|
||||
Self::ServerError => AR::Failed("unknown server error"),
|
||||
Self::Locked => AR::Failed("account locked, too many login attempts"),
|
||||
Self::Failed => AR::Failed("wrong credentials"),
|
||||
Self::Succeeded => AR::Success,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn authenticate(
|
||||
cfg: &crate::args::Args,
|
||||
user: &str,
|
||||
passwd: &str,
|
||||
) -> Option<AuthenticateResponse<impl Display + Debug>> {
|
||||
use std::os::unix::net::UnixStream;
|
||||
use tokio::net::UnixStream as AsyncUnixStream;
|
||||
|
||||
let std_sock = UnixStream::connect_addr(&cfg.pamsock_abstract_name).ok()?;
|
||||
std_sock.set_nonblocking(true).ok();
|
||||
let async_sock = AsyncUnixStream::from_std(std_sock).ok()?;
|
||||
|
||||
Some(
|
||||
pamsock::prot::attempt_login_async(async_sock, user, passwd)
|
||||
.await?
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user