Mercurial > crates > nonstick
diff src/items.rs @ 72:47eb242a4f88
Fill out the PamHandle trait.
This updates the PamHandle trait to have methods for each Item,
and implements them on the LibPamHandle.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Wed, 04 Jun 2025 03:53:36 -0400 |
parents | a674799a5cd3 |
children |
line wrap: on
line diff
--- a/src/items.rs Tue Jun 03 21:54:58 2025 -0400 +++ b/src/items.rs Wed Jun 04 03:53:36 2025 -0400 @@ -3,12 +3,10 @@ use crate::constants::InvalidEnum; use num_derive::FromPrimitive; use num_traits::FromPrimitive; -use std::ffi::{c_int, CStr}; +use std::ffi::c_int; -/// Enum identifying what a `pam_get_item` return is. -/// -/// Generally, you shouldn’t have to worry about this, and instead -/// just use the various [Item] implementations. +/// Identifies what is being gotten or set with `pam_get_item` +/// or `pam_set_item`. #[derive(FromPrimitive)] #[repr(i32)] #[non_exhaustive] // because C could give us anything! @@ -53,68 +51,3 @@ val as Self } } - -/// A type that can be requested by [`PamHandle::get_item`](crate::LibPamHandle::get_item). -pub trait Item { - /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function. - /// This memory is owned by the PAM session. - type Raw; - - /// The [ItemType] corresponding to this Rust type. - fn type_id() -> ItemType; - - /// The function to convert from the pointer to the C-representation to this safer wrapper type. - /// - /// # Safety - /// - /// This function assumes the pointer is a valid pointer to a [Self::Raw] instance. - unsafe fn from_raw(raw: *const Self::Raw) -> Self; - - /// The function to convert from this wrapper type to a C-compatible pointer. - fn into_raw(self) -> *const Self::Raw; -} - -/// Macro to generate PAM [Item]s represented as [CStr]s. -macro_rules! cstr_item { - ($name:ident) => { - #[doc = "The [CStr] representation of the "] - #[doc = concat!("[`", stringify!($name), "`](ItemType::", stringify!($name), ")")] - #[doc = " [Item]."] - #[derive(Debug)] - pub struct $name<'s>(pub &'s CStr); - - impl<'s> std::ops::Deref for $name<'s> { - type Target = &'s CStr; - fn deref(&self) -> &Self::Target { - &self.0 - } - } - - impl<'s> Item for $name<'s> { - type Raw = libc::c_char; - - fn type_id() -> ItemType { - ItemType::$name - } - - unsafe fn from_raw(raw: *const Self::Raw) -> Self { - Self(std::ffi::CStr::from_ptr(raw)) - } - - fn into_raw(self) -> *const Self::Raw { - self.0.as_ptr() - } - } - }; -} - -// Conversation is not included here since it's special. - -cstr_item!(Service); -cstr_item!(User); -cstr_item!(Tty); -cstr_item!(RemoteHost); -cstr_item!(AuthTok); -cstr_item!(OldAuthTok); -cstr_item!(RemoteUser); -cstr_item!(UserPrompt);