annotate src/libpam/pam_ffi.rs @ 89:dd3e9c4bcde3

Simplify memory management in Questions. When we're sending Questions to the client, we don't need them to be C-managed, we just need the pointers going to the right place. This replaces a bunch of Question management cruft with Vecs and Boxes.
author Paul Fisher <paul@pfish.zone>
date Fri, 13 Jun 2025 05:22:48 -0400
parents 05291b601f0a
children f6186e41399b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 //! The types that are directly represented in PAM function signatures.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
3 #![allow(non_camel_case_types)]
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5 use crate::libpam::memory::Immovable;
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 81
diff changeset
6 use std::ffi::{c_int, c_uint, c_void};
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7 use std::marker::PhantomData;
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9 /// An opaque structure that a PAM handle points to.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 #[repr(C)]
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 pub struct LibPamHandle {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12 _data: (),
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13 _marker: Immovable,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 /// An opaque structure that is passed through PAM in a conversation.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 #[repr(C)]
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 pub struct AppData {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 _data: (),
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 _marker: Immovable,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 /// Generic version of answer data.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 ///
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 /// This has the same structure as [`BinaryAnswer`](crate::libpam::answer::BinaryAnswer)
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 /// and [`TextAnswer`](crate::libpam::answer::TextAnswer).
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 #[repr(C)]
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 #[derive(Debug)]
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 pub struct Answer {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 /// Pointer to the data returned in an answer.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
31 /// For most answers, this will be a [`CStr`](std::ffi::CStr),
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32 /// but for [`BinaryQAndA`](crate::conv::BinaryQAndA)s (a Linux-PAM extension),
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
33 /// this will be [`CBinaryData`](crate::libpam::memory::CBinaryData).
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
34 ///
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
35 /// No matter what, this can be freed with a simple [`libc::free`].
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
36 pub data: *mut c_void,
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
37 /// Unused. Just here for the padding.
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
38 return_code: c_int,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
39 _marker: Immovable,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
40 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
41
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
42 /// A question sent by PAM or a module to an application.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 ///
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
44 /// PAM refers to this as a "message", but we call it a question
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
45 /// to avoid confusion with [`Message`](crate::conv::Message).
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 ///
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 /// This question, and its internal data, is owned by its creator
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 /// (either the module or PAM itself).
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
49 #[repr(C)]
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
50 #[derive(Debug)]
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 pub struct Question {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 /// The style of message to request.
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 81
diff changeset
53 pub style: c_uint,
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 /// A description of the data requested.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 ///
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
56 /// For most requests, this will be an owned [`CStr`](std::ffi::CStr),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
57 /// but for requests with style `PAM_BINARY_PROMPT`,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
58 /// this will be `CBinaryData` (a Linux-PAM extension).
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 pub data: *mut c_void,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 pub _marker: Immovable,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 /// The callback that PAM uses to get information in a conversation.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 ///
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
65 /// - `num_msg` is the number of messages in the `questions` array.
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 /// - `questions` is a pointer to the [`Question`]s being sent to the user.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 /// For information about its structure,
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
68 /// see [`QuestionsTrait`](super::question::QuestionsTrait).
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
69 /// - `answers` is a pointer to an array of [`Answer`]s,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 /// which PAM sets in response to a module's request.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
71 /// This is an array of structs, not an array of pointers to a struct.
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
72 /// There must always be exactly as many `answers` as `num_msg`.
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
73 /// - `appdata` is the `appdata` field of the [`LibPamConversation`].
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
74 pub type ConversationCallback = unsafe extern "C" fn(
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
75 num_msg: c_int,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
76 questions: *const *const Question,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
77 answers: *mut *mut Answer,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
78 appdata: *mut AppData,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
79 ) -> c_int;
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
81 /// The type used by PAM to call back into a conversation.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
82 #[repr(C)]
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
83 pub struct LibPamConversation<'a> {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84 /// The function that is called to get information from the user.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 pub callback: ConversationCallback,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 /// The pointer that will be passed as the last parameter
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
87 /// to the conversation callback.
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
88 pub appdata: *mut AppData,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
89 pub life: PhantomData<&'a mut ()>,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
90 pub _marker: Immovable,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
91 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
92
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
93 type pam_handle = LibPamHandle;
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
94 type pam_conv = LibPamConversation<'static>;
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
95
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
96 include!(concat!(env!("OUT_DIR"), "/bindings.rs"));