view src/items.rs @ 61:5eecd797fc69

Bump version to v0.0.5.
author Paul Fisher <paul@pfish.zone>
date Wed, 21 May 2025 19:01:17 -0400
parents 05cc2c27334f
children d83623951070
line wrap: on
line source

//! Things that can be gotten with the `pam_get_item` function.

use crate::constants::InvalidEnum;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::ffi::{c_int, CStr};

/// 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.
#[derive(FromPrimitive)]
#[repr(i32)]
#[non_exhaustive] // because C could give us anything!
pub enum ItemType {
    /// The PAM service name.
    Service = 1,
    /// The user's login name.
    User = 2,
    /// The TTY name.
    Tty = 3,
    /// The remote host (if applicable).
    RemoteHost = 4,
    /// The [crate::Conversation] structure.
    Conversation = 5,
    /// The authentication token (password).
    AuthTok = 6,
    /// The old authentication token (when changing passwords).
    OldAuthTok = 7,
    /// The remote user's name.
    RemoteUser = 8,
    /// The prompt shown when requesting a username.
    UserPrompt = 9,
    /// App-supplied function to override failure delays.
    FailDelay = 10,
    /// X display name.
    XDisplay = 11,
    /// X server authentication data.
    XAuthData = 12,
    /// The type of `pam_get_authtok`.
    AuthTokType = 13,
}

impl TryFrom<c_int> for ItemType {
    type Error = InvalidEnum<Self>;
    fn try_from(value: c_int) -> Result<Self, Self::Error> {
        Self::from_i32(value).ok_or(value.into())
    }
}

impl From<ItemType> for c_int {
    fn from(val: ItemType) -> Self {
        val as Self
    }
}

/// A type that can be requested by [crate::PamHandle::get_item].
pub trait Item {
    /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function.
    type Raw;

    /// The `ItemType` for this 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_rules! cstr_item {
    ($name:ident) => {
        #[doc = concat!("The [ItemType::", stringify!($name), "]")]
        #[doc = " item, represented as a [CStr]."]
        #[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);