comparison src/libpam/conversation.rs @ 98:b87100c5eed4

Start on environment variables, and make pointers nicer. This starts work on the PAM environment handling, and in so doing, introduces the CHeapBox and CHeapString structs. These are analogous to Box and CString, but they're located on the C heap rather than being Rust-managed memory. This is because environment variables deal with even more pointers and it turns out we can lose a lot of manual freeing using homemade smart pointers.
author Paul Fisher <paul@pfish.zone>
date Tue, 24 Jun 2025 04:25:25 -0400
parents efe2f5f8b5b2
children 3f11b8d30f63
comparison
equal deleted inserted replaced
97:efe2f5f8b5b2 98:b87100c5eed4
10 use crate::ErrorCode; 10 use crate::ErrorCode;
11 use crate::Result; 11 use crate::Result;
12 use std::ffi::c_int; 12 use std::ffi::c_int;
13 use std::iter; 13 use std::iter;
14 use std::marker::PhantomData; 14 use std::marker::PhantomData;
15 use std::ptr::NonNull;
15 use std::result::Result as StdResult; 16 use std::result::Result as StdResult;
16 17
17 impl LibPamConversation<'_> { 18 impl LibPamConversation<'_> {
18 pub fn wrap<C: Conversation>(conv: &C) -> Self { 19 pub fn wrap<C: Conversation>(conv: &C) -> Self {
19 Self { 20 Self {
53 // TODO: Do we want to log something here? 54 // TODO: Do we want to log something here?
54 conv.communicate(&borrowed?); 55 conv.communicate(&borrowed?);
55 56
56 // Send our answers back. 57 // Send our answers back.
57 let owned = Answers::build(messages).map_err(|_| ErrorCode::ConversationError)?; 58 let owned = Answers::build(messages).map_err(|_| ErrorCode::ConversationError)?;
58 *answers_ptr = owned.into_ptr(); 59 *answers_ptr = owned.into_ptr().as_ptr();
59 Ok(()) 60 Ok(())
60 }; 61 };
61 ErrorCode::result_to_c(internal()) 62 ErrorCode::result_to_c(internal())
62 } 63 }
63 } 64 }
79 ErrorCode::result_from(result)?; 80 ErrorCode::result_from(result)?;
80 // SAFETY: This is a pointer we just got back from PAM. 81 // SAFETY: This is a pointer we just got back from PAM.
81 // We have to trust that the responses from PAM match up 82 // We have to trust that the responses from PAM match up
82 // with the questions we sent. 83 // with the questions we sent.
83 unsafe { 84 unsafe {
85 let response_pointer =
86 NonNull::new(response_pointer).ok_or(ErrorCode::ConversationError)?;
84 let mut owned_responses = Answers::from_c_heap(response_pointer, messages.len()); 87 let mut owned_responses = Answers::from_c_heap(response_pointer, messages.len());
85 for (msg, response) in iter::zip(messages, owned_responses.iter_mut()) { 88 for (msg, response) in iter::zip(messages, owned_responses.iter_mut()) {
86 convert(msg, response); 89 convert(msg, response);
87 } 90 }
88 }; 91 };