diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-01-29 17:26:42 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-01-29 17:26:42 +0000 |
commit | 7365f0c5758954ea29b3e34648baca4b9876e624 (patch) | |
tree | daaade7385778a638ac4262ad49f05029ba47fd3 /src/pam.rs | |
parent | dca986f599e1abe4270a06efaf91759a926f2424 (diff) | |
download | pam_usercg_rust-7365f0c5758954ea29b3e34648baca4b9876e624.tar.gz pam_usercg_rust-7365f0c5758954ea29b3e34648baca4b9876e624.tar.xz pam_usercg_rust-7365f0c5758954ea29b3e34648baca4b9876e624.zip |
Remove dependency on pam-bindings
Diffstat (limited to 'src/pam.rs')
-rw-r--r-- | src/pam.rs | 39 |
1 files changed, 39 insertions, 0 deletions
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<S: AsRef<CStr> + ?Sized>( + &self, + prompt: Option<&S>, + ) -> Result<CString, c_int> { + 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), + } + } +} |