comparison src/pam_ffi/mod.rs @ 72:47eb242a4f88

Fill out the PamHandle trait. This updates the PamHandle trait to have methods for each Item, and implements them on the LibPamHandle.
author Paul Fisher <paul@pfish.zone>
date Wed, 04 Jun 2025 03:53:36 -0400
parents 58f9d2a4df38
children ac6881304c78
comparison
equal deleted inserted replaced
71:58f9d2a4df38 72:47eb242a4f88
3 //! This includes the functions provided by PAM and the data structures 3 //! This includes the functions provided by PAM and the data structures
4 //! used by PAM, as well as a few low-level abstractions for dealing with 4 //! used by PAM, as well as a few low-level abstractions for dealing with
5 //! those data structures. 5 //! those data structures.
6 //! 6 //!
7 //! Everything in here is hazmat. 7 //! Everything in here is hazmat.
8 //! 8 //!
9 9
10 #![allow(dead_code)] 10 #![allow(dead_code)]
11 11
12 pub mod memory; 12 pub mod memory;
13 mod message; 13 mod message;
14 mod response; 14 mod response;
15 15
16 use crate::pam_ffi::memory::Immovable; 16 use crate::pam_ffi::memory::Immovable;
17 use crate::pam_ffi::message::OwnedMessages; 17 use crate::pam_ffi::message::OwnedMessages;
18 #[doc(inline)]
18 pub use message::Message; 19 pub use message::Message;
20 #[doc(inline)]
19 pub use response::RawResponse; 21 pub use response::RawResponse;
20 use std::ffi::{c_char, c_int, c_void}; 22 use std::ffi::{c_char, c_int, c_void};
21 23
22 /// An opaque structure that a PAM handle points to. 24 /// An opaque structure that a PAM handle points to.
23 #[repr(C)] 25 #[repr(C)]
24 pub struct Handle { 26 pub struct LibPamHandle {
25 _data: (), 27 _data: (),
26 _marker: Immovable, 28 _marker: Immovable,
27 } 29 }
28 30
29 /// An opaque structure that is passed through PAM in a conversation. 31 /// An opaque structure that is passed through PAM in a conversation.
41 /// [`OwnedMessages`](super::OwnedMessages). 43 /// [`OwnedMessages`](super::OwnedMessages).
42 /// - `responses` is a pointer to an array of [`RawResponse`]s, 44 /// - `responses` is a pointer to an array of [`RawResponse`]s,
43 /// which PAM sets in response to a module's request. 45 /// which PAM sets in response to a module's request.
44 /// This is an array of structs, not an array of pointers to a struct. 46 /// This is an array of structs, not an array of pointers to a struct.
45 /// There should always be exactly as many `responses` as `num_msg`. 47 /// There should always be exactly as many `responses` as `num_msg`.
46 /// - `appdata` is the `appdata` field of the [`Conversation`] we were passed. 48 /// - `appdata` is the `appdata` field of the [`LibPamConversation`] we were passed.
47 pub type ConversationCallback = extern "C" fn( 49 pub type ConversationCallback = extern "C" fn(
48 num_msg: c_int, 50 num_msg: c_int,
49 messages: &OwnedMessages, 51 messages: &OwnedMessages,
50 responses: &mut *mut RawResponse, 52 responses: &mut *mut RawResponse,
51 appdata: *const AppData, 53 appdata: *const AppData,
52 ) -> c_int; 54 ) -> c_int;
53 55
54 /// A callback and the associated [`AppData`] pointer that needs to be passed back to it. 56 /// The type used by libpam to call back into a conversation.
55 #[repr(C)] 57 #[repr(C)]
56 pub struct Conversation { 58 pub struct LibPamConversation {
59 /// The function that is called to get information from the user.
57 callback: ConversationCallback, 60 callback: ConversationCallback,
61 /// The pointer that will be passed as the last parameter
62 /// to the conversation callback.
58 appdata: *const AppData, 63 appdata: *const AppData,
59 } 64 }
60 65
61 #[link(name = "pam")] 66 #[link(name = "pam")]
62 extern "C" { 67 extern "C" {
63 pub fn pam_get_data( 68 pub fn pam_get_data(
64 pamh: *const Handle, 69 pamh: *const LibPamHandle,
65 module_data_name: *const c_char, 70 module_data_name: *const c_char,
66 data: &mut *const c_void, 71 data: &mut *const c_void,
67 ) -> c_int; 72 ) -> c_int;
68 73
69 pub fn pam_set_data( 74 pub fn pam_set_data(
70 pamh: *mut Handle, 75 pamh: *mut LibPamHandle,
71 module_data_name: *const c_char, 76 module_data_name: *const c_char,
72 data: *const c_void, 77 data: *const c_void,
73 cleanup: extern "C" fn(pamh: *const c_void, data: *mut c_void, error_status: c_int), 78 cleanup: extern "C" fn(pamh: *const c_void, data: *mut c_void, error_status: c_int),
74 ) -> c_int; 79 ) -> c_int;
75 80
76 pub fn pam_get_item(pamh: *const Handle, item_type: c_int, item: &mut *const c_void) -> c_int; 81 pub fn pam_get_item(
82 pamh: *mut LibPamHandle,
83 item_type: c_int,
84 item: &mut *const c_void,
85 ) -> c_int;
77 86
78 pub fn pam_set_item(pamh: *mut Handle, item_type: c_int, item: *const c_void) -> c_int; 87 pub fn pam_set_item(pamh: *mut LibPamHandle, item_type: c_int, item: *const c_void) -> c_int;
79 88
80 pub fn pam_get_user( 89 pub fn pam_get_user(
81 pamh: *const Handle, 90 pamh: *mut LibPamHandle,
82 user: &mut *const c_char, 91 user: &mut *const c_char,
83 prompt: *const c_char, 92 prompt: *const c_char,
84 ) -> c_int; 93 ) -> c_int;
85 94
86 pub fn pam_get_authtok( 95 pub fn pam_get_authtok(
87 pamh: *const Handle, 96 pamh: *mut LibPamHandle,
88 item_type: c_int, 97 item_type: c_int,
89 data: &mut *const c_char, 98 data: &mut *const c_char,
90 prompt: *const c_char, 99 prompt: *const c_char,
91 ) -> c_int; 100 ) -> c_int;
92 101
93 pub fn pam_end(pamh: *mut Handle, status: c_int) -> c_int; 102 pub fn pam_end(pamh: *mut LibPamHandle, status: c_int) -> c_int;
94 } 103 }