comparison src/libpam/pam_ffi.rs @ 90:f6186e41399b

Miscellaneous fixes and cleanup: - Rename `get_user` to `username` and `get_authtok` to `authtok`. - Use pam_strerror for error messages. - Add library linkage to build.rs (it was missing???).
author Paul Fisher <paul@pfish.zone>
date Sat, 14 Jun 2025 09:30:16 -0400
parents dd3e9c4bcde3
children 5ddbcada30f2
comparison
equal deleted inserted replaced
89:dd3e9c4bcde3 90:f6186e41399b
1 //! The types that are directly represented in PAM function signatures. 1 //! The types that are directly represented in PAM function signatures.
2 2
3 #![allow(non_camel_case_types)] 3 #![allow(non_camel_case_types)]
4 4
5 use crate::libpam::memory::Immovable; 5 use crate::libpam::memory::Immovable;
6 use std::ffi::{c_int, c_uint, c_void}; 6 use std::ffi::{c_int, c_uint, c_void, CStr};
7 use std::marker::PhantomData; 7 use std::marker::PhantomData;
8 use std::ptr;
8 9
9 /// An opaque structure that a PAM handle points to. 10 /// An opaque structure that a PAM handle points to.
10 #[repr(C)] 11 #[repr(C)]
11 pub struct LibPamHandle { 12 pub struct LibPamHandle {
12 _data: (), 13 _data: (),
26 /// and [`TextAnswer`](crate::libpam::answer::TextAnswer). 27 /// and [`TextAnswer`](crate::libpam::answer::TextAnswer).
27 #[repr(C)] 28 #[repr(C)]
28 #[derive(Debug)] 29 #[derive(Debug)]
29 pub struct Answer { 30 pub struct Answer {
30 /// Pointer to the data returned in an answer. 31 /// Pointer to the data returned in an answer.
31 /// For most answers, this will be a [`CStr`](std::ffi::CStr), 32 /// For most answers, this will be a [`CStr`],
32 /// but for [`BinaryQAndA`](crate::conv::BinaryQAndA)s (a Linux-PAM extension), 33 /// but for [`BinaryQAndA`](crate::conv::BinaryQAndA)s (a Linux-PAM extension),
33 /// this will be [`CBinaryData`](crate::libpam::memory::CBinaryData). 34 /// this will be [`CBinaryData`](crate::libpam::memory::CBinaryData).
34 /// 35 ///
35 /// No matter what, this can be freed with a simple [`libc::free`]. 36 /// No matter what, this can be freed with a simple [`libc::free`].
36 pub data: *mut c_void, 37 pub data: *mut c_void,
51 pub struct Question { 52 pub struct Question {
52 /// The style of message to request. 53 /// The style of message to request.
53 pub style: c_uint, 54 pub style: c_uint,
54 /// A description of the data requested. 55 /// A description of the data requested.
55 /// 56 ///
56 /// For most requests, this will be an owned [`CStr`](std::ffi::CStr), 57 /// For most requests, this will be an owned [`CStr`],
57 /// but for requests with style `PAM_BINARY_PROMPT`, 58 /// but for requests with style `PAM_BINARY_PROMPT`,
58 /// this will be `CBinaryData` (a Linux-PAM extension). 59 /// this will be `CBinaryData` (a Linux-PAM extension).
59 pub data: *mut c_void, 60 pub data: *mut c_void,
60 pub _marker: Immovable, 61 pub _marker: Immovable,
61 } 62 }
88 pub appdata: *mut AppData, 89 pub appdata: *mut AppData,
89 pub life: PhantomData<&'a mut ()>, 90 pub life: PhantomData<&'a mut ()>,
90 pub _marker: Immovable, 91 pub _marker: Immovable,
91 } 92 }
92 93
94 /// Gets a string version of an error message.
95 pub fn strerror(code: c_uint) -> Option<&'static str> {
96 // SAFETY: Every single PAM implementation I can find (Linux-PAM, OpenPAM,
97 // Solaris, etc.) returns a static string and ignores the handle value.
98 let strerror = unsafe { pam_strerror(ptr::null_mut(), code as c_int) };
99 if strerror.is_null() {
100 None
101 } else {
102 unsafe { CStr::from_ptr(strerror) }.to_str().ok()
103 }
104 }
105
93 type pam_handle = LibPamHandle; 106 type pam_handle = LibPamHandle;
94 type pam_conv = LibPamConversation<'static>; 107 type pam_conv = LibPamConversation<'static>;
95 108
96 include!(concat!(env!("OUT_DIR"), "/bindings.rs")); 109 include!(concat!(env!("OUT_DIR"), "/bindings.rs"));