pam-ssh-agent 0.9.6

A PAM module that authenticates using the ssh-agent.
Documentation
use anyhow::{Result, anyhow};
use pam::items::{Service, User};
use pam::module::PamHandle;
use std::str::from_utf8;

/// This extension trait adds some extra methods to PamHandle
pub trait PamHandleExt {
    /// Fetch the PAM_USER value.
    fn get_calling_user(&self) -> Result<String>;

    /// Fetch the name of the current service, i.e. the software that uses pam for authentication
    /// using the PamHandle::get_item() method.
    fn get_service(&self) -> Result<String>;
}

macro_rules! get_item {
    ($name:ident, $type:ty) => {
        fn $name(&self) -> Result<String> {
            let service = self
                .get_item::<$type>()
                .unwrap()
                .ok_or(anyhow!("Could not get_item {}", stringify!($type)))?;
            Ok(from_utf8(service.0.to_bytes())?.to_string())
        }
    };
}

impl PamHandleExt for PamHandle {
    get_item!(get_calling_user, User);
    get_item!(get_service, Service);
}