diff src/pam_ffi.rs @ 66:a674799a5cd3

Make `PamHandle` and `PamModuleHandle` traits. This creates traits for PAM functionality and pulls the definitions of that functionality out of the original `PamHandle` (renamed to `LibPamHandle`) and into those traits. This supports testing PAM module implementations using mock PAM library implementations. Also uses a better representation of opaque pointers.
author Paul Fisher <paul@pfish.zone>
date Tue, 27 May 2025 14:37:28 -0400
parents bbe84835d6db
children
line wrap: on
line diff
--- a/src/pam_ffi.rs	Thu May 22 02:08:10 2025 -0400
+++ b/src/pam_ffi.rs	Tue May 27 14:37:28 2025 -0400
@@ -2,17 +2,25 @@
 
 use libc::c_char;
 use std::ffi::c_int;
+use std::marker::{PhantomData, PhantomPinned};
+
+/// An opaque pointer given to us by PAM.
+#[repr(C)]
+pub struct Handle {
+    _data: (),
+    _marker: PhantomData<(*mut u8, PhantomPinned)>,
+}
 
 #[link(name = "pam")]
 extern "C" {
     pub fn pam_get_data(
-        pamh: *const libc::c_void,
+        pamh: *const Handle,
         module_data_name: *const c_char,
         data: &mut *const libc::c_void,
     ) -> c_int;
 
     pub fn pam_set_data(
-        pamh: *mut libc::c_void,
+        pamh: *mut Handle,
         module_data_name: *const c_char,
         data: *const libc::c_void,
         cleanup: extern "C" fn(
@@ -23,27 +31,25 @@
     ) -> c_int;
 
     pub fn pam_get_item(
-        pamh: *const libc::c_void,
+        pamh: *const Handle,
         item_type: c_int,
         item: &mut *const libc::c_void,
     ) -> c_int;
 
-    pub fn pam_set_item(
-        pamh: *mut libc::c_void,
-        item_type: c_int,
-        item: *const libc::c_void,
-    ) -> c_int;
+    pub fn pam_set_item(pamh: *mut Handle, item_type: c_int, item: *const libc::c_void) -> c_int;
 
     pub fn pam_get_user(
-        pamh: *const libc::c_void,
+        pamh: *const Handle,
         user: &mut *const c_char,
         prompt: *const c_char,
     ) -> c_int;
 
     pub fn pam_get_authtok(
-        pamh: *const libc::c_void,
+        pamh: *const Handle,
         item_type: c_int,
         data: &mut *const c_char,
         prompt: *const c_char,
     ) -> c_int;
+
+    pub fn pam_end(pamh: *mut Handle, status: c_int) -> c_int;
 }