From 7365f0c5758954ea29b3e34648baca4b9876e624 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sun, 29 Jan 2023 17:26:42 +0000 Subject: Remove dependency on pam-bindings --- src/pam.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/pam.rs (limited to 'src/pam.rs') diff --git a/src/pam.rs b/src/pam.rs new file mode 100644 index 0000000..17e4590 --- /dev/null +++ b/src/pam.rs @@ -0,0 +1,39 @@ +use core::marker::{PhantomData, PhantomPinned}; +use std::ffi::{c_char, c_int, CStr, CString}; + +#[repr(C)] +pub struct PAMHandle { + _data: [u8; 0], + _marker: PhantomData<(*mut u8, PhantomPinned)>, +} + +pub const SUCCESS: c_int = 0; +pub const SESSION_ERR: c_int = 14; + +extern "C" { + fn pam_get_user( + pamh: *const PAMHandle, + user: &*const c_char, + prompt: *const c_char, + ) -> c_int; +} + +impl PAMHandle { + pub fn get_user + ?Sized>( + &self, + prompt: Option<&S>, + ) -> Result { + let ptr: *const c_char = std::ptr::null_mut(); + let prompt = match prompt { + Some(prompt) => prompt.as_ref().as_ptr(), + None => core::ptr::null(), + }; + match unsafe { pam_get_user(self, &ptr, prompt) } { + SUCCESS if !ptr.is_null() => { + let cstr = unsafe { CStr::from_ptr(ptr) }; + Ok(CString::from(cstr)) + } + e => Err(e), + } + } +} -- cgit v1.2.3-54-g00ecf