use anyhow::{anyhow, Result};
use pam::items::{Service, User};
use pam::module::PamHandle;
pub trait PamHandleExt {
fn get_calling_user(&self) -> Result<String>;
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(String::from_utf8(service.0.to_bytes().to_vec())?.into())
}
};
}
impl PamHandleExt for PamHandle {
get_item!(get_calling_user, User);
get_item!(get_service, Service);
}