Mercurial > crates > nonstick
diff src/libpam/handle.rs @ 144:56b559b7ecea
Big rename: separate concepts of Transaction from Handle.
- An application that uses PAM creates a Transaction.
- The Transaction has a Handle.
Currently, a module still get something called a "handle",
but that's probably going to change soon.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 06 Jul 2025 11:59:26 -0400 |
parents | ebb71a412b58 |
children | 1bc52025156b |
line wrap: on
line diff
--- a/src/libpam/handle.rs Sat Jul 05 22:12:46 2025 -0400 +++ b/src/libpam/handle.rs Sun Jul 06 11:59:26 2025 -0400 @@ -7,8 +7,7 @@ use crate::libpam::memory; use crate::logging::{Level, Location}; use crate::{ - guide, linklist, stdlinks, Conversation, EnvironMap, Flags, PamHandleApplication, - PamHandleModule, + guide, linklist, stdlinks, Conversation, EnvironMap, Flags, PamHandleModule, Transaction, }; use libpam_sys_helpers::constants; use num_enum::{IntoPrimitive, TryFromPrimitive}; @@ -20,9 +19,9 @@ use std::ptr::NonNull; /// An owned PAM handle. -pub struct OwnedLibPamHandle<C: Conversation> { +pub struct LibPamTransaction<C: Conversation> { /// The handle itself. - handle: ManuallyDrop<RawPamHandle>, + handle: ManuallyDrop<LibPamHandle>, /// The last return value from the handle. last_return: Cell<Result<()>>, /// If set, the Conversation that this PAM handle owns. @@ -36,12 +35,12 @@ } #[derive(Debug, PartialEq)] -pub struct HandleBuilder { +pub struct TransactionBuilder { service_name: OsString, username: Option<OsString>, } -impl HandleBuilder { +impl TransactionBuilder { /// Updates the service name. pub fn service_name(mut self, service_name: OsString) -> Self { self.service_name = service_name; @@ -55,12 +54,12 @@ self } /// Builds a PAM handle and starts the transaction. - pub fn build(self, conv: impl Conversation) -> Result<OwnedLibPamHandle<impl Conversation>> { - OwnedLibPamHandle::start(self.service_name, self.username, conv) + pub fn build(self, conv: impl Conversation) -> Result<LibPamTransaction<impl Conversation>> { + LibPamTransaction::start(self.service_name, self.username, conv) } } -impl<C: Conversation> OwnedLibPamHandle<C> { +impl<C: Conversation> LibPamTransaction<C> { /// Creates a builder to start a PAM transaction for the given service. /// /// The service name is what controls the steps and checks PAM goes through @@ -72,8 +71,8 @@ /// #[doc = stdlinks!(3 pam_start)] #[doc = guide!(adg: "adg-interface-by-app-expected.html#adg-pam_start")] - pub fn build_with_service(service_name: OsString) -> HandleBuilder { - HandleBuilder { + pub fn build_with_service(service_name: OsString) -> TransactionBuilder { + TransactionBuilder { service_name, username: None, } @@ -101,7 +100,7 @@ ErrorCode::result_from(result)?; let handle = NonNull::new(handle).ok_or(ErrorCode::BufferError)?; Ok(Self { - handle: ManuallyDrop::new(RawPamHandle(handle)), + handle: ManuallyDrop::new(LibPamHandle(handle)), last_return: Cell::new(Ok(())), conversation: conv, }) @@ -128,7 +127,7 @@ }; } -impl PamHandleApplication for RawPamHandle { +impl Transaction for LibPamHandle { wrap!(fn authenticate { pam_authenticate }); wrap!(fn account_management { pam_acct_mgmt }); wrap!(fn change_authtok { pam_chauthtok }); @@ -144,7 +143,7 @@ // pam_getenv - shared // pam_getenvlist - shared -impl<C: Conversation> Drop for OwnedLibPamHandle<C> { +impl<C: Conversation> Drop for LibPamTransaction<C> { /// Closes the PAM session on an owned PAM handle. /// /// This internally calls `pam_end` with the appropriate error code. @@ -205,7 +204,7 @@ result.as_ref().map(drop).map_err(|&e| e) } -impl<C: Conversation> PamShared for OwnedLibPamHandle<C> { +impl<C: Conversation> PamShared for LibPamTransaction<C> { delegate!(fn log(&self, level: Level, location: Location<'_>, entry: &str) -> ()); delegate!(fn environ(&self) -> impl EnvironMap); delegate!(fn environ_mut(&mut self) -> impl EnvironMapMut); @@ -237,14 +236,15 @@ /// An owned variation of a basic PAM handle. /// /// This is the most basic version of a wrapped PAM handle. It's mostly used -/// as the inside of the [`OwnedLibPamHandle`], but can also be used to "adopt" +/// as the inside of the [`LibPamTransaction`], but can also be used to "adopt" /// a PAM handle created by another library. /// /// If [`Self::end`] is not called, this will always call `pam_end` reporting /// successful completion. -pub struct RawPamHandle(NonNull<libpam_sys::pam_handle>); +#[repr(transparent)] +pub struct LibPamHandle(NonNull<libpam_sys::pam_handle>); -impl RawPamHandle { +impl LibPamHandle { /// Takes ownership of the pointer to the given PAM handle. /// /// **Do not use this just to get a reference to a PAM handle.** @@ -317,13 +317,13 @@ } } -impl Drop for RawPamHandle { +impl Drop for LibPamHandle { fn drop(&mut self) { unsafe { libpam_sys::pam_end(self.0.as_mut(), 0) }; } } -impl PamShared for RawPamHandle { +impl PamShared for LibPamHandle { #[cfg(any())] fn log(&self, level: Level, loc: Location<'_>, entry: &str) { let entry = match CString::new(entry).or_else(|_| CString::new(dbg!(entry))) { @@ -393,7 +393,7 @@ cstr_item!(set = set_old_authtok_item, item = ItemType::OldAuthTok); } -impl Conversation for RawPamHandle { +impl Conversation for LibPamHandle { fn communicate(&self, messages: &[Exchange]) { match self.conversation_item() { Ok(conv) => conv.communicate(messages), @@ -406,7 +406,7 @@ } } -impl PamHandleModule for RawPamHandle { +impl PamHandleModule for LibPamHandle { fn authtok(&mut self, prompt: Option<&OsStr>) -> Result<OsString> { self.get_authtok(prompt, ItemType::AuthTok) } @@ -430,7 +430,7 @@ } // Implementations of internal functions. -impl RawPamHandle { +impl LibPamHandle { #[cfg(any(pam_impl = "LinuxPam", pam_impl = "OpenPam"))] fn get_authtok(&mut self, prompt: Option<&OsStr>, item_type: ItemType) -> Result<OsString> { let prompt = memory::option_cstr_os(prompt);