Mercurial > crates > nonstick
annotate libpam-sys/src/ffi.rs @ 130:80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
This reimplements everything in nonstick on top of the new -sys crate.
We don't yet use libpam-sys's helpers for binary message payloads. Soon.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Tue, 01 Jul 2025 06:11:43 -0400 |
| parents | 5b2de52dd8b2 |
| children | a632a8874131 |
| rev | line source |
|---|---|
|
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
1 #![allow(non_camel_case_types)] |
|
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
2 |
| 127 | 3 use std::ffi::{c_char, c_int, c_void}; |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
4 use std::fmt; |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
5 use std::marker::{PhantomData, PhantomPinned}; |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
6 |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
7 /// A marker struct to make whatever it's in `!Sync`, `!Send`, and `!Unpin`. |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
8 #[derive(Default, PartialOrd, PartialEq, Ord, Eq)] |
|
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
9 #[repr(C)] |
|
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
10 struct ExtremelyUnsafe { |
|
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
11 _value: (), |
|
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
12 _marker: PhantomData<(PhantomPinned, *mut c_void)>, |
|
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
13 } |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
14 |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
15 impl fmt::Debug for ExtremelyUnsafe { |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
17 f.write_str("ExtremelyUnsafe") |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
18 } |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
19 } |
|
106
49d9e2b5c189
An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
20 |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
21 /// An opaque structure that PAM uses to communicate. |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
22 /// |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
23 /// This is only ever returned in pointer form and cannot be constructed. |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
24 #[repr(C)] |
| 127 | 25 pub struct pam_handle(ExtremelyUnsafe); |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
26 |
| 127 | 27 impl fmt::Debug for pam_handle { |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
29 write!(f, "PamHandle({self:p}") |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
30 } |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
31 } |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
32 |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
33 /// An opaque structure that is passed through PAM in a conversation. |
|
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
34 #[repr(C)] |
|
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
35 pub struct AppData(ExtremelyUnsafe); |
|
106
49d9e2b5c189
An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
36 |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
37 impl fmt::Debug for AppData { |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
39 write!(f, "AppData({self:p}") |
|
106
49d9e2b5c189
An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
40 } |
|
49d9e2b5c189
An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
41 } |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
42 |
| 127 | 43 /// Just an alias for the type of [`pam_conv::conv`]. |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
44 /// |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
45 /// For important details about the format of `messages`, |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
46 /// see the [`helpers`](crate::helpers) module. |
| 127 | 47 /// |
| 48 /// ```no_run | |
| 49 /// # use libpam_sys::{ConversationCallback, pam_conv}; | |
| 50 /// fn convo() -> ConversationCallback { | |
| 51 /// // ... | |
| 52 /// # unimplemented!() | |
| 53 /// } | |
| 54 /// let conv = pam_conv{conv: convo(), appdata_ptr: std::ptr::null_mut()}; | |
| 55 /// ``` | |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
56 pub type ConversationCallback = unsafe extern "C" fn( |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
57 num_msg: c_int, |
|
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
58 msg: *const *const pam_message, |
| 127 | 59 resp: *mut *mut pam_response, |
| 60 appdata: *mut AppData, | |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
61 ) -> c_int; |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
62 |
| 127 | 63 /// Alias for the callback to [`pam_set_data`](crate::pam_set_data). |
| 64 /// | |
| 65 /// ```no_run | |
| 66 /// # use std::ffi::CString; | |
| 67 /// use libpam_sys::{CleanupCallback, pam_set_data}; | |
| 68 /// # use libpam_sys::pam_handle; | |
| 69 /// # let handle: *mut pam_handle = std::ptr::null_mut(); | |
| 70 /// # let mut my_data = 100; | |
| 71 /// # let data_ptr = &mut my_data as *mut i32; | |
| 72 /// fn cleanup() -> CleanupCallback { | |
| 73 /// // ... | |
| 74 /// # unimplemented!() | |
| 75 /// } | |
| 76 /// let name = CString::new("name").unwrap(); | |
| 77 /// unsafe { | |
| 78 /// pam_set_data(handle, name.as_ptr().cast_mut(), data_ptr.cast(), cleanup()); | |
| 79 /// } | |
| 80 /// ``` | |
|
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
81 pub type CleanupCallback = |
|
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
82 unsafe extern "C" fn(pamh: *mut pam_handle, data: *mut c_void, pam_end_status: c_int); |
|
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
83 |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
84 /// Used by PAM to communicate between the module and the application. |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
85 #[repr(C)] |
|
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
86 pub struct pam_conv { |
| 127 | 87 pub conv: unsafe extern "C" fn( |
| 88 num_msg: c_int, | |
| 89 msg: *const *const pam_message, | |
| 90 resp: *mut *mut pam_response, | |
| 91 appdata: *mut AppData, | |
| 92 ) -> c_int, | |
| 93 pub appdata_ptr: *mut AppData, | |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
94 } |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
95 |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
96 /// A message sent into a PAM conversation. |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
97 #[repr(C)] |
|
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
98 pub struct pam_message { |
|
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
99 pub msg_style: c_int, |
| 127 | 100 pub msg: *const c_char, |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
101 } |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
102 |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
103 /// A response returned from a PAM conversation. |
|
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
104 #[repr(C)] |
|
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
105 pub struct pam_response { |
| 127 | 106 pub resp: *mut c_char, |
|
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
107 /// Completely unused. |
|
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
108 pub resp_retcode: c_int, |
|
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
109 } |
| 127 | 110 |
| 111 // These are the functions specified in X/SSO. Everybody exports them. | |
| 112 extern "C" { | |
| 113 /// Account validation. | |
| 114 pub fn pam_acct_mgmt(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
| 115 | |
| 116 /// Authenticate a user. | |
| 117 pub fn pam_authenticate(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
| 118 | |
| 119 // Nobody implements pam_authenticate_secondary. | |
| 120 | |
| 121 /// Manage authentication tokens. | |
| 122 pub fn pam_chauthtok(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
| 123 | |
| 124 /// Close an opened user session. | |
| 125 pub fn pam_close_session(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
| 126 | |
| 127 /// Ends the PAM transaction. | |
| 128 pub fn pam_end(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
| 129 | |
| 130 /// Gets module-specific data. PAM still owns the data. | |
| 131 pub fn pam_get_data( | |
| 132 pamh: *mut pam_handle, | |
| 133 module_data_name: *const c_char, | |
| 134 data: *mut *const c_void, | |
| 135 ) -> c_int; | |
| 136 | |
| 137 /// Gets an environment variable. You own the return value. | |
|
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
138 pub fn pam_getenv(pamh: *const pam_handle, name: *const c_char) -> *mut c_char; |
| 127 | 139 |
| 140 /// Gets all the environment variables. You own everything it points to. | |
|
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
141 pub fn pam_getenvlist(pamh: *const pam_handle) -> *mut *mut c_char; |
| 127 | 142 |
| 143 /// Get information about the transaction. | |
| 144 /// | |
| 145 /// The item is owned by PAM. | |
| 146 pub fn pam_get_item( | |
|
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
147 pamh: *const pam_handle, |
| 127 | 148 item_type: c_int, |
| 149 item: *mut *const c_void, | |
| 150 ) -> c_int; | |
| 151 | |
| 152 // Nobody implements pam_get_mapped_authtok. | |
| 153 // Nobody implements pam_get_mapped_username. | |
| 154 | |
| 155 /// Get the username. PAM owns it. | |
| 156 pub fn pam_get_user( | |
| 157 pamh: *mut pam_handle, | |
| 158 user: *mut *const c_char, | |
| 159 prompt: *const c_char, | |
| 160 ) -> c_int; | |
| 161 | |
| 162 /// Opens a user session. | |
| 163 pub fn pam_open_session(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
| 164 | |
| 165 /// Sets the value of an environment variable. `namevalue` is copied. | |
| 166 pub fn pam_putenv(pamh: *mut pam_handle, namevalue: *const c_char) -> c_int; | |
| 167 | |
| 168 /// Update or delete user credentials. | |
| 169 pub fn pam_setcred(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
| 170 | |
| 171 /// Set module-specific data. PAM will call `cleanup` when completed. | |
| 172 pub fn pam_set_data( | |
| 173 pamh: *mut pam_handle, | |
| 174 module_data_name: *const c_char, | |
| 175 data: *mut c_void, | |
| 176 cleanup: unsafe extern "C" fn( | |
| 177 pamh: *mut pam_handle, | |
| 178 data: *mut c_void, | |
| 179 pam_end_status: c_int, | |
| 180 ), | |
| 181 ) -> c_int; | |
| 182 | |
| 183 /// Set information about the transaction. The `item` is copied. | |
| 184 pub fn pam_set_item(pamh: *mut pam_handle, item_type: c_int, item: *const c_void) -> c_int; | |
| 185 | |
| 186 // Nobody implements pam_set_mapped_authtok. | |
| 187 // Nobody implements pam_set_mapped_username. | |
| 188 | |
| 189 // The pam_sm_whatever functions are prototypes for the functions that | |
| 190 // a PAM module should implement, not symbols provided by PAM. | |
| 191 | |
| 192 // Nobody implements pam_authenticate_secondary. | |
| 193 | |
| 194 /// Starts a PAM transaction. The `conv` may or may not be copied. | |
| 195 pub fn pam_start( | |
| 196 service: *const c_char, | |
| 197 user: *const c_char, | |
| 198 pam_conv: *mut pam_conv, | |
| 199 pamh: *mut *mut pam_handle, | |
| 200 ) -> c_int; | |
| 201 | |
| 202 /// Gets a statically-allocated error string. | |
| 203 /// | |
| 204 /// All implementations of PAM known to this library (Linux-PAM, OpenPAM, | |
| 205 /// and Sun) ignore `pamh` and will accept a null pointer. | |
| 206 pub fn pam_strerror(pamh: *const pam_handle, error_number: c_int) -> *mut c_char; | |
| 207 } | |
| 208 | |
| 209 // We use `_private_pam_impl_hack` because ctest loses its mind | |
| 210 // when it comes across the `cfg_pam_impl` macro. | |
| 211 // This is a custom cfg variable set in our build.rs. Don't do this; just use | |
| 212 // cfg_pam_impl. | |
|
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
213 #[cfg(any(_private_pam_impl_hack = "LinuxPam", _private_pam_impl_hack = "OpenPam"))] |
| 127 | 214 extern "C" { |
|
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
215 pub fn pam_get_authtok( |
|
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
216 pamh: *mut pam_handle, |
|
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
217 x: c_int, |
|
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
218 token: *mut *const c_char, |
|
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
219 prompt: *const c_char, |
|
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
129
diff
changeset
|
220 ) -> c_int; |
| 127 | 221 } |
