Mercurial > crates > nonstick
annotate libpam-sys/src/ffi.rs @ 127:c77846f3a979
GET CTEST WORKING.
This will verify that the functions we're exporting are correct.
It has been a nightmare.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Mon, 30 Jun 2025 22:56:26 -0400 |
parents | libpam-sys/src/structs.rs@2b255c92417b |
children |
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 /// ``` | |
125
2b255c92417b
Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents:
119
diff
changeset
|
81 pub type CleanupCallback = unsafe extern "C" fn( |
127 | 82 pamh: *mut pam_handle, |
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 data: *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
|
84 pam_end_status: c_int, |
127 | 85 ); |
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 |
118
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
87 /// 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
|
88 #[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
|
89 pub struct pam_conv { |
127 | 90 pub conv: unsafe extern "C" fn( |
91 num_msg: c_int, | |
92 msg: *const *const pam_message, | |
93 resp: *mut *mut pam_response, | |
94 appdata: *mut AppData, | |
95 ) -> c_int, | |
96 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
|
97 } |
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
98 |
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
99 /// 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
|
100 #[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
|
101 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
|
102 pub msg_style: c_int, |
127 | 103 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
|
104 } |
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
105 |
39760dfc9b3b
Detect PAM library based only on system lib; rename minimal lib to XSso.
Paul Fisher <paul@pfish.zone>
parents:
117
diff
changeset
|
106 /// 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
|
107 #[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
|
108 pub struct pam_response { |
127 | 109 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
|
110 /// 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
|
111 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
|
112 } |
127 | 113 |
114 | |
115 // These are the functions specified in X/SSO. Everybody exports them. | |
116 extern "C" { | |
117 /// Account validation. | |
118 pub fn pam_acct_mgmt(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
119 | |
120 /// Authenticate a user. | |
121 pub fn pam_authenticate(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
122 | |
123 // Nobody implements pam_authenticate_secondary. | |
124 | |
125 /// Manage authentication tokens. | |
126 pub fn pam_chauthtok(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
127 | |
128 /// Close an opened user session. | |
129 pub fn pam_close_session(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
130 | |
131 /// Ends the PAM transaction. | |
132 pub fn pam_end(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
133 | |
134 /// Gets module-specific data. PAM still owns the data. | |
135 pub fn pam_get_data( | |
136 pamh: *mut pam_handle, | |
137 module_data_name: *const c_char, | |
138 data: *mut *const c_void, | |
139 ) -> c_int; | |
140 | |
141 /// Gets an environment variable. You own the return value. | |
142 pub fn pam_getenv(pamh: *mut pam_handle, name: *const c_char) -> *mut c_char; | |
143 | |
144 /// Gets all the environment variables. You own everything it points to. | |
145 pub fn pam_getenvlist(pamh: *mut pam_handle) -> *mut *mut c_char; | |
146 | |
147 /// Get information about the transaction. | |
148 /// | |
149 /// The item is owned by PAM. | |
150 pub fn pam_get_item( | |
151 pamh: *mut pam_handle, | |
152 item_type: c_int, | |
153 item: *mut *const c_void, | |
154 ) -> c_int; | |
155 | |
156 // Nobody implements pam_get_mapped_authtok. | |
157 // Nobody implements pam_get_mapped_username. | |
158 | |
159 /// Get the username. PAM owns it. | |
160 pub fn pam_get_user( | |
161 pamh: *mut pam_handle, | |
162 user: *mut *const c_char, | |
163 prompt: *const c_char, | |
164 ) -> c_int; | |
165 | |
166 /// Opens a user session. | |
167 pub fn pam_open_session(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
168 | |
169 /// Sets the value of an environment variable. `namevalue` is copied. | |
170 pub fn pam_putenv(pamh: *mut pam_handle, namevalue: *const c_char) -> c_int; | |
171 | |
172 /// Update or delete user credentials. | |
173 pub fn pam_setcred(pamh: *mut pam_handle, flags: c_int) -> c_int; | |
174 | |
175 /// Set module-specific data. PAM will call `cleanup` when completed. | |
176 pub fn pam_set_data( | |
177 pamh: *mut pam_handle, | |
178 module_data_name: *const c_char, | |
179 data: *mut c_void, | |
180 cleanup: unsafe extern "C" fn( | |
181 pamh: *mut pam_handle, | |
182 data: *mut c_void, | |
183 pam_end_status: c_int, | |
184 ), | |
185 ) -> c_int; | |
186 | |
187 /// Set information about the transaction. The `item` is copied. | |
188 pub fn pam_set_item(pamh: *mut pam_handle, item_type: c_int, item: *const c_void) -> c_int; | |
189 | |
190 // Nobody implements pam_set_mapped_authtok. | |
191 // Nobody implements pam_set_mapped_username. | |
192 | |
193 // The pam_sm_whatever functions are prototypes for the functions that | |
194 // a PAM module should implement, not symbols provided by PAM. | |
195 | |
196 // Nobody implements pam_authenticate_secondary. | |
197 | |
198 /// Starts a PAM transaction. The `conv` may or may not be copied. | |
199 pub fn pam_start( | |
200 service: *const c_char, | |
201 user: *const c_char, | |
202 pam_conv: *mut pam_conv, | |
203 pamh: *mut *mut pam_handle, | |
204 ) -> c_int; | |
205 | |
206 /// Gets a statically-allocated error string. | |
207 /// | |
208 /// All implementations of PAM known to this library (Linux-PAM, OpenPAM, | |
209 /// and Sun) ignore `pamh` and will accept a null pointer. | |
210 pub fn pam_strerror(pamh: *const pam_handle, error_number: c_int) -> *mut c_char; | |
211 } | |
212 | |
213 // We use `_private_pam_impl_hack` because ctest loses its mind | |
214 // when it comes across the `cfg_pam_impl` macro. | |
215 // This is a custom cfg variable set in our build.rs. Don't do this; just use | |
216 // cfg_pam_impl. | |
217 #[cfg(_private_pam_impl_hack = "LinuxPam")] | |
218 extern "C" { | |
219 pub fn pam_get_authtok(pamh: *mut pam_handle, x: c_int, token: *mut *const c_char, prompt: *const c_char) -> c_int; | |
220 } | |
221 | |
222 | |
223 // int (*)(struct pam_handle *, char *, void *, void (*)(struct pam_handle *, void *, int)) | |
224 // int (*)(struct pam_handle *, char *, void *, int (*)(struct pam_handle *, void *, int)) |