Mercurial > crates > nonstick
comparison src/libpam/handle.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 | 5aa1a010f1e8 |
children | 5ddbcada30f2 |
comparison
equal
deleted
inserted
replaced
89:dd3e9c4bcde3 | 90:f6186e41399b |
---|---|
6 use crate::libpam::{memory, pam_ffi}; | 6 use crate::libpam::{memory, pam_ffi}; |
7 use crate::{Conversation, PamHandleModule}; | 7 use crate::{Conversation, PamHandleModule}; |
8 use num_enum::{IntoPrimitive, TryFromPrimitive}; | 8 use num_enum::{IntoPrimitive, TryFromPrimitive}; |
9 use std::cell::Cell; | 9 use std::cell::Cell; |
10 use std::ffi::{c_char, c_int}; | 10 use std::ffi::{c_char, c_int}; |
11 use std::marker::PhantomData; | |
11 use std::ops::{Deref, DerefMut}; | 12 use std::ops::{Deref, DerefMut}; |
12 use std::ptr; | 13 use std::ptr; |
13 | 14 |
14 struct HandleWrap(*mut LibPamHandle); | 15 struct HandleWrap(*mut LibPamHandle); |
15 | 16 |
25 unsafe { &mut *self.0 } | 26 unsafe { &mut *self.0 } |
26 } | 27 } |
27 } | 28 } |
28 | 29 |
29 /// An owned PAM handle. | 30 /// An owned PAM handle. |
30 pub struct OwnedLibPamHandle { | 31 pub struct OwnedLibPamHandle<'a> { |
31 handle: HandleWrap, | 32 handle: HandleWrap, |
32 last_return: Cell<Result<()>>, | 33 last_return: Cell<Result<()>>, |
34 _conversation_lifetime: PhantomData<&'a mut ()>, | |
33 } | 35 } |
34 | 36 |
35 // TODO: pam_authenticate - app | 37 // TODO: pam_authenticate - app |
36 // pam_setcred - app | 38 // pam_setcred - app |
37 // pam_acct_mgmt - app | 39 // pam_acct_mgmt - app |
40 // pam_close_session - app | 42 // pam_close_session - app |
41 // pam_putenv - shared | 43 // pam_putenv - shared |
42 // pam_getenv - shared | 44 // pam_getenv - shared |
43 // pam_getenvlist - shared | 45 // pam_getenvlist - shared |
44 | 46 |
45 impl Drop for OwnedLibPamHandle { | 47 impl Drop for OwnedLibPamHandle<'_> { |
46 /// Closes the PAM session on an owned PAM handle. | 48 /// Closes the PAM session on an owned PAM handle. |
47 /// | 49 /// |
48 /// See the [`pam_end` manual page][man] for more information. | 50 /// See the [`pam_end` manual page][man] for more information. |
49 /// | 51 /// |
50 /// [man]: https://www.man7.org/linux/man-pages/man3/pam_end.3.html | 52 /// [man]: https://www.man7.org/linux/man-pages/man3/pam_end.3.html |
70 } | 72 } |
71 }; | 73 }; |
72 } | 74 } |
73 | 75 |
74 impl PamShared for LibPamHandle { | 76 impl PamShared for LibPamHandle { |
75 fn get_user(&mut self, prompt: Option<&str>) -> Result<&str> { | 77 fn username(&mut self, prompt: Option<&str>) -> Result<&str> { |
76 let prompt = memory::option_cstr(prompt)?; | 78 let prompt = memory::option_cstr(prompt)?; |
77 let mut output: *const c_char = ptr::null(); | 79 let mut output: *const c_char = ptr::null(); |
78 let ret = unsafe { | 80 let ret = unsafe { |
79 pam_ffi::pam_get_user(self, &mut output, memory::prompt_ptr(prompt.as_ref())) | 81 pam_ffi::pam_get_user(self, &mut output, memory::prompt_ptr(prompt.as_ref())) |
80 }; | 82 }; |
112 } | 114 } |
113 } | 115 } |
114 } | 116 } |
115 | 117 |
116 impl PamHandleModule for LibPamHandle { | 118 impl PamHandleModule for LibPamHandle { |
117 fn get_authtok(&mut self, prompt: Option<&str>) -> Result<&str> { | 119 fn authtok(&mut self, prompt: Option<&str>) -> Result<&str> { |
118 let prompt = memory::option_cstr(prompt)?; | 120 let prompt = memory::option_cstr(prompt)?; |
119 let mut output: *const c_char = ptr::null_mut(); | 121 let mut output: *const c_char = ptr::null_mut(); |
120 // SAFETY: We're calling this with known-good values. | 122 // SAFETY: We're calling this with known-good values. |
121 let res = unsafe { | 123 let res = unsafe { |
122 pam_ffi::pam_get_authtok( | 124 pam_ffi::pam_get_authtok( |
219 | 221 |
220 fn split<T>(result: &Result<T>) -> Result<()> { | 222 fn split<T>(result: &Result<T>) -> Result<()> { |
221 result.as_ref().map(drop).map_err(|&e| e) | 223 result.as_ref().map(drop).map_err(|&e| e) |
222 } | 224 } |
223 | 225 |
224 impl PamShared for OwnedLibPamHandle { | 226 impl PamShared for OwnedLibPamHandle<'_> { |
225 delegate!(fn get_user(&mut self, prompt: Option<&str>) -> Result<&str>); | 227 delegate!(fn username(&mut self, prompt: Option<&str>) -> Result<&str>); |
226 delegate!(get = user_item, set = set_user_item); | 228 delegate!(get = user_item, set = set_user_item); |
227 delegate!(get = service, set = set_service); | 229 delegate!(get = service, set = set_service); |
228 delegate!(get = user_prompt, set = set_user_prompt); | 230 delegate!(get = user_prompt, set = set_user_prompt); |
229 delegate!(get = tty_name, set = set_tty_name); | 231 delegate!(get = tty_name, set = set_tty_name); |
230 delegate!(get = remote_user, set = set_remote_user); | 232 delegate!(get = remote_user, set = set_remote_user); |