comparison libpam-sys/src/structs.rs @ 125:2b255c92417b

Introduce base PAM functions; use the real X/SSO PAM header for tests.
author Paul Fisher <paul@pfish.zone>
date Mon, 30 Jun 2025 17:47:32 -0400
parents 476a22db8639
children
comparison
equal deleted inserted replaced
124:f469b8d9ad78 125:2b255c92417b
1 //! Structs and wrappers that PAM is made of.
2 #![allow(non_camel_case_types)]
3
1 use std::ffi::{c_int, c_void}; 4 use std::ffi::{c_int, c_void};
2 use std::fmt; 5 use std::fmt;
3 use std::marker::{PhantomData, PhantomPinned}; 6 use std::marker::{PhantomData, PhantomPinned};
4 7
5 /// A marker struct to make whatever it's in `!Sync`, `!Send`, and `!Unpin`. 8 /// A marker struct to make whatever it's in `!Sync`, `!Send`, and `!Unpin`.
6 #[derive(Default, PartialOrd, PartialEq, Ord, Eq)] 9 #[derive(Default, PartialOrd, PartialEq, Ord, Eq)]
7 #[repr(transparent)] 10 #[repr(C)]
8 struct ExtremelyUnsafe(PhantomData<(PhantomPinned, *mut c_void)>); 11 struct ExtremelyUnsafe {
12 _value: (),
13 _marker: PhantomData<(PhantomPinned, *mut c_void)>,
14 }
9 15
10 impl fmt::Debug for ExtremelyUnsafe { 16 impl fmt::Debug for ExtremelyUnsafe {
11 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12 f.write_str("ExtremelyUnsafe") 18 f.write_str("ExtremelyUnsafe")
13 } 19 }
15 21
16 /// An opaque structure that PAM uses to communicate. 22 /// An opaque structure that PAM uses to communicate.
17 /// 23 ///
18 /// This is only ever returned in pointer form and cannot be constructed. 24 /// This is only ever returned in pointer form and cannot be constructed.
19 #[repr(C)] 25 #[repr(C)]
20 pub struct PamHandle { 26 pub struct pam_handle_t(ExtremelyUnsafe);
21 _marker: ExtremelyUnsafe,
22 }
23 27
24 impl fmt::Debug for PamHandle { 28 impl fmt::Debug for pam_handle_t {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 write!(f, "PamHandle({self:p}") 30 write!(f, "PamHandle({self:p}")
27 } 31 }
28 } 32 }
29 33
30 /// An opaque structure that is passed through PAM in a conversation. 34 /// An opaque structure that is passed through PAM in a conversation.
31 pub struct AppData { 35 #[repr(C)]
32 _marker: ExtremelyUnsafe, 36 pub struct AppData(ExtremelyUnsafe);
33 }
34 37
35 impl fmt::Debug for AppData { 38 impl fmt::Debug for AppData {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 write!(f, "AppData({self:p}") 40 write!(f, "AppData({self:p}")
38 } 41 }
45 pub type ConversationCallback = unsafe extern "C" fn( 48 pub type ConversationCallback = unsafe extern "C" fn(
46 num_msg: c_int, 49 num_msg: c_int,
47 // This is a *const *const because accessing memory from a reference 50 // This is a *const *const because accessing memory from a reference
48 // outside its bounds is undefined behavior, and *messages is an array 51 // outside its bounds is undefined behavior, and *messages is an array
49 // in X/SSO PAM impls. 52 // in X/SSO PAM impls.
50 messages: *const *const Message, 53 msg: *const *const pam_message,
51 // This is a &mut *mut because the caller sets the pointer in `responses` 54 // This is a &mut *mut because the caller sets the pointer in `resp`
52 // but does not mess around outside its memory space. 55 // but does not mess around outside its memory space.
53 responses: &mut *mut Response, 56 resp: &mut *mut pam_response,
54 appdata: *const AppData, 57 appdata: *const AppData,
58 ) -> c_int;
59
60 /// Called to clean up data set using [`pam_set_data`](crate::pam_set_data).
61 pub type CleanupCallback = unsafe extern "C" fn(
62 pamh: *mut pam_handle_t,
63 data: *mut c_void,
64 pam_end_status: c_int,
55 ) -> c_int; 65 ) -> c_int;
56 66
57 /// Used by PAM to communicate between the module and the application. 67 /// Used by PAM to communicate between the module and the application.
58 #[repr(C)] 68 #[repr(C)]
59 pub struct Conversation { 69 pub struct pam_conv {
60 pub callback: ConversationCallback, 70 pub conv: ConversationCallback,
61 pub appdata: *const AppData, 71 pub appdata_ptr: *const AppData,
62 } 72 }
63 73
64 /// A message sent into a PAM conversation. 74 /// A message sent into a PAM conversation.
65 #[repr(C)] 75 #[repr(C)]
66 pub struct Message { 76 pub struct pam_message {
67 pub style: c_int, 77 pub msg_style: c_int,
68 pub data: *const c_void, 78 pub msg: *const c_void,
69 } 79 }
70 80
71 /// A response returned from a PAM conversation. 81 /// A response returned from a PAM conversation.
72 #[repr(C)] 82 #[repr(C)]
73 pub struct Response { 83 pub struct pam_response {
74 pub data: *mut c_void, 84 pub resp: *mut c_void,
75 pub _unused: c_int, 85 /// Completely unused.
86 pub resp_retcode: c_int,
76 } 87 }