diff src/libpam/conversation.rs @ 141:a508a69c068a

Remove a lot of Results from functions. Many functions are documented to only return failing Results when given improper inputs or when there is a memory allocation failure (which can be verified by looking at the source). In cases where we know our input is correct, we don't need to check for memory allocation errors for the same reason that Rust doesn't do so when you, e.g., create a new Vec.
author Paul Fisher <paul@pfish.zone>
date Sat, 05 Jul 2025 17:16:56 -0400
parents 33b9622ed6d2
children 4b3a5095f68c
line wrap: on
line diff
--- a/src/libpam/conversation.rs	Sat Jul 05 17:11:33 2025 -0400
+++ b/src/libpam/conversation.rs	Sat Jul 05 17:16:56 2025 -0400
@@ -5,38 +5,35 @@
 use crate::libpam::question::Question;
 use crate::ErrorCode;
 use crate::Result;
-use libpam_sys::AppData;
+use libpam_sys::{AppData, ConversationCallback};
 use libpam_sys_helpers::memory::PtrPtrVec;
 use std::ffi::c_int;
 use std::iter;
-use std::marker::PhantomData;
 use std::ptr::NonNull;
 use std::result::Result as StdResult;
 
 /// The type used by PAM to call back into a conversation.
+///
+/// This has the same structure as a [`libpam_sys::pam_conv`].
+#[derive(Debug)]
 #[repr(C)]
-pub struct LibPamConversation<'a> {
-    pam_conv: libpam_sys::pam_conv,
-    /// Marker to associate the lifetime of this with the conversation
-    /// that was passed in.
-    pub life: PhantomData<&'a mut ()>,
+pub struct OwnedConversation<C: Conversation> {
+    callback: ConversationCallback,
+    conv: Box<C>,
 }
 
-impl LibPamConversation<'_> {
-    pub fn wrap<C: Conversation>(conv: &C) -> Self {
+impl<C: Conversation> OwnedConversation<C> {
+    pub fn new(conv: C) -> Self {
         Self {
-            pam_conv: libpam_sys::pam_conv {
-                conv: Self::wrapper_callback::<C>,
-                appdata_ptr: (conv as *const C).cast_mut().cast(),
-            },
-            life: PhantomData,
+            callback: Self::wrapper_callback,
+            conv: Box::new(conv),
         }
     }
 
     /// Passed as the conversation function into PAM for an owned handle.
     ///
     /// PAM calls this, we compute answers, then send them back.
-    unsafe extern "C" fn wrapper_callback<C: Conversation>(
+    unsafe extern "C" fn wrapper_callback(
         count: c_int,
         questions: *const *const libpam_sys::pam_message,
         answers: *mut *mut libpam_sys::pam_response,
@@ -71,7 +68,10 @@
     }
 }
 
-impl Conversation for LibPamConversation<'_> {
+/// A conversation owned by a PAM handle and lent to us.
+pub struct PamConv(libpam_sys::pam_conv);
+
+impl Conversation for PamConv {
     fn communicate(&self, messages: &[Exchange]) {
         let internal = || {
             let questions: Result<_> = messages.iter().map(Question::try_from).collect();
@@ -79,11 +79,11 @@
             let mut response_pointer = std::ptr::null_mut();
             // SAFETY: We're calling into PAM with valid everything.
             let result = unsafe {
-                (self.pam_conv.conv)(
+                (self.0.conv)(
                     messages.len() as c_int,
                     questions.as_ptr(),
                     &mut response_pointer,
-                    self.pam_conv.appdata_ptr,
+                    self.0.appdata_ptr,
                 )
             };
             ErrorCode::result_from(result)?;