view src/libpam/conversation.rs @ 105:13b4d2a19674

Support Rust v1.75.0. This is the version included in Ubuntu 24.04 LTS and Debian Trixie, so it's old enough to have wide penetration without being too old to get new features (Debian Stable, I love you but v1.63 is just not going to work out).
author Paul Fisher <paul@pfish.zone>
date Thu, 26 Jun 2025 00:48:51 -0400
parents 94b51fa4f797
children
line wrap: on
line source

use crate::conv::{BinaryQAndA, RadioQAndA};
use crate::conv::{Conversation, ErrorMsg, InfoMsg, MaskedQAndA, Message, QAndA};
use crate::libpam::answer::BinaryAnswer;
use crate::libpam::answer::{Answer, Answers, TextAnswer};
use crate::libpam::memory::CBinaryData;
use crate::libpam::pam_ffi::AppData;
pub use crate::libpam::pam_ffi::LibPamConversation;
use crate::libpam::question::QuestionsTrait;
use crate::libpam::question::{Question, Questions};
use crate::ErrorCode;
use crate::Result;
use std::ffi::c_int;
use std::iter;
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::result::Result as StdResult;

impl LibPamConversation<'_> {
    pub fn wrap<C: Conversation>(conv: &C) -> Self {
        Self {
            callback: Self::wrapper_callback::<C>,
            appdata: (conv as *const C).cast(),
            life: PhantomData,
        }
    }

    /// Passed as the conversation function into PAM for an owned handle.
    ///
    /// PAM calls this, we compute answers, then send them back.
    unsafe extern "C" fn wrapper_callback<C: Conversation>(
        count: c_int,
        questions: *const *const Question,
        answers: *mut *mut Answer,
        me: *const AppData,
    ) -> c_int {
        let internal = || {
            // Collect all our pointers
            let conv = me
                .cast::<C>()
                .as_ref()
                .ok_or(ErrorCode::ConversationError)?;
            let indirect = Questions::borrow_ptr(questions, count as usize);
            let answers_ptr = answers.as_mut().ok_or(ErrorCode::ConversationError)?;

            // Build our owned list of Q&As from the questions we've been asked
            let messages: Vec<OwnedMessage> = indirect
                .map(TryInto::try_into)
                .collect::<Result<_>>()
                .map_err(|_| ErrorCode::ConversationError)?;
            // Borrow all those Q&As and ask them.
            // If we got an invalid message type, bail before sending.
            let borrowed: Result<Vec<_>> = messages.iter().map(Message::try_from).collect();
            // TODO: Do we want to log something here?
            conv.communicate(&borrowed?);

            // Send our answers back.
            let owned = Answers::build(messages)?;
            *answers_ptr = owned.into_ptr().as_ptr();
            Ok(())
        };
        ErrorCode::result_to_c(internal())
    }
}

impl Conversation for LibPamConversation<'_> {
    fn communicate(&self, messages: &[Message]) {
        let internal = || {
            let questions = Box::pin(Questions::new(messages)?);
            let mut response_pointer = std::ptr::null_mut();
            // SAFETY: We're calling into PAM with valid everything.
            let result = unsafe {
                (self.callback)(
                    messages.len() as c_int,
                    questions.as_ref().ptr(),
                    &mut response_pointer,
                    self.appdata,
                )
            };
            ErrorCode::result_from(result)?;
            // SAFETY: This is a pointer we just got back from PAM.
            // We have to trust that the responses from PAM match up
            // with the questions we sent.
            unsafe {
                let response_pointer =
                    NonNull::new(response_pointer).ok_or(ErrorCode::ConversationError)?;
                let mut owned_responses = Answers::from_c_heap(response_pointer, messages.len());
                for (msg, response) in iter::zip(messages, owned_responses.iter_mut()) {
                    convert(msg, response);
                }
            };
            Ok(())
        };
        if let Err(e) = internal() {
            messages.iter().for_each(|m| m.set_error(e))
        }
    }
}

/// Like [`Message`], but this time we own the contents.
#[derive(Debug)]
pub enum OwnedMessage<'a> {
    MaskedPrompt(MaskedQAndA<'a>),
    Prompt(QAndA<'a>),
    Info(InfoMsg<'a>),
    Error(ErrorMsg<'a>),
    RadioPrompt(RadioQAndA<'a>),
    BinaryPrompt(BinaryQAndA<'a>),
}

impl<'a> TryFrom<&'a OwnedMessage<'a>> for Message<'a> {
    type Error = ErrorCode;
    fn try_from(src: &'a OwnedMessage) -> StdResult<Self, ErrorCode> {
        match src {
            OwnedMessage::MaskedPrompt(m) => Ok(Message::MaskedPrompt(m)),
            OwnedMessage::Prompt(m) => Ok(Message::Prompt(m)),
            OwnedMessage::Info(m) => Ok(Message::Info(m)),
            OwnedMessage::Error(m) => Ok(Message::Error(m)),
            OwnedMessage::RadioPrompt(m) => Ok(Message::RadioPrompt(m)),
            OwnedMessage::BinaryPrompt(m) => Ok(Message::BinaryPrompt(m)),
        }
    }
}

/// Fills in the answer of the Message with the given response.
///
/// # Safety
///
/// You are responsible for ensuring that the src-dst pair matches.
unsafe fn convert(msg: &Message, resp: &mut Answer) {
    macro_rules! fill_text {
        ($dst:ident, $src:ident) => {{
            let text_resp = unsafe { TextAnswer::upcast($src) };
            $dst.set_answer(text_resp.contents().map(Into::into));
        }};
    }
    match *msg {
        Message::MaskedPrompt(qa) => fill_text!(qa, resp),
        Message::Prompt(qa) => fill_text!(qa, resp),
        Message::Error(m) => m.set_answer(Ok(())),
        Message::Info(m) => m.set_answer(Ok(())),
        Message::RadioPrompt(qa) => fill_text!(qa, resp),
        Message::BinaryPrompt(qa) => {
            let bin_resp = unsafe { BinaryAnswer::upcast(resp) };
            qa.set_answer(Ok(bin_resp
                .data()
                .map(|d| unsafe { CBinaryData::as_binary_data(d) })
                .unwrap_or_default()));
            bin_resp.zero_contents()
        }
    }
}